Example #1
0
             if (array_search($tag, $keep_tags)) {
                 array_push($new_tags, $tag);
             }
         }
         $new_tags = implode(",", $new_tags);
         if (strlen($new_tags) <= 0) {
             $new_tags = $tags;
         } else {
             $new_tags = $new_tags . "," . $tags;
         }
         $product->description_short[1] = $new_tags;
     } else {
         $product->description_short[1] = $tags;
     }
     $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true);
     $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true);
     if (!empty($fieldError)) {
         $product->update();
     } else {
         $errors[] = "<br/>{$id_product} : " . $langFieldError;
     }
     $count++;
     array_push($product_ids, $id_product);
 }
 fclose($f);
 SolrSearch::updateProducts($product_ids);
 echo "Total {$count} rows processed";
 echo "<br/>";
 echo "<a href='http://www.indusdiva.com/products/{$tagname}'>link</a>";
 echo "<br/>" . "--------------------------------------------------";
 echo "<br/>Errors";
 public function handleConfirm($update = false)
 {
     global $currentIndex, $cookie, $smarty;
     $products_to_import = array();
     $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT');
     $file_path = Tools::getValue('current_file');
     $overwrite_imgs = Tools::getValue("overwrite_imgs");
     //$file_path = '/Users/rohit/webroot/indusdiva/admin12/product-uploads/upload_sheet_1.csv';
     $f = fopen($file_path, 'r');
     $file_error = false;
     if ($f) {
         //discard header
         $line = fgetcsv($f);
         while ($line = fgetcsv($f)) {
             //ignore empty lines
             if (empty($line)) {
                 continue;
             }
             //trim data
             foreach ($line as $key => $value) {
                 $line[$key] = trim($value);
             }
             $id_product = $line[0];
             $images = $line[1];
             $product_name = $line[2];
             $fabric = $line[3];
             $color = $line[4];
             $mrp = $line[5];
             $supplier_code = $line[6];
             $reference = $line[7];
             $location = $line[8];
             $length = $line[9];
             $width = $line[10];
             $blouse_length = $line[11];
             $garment_type = $line[12];
             $work_type = $line[13];
             $weight = $line[14];
             $description = $line[15];
             $other_info = $line[16];
             $wash_care = $line[17];
             $shipping_estimate = $line[18];
             $supplier_price = $line[19];
             $manufacturer = $line[20];
             $categories = explode(",", $line[21]);
             $tax_rule = $line[22];
             $quantity = $line[23];
             $active = $line[24];
             $discount = $line[25];
             $tags = $line[26];
             $kameez_style = $line[27];
             $salwar_style = $line[28];
             $sleeves = $line[29];
             $customizable = $line[30];
             $generic_color = $line[31];
             $skirt_length = $line[32];
             $dupatta_length = $line[33];
             $stone = $line[34];
             $plating = $line[35];
             $material = $line[36];
             $dimensions = $line[37];
             $look = $line[38];
             $as_shown = isset($line[39]) && !empty($line[39]) ? intval($line[39]) : 0;
             $id_sizechart = isset($line[40]) && !empty($line[40]) ? intval($line[40]) : 0;
             $is_exclusive = isset($line[41]) && !empty($line[41]) ? intval($line[41]) : 0;
             $handbag_occasion = isset($line[42]) && !empty($line[42]) ? $line[42] : null;
             $handbag_style = isset($line[43]) && !empty($line[43]) ? $line[43] : null;
             $handbag_material = isset($line[44]) && !empty($line[44]) ? $line[44] : null;
             $images = explode(",", $images);
             $error = false;
             //validate fields
             if (!Validate::isFloat($mrp)) {
                 $error = 'MRP should be a number: ' . trim($reference);
             } elseif (!Validate::isFloat($supplier_price)) {
                 $error = 'Supplier Price should be a number: ' . trim($reference);
             }
             $importCategories = array();
             if (is_array($categories)) {
                 $categories = array_unique($categories);
                 foreach ($categories as $category) {
                     $category = intval(trim($category));
                     if (empty($category)) {
                         continue;
                     }
                     if (!is_numeric($category) || !Category::categoryExists($category)) {
                         $error = 'Category does not exist: ' . $category;
                     }
                     $importCategories[] = $category;
                 }
             } else {
                 $error = 'Atleast one category required: ' . trim($reference);
             }
             if (!Validate::isFloat($weight)) {
                 $error = 'Weight has to be a number: ' . trim($reference);
             }
             if (!empty($manufacturer) && (!is_numeric($manufacturer) || !Manufacturer::manufacturerExists((int) $manufacturer))) {
                 $error = 'Manufacturer does not exist';
             }
             if ($quantity && !is_numeric($quantity) || $discount && !is_numeric($discount)) {
                 $error = 'Quantity and discount should be numbers: ' . trim($reference);
             }
             if (!Validate::isLoadedObject(new TaxRulesGroup($tax_rule))) {
                 $error = 'Tax rate invalid: ' . trim($reference);
             }
             if (!$update) {
                 $sql = "SELECT `reference`\n\t\t\t\t\t\t\tFROM " . _DB_PREFIX_ . "product p\n\t\t\t\t\t\t\tWHERE p.`reference` = '" . $reference . "'";
                 $row = Db::getInstance()->getRow($sql);
                 if (isset($row['reference'])) {
                     $error = "Duplicate indusdiva code : " . trim($reference);
                 }
             }
             //check for souring price
             if ($supplier_price > $mrp / 1.2) {
                 $error = "MRP too low : " . trim($reference);
             }
             //check for images
             if (!$update || $overwrite_imgs == "on") {
                 foreach ($images as $image_name) {
                     $image_name = trim($image_name);
                     $image_path = IMAGE_UPLOAD_PATH . $image_name;
                     if (!empty($image_name) && !file_exists($image_path)) {
                         $error = "Image not found for: " . trim($reference) . ", Image Name: " . $image_name;
                         break;
                     }
                 }
             }
             $vendor_code = substr($reference, 0, 6);
             $sql = "select id_supplier from ps_supplier where code = '{$vendor_code}'";
             $row = Db::getInstance()->getRow($sql);
             if (!isset($row['id_supplier'])) {
                 $error = "Vendor Details not found for : " . trim($reference);
             } else {
                 $id_supplier = $row['id_supplier'];
             }
             //For sudarshan, supplier_code (vendor product code) is mandatory
             if (false) {
                 //(int) $id_supplier === 2 ) {
                 if (empty($supplier_code)) {
                     $error = "Reference: {$reference} -- Supplier Code is Mandatory for Vendor {$vendor_code}";
                 } else {
                     if (strpos("::", ${$supplier_code}) === false) {
                         $error = "Reference: {$reference} -- Supplier Code:{$supplier_code} is not in DESIGN_NO::ITEM_CODE format for Vendor {$vendor_code}";
                     }
                 }
             }
             if (!$error) {
                 if ($update && !empty($id_product)) {
                     $product = new Product((int) $id_product);
                     if (!Validate::isLoadedObject($product)) {
                         $error = "Error loading the product: " . $id_product;
                         return;
                     }
                 } elseif (!$update) {
                     $product = new Product();
                 }
                 $product->id_tax_rules_group = $tax_rule;
                 $product->reference = $reference;
                 $product->id_supplier = $id_supplier;
                 $product->location = $location;
                 $product->tax_rate = TaxRulesGroup::getTaxesRate((int) $product->id_tax_rules_group, Configuration::get('PS_COUNTRY_DEFAULT'), 0, 0);
                 if (isset($manufacturer) and is_numeric($manufacturer) and Manufacturer::manufacturerExists((int) $manufacturer)) {
                     $product->id_manufacturer = $manufacturer;
                 }
                 $product->price = (double) $mrp;
                 $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', '');
                 $product->id_category = $importCategories;
                 $product->id_category_default = 1;
                 $product->name = array();
                 $product->name[$defaultLanguageId] = $product_name;
                 $product->description_short = array();
                 $product->description_short[$defaultLanguageId] = $style_tips;
                 $product->description = array();
                 $product->description[$defaultLanguageId] = $description;
                 $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]);
                 $product->link_rewrite = array();
                 $product->link_rewrite[$defaultLanguageId] = $link_rewrite;
                 $product->quantity = $quantity ? intval($quantity) : 0;
                 if ($discount && is_numeric($discount)) {
                     $product->discount = $discount;
                 }
                 if (!empty($tags)) {
                     $product->tags = $tags;
                 }
                 $product->weight = is_numeric($weight) ? $weight : 0;
                 $product->width = is_numeric($width) ? $width : 0;
                 $product->height = is_numeric($length) ? $length : 0;
                 $product->supplier_reference = $supplier_code;
                 $product->wholesale_price = $supplier_price ? (double) $supplier_price : 0;
                 $product->active = $active == 1 ? 1 : 0;
                 $product->images = $images;
                 $product->fabric = $fabric;
                 $product->color = $color;
                 $product->generic_color = $generic_color;
                 $product->garment_type = $garment_type;
                 $product->work_type = $work_type;
                 $product->blouse_length = $blouse_length ? $blouse_length : ' ';
                 $product->wash_care = $wash_care ? $wash_care : ' ';
                 $product->other_info = $other_info ? $other_info : ' ';
                 $product->shipping_estimate = $shipping_estimate ? $shipping_estimate : ' ';
                 $product->is_customizable = $customizable == 1 ? 1 : 0;
                 $product->kameez_style = $kameez_style;
                 $product->salwar_style = $salwar_style;
                 $product->sleeves = $sleeves;
                 $product->skirt_length = $skirt_length;
                 $product->dupatta_length = $dupatta_length;
                 $product->stone = $stone;
                 $product->plating = $plating;
                 $product->material = $material;
                 $product->dimensions = $dimensions;
                 $product->look = $look;
                 $product->as_shown = $as_shown;
                 $product->id_sizechart = $id_sizechart;
                 $product->is_exclusive = $is_exclusive;
                 $product->handbag_occasion = $handbag_occasion;
                 $product->handbag_style = $handbag_style;
                 $product->handbag_material = $handbag_material;
                 $product->indexed = 0;
                 $products_to_import[] = $product;
             } else {
                 $smarty->assign('error', $error);
                 return;
                 $file_error = true;
             }
         }
         if (!$file_error) {
             $added_product_ids = array();
             foreach ($products_to_import as $product) {
                 $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true);
                 $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true);
                 if ($fieldError === true and $langFieldError === true) {
                     // check quantity
                     if ($product->quantity == NULL) {
                         $product->quantity = 0;
                     }
                     // If no id_product or update failed
                     if ($update && $product->id) {
                         $res = $product->update();
                     } else {
                         $res = $product->add();
                     }
                     $added_product_ids[] = $product->id;
                 }
                 if (isset($product->discount) && $product->discount > 0) {
                     SpecificPrice::deleteByProductId((int) $product->id);
                     $specificPrice = new SpecificPrice();
                     $specificPrice->id_product = (int) $product->id;
                     $specificPrice->id_shop = (int) Shop::getCurrentShop();
                     $specificPrice->id_currency = 0;
                     $specificPrice->id_country = 0;
                     $specificPrice->id_group = 0;
                     $specificPrice->from_quantity = 1;
                     $specificPrice->reduction = $product->discount / 100;
                     $specificPrice->reduction_type = 'percentage';
                     $specificPrice->from = '2012-01-01 00:00:00';
                     $specificPrice->to = '2016-01-01 00:00:00';
                     $specificPrice->price = $product->price;
                     $specificPrice->add();
                 }
                 if (isset($product->tags) and !empty($product->tags)) {
                     // Delete tags for this id product, for no duplicating error
                     Tag::deleteTagsForProduct($product->id);
                     $tag = new Tag();
                     $tag->addTags($defaultLanguageId, $product->id, $tags);
                 }
                 if (isset($product->images) and is_array($product->images) and sizeof($product->images) and !$update || $overwrite_imgs == "on") {
                     $product->deleteImages();
                     $first_image = true;
                     foreach ($product->images as $image_name) {
                         $image_name = trim($image_name);
                         $image_path = IMAGE_UPLOAD_PATH . $image_name;
                         if (!empty($image_name)) {
                             $image = new Image();
                             $image->id_product = (int) $product->id;
                             $image->position = Image::getHighestPosition($product->id) + 1;
                             $image->cover = $first_image;
                             $image->legend[$defaultLanguageId] = $product->name[$defaultLanguageId];
                             if (($fieldError = $image->validateFields(false, true)) === true and ($langFieldError = $image->validateFieldsLang(false, true)) === true and $image->add()) {
                                 if (!self::copyImg($product->id, $image->id, $image_path)) {
                                     $_warnings[] = Tools::displayError('Error copying image: ') . $image_path;
                                 } else {
                                     //delete the original image
                                     @unlink($image_path);
                                 }
                             } else {
                                 $_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                                 $_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
                             }
                         }
                         $first_image = false;
                     }
                 }
                 if (isset($product->id_category)) {
                     $product->updateCategories(array_map('intval', $product->id_category));
                 }
                 $this->addFeature($product->id, 'fabric', $product->fabric);
                 $this->addFeature($product->id, 'color', $product->color);
                 $this->addFeature($product->id, 'garment_type', $product->garment_type);
                 $this->addFeature($product->id, 'work_type', $product->work_type);
                 $this->addFeature($product->id, 'blouse_length', $product->blouse_length);
                 $this->addFeature($product->id, 'wash_care', $product->wash_care);
                 $this->addFeature($product->id, 'other_info', $product->other_info);
                 // to avoid type errors in the catalog sheet - construct the string here again
                 $shipping_sla = (int) preg_replace('/\\D/', '', $product->shipping_estimate);
                 $shipping_estimate_str = "";
                 if ($shipping_sla > 0) {
                     $shipping_estimate_str = $shipping_sla === 1 ? "Ready to be shipped in 1 day" : "Ready to be shipped in {$shipping_sla} days";
                 }
                 $this->addFeature($product->id, 'shipping_estimate', $shipping_estimate_str);
                 $this->addFeature($product->id, 'kameez_style', $product->kameez_style);
                 $this->addFeature($product->id, 'salwar_style', $product->salwar_style);
                 $this->addFeature($product->id, 'sleeves', $product->sleeves);
                 $this->addFeature($product->id, 'generic_color', $product->generic_color);
                 $this->addFeature($product->id, 'skirt_length', $product->skirt_length);
                 $this->addFeature($product->id, 'dupatta_length', $product->dupatta_length);
                 $this->addFeature($product->id, 'stone', $product->stone);
                 $this->addFeature($product->id, 'plating', $product->plating);
                 $this->addFeature($product->id, 'material', $product->material);
                 $this->addFeature($product->id, 'dimensions', $product->dimensions);
                 $this->addFeature($product->id, 'look', $product->look);
                 $this->addFeature($product->id, 'handbag_occasion', $product->handbag_occasion);
                 $this->addFeature($product->id, 'handbag_style', $product->handbag_style);
                 $this->addFeature($product->id, 'handbag_material', $product->handbag_material);
             }
             $smarty->assign("products_affected", $products_to_import);
             //reindex the products
             SolrSearch::updateProducts($added_product_ids);
             $smarty->assign("is_update", $update);
         } else {
             $smarty->assign('file_error', 1);
         }
     } else {
         $smarty->assign('error_reading', 1);
     }
 }