Esempio n. 1
0
 public function get_quantity_by_product($row_data)
 {
     $quantity_balance = 0;
     if ($row_data['product_unique_id']) {
         $product_unique_id = $row_data['product_unique_id'];
     } else {
         if (AppService::is_app_active('productAttribute')) {
             $attribute_fields = D('ProductAttribute/ProductAttribute')->get_attribute_fields();
             $product_unique_id = generate_product_unique_id($row_data, $attribute_fields);
         } else {
             if ($row_data['product_id']) {
                 $product_unique_id = $row_data['product_id'];
             }
         }
     }
     if (!$product_unique_id) {
         return $quantity_balance;
     }
     $map = ['product_unique_id' => $product_unique_id];
     // 指定仓库
     if ($row_data['storage_id']) {
         $map['storage_id'] = $row_data['storage_id'];
     }
     $raw_data = $this->where($map)->select();
     foreach ($raw_data as $v) {
         $quantity_balance += $v['balance'];
     }
     return round((string) $quantity_balance, DBC('decimal_scale'));
 }
Esempio n. 2
0
 public function _EM_fetch_product_unit_price()
 {
     $response_data = ['source_price' => '', 'supply_price' => ''];
     // 获取原价
     $map = ['id' => I('get.product_id')];
     $response_data['source_price'] = D('Product/Product')->where($map)->getField('price');
     if (I('get.supplier_id')) {
         // 获取供应商供应价格
         $map = ['product_id' => I('get.product_id'), 'product_unique_id' => generate_product_unique_id(I('get.')), 'supplier_id' => D('Supplier/Supplier')->where(['contacts_company_id' => I('get.supplier_id')])->getField('id')];
         $response_data['supply_price'] = D('Supplier/SupplierSupplyProduct')->where($map)->getField('supply_price');
     }
     $response_data['source_price'] = (double) $response_data['source_price'];
     $response_data['supply_price'] = (double) $response_data['supply_price'];
     $this->response($response_data);
 }
Esempio n. 3
0
 public function edit_bill($id, $meta, $rows)
 {
     if (!$this->check_params()) {
         $this->error = __('common.System Error');
         return false;
     }
     $old_meta = $this->where(['id' => $id])->find();
     // 获得当前数据状态,判断是否可修改
     if (in_array($old_meta['status'], $this->LOCKED_STATUS)) {
         $this->error = __('workflow.Can not be edit this item because the data is in progress');
         return false;
     }
     /*
      * 产品属性
      * */
     if (AppService::is_app_active('productAttribute')) {
         $attribute_active = true;
         $attribute_service = D('ProductAttribute/ProductAttribute');
         // 所有的产品属性字段
         $attribute_fields = $attribute_service->get_attribute_fields();
     }
     // 更新meta信息
     // 统计
     if (!$meta['quantity']) {
         $quantity = 0;
         $row_quantity = get_array_by_field($rows, 'quantity');
         foreach ($row_quantity as $v) {
             $quantity += $v;
         }
         $meta['quantity'] = decimal_scale($quantity);
     }
     foreach ($meta as $k => $v) {
         if (is_array($v)) {
             unset($meta[$k]);
         }
     }
     $this->startTrans();
     if (false === $this->where(['id' => $id])->save($meta)) {
         $this->error = __('storage.Trigger when save meta data');
         $this->rollback();
         return false;
     }
     // 获取已存在数据
     $detail_service = D($this->detail_model);
     $exists_rows = $detail_service->where([$this->detail_main_foreign => $id])->select();
     $exists_rows = get_array_to_ka($exists_rows, 'product_unique_id');
     // 已存在ID数组
     $exists_ids = get_array_by_field($exists_rows, 'id');
     // 修改的ID数组
     $edited_ids = '';
     $rows = $this->merge_same_rows($rows, $attribute_fields);
     /*
      * 更新行信息
      * 1、 已存在行,尝试判断数量等信息
      * 2、 未存在行插入
      * 3、 删除行删除
      * */
     foreach ($rows as $row) {
         $row['product_unique_id'] = $product_unique_id = generate_product_unique_id($row, $attribute_fields);
         if (array_key_exists($product_unique_id, $exists_rows)) {
             // 存在行
             //                $row['id'] = $exists_rows[$product_unique_id]['id'];
             $detail_service->create($row);
             $detail_service->save();
             //                $detail_service->where([
             //                    'id' => $exists_rows[$product_unique_id]['id']
             //                ])->save($row);
             //
             //                echo $detail_service->getLastSql();exit;
             array_push($edited_ids, $exists_rows['id']);
             $row['id'] = $exists_rows[$product_unique_id]['id'];
         } else {
             // 不存在行 插入
             $row[$this->detail_main_foreign] = $id;
             $detail_service->create($row);
             $row['id'] = $row_id = $detail_service->add();
         }
         // 更新属性map
         if ($attribute_active) {
             $this->record_detail_attribute($id, $row, $attribute_fields);
         }
     }
     // 删除未使用的明细条目
     $need_delete = array_diff($exists_ids, $edited_ids);
     $detail_service->where(['id' => ['IN', $need_delete]])->delete();
     $this->commit();
 }