コード例 #1
0
    public function supplyOrdersDetailsImport()
    {
        // opens CSV & sets locale
        $this->receiveTab();
        $handle = $this->openCsvFile();
        AdminImportController::setLocale();
        $products = array();
        $reset = true;
        // main loop, for each supply orders details to import
        for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); ++$current_line) {
            // if convert requested
            if (Tools::getValue('convert')) {
                $line = $this->utf8EncodeArray($line);
            }
            $info = AdminImportController::getMaskedRow($line);
            // sets default values if needed
            AdminImportController::setDefaultValues($info);
            // gets the supply order
            if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) {
                $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference']));
            } else {
                $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), (int) $info['supply_order_reference'], $current_line + 1);
            }
            if (empty($this->errors)) {
                // sets parameters
                $id_product = (int) $info['id_product'];
                if (!$info['id_product_attribute']) {
                    $info['id_product_attribute'] = 0;
                }
                $id_product_attribute = (int) $info['id_product_attribute'];
                $unit_price_te = (double) $info['unit_price_te'];
                $quantity_expected = (int) $info['quantity_expected'];
                $discount_rate = (double) $info['discount_rate'];
                $tax_rate = (double) $info['tax_rate'];
                // checks if one product is there only once
                if (isset($product['id_product'])) {
                    if ($product['id_product'] == $id_product_attribute) {
                        $this->errors[] = sprintf($this->l('Product (%d/%D) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
                    } else {
                        $product['id_product'] = $id_product_attribute;
                    }
                } else {
                    $product['id_product'] = 0;
                }
                // checks parameters
                if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) {
                    $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
                }
                if ($unit_price_te < 0) {
                    $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1);
                }
                if ($quantity_expected < 0) {
                    $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1);
                }
                if ($discount_rate < 0 || $discount_rate > 100) {
                    $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
                }
                if ($tax_rate < 0 || $tax_rate > 100) {
                    $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
                }
                // if no errors, sets supply order details
                if (empty($this->errors)) {
                    // resets order if needed
                    if ($reset) {
                        $supply_order->resetProducts();
                        $reset = false;
                    }
                    // creates new product
                    $supply_order_detail = new SupplyOrderDetail();
                    AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail);
                    // sets parameters
                    $supply_order_detail->id_supply_order = $supply_order->id;
                    $currency = new Currency($supply_order->id_ref_currency);
                    $supply_order_detail->id_currency = $currency->id;
                    $supply_order_detail->exchange_rate = $currency->conversion_rate;
                    $supply_order_detail->supplier_reference = $supplier_reference;
                    $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang);
                    // gets ean13 / ref / upc
                    $query = new DbQuery();
                    $query->select('
						IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference,
						IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13,
						IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc
					');
                    $query->from('product', 'p');
                    $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute);
                    $query->where('p.id_product = ' . (int) $id_product);
                    $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0');
                    $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
                    $product_infos = $res['0'];
                    $supply_order_detail->reference = $product_infos['reference'];
                    $supply_order_detail->ean13 = $product_infos['ean13'];
                    $supply_order_detail->upc = $product_infos['upc'];
                    $supply_order_detail->add();
                    $supply_order->update();
                    unset($supply_order_detail);
                }
            }
        }
        // closes
        $this->closeCsvFile($handle);
    }
コード例 #2
0
    protected function supplyOrdersDetailsImportOne($info, &$products, &$reset, $force_ids, $current_line, $validateOnly = false)
    {
        // sets default values if needed
        AdminImportController::setDefaultValues($info);
        // gets the supply order
        if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) {
            $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference']));
        } else {
            $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), $info['supply_order_reference'], $current_line + 1);
        }
        if (empty($this->errors)) {
            // sets parameters
            $id_product = (int) $info['id_product'];
            if (!$info['id_product_attribute']) {
                $info['id_product_attribute'] = 0;
            }
            $id_product_attribute = (int) $info['id_product_attribute'];
            $unit_price_te = (double) $info['unit_price_te'];
            $quantity_expected = (int) $info['quantity_expected'];
            $discount_rate = (double) $info['discount_rate'];
            $tax_rate = (double) $info['tax_rate'];
            // checks if one product/attribute is there only once
            if (isset($products[$id_product][$id_product_attribute])) {
                $this->errors[] = sprintf($this->l('Product/Attribute (%d/%d) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
            } else {
                $products[$id_product][$id_product_attribute] = $quantity_expected;
            }
            // checks parameters
            if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) {
                $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1);
            }
            if ($unit_price_te < 0) {
                $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1);
            }
            if ($quantity_expected < 0) {
                $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1);
            }
            if ($discount_rate < 0 || $discount_rate > 100) {
                $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
            }
            if ($tax_rate < 0 || $tax_rate > 100) {
                $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100'));
            }
            // if no errors, sets supply order details
            if (empty($this->errors)) {
                // resets order if needed
                if (!$validateOnly && $reset) {
                    $supply_order->resetProducts();
                    $reset = false;
                }
                // creates new product
                $supply_order_detail = new SupplyOrderDetail();
                AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail);
                // sets parameters
                $supply_order_detail->id_supply_order = $supply_order->id;
                $currency = new Currency($supply_order->id_ref_currency);
                $supply_order_detail->id_currency = $currency->id;
                $supply_order_detail->exchange_rate = $currency->conversion_rate;
                $supply_order_detail->supplier_reference = $supplier_reference;
                $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang);
                // gets ean13 / ref / upc
                $query = new DbQuery();
                $query->select('
					IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference,
					IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13,
					IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc
				');
                $query->from('product', 'p');
                $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute);
                $query->where('p.id_product = ' . (int) $id_product);
                $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0');
                $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
                $product_infos = $res['0'];
                $supply_order_detail->reference = $product_infos['reference'];
                $supply_order_detail->ean13 = $product_infos['ean13'];
                $supply_order_detail->upc = $product_infos['upc'];
                $supply_order_detail->force_id = (bool) $force_ids;
                if (!$validateOnly) {
                    $supply_order_detail->add();
                    $supply_order->update();
                }
                unset($supply_order_detail);
            }
        }
    }
コード例 #3
0
 private function getItemXML($product, $lang, $id_curr, $id_shop, $combination = false)
 {
     $xml_googleshopping = '';
     $id_lang = (int) $lang['id_lang'];
     $title_limit = 70;
     $description_limit = 4990;
     $languages = Language::getLanguages();
     $tailleTabLang = sizeof($languages);
     $this->context->language->id = $id_lang;
     $this->context->shop->id = $id_shop;
     $p = new Product($product['id_product'], true, $id_lang, $id_shop, $this->context);
     // Get module configuration for this shop
     if (!$combination) {
         $product['quantity'] = StockAvailable::getQuantityAvailableByProduct($product['id_product'], 0, $id_shop);
     }
     // Exclude non-available products
     if ($this->module_conf['export_nap'] === 0 && $product['quantity'] < 1) {
         $this->nb_not_exported_products++;
         return;
     }
     // Check minimum product price
     $price = Product::getPriceStatic((int) $product['id_product'], true);
     if ((double) $this->module_conf['export_min_price'] > 0 && (double) $this->module_conf['export_min_price'] > (double) $price) {
         return;
     }
     $cat_link_rew = Category::getLinkRewrite($product['id_gcategory'], (int) $lang);
     $product_link = $this->context->link->getProductLink((int) $product['id_product'], $product['link_rewrite'], $cat_link_rew, $product['ean13'], (int) $product['id_lang'], $id_shop, $combination, true);
     // Product name
     $title_crop = $product['name'];
     //  Product color attribute, if any
     if (!empty($product['color'])) {
         $title_crop .= ' ' . $product['color'];
     }
     if (!empty($product['material'])) {
         $title_crop .= ' ' . $product['material'];
     }
     if (!empty($product['pattern'])) {
         $title_crop .= ' ' . $product['pattern'];
     }
     if (!empty($product['size'])) {
         $title_crop .= ' ' . $product['size'];
     }
     if (Tools::strlen($product['name']) > $title_limit) {
         $title_crop = Tools::substr($title_crop, 0, $title_limit - 1);
         $title_crop = Tools::substr($title_crop, 0, strrpos($title_crop, " "));
     }
     // Description type
     if ($this->module_conf['description'] == 'long') {
         $description_crop = $product['description'];
     } else {
         if ($this->module_conf['description'] == 'short') {
             $description_crop = $product['description_short'];
         } else {
             if ($this->module_conf['description'] == 'meta') {
                 $description_crop = $product['meta_description'];
             }
         }
     }
     $description_crop = $this->rip_tags($description_crop);
     if (Tools::strlen($description_crop) > $description_limit) {
         $description_crop = Tools::substr($description_crop, 0, $description_limit - 1);
         $description_crop = Tools::substr($description_crop, 0, strrpos($description_crop, " ")) . ' ...';
     }
     $xml_googleshopping .= '<item>' . "\n";
     $xml_googleshopping .= '<g:id>' . $product['gid'] . '</g:id>' . "\n";
     $xml_googleshopping .= '<title><![CDATA[' . $title_crop . ']]></title>' . "\n";
     $xml_googleshopping .= '<description><![CDATA[' . $description_crop . ']]></description>' . "\n";
     $xml_googleshopping .= '<link><![CDATA[' . htmlspecialchars($product_link, self::REPLACE_FLAGS, self::CHARSET, false) . ']]></link>' . "\n";
     // Image links
     $images = Image::getImages($lang['id_lang'], $product['id_product'], $combination);
     $indexTabLang = 0;
     if ($tailleTabLang > 1) {
         while (sizeof($images) < 1 && $indexTabLang < $tailleTabLang) {
             if ($languages[$indexTabLang]['id_lang'] != $lang['id_lang']) {
                 $images = Image::getImages($languages[$indexTabLang]['id_lang'], $product['id_product']);
             }
             $indexTabLang++;
         }
     }
     $nbimages = 0;
     $image_type = $this->module_conf['img_type'];
     if ($image_type == '') {
         $image_type = 'large_default';
     }
     foreach ($images as $im) {
         $image = $this->context->link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $im['id_image'], $image_type);
         $image = preg_replace('*http://' . Tools::getHttpHost() . '/*', $this->uri, $image);
         if ($im['cover'] == 1) {
             $xml_googleshopping .= '<g:image_link><![CDATA[' . $image . ']]></g:image_link>' . "\n";
         } else {
             $xml_googleshopping .= '<g:additional_image_link><![CDATA[' . $image . ']]></g:additional_image_link>' . "\n";
         }
         // max images by product
         if (++$nbimages == 10) {
             break;
         }
     }
     // Product condition, or category's condition attribute, or its parent one...
     // Product condition = new, used, refurbished
     if (empty($product['condition'])) {
         $product['condition'] = $this->categories_values[$product['id_gcategory']]['gcat_condition'];
     }
     if (!empty($product['condition'])) {
         $xml_googleshopping .= '<g:condition><![CDATA[' . $product['condition'] . ']]></g:condition>' . "\n";
     }
     // Shop category
     $breadcrumb = GCategories::getPath($product['id_gcategory'], '', $id_lang, $id_shop, $this->id_root);
     $product_type = '';
     if (!empty($this->module_conf['product_type[]'][$id_lang])) {
         $product_type = $this->module_conf['product_type[]'][$id_lang];
         if (!empty($breadcrumb)) {
             $product_type .= " > ";
         }
     }
     $product_type .= $breadcrumb;
     $xml_googleshopping .= '<g:product_type><![CDATA[' . $product_type . ']]></g:product_type>' . "\n";
     // Matching Google category, or parent categories' one
     $product['gcategory'] = $this->categories_values[$product['category_default']]['gcategory'];
     $xml_googleshopping .= '<g:google_product_category><![CDATA[' . $product['gcategory'] . ']]></g:google_product_category>' . "\n";
     // Product quantity & availability
     if (empty($this->categories_values[$product['category_default']]['gcat_avail'])) {
         if ($this->module_conf['quantity'] == 1) {
             $xml_googleshopping .= '<g:quantity>' . $product['quantity'] . '</g:quantity>' . "\n";
         }
         if ($product['quantity'] > 0 && $product['available_for_order']) {
             $xml_googleshopping .= '<g:availability>in stock</g:availability>' . "\n";
         } elseif ($p->isAvailableWhenOutOfStock((int) $p->out_of_stock) && $product['available_for_order']) {
             $xml_googleshopping .= '<g:availability>preorder</g:availability>' . "\n";
         } else {
             $xml_googleshopping .= '<g:availability>out of stock</g:availability>' . "\n";
         }
     } else {
         if ($this->module_conf['quantity'] == 1 && $product['quantity'] > 0) {
             $xml_googleshopping .= '<g:quantity>' . $product['quantity'] . '</g:quantity>' . "\n";
         }
         $xml_googleshopping .= '<g:availability>' . $this->categories_values[$product['category_default']]['gcat_avail'] . '</g:availability>' . "\n";
     }
     // Price(s)
     $currency = new Currency((int) $id_curr);
     $use_tax = $product['tax_included'] ? true : false;
     $no_tax = !$use_tax ? true : false;
     $product['price'] = (double) $p->getPriceStatic($product['id_product'], $use_tax, $combination) * $currency->conversion_rate;
     $product['price_without_reduct'] = (double) $p->getPriceWithoutReduct($no_tax, $combination) * $currency->conversion_rate;
     $product['price'] = number_format(round($product['price'], 2, PHP_ROUND_HALF_DOWN), 2, '.', ' ');
     $product['price_without_reduct'] = number_format(round($product['price_without_reduct'], 2, PHP_ROUND_HALF_DOWN), 2, '.', ' ');
     if ((double) $product['price'] < (double) $product['price_without_reduct']) {
         $xml_googleshopping .= '<g:price>' . $product['price_without_reduct'] . ' ' . $currency->iso_code . '</g:price>' . "\n";
         $xml_googleshopping .= '<g:sale_price>' . $product['price'] . ' ' . $currency->iso_code . '</g:sale_price>' . "\n";
     } else {
         $xml_googleshopping .= '<g:price>' . $product['price'] . ' ' . $currency->iso_code . '</g:price>' . "\n";
     }
     $identifier_exists = 0;
     // GTIN (EAN, UPC, JAN, ISBN)
     if (!empty($product['ean13'])) {
         $xml_googleshopping .= '<g:gtin>' . $product['ean13'] . '</g:gtin>' . "\n";
         $identifier_exists++;
     }
     // Brand
     if ($this->module_conf['no_brand'] != 0 && !empty($product['id_manufacturer'])) {
         $xml_googleshopping .= '<g:brand><![CDATA[' . htmlspecialchars(Manufacturer::getNameById((int) $product['id_manufacturer']), self::REPLACE_FLAGS, self::CHARSET, false) . ']]></g:brand>' . "\n";
         $identifier_exists++;
     }
     // MPN
     if (empty($product['supplier_reference'])) {
         $product['supplier_reference'] = ProductSupplier::getProductSupplierReference($product['id_product'], 0, $product['id_supplier']);
     }
     if ($this->module_conf['mpn_type'] == 'reference' && !empty($product['reference'])) {
         $xml_googleshopping .= '<g:mpn><![CDATA[' . $product['reference'] . ']]></g:mpn>' . "\n";
         $identifier_exists++;
     } else {
         if ($this->module_conf['mpn_type'] == 'supplier_reference' && !empty($product['supplier_reference'])) {
             $xml_googleshopping .= '<g:mpn><![CDATA[' . $product['supplier_reference'] . ']]></g:mpn>' . "\n";
             $identifier_exists++;
         }
     }
     // Tag "identifier_exists"
     if ($this->module_conf['id_exists_tag'] && $identifier_exists < 2) {
         $xml_googleshopping .= '<g:identifier_exists>FALSE</g:identifier_exists>' . "\n";
     }
     // Product gender and age_group attributes association
     $product_features = $this->getProductFeatures($product['id_product'], $id_lang, $id_shop);
     $product['gender'] = $this->categories_values[$product['category_default']]['gcat_gender'];
     $product['age_group'] = $this->categories_values[$product['category_default']]['gcat_age_group'];
     foreach ($product_features as $feature) {
         switch ($feature['id_feature']) {
             case $this->module_conf['gender']:
                 $product['gender'] = $feature['value'];
                 continue 2;
             case $this->module_conf['age_group']:
                 $product['age_group'] = $feature['value'];
                 continue 2;
         }
         if (!$product['color']) {
             foreach ($this->module_conf['color[]'] as $id => $v) {
                 if ($v == $feature['id_feature']) {
                     $product['color'] = $feature['value'];
                 }
             }
         }
         if (!$product['material']) {
             foreach ($this->module_conf['material[]'] as $id => $v) {
                 if ($v == $feature['id_feature']) {
                     $product['material'] = $feature['value'];
                 }
             }
         }
         if (!$product['pattern']) {
             foreach ($this->module_conf['pattern[]'] as $id => $v) {
                 if ($v == $feature['id_feature']) {
                     $product['pattern'] = $feature['value'];
                 }
             }
         }
         if (!$product['size']) {
             foreach ($this->module_conf['size[]'] as $id => $v) {
                 if ($v == $feature['id_feature']) {
                     $product['size'] = $feature['value'];
                 }
             }
         }
     }
     //  Product gender attribute, or category gender attribute, or parent's one
     if (!empty($product['gender'])) {
         $xml_googleshopping .= '<g:gender><![CDATA[' . $product['gender'] . ']]></g:gender>' . "\n";
     }
     // Product age_group attribute, or category age_group attribute, or parent's one
     if (!empty($product['age_group'])) {
         $xml_googleshopping .= '<g:age_group><![CDATA[' . $product['age_group'] . ']]></g:age_group>' . "\n";
     }
     // Product attributes combination groups
     if ($combination && !empty($product['item_group_id'])) {
         $xml_googleshopping .= '<g:item_group_id>' . $product['item_group_id'] . '</g:item_group_id>' . "\n";
     }
     // Product color attribute, or category color attribute, or parent's one
     if (!empty($product['color'])) {
         $xml_googleshopping .= '<g:color><![CDATA[' . $product['color'] . ']]></g:color>' . "\n";
     }
     // Product material attribute, or category material attribute, or parent's one
     if (!empty($product['material'])) {
         $xml_googleshopping .= '<g:material><![CDATA[' . $product['material'] . ']]></g:material>' . "\n";
     }
     // Product pattern attribute, or category pattern attribute, or parent's one
     if (!empty($product['pattern'])) {
         $xml_googleshopping .= '<g:pattern><![CDATA[' . $product['pattern'] . ']]></g:pattern>' . "\n";
     }
     // Product size attribute, or category size attribute, or parent's one
     if (!empty($product['size'])) {
         $xml_googleshopping .= '<g:size><![CDATA[' . $product['size'] . ']]></g:size>' . "\n";
     }
     // Featured products
     if ($this->module_conf['featured_products'] == 1 && $product['on_sale'] != '0') {
         $xml_googleshopping .= '<g:featured_product>true</g:featured_product>' . "\n";
     }
     // Shipping
     $xml_googleshopping .= '<g:shipping>' . "\n";
     $xml_googleshopping .= "\t" . '<g:country>' . $this->module_conf['shipping_country'] . '</g:country>' . "\n";
     $xml_googleshopping .= "\t" . '<g:service>Standard</g:service>' . "\n";
     $xml_googleshopping .= "\t" . '<g:price>' . number_format($this->module_conf['shipping_price'], 2, '.', ' ') . ' ' . $currency->iso_code . '</g:price>' . "\n";
     $xml_googleshopping .= '</g:shipping>' . "\n";
     // Shipping weight
     if ($product['weight'] != '0') {
         $xml_googleshopping .= '<g:shipping_weight>' . number_format($product['weight'], 2, '.', '') . ' ' . Configuration::get('PS_WEIGHT_UNIT') . '</g:shipping_weight>' . "\n";
     }
     $xml_googleshopping .= '</item>' . "\n\n";
     if ($combination) {
         $this->nb_combinations++;
         $this->nb_prd_w_attr[$product['id_product']] = 1;
     }
     $this->nb_total_products++;
     return $xml_googleshopping;
 }