Example #1
0
 /**
  *
  * @param  array $params Required field warehouse_id, product_id, qty
  * Optional field app, reff_id, uom_id, item_value
  * @return boolean
  * @throws UserException
  */
 public function updateStock($params)
 {
     $stock = MProductStock::findOne(['warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id']]);
     if (isset($params['uom_id'])) {
         $qty_per_uom = ProductUom::find()->select('isi')->where(['product_id' => $params['product_id'], 'uom_id' => $params['uom_id']])->scalar();
         if ($qty_per_uom === false) {
             throw new NotFoundException("Uom '{$params['uom_id']}' not found for product '{$params['product_id']}'");
         }
     } else {
         $qty_per_uom = 1;
     }
     if (!$stock) {
         $stock = new MProductStock(['warehouse_id' => $params['warehouse_id'], 'product_id' => $params['product_id'], 'qty' => 0]);
     }
     // update cogs
     if (isset($params['price']) && $params['price'] !== '') {
         $params['qty_per_uom'] = $qty_per_uom;
         $this->updateCogs($params);
     }
     $stock->qty = $stock->qty + $params['qty'] * $qty_per_uom;
     if ($stock->canSetProperty('logParams')) {
         $logParams = ['mv_qty' => $params['qty'] * $qty_per_uom];
         foreach (['app', 'reff_id'] as $key) {
             if (isset($params[$key]) || array_key_exists($key, $params)) {
                 $logParams[$key] = $params[$key];
             }
         }
         $stock->logParams = $logParams;
     }
     if (!$stock->save()) {
         throw new UserException(implode(",\n", $stock->firstErrors));
     }
     return true;
 }
Example #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ProductUomModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['product_id' => $this->product_id, 'uom_id' => $this->uom_id, 'isi' => $this->isi, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     return $dataProvider;
 }
Example #3
0
 public function getMasters()
 {
     $result = [];
     // master product
     $query_product = Product::find()->select(['id', 'code', 'name'])->andWhere(['status' => Product::STATUS_ACTIVE])->indexBy('id')->asArray();
     $result['products'] = $query_product->all();
     // uoms
     $_uoms = Uom::find()->indexBy('id')->asArray()->all();
     $uoms = [];
     foreach (ProductUom::find()->asArray()->all() as $row) {
         $uoms[$row['product_id']][] = ['id' => $row['uom_id'], 'name' => $_uoms[$row['uom_id']]['name'], 'isi' => $row['isi']];
     }
     $result['product_uoms'] = $uoms;
     // barcodes
     $barcodes = [];
     $query_barcode = ProductChild::find()->select(['barcode', 'id' => 'product_id'])->union(Product::find()->select(['code', 'id']))->asArray();
     foreach ($query_barcode->all() as $row) {
         $barcodes[strtoupper($row['barcode'])] = $row['id'];
     }
     $result['barcodes'] = $barcodes;
     // price_category
     $price_category = [];
     $query_price_category = PriceCategory::find()->asArray();
     foreach ($query_price_category->all() as $row) {
         $price_category[$row['id']] = $row['name'];
     }
     $result['price_category'] = $price_category;
     // prices
     $prices = [];
     $query_prices = Price::find()->asArray();
     foreach ($query_prices->all() as $row) {
         $prices[$row['product_id']][$row['price_category_id']] = $row['price'];
     }
     $result['prices'] = $prices;
     // customer
     $result['customers'] = Customer::find()->select(['id', 'name'])->asArray()->all();
     // supplier
     $result['suppliers'] = Supplier::find()->select(['id', 'name'])->asArray()->all();
     // product_supplier
     $prod_supp = [];
     $query_prod_supp = ProductSupplier::find()->select(['supplier_id', 'product_id'])->asArray();
     foreach ($query_prod_supp->all() as $row) {
         $prod_supp[$row['supplier_id']][] = $row['product_id'];
     }
     $result['product_supplier'] = $prod_supp;
     // product_stock
     $prod_stock = [];
     $query_prod_stock = ProductStock::find()->select(['warehouse_id', 'product_id', 'qty'])->asArray();
     foreach ($query_prod_stock->all() as $row) {
         $prod_stock[$row['warehouse_id']][$row['product_id']] = $row['qty'];
     }
     $result['product_stock'] = $prod_stock;
     // branch
     $result['branchs'] = Branch::find()->asArray()->all();
     // warehouse
     $warehouses = Warehouse::find()->asArray()->all();
     $result['warehouses'] = [];
     foreach ($warehouses as $warehouse) {
         $result['warehouses'][$warehouse['branch_id']][] = $warehouse;
     }
     //apicode
     $result['mvconfig'] = $this->module->mvConfig;
     // coa
     $result['coa'] = Coa::find()->select(['id', 'cd' => 'code', 'text' => 'name', 'label' => 'name'])->asArray()->all();
     return $result;
 }
Example #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductUoms()
 {
     return $this->hasMany(ProductUom::className(), ['uom_id' => 'id']);
 }