Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Magento\Framework\App\Bootstrap;
- require __DIR__ . '/app/bootstrap.php';
- $local_file = '/srv/public_html/var/log/Kroll_Stock_Disco_CLSO.txt';
- $stock_inventory_file = '/srv/public_html/var/log/Kroll_Stock_Inventory.txt';
- //$local_file = '/tmp/kroll_test.csv';
- $inventory_file = '/ProductData/Kroll_Stock_Inventory.txt';
- $clso_disc_file = '/ProductData/Kroll_Stock_Disco_CLSO.txt';
- $params = $_SERVER;
- $bootstrap = Bootstrap::create(BP, $params);
- $objectManager = $bootstrap->getObjectManager();
- $state = $objectManager->get('Magento\Framework\App\State');
- $state->setAreaCode('frontend');
- $productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
- $collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
- $ftp_server = "ftp.krollcorp.com";
- $conn = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
- if (ftp_login($conn, '25245', '5!b3f258'))
- {
- ftp_pasv($conn, true);
- if (ftp_get($conn, $local_file, $clso_disc_file, FTP_BINARY)) {
- echo "Successfully written to $local_file\n";
- } else {
- send_email("rai@rltsquare.com,jarod@911custom.com", "Error in copying clso file from ftp", "Error in copying files from ftp");
- echo "There was a problem\n";
- }
- }
- $first_loop = 0;
- $second_loop = 0;
- $file = $local_file;
- $message = "";
- $arrClso = array();
- $arrDisc= array();
- $arrLimitedAvailability = array();
- $arrInStock = array();
- $headers = false;
- $handle = fopen($file, "r");
- $inventory_file = fopen($stock_inventory_file, "r");
- $startTime ="17:10:00";
- $endTime ="17:20";
- echo time()."\n";
- echo strtotime($startTime)."\n";
- echo strtotime($endTime)."\n";
- echo time();
- //die();
- send_sms_to_rai('started');
- if (1==1) {
- while (!feof($handle)) {
- $first_loop++;
- echo "\nUpdating CLSO/DISC-->".$first_loop."--";
- $line = fgets($handle);
- $line_arr = explode("\t", $line);
- $sku = @$line_arr[0];
- if ($sku==""){continue;}
- echo $line."\n";
- $qty = @$line_arr[1];//if qty is 0, product is DISC else its on CLSO
- try {
- $product = $productRepository->get($sku);
- if (!$product){echo "product not found\n";}
- } catch (Exception $e) {
- echo "Exception during fetching product in CLSO \n";
- continue;
- }
- if ($product->getTypeId()!="simple"){echo 'configurable product';continue;}
- echo $product->getId()."\n";
- echo $product->getSku()."\n";
- $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
- $stockItem=$stockRegistry->getStockItem($product->getId());
- $db_mng_stock = $stockItem->getData('manage_stock');
- $db_mng_stock_cnfg = $stockItem->getData('use_config_manage_stock');
- $db_qty = (int)$stockItem->getData('qty');
- $db_is_in_stock = (int)$stockItem->getData('is_in_stock');
- if ((int)$qty == 0){//product is discontinued
- echo "Product is DISC \n";
- if ($db_qty != 0 || $db_is_in_stock != 0 || $db_mng_stock_cnfg != 1 || $db_mng_stock != 1 ){
- echo "\nUpdating product status to make it DISC\n";
- $product->setStockData(['qty' => 0, 'is_in_stock' => 0, 'use_config_manage_stock' => 1, 'manage_stock' => 1]);
- $product->setVisibility(1);
- $product->save();
- }else{
- $product->setVisibility(1);
- $product->save();
- echo "Already up to date\n";
- }
- array_push($arrDisc, $sku);
- } else {//limited availability because the product is on CLSO
- echo "Product is on CLSO \n";
- if ($db_qty != 0 || $db_is_in_stock != 1 || $db_mng_stock_cnfg != 0 || $db_mng_stock != 1 ){
- $product->setStockData(['qty' => 0, 'is_in_stock' => 1, 'use_config_manage_stock' => 0, 'manage_stock' => 1]);
- $product->save();
- }else{
- echo "Already up to date\n";
- }
- array_push($arrClso, $sku);
- }
- $productcollection =$collection->addAttributeToSelect('*')
- ->addAttributeToFilter(
- [
- ['attribute'=>'sku','like'=> "$sku^%"]
- ]
- );
- foreach ($productcollection as $product) {
- $_product = $productRepository->get($product->getSku() );
- if ($_product->getTypeId()!="simple"){echo 'configurable product';continue;}
- echo $_product->getId()."\n";
- echo $_product->getSku()."\n";
- $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
- $stockItem=$stockRegistry->getStockItem($_product->getId());
- $db_mng_stock = $stockItem->getData('manage_stock');
- $db_mng_stock_cnfg = $stockItem->getData('use_config_manage_stock');
- $db_qty = (int)$stockItem->getData('qty');
- $db_is_in_stock = (int)$stockItem->getData('is_in_stock');
- if ((int)$qty == 0){//product is discontinued
- echo "Product is DISC \n";
- if ($db_qty != 0 || $db_is_in_stock != 0 || $db_mng_stock_cnfg != 1 || $db_mng_stock != 1 ){
- echo "\nUpdating product status to make it DISC\n";
- $_product->setStockData(['qty' => 0, 'is_in_stock' => 0, 'use_config_manage_stock' => 1, 'manage_stock' => 1]);
- $_product->setVisibility(1);
- $_product->save();
- }else{
- $_product->setVisibility(1);
- $_product->save();
- echo "Already up to date\n";
- }
- array_push($arrDisc, $sku);
- } else {//limited availability because the product is on CLSO
- echo "Product is on CLSO \n";
- if ($db_qty != 0 || $db_is_in_stock != 1 || $db_mng_stock_cnfg != 0 || $db_mng_stock != 1 ){
- $_product->setStockData(['qty' => 0, 'is_in_stock' => 1, 'use_config_manage_stock' => 0, 'manage_stock' => 1]);
- $_product->save();
- }else{
- echo "Already up to date\n";
- }
- array_push($arrClso, $sku);
- }
- }
- }
- //die();
- $message = "Products on Closeout \n";
- foreach ($arrClso as $arrItem) {
- $message .=$arrItem."\n";
- }
- $message .= "\nDiscontinued Products \n";
- foreach ($arrDisc as $arrItem) {
- $message .=$arrItem."\n";
- }
- }
- send_email("rai@rltsquare.com,jarod@911custom.com", "Kroll CLSO/DISC Product Stock Statuses", $message);
- send_sms_to_rai('success');
- function send_sms_to_rai($status){
- if ($status=='success'){
- $ch = curl_init();
- $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-Updated";
- curl_setopt($ch, CURLOPT_URL, $url_content);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- curl_close($ch);
- }
- if ($status=='error'){
- $ch = curl_init();
- $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-UpdateFailed";
- curl_setopt($ch, CURLOPT_URL, $url_content);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- curl_close($ch);
- }
- if ($status=='started'){
- $ch = curl_init();
- $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-UpdateStarted";
- curl_setopt($ch, CURLOPT_URL, $url_content);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- curl_close($ch);
- }
- return true;
- }
- function send_email($mailto, $subject, $message){
- $uid = md5(uniqid(time()));
- $header = "From: 911Custom Admin <noreply@911custom.com>\r\n";
- $header .= "Reply-To: admin@911custom.com\r\n";
- $header .= "MIME-Version: 1.0\r\n";
- $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
- if (mail($mailto, $subject, $message, $header)) {
- echo 'sent';
- return true; // Or do something here
- } else {
- echo 'not sent';
- return false;
- }
- }
- function is_on_clso($group){
- if ($group=="CLSO"){
- return true;
- }
- return false;
- }
- ?>
Add Comment
Please, Sign In to add comment