protected function getTotalCount($options)
 {
     $total_count = waRequest::get('total_count', null, waRequest::TYPE_INT);
     if (!$total_count) {
         $pslm = new shopProductStocksLogModel();
         $total_count = $pslm->countByField($options);
     }
     return $total_count;
 }
 public function postExecute($order_id = null, $result = null)
 {
     $data = parent::postExecute($order_id, $result);
     if ($order_id != null) {
         $log_model = new waLogModel();
         $log_model->add('order_delete', $order_id);
         $order_model = new shopOrderModel();
         $app_settings_model = new waAppSettingsModel();
         if ($data['before_state_id'] != 'refunded') {
             $update_on_create = $app_settings_model->get('shop', 'update_stock_count_on_create_order');
             // for logging changes in stocks
             shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was deleted', array('order_id' => $order_id));
             if ($update_on_create) {
                 $order_model->returnProductsToStocks($order_id);
             } else {
                 if (!$update_on_create && $data['before_state_id'] != 'new') {
                     $order_model->returnProductsToStocks($order_id);
                 }
             }
             shopProductStocksLogModel::clearContext();
         }
         $order = $order_model->getById($order_id);
         if ($order && $order['paid_date']) {
             // Remember paid_date in log params for Restore action
             $olpm = new shopOrderLogParamsModel();
             $olpm->insert(array('name' => 'paid_date', 'value' => $order['paid_date'], 'order_id' => $order_id, 'log_id' => $data['id']));
             // Empty paid_date and update stats so that deleted orders do not affect reports
             $order_model->updateById($order_id, array('paid_date' => null, 'paid_year' => null, 'paid_month' => null, 'paid_quarter' => null));
             $order_model->recalculateProductsTotalSales($order_id);
             shopCustomers::recalculateTotalSpent($order['contact_id']);
         }
     }
     return $data;
 }
 public function postExecute($order_id = null, $result = null)
 {
     $data = parent::postExecute($order_id, $result);
     if ($order_id != null) {
         $log_model = new waLogModel();
         $log_model->add('order_restore', $order_id);
         $order_model = new shopOrderModel();
         $app_settings_model = new waAppSettingsModel();
         if ($this->state_id != 'refunded') {
             // for logging changes in stocks
             shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was restored', array('order_id' => $order_id));
             $update_on_create = $app_settings_model->get('shop', 'update_stock_count_on_create_order');
             if ($update_on_create) {
                 $order_model->reduceProductsFromStocks($order_id);
             } else {
                 if (!$update_on_create && $this->state_id != 'new') {
                     $order_model->reduceProductsFromStocks($order_id);
                 }
             }
             shopProductStocksLogModel::clearContext();
         }
         $order = $order_model->getById($order_id);
         if ($order && $order['paid_date']) {
             shopAffiliate::applyBonus($order_id);
             shopCustomers::recalculateTotalSpent($order['contact_id']);
         }
     }
     return $data;
 }
 public function postExecute($order_id = null, $result = null)
 {
     $data = parent::postExecute($order_id, $result);
     $log_model = new waLogModel();
     $log_model->add('order_complete', $order_id);
     $order_model = new shopOrderModel();
     if (is_array($order_id)) {
         $order = $order_id;
         $order_id = $order['id'];
     } else {
         $order = $order_model->getById($order_id);
     }
     shopCustomers::recalculateTotalSpent($order['contact_id']);
     if ($order !== null) {
         $log_model = new shopOrderLogModel();
         $state_id = $log_model->getPreviousState($order_id);
         $app_settings_model = new waAppSettingsModel();
         $update_on_create = $app_settings_model->get('shop', 'update_stock_count_on_create_order');
         if (!$update_on_create && $state_id == 'new') {
             // jump through 'processing' state - reduce
             // for logging changes in stocks
             shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was completed', array('order_id' => $order_id));
             $order_model = new shopOrderModel();
             $order_model->reduceProductsFromStocks($order_id);
             shopProductStocksLogModel::clearContext();
         }
         $order_model->recalculateProductsTotalSales($order_id);
     }
     return $data;
 }
 public function execute()
 {
     $sku_id = waRequest::post('sku_id', 0, waRequest::TYPE_INT);
     $src_stock = waRequest::post('src_stock', 0, waRequest::TYPE_INT);
     $dst_stock = waRequest::post('dst_stock', 0, waRequest::TYPE_INT);
     $count = waRequest::post('count', 0, waRequest::TYPE_INT);
     if ($src_stock == $dst_stock || !$src_stock || !$dst_stock || !$count) {
         $this->errors[] = _w("Error when transfer");
         return;
     }
     $product_skus_model = new shopProductSkusModel();
     shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_STOCK);
     if (!$product_skus_model->transfer($sku_id, $count, $src_stock, $dst_stock)) {
         $this->errors[] = _w("Error when transfer");
         return;
     }
     shopProductStocksLogModel::clearContext();
     $sku = $product_skus_model->getById($sku_id);
     /*
     $product_stocks_model = new shopProductStocksModel();
     $data = $product_stocks_model->getStocksOfProduct($sku['product_id'], array($src_stock, $dst_stock), 'sku.count DESC');
     
     foreach ($data as &$stock) {
         foreach ($stock as &$stock_sku) {
             $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count']);
         }
     }
     unset($stock, $stock_sku);
     */
     $stock_skus = array();
     $product_model = new shopProductModel();
     $data = $product_model->getProductStocksByProductId($sku['product_id']);
     if (isset($data[$sku['product_id']])) {
         $data = $data[$sku['product_id']];
         if (isset($data['stocks'][$src_stock])) {
             $stock_skus[$src_stock] = array();
             foreach ($data['stocks'][$src_stock] as $stock_sku) {
                 $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count'], $src_stock);
                 $stock_skus[$src_stock][] = $stock_sku;
             }
         }
         if (isset($data['stocks'][$dst_stock])) {
             $stock_skus[$dst_stock] = array();
             foreach ($data['stocks'][$dst_stock] as $stock_sku) {
                 $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count'], $dst_stock);
                 $stock_skus[$dst_stock][] = $stock_sku;
             }
         }
     }
     $this->response = array('stocks' => $stock_skus ? $stock_skus : new stdClass(), 'product_id' => $sku['product_id']);
 }
 public function postExecute($order_id = null, $result = null)
 {
     $data = parent::postExecute($order_id, $result);
     if ($order_id != null) {
         $log_model = new waLogModel();
         $log_model->add('order_process', $order_id);
         $app_settings_model = new waAppSettingsModel();
         if (!$app_settings_model->get('shop', 'update_stock_count_on_create_order')) {
             // for logging changes in stocks
             shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was processed', array('order_id' => $order_id));
             $order_model = new shopOrderModel();
             $order_model->reduceProductsFromStocks($order_id);
             shopProductStocksLogModel::clearContext();
         }
     }
     return $data;
 }
 public function postExecute($order_id = null, $result = null)
 {
     $data = parent::postExecute($order_id, $result);
     $order_model = new shopOrderModel();
     if (is_array($order_id)) {
         $order = $order_id;
         $order_id = $order['id'];
     } else {
         $order = $order_model->getById($order_id);
     }
     shopCustomers::recalculateTotalSpent($order['contact_id']);
     if ($order_id != null) {
         $log_model = new waLogModel();
         $log_model->add('order_refund', $order_id);
         $order_model = new shopOrderModel();
         $order_model->updateById($order_id, array('paid_date' => null, 'paid_year' => null, 'paid_month' => null, 'paid_quarter' => null));
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was refunded', array('order_id' => $order_id));
         $order_model->returnProductsToStocks($order_id);
         shopAffiliate::cancelBonus($order_id);
         $order_model->recalculateProductsTotalSales($order_id);
     }
     return $data;
 }
 public function postExecute($params = null, $result = null)
 {
     if (is_array($params)) {
         $order_id = $params['order_id'];
     } else {
         $order_id = $params;
     }
     $data = parent::postExecute($order_id, $result);
     $log_model = new waLogModel();
     $log_model->add('order_ship', $order_id);
     $log_model = new shopOrderLogModel();
     $state_id = $log_model->getPreviousState($order_id);
     $app_settings_model = new waAppSettingsModel();
     $update_on_create = $app_settings_model->get('shop', 'update_stock_count_on_create_order');
     if (!$update_on_create && $state_id == 'new') {
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was shipped', array('order_id' => $order_id));
         // jump through 'processing' state - reduce
         $order_model = new shopOrderModel();
         $order_model->reduceProductsFromStocks($order_id);
         shopProductStocksLogModel::clearContext();
     }
     return $data;
 }
 /**
  * @usedby stepImport
  * @param $data
  * @return bool
  */
 private function stepImportSku($data)
 {
     static $sku_primary;
     static $sku_secondary;
     static $empty_sku;
     static $empty;
     if (!isset($sku_primary)) {
         $secondary = explode(':', $this->data['secondary']);
         $sku_primary = end($secondary);
     }
     if (!isset($sku_secondary)) {
         $extra_secondary = explode(':', $this->data['extra_secondary']);
         $sku_secondary = end($extra_secondary);
     }
     if (!isset($empty)) {
         $empty = $this->reader->getEmpty();
     }
     if (!isset($empty_sku)) {
         $empty_sku = ifset($empty['skus'][-1], array());
     }
     $data += $empty;
     if ($product = $this->findProduct($data)) {
         $item_sku_id = false;
         $current_id = ifset($this->data['map'][self::STAGE_PRODUCT]);
         $id = $product->getId();
         if ($this->emulate()) {
             $target = $id ? 'found' : 'add';
             $target_sku = 'add';
         } else {
             $target = $id ? 'update' : 'new';
             $target_sku = 'new';
         }
         $key = null;
         $sku_only = false;
         $product_exists = $this->emulate() ? $product->__hash == $current_id : $id;
         if ($id && isset($data['skus'][-1])) {
             if ($this->emulate() ? $product->__hash == $current_id : $id == $current_id) {
                 $sku_only = true;
             }
             $sku = $data['skus'][-1] + $empty_sku;
             $this->castSku($sku);
             unset($data['skus'][-1]);
             $item_sku_id = -1;
             $matches = 0;
             foreach ($product->skus as $sku_id => $current_sku) {
                 if ($current_sku[$sku_primary] === ifset($sku[$sku_primary], '')) {
                     //extra workaround for empty primary attribute
                     if (false && $current_sku[$sku_primary] === '' && $sku_secondary) {
                         if (ifset($sku[$sku_secondary], '') !== $current_sku[$sku_secondary]) {
                             continue;
                         }
                     }
                     if (++$matches == 1) {
                         $item_sku_id = $sku_id;
                         $target_sku = $this->emulate() ? 'found' : 'update';
                         $sku = array_merge($current_sku, $sku);
                     } else {
                         $target_sku = 'skip';
                         $item_sku_id = false;
                         break;
                     }
                 }
             }
             if ($item_sku_id !== false) {
                 if ($item_sku_id < 0 && !isset($sku['available'])) {
                     $sku['available'] = true;
                 }
                 if (!$sku_only && !$product->skus) {
                     $data['sku_id'] = $item_sku_id;
                 }
                 $data['skus'][$item_sku_id] = $sku;
                 $key = 's:';
                 if ($item_sku_id > 0) {
                     $key .= 'u:' . $item_sku_id;
                 } else {
                     $key .= 'i:';
                     $key .= $this->getKey(array($sku_primary => ifset($sku[$sku_primary], '')));
                 }
             } else {
                 unset($data['skus']);
             }
         } elseif (isset($data['skus'][-1])) {
             if ($this->emulate() && $product->__hash == $current_id) {
                 $sku_only = true;
                 $item_sku_id = true;
             } else {
             }
             $sku = $data['skus'][-1] + $empty_sku;
             $key = 's:';
             $key .= 'i:';
             $key .= $this->getKey(array($sku_primary => ifset($sku[$sku_primary], '')));
         } elseif (!empty($data['features_selectable'])) {
             if ($product_exists) {
                 $target = $this->emulate() ? 'found' : 'update';
             }
             //TODO recount virtual SKUs count
             $key = 's:v:';
             $key .= $this->getKey($data['features_selectable']);
             if ($id) {
                 $target_sku = $this->emulate() ? 'found' : 'update';
             } else {
                 //add
             }
         }
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_IMPORT);
         if ($sku_only || empty($this->data['primary'])) {
             if ($product_exists && $item_sku_id !== false) {
                 if (!$this->emulate($product->__hash, $key)) {
                     $truncated_data = array('skus' => $data['skus']);
                     $virtual_fields = array();
                     foreach ($virtual_fields as $field) {
                         if (isset($data[$field])) {
                             $truncated_data['skus'][$item_sku_id][$field] = $data[$field];
                         }
                     }
                     if (isset($data['features'])) {
                         $model = new shopFeatureModel();
                         $features = $model->getMultipleSelectableFeaturesByType($data['type_id'], 'code');
                         if (!$features) {
                             $features = array();
                         }
                         $features['weight'] = true;
                         foreach (array_keys($features) as $code) {
                             if (isset($data['features'][$code])) {
                                 if (!isset($truncated_data['skus'][$item_sku_id]['features'])) {
                                     $truncated_data['skus'][$item_sku_id]['features'] = array();
                                 }
                                 $truncated_data['skus'][$item_sku_id]['features'][$code] = $data['features'][$code];
                             }
                         }
                     }
                     $product->save($truncated_data);
                     $this->data['map'][self::STAGE_PRODUCT] = $product->getId();
                 } else {
                     $this->data['map'][self::STAGE_PRODUCT] = $product->__hash;
                 }
             }
         } else {
             if (!$this->emulate($product->__hash, $key)) {
                 $product->save($data);
                 $this->data['map'][self::STAGE_PRODUCT] = $product->getId();
                 if (!empty($data['images'])) {
                     $this->data['map'][self::STAGE_IMAGE] = $data['images'];
                     $this->data['count'][self::STAGE_IMAGE] += count($data['images']);
                 }
             } else {
                 $this->data['map'][self::STAGE_PRODUCT] = $product->__hash;
             }
             $this->data['processed_count'][self::STAGE_PRODUCT][$target]++;
         }
         shopProductStocksLogModel::clearContext();
         if ($product->getId() || $this->emulate()) {
             $this->data['processed_count'][self::STAGE_SKU][$target_sku]++;
         }
     } else {
         $this->data['processed_count'][self::STAGE_PRODUCT]['rights']++;
     }
     return true;
 }
Ejemplo n.º 10
0
 /**
  * Set count for stock and sku. 
  * Make insert, update or delete depending on input parameters and current state (sku-stock record in table)
  * Take into account stocking log (@see shopProductStocksLog)
  * 
  * @param string[mixed] $data Specify sku ID, stock ID, count
  * @param int           $data['sku_id'] sku ID, obligatory
  * @param int           $data['product_id'] product ID, optional
  * @param int           $data['stock_id'] stock ID, obligatory
  * @param int|null      $data['count'] count, obligatory. 
  * 
  * <code>
  * array(
  *     'sku_id' => 123,      // sku ID, obligatory
  *     'product_id' => 111,  // product ID, optional
  *     'stock_id' => 23,     // stock ID, obligatory
  *     'count' => 12,        // Maybe null or integer greater or equals 0
  * )
  * </code>
  * 
  * @return boolean
  */
 public function set($data)
 {
     if (empty($data['sku_id']) || empty($data['stock_id'])) {
         return false;
     }
     // isset doesn't work correctly with null
     if (!array_key_exists('count', $data)) {
         return false;
     }
     $count = $data['count'];
     if (empty($data['product_id'])) {
         $product_skus_model = new shopProductSkusModel();
         $data['product_id'] = $product_skus_model->select('product_id')->where('sku_id = :sku_id', array('sku_id' => $data['sku_id']))->fetchField();
     }
     if (empty($data['product_id'])) {
         return false;
     }
     $key = array('sku_id' => $data['sku_id'], 'stock_id' => $data['stock_id']);
     $item = $this->getByField($key);
     if ($item && $count !== null && $count == $item['count'] || !$item && $count === null) {
         // nothing to update
         return true;
     }
     $log_data = array('product_id' => $data['product_id'], 'sku_id' => $data['sku_id'], 'stock_id' => $data['stock_id'], 'before_count' => null, 'after_count' => $count);
     if ($item) {
         $log_data['before_count'] = $item['count'];
     }
     if ($count === null) {
         $op = 'delete';
     } else {
         if ($item) {
             $op = 'update';
         } else {
             $op = 'insert';
         }
     }
     if ($op == 'delete') {
         $this->deleteByField($key);
     } else {
         if ($op == 'update') {
             $this->updateByField($key, $data);
         } else {
             $this->insert($data);
         }
     }
     $log_model = new shopProductStocksLogModel();
     $log_model->add($log_data);
     return true;
 }
Ejemplo n.º 11
0
 private function writeOffCount($product_id, $sku_id)
 {
     /**
      * @var shopProductStocksLogModel
      */
     static $log_model = null;
     $count = $this->select('count')->where('id=i:sku_id', array('sku_id' => $sku_id))->fetchField();
     if ($count === null) {
         return;
     }
     if (!$log_model) {
         $log_model = new shopProductStocksLogModel();
     }
     $log_model->add(array('product_id' => $product_id, 'sku_id' => $sku_id, 'before_count' => $count, 'after_count' => 0));
 }
Ejemplo n.º 12
0
 public function updateStockCount($data)
 {
     if (!$data) {
         return;
     }
     $product_skus_model = new shopProductSkusModel();
     $product_stocks_model = new shopProductStocksModel();
     $stocks_log_model = new shopProductStocksLogModel();
     $sku_ids = array_map('intval', array_keys($data));
     if (!$sku_ids) {
         return;
     }
     $skus = $product_skus_model->select('id,product_id')->where("id IN(" . implode(',', $sku_ids) . ")")->fetchAll('id');
     $sku_ids = array_keys($skus);
     if (!$sku_ids) {
         return;
     }
     $product_ids = array();
     foreach ($data as $sku_id => $sku_stock) {
         $sku_id = (int) $sku_id;
         if (!isset($skus[$sku_id]['product_id'])) {
             continue;
         }
         $product_id = $skus[$sku_id]['product_id'];
         foreach ($sku_stock as $stock_id => $count) {
             $stock_id = (int) $stock_id;
             if ($stock_id) {
                 $item = $product_stocks_model->getByField(array('sku_id' => $sku_id, 'stock_id' => $stock_id));
                 if (!$item) {
                     continue;
                 }
                 $product_stocks_model->set(array('sku_id' => $sku_id, 'product_id' => $product_id, 'stock_id' => $stock_id, 'count' => $item['count'] + $count));
             } else {
                 $old_count = $product_skus_model->select('count')->where('id=i:sku_id', array('sku_id' => $sku_id))->fetchField();
                 if ($old_count !== null) {
                     $log_data = array('product_id' => $product_id, 'sku_id' => $sku_id, 'before_count' => $old_count, 'after_count' => $old_count + $count, 'diff_count' => $count);
                     $stocks_log_model->insert($log_data);
                 }
                 $this->exec("UPDATE `shop_product_skus` SET count = count + ({$count})\n                        WHERE id = {$sku_id}");
             }
             if (isset($skus[$sku_id]['product_id'])) {
                 $product_ids[] = $product_id;
             }
         }
     }
     if (!$product_ids) {
         return;
     }
     // correct sku counters
     $sql = "\n            UPDATE `shop_product_skus` sk JOIN (\n                SELECT sk.id, SUM(st.count) AS count FROM `shop_product_skus` sk\n                JOIN `shop_product_stocks` st ON sk.id = st.sku_id\n                WHERE sk.id IN(" . implode(',', $sku_ids) . ")\n                GROUP BY sk.id\n                ORDER BY sk.id\n            ) r ON sk.id = r.id\n            SET sk.count = r.count\n            WHERE sk.count IS NOT NULL";
     $this->exec($sql);
     // correct product counters
     $sql = "\n            UPDATE `shop_product` p JOIN (\n                SELECT p.id, SUM(sk.count) AS count FROM `shop_product` p\n                JOIN `shop_product_skus` sk ON p.id = sk.product_id\n                WHERE p.id IN(" . implode(',', array_unique($product_ids)) . ") AND sk.available = 1\n                GROUP BY p.id\n                ORDER BY p.id\n            ) r ON p.id = r.id\n            SET p.count = r.count\n            WHERE p.count IS NOT NULL";
     $this->exec($sql);
 }
Ejemplo n.º 13
0
 protected function getList($options)
 {
     $pslm = new shopProductStocksLogModel();
     return $pslm->getList('*,stock_name,sku_name,product_name', $options);
 }
 public function execute($data = null)
 {
     $order_model = new shopOrderModel();
     $order = $order_model->getById($data['id']);
     $subtotal = 0;
     $services = $products = array();
     foreach ($data['items'] as $item) {
         if ($item['service_id']) {
             $services[] = $item['service_id'];
         } else {
             $products[] = $item['product_id'];
         }
     }
     $service_model = new shopServiceModel();
     $product_model = new shopProductModel();
     $services = $service_model->getById($services);
     $products = $product_model->getById($products);
     foreach ($data['items'] as &$item) {
         $item['currency'] = $order['currency'];
         $item['price'] = $this->price($item['price']);
         if ($item['service_id']) {
             $item['service'] = $services[$item['service_id']];
         } else {
             $item['product'] = $products[$item['product_id']];
         }
         $subtotal += $item['price'] * $item['quantity'];
     }
     unset($item);
     foreach (array('shipping', 'discount') as $k) {
         if (!isset($data[$k])) {
             $data[$k] = 0;
         }
     }
     $contact = new waContact($order['contact_id']);
     $shipping_address = $contact->getFirst('address.shipping');
     if (!$shipping_address) {
         $shipping_address = $contact->getFirst('address');
     }
     $shipping_address = $shipping_address ? $shipping_address['data'] : array();
     $billing_address = $contact->getFirst('address.billing');
     if (!$billing_address) {
         $billing_address = $contact->getFirst('address');
     }
     $billing_address = $billing_address ? $billing_address['data'] : array();
     $discount_rate = $subtotal ? $data['discount'] / $subtotal : 0;
     $taxes = shopTaxes::apply($data['items'], array('shipping' => $shipping_address, 'billing' => $billing_address, 'discount_rate' => $discount_rate), $order['currency']);
     $tax = $tax_included = 0;
     foreach ($taxes as $t) {
         if (isset($t['sum'])) {
             $tax += $t['sum'];
         }
         if (isset($t['sum_included'])) {
             $tax_included += $t['sum_included'];
         }
     }
     $data['tax'] = $tax_included + $tax;
     $data['total'] = $subtotal + $tax + $this->price($data['shipping']) - $this->price($data['discount']);
     // for logging changes in stocks
     shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was edited', array('order_id' => $data['id']));
     // update
     $order_model->update($data, $data['id']);
     $log_model = new waLogModel();
     $log_model->add('order_edit', $data['id']);
     shopProductStocksLogModel::clearContext();
     if (!empty($data['params'])) {
         $params_model = new shopOrderParamsModel();
         $params_model->set($data['id'], $data['params'], false);
     }
     return true;
 }
Ejemplo n.º 15
0
 public function execute()
 {
     $update = waRequest::post('update');
     // just update one or any field of product
     if ($update) {
         $this->update($update);
         return;
     }
     $data = waRequest::post('product');
     $id = empty($data['id']) || !intval($data['id']) ? null : $data['id'];
     if (!$id && isset($data['id'])) {
         unset($data['id']);
     }
     # edit product info - check rights
     $product_model = new shopProductModel();
     if ($id) {
         if (!$product_model->checkRights($id)) {
             throw new waRightsException(_w("Access denied"));
         }
     } else {
         if (!$product_model->checkRights($data)) {
             throw new waRightsException(_w("Access denied"));
         }
     }
     $skus = waRequest::post('skus', array());
     if (isset($data['skus'])) {
         foreach ($skus as $s_id => $s) {
             if (isset($data['skus'][$s_id])) {
                 $data['skus'][$s_id] += $s;
             } else {
                 $data['skus'][$s_id] = $s;
             }
         }
     } else {
         $data['skus'] = $skus;
     }
     if (empty($data['categories'])) {
         $data['categories'] = array();
     }
     if (empty($data['tags'])) {
         $data['tags'] = array();
     }
     if (empty($data['features_selectable'])) {
         $data['features_selectable'] = array();
     }
     # verify sku_type before save
     if ($data['type_id']) {
         $features_model = new shopFeatureModel();
         if ($features_model->isTypeMultipleSelectable($data['type_id'])) {
             if ($data['sku_type'] == shopProductModel::SKU_TYPE_SELECTABLE) {
                 if (empty($data['features_selectable'])) {
                     throw new waException(_w("Check at least one feature value"));
                 }
             }
         } else {
             $data['sku_type'] = shopProductModel::SKU_TYPE_FLAT;
         }
     } else {
         $data['sku_type'] = shopProductModel::SKU_TYPE_FLAT;
     }
     if ($data['sku_type'] == shopProductModel::SKU_TYPE_FLAT) {
         $data['features_selectable'] = array();
     }
     try {
         $product = new shopProduct($id);
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_PRODUCT);
         if ($product->save($data, true, $this->errors)) {
             $features_counts = null;
             if ($product->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
                 $features_counts = array();
                 foreach ($product->features_selectable as $f) {
                     if (isset($f['selected'])) {
                         $features_counts[] = $f['selected'];
                     } else {
                         $features_counts[] = count($f['values']);
                     }
                 }
                 $features_total_count = array_product($features_counts);
                 $this->response['features_selectable_strings'] = array('options' => implode(' x ', $features_counts) . ' ' . _w('option', 'options', $features_total_count), 'skus' => _w('%d SKU in total', '%d SKUs in total', $features_total_count));
             }
             shopProductStocksLogModel::clearContext();
             if ($id) {
                 $this->logAction('product_edit', $id);
             } else {
                 $this->logAction('product_add', $product->getId());
             }
             $this->response['id'] = $product->getId();
             $this->response['name'] = $product->name;
             $this->response['url'] = $product->url;
             $this->response['frontend_urls'] = $this->getUrl($product);
             $this->response['raw'] = $this->workupData($product->getData());
             $sales_rate = waRequest::post('sales_rate', 0, waRequest::TYPE_STRING_TRIM);
             $sales_rate = (double) str_replace(',', '.', $sales_rate);
             $runout = $product->getRunout($sales_rate);
             if (!empty($runout['product'])) {
                 $runout['product']['date_str'] = wa_date("humandate", $runout['product']['date']);
                 $runout['product']['days_str'] = _w('%d day', '%d days', $runout['product']['days']);
                 if ($runout['product']['days'] < 3 * 365 && $runout['product']['days'] > 0) {
                     $runout['product_str'] = sprintf(_w('Based on last 30 days sales dynamic (%d items of %s sold during last 30 days), you will run out of %s in <strong>%d days</strong> (on %s)'), $sales_rate * 30, $product->name, $product->name, $runout['product']['days'], wa_date("humandate", $runout['product']['date']));
                 }
             } else {
                 $runout['product'] = new stdClass();
                 /* {} */
             }
             if (!empty($runout['sku'])) {
                 foreach ($runout['sku'] as &$sk_r) {
                     if (empty($sk_r['stock'])) {
                         $sk_r['date_str'] = wa_date("humandate", $sk_r['date']);
                         $sk_r['days_str'] = _w('%d day', '%d days', $sk_r['days']);
                     } else {
                         foreach ($sk_r['stock'] as &$st_r) {
                             $st_r['date_str'] = wa_date("humandate", $st_r['date']);
                             $st_r['days_str'] = _w('%d day', '%d days', $st_r['days']);
                         }
                     }
                 }
                 unset($sk_r, $st_r);
             } else {
                 $runout['sku'] = new stdClass();
                 /* {} */
             }
             $this->response['raw']['runout'] = $runout;
             $this->response['storefront_map'] = $product_model->getStorefrontMap($product->id);
         }
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function postExecute($order_id = null, $result = null)
 {
     $order_id = $result['order_id'];
     $data = is_array($result) ? $result : array();
     $data['order_id'] = $order_id;
     $data['action_id'] = $this->getId();
     $data['before_state_id'] = '';
     $data['after_state_id'] = 'new';
     $order_log_model = new shopOrderLogModel();
     $order_log_model->add($data);
     /**
      * @event order_action.create
      */
     wa('shop')->event('order_action.create', $data);
     $order_model = new shopOrderModel();
     $order = $order_model->getById($order_id);
     $params_model = new shopOrderParamsModel();
     $order['params'] = $params_model->get($order_id);
     // send notifications
     shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data));
     // Update stock count, but take into account 'update_stock_count_on_create_order'-setting
     $app_settings_model = new waAppSettingsModel();
     if ($app_settings_model->get('shop', 'update_stock_count_on_create_order')) {
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was placed', array('order_id' => $order_id));
         $order_model->reduceProductsFromStocks($order_id);
         shopProductStocksLogModel::clearContext();
     }
     return $order_id;
 }
Ejemplo n.º 17
0
 protected function assignReportsData(shopProduct $product)
 {
     $order_model = new shopOrderModel();
     $sales_total = $order_model->getTotalSalesByProduct($product['id'], $product['currency']);
     $this->view->assign('sales', $sales_total);
     $profit = $sales_total['total'];
     $rows = $order_model->getSalesByProduct($product['id']);
     $date = strtotime(date('Y-m-d') . " -30 day");
     $sales_data = array();
     $i = 0;
     while ($date < time()) {
         $date = date('Y-m-d', $date);
         $sales_data[] = array($i++, isset($rows[$date]) ? (double) $rows[$date] : 0);
         $date = strtotime($date . " +1 day");
     }
     $this->view->assign('sales_plot_data', array($sales_data));
     if (count($product['skus']) > 1) {
         $sku_sales_data = array();
         $rows = $order_model->getTotalSkuSalesByProduct($product['id'], $product['currency']);
         foreach ($rows as $sku_id => $v) {
             $sku_sales_data[] = array($product['skus'][$sku_id]['name'], (double) $v['total']);
             if (!(double) $v['purchase']) {
                 $profit = false;
             } elseif ($profit) {
                 $profit -= $v['purchase'];
             }
         }
         $this->view->assign('sku_plot_data', array($sku_sales_data));
     } else {
         if ((double) $sales_total['purchase']) {
             $profit -= $sales_total['purchase'];
         } else {
             $profit = false;
         }
     }
     if ($profit) {
         $this->view->assign('profit', $profit);
     }
     $runout = array();
     $sales_rate = 0;
     if ($sales_total['quantity'] >= 3) {
         // < 3 means not enough data for proper statistic
         $sales_rate = $sales_total['quantity'] / 30;
         $runout = $product->getRunout($sales_rate);
     }
     $this->view->assign('runout', $runout);
     $this->view->assign('sales_rate', $sales_rate);
     $stocks_log_model = new shopProductStocksLogModel();
     $stocks_log = $stocks_log_model->getList('*,stock_name,sku_name,product_name', array('where' => array('product_id' => $product->id), 'limit' => 5, 'order' => 'datetime DESC'));
     $this->view->assign('stocks_log', $stocks_log);
 }
Ejemplo n.º 18
0
 private function workupList(&$list, $fields)
 {
     if (!$list) {
         return;
     }
     foreach ($list as &$v) {
         $v['icon'] = shopProductStocksLogModel::getIcon($v['type']);
         if (!$v['description']) {
             if ($v['after_count'] === null) {
                 $v['description'] = _w('In stock value updated to ∞');
             } else {
                 $v['description'] = sprintf(_w('In stock value updated to %d'), $v['after_count']);
             }
         } else {
             if ($v['type'] == self::TYPE_ORDER) {
                 $v['description'] = sprintf(_w($v['description']), '<a href="?action=orders#/order/' . $v['order_id'] . '/">' . shopHelper::encodeOrderId($v['order_id']) . '</a>');
             }
         }
     }
     unset($v);
     $stock_ids = array();
     foreach ($list as $v) {
         $stock_ids[] = $v['stock_id'];
     }
     $model = new shopStockModel();
     $stocks = $model->getByField('id', array_unique($stock_ids), 'id');
     foreach ($list as &$v) {
         if (isset($stocks[$v['stock_id']])) {
             $v['stock_name'] = $stocks[$v['stock_id']]['name'];
         }
     }
     unset($v);
     foreach ($fields as $f) {
         if ($f == 'sku_name') {
             $sku_ids = array();
             foreach ($list as $v) {
                 $sku_ids[] = $v['sku_id'];
             }
             $model = new shopProductSkusModel();
             $skus = $model->select('id,sku,name')->where("id IN(" . implode(',', array_unique($sku_ids)) . ")")->fetchAll('id');
             foreach ($list as &$v) {
                 if (isset($skus[$v['sku_id']])) {
                     $v['sku_name'] = $skus[$v['sku_id']]['name'];
                     if ($v['sku_name']) {
                         if ($skus[$v['sku_id']]['sku']) {
                             $v['sku_name'] .= ' (' . $skus[$v['sku_id']]['sku'] . ')';
                         }
                     } else {
                         if ($skus[$v['sku_id']]['sku']) {
                             $v['sku_name'] = $skus[$v['sku_id']]['sku'];
                         }
                     }
                 }
             }
             unset($v);
         }
         if ($f == 'product_name') {
             $product_ids = array();
             foreach ($list as $v) {
                 $product_ids[] = $v['product_id'];
             }
             $model = new shopProductModel();
             $products = $model->select('id,name')->where("id IN (" . implode(',', array_unique($product_ids)) . ")")->fetchAll('id');
             foreach ($list as &$v) {
                 if (isset($products[$v['product_id']])) {
                     $v['product_name'] = $products[$v['product_id']]['name'];
                 }
             }
             unset($v);
         }
     }
 }