FyyOrderService.php 16 KB

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