$params['warehouse_id'], 'order_type' => $params['order_type']??0, 'oid' => 0, 'admin_id' => $params['admin_id'], 'batch' => 0, 'code' => $code, 'mark' => $params['remark'] ?? '', 'total_price' => 0 ]; $res = PurchaseOrder::create($arr); $product_arr=[]; foreach ($params['product_arr'] as $k => $v) { if($v['product_id']<=0){ continue ; } $data['admin_id'] = $params['admin_id']; $data['order_type'] = $params['order_type']; $data['store_id'] = 0; $data['oid'] = $res['id']; $data['supplier_id'] = 0; $data['warehouse_id'] = $params['warehouse_id']; $data['code'] = $code; $data['product_id'] = $v['product_id']; $data['need_nums'] = $v['nums']; $data['nums'] = $v['nums']; $data['price'] = $v['price']; $data['total_price'] = $v['total_price']; $product_arr[]=$data; } (new PurchaseProduct())->saveAll($product_arr); $total_price=PurchaseProduct::where('oid', $res['id'])->sum('total_price'); PurchaseOrder::where('id', $res['id'])->update(['total_price' => $total_price]); Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new Exception($e->getMessage()); } } /** * @notes 编辑仓储商品单 * @param array $params * @return bool * @author admin * @date 2024/08/20 10:50 */ public static function edit(array $params): bool { $find = PurchaseOrder::where('id', $params['id'])->find(); if (!$find) { throw new Exception('订单不存在'); } Db::startTrans(); try { foreach ($params['product_arr'] as $k => $v) { $data['order_type'] = $find['order_type']; $data['supplier_id'] = $v['supplier_id']??0; $data['pay_type'] = $v['pay_type']??0; $data['admin_id'] = $params['admin_id']; $data['store_id'] = $find['store_id']; $data['oid'] = $find['id']; $data['warehouse_id'] = $find['warehouse_id']; $data['code'] = $find['code']; $data['product_id'] = $v['id']; $data['nums'] = $v['nums']; $data['price'] = $v['price']; $data['total_price'] = $v['total_price']; $data['financial_pm'] = $find['financial_pm']; if (!empty($v['manufacture'])) { $data['manufacture'] = $v['manufacture']; } if (!empty($v['expiration_date'])) { $data['expiration_date'] = $v['expiration_date']; } PurchaseProductLogic::add($data,1,$params['admin_id']); } $find = PurchaseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); if ($find) { PurchaseOrder::where('id', $params['id'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @notes 删除仓储商品单 * @param array $params * @return bool * @author admin * @date 2024/08/20 10:50 */ public static function delete(array $params): bool { $count = WarehouseProduct::where('oid', $params['id'])->count(); if ($count >= 1) { throw new BusinessException('该订单下还有商品没有删除,请先删除商品'); } WarehouseOrder::destroy($params['id']); $find = WarehouseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); if ($find) { WarehouseOrder::where('id', $params['id'])->update([ 'nums' => $find['nums'], 'total_price' => $find['total_price'] ]); } return true; } /** * @notes 编辑仓储商品单 * @param $data * @return bool * @author admin * @date 2024/08/20 10:50 */ public static function update_edit($data){ $res=WarehouseOrder::update($data); if($res){ return true; }else{ return false; } } /** * @notes 获取仓储商品单详情 * @param $params * @return array * @author admin * @date 2024/08/20 10:50 */ public static function detail($params): array { return WarehouseOrder::findOrEmpty($params['id'])->toArray(); } }