public function execute()
 {
     $type_model = new shopTypeModel();
     $types = $type_model->getAll('id');
     $category_model = new shopCategoryModel();
     $categories = $category_model->getFullTree('id, name, depth', true);
     $features_model = new shopFeatureModel();
     $features = $features_model->getAll('id');
     $data = array();
     $type_values = array();
     foreach ($types as $type_id => $type) {
         $type_values[] = array($type_id, $type['name']);
         $data[$type_id]['price'] = array('feature' => 'price');
         $data[$type_id]['type_id'] = array('feature' => 'type_id');
         $data[$type_id]['tag'] = array('feature' => 'tag');
     }
     $type_features_model = new shopTypeFeaturesModel();
     $rows = $type_features_model->getAll();
     foreach ($rows as $row) {
         if (isset($features[$row['feature_id']])) {
             $code = $features[$row['feature_id']]['code'];
             $data[$row['type_id']][$code] = array('feature' => $code, 'feature_id' => $row['feature_id']);
         }
     }
     $type_upselling_model = new shopTypeUpsellingModel();
     $rows = $type_upselling_model->getAll();
     foreach ($rows as $row) {
         $data[$row['type_id']][$row['feature']] = array('feature_id' => $row['feature_id'], 'feature' => $row['feature'], 'cond' => $row['cond'], 'value' => $row['value']);
     }
     foreach ($data as &$row) {
         $row = array_values($row);
     }
     unset($row);
     foreach ($types as &$type) {
         if ($type['upselling']) {
             $type['upselling_html'] = self::getConditionHTML($data[$type['id']], $features);
         }
     }
     unset($type);
     $fids = array();
     foreach ($features as $f_key => $f) {
         $features[$f_key]['selectable'] = (int) $f['selectable'];
         $features[$f_key]['multiple'] = (int) $f['multiple'];
         if ($f['selectable']) {
             $fids[$f['id']] = $f;
         }
     }
     if ($fids) {
         $fids = $features_model->getValues($fids);
         foreach ($fids as $feature_id => $f) {
             foreach ($f['values'] as $value_id => $value) {
                 $features[$feature_id]['values'][] = array($value_id, $value);
             }
         }
         unset($fids);
     }
     $features['type_id'] = array('name' => _w('Type'), 'type' => 'varchar', 'selectable' => 1, 'values' => $type_values);
     $this->view->assign(array('types' => $types, 'categories' => $categories, 'features' => $features, 'data' => $data));
 }
 public function deleteByField($field, $value = null)
 {
     if (is_array($field)) {
         $items = $this->getByField($field, $this->id);
     } else {
         $items = $this->getByField($field, $value, $this->id);
     }
     $res = false;
     if ($ids = array_keys($items)) {
         $res = parent::deleteByField($this->id, $ids);
         if ($res) {
             $type_upselling_model = new shopTypeUpsellingModel();
             $type_upselling_model->deleteByField('feature_id', $ids);
             $params_model = new shopProductFeaturesModel();
             $params_model->deleteByField('feature_id', $ids);
             $types = $this->groupByValueType($items);
             foreach ($types as $type => $type_ids) {
                 if ($model = self::getValuesModel($type)) {
                     $model->deleteByField('feature_id', $type_ids);
                 }
             }
             $this->deleteByField('parent_id', $ids);
         }
     }
     return $res;
 }
 public function execute()
 {
     $product_id = (int) waRequest::get('id');
     $product = new shopProduct($product_id);
     $type_model = new shopTypeModel();
     $type = $type_model->getById($product['type_id']);
     if ($product['cross_selling'] === null) {
         $product['cross_selling'] = $type['cross_selling'] ? 1 : 0;
     }
     if ($product['upselling'] === null) {
         $product['upselling'] = $type['upselling'];
     }
     // if manually
     if ($product['cross_selling'] == 2 || $product['upselling'] == 2) {
         $related_model = new shopProductRelatedModel();
         $related = $related_model->getAllRelated($product_id);
     } else {
         $related = array();
     }
     if ($type['upselling']) {
         $type_upselling_model = new shopTypeUpsellingModel();
         $data = $type_upselling_model->getByType($type['id']);
         $type['upselling_html'] = shopSettingsRecommendationsAction::getConditionHTML($data);
     }
     if ($type['cross_selling'] && substr($type['cross_selling'], 0, 9) == 'category/') {
         $category_model = new shopCategoryModel();
         $type['category'] = $category_model->getById(substr($type['cross_selling'], 9));
     }
     $this->view->assign(array('type' => $type, 'product' => $product, 'related' => $related));
 }
 protected function saveUpSelling()
 {
     $value = waRequest::post('value');
     $this->type_model->updateById($this->type_id, array('upselling' => $value));
     $type_upselling_model = new shopTypeUpsellingModel();
     $type_upselling_model->deleteByField('type_id', $this->type_id);
     if ($value) {
         $rows = array();
         $data = waRequest::post('data', array());
         foreach ($data as $feature => $row) {
             if (!isset($row['feature'])) {
                 continue;
             }
             $rows[] = array('type_id' => $this->type_id, 'feature' => $feature, 'feature_id' => isset($row['feature_id']) ? $row['feature_id'] : null, 'cond' => $row['cond'], 'value' => isset($row['value']) ? is_array($row['value']) ? implode(',', $row['value']) : $row['value'] : '');
         }
         if ($rows) {
             $type_upselling_model->multipleInsert($rows);
         }
         $this->response['type_id'] = $this->type_id;
         $this->response['data'] = array('price' => array('feature' => 'price'), 'tag' => array('feature' => 'tag'), 'type_id' => array('feature' => 'type_id'));
         $type_features_model = new shopTypeFeaturesModel();
         $rows = $type_features_model->getByType($this->type_id);
         foreach ($rows as $row) {
             $this->response['data'][$row['code']] = array('feature' => $row['code'], 'feature_id' => $row['feature_id']);
         }
         $data = $type_upselling_model->getByType($this->type_id);
         foreach ($data as $row) {
             $this->response['data'][$row['feature']] = array('feature_id' => $row['feature_id'], 'feature' => $row['feature'], 'cond' => $row['cond'], 'value' => $row['value']);
         }
         $this->response['html'] = shopSettingsRecommendationsAction::getConditionHTML($data);
         $this->response['data'] = array_values($this->response['data']);
     }
 }
 protected function upsellingPrepare($product_id, $auto_title = false)
 {
     $model = $this->getModel();
     if (isset($this->options['product'])) {
         $product = $this->options['product'];
         $conditions = $this->options['conditions'];
     } else {
         $product = new shopProduct($product_id);
         $type_upselling_model = new shopTypeUpsellingModel();
         $conditions = $type_upselling_model->getByField('type_id', $product['type'], true);
     }
     $this->where[] = 'p.id != ' . (int) $product_id;
     $sum = array();
     foreach ($conditions as $row) {
         if ($row['feature'] == 'tag') {
             $tag_model = new shopTagModel();
             $tag = $tag_model->getByName($row['value']);
             if ($tag) {
                 $this->where[] = 'pt.tag_id = ' . (int) $tag['id'];
                 $this->joins[] = array('table' => 'shop_product_tags', 'alias' => 'pt');
             }
             continue;
         } elseif ($row['feature'] == 'type_id') {
             if ($row['cond'] == 'same') {
                 $this->where[] = 'p.type_id = ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'notsame') {
                 $this->where[] = 'p.type_id != ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'is') {
                 $this->where[] = 'p.type_id = ' . (int) $row['value'];
             }
             continue;
         }
         switch ($row['cond']) {
             case 'between':
                 list($min, $max) = explode(',', $row['value']);
                 if ($model->fieldExists($row['feature'])) {
                     $v = $product[$row['feature']];
                 } else {
                     $v = isset($product['features'][$row['feature']]) ? $product['features'][$row['feature']] : null;
                 }
                 if (!$v) {
                     continue;
                 }
                 $min = $v * (double) (100 + $min) / 100;
                 $max = $v * (double) (100 + $max) / 100;
                 $v = str_replace(',', '.', $v);
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . ' > ' . str_replace(',', '.', $min);
                     $this->where[] = 'p.' . $row['feature'] . ' < ' . str_replace(',', '.', $max);
                     $sum[] = 'ABS(p.' . $row['feature'] . ' - ' . $v . ')/' . $v;
                 }
                 break;
             case 'is':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " = '" . $model->escape($row['value']) . "'";
                 } else {
                     $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id = " . (int) $row['value']);
                     $this->group_by = 'p.id';
                 }
                 break;
             case 'any':
             case 'all':
                 if ($model->fieldExists($row['feature'])) {
                     //$this->where[] = 'p.'.$row['feture']." = '".$model->escape($row['value'])."'";
                 } else {
                     if ($row['value']) {
                         $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id IN (" . $row['value'] . ")");
                         $this->group_by = 'p.id';
                     } else {
                         $this->where[] = '0';
                     }
                 }
                 break;
             case 'notsame':
             case 'same':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " " . ($row['cond'] == 'notsame' ? '!' : '') . "= '" . $model->escape($product->features[$row['feature']]) . "'";
                 } else {
                     $product_features_model = new shopProductFeaturesModel();
                     $rows = $product_features_model->getByField(array('product_id' => $product['id'], 'sku_id' => null, 'feature_id' => $row['feature_id']), true);
                     $values = array();
                     foreach ($rows as $r) {
                         $values[] = $r['feature_value_id'];
                     }
                     if ($values) {
                         $alias = $this->addJoin('shop_product_features');
                         $this->where[] = $alias . ".feature_id = " . $row['feature_id'];
                         $this->where[] = $alias . ".feature_value_id " . (count($values) == 1 ? ($row['cond'] == 'notsame' ? '!' : '') . "= " . $values[0] : ($row['cond'] == 'notsame' ? 'NOT ' : '') . "IN (" . implode(',', $values) . ")");
                         $this->group_by = 'p.id';
                     }
                 }
                 break;
         }
     }
     if ($sum) {
         $this->fields[] = '(' . implode(' + ', $sum) . ') AS upselling_deviation';
         $this->order_by = 'upselling_deviation';
     }
 }
 /**
  * Returns products identified as upselling items for current product.
  *
  * @param int $limit Maximum number of items to be returned
  * @param bool $available_only Whether only products with positive or unlimited stock count must be returned
  *
  * @return array Array of upselling products' data sub-arrays
  */
 public function upSelling($limit = 5, $available_only = false)
 {
     $upselling = $this->getData('upselling');
     // upselling on (usign similar settting for type)
     if ($upselling == 1 || $upselling === null) {
         $type_upselling_model = new shopTypeUpsellingModel();
         $conditions = $type_upselling_model->getByField('type_id', $this->getData('type_id'), true);
         if ($conditions) {
             $collection = new shopProductsCollection('upselling/' . $this->getId(), array('product' => $this, 'conditions' => $conditions));
             if ($available_only) {
                 $collection->addWhere('(p.count > 0 OR p.count IS NULL)');
             }
             return $collection->getProducts('*', $limit);
         } else {
             return array();
         }
     } elseif (!$upselling) {
         return array();
     } else {
         $collection = new shopProductsCollection('related/upselling/' . $this->getId());
         if ($available_only) {
             $collection->addWhere('(p.count > 0 OR p.count IS NULL)');
         }
         return $collection->getProducts('*', $limit);
     }
 }