|
@@ -0,0 +1,424 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\Depart;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\OutBoundOrder;
|
|
|
+use App\Model\OutBoundOrderInfo;
|
|
|
+use App\Model\ReturnExchangeOrder;
|
|
|
+use App\Model\ReturnExchangeOrderProductInfo;
|
|
|
+use App\Model\SalesOrder;
|
|
|
+use App\Model\SalesOrderProductInfo;
|
|
|
+use App\Model\Storehouse;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class OutBoundOrderService extends Service
|
|
|
+{
|
|
|
+ public function edit($data,$user){
|
|
|
+ list($status,$msg) = $this->orderRule($data,$user,false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ $material_model = OutBoundOrder::where('order_number',$data['order_number'])->first();
|
|
|
+ $material_model->data_id = $data['data_id'];
|
|
|
+ $material_model->type = $data['type'];
|
|
|
+ $material_model->out_id = $data['out_id'];
|
|
|
+ $material_model->mark = $data['mark'] ?? "";
|
|
|
+ $material_model->storehouse_id = $data['storehouse_id'];
|
|
|
+ $material_model->save();
|
|
|
+
|
|
|
+ OutBoundOrderInfo::where('order_number',$data['order_number'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+ if(! empty($data['product'])){
|
|
|
+ $sub = [];
|
|
|
+ foreach ($data['product'] as $value){
|
|
|
+ $sub[] = [
|
|
|
+ 'invoice_id' => $material_model->id,
|
|
|
+ 'order_number' => $data['order_number'],
|
|
|
+ 'storehouse_id' => $data['storehouse_id'] ?? 0,
|
|
|
+ 'product_id' => $value['product_id'],
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'price' => $value['price'] ?? 0,
|
|
|
+ 'final_amount' => $value['final_amount'] ?? 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ OutBoundOrderInfo::insert($sub);
|
|
|
+
|
|
|
+ //锁定库存
|
|
|
+ ProductInventoryService::changeLockNumber($user, $msg[0],$msg[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $e){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$e->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function add($data,$user){
|
|
|
+ list($status,$msg) = $this->orderRule($data, $user);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ $material_model = new OutBoundOrder();
|
|
|
+ $material_model->order_number = $data['order_number'];
|
|
|
+ $material_model->data_id = $data['data_id'];
|
|
|
+ $material_model->type = $data['type'];
|
|
|
+ $material_model->out_id = $data['out_id'];
|
|
|
+ $material_model->depart_id = $data['depart_id'] ?? 0;
|
|
|
+ $material_model->top_depart_id = $data['top_depart_id'] ?? 0;
|
|
|
+ $material_model->mark = $data['mark'] ?? "";
|
|
|
+ $material_model->crt_id = $user['id'];
|
|
|
+ $material_model->storehouse_id = $data['storehouse_id'];
|
|
|
+ $material_model->save();
|
|
|
+
|
|
|
+ if(!empty($data['product'])){
|
|
|
+ $sub = [];
|
|
|
+ foreach ($data['product'] as $value){
|
|
|
+ $sub[] = [
|
|
|
+ 'out_bound_id' => $material_model->id,
|
|
|
+ 'order_number' => $data['order_number'],
|
|
|
+ 'storehouse_id' => $data['storehouse_id'] ?? 0,
|
|
|
+ 'product_id' => $value['product_id'],
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'price' => $value['price'] ?? 0,
|
|
|
+ 'final_amount' => $value['final_amount'] ?? 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ OutBoundOrderInfo::insert($sub);
|
|
|
+
|
|
|
+ //锁定库存
|
|
|
+ ProductInventoryService::changeLockNumber($user,$msg[0],[]);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
|
|
|
+ return [false, '网络波动,请重新操作!'];
|
|
|
+ }
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['check'])) {
|
|
|
+ list($status,$msg) = (new CheckService())->checkAll([
|
|
|
+ "id" => $material_model->id,
|
|
|
+ "order_number" => $data['order_number'],
|
|
|
+ "opt_case" => CheckService::sixteen,
|
|
|
+ "menu_id" => $data['menu_id']
|
|
|
+ ],$user);
|
|
|
+// if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ["order" => ['order_number' => $data['order_number'], 'is_check_stock' => $data['is_check_stock']]]];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function detail($data){
|
|
|
+ if($this->isEmpty($data,'order_number')) return [false,'请选择数据'];
|
|
|
+
|
|
|
+ $order = OutBoundOrder::where('order_number',$data['order_number'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->first();
|
|
|
+ if(empty($order)) return [false, '出库单不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ $order['storehouse_title'] = Storehouse::where('id',$order['storehouse_id'])->value('title');
|
|
|
+ $order['state_title'] = OutBoundOrder::$name[$order['state']] ?? '';
|
|
|
+
|
|
|
+ $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$order['crt_id']],[$order['out_id']])))
|
|
|
+ ->pluck('emp_name','id')
|
|
|
+ ->toArray();
|
|
|
+ $info = OutBoundOrderInfo::where('del_time',0)
|
|
|
+ ->where('order_number',$data['order_number'])
|
|
|
+ ->get()->toArray();
|
|
|
+ $map = (new ProductService())->getProductDetail(array_column($info,'product_id'));
|
|
|
+ foreach ($info as $value){
|
|
|
+ $tmp = $map[$value['product_id']] ?? [];
|
|
|
+ $value['title'] = $tmp['title'] ?? "";
|
|
|
+ $value['code'] = $tmp['code'] ?? "";
|
|
|
+ $value['size'] = $tmp['size'] ?? "";
|
|
|
+ $value['unit'] = $tmp['unit'] ?? "";
|
|
|
+ $value['bar_code'] = $tmp['bar_code'] ?? "";
|
|
|
+ $order['product'][] = $value;
|
|
|
+ }
|
|
|
+ $order['out_name'] = $emp_map[$order['out_id']] ?? '';
|
|
|
+ $order['crt_name'] = $emp_map[$order['crt_id']] ?? '';
|
|
|
+ $order['crt_time'] = $order['crt_time'] ? date("Y-m-d H:i:s",$order['crt_time']): '';
|
|
|
+
|
|
|
+ //可见范围
|
|
|
+// $return = (new RangeService())->RangeDetail($order['id'],SeeRange::type_three);
|
|
|
+// $order['depart'] = $return[0] ?? [];
|
|
|
+// $order['employee'] = $return[1] ?? [];
|
|
|
+
|
|
|
+ return [true, $order];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function del($data, $user){
|
|
|
+ if($this->isEmpty($data,'order_number')) return [false,'请选择数据'];
|
|
|
+
|
|
|
+ $order = OutBoundOrder::where('order_number',$data['order_number'])
|
|
|
+ ->first();
|
|
|
+ if(empty($order)) return [false,'出库单不存在或已被删除'];
|
|
|
+ $order = $order->toArray();
|
|
|
+ if($order['state'] > OutBoundOrder::STATE_ZERO) return [false,'请确认出库单状态,删除失败'];
|
|
|
+
|
|
|
+ $product_save = $this->getSaveDetail($data['id']);
|
|
|
+ try{
|
|
|
+ DB::beginTransaction();
|
|
|
+ OutBoundOrder::where('order_number',$data['order_number'])->update([
|
|
|
+ 'del_time'=>time()
|
|
|
+ ]);
|
|
|
+ OutBoundOrderInfo::where('order_number',$data['order_number'])->update([
|
|
|
+ 'del_time'=>time()
|
|
|
+ ]);
|
|
|
+// (new RangeService())->RangeDelete($order['id'],SeeRange::type_three);
|
|
|
+ //锁定库存释放
|
|
|
+ ProductInventoryService::changeLockNumber($user,[],$product_save);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $e){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$e->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getList($data,$user){
|
|
|
+ $model = OutBoundOrder::Clear($user,$data);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select('id','order_number','data_id','type','crt_id','mark','state','crt_time','storehouse_id','top_depart_id','storehouse_id','out_id')
|
|
|
+ ->orderby('id', 'desc');
|
|
|
+ if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
|
|
|
+ if(isset($data['state'])) $model->where('state',$data['state']);
|
|
|
+ if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
|
|
|
+ $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
|
|
|
+ $model->whereBetween('crt_time',[$return[0],$return[1]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['sale_order'])){
|
|
|
+ $sale_order_number = $data['sale_order'] ?? "";
|
|
|
+ $model2 = SalesOrder::Clear($user,$data);
|
|
|
+ $sale = $model2->where('del_time',0)
|
|
|
+ ->when(! empty($sale_order_number), function ($query) use ($sale_order_number) {
|
|
|
+ return $query->where('order_number', 'LIKE', '%'.$sale_order_number.'%');
|
|
|
+ })
|
|
|
+ ->select('id')
|
|
|
+ ->get()->toArray();
|
|
|
+ $model->whereIn('data_id',array_unique(array_column($sale,'id')))
|
|
|
+ ->where('type', OutBoundOrder::out_type_one);
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillListData($list);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillListData($data){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $depart_id = array_unique(array_column($data['data'],'depart_id'));
|
|
|
+ $depart_map = Depart::whereIn('id',$depart_id)
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $emp_id = array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'out_id')));
|
|
|
+ $emp_map = Employee::whereIn('id',$emp_id)
|
|
|
+ ->pluck('emp_name','id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','contact_order_no')->get()->toArray();
|
|
|
+ $sales = array_column($sales,null,'id');
|
|
|
+ $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['depart_name'] = $depart_map[$value['depart_id']] ?? '';
|
|
|
+ $data['data'][$key]['crt_name'] = $emp_map[$value['crt_id']] ?? '';
|
|
|
+ $data['data'][$key]['out_name'] = $emp_map[$value['out_id']] ?? '';
|
|
|
+ $data['data'][$key]['state_title'] = OutBoundOrder::$name[$value['state']] ?? '';
|
|
|
+ $tmp = $sales[$value['sales_order_id']] ?? [];
|
|
|
+ $data['data'][$key]['sales_order_number'] = $tmp['order_number'] ?? '';
|
|
|
+ $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function orderRule(&$data, $user, $is_check = true){
|
|
|
+ if(empty($data['storehouse_id'])) return [false, '仓库不能为空'];
|
|
|
+ if(empty($data['product'])) return [false, '发货数据不能为空'];
|
|
|
+ if(empty($data['data_id'])) return [false, '发货数据源id不能为空'];
|
|
|
+ if(empty($data['type'])) return [false, '发货类型不能为空'];
|
|
|
+ if(empty($data['out_id'])) $data['out_id'] = $user['id'];
|
|
|
+
|
|
|
+ $product_submit = $product_id = [];
|
|
|
+ foreach ($data['product'] as $value){
|
|
|
+ if(empty($value['product_id'])) return [false, '发货产品不能为空'];
|
|
|
+ if(empty($value['number'])) return [false, '发货产品数量不能为空'];
|
|
|
+ $res = $this->checkNumber($value['number']);
|
|
|
+ if(! $res) return [false,'请输入正确的产品数量'];
|
|
|
+ if(! isset($value['price'])) return [false, '发货产品单价不能为空'];
|
|
|
+ if(! isset($value['final_amount'])) return [false, '发货产品总价值不能为空'];
|
|
|
+
|
|
|
+ $key = $value['product_id'] . ',' .$data['storehouse_id'];
|
|
|
+ if(isset($product_submit[$key])){
|
|
|
+ $product_submit[$key] += $value['number'];
|
|
|
+ }else{
|
|
|
+ $product_submit[$key] = $value['number'];
|
|
|
+ }
|
|
|
+ $product_id[] = $value['product_id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ //剩余能发
|
|
|
+ $id = $data['id'] ?? 0;
|
|
|
+ if($data['type'] == OutBoundOrder::out_type_one) $s_product = $this->getSalesProduct(['out_bound_id' => $id, 'data_id' => $data['data_id']], $user);
|
|
|
+
|
|
|
+ //比较
|
|
|
+ foreach ($product_submit as $pro => $number){
|
|
|
+ $tmp = explode(',',$pro);
|
|
|
+ $p = $tmp[0];
|
|
|
+ if(! isset($s_product[$p])) return [false,'产品错误,合同中不存在该产品'];
|
|
|
+ $s_number = $s_product[$p];
|
|
|
+
|
|
|
+ if($number > $s_number) return [false,'发货产品数量不能超过合同产品数量'];
|
|
|
+ }
|
|
|
+
|
|
|
+ //已保存
|
|
|
+ $product_save = $this->getSaveDetail($id);
|
|
|
+
|
|
|
+ //校验库存
|
|
|
+ list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
+ //所属部门 以及 顶级部门
|
|
|
+ if(empty($data['depart_id'])) {
|
|
|
+ $data['depart_id'] = $this->getDepart($user);
|
|
|
+ $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $data['is_check_stock'] = $user['is_check_stock'];
|
|
|
+
|
|
|
+ if($is_check){
|
|
|
+ $order_number = (new OrderNoService())->createOrderNumber(OutBoundOrder::prefix);
|
|
|
+ if(empty($order_number)) return [false,'出库单号生成失败!'];
|
|
|
+ $data['order_number'] = $order_number;
|
|
|
+ }else{
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'出库单ID不能为空!'];
|
|
|
+ if($this->isEmpty($data,'order_number')) return [false,'出库单号不能为空!'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, [$product_submit, $product_save]];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function outBoundGetProduct($data, $user){
|
|
|
+ if(empty($data['data_id'])) return [false, '合同ID不能为空'];
|
|
|
+ $data_id = $data['data_id'];
|
|
|
+ $out_bound_id = $data['out_bound_id'] ?? 0;
|
|
|
+ if(empty($data['type'])) return [false, '出库类型不能为空'];
|
|
|
+ if(! in_array($data['type'], OutBoundOrder::$out_type)) return [false, '出库类型不存在'];
|
|
|
+
|
|
|
+ // 合同 发货
|
|
|
+ $return = [];
|
|
|
+ if($data['type'] == OutBoundOrder::out_type_one) $this->getSalesProduct($data, $user);
|
|
|
+
|
|
|
+ return [true, $return];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getSalesProduct($data, $user){
|
|
|
+ $return = [];
|
|
|
+ $data_id = $data['data_id'];
|
|
|
+ $out_bound_id = $data['out_bound_id'] ?? 0;
|
|
|
+ $product = SalesOrderProductInfo::where('del_time',0)
|
|
|
+ ->where('sales_order_id', $data_id)
|
|
|
+ ->select('product_id', 'number', 'final_amount','price')
|
|
|
+ ->get()->toArray();
|
|
|
+ $map = (new ProductService())->getProductDetail(array_column($product,'product_id'));
|
|
|
+
|
|
|
+ $product_map = [];
|
|
|
+ $save = OutBoundOrderInfo::where('del_time',0)
|
|
|
+ ->where('type', $data['type'])
|
|
|
+ ->where('id', $data_id)
|
|
|
+ ->when(! empty($out_bound_id), function ($query) use ($out_bound_id) {
|
|
|
+ return $query->where('out_bound_id', '<>', $out_bound_id);
|
|
|
+ })
|
|
|
+ ->select('product_id', 'number', 'final_amount')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($save as $value){
|
|
|
+ if(isset($product_map[$value['product_id']])){
|
|
|
+ $number = bcadd($value['number'], $product_map[$value['product_id']]);
|
|
|
+ $product_map[$value['product_id']] = $number;
|
|
|
+ }else{
|
|
|
+ $product_map[$value['product_id']] = $value['number'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $product_map2 = [];
|
|
|
+ $return_id = ReturnExchangeOrder::where('del_time',0)
|
|
|
+ ->where('type', ReturnExchangeOrder::Order_type)
|
|
|
+ ->where('model_type', ReturnExchangeOrder::Model_type_one)
|
|
|
+ ->where('data_id', $data_id)
|
|
|
+ ->select('id')
|
|
|
+ ->get()->toArray();
|
|
|
+ $save2 = ReturnExchangeOrderProductInfo::where('del_time',0)
|
|
|
+ ->whereIn('return_exchange_id', array_column($return_id,'id'))
|
|
|
+ ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
|
|
|
+ ->select('product_id', 'number', 'final_amount')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($save2 as $value){
|
|
|
+ if(isset($product_map2[$value['product_id']])){
|
|
|
+ $number = bcadd($value['number'], $product_map2[$value['product_id']]);
|
|
|
+ $product_map2[$value['product_id']] = $number;
|
|
|
+ }else{
|
|
|
+ $product_map2[$value['product_id']] = $value['number'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($product as $value){
|
|
|
+ $p1 = $product_map[$value['product_id']] ?? 0;
|
|
|
+ $p2 = $product_map2[$value['product_id']] ?? 0;
|
|
|
+ $number = bcadd(bcsub($value['number'], $p1), $p2);
|
|
|
+ if($number <= 0) continue;
|
|
|
+
|
|
|
+ $tmp = $map[$value['product_id']] ?? [];
|
|
|
+ $return[] = [
|
|
|
+ 'number' => $number,
|
|
|
+ 'price' => $value['price'],
|
|
|
+ 'final_amount' => bcmul($value['price'], $number,2),
|
|
|
+ 'product_id' => $value['product_id'],
|
|
|
+ 'title' => $tmp['title'] ?? "",
|
|
|
+ 'code' => $tmp['code'] ?? "",
|
|
|
+ 'size' => $tmp['size'] ?? "",
|
|
|
+ 'unit' => $tmp['unit'] ?? "",
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $return;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getSaveDetail($id){
|
|
|
+ $product_save = [];
|
|
|
+ if(empty($id)) return $product_save;
|
|
|
+
|
|
|
+ $sub = OutBoundOrderInfo::where('out_bound_id',$id)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($sub as $value){
|
|
|
+ $key = $value['product_id'] . ',' . $value['storehouse_id'];
|
|
|
+ if(isset($product_save[$key])){
|
|
|
+ $product_save[$key] += $value['number'];
|
|
|
+ }else{
|
|
|
+ $product_save[$key] = $value['number'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $product_save;
|
|
|
+ }
|
|
|
+}
|