public function prepareFeed() { $this->echomsg("Start - New Feed Preparation"); // select all EXLUSIVE products added since the last feed $sql = $this->getProductsSQL(); //echo $sql; exit; $products = $this->db->ExecuteS($sql); //print_r($products); if (count($products) === 0) { $this->echomsg("No New Products pending to be added"); $this->echomsg("End - Feed Preparation"); return; } //Prepare SKUs $skus = $this->getSKUs($products); $this->echomsg("SKUs Count"); $this->echomsg(count($skus)); $prepareFeed = false; $atleastOneFailed = false; $atleastOneProdFailed = false; $atleastOnePriceFailed = false; $atleastOneInvFailed = false; $atleastOneImageFailed = false; $atleastOneProdSuccess = false; $atleastOnePriceSuccess = false; $atleastOneInvSuccess = false; $atleastOneImageSuccess = false; $newProductsAdded = array(); foreach ($skus as &$sku) { $id_product = $sku["id_product"]; $id_sku = $sku['id_sku']; $this->echomsg("Working on SKU #{$id_sku}"); $is_parent = $sku["is_parent"]; $has_children = empty($sku["attribute"]) ? false : true; $size = empty($sku["attribute"]) ? "One Size" : $sku["attribute"]["attribute_name"]; $force_inactive = isset($sku['inactive']) && (int) $sku['inactive'] === 1 ? true : false; $priceData = null; $inventoryData = null; $imageDataArray = null; $amazonProduct = null; $deleted = false; $delete_list = array(); $build_relationship_feed = false; $allSubFeedsSuccess = true; $divaProduct = new Product($id_product, true, 1); $this->echomsg("SKU #{$id_sku} - Start"); //check to see if the product is on amazon already $sql = "select * from ps_affiliate_feed_product_info where id_sku = '{$id_sku}'"; // $sql = "select * from ps_affiliate_feed_product_info where id_sku = 'ID-58709'"; $res = $this->db->getRow($sql); if ($is_parent === false && $has_children === false) { $new_price = (int) round($divaProduct->getPriceWithoutReduct()); $new_specific_price = (int) round($divaProduct->getAffiliatePrice($this->id_affiliate)); $new_quantity = (int) Product::getQuantity($id_product); } else { if ($is_parent) { $new_price = (int) round($divaProduct->getPriceWithoutReduct()); $new_specific_price = (int) round($divaProduct->getAffiliatePrice($this->id_affiliate)); $new_quantity = (int) Product::getQuantity($id_product); } else { $new_price = (int) round($divaProduct->getPriceWithoutReduct(false, $sku['attribute']['id_product_attribute'])); $new_specific_price = (int) round($divaProduct->getAffiliatePrice($this->id_affiliate, true, $sku['attribute']['id_product_attribute'])); $new_quantity = (int) Product::getQuantity($id_product, $sku['attribute']['id_product_attribute']); $build_relationship_feed = true; } } if (!empty($res)) { $new_shipping_sla = (int) $divaProduct->shipping_sla; $new_active = (int) $divaProduct->active; if ($force_inactive) { $new_active = 0; } $new_exclusive = (int) $divaProduct->is_exclusive; $old_price = (int) $res['price']; $old_specific_price = (int) $res['specific_price']; $old_quantity = (int) $res['quantity']; $old_shipping_sla = (int) $res['shipping_sla']; $old_active = (int) $res['active']; $old_exclusive = (int) $res['exclusive']; if ($new_exclusive === 0) { $this->echomsg("SKU #{$id_sku} - ... Preparing Delete product"); $this->initMainFeed(); $id_fp = MarketplaceWebService_DB::newFeedProduct($this->id_feed, $id_product, $id_sku); $this->initSubFeed(MarketplaceWebService_DB::$PRODUCT_FEED); $op_type = 'D'; $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_prod_feed, $id_product, $id_sku, $op_type); try { $amazonProduct = $this->getDeleteObj($divaProduct, $sku); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneProdSuccess = true; $deleted = true; $this->echomsg("SKU #{$id_sku} - ... Success"); $sql = "delete from ps_affiliate_feed_product_info where id_sku = '{$id_sku}'"; array_push($delete_list, "{$id_sku}"); $this->db->ExecuteS($sql); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $atleastOneProdFailed = true; $allSubFeedsSuccess = false; $this->echomsg("SKU #{$id_sku} - ... Failed"); } } else { $change = false; if ($new_active === 0 || $new_quantity < 1) { //make product quantity 0 (inactive on amazon) $this->echomsg("SKU #{$id_sku} - ... Preparing Inventory Feed(Quantity 0)"); $this->initMainFeed(); $id_fp = MarketplaceWebService_DB::newFeedProduct($this->id_feed, $id_product, $id_sku); $this->initSubFeed(MarketplaceWebService_DB::$INVENTORY_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_inv_feed, $id_product, $id_sku); try { $inventoryData = $this->getInventoryObj($divaProduct, $sku, $new_quantity, $new_shipping_sla, true); $date_upd = date('Y-m-d H:i:s'); $sql = "update ps_affiliate_feed_product_info set quantity = {$new_quantity},active = {$new_active},date_upd = '{$date_upd}' where id_sku = '{$id_sku}'"; $this->db->ExecuteS($sql); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneInvSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOneInvFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } $change = true; } $make_active = false; if ($old_active === 0 && $new_active === 1) { $make_active = true; } if ($make_active || $old_price !== $new_price || $old_specific_price !== $new_specific_price || $old_quantity !== $new_quantity || $old_shipping_sla !== $new_shipping_sla) { // Price and Inventory data on Amazon should be updated $this->initMainFeed(); $id_fp = MarketplaceWebService_DB::newFeedProduct($this->id_feed, $id_product, $id_sku); if ($old_price !== $new_price || $old_specific_price !== $new_specific_price) { $this->echomsg("SKU #{$id_sku} - ... Preparing Price Feed"); $this->echomsg("SKU #{$new_specific_price} - ...new specific price"); $this->initSubFeed(MarketplaceWebService_DB::$PRICE_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_price_feed, $id_product, $id_sku); try { $priceData = $this->getPriceObj($divaProduct, $sku, $new_price, $new_specific_price); $date_upd = date('Y-m-d H:i:s'); $sql = "update ps_affiliate_feed_product_info set price ='{$new_price}',specific_price='{$new_specific_price}', date_upd='{$date_upd}' where id_sku = '{$id_sku}'"; $this->db->ExecuteS($sql); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOnePriceSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { $allSubFeedsSuccess = false; $atleastOnePriceFailed = true; MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $this->echomsg("SKU #{$id_sku} - ... Failed"); } } if ($make_active || $old_quantity !== $new_quantity || $old_shipping_sla !== $new_shipping_sla) { $this->echomsg("SKU #{$id_sku} - ... Preparing Inventory Feed"); $this->initSubFeed(MarketplaceWebService_DB::$INVENTORY_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_inv_feed, $id_product, $id_sku); try { $inventoryData = $this->getInventoryObj($divaProduct, $sku, $new_quantity, $new_shipping_sla); $date_upd = date('Y-m-d H:i:s'); if ($make_active) { $csql = ",active = 1"; } else { $csql = ""; } $sql = "update ps_affiliate_feed_product_info set quantity = {$new_quantity}, shipping_sla = {$new_shipping_sla} {$csql}, date_upd='{$date_upd}' where id_sku = '{$id_sku}'"; $this->db->ExecuteS($sql); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneInvSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOneInvFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } } $change = true; } if (!$change) { $this->echomsg("SKU #{$id_sku} - ... No change in price and inventory - skip"); $this->echomsg("SKU #{$id_sku} - End"); continue; } } } else { //this product is not on amazon - push full feed // if( (((int)$divaProduct->active) === 0) || (((int)$divaProduct->is_exclusive) === 0) || ($divaProduct->quantity < 1) ) { if ((int) $divaProduct->active === 0 || (int) $divaProduct->is_exclusive === 0) { $this->echomsg("SKU #{$id_sku} - ... Not Active or Not Exclusive product"); $this->echomsg("SKU #{$id_sku} - End"); continue; } $this->initMainFeed(); $id_fp = MarketplaceWebService_DB::newFeedProduct($this->id_feed, $id_product, $id_sku); try { $this->echomsg("SKU #{$id_sku} - ... Preparing Product Feed"); $this->initSubFeed(MarketplaceWebService_DB::$PRODUCT_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_prod_feed, $id_product, $id_sku); $amazonProduct = $this->getProductObj($divaProduct, $sku); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneProdSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOneProdFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } try { $this->echomsg("SKU #{$id_sku} - ... Preparing Image Feed"); $this->initSubFeed(MarketplaceWebService_DB::$IMAGE_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_image_feed, $id_product, $id_sku); $imageDataArray = $this->getImageObj($divaProduct, $sku); if (count($imageDataArray) === 0) { throw new Exception("No Image(s) available for this Product"); } MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneImageSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOneImageFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } $price = null; $specific_price = null; try { $this->echomsg("SKU #{$id_sku} - ... Preparing Price Feed"); $this->initSubFeed(MarketplaceWebService_DB::$PRICE_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_price_feed, $id_product, $id_sku); $priceData = $this->getPriceObj($divaProduct, $sku, $price, $specific_price); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOnePriceSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOnePriceFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } $quantity = null; $shipping_sla = null; try { $this->echomsg("SKU #{$id_sku} - ... Preparing Inventory Feed"); $this->initSubFeed(MarketplaceWebService_DB::$INVENTORY_FEED); $id_sfp = MarketplaceWebService_DB::newSubFeedProduct($this->id_inv_feed, $id_product, $id_sku); $inventoryData = $this->getInventoryObj($divaProduct, $sku, $quantity, $shipping_sla); MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $atleastOneInvSuccess = true; $this->echomsg("SKU #{$id_sku} - ... Success"); } catch (Exception $ex) { MarketplaceWebService_DB::updateSubFeedProduct($id_sfp, MarketplaceWebService_DB::$STATUS_PREP_FAILED, $ex->getMessage()); $allSubFeedsSuccess = false; $atleastOneInvFailed = true; $this->echomsg("SKU #{$id_sku} - ... Failed"); } if ($allSubFeedsSuccess) { if (!in_array($id_product, $newProductsAdded)) { array_push($newProductsAdded, $id_product); } //Make a note that this product is on amazon $date_add = date('Y-m-d H:i:s'); $date_upd = date('Y-m-d H:i:s'); $sql = "insert into ps_affiliate_feed_product_info(id_product, id_sku, price, specific_price, quantity, shipping_sla, active, exclusive, date_add, date_upd) \n values({$id_product}, '{$id_sku}',{$price},{$specific_price},{$quantity}, {$shipping_sla},1,1,'{$date_add}','{$date_upd}')"; $this->db->ExecuteS($sql); } } if ($allSubFeedsSuccess) { if ($amazonProduct !== null) { $this->initFeedObject('Product'); if ($deleted) { $this->productFeed->addProduct($amazonProduct, 'Delete'); } else { $this->productFeed->addProduct($amazonProduct); } } if ($inventoryData !== null) { $this->initFeedObject('Inventory'); $this->inventoryFeed->addInventory($inventoryData); } if ($priceData !== null) { $this->initFeedObject('Price'); $this->priceFeed->addPrice($priceData); } if ($imageDataArray !== null) { $this->initFeedObject('ProductImage'); foreach ($imageDataArray as $imageData) { $this->imageFeed->addImage($imageData); } } MarketplaceWebService_DB::updateFeedProduct($id_fp, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); $prepareFeed = true; $this->echomsg("SKU #{$id_sku} - ... All Feeds Success"); } else { //Atleast one feed failed for this product //Do not push this product into XML Feed $atleastOneFailed = true; MarketplaceWebService_DB::updateFeedProduct($id_fp, MarketplaceWebService_DB::$STATUS_PREP_FAILED); $this->echomsg("SKU #{$id_sku} - ... Atleast One Feed Failed"); } $this->echomsg("SKU #{$id_sku} - End"); unset($divaProduct); } // End foreach products $id = time() . date('Y-m-d'); if ($prepareFeed) { $data = array(); //Prepare Feed Files if ($this->productFeed instanceof MarketplaceWebService_XML_Feed_Product) { $productFeedFile = "xmlfeeds/products-{$id}.xml"; $this->productFeed->save($productFeedFile); $data['feed_file'] = $productFeedFile; if ($atleastOneProdFailed) { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_PARTIAL_SUCCESS; } else { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_SUCCESS; } MarketplaceWebService_DB::updateSubFeedStatus($this->id_prod_feed, $data); } if ($this->priceFeed instanceof MarketplaceWebService_XML_Feed_Price) { $priceFeedFile = "xmlfeeds/products-price-{$id}.xml"; $this->priceFeed->save($priceFeedFile); $data['feed_file'] = $priceFeedFile; if ($atleastOnePriceFailed) { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_PARTIAL_SUCCESS; } else { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_SUCCESS; } MarketplaceWebService_DB::updateSubFeedStatus($this->id_price_feed, $data); } if ($this->inventoryFeed instanceof MarketplaceWebService_XML_Feed_Inventory) { $inventoryFeedFile = "xmlfeeds/products-inventory-{$id}.xml"; $this->inventoryFeed->save($inventoryFeedFile); $data['feed_file'] = $inventoryFeedFile; if ($atleastOneInvFailed) { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_PARTIAL_SUCCESS; } else { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_SUCCESS; } MarketplaceWebService_DB::updateSubFeedStatus($this->id_inv_feed, $data); } if ($this->imageFeed instanceof MarketplaceWebService_XML_Feed_Image) { $imageFeedFile = "xmlfeeds/products-images-{$id}.xml"; $this->imageFeed->save($imageFeedFile); $data['feed_file'] = $imageFeedFile; if ($atleastOneImageFailed) { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_PARTIAL_SUCCESS; } else { $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_SUCCESS; } MarketplaceWebService_DB::updateSubFeedStatus($this->id_image_feed, $data); } if ($atleastOneFailed) { MarketplaceWebService_DB::updateFeedStatus($this->id_feed, MarketplaceWebService_DB::$STATUS_PREP_PARTIAL_SUCCESS); } else { MarketplaceWebService_DB::updateFeedStatus($this->id_feed, MarketplaceWebService_DB::$STATUS_PREP_SUCCESS); } } else { MarketplaceWebService_DB::updateFeedStatus($this->id_feed, MarketplaceWebService_DB::$STATUS_PREP_FAILED); MarketplaceWebService_DB::updateSubFeedStatus($this->id_prod_feed, array('status' => MarketplaceWebService_DB::$STATUS_PREP_FAILED)); MarketplaceWebService_DB::updateSubFeedStatus($this->id_price_feed, array('status' => MarketplaceWebService_DB::$STATUS_PREP_FAILED)); MarketplaceWebService_DB::updateSubFeedStatus($this->id_inv_feed, array('status' => MarketplaceWebService_DB::$STATUS_PREP_FAILED)); MarketplaceWebService_DB::updateSubFeedStatus($this->id_image_feed, array('status' => MarketplaceWebService_DB::$STATUS_PREP_FAILED)); } //If applicable, build relationship for this SKU if (count($newProductsAdded) > 0) { foreach ($newProductsAdded as $id_product) { $csql = "select id_product, id_sku from ps_affiliate_feed_product_info where id_product = {$id_product}"; $cres = $this->db->ExecuteS($csql); if (count($cres) > 1) { $id_parent_sku = null; $child_skus = array(); foreach ($cres as $res) { $id_parent_sku = "ID-" . $res['id_product']; $id_sku = $res['id_sku']; if ($id_parent_sku !== $id_sku) { array_push($child_skus, $id_sku); } } $relationshipData = null; $this->echomsg("SKU #{$id_parent_sku} - ... Preparing Relationship Feed"); if ($id_parent_sku !== null && count($child_skus) > 0) { $relationshipData = $this->getRelationshipObj($id_parent_sku, $child_skus); } $this->echomsg("SKU #{$id_parent_sku} - ... Success"); if ($relationshipData !== null) { $this->initFeedObject('Relationship'); $this->relFeed->addRelationship($relationshipData); } } } if ($this->relFeed instanceof MarketplaceWebService_XML_Feed_Relationship) { $this->initSubFeed(MarketplaceWebService_DB::$RELATIONSHIP_FEED); $relFeedFile = "xmlfeeds/products-relationship-{$id}.xml"; $this->relFeed->save($relFeedFile); $data['feed_file'] = $relFeedFile; $data['status'] = MarketplaceWebService_DB::$STATUS_PREP_SUCCESS; MarketplaceWebService_DB::updateSubFeedStatus($this->id_rel_feed, $data); } } $n = false; //Notify all the updates $mail_text = ""; if (count($delete_list)) { $n = true; } $mail_text .= "<h1>Deleted List</h1>"; $mail_text .= "<ul><li>" . implode("</li><li>", $delete_list) . "</li></ul>"; $mail_text .= "<h1>Updated List</h1>"; $sql = "select id_sku, price, specific_price,quantity, shipping_sla,active, exclusive from ps_affiliate_feed_product_info where date_upd > '" . $this->date_res['last_date_add'] . "' order by id_sku desc"; $skus = $this->db->ExecuteS($sql); $mail_text .= '<table border="1" style="border-collapse: collapse;">'; $mail_text .= '<tr>'; $mail_text .= '<td>SKU</td>'; $mail_text .= '<td>Price</td>'; $mail_text .= '<td>Discount Price</td>'; $mail_text .= '<td>Quantity</td>'; $mail_text .= '<td>Shipping SLA</td>'; $mail_text .= '<td>Active</td>'; $mail_text .= '<td>Exclusive</td>'; $mail_text .= '</tr>'; foreach ($skus as $u) { $n = true; $mail_text .= '<tr>'; $mail_text .= '<td>' . $u['id_sku'] . '</td>'; $mail_text .= '<td>' . $u['price'] . '</td>'; $mail_text .= '<td>' . $u['specific_price'] . '</td>'; $mail_text .= '<td>' . $u['quantity'] . '</td>'; $mail_text .= '<td>' . $u['shipping_sla'] . '</td>'; $mail_text .= '<td>' . $u['active'] . '</td>'; $mail_text .= '<td>' . $u['exclusive'] . '</td>'; $mail_text .= '</tr>'; } $mail_text .= '</table>'; $mail_text .= "<h1>New Additions</h1>"; $sql = "select id_sku, price, specific_price,quantity, shipping_sla,active, exclusive from ps_affiliate_feed_product_info where date_add > '" . $this->date_res['last_date_add'] . "' order by id_sku desc"; $skus = $this->db->ExecuteS($sql); $mail_text .= '<table border="1" style="border-collapse: collapse;">'; $mail_text .= '<tr>'; $mail_text .= '<td>SKU</td>'; $mail_text .= '<td>Price</td>'; $mail_text .= '<td>Discount Price</td>'; $mail_text .= '<td>Quantity</td>'; $mail_text .= '<td>Shipping SLA</td>'; $mail_text .= '<td>Active</td>'; $mail_text .= '<td>Exclusive</td>'; $mail_text .= '</tr>'; foreach ($skus as $u) { $n = true; $mail_text .= '<tr>'; $mail_text .= '<td>' . $u['id_sku'] . '</td>'; $mail_text .= '<td>' . $u['price'] . '</td>'; $mail_text .= '<td>' . $u['specific_price'] . '</td>'; $mail_text .= '<td>' . $u['quantity'] . '</td>'; $mail_text .= '<td>' . $u['shipping_sla'] . '</td>'; $mail_text .= '<td>' . $u['active'] . '</td>'; $mail_text .= '<td>' . $u['exclusive'] . '</td>'; $mail_text .= '</tr>'; } $mail_text .= '</table>'; if ($n) { $templateVars = array('event' => 'Amazon Stock Sync Updates', 'description' => $mail_text); @Mail::Send(1, 'alert', Mail::l('Amazon Stock Sync Update'), $templateVars, array("*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**"), null, '*****@*****.**', 'Indusdiva Monitoring', NULL, NULL, _PS_MAIL_DIR_, false); } }