123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- namespace App\Service;
- use App\Jobs\ProcessDataJob;
- use App\Model\Box;
- use App\Model\BoxDetail;
- use App\Model\Orders;
- use App\Model\SaleOrdersProduct;
- use App\Model\SaleOrdersProductStockDetail;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- class FyyOrderService extends Service
- {
- public function edit($data){
- return [true,'保存成功!'];
- }
- public function setOrderNo(){
- $order_no = date('Ymd') . time() . rand(1000, 9999);
- return $order_no;
- }
- public function add($data,$user){
- //获取数据
- $sqlServerModel = new FyySqlServerService($user['id']);
- list($status,$return,$return_stock_detail) = $sqlServerModel->getDataFromSqlServer($data);
- if(! $status) return [false, $return];
- //数据校验
- // list($status,$msg) = $this->orderRule($return);
- // if(!$status) return [$status,$msg];
- try{
- DB::beginTransaction();
- $keys = array_unique(array_column($return,'out_order_no'));
- $map = array_fill_keys($keys, 0);
- $orders = [];
- foreach ($return as $key => $value){
- if(! empty($map[$value['out_order_no']])){
- $order_no = $map[$value['out_order_no']];
- }else{
- $order_no = $this->setOrderNo();
- $map[$value['out_order_no']] = $order_no;
- $orders[] = [
- 'order_no' => $order_no,
- 'out_order_no' => $value['out_order_no'],
- 'crt_time' => time()
- ];
- }
- $return[$key]['order_no'] = $order_no;
- $return[$key]['crt_id'] = $user['id'];
- $return[$key]['crt_time'] = time();
- }
- $args = '';
- if(! empty($return_stock_detail)){
- foreach ($return_stock_detail as $value){
- $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
- }
- $args = rtrim($args,'OR ');
- SaleOrdersProductStockDetail::where('del_time',0)
- ->whereRaw("($args)")
- ->update(['del_time' => time()]);
- }
- Orders::insert($orders);
- SaleOrdersProduct::insert($return);
- SaleOrdersProductStockDetail::insert($return_stock_detail);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getLine().':'.$e->getMessage()];
- }
- return [true,'保存成功!'];
- }
- public function del($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- Orders::whereIn('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- SaleOrdersProduct::where('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- return [true,'删除成功'];
- }
- public function orderDetail($data){
- return [200,''];
- }
- public function orderList($data){
- $model = SaleOrdersProduct::where('del_time',0)
- ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','box_num','finished_num','shipment_num')
- ->orderBy('crt_time','desc')
- ->orderBy('id','asc');
- if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
- if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
- if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
- if(! empty($data['product_no'])) $model->where('product_no', 'LIKE', '%'.$data['product_no'].'%');
- if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
- if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
- if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
- if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
- if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
- if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
- if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
- if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
- if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
- if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
- if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
- if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
- if(isset($data['status'])) $model->where('status',$data['status']);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $product_no = array_filter(array_column($data['data'],'product_no'));
- $detail = SaleOrdersProductStockDetail::where('del_time',0)
- ->whereIn('product_no',$product_no)
- ->select('product_no','product_quantity_on_hand','technology_name','wood_name','warehouse_name')
- ->get()->toArray();
- $detailMap = $detail_map = [];
- foreach ($detail as $value){
- $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
- $detail_map[$keys][] = [
- 'warehouse_name' => $value['warehouse_name'],
- 'product_quantity_on_hand' => $value['product_quantity_on_hand']
- ];
- if(isset($detailMap[$keys])){
- $detailMap[$keys] += $value['product_quantity_on_hand'];
- }else{
- $detailMap[$keys] = $value['product_quantity_on_hand'];
- }
- }
- foreach ($data['data'] as $key => $value){
- $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
- $data['data'][$key]['sub'] = $detail_map[$keys] ?? [];
- $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
- $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
- $data['data'][$key]['product_quantity_on_hand'] = $detailMap[$keys] ?? 0;
- }
- $data['order_quantity'] = array_sum(array_column($data['data'], 'order_quantity'));
- $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
- $data['product_quantity_on_hand'] = array_sum(array_column($data['data'], 'product_quantity_on_hand'));
- return $data;
- }
- public function orderRule($data){
- $result = Orders::where('del_time',0)
- ->whereIn('out_order_no',array_column($data,'out_order_no'))
- ->select('out_order_no')
- ->get()->toArray();
- if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
- return [true,''];
- }
- public function fyyRefreshOnHandQuantity($data,$user){
- //获取数据
- $sqlServerModel = new FyySqlServerService($user['id']);
- list($status,$return,$return_product) = $sqlServerModel->getDataFromSqlServerForOnHand($data);
- if(! $status) return [false, $return];
- DB::beginTransaction();
- try {
- $args = '';
- foreach ($return_product as $value){
- $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
- }
- $args = rtrim($args,'OR ');
- SaleOrdersProductStockDetail::where('del_time',0)
- ->whereRaw("($args)")
- ->update(['del_time' => time()]);
- SaleOrdersProductStockDetail::insert($return);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function fyySaveOutOrder($data,$user){
- list($status,$msg) = $this->orderOutRule($data);
- if(! $status) return [false,$msg];
- $user = [
- 'id' => $user['id'],
- 'operate_time' => time()
- ];
- $redis = [
- 'result' => $msg,
- 'data' => $data,
- ];
- $job = dispatch(new ProcessDataJob($redis,$user,2))->onQueue(ProcessDataJob::job_two);
- if(! $job) return [false,'任务没有进入队列!'];
- //错误计数
- Redis::hSet('order_failures', md5(json_encode($redis)), 0);
- //标记进入队列的数据
- Box::whereIn('order_no',$data['order_no'])->update([
- 'state' => 1,
- ]);
- return [true,'任务已进入队列!'];
- }
- public function addInJob($result,$data,$user){
- try{
- $sqlServerModel = new FyySqlServerService($user['id']);
- if($sqlServerModel->error) return [false,$sqlServerModel->error];
- list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($data['post']);
- if(! $status) return [false,$msg];
- //更新
- DB::beginTransaction();
- Box::whereIn('order_no',$data['order_no'])->update([
- 'state' => 2,
- 'shipment_order_no' => $data['shipment_code'],//发货单号
- ]);
- $box = new BoxDetail(['channel'=>$data['top_order_no']]);
- $box->where('order_no',$data['order_no'])->update([
- 'shipment_order_no' => $data['shipment_code']//发货单号
- ]);
- foreach ($data['box_data'] as $value){
- SaleOrdersProduct::where('id',$value['top_id'])->update([
- 'shipment_num' => DB::raw("shipment_num + {$value['num']}"),
- ]);
- }
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
- }
- return [true,''];
- }
- public function orderOutRule($data){
- if(empty($data['order_no'])) return [false,'包装单号不能为空!'];
- if(empty($data['order_number'])) return [false,'销售订单号不能为空!'];
- if(empty($data['post'])) return [false,'发货单数据不能为空!'];
- if(empty($data['box_data'])) return [false,'包装单数据不能为空!'];
- if(empty($data['top_order_no'])) return [false,'顶部订单号不能为空!'];
- if(empty($data['shipment_code'])) return [false,'发货单订单号不能为空!'];
- $boxList = Box::whereIn('order_no',$data['order_no'])->get()->toArray();
- if(empty($boxList)) return [false,'包装单不存在!'];
- foreach ($boxList as $value){
- if($value['del_time'] > 0) return [false,'包装单:' . $value['order_no'] . '已删除'];
- if($value['state'] == 1) return [false,'包装单:' . $value['order_no'] . '已操作出库!'];
- if($value['state'] > 1) return [false,'包装单:' . $value['order_no'] . '已出库!'];
- }
- return [true, ''];
- }
- public function orderMobileList($data){
- $list = [];
- if($data['type'] == 1){
- $list = SaleOrdersProduct::where('del_time',0)
- ->select('order_no','out_order_no','customer_name')
- ->where('box_num', '>', 0)
- ->whereColumn('order_quantity', '>', 'shipment_num')
- ->groupBy('order_no')
- ->orderBy('id','desc')
- ->get()->toArray();
- $key_list = [];
- foreach ($list as $v){
- $customer_name = trim($v['customer_name']);
- if(! isset($key_list[$customer_name])) $key_list[$customer_name] = [
- 'customer_name' => $customer_name,
- 'list' => [],
- ];
- $key_list[$customer_name]['list'][] = $v;
- }
- sort($key_list);
- $list = $key_list;
- }elseif($data['type'] == 2){
- $list = SaleOrdersProduct::where('del_time',0)
- ->select('order_no','out_order_no','customer_name')
- // ->whereColumn('order_quantity', '>', 'box_num')
- ->where(function ($query){
- $query->whereColumn('order_quantity', '>', 'box_num')->orwhereColumn('box_num','=','order_quantity');
- })
- ->groupBy('order_no')
- ->orderBy('id','desc')
- ->get()->toArray();
- $key_list = [];
- foreach ($list as $v){
- $customer_name = $v['customer_name'];
- if(!isset($key_list[$customer_name])) $key_list[$customer_name] = [
- 'customer_name' => $customer_name,
- 'list' => [],
- ];
- $key_list[$customer_name]['list'][] = $v;
- }
- sort($key_list);
- $list = $key_list;
- }
- return [true, $list];
- }
- public function getShipmentOrder($data,$user){
- if(empty($data['time'][0]) || empty($data['time'][1])) return ['false', '时间范围不能为空!'];
- $sqlServerModel = new FyySqlServerService($user['id']);
- if($sqlServerModel->error) return [false,$sqlServerModel->error];
- $result = $sqlServerModel->getDataFromDispatchList($data);
- $return = $shipment_code = [];
- if(! empty($result)){
- foreach ($result as $value){
- $cfree1 = $value['cfree1'] ?? '';
- $cfree2 = $value['cfree2'] ?? '';
- $tmp_key = $value['cdlcode'] . $value['cinvcode'] . $cfree1 . $cfree2;
- $n = $value['iquantity'] - ($value['out_quantity'] ?? 0);
- if(isset($return[$tmp_key])){
- $return[$tmp_key]['num'] += $n;
- }else{
- $return[$tmp_key] = [
- 'cdlcode' => $value['cdlcode'],
- 'product_no' => $value['cinvcode'],
- 'product_title' => $value['product_title'],
- 'technology_name' => $cfree1 ?? '',
- 'wood_name' => $cfree2 ?? '',
- 'num' => $n,
- 'technology_material' => $value['technology_material'] ?? '',
- 'process_mark' => $value['process_mark'] ?? '',
- 'product_size' => $value['product_size'] ?? ''
- ];
- }
- if(! isset($shipment_code[$value['cdlcode']])){
- $shipment_code[$value['cdlcode']] = [
- 'shipment_code' => $value['cdlcode'],
- 'order_no' => $value['csocode'],
- 'customer_name' => $value['customer_name']
- ];
- }
- }
- }
- return [true,['shipment_code'=>array_values($shipment_code), 'show'=>array_values($return), 'post'=>$result]];
- }
- }
|