/**
  * Set brand of a specific product. This fnction will update brand for all families of the given products.
  * However, it will return only total successful products from the give products (updated familes are not included).
  * Updated products will also be automatically included in next catalog upload.
  * @param Int $products_id Product ID
  * @param Int $brand_id Brand ID
  * @return Int Total products updated
  */
 function updateProductBrand($products_id, $brand_id)
 {
     use_class('products_family');
     $pids = explode(',', $products_id);
     $n_pid = count($pids);
     $n_pid_success = 0;
     $break_the_rules = true;
     foreach ($pids as $products_id) {
         $obj_family = new products_family(null, $products_id);
         $is_family_locked = $obj_family->productIsFamilyLocked($products_id);
         if (!$is_family_locked || $break_the_rules) {
             $mother_is_locked = $obj_family->mother > 0 ? $obj_family->productIsFamilyLocked($obj_family->mother) : false;
             if (!$mother_is_locked || $break_the_rules) {
                 $members = $obj_family->retrieveMembers();
                 $sda = array('products_brand_id' => $brand_id);
                 $products_ids = is_array($members) && count($members) > 0 ? implode(',', $members) : $products_id;
                 tep_db_perform('products', $sda, 'update', "products_id IN ({$products_ids})");
                 //refresh family search key to all members
                 //we stop it since brand is already excluded from family search key
                 //                            $pids_m = explode(',', $products_ids);
                 //                            foreach($pids_m as $pid_m) $obj_family->refreshFamilyMatchKey($pid_m);
                 use_class('products_minierp');
                 $class_pm = new products_minierp();
                 $class_pm->autoUpdateSalesPartnerCatalog($products_id);
                 $n_pid_success++;
             }
         }
     }
     return $n_pid_success;
 }
 /**
  * get products based on filter (family, main element, symbol, color, and brand)
  * @param Int $product_id Products ID
  * @return Array Array of products ID
  */
 function getProductsCrossSelling($product_id)
 {
     use_class('products_brand');
     use_class('styles_php4');
     use_class('products_minierp');
     use_class('products_family_php4');
     $class_s = new styles();
     $class_pb = new products_brand();
     $class_pm = new products_minierp();
     $max_products = 50;
     $max_products_by_family = 20;
     $max_products_by_main_element = 10;
     $max_products_by_symbol = 30;
     //            $max_products_by_color = 5;
     //            $max_products_by_brand = 5;
     $max_products_by_category = 40;
     $pids = array();
     $p_data = $class_pm->retrieveDetail($product_id, 'p,cat');
     //FAMILY
     $family_id = $p_data['p']['family_id'];
     $obj_family = new products_family(null, $product_id);
     $pid_family = $obj_family->retrieveMembers();
     $pid_family_exclude_kill_prod = $class_pm->filterExcludeKilledProducts($pid_family);
     $pid_similar = $obj_family->getSimilarProducts($product_id, false, false);
     $pids = array_merge($pid_family_exclude_kill_prod, $pid_similar);
     $key = array_search($product_id, $pids);
     unset($pids[$key]);
     $pids = array_slice($pids, 0, 20);
     //MAIN ELEMENT
     $pid_family_filter = implode(",", $pids);
     if ($pid_family_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_family_filters = "AND pue.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_family_filters = "AND pue.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_family_filters = "AND pue.products_id NOT IN ({$pid_family_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_family_filters = "AND pue.products_id NOT IN ({$pid_family_filter}, {$product_id})";
         }
     }
     $main_element_id = $class_pm->getMainElement($product_id);
     if ($main_element_id != NULL) {
         $pid_main_element = $class_pm->getProductsByMainElement($main_element_id, false, $pid_family_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_main_element}");
         $pids = array_merge($pids, $pid_main_element);
     }
     //SYMBOL
     $symbol_id = $class_s->getProductStyle($product_id);
     $pid_main_element_filter = implode(",", $pids);
     if ($pid_main_element_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$pid_main_element_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_main_element_filters = "AND shp.products_id NOT IN ({$pid_main_element_filter}, {$product_id})";
         }
     }
     if ($symbol_id['Symbol'] != '') {
         $pid_symbol = array_keys($class_s->retrieveProducts($symbol_id['Symbol'], false, $pid_main_element_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_symbol}"));
         $pids = array_merge($pids, $pid_symbol);
     }
     //COLOR
     //            $color_id = $class_pm->loadColorPattern($product_id);
     //            if ($color_id) {
     //                $pids_symbol_filter = implode(",", $pids);
     //                if ($pids_symbol_filter == '') {
     //                    $pid_symbol_filters = "AND ptcp.products_id NOT IN ($product_id)";
     //                } else {
     //                    $pid_symbol_filters = "AND ptcp.products_id NOT IN ($pids_symbol_filter, $product_id)";
     //                }
     //                $pid_color = $class_pm->getProductsByColor(
     //                        $color_id[0],
     //                        false,
     //                        $pid_symbol_filters,
     //                        "ORDER BY pl.sold_monthly_3 DESC LIMIT $max_products_by_color");
     //                $pids = array_merge($pids, $pid_color);
     //            }
     //BRAND
     //            $brand_id = $p_data['p']['products_brand_id'];
     //            if ($brand_id) {
     //                $pids_color_filter = implode(",", $pids);
     //                if ($pids_color_filter == '') {
     //                    $pid_color_filters = "AND p.products_id NOT IN ($product_id)";
     //                } else {
     //                    $pid_color_filters = "AND p.products_id NOT IN ($pids_color_filter, $product_id)";
     //                }
     //                $pid_brand = $class_pb->getProducts(
     //                        $brand_id,
     //                        false,
     //                        $pid_color_filters,
     //                        "ORDER BY pl.sold_monthly_3 DESC LIMIT $max_products_by_brand");
     //                $pids = array_merge($pids, $pid_brand);
     //            }
     //CATEGORY
     $cat_id = $p_data['categories_id'];
     $pids_color_filter = implode(",", $pids);
     if ($pids_color_filter == '') {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_color_filters = "AND p.products_id NOT IN ({$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_color_filters = "AND p.products_id NOT IN ({$product_id})";
         }
     } else {
         if ($p_data['p']['products_brand_id'] == 14) {
             $pid_color_filters = "AND p.products_id NOT IN ({$pids_color_filter}, {$product_id}) AND p.products_brand_id = 14 ";
         } else {
             $pid_color_filters = "AND p.products_id NOT IN ({$pids_color_filter}, {$product_id})";
         }
     }
     $pid_cat = $class_pm->getProductsByCategory($cat_id, false, $pid_color_filters, "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products_by_category}");
     $pids = array_merge($pids, $pid_cat);
     //LIMIT ALL PRODUCTS BY 50
     $pids = array_slice($pids, 0, $max_products);
     $pids_implode = implode(",", $pids);
     if ($p_data['p']['products_brand_id'] == 14) {
         $pids = $class_pb->getProducts($p_data['p']['products_brand_id'], false, "AND p.products_id IN ({$pids_implode})", "ORDER BY pl.sold_monthly_3 DESC LIMIT {$max_products}");
     }
     return $pids;
 }
     }
     //Add to family if there products and family
     if (isset($tmp[0]) && $obj_family->id != '') {
         $status = $obj_family->addToFamily($tmp);
         $pids[] = "#box-pids-family|{$tmp['0']}";
     } else {
         $status = false;
     }
     $result = array();
     $result['status'] = (int) $status;
     $result['fid'] = $fid;
     $result['pids'] = implode(',', $pids);
     ajaxReturn($result);
 } elseif ($_POST['me_action'] == 'REMOVEFROMFAMILY') {
     $error = '';
     $fmembers = $obj_family->retrieveMembers();
     if ($obj_family->isMother($pid)) {
         $error = 'You cannot removed mother from family!';
     } elseif (count($fmembers) > 1) {
         $obj_family->removeFromFamily($pid);
     } else {
         $error = 'You cannot removed last product from family!';
     }
     $result = array();
     $result['pidremoved'] = "#box-pids-similar|{$pid}";
     $result['error'] = $error;
     ajaxReturn($result);
 } elseif ($_POST['me_action'] == 'UPDATEFAMILYNAME') {
     $fname = tep_db_prepare_input($_POST['fname']);
     $field_updated = array('family_name' => $fname);
     $obj_family->updateField($field_updated);