public function execute()
 {
     $model = new shopStockModel();
     foreach ($this->getEditData() as $id => $item) {
         $model->updateById($id, $item);
     }
     $inventory_stock_id = null;
     foreach ($this->getAddData() as $before_id => $data) {
         foreach ($data as $item) {
             $id = $model->add($item, $before_id);
             if (!empty($item['inventory'])) {
                 $inventory_stock_id = $id;
             }
         }
     }
     if ($inventory_stock_id) {
         // Assign all inventory to this stock
         $product_stocks_model = new shopProductStocksModel();
         $product_stocks_model->insertFromSkus($inventory_stock_id);
     }
     $app_id = $this->getAppId();
     $app_settings_model = new waAppSettingsModel();
     if (waRequest::post('ignore_stock_count')) {
         $app_settings_model->set($app_id, 'ignore_stock_count', 1);
     } else {
         $app_settings_model->set($app_id, 'ignore_stock_count', 0);
     }
     if (waRequest::post('update_stock_count_on_create_order')) {
         $app_settings_model->set($app_id, 'update_stock_count_on_create_order', 1);
     } else {
         $app_settings_model->set($app_id, 'update_stock_count_on_create_order', 0);
     }
 }
 private function getSkuStocks($sku_ids)
 {
     if (!$sku_ids) {
         return array();
     }
     $product_stocks_model = new shopProductStocksModel();
     return $product_stocks_model->getBySkuId($sku_ids);
 }
Example #3
0
 /**
  * Delete stock with or not writing-off products
  *
  * @param int $stock_id
  * @param int|null $dst_stock If null than writing-off products else move products to $dst_stock
  * @return bool
  */
 public function delete($stock_id, $dst_stock = null)
 {
     $stock = $this->getById($stock_id);
     if (!$stock) {
         return false;
     }
     $stock_counts = $this->countAll();
     $product_stocks_model = new shopProductStocksModel();
     if (!$dst_stock) {
         if (!$product_stocks_model->deleteStock($stock_id, $stock_counts > 1)) {
             return false;
         }
     } else {
         if ($stock_counts <= 1) {
             return false;
         }
         if (!$product_stocks_model->move($stock_id, $dst_stock)) {
             return false;
         }
     }
     return $this->deleteById($stock_id);
 }
 public function transfer($sku_id, $count, $src_stock, $dst_stock)
 {
     $src_stock = (int) $src_stock;
     $dst_stock = (int) $dst_stock;
     $sku_id = (int) $sku_id;
     $count = (int) $count;
     $sku = $this->getById($sku_id);
     if (empty($sku)) {
         return false;
     }
     $product_stocks_model = new shopProductStocksModel();
     return $product_stocks_model->transfer($src_stock, $dst_stock, $sku_id, $count);
 }
Example #5
0
 public function getSkuStocks($sku_ids)
 {
     if (!$sku_ids) {
         return array();
     }
     $model = new shopProductStocksModel();
     return $model->getBySkuId($sku_ids);
 }
 public function execute($data = null)
 {
     if (wa()->getEnv() == 'frontend') {
         // Now we are in frontend, so fill stock_id for items. Stock_id get from storefront-settings
         // But:
         //   - some skus may have not any stock
         //   - stock_id from storefront isn't setted (empty)
         $sku_ids = array();
         foreach ($data['items'] as $item) {
             if ($item['type'] == 'product') {
                 $sku_ids[] = (int) $item['sku_id'];
             }
         }
         $product_stocks_model = new shopProductStocksModel();
         $sku_ids = $product_stocks_model->filterSkusByNoStocks($sku_ids);
         $sku_ids_map = array_fill_keys($sku_ids, true);
         // storefront stock-id
         $stock_id = waRequest::param('stock_id');
         $stock_model = new shopStockModel();
         if (!$stock_id || !$stock_model->stockExists($stock_id)) {
             $stock_id = $stock_model->select('id')->order('sort')->limit(1)->fetchField();
         }
         foreach ($data['items'] as &$item) {
             if ($item['type'] == 'product') {
                 if (!isset($sku_ids_map[$item['sku_id']])) {
                     // have stocks
                     $item['stock_id'] = $stock_id;
                 }
             }
         }
     }
     $currency = wa()->getConfig()->getCurrency(false);
     $rate_model = new shopCurrencyModel();
     $row = $rate_model->getById($currency);
     $rate = $row['rate'];
     // Save contact
     if (isset($data['contact'])) {
         if (is_numeric($data['contact'])) {
             $contact = new waContact($data['contact']);
         } else {
             /**
              * @var waContact $contact
              */
             $contact = $data['contact'];
             if (!$contact->getId()) {
                 $contact->save();
                 // if user has been created
                 if ($contact['password']) {
                     $signup_action = new shopSignupAction();
                     $signup_action->send($contact);
                 }
             }
         }
     } else {
         $data['contact'] = $contact = wa()->getUser();
     }
     $subtotal = 0;
     foreach ($data['items'] as &$item) {
         if ($currency != $item['currency']) {
             $item['price'] = shop_currency($item['price'], $item['currency'], null, false);
             if (!empty($item['purchase_price'])) {
                 $item['purchase_price'] = shop_currency($item['purchase_price'], $item['currency'], null, false);
             }
             $item['currency'] = $currency;
         }
         $subtotal += $item['price'] * $item['quantity'];
     }
     unset($item);
     if ($data['discount'] === '') {
         $data['total'] = $subtotal;
         $data['discount'] = shopDiscounts::apply($data);
     }
     $shipping_address = $contact->getFirst('address.shipping');
     if (!$shipping_address) {
         $shipping_address = $contact->getFirst('address');
     }
     $billing_address = $contact->getFirst('address.billing');
     if (!$billing_address) {
         $billing_address = $contact->getFirst('address');
     }
     $discount_rate = $subtotal ? $data['discount'] / $subtotal : 0;
     $taxes = shopTaxes::apply($data['items'], array('shipping' => isset($shipping_address['data']) ? $shipping_address['data'] : array(), 'billing' => isset($billing_address['data']) ? $billing_address['data'] : array(), 'discount_rate' => $discount_rate));
     $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'];
         }
     }
     $order = array('state_id' => 'new', 'total' => $subtotal - $data['discount'] + $data['shipping'] + $tax, 'currency' => $currency, 'rate' => $rate, 'tax' => $tax_included + $tax, 'discount' => $data['discount'], 'shipping' => $data['shipping'], 'comment' => isset($data['comment']) ? $data['comment'] : '');
     $order['contact_id'] = $contact->getId();
     // Add contact to 'shop' category
     $contact->addToCategory('shop');
     // Save order
     $order_model = new shopOrderModel();
     $order_id = $order_model->insert($order);
     // Create record in shop_customer, or update existing record
     $scm = new shopCustomerModel();
     $scm->updateFromNewOrder($order['contact_id'], $order_id);
     // save items
     $items_model = new shopOrderItemsModel();
     $parent_id = null;
     foreach ($data['items'] as $item) {
         $item['order_id'] = $order_id;
         if ($item['type'] == 'product') {
             $parent_id = $items_model->insert($item);
         } elseif ($item['type'] == 'service') {
             $item['parent_id'] = $parent_id;
             $items_model->insert($item);
         }
     }
     // Order params
     if (empty($data['params'])) {
         $data['params'] = array();
     }
     $data['params']['auth_code'] = self::generateAuthCode($order_id);
     $data['params']['auth_pin'] = self::generateAuthPin();
     // Save params
     $params_model = new shopOrderParamsModel();
     $params_model->set($order_id, $data['params']);
     $log_model = new waLogModel();
     $log_model->add('order_create', $order_id, null, $order['contact_id']);
     return array('order_id' => $order_id, 'contact_id' => wa()->getEnv() == 'frontend' ? $contact->getId() : wa()->getUser()->getId());
 }
Example #7
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);
 }