|
@@ -5,10 +5,14 @@ namespace App\Service\Box;
|
|
|
|
|
|
use App\Model\Box;
|
|
|
use App\Model\BoxDetail;
|
|
|
+use App\Model\DispatchSub;
|
|
|
use App\Model\Header_ext;
|
|
|
use App\Model\OrdersProduct;
|
|
|
+use App\Model\OrdersProductBom;
|
|
|
+use App\Model\OrdersProductProcess;
|
|
|
use App\Model\SaleOrdersProduct;
|
|
|
use App\Model\Team;
|
|
|
+use App\Service\DispatchService;
|
|
|
use App\Service\FinishedOrderService;
|
|
|
use App\Service\FyySqlServerService;
|
|
|
use App\Service\Service;
|
|
@@ -25,15 +29,13 @@ class BoxService extends Service
|
|
|
protected static $box_header;
|
|
|
protected static $box_detail_header;
|
|
|
protected static $box_hook;
|
|
|
- public $lock_key = 'box';
|
|
|
+ public $lock_key = 'hc_box_add';
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
-
|
|
|
self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
|
|
|
self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
|
|
|
self::$box_hook = BoxHookService::getInstance();
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -67,12 +69,15 @@ class BoxService extends Service
|
|
|
}
|
|
|
$insert = [];
|
|
|
|
|
|
+ //限制频率
|
|
|
+ $limit_key = $this->lock_key;
|
|
|
+ list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
|
|
|
+ if(! $status) return [false, '正常生成包装数据,以及用友数据,请稍后操作!'];
|
|
|
+
|
|
|
try{
|
|
|
DB::beginTransaction();
|
|
|
- $time = time();
|
|
|
- $key = $this->lock_key.'_'.$v['id'];
|
|
|
- $lock_status = $this->isLock($key);
|
|
|
- if(!$lock_status) return [false,'操作过于频繁'];
|
|
|
+
|
|
|
+ //数据获取
|
|
|
$product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
|
|
|
$sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();
|
|
|
|
|
@@ -83,8 +88,8 @@ class BoxService extends Service
|
|
|
$total = $num_list['total'];
|
|
|
$detail = $num_list['detail'];
|
|
|
$team_id = $num_list['team_id'];
|
|
|
- $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num'];//todo
|
|
|
- if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
|
|
|
+ $un_box_num = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
|
|
|
+ if ($total > $un_box_num) return [false, $v['product_title'] . ' ' . $v['technology_name'] . ' 可包装的数量不足'];
|
|
|
|
|
|
$ext_1 = $v['product_no'];//产品编号
|
|
|
$ext_2 = $v['technology_material']; //工艺材质 废弃
|
|
@@ -122,8 +127,8 @@ class BoxService extends Service
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- //销售订单包装----------------
|
|
|
- foreach ($sale_product_list as $v) {
|
|
|
+ //销售订单包装----------------todo 目前只能按找生产包所以continue
|
|
|
+ foreach ($sale_product_list as $v) {continue;
|
|
|
$box_type = 0;
|
|
|
if($v['id'] < 0) {
|
|
|
$v['id'] = -$v['id'];
|
|
@@ -134,7 +139,7 @@ class BoxService extends Service
|
|
|
$detail = $num_list['detail'];
|
|
|
$team_id = $num_list['team_id'];
|
|
|
$box_detail = new BoxDetail(['channel'=>$top_order_no]);
|
|
|
- $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num');//todo
|
|
|
+ $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num');
|
|
|
if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
|
|
|
|
|
|
$ext_1 = $v['product_no'];//产品编号
|
|
@@ -181,10 +186,18 @@ class BoxService extends Service
|
|
|
$insert['shipment_order_no'] = $transport_no;
|
|
|
list($status,$msg) = self::$box_hook->boxInsert($insert);
|
|
|
if(! $status) {
|
|
|
- $this->delLock($key);
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新完工数量
|
|
|
+ list($status, $msg) = $this->updateFinish($product_list,$key_list);
|
|
|
+ if(! $status) {
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
DB::rollBack();
|
|
|
return [false,$msg];
|
|
|
- }$this->delLock($key);
|
|
|
+ }
|
|
|
|
|
|
//用友 ------产成品入库
|
|
|
$package_data = $msg->toArray();
|
|
@@ -192,71 +205,167 @@ class BoxService extends Service
|
|
|
$service = new FinishedOrderService();
|
|
|
list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
|
|
|
if(! $status) {
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
DB::rollBack();
|
|
|
return [false, $msg];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
return [true, ['package_data' => $package_data]];
|
|
|
}catch (\Exception $e){
|
|
|
DB::rollBack();
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
return [false,$e->getLine().':'.$e->getMessage()];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function saveOutOrder($data,$user){
|
|
|
- list($status,$msg) = $this->saveOutOrderRule($data,$user);
|
|
|
- if(! $status) return [false, $msg];
|
|
|
-
|
|
|
- //组织数据
|
|
|
- $yongyou_data = $update = [];
|
|
|
- foreach ($data['out_data'] as $value){
|
|
|
- $pro_tmp = [];
|
|
|
- foreach ($value['product'] as $p){
|
|
|
- $t_k = $p['cinvcode'] . $p['cfree1'];
|
|
|
- $pro_tmp[$t_k] = [
|
|
|
- 'cinvcode' => $p['cinvcode'],
|
|
|
- 'cfree1' => $p['cfree1'],
|
|
|
- 'iquantity' => $p['iquantity'],
|
|
|
- ];
|
|
|
- }
|
|
|
+ public function updateFinish($production_list = [], $map){
|
|
|
+ if(empty($production_list)) return [true,''];
|
|
|
|
|
|
- if(isset($yongyou_data[$value['customer_code']])){
|
|
|
- $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
|
|
|
- foreach ($pro_tmp as $k => $v){
|
|
|
- if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
|
|
|
- $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
|
|
|
- }else{
|
|
|
- $yongyou_data[$value['customer_code']]['product'][$k] = $v;
|
|
|
- }
|
|
|
+ //组织派工数据
|
|
|
+ $dispatch = DispatchSub::where('del_time',0)
|
|
|
+ ->where('order_product_id', array_column($production_list,'id'))
|
|
|
+ ->get()->toArray();
|
|
|
+ if(empty($dispatch)) return [true,''];
|
|
|
+ $dispatch_map = [];
|
|
|
+ foreach ($dispatch as $value){
|
|
|
+ $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
|
|
|
+ $dispatch_map[$key][] = $value;
|
|
|
+ }
|
|
|
+ $result = [];
|
|
|
+ foreach ($dispatch_map as $value){
|
|
|
+ $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
|
|
|
+ $finished_num = array_sum(array_column($value, 'finished_num'));
|
|
|
+ if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
|
|
|
+ foreach ($value as $val){
|
|
|
+ if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
|
|
|
+ //包装数量
|
|
|
+ $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
|
|
|
+ if($tmp_num < 0) continue;
|
|
|
+ $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
|
|
|
+ if($tmp_num2 >= $tmp_num){
|
|
|
+ $val['quantity'] = $tmp_num;
|
|
|
+ }else{
|
|
|
+ $val['quantity'] = $tmp_num2;
|
|
|
}
|
|
|
- }else{
|
|
|
- $yongyou_data[$value['customer_code']] = [
|
|
|
- 'customer_code' => $value['customer_code'],
|
|
|
- 'cdlcode_string' => $value['cdlcode'],
|
|
|
- 'product' => $pro_tmp
|
|
|
- ];
|
|
|
+ $result[] = $val;
|
|
|
}
|
|
|
+ }
|
|
|
+ if(empty($result)) return [true,''];
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ //根据派工数据回写
|
|
|
+ foreach ($result as $value){
|
|
|
+ $finished_num = $value['quantity'] + $value['finished_num'];
|
|
|
|
|
|
- foreach ($value['box_no'] as $box_no){
|
|
|
- $update[$box_no] = $value['cdlcode'];
|
|
|
+ DispatchSub::where('id',$value['id'])->update([
|
|
|
+ 'finished_num' => $finished_num
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $quantity = $value['quantity'] * 1000;
|
|
|
+ //工序表
|
|
|
+ $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
|
|
|
+ $process_model->where('order_product_id',$value['order_product_id'])
|
|
|
+ ->where('process_id',$value['process_id'])
|
|
|
+ ->where('dispatch_no',$value['dispatch_no'])
|
|
|
+ ->where('status', 1)
|
|
|
+ ->take($quantity)
|
|
|
+ ->update([
|
|
|
+ 'finished_time' => $time,
|
|
|
+ 'status' => 2,
|
|
|
+ 'finished_id' => 1,
|
|
|
+ 'team_id' => 72,
|
|
|
+ 'equipment_id' => 13,
|
|
|
+ ]);
|
|
|
}
|
|
|
+
|
|
|
+ $service = new FinishedOrderService();
|
|
|
+ //反写数量
|
|
|
+ //生产订单
|
|
|
+ $id = array_column($result,'order_product_id');
|
|
|
+ $service->writeFinishedQuantityByOrdersProductId($id);
|
|
|
+ //销售订单
|
|
|
+ $service->writeFinishedQuantity($id);
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
|
|
|
}
|
|
|
- if(empty($yongyou_data)) return [false, '暂无写入用友发货出库数据!'];
|
|
|
|
|
|
- //用友 ------发货出库
|
|
|
- $service = new FinishedOrderService();
|
|
|
- list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function saveOutOrder($data,$user){
|
|
|
+ list($status,$msg) = $this->saveOutOrderRule($data,$user);
|
|
|
if(! $status) return [false, $msg];
|
|
|
|
|
|
- //本地数据更新
|
|
|
- if(! empty($update)){
|
|
|
- foreach ($update as $key => $value){
|
|
|
- Box::where('order_no', $key)->update([
|
|
|
- 'shipment_order_no' => $value
|
|
|
- ]);
|
|
|
+ $limit_key = "saveOutOrder" . $user['id'];
|
|
|
+ list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
|
|
|
+ if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ //组织数据
|
|
|
+ $yongyou_data = $update = [];
|
|
|
+ foreach ($data['out_data'] as $value){
|
|
|
+ $pro_tmp = [];
|
|
|
+ foreach ($value['product'] as $p){
|
|
|
+ $t_k = $p['cinvcode'] . $p['cfree1'];
|
|
|
+ $pro_tmp[$t_k] = [
|
|
|
+ 'cinvcode' => $p['cinvcode'],
|
|
|
+ 'cfree1' => $p['cfree1'],
|
|
|
+ 'iquantity' => $p['iquantity'],
|
|
|
+ 'line' => $p['line'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($yongyou_data[$value['customer_code']])){
|
|
|
+ $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
|
|
|
+ foreach ($pro_tmp as $k => $v){
|
|
|
+ if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
|
|
|
+ $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
|
|
|
+ }else{
|
|
|
+ $yongyou_data[$value['customer_code']]['product'][$k] = $v;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $yongyou_data[$value['customer_code']] = [
|
|
|
+ 'customer_code' => $value['customer_code'],
|
|
|
+ 'cdlcode_string' => $value['cdlcode'],
|
|
|
+ 'product' => $pro_tmp
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($value['box_no'] as $box_no){
|
|
|
+ $update[$box_no] = $value['cdlcode'];
|
|
|
+ }
|
|
|
}
|
|
|
+ if(empty($yongyou_data)) return [false, '暂无写入用友发货出库数据!'];
|
|
|
+
|
|
|
+ //用友 ------发货出库
|
|
|
+ $service = new FinishedOrderService();
|
|
|
+ list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ //本地数据更新
|
|
|
+ if(! empty($update)){
|
|
|
+ foreach ($update as $key => $value){
|
|
|
+ Box::where('order_no', $key)->update([
|
|
|
+ 'shipment_order_no' => $value
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ $this->dellimitingSendRequestBackgNeed($limit_key);
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
|
|
|
}
|
|
|
|
|
|
return [true, ''];
|
|
@@ -307,6 +416,7 @@ class BoxService extends Service
|
|
|
}
|
|
|
if(empty($value['product'])) return [false, '发货产品不能为空!'];
|
|
|
foreach ($value['product'] as $p){
|
|
|
+ if(empty($p['line'])) return [false, '存货行号不能为空!'];
|
|
|
if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
|
|
|
if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
|
|
|
$p_k = $p['cinvcode'] . $p['cfree1'];
|
|
@@ -469,38 +579,45 @@ class BoxService extends Service
|
|
|
->select('id','out_order_no','order_no','customer_no','customer_name','product_no','product_title','product_size','crt_time as production_time','id as sale_orders_product_id','finished_num as dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','order_quantity','technology_material','technology_name','wood_name','process_mark')
|
|
|
->orderBy('id','desc')
|
|
|
->get()->toArray();
|
|
|
+ //原先按照完工数量 未包装 = 完工数量 - 已包数量
|
|
|
$product_list = ordersProduct::where('del_time',0)
|
|
|
- ->whereIn('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','box_num','technology_name','wood_name','crt_time',DB::raw('(production_quantity - scrapp_num) as production_quantity'),DB::raw('finished_num as dispatch_complete_quantity'),'technology_material','technology_name','wood_name','process_mark',"production_no")->get()->toArray();
|
|
|
+ ->whereIn('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','box_num','technology_name','wood_name','crt_time','production_quantity','dispatch_complete_quantity','technology_material','technology_name','wood_name','process_mark',"production_no")
|
|
|
+ ->get()->toArray();
|
|
|
$model_key_list = [];
|
|
|
foreach ($model as $v){
|
|
|
$model_key_list[$v['id']] = $v;
|
|
|
}
|
|
|
+ $model = [];
|
|
|
|
|
|
//存在生产订单,组织数据
|
|
|
if(! empty($product_list)){
|
|
|
$product_key_num_list = [];
|
|
|
foreach ($product_list as $v){
|
|
|
- if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
|
|
|
- 'product_num' => 0 ,
|
|
|
- 'box_num' => 0 ,
|
|
|
- ];
|
|
|
+ if($v['production_quantity'] == $v['box_num']) continue;
|
|
|
+ if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
|
|
|
+ $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
|
|
|
+ 'product_num' => 0 ,
|
|
|
+ 'box_num' => 0 ,
|
|
|
+ ];
|
|
|
+ }
|
|
|
$product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
|
|
|
$product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
|
|
|
$detail = $model_key_list[$v['sale_orders_product_id']];
|
|
|
$detail['box_type'] = 1;
|
|
|
$detail['id'] = $v['id'];
|
|
|
$detail['box_num'] = $v['box_num'];
|
|
|
- $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num'];
|
|
|
+ $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
|
|
|
+ $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
|
|
|
$detail['type'] = 1;
|
|
|
$detail['production_no'] = $v['production_no'];
|
|
|
+ $detail['production_quantity'] = $v['production_quantity'];
|
|
|
$model[] = $detail;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$return = [];
|
|
|
- $product_key_list = [];
|
|
|
foreach ($model as $v){
|
|
|
- if(!isset($v['box_type'])) {
|
|
|
+ if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
|
|
|
$product_num = 0;
|
|
|
$box_num = 0;
|
|
|
if(isset($product_key_num_list[$v['id']])){
|
|
@@ -550,14 +667,12 @@ class BoxService extends Service
|
|
|
'is_box_num' => $v['box_num'],
|
|
|
'un_box_num' => $v['un_box_num'],
|
|
|
'sale_num' => $v['order_quantity'],
|
|
|
+ 'production_num' => $v['production_quantity'],
|
|
|
+ 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- foreach ($product_key_list as $v){
|
|
|
- $return[] = $v;
|
|
|
- }
|
|
|
-
|
|
|
return [true,$return];
|
|
|
}
|
|
|
|
|
@@ -935,25 +1050,22 @@ class BoxService extends Service
|
|
|
->get()->toArray();
|
|
|
$detail_list = [];
|
|
|
foreach ($list as $v){
|
|
|
- if(empty($v['transport_no'])) return [false, "包装单:" . $v['transport_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
|
|
|
+ if(! empty($v['transport_no'])) return [false, "包装单:" . $v['transport_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
|
|
|
$model = new BoxDetail(['channel'=>$v['top_order_no']]);
|
|
|
$detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
|
|
|
}
|
|
|
|
|
|
$return = [];
|
|
|
foreach ($detail_list as $value){
|
|
|
- //销售订单号 包装单号 产品编码 颜色
|
|
|
- $key = $value['ext_1'] . $value['ext_3'];
|
|
|
- if(isset($return[$key])){
|
|
|
- $return[$key]['num'] += $value['num'];
|
|
|
- }else{
|
|
|
- $return[$key] = [
|
|
|
- 'ext_1' => $value['ext_1'],
|
|
|
- 'ext_3' => $value['ext_3'],
|
|
|
- 'ext_8' => $value['ext_8'],
|
|
|
- 'num' => $value['num'],
|
|
|
- ];
|
|
|
- }
|
|
|
+ //销售订单号 产品编码 颜色
|
|
|
+ $return[] = [
|
|
|
+ 'ext_1' => $value['ext_1'],
|
|
|
+ 'ext_3' => $value['ext_3'],
|
|
|
+ 'ext_8' => $value['ext_8'],
|
|
|
+ 'num' => $value['num'],
|
|
|
+ 'out_order_no' => $value['out_order_no'],
|
|
|
+ 'box_no' => $value['order_no']
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
return [true, array_values($return)];
|