FyyOrderService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Box;
  5. use App\Model\BoxDetail;
  6. use App\Model\Orders;
  7. use App\Model\SaleOrdersProduct;
  8. use App\Model\SaleOrdersProductStockDetail;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Redis;
  11. class FyyOrderService extends Service
  12. {
  13. public function edit($data){
  14. return [true,'保存成功!'];
  15. }
  16. public function setOrderNo(){
  17. $order_no = date('Ymd') . time() . rand(1000, 9999);
  18. return $order_no;
  19. }
  20. public function add($data,$user){
  21. //获取数据
  22. $sqlServerModel = new FyySqlServerService($user['id']);
  23. list($status,$return,$return_stock_detail) = $sqlServerModel->getDataFromSqlServer($data);
  24. if(! $status) return [false, $return];
  25. //数据校验
  26. // list($status,$msg) = $this->orderRule($return);
  27. // if(!$status) return [$status,$msg];
  28. try{
  29. DB::beginTransaction();
  30. $keys = array_unique(array_column($return,'out_order_no'));
  31. $map = array_fill_keys($keys, 0);
  32. $orders = [];
  33. foreach ($return as $key => $value){
  34. if(! empty($map[$value['out_order_no']])){
  35. $order_no = $map[$value['out_order_no']];
  36. }else{
  37. $order_no = $this->setOrderNo();
  38. $map[$value['out_order_no']] = $order_no;
  39. $orders[] = [
  40. 'order_no' => $order_no,
  41. 'out_order_no' => $value['out_order_no'],
  42. 'crt_time' => time()
  43. ];
  44. }
  45. $return[$key]['order_no'] = $order_no;
  46. $return[$key]['crt_id'] = $user['id'];
  47. $return[$key]['crt_time'] = time();
  48. }
  49. $args = '';
  50. if(! empty($return_stock_detail)){
  51. foreach ($return_stock_detail as $value){
  52. $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
  53. }
  54. $args = rtrim($args,'OR ');
  55. SaleOrdersProductStockDetail::where('del_time',0)
  56. ->whereRaw("($args)")
  57. ->update(['del_time' => time()]);
  58. }
  59. Orders::insert($orders);
  60. SaleOrdersProduct::insert($return);
  61. SaleOrdersProductStockDetail::insert($return_stock_detail);
  62. DB::commit();
  63. }catch (\Exception $e){
  64. DB::rollBack();
  65. return [false,$e->getLine().':'.$e->getMessage()];
  66. }
  67. return [true,'保存成功!'];
  68. }
  69. public function del($data){
  70. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  71. Orders::whereIn('id',$data['id'])->update([
  72. 'del_time' => time()
  73. ]);
  74. SaleOrdersProduct::where('id',$data['id'])->update([
  75. 'del_time'=>time()
  76. ]);
  77. return [true,'删除成功'];
  78. }
  79. public function orderDetail($data){
  80. return [200,''];
  81. }
  82. public function orderList($data){
  83. $model = SaleOrdersProduct::where('del_time',0)
  84. ->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')
  85. ->orderBy('crt_time','desc')
  86. ->orderBy('id','asc');
  87. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  88. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  89. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  90. if(! empty($data['product_no'])) $model->where('product_no', 'LIKE', '%'.$data['product_no'].'%');
  91. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  92. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  93. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  94. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  95. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  96. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  97. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  98. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  99. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  100. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  101. 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]]);
  102. 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]]);
  103. if(isset($data['status'])) $model->where('status',$data['status']);
  104. $list = $this->limit($model,'',$data);
  105. $list = $this->fillData($list);
  106. return [true, $list];
  107. }
  108. public function fillData($data){
  109. if(empty($data['data'])) return $data;
  110. $product_no = array_filter(array_column($data['data'],'product_no'));
  111. $detail = SaleOrdersProductStockDetail::where('del_time',0)
  112. ->whereIn('product_no',$product_no)
  113. ->select('product_no','product_quantity_on_hand','technology_name','wood_name','warehouse_name')
  114. ->get()->toArray();
  115. $detailMap = $detail_map = [];
  116. foreach ($detail as $value){
  117. $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
  118. $detail_map[$keys][] = [
  119. 'warehouse_name' => $value['warehouse_name'],
  120. 'product_quantity_on_hand' => $value['product_quantity_on_hand']
  121. ];
  122. if(isset($detailMap[$keys])){
  123. $detailMap[$keys] += $value['product_quantity_on_hand'];
  124. }else{
  125. $detailMap[$keys] = $value['product_quantity_on_hand'];
  126. }
  127. }
  128. foreach ($data['data'] as $key => $value){
  129. $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
  130. $data['data'][$key]['sub'] = $detail_map[$keys] ?? [];
  131. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  132. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  133. $data['data'][$key]['product_quantity_on_hand'] = $detailMap[$keys] ?? 0;
  134. }
  135. $data['order_quantity'] = array_sum(array_column($data['data'], 'order_quantity'));
  136. $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
  137. $data['product_quantity_on_hand'] = array_sum(array_column($data['data'], 'product_quantity_on_hand'));
  138. return $data;
  139. }
  140. public function orderRule($data){
  141. $result = Orders::where('del_time',0)
  142. ->whereIn('out_order_no',array_column($data,'out_order_no'))
  143. ->select('out_order_no')
  144. ->get()->toArray();
  145. if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
  146. return [true,''];
  147. }
  148. public function fyyRefreshOnHandQuantity($data,$user){
  149. //获取数据
  150. $sqlServerModel = new FyySqlServerService($user['id']);
  151. list($status,$return,$return_product) = $sqlServerModel->getDataFromSqlServerForOnHand($data);
  152. if(! $status) return [false, $return];
  153. DB::beginTransaction();
  154. try {
  155. $args = '';
  156. foreach ($return_product as $value){
  157. $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
  158. }
  159. $args = rtrim($args,'OR ');
  160. SaleOrdersProductStockDetail::where('del_time',0)
  161. ->whereRaw("($args)")
  162. ->update(['del_time' => time()]);
  163. SaleOrdersProductStockDetail::insert($return);
  164. DB::commit();
  165. }catch (\Throwable $exception){
  166. DB::rollBack();
  167. return [false,$exception->getMessage()];
  168. }
  169. return [true,''];
  170. }
  171. public function fyySaveOutOrder($data,$user){
  172. list($status,$msg) = $this->orderOutRule($data);
  173. if(! $status) return [false,$msg];
  174. $user = [
  175. 'id' => $user['id'],
  176. 'operate_time' => time()
  177. ];
  178. $redis = [
  179. 'result' => $msg,
  180. 'data' => $data,
  181. ];
  182. $job = dispatch(new ProcessDataJob($redis,$user,2))->onQueue(ProcessDataJob::job_two);
  183. if(! $job) return [false,'任务没有进入队列!'];
  184. //错误计数
  185. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  186. //标记进入队列的数据
  187. Box::whereIn('order_no',$data['order_no'])->update([
  188. 'state' => 1,
  189. ]);
  190. return [true,'任务已进入队列!'];
  191. }
  192. public function addInJob($result,$data,$user){
  193. try{
  194. $sqlServerModel = new FyySqlServerService($user['id']);
  195. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  196. list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($data['post']);
  197. if(! $status) return [false,$msg];
  198. //更新
  199. DB::beginTransaction();
  200. Box::whereIn('order_no',$data['order_no'])->update([
  201. 'state' => 2,
  202. 'shipment_order_no' => $data['shipment_code'],//发货单号
  203. ]);
  204. $box = new BoxDetail(['channel'=>$data['top_order_no']]);
  205. $box->where('order_no',$data['order_no'])->update([
  206. 'shipment_order_no' => $data['shipment_code']//发货单号
  207. ]);
  208. foreach ($data['box_data'] as $value){
  209. SaleOrdersProduct::where('id',$value['top_id'])->update([
  210. 'shipment_num' => DB::raw("shipment_num + {$value['num']}"),
  211. ]);
  212. }
  213. DB::commit();
  214. }catch (\Exception $e){
  215. DB::rollBack();
  216. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  217. }
  218. return [true,''];
  219. }
  220. public function orderOutRule($data){
  221. if(empty($data['order_no'])) return [false,'包装单号不能为空!'];
  222. if(empty($data['order_number'])) return [false,'销售订单号不能为空!'];
  223. if(empty($data['post'])) return [false,'发货单数据不能为空!'];
  224. if(empty($data['box_data'])) return [false,'包装单数据不能为空!'];
  225. if(empty($data['top_order_no'])) return [false,'顶部订单号不能为空!'];
  226. if(empty($data['shipment_code'])) return [false,'发货单订单号不能为空!'];
  227. $boxList = Box::whereIn('order_no',$data['order_no'])->get()->toArray();
  228. if(empty($boxList)) return [false,'包装单不存在!'];
  229. foreach ($boxList as $value){
  230. if($value['del_time'] > 0) return [false,'包装单:' . $value['order_no'] . '已删除'];
  231. if($value['state'] == 1) return [false,'包装单:' . $value['order_no'] . '已操作出库!'];
  232. if($value['state'] > 1) return [false,'包装单:' . $value['order_no'] . '已出库!'];
  233. }
  234. return [true, ''];
  235. }
  236. public function orderMobileList($data){
  237. $list = [];
  238. if($data['type'] == 1){
  239. $list = SaleOrdersProduct::where('del_time',0)
  240. ->select('order_no','out_order_no','customer_name')
  241. ->where('box_num', '>', 0)
  242. ->whereColumn('order_quantity', '>', 'shipment_num')
  243. ->groupBy('order_no')
  244. ->orderBy('id','desc')
  245. ->get()->toArray();
  246. $key_list = [];
  247. foreach ($list as $v){
  248. $customer_name = trim($v['customer_name']);
  249. if(! isset($key_list[$customer_name])) $key_list[$customer_name] = [
  250. 'customer_name' => $customer_name,
  251. 'list' => [],
  252. ];
  253. $key_list[$customer_name]['list'][] = $v;
  254. }
  255. sort($key_list);
  256. $list = $key_list;
  257. }elseif($data['type'] == 2){
  258. $list = SaleOrdersProduct::where('del_time',0)
  259. ->select('order_no','out_order_no','customer_name')
  260. // ->whereColumn('order_quantity', '>', 'box_num')
  261. ->where(function ($query){
  262. $query->whereColumn('order_quantity', '>', 'box_num')->orwhereColumn('box_num','=','order_quantity');
  263. })
  264. ->groupBy('order_no')
  265. ->orderBy('id','desc')
  266. ->get()->toArray();
  267. $key_list = [];
  268. foreach ($list as $v){
  269. $customer_name = $v['customer_name'];
  270. if(!isset($key_list[$customer_name])) $key_list[$customer_name] = [
  271. 'customer_name' => $customer_name,
  272. 'list' => [],
  273. ];
  274. $key_list[$customer_name]['list'][] = $v;
  275. }
  276. sort($key_list);
  277. $list = $key_list;
  278. }
  279. return [true, $list];
  280. }
  281. public function getShipmentOrder($data,$user){
  282. if(empty($data['time'][0]) || empty($data['time'][1])) return ['false', '时间范围不能为空!'];
  283. $sqlServerModel = new FyySqlServerService($user['id']);
  284. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  285. $result = $sqlServerModel->getDataFromDispatchList($data);
  286. $return = $shipment_code = [];
  287. if(! empty($result)){
  288. foreach ($result as $value){
  289. $cfree1 = $value['cfree1'] ?? '';
  290. $cfree2 = $value['cfree2'] ?? '';
  291. $tmp_key = $value['cdlcode'] . $value['cinvcode'] . $cfree1 . $cfree2;
  292. $n = $value['iquantity'] - ($value['out_quantity'] ?? 0);
  293. if(isset($return[$tmp_key])){
  294. $return[$tmp_key]['num'] += $n;
  295. }else{
  296. $return[$tmp_key] = [
  297. 'cdlcode' => $value['cdlcode'],
  298. 'product_no' => $value['cinvcode'],
  299. 'product_title' => $value['product_title'],
  300. 'technology_name' => $cfree1 ?? '',
  301. 'wood_name' => $cfree2 ?? '',
  302. 'num' => $n,
  303. 'technology_material' => $value['technology_material'] ?? '',
  304. 'process_mark' => $value['process_mark'] ?? '',
  305. 'product_size' => $value['product_size'] ?? ''
  306. ];
  307. }
  308. if(! isset($shipment_code[$value['cdlcode']])){
  309. $shipment_code[$value['cdlcode']] = [
  310. 'shipment_code' => $value['cdlcode'],
  311. 'order_no' => $value['csocode'],
  312. 'customer_name' => $value['customer_name']
  313. ];
  314. }
  315. }
  316. }
  317. return [true,['shipment_code'=>array_values($shipment_code), 'show'=>array_values($return), 'post'=>$result]];
  318. }
  319. }