refactor(warehouse): 重构仓库出库逻辑

- 移除了单店和多店的区分逻辑
- 新增了订单类型的判断逻辑
- 优化了订单创建和商品添加的流程
- 删除了一些冗余代码
- 调整了部分变量命名
This commit is contained in:
mkm 2024-12-19 10:55:14 +08:00
parent bdc290c6b1
commit 535bd7e47e
2 changed files with 79 additions and 86 deletions

View File

@ -71,27 +71,22 @@ class WarehouseOrderController extends BaseAdminController
public function outbound() public function outbound()
{ {
$product_arr = $this->request->post('product_arr'); $product_arr = $this->request->post('product_arr');
$store_arr = $this->request->post('store_arr');
$warehouse_id = $this->request->post('warehouse_id'); $warehouse_id = $this->request->post('warehouse_id');
$delivery_time = $this->request->post('delivery_time'); $delivery_time = $this->request->post('delivery_time');
$mark = $this->request->post('mark'); $mark = $this->request->post('mark');
$count = count($store_arr); $order_type = $this->request->post('order_type', 1);
// foreach ($product_arr as $key => $arr) {
// $stock = bcmul($arr['stock'], $count);
// $nums = WarehouseProductStorege::where('warehouse_id', $warehouse_id)->where('product_id', $arr['id'])->value('nums');
// if ($nums < $stock) {
// return $this->fail('商品库存不足');
// }
// }
Db::startTrans(); Db::startTrans();
try { try {
if ($count == 1) { if($order_type==1){
$store_id = $store_arr[0]; $code=getNewOrderId('TGY');
}else{
$code=getNewOrderId('BS');
}
$arr = [ $arr = [
'warehouse_id' => $warehouse_id, 'warehouse_id' => $warehouse_id,
'store_id' => $store_id, 'store_id' => 0,
'supplier_id' => 0, 'supplier_id' => 0,
'code' => getNewOrderId('PS'), 'code' => $code,
'admin_id' => $this->adminId, 'admin_id' => $this->adminId,
'financial_pm' => 0, 'financial_pm' => 0,
'batch' => 0, 'batch' => 0,
@ -103,33 +98,30 @@ class WarehouseOrderController extends BaseAdminController
$data = [ $data = [
'warehouse_id' => $warehouse_id, 'warehouse_id' => $warehouse_id,
'product_id' => $arr['id'], 'product_id' => $arr['id'],
'store_id' => $store_id, 'store_id' => 0,
'financial_pm' => 0, 'financial_pm' => 0,
'batch' => 1, 'batch' => 1,
'nums' => $arr['stock'], 'nums' => $arr['stock'],
'status' => 1, 'status' => 1,
'admin_id' => $this->adminId, 'admin_id' => $this->adminId,
'order_type' => $order_type,
]; ];
$storeProduct = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray(); $storeProduct = StoreProduct::where('id', $arr['id'])->find();
if ($arr['stock'] == 0) {
StoreProductLogic::ordinary($arr, $store_id, $this->adminId, $storeProduct);
} else {
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2); $data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
$data['purchase'] = $storeProduct['purchase']; $data['purchase'] = $storeProduct['purchase'];
$data['oid'] = $res['id']; $data['oid'] = $res['id'];
$data['financial_pm'] = 0; $data['financial_pm'] = 0;
$data['price'] = $storeProduct['price'];
WarehouseProductLogic::add($data); WarehouseProductLogic::add($data);
$finds = WarehouseProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); $finds = WarehouseProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
WarehouseOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]); WarehouseOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]);
} }
}
}
Db::commit(); Db::commit();
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());
} }
return $this->success('已导入后台队列,请在门店入库记录中查看', [], 1, 1); return $this->success('出库成功', [], 1, 1);
} }
@ -142,7 +134,7 @@ class WarehouseOrderController extends BaseAdminController
public function edit() public function edit()
{ {
$params = $this->request->post(); $params = $this->request->post();
$params['admin_id']=$this->adminId; $params['admin_id'] = $this->adminId;
$result = WarehouseOrderLogic::edit($params); $result = WarehouseOrderLogic::edit($params);
if (true === $result) { if (true === $result) {
return $this->success('编辑成功', [], 1, 1); return $this->success('编辑成功', [], 1, 1);
@ -155,7 +147,8 @@ class WarehouseOrderController extends BaseAdminController
* @author admin * @author admin
* @date 2024/08/20 10:50 * @date 2024/08/20 10:50
*/ */
public function update_edit(){ public function update_edit()
{
$params = $this->request->post(); $params = $this->request->post();
$result = WarehouseOrderLogic::update_edit($params); $result = WarehouseOrderLogic::update_edit($params);
if (true === $result) { if (true === $result) {
@ -212,19 +205,19 @@ class WarehouseOrderController extends BaseAdminController
$value->unit_name = ''; $value->unit_name = '';
} }
$order['total_num'] += $value->nums; $order['total_num'] += $value->nums;
$value->supplier_name= Supplier::where('id', $value->supplier_id)->value('mer_name'); $value->supplier_name = Supplier::where('id', $value->supplier_id)->value('mer_name');
if($value->pay_type==1){ if ($value->pay_type == 1) {
$credit_pay += $value->total_price; $credit_pay += $value->total_price;
$value->pay_type_name='赊账'; $value->pay_type_name = '赊账';
}elseif($value->pay_type==2){ } elseif ($value->pay_type == 2) {
$cash_pay += $value->total_price; $cash_pay += $value->total_price;
$value->pay_type_name='现款'; $value->pay_type_name = '现款';
}else{ } else {
$value->pay_type_name='未设置'; $value->pay_type_name = '未设置';
} }
} }
$order['credit_pay']=$credit_pay; $order['credit_pay'] = $credit_pay;
$order['cash_pay']=$cash_pay; $order['cash_pay'] = $cash_pay;
$file_path = $xlsx->export($data, $order); $file_path = $xlsx->export($data, $order);
return $this->success('导出成功', ['url' => $file_path]); return $this->success('导出成功', ['url' => $file_path]);
@ -237,7 +230,7 @@ class WarehouseOrderController extends BaseAdminController
{ {
$id = $this->request->post('id'); $id = $this->request->post('id');
$type = $this->request->post('type'); $type = $this->request->post('type');
if(in_array($type, [2,3])){ if (in_array($type, [2, 3])) {
return $this->fail('暂不支持此操作'); return $this->fail('暂不支持此操作');
} }
$xlsx = new OrderDetail(); $xlsx = new OrderDetail();
@ -245,11 +238,11 @@ class WarehouseOrderController extends BaseAdminController
$system_store = SystemStore::where('id', $order['store_id'])->value('name'); $system_store = SystemStore::where('id', $order['store_id'])->value('name');
$data = WarehouseProduct::where('oid', $id)->select(); $data = WarehouseProduct::where('oid', $id)->select();
$order['total_num'] = 0; $order['total_num'] = 0;
$total_price=0; $total_price = 0;
foreach ($data as $key => &$value) { foreach ($data as $key => &$value) {
if(in_array($order['store_id'],[17,18])){ if (in_array($order['store_id'], [17, 18])) {
$find = StoreBranchProduct::where('product_id', $value->product_id)->where('store_id',$order['store_id'])->withTrashed()->find(); $find = StoreBranchProduct::where('product_id', $value->product_id)->where('store_id', $order['store_id'])->withTrashed()->find();
}else{ } else {
$find = StoreProduct::where('id', $value->product_id)->withTrashed()->find(); $find = StoreProduct::where('id', $value->product_id)->withTrashed()->find();
} }
$value->store_name = $find['store_name'] ?? ''; $value->store_name = $find['store_name'] ?? '';
@ -267,10 +260,10 @@ class WarehouseOrderController extends BaseAdminController
// $value->total_price=bcmul($find['price'],$value['nums'],2); // $value->total_price=bcmul($find['price'],$value['nums'],2);
// $total_price+=$value->total_price; // $total_price+=$value->total_price;
// }else{ // }else{
if($type==1){ if ($type == 1) {
$value->price = $value['purchase']; $value->price = $value['purchase'];
$value->total_price=bcmul($value['purchase'],$value['nums'],2); $value->total_price = bcmul($value['purchase'], $value['nums'], 2);
$total_price+=$value->total_price; $total_price += $value->total_price;
} }
@ -283,18 +276,18 @@ class WarehouseOrderController extends BaseAdminController
} }
$order['total_num'] += $value->nums; $order['total_num'] += $value->nums;
} }
if($type==2){ if ($type == 2) {
$order['total_price']=$total_price; $order['total_price'] = $total_price;
} }
$order['delivery_time']=date('Y-m-d H:i:s',$order['delivery_time']); $order['delivery_time'] = date('Y-m-d H:i:s', $order['delivery_time']);
$order['pay_time']=$order['create_time']; $order['pay_time'] = $order['create_time'];
$order['order_id']=$order['code']; $order['order_id'] = $order['code'];
if($order['oid']>0){ if ($order['oid'] > 0) {
$orders=StoreOrder::where('id',$order['oid'])->findOrEmpty(); $orders = StoreOrder::where('id', $order['oid'])->findOrEmpty();
$order['real_name']=$orders['real_name']; $order['real_name'] = $orders['real_name'];
$order['user_phone']=$orders['user_phone']; $order['user_phone'] = $orders['user_phone'];
$order['user_address']=$orders['user_address']; $order['user_address'] = $orders['user_address'];
} }
$file_path = $xlsx->export($data, $system_store, $order); $file_path = $xlsx->export($data, $system_store, $order);
@ -309,26 +302,26 @@ class WarehouseOrderController extends BaseAdminController
$id = $this->request->post('id'); $id = $this->request->post('id');
$warehouseOrder = WarehouseOrder::where('id', $id)->field('oid,store_id,delivery_time')->find(); $warehouseOrder = WarehouseOrder::where('id', $id)->field('oid,store_id,delivery_time')->find();
$system_store = SystemStore::where('id', $warehouseOrder['store_id'])->value('introduction'); $system_store = SystemStore::where('id', $warehouseOrder['store_id'])->value('introduction');
$data = WarehouseProduct::where('oid', $id)->where('financial_pm',0)->field('oid,product_id,nums')->select() $data = WarehouseProduct::where('oid', $id)->where('financial_pm', 0)->field('oid,product_id,nums')->select()
->each(function ($item) use ($system_store,$warehouseOrder) { ->each(function ($item) use ($system_store, $warehouseOrder) {
$find = StoreProduct::where('id', $item['product_id'])->field('store_name,unit')->withTrashed()->find(); $find = StoreProduct::where('id', $item['product_id'])->field('store_name,unit')->withTrashed()->find();
$unit_name = StoreProductUnit::where('id', $find['unit'])->value('name'); $unit_name = StoreProductUnit::where('id', $find['unit'])->value('name');
$item['system_store'] = $system_store; $item['system_store'] = $system_store;
$item['subtitle'] = $item['oid'].' '.convertStringToNumber($item['nums']).'/'.$unit_name; $item['subtitle'] = $item['oid'] . ' ' . convertStringToNumber($item['nums']) . '/' . $unit_name;
$item['store_name'] = $find['store_name']; $item['store_name'] = $find['store_name'];
if($warehouseOrder['oid']){ if ($warehouseOrder['oid']) {
$find=StoreOrder::where('id',$warehouseOrder['oid'])->field('real_name,user_address')->find(); $find = StoreOrder::where('id', $warehouseOrder['oid'])->field('real_name,user_address')->find();
if($find){ if ($find) {
$item['address'] = $find['real_name'].' '.$find['user_address']; $item['address'] = $find['real_name'] . ' ' . $find['user_address'];
}else{ } else {
$item['address'] = '无地址'; $item['address'] = '无地址';
} }
}else{ } else {
$item['address'] = '无地址'; $item['address'] = '无地址';
} }
}) })
->toArray(); ->toArray();
$file_path=(new Beforehand())->export($data, $system_store); $file_path = (new Beforehand())->export($data, $system_store);
return $this->success('导出成功', ['url' => $file_path]); return $this->success('导出成功', ['url' => $file_path]);
} }
} }

View File

@ -91,7 +91,7 @@ class WarehouseProductLogic extends BaseLogic
'nums' => $params['nums'], 'nums' => $params['nums'],
'before_nums' => $before_nums, 'before_nums' => $before_nums,
'after_nums' => $after_nums, 'after_nums' => $after_nums,
// 'price' => $params['price'] ?? '', 'price' => $params['price'] ?? '',
'purchase' => $params['purchase'] ?? '', 'purchase' => $params['purchase'] ?? '',
// 'cost' => $params['cost'] ?? '', // 'cost' => $params['cost'] ?? '',
'total_price' => $params['total_price'] ?? '', 'total_price' => $params['total_price'] ?? '',