Guest User

Untitled

a guest
Jun 12th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. <?php
  2. use Magento\Framework\App\Bootstrap;
  3.  
  4. require __DIR__ . '/app/bootstrap.php';
  5. $local_file = '/srv/public_html/var/log/Kroll_Stock_Disco_CLSO.txt';
  6. $stock_inventory_file = '/srv/public_html/var/log/Kroll_Stock_Inventory.txt';
  7. //$local_file = '/tmp/kroll_test.csv';
  8. $inventory_file = '/ProductData/Kroll_Stock_Inventory.txt';
  9. $clso_disc_file = '/ProductData/Kroll_Stock_Disco_CLSO.txt';
  10. $params = $_SERVER;
  11.  
  12. $bootstrap = Bootstrap::create(BP, $params);
  13.  
  14. $objectManager = $bootstrap->getObjectManager();
  15. $state = $objectManager->get('Magento\Framework\App\State');
  16. $state->setAreaCode('frontend');
  17. $productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
  18. $collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
  19. $ftp_server = "ftp.krollcorp.com";
  20. $conn = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
  21. if (ftp_login($conn, '25245', '5!b3f258'))
  22. {
  23. ftp_pasv($conn, true);
  24. if (ftp_get($conn, $local_file, $clso_disc_file, FTP_BINARY)) {
  25. echo "Successfully written to $local_file\n";
  26. } else {
  27. send_email("rai@rltsquare.com,jarod@911custom.com", "Error in copying clso file from ftp", "Error in copying files from ftp");
  28. echo "There was a problem\n";
  29. }
  30. }
  31.  
  32. $first_loop = 0;
  33. $second_loop = 0;
  34. $file = $local_file;
  35. $message = "";
  36. $arrClso = array();
  37. $arrDisc= array();
  38. $arrLimitedAvailability = array();
  39. $arrInStock = array();
  40. $headers = false;
  41. $handle = fopen($file, "r");
  42. $inventory_file = fopen($stock_inventory_file, "r");
  43. $startTime ="17:10:00";
  44. $endTime ="17:20";
  45. echo time()."\n";
  46. echo strtotime($startTime)."\n";
  47. echo strtotime($endTime)."\n";
  48. echo time();
  49. //die();
  50. send_sms_to_rai('started');
  51. if (1==1) {
  52. while (!feof($handle)) {
  53. $first_loop++;
  54. echo "\nUpdating CLSO/DISC-->".$first_loop."--";
  55. $line = fgets($handle);
  56. $line_arr = explode("\t", $line);
  57. $sku = @$line_arr[0];
  58. if ($sku==""){continue;}
  59. echo $line."\n";
  60. $qty = @$line_arr[1];//if qty is 0, product is DISC else its on CLSO
  61. try {
  62.  
  63.  
  64.  
  65. $product = $productRepository->get($sku);
  66. if (!$product){echo "product not found\n";}
  67. } catch (Exception $e) {
  68. echo "Exception during fetching product in CLSO \n";
  69. continue;
  70. }
  71. if ($product->getTypeId()!="simple"){echo 'configurable product';continue;}
  72. echo $product->getId()."\n";
  73. echo $product->getSku()."\n";
  74. $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
  75. $stockItem=$stockRegistry->getStockItem($product->getId());
  76. $db_mng_stock = $stockItem->getData('manage_stock');
  77. $db_mng_stock_cnfg = $stockItem->getData('use_config_manage_stock');
  78. $db_qty = (int)$stockItem->getData('qty');
  79. $db_is_in_stock = (int)$stockItem->getData('is_in_stock');
  80.  
  81. if ((int)$qty == 0){//product is discontinued
  82.  
  83. echo "Product is DISC \n";
  84. if ($db_qty != 0 || $db_is_in_stock != 0 || $db_mng_stock_cnfg != 1 || $db_mng_stock != 1 ){
  85. echo "\nUpdating product status to make it DISC\n";
  86. $product->setStockData(['qty' => 0, 'is_in_stock' => 0, 'use_config_manage_stock' => 1, 'manage_stock' => 1]);
  87. $product->setVisibility(1);
  88. $product->save();
  89. }else{
  90. $product->setVisibility(1);
  91. $product->save();
  92. echo "Already up to date\n";
  93. }
  94. array_push($arrDisc, $sku);
  95. } else {//limited availability because the product is on CLSO
  96. echo "Product is on CLSO \n";
  97. if ($db_qty != 0 || $db_is_in_stock != 1 || $db_mng_stock_cnfg != 0 || $db_mng_stock != 1 ){
  98. $product->setStockData(['qty' => 0, 'is_in_stock' => 1, 'use_config_manage_stock' => 0, 'manage_stock' => 1]);
  99. $product->save();
  100. }else{
  101. echo "Already up to date\n";
  102. }
  103. array_push($arrClso, $sku);
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. $productcollection =$collection->addAttributeToSelect('*')
  113. ->addAttributeToFilter(
  114. [
  115. ['attribute'=>'sku','like'=> "$sku^%"]
  116. ]
  117. );
  118. foreach ($productcollection as $product) {
  119. $_product = $productRepository->get($product->getSku() );
  120. if ($_product->getTypeId()!="simple"){echo 'configurable product';continue;}
  121. echo $_product->getId()."\n";
  122. echo $_product->getSku()."\n";
  123. $stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
  124. $stockItem=$stockRegistry->getStockItem($_product->getId());
  125. $db_mng_stock = $stockItem->getData('manage_stock');
  126. $db_mng_stock_cnfg = $stockItem->getData('use_config_manage_stock');
  127. $db_qty = (int)$stockItem->getData('qty');
  128. $db_is_in_stock = (int)$stockItem->getData('is_in_stock');
  129. if ((int)$qty == 0){//product is discontinued
  130. echo "Product is DISC \n";
  131. if ($db_qty != 0 || $db_is_in_stock != 0 || $db_mng_stock_cnfg != 1 || $db_mng_stock != 1 ){
  132. echo "\nUpdating product status to make it DISC\n";
  133. $_product->setStockData(['qty' => 0, 'is_in_stock' => 0, 'use_config_manage_stock' => 1, 'manage_stock' => 1]);
  134. $_product->setVisibility(1);
  135. $_product->save();
  136. }else{
  137. $_product->setVisibility(1);
  138. $_product->save();
  139. echo "Already up to date\n";
  140. }
  141. array_push($arrDisc, $sku);
  142. } else {//limited availability because the product is on CLSO
  143. echo "Product is on CLSO \n";
  144. if ($db_qty != 0 || $db_is_in_stock != 1 || $db_mng_stock_cnfg != 0 || $db_mng_stock != 1 ){
  145. $_product->setStockData(['qty' => 0, 'is_in_stock' => 1, 'use_config_manage_stock' => 0, 'manage_stock' => 1]);
  146. $_product->save();
  147. }else{
  148. echo "Already up to date\n";
  149. }
  150. array_push($arrClso, $sku);
  151. }
  152. }
  153. }
  154. //die();
  155.  
  156. $message = "Products on Closeout \n";
  157. foreach ($arrClso as $arrItem) {
  158. $message .=$arrItem."\n";
  159. }
  160. $message .= "\nDiscontinued Products \n";
  161. foreach ($arrDisc as $arrItem) {
  162. $message .=$arrItem."\n";
  163. }
  164. }
  165.  
  166.  
  167.  
  168.  
  169. send_email("rai@rltsquare.com,jarod@911custom.com", "Kroll CLSO/DISC Product Stock Statuses", $message);
  170. send_sms_to_rai('success');
  171. function send_sms_to_rai($status){
  172. if ($status=='success'){
  173. $ch = curl_init();
  174. $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-Updated";
  175. curl_setopt($ch, CURLOPT_URL, $url_content);
  176. curl_setopt($ch, CURLOPT_HEADER, 0);
  177. curl_exec($ch);
  178. curl_close($ch);
  179. }
  180. if ($status=='error'){
  181. $ch = curl_init();
  182. $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-UpdateFailed";
  183. curl_setopt($ch, CURLOPT_URL, $url_content);
  184. curl_setopt($ch, CURLOPT_HEADER, 0);
  185. curl_exec($ch);
  186. curl_close($ch);
  187. }
  188. if ($status=='started'){
  189. $ch = curl_init();
  190. $url_content = "http://65.98.91.178:13013/cgi-bin/sendsms?username=smsall&password=smsall&to=%2B923214075192&from=800111&text=CLSO-UpdateStarted";
  191. curl_setopt($ch, CURLOPT_URL, $url_content);
  192. curl_setopt($ch, CURLOPT_HEADER, 0);
  193. curl_exec($ch);
  194. curl_close($ch);
  195. }
  196. return true;
  197. }
  198.  
  199. function send_email($mailto, $subject, $message){
  200. $uid = md5(uniqid(time()));
  201. $header = "From: 911Custom Admin <noreply@911custom.com>\r\n";
  202. $header .= "Reply-To: admin@911custom.com\r\n";
  203. $header .= "MIME-Version: 1.0\r\n";
  204. $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  205. if (mail($mailto, $subject, $message, $header)) {
  206. echo 'sent';
  207. return true; // Or do something here
  208. } else {
  209. echo 'not sent';
  210. return false;
  211. }
  212. }
  213. function is_on_clso($group){
  214. if ($group=="CLSO"){
  215. return true;
  216. }
  217. return false;
  218. }
  219.  
  220. ?>
Add Comment
Please, Sign In to add comment