FyyOrderService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Orders;
  4. use App\Model\SaleOrdersProduct;
  5. use App\Model\SaleOrdersProductStockDetail;
  6. use Illuminate\Support\Facades\DB;
  7. class FyyOrderService extends Service
  8. {
  9. public function edit($data){
  10. return [true,'保存成功!'];
  11. }
  12. public function setOrderNo(){
  13. date_default_timezone_set('PRC');
  14. $order_no = date('Ymd') . time() . rand(1000, 9999);
  15. return $order_no;
  16. }
  17. public function add($data,$user){
  18. //获取数据
  19. $sqlServerModel = new FyySqlServerService();
  20. list($status,$return,$return_stock_detail) = $sqlServerModel->getDataFromSqlServer($data);
  21. if(! $status) return [false, $return];
  22. //数据校验
  23. // list($status,$msg) = $this->orderRule($return);
  24. // if(!$status) return [$status,$msg];
  25. try{
  26. DB::beginTransaction();
  27. $keys = array_unique(array_column($return,'out_order_no'));
  28. $map = array_fill_keys($keys, 0);
  29. $orders = [];
  30. foreach ($return as $key => $value){
  31. if(! empty($map[$value['out_order_no']])){
  32. $order_no = $map[$value['out_order_no']];
  33. }else{
  34. $order_no = $this->setOrderNo();
  35. $map[$value['out_order_no']] = $order_no;
  36. $orders[] = [
  37. 'order_no' => $order_no,
  38. 'out_order_no' => $value['out_order_no'],
  39. 'crt_time' => time()
  40. ];
  41. }
  42. $return[$key]['order_no'] = $order_no;
  43. $return[$key]['crt_id'] = $user['id'];
  44. $return[$key]['crt_time'] = time();
  45. }
  46. $args = '';
  47. if(! empty($return_stock_detail)){
  48. foreach ($return_stock_detail as $value){
  49. $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
  50. }
  51. $args = rtrim($args,'OR ');
  52. SaleOrdersProductStockDetail::where('del_time',0)
  53. ->whereRaw("($args)")
  54. ->update(['del_time' => time()]);
  55. }
  56. Orders::insert($orders);
  57. SaleOrdersProduct::insert($return);
  58. SaleOrdersProductStockDetail::insert($return_stock_detail);
  59. DB::commit();
  60. }catch (\Exception $e){
  61. DB::rollBack();
  62. return [false,$e->getLine().':'.$e->getMessage()];
  63. }
  64. return [true,'保存成功!'];
  65. }
  66. public function del($data){
  67. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  68. Orders::whereIn('id',$data['id'])->update([
  69. 'del_time' => time()
  70. ]);
  71. SaleOrdersProduct::where('id',$data['id'])->update([
  72. 'del_time'=>time()
  73. ]);
  74. return [true,'删除成功'];
  75. }
  76. public function orderDetail($data){
  77. return [200,''];
  78. }
  79. public function orderList($data){
  80. $model = SaleOrdersProduct::where('del_time',0)
  81. ->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')
  82. ->orderBy('id','desc');
  83. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  84. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  85. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  86. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  87. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  88. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  89. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  90. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  91. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  92. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  93. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  94. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  95. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  96. 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]]);
  97. 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]]);
  98. if(isset($data['status'])) $model->where('status',$data['status']);
  99. $list = $this->limit($model,'',$data);
  100. $list = $this->fillData($list);
  101. return [true, $list];
  102. }
  103. public function fillData($data){
  104. if(empty($data['data'])) return $data;
  105. $product_no = array_filter(array_column($data['data'],'product_no'));
  106. $detail = SaleOrdersProductStockDetail::where('del_time',0)
  107. ->whereIn('product_no',$product_no)
  108. ->select('product_no','product_quantity_on_hand','technology_name','wood_name','warehouse_name')
  109. ->get()->toArray();
  110. $detailMap = $detail_map = [];
  111. foreach ($detail as $value){
  112. $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
  113. $detail_map[$keys][] = [
  114. 'warehouse_name' => $value['warehouse_name'],
  115. 'product_quantity_on_hand' => $value['product_quantity_on_hand']
  116. ];
  117. if(isset($detailMap[$keys])){
  118. $detailMap[$keys] += $value['product_quantity_on_hand'];
  119. }else{
  120. $detailMap[$keys] = $value['product_quantity_on_hand'];
  121. }
  122. }
  123. date_default_timezone_set("PRC");
  124. foreach ($data['data'] as $key => $value){
  125. $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
  126. $data['data'][$key]['sub'] = $detail_map[$keys] ?? [];
  127. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  128. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  129. $data['data'][$key]['product_quantity_on_hand'] = $detailMap[$keys] ?? 0;
  130. }
  131. $data['order_quantity'] = array_sum(array_column($data['data'], 'order_quantity'));
  132. $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
  133. $data['product_quantity_on_hand'] = array_sum(array_column($data['data'], 'product_quantity_on_hand'));
  134. return $data;
  135. }
  136. public function orderRule($data){
  137. $result = Orders::where('del_time',0)
  138. ->whereIn('out_order_no',array_column($data,'out_order_no'))
  139. ->select('out_order_no')
  140. ->get()->toArray();
  141. if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
  142. return [true,''];
  143. }
  144. public function fyyRefreshOnHandQuantity($data){
  145. //获取数据
  146. $sqlServerModel = new FyySqlServerService();
  147. list($status,$return,$return_product) = $sqlServerModel->getDataFromSqlServerForOnHand($data);
  148. if(! $status) return [false, $return];
  149. DB::beginTransaction();
  150. try {
  151. $args = '';
  152. foreach ($return_product as $value){
  153. $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
  154. }
  155. $args = rtrim($args,'OR ');
  156. SaleOrdersProductStockDetail::where('del_time',0)
  157. ->whereRaw("($args)")
  158. ->update(['del_time' => time()]);
  159. SaleOrdersProductStockDetail::insert($return);
  160. DB::commit();
  161. }catch (\Throwable $exception){
  162. DB::rollBack();
  163. return [false,$exception->getMessage()];
  164. }
  165. return [true,''];
  166. }
  167. public function fyySaveOutOrder($data){
  168. if(empty($data['order_number'])) return [false,'销售订单号不能为空!'];
  169. //销售出库单生成
  170. $sqlServerModel = new FyySqlServerService();
  171. list($status,$return) = $sqlServerModel->U8Rdrecord32Save($data);
  172. if(! $status) return [false, $return];
  173. return [true,''];
  174. }
  175. }