Exemplo n.º 1
0
function sp_addProductToCatalog($jng_sp_id, $product_id)
{
    use_class('Product');
    use_class('jng_sp');
    use_class('products_minierp');
    use_class('jng_sp_catalog');
    use_class('jng_sp_catalog_params');
    $class_sp = new jng_sp();
    $class_pm = new products_minierp();
    $class_jc = new jng_sp_catalog();
    $class_jcp = new jng_sp_catalog_params();
    $sp_detail = $class_sp->retrieveDetail($jng_sp_id);
    $sp_used_selling_points = getSalesPartnerUseSellingPoint();
    $sp_used_selling_points_amde = getSalesPartnerUseSellingPoint(true);
    if (!is_array($product_id)) {
        $product_id = explode(',', $product_id);
    }
    $new_id = array();
    foreach ($product_id as $pid) {
        $check_exist = $class_jc->retrieveDetail(null, $jng_sp_id, $pid);
        if ($check_exist === false) {
            $obj_product = new Product($pid);
            $product_data_raw = $class_pm->retrieveDetail($pid);
            $products_selling_points = '';
            $psp = $product_data_raw['pd'][2]['products_selling_points'];
            $pspa = $product_data_raw['pd'][2]['products_selling_points_amde'];
            if (in_array($jng_sp_id, $sp_used_selling_points)) {
                $products_selling_points = $psp;
            } elseif (in_array($jng_sp_id, $sp_used_selling_points_amde)) {
                $products_selling_points = $pspa;
            }
            if (!is_null($obj_product->id)) {
                $desc = $product_data_raw['pd'][$sp_detail['languages_id']];
                $product = array();
                $product['products_length'] = $product_data_raw['pnc']['products_length'];
                $product['products_name'] = $desc['products_name'];
                $product['products_description'] = $desc['products_description'];
                $product['products_head_keywords_tag'] = $desc['products_head_keywords_tag'];
                $article_number = str_replace('-', '_', $obj_product->code);
                $price_uvp = $obj_product->getPriceUVP();
                $price_default = $obj_product->getPriceDefault();
                //TODO: USE B2B FLAG
                if ($jng_sp_id == '5') {
                    $product_price = $obj_product->getPriceSellingDefaultB2B($price_uvp, $jng_sp_id);
                    $product_price_old = 0;
                    $product_price_new = 0;
                    $active_status = '1';
                } else {
                    $product_price = $price_default;
                    $product_price_old = $price_uvp > $price_default ? $price_uvp : 0;
                    $product_price_new = 0;
                    $active_status = '0';
                }
                $product_name = $product['products_name'];
                $product_desc = $product['products_description'];
                $keywords_ok = array();
                $keywords_temp = str_replace('|||', ',', $product['products_head_keywords_tag']);
                $keywords_split = explode(',', $keywords_temp);
                foreach ($keywords_split as $kws) {
                    $kws = trim($kws);
                    if ($kws != '') {
                        $rule1 = strpos($product_name, $kws) === false;
                        $rule2 = strpos($product_desc, $kws) === false;
                        $rule3 = strpos($kws, 'cm') === false;
                        if ($rule1 && $rule2 && $rule3) {
                            $keywords_ok[] = $kws;
                        }
                    }
                }
                $product_keywords = count($keywords_ok) > 0 ? implode(', ', $keywords_ok) : '';
                $product_length = $product['products_length'] == '' ? '0' : $product['products_length'];
                //Retrieve Products Color and connect it with SP Colors
                $color_pattern_id = $class_pm->loadColorPattern($obj_product->id);
                $sp_params_raw = $class_jcp->load($jng_sp_id, 'C');
                $sp_params = array();
                foreach ($sp_params_raw as $spr) {
                    if (in_array($spr['param_id'], $color_pattern_id)) {
                        $sp_params[] = $spr['param_value'];
                    }
                }
                $product_color = implode(', ', $sp_params);
                //Retrieve Products Materials and connect it with SP Materials
                $products_materials_id = $class_pm->loadProductMaterials($obj_product->id);
                $sp_params_raw = $class_jcp->load($jng_sp_id, 'M');
                $sp_params = array();
                foreach ($sp_params_raw as $spr) {
                    if (in_array($spr['param_id'], $products_materials_id)) {
                        $sp_params[] = $spr['param_value'];
                    }
                }
                $product_material = implode(', ', $sp_params);
                $date_added = date('Y-m-d H:i:s');
                //Retrieve Max Resourcing Days
                $elmax_rd_query = "SELECT pe.products_id, MAX(es.resourcing_day) AS max_res_day";
                $elmax_rd_query .= " FROM products_use_elements pe";
                $elmax_rd_query .= " LEFT JOIN elements_stock es ON es.elements_id=pe.elements_id";
                $elmax_rd_query .= " WHERE pe.products_id = {$obj_product->id}";
                $elmax_rd_query .= " GROUP BY pe.products_id";
                $elmax_rd_result = tep_db_query($elmax_rd_query);
                $elmax_rd = tep_db_fetch_array($elmax_rd_result);
                $resplacement_days = $elmax_rd['max_res_day'] == '' ? '0' : $elmax_rd['max_res_day'];
                //INSERT DATA
                $sda = array('jng_sp_id' => $jng_sp_id, 'products_id' => $obj_product->id, 'article_number' => $article_number, 'price' => $product_price, 'price_old' => $product_price_old, 'price_new' => $product_price_new, 'name' => $product_name, 'description' => $product_desc, 'keywords' => $product_keywords, 'length' => $product_length, 'color' => $product_color, 'material' => $product_material, 'use_logo' => '1', 'status_image' => '0', 'date_added' => $date_added, 'replacement_days' => $resplacement_days, 'products_selling_points' => $products_selling_points, 'active_status' => $active_status);
                tep_db_perform('jng_sp_catalog', $sda);
                $new_id[] = tep_db_insert_id();
            }
        }
    }
    return $new_id;
}
Exemplo n.º 2
0
 /**
  * Set selling point to products with type 1 = Otto DE or 2 = Amazon DE or 3 = Lazada ID
  * @param int $pids
  * @param int $type
  * @param boolean $reset
  * @return int
  */
 function setSellingPointToProducts($pids, $type = null, $reset = false)
 {
     /* PRODUCT CATEGORIES
        2  => Necklace
        3  => Bracelet
        4  => Earrings
        9  => Charms (if style is 32 "Beads Schmuck" so its Beads)
        28 => Pendant
        29 => Rings
        33 => Anklets
        30 => Sets
        */
     $result = array();
     if (!is_null($type) && $type != '') {
         $type = explode(",", $type);
     }
     $sp_used_selling_points = getSalesPartnerUseSellingPoint();
     $sp_used_selling_points_amde = getSalesPartnerUseSellingPoint(true);
     $selling_point_category_ids = array(2, 3, 4, 9, 28, 29, 'beads', 33, 30);
     /* MATERIAL ID's
           1 is default, 17 or 57 or 59 is autochecked when products is contain gold
        */
     $selling_point_material_ids = array(2, 4, 5, 22, 52, 53);
     $selling_point_material_ids_amde = array(2, 4, 5, 22, 52, 53);
     $selling_point_material_ids_laid = array(2, 4, 5, 13);
     /* MATERIAL DEGREE
            1. Diamond (52)
            2. Gemstone (53)
            3. Freshwater Pearl (5)
            4. Swarovski (4)
            5. Zircon (2)
            6. Enamel (22)
            7. Rock Crystal (13)
            8. priority from top to bottom
               ~ 14k(585) Yellow Gold (57) 
               ~ 14k(585) White Gold (59) 
               ~ 9k(375) Yellow Gold (60) 
               ~ 9k(375) White Gold (61) 
               ~ Goldplated (17) 
               ~ Rose Goldplated (64) 
               ~ Silver (1)
        */
     $selling_point_material_degree = array('52' => 1, '53' => 2, '5' => 3, '4' => 4, '2' => 5, '22' => 6, '1' => 7);
     $selling_point_material_degree_amde = array('52' => 1, '53' => 2, '5' => 3, '4' => 4, '2' => 5, '22' => 6, '1' => 7);
     $selling_point_material_degree_laid = array('5' => 1, '4' => 2, '2' => 3, '13' => 4, '1' => 5);
     /* MATERIAL HAVE SUBS
                 $material_have_subs_amde INFO:
                 - on am.de matrix material 5 and 52 could possible combined each-other (52-5) or 5 combined with 2 or 4 (5-2, 5-4) 
                 - please see getSellingPointMatrix on functions-2.php for more details
     
                 $material_have_subs_laid INFO:
                 - on Lazada ID matrix material 5 could possible combined with 2 or 4 (5-2, 5-4) 
              */
     $material_have_subs = array(52);
     $material_have_subs_amde = array(5, 52);
     $material_have_subs_laid = array(5);
     /* MATERIAL GOLDPURE DEGREE
             1. 14k(585) Yellow Gold (57)
             2. 14k(585) White Gold (59)
             3. 9k(375) Yellow Gold (60)
             4. 9k(375) White Gold (61)
        */
     $material_goldpure_degree = array('57' => 1, '59' => 2, '60' => 3, '61' => 4);
     $material_goldpure = array_keys($material_goldpure_degree);
     if (!is_array($pids)) {
         $pids = explode(',', $pids);
     }
     $n_s_point_added = 0;
     $spm_array = array();
     foreach ($pids as $products_id) {
         $materials_id_selected = '1';
         $materials_id_selected_amde = '1';
         $materials_id_selected_laid = '1';
         $materials_id_goldpure_selected = '';
         $mat_goldplated = '';
         $is_goldpure = false;
         $pid = $products_id;
         $cats = $this->retrieveDetail($pid, 'cat');
         $category_id = $cats['categories_id'];
         if ($category_id == '9' && $this->productIsBeads($pid)) {
             $category_id = 'beads';
         }
         $spm = '';
         if (in_array($category_id, $selling_point_category_ids)) {
             $mat_used = $this->retrieveProductMaterialsUsed($pid);
             $dbqg = tep_db_query("SELECT SUM(finishing_goldplate) use_gold, SUM(finishing_rosegoldplate) use_rosegoldplate FROM products_use_elements WHERE products_id = {$pid}");
             $rg = tep_db_fetch_array($dbqg);
             if ($rg['use_gold'] == '1') {
                 $mat_goldplated = '17';
             } elseif ($rg['use_rosegoldplate'] == '1') {
                 $mat_goldplated = '64';
             }
             foreach ($mat_used as $mat_id) {
                 # --------------- Begin Selling Point General Logic ---------------
                 if (in_array($mat_id, $selling_point_material_ids)) {
                     if ($selling_point_material_degree[$mat_id] < $selling_point_material_degree[$materials_id_selected]) {
                         $materials_id_selected = $mat_id;
                     }
                 }
                 if (in_array($mat_id, $material_goldpure)) {
                     $is_goldpure = true;
                     if ($materials_id_goldpure_selected == '') {
                         $materials_id_goldpure_selected = $mat_id;
                     } else {
                         if ($material_goldpure_degree[$mat_id] < $material_goldpure_degree[$materials_id_goldpure_selected]) {
                             $materials_id_goldpure_selected = $mat_id;
                         }
                     }
                 }
                 # --------------- End Selling Point General Logic ---------------
                 # --------------- Begin Selling Point AMDE Logic ---------------
                 if (in_array($mat_id, $selling_point_material_ids_amde)) {
                     if ($selling_point_material_degree_amde[$mat_id] < $selling_point_material_degree_amde[$materials_id_selected_amde]) {
                         $materials_id_selected_amde = $mat_id;
                     }
                 }
                 if (in_array($mat_id, $material_goldpure)) {
                     $is_goldpure = true;
                     if ($materials_id_goldpure_selected_amde == '') {
                         $materials_id_goldpure_selected_amde = $mat_id;
                     } else {
                         if ($material_goldpure_degree[$mat_id] < $material_goldpure_degree[$materials_id_goldpure_selected_amde]) {
                             $materials_id_goldpure_selected_amde = $mat_id;
                         }
                     }
                 }
                 # --------------- End Selling Point AMDE Logic ---------------
                 # --------------- Begin Selling Point Lazada ID Logic ---------------
                 if (in_array($mat_id, $selling_point_material_ids_laid)) {
                     if ($selling_point_material_degree_laid[$mat_id] < $selling_point_material_degree_laid[$materials_id_selected_laid]) {
                         $materials_id_selected_laid = $mat_id;
                     }
                 }
                 # --------------- End Selling Point Lazada ID Logic ---------------
             }
             $materials_id = $materials_id_selected;
             $materials_id_amde = $materials_id_selected_amde;
             $materials_id_laid = $materials_id_selected_laid;
             //if material have subs, get sub materials
             if (in_array($materials_id, $material_have_subs)) {
                 $sub_materials_id = $this->getSubsSellingPoints($mat_used, $materials_id);
                 if ($sub_materials_id != '') {
                     $materials_id = "{$materials_id}-{$sub_materials_id}";
                     $materials_id_selected = $materials_id;
                 }
             }
             if (in_array($materials_id_amde, $material_have_subs_amde)) {
                 $sub_materials_id_amde = $this->getSubsSellingPointsAMDE($mat_used, $materials_id_amde);
                 if ($sub_materials_id_amde != '') {
                     $materials_id_amde = "{$materials_id_amde}-{$sub_materials_id_amde}";
                     $materials_id_selected_amde = $materials_id_amde;
                 }
             }
             if (in_array($materials_id_laid, $material_have_subs_laid)) {
                 $sub_materials_id_laid = $this->getSubsSellingPointsLAID($mat_used, $materials_id_laid);
                 if ($sub_materials_id_laid != '') {
                     $materials_id_laid = "{$materials_id_laid}-{$sub_materials_id_laid}";
                     $materials_id_selected_laid = $materials_id_laid;
                 }
             }
             //if goldplated products
             if ($mat_goldplated != '') {
                 if ($materials_id_selected != '1') {
                     $materials_id = "{$mat_goldplated}-{$materials_id_selected}";
                 } else {
                     $materials_id = $mat_goldplated;
                 }
                 if ($materials_id_selected_amde != '1') {
                     $materials_id_amde = "{$mat_goldplated}-{$materials_id_selected_amde}";
                 } else {
                     $materials_id_amde = $mat_goldplated;
                 }
                 if ($materials_id_selected_laid != '1') {
                     $materials_id_laid = "{$mat_goldplated}-{$materials_id_selected_laid}";
                 } else {
                     $materials_id_laid = $mat_goldplated;
                 }
             }
             //override to goldpure if product is goldpure products (considering also degree of goldpure)
             if ($is_goldpure) {
                 $materials_id = $materials_id_selected != '1' ? "{$materials_id_goldpure_selected}-{$materials_id_selected}" : $materials_id_goldpure_selected;
                 $materials_id_amde = $materials_id_selected_amde != '1' ? "{$materials_id_goldpure_selected_amde}-{$materials_id_selected_amde}" : $materials_id_goldpure_selected_amde;
             }
             //if products have stone except swaro and zircon set it with selling point for stones (Lazada ID)
             $product_elements_used_attr_category = $this->retrieveProductCategoryElementsUsed($products_id);
             $contain_stones = in_array('17', $product_elements_used_attr_category) || in_array('5', $product_elements_used_attr_category);
             if ($contain_stones && ($materials_id_laid == '1' || $materials_id_laid == '17')) {
                 $materials_id_laid = "{$materials_id}-stones";
             }
             $cond_pd1 = "products_id = '{$pid}' AND language_id = 2";
             $cond_pd2 = "products_id = '{$pid}' AND language_id = 2";
             $cond_pd3 = "products_id = '{$pid}' AND language_id = 3";
             $cond_jc1 = "products_id = '{$pid}' AND jng_sp_id IN (" . implode(',', $sp_used_selling_points) . ")";
             $cond_jc2 = "products_id = '{$pid}' AND jng_sp_id IN (" . implode(',', $sp_used_selling_points_amde) . ")";
             //OTTO DE
             if (is_null($type) || array_search('1', $type) !== false) {
                 //echo "$pid || $category_id, $materials_id<br/>";die();
                 $spm = getSellingPointMatrix($category_id, $materials_id, '2');
                 $spm_array[$products_id]['products_selling_points'] = $spm;
                 $res = tep_db_query("SELECT products_id FROM products_description WHERE {$cond_pd1}");
                 if (!$reset) {
                     $cond_pd1 .= " AND (products_selling_points = '' OR products_selling_points IS NULL)";
                     $cond_jc1 .= " AND (products_selling_points = '' OR products_selling_points IS NULL)";
                 }
                 if (tep_db_num_rows($res) > 0) {
                     //Update selling points otto on product level
                     tep_db_perform('products_description', array('products_selling_points' => $spm), 'update', $cond_pd1);
                     //Update selling points on catalog level
                     tep_db_perform('jng_sp_catalog', array('products_selling_points' => $spm), 'update', $cond_jc1);
                 } else {
                     //Add selling points
                     tep_db_perform('products_description', array('products_id' => $products_id, 'language_id' => 2, 'products_selling_points' => $spm));
                 }
             }
             //AMAZON DE
             if (is_null($type) || array_search('2', $type) !== false) {
                 //echo "$pid || $category_id, $materials_id_amde<br/>";die();
                 $spm_amde = getSellingPointMatrix($category_id, $materials_id_amde, '5');
                 $spm_array[$products_id]['products_selling_points_amde'] = $spm_amde;
                 $res = tep_db_query("SELECT products_id FROM products_description WHERE {$cond_pd2}");
                 if (!$reset) {
                     $cond_pd2 .= " AND (products_selling_points_amde = '' OR products_selling_points_amde IS NULL)";
                     $cond_jc2 .= " AND (products_selling_points = '' OR products_selling_points IS NULL)";
                 }
                 if (tep_db_num_rows($res) > 0) {
                     //Update selling points amazon on product level
                     tep_db_perform('products_description', array('products_selling_points_amde' => $spm_amde), 'update', $cond_pd2);
                     //Update selling points on catalog level
                     tep_db_perform('jng_sp_catalog', array('products_selling_points' => $spm_amde), 'update', $cond_jc2);
                 } else {
                     //Add selling points
                     tep_db_perform('products_description', array('products_id' => $products_id, 'language_id' => 2, 'products_selling_points_amde' => $spm_amde));
                 }
             }
             //LAZADA ID
             if (is_null($type) || array_search('3', $type) !== false) {
                 //echo "$pid || $category_id, $materials_id<br/>";die();
                 $spm_laid = getSellingPointMatrix($category_id, $materials_id_laid, '1001');
                 $spm_array[$products_id]['products_selling_points_laid'] = $spm_laid;
                 $res = tep_db_query("SELECT products_id FROM products_description WHERE {$cond_pd3}");
                 if (!$reset) {
                     $cond_pd3 .= " AND (products_selling_points = '' OR products_selling_points IS NULL)";
                     $cond_jc3 .= " AND (products_selling_points = '' OR products_selling_points IS NULL)";
                 }
                 if (tep_db_num_rows($res) > 0) {
                     //Update selling points Lazada ID on product level
                     tep_db_perform('products_description', array('products_selling_points' => $spm_laid), 'update', $cond_pd3);
                     //Update selling points on catalog level (not yet activated since we not prepare yet lazada as sales partner)
                     /*
                                             tep_db_perform(
                            'jng_sp_catalog', 
                            array('products_selling_points' => $spm), 
                            'update', $cond_jc3);
                     */
                 } else {
                     //Add selling points
                     tep_db_perform('products_description', array('products_id' => $products_id, 'language_id' => 3, 'products_selling_points' => $spm_laid));
                 }
             }
             $n_s_point_added++;
         }
     }
     $result = $spm_array;
     $result['total_selling_point_added'] = $n_s_point_added;
     return $result;
 }