FinishedOrderService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Dispatch;
  4. use App\Model\DispatchSub;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeTeamPermission;
  7. use App\Model\Equipment;
  8. use App\Model\FinishedOrder;
  9. use App\Model\FinishedOrderScrapp;
  10. use App\Model\FinishedOrderSub;
  11. use App\Model\OrdersProduct;
  12. use App\Model\OrdersProductProcess;
  13. use App\Model\Process;
  14. use App\Model\SaleOrdersProduct;
  15. use App\Model\Scrapp;
  16. use App\Model\Team;
  17. use Illuminate\Support\Facades\DB;
  18. class FinishedOrderService extends Service
  19. {
  20. public function edit($data){}
  21. public function setOrderNO(){
  22. $str = date('Ymd',time());
  23. $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')
  24. ->max('finished_no');
  25. if(empty($order_number)){
  26. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  27. $number = $str . $number;
  28. }else{
  29. $tmp = substr($order_number, -3);
  30. $tmp = $tmp + 1;
  31. //超过99999
  32. if(strlen($tmp) > 3) return '';
  33. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  34. $number = $str . $number;
  35. }
  36. return $number;
  37. }
  38. public function add($data,$user){
  39. //数据校验以及填充
  40. list($status,$msg) = $this->orderRule($data);
  41. if(!$status) return [$status,$msg];
  42. try{
  43. DB::beginTransaction();
  44. $waste = [];
  45. foreach ($data['waste'] as $key => $value){
  46. $waste[$key] = array_sum(array_column($value,'num'));
  47. }
  48. //生产数据的源数据
  49. $result = $msg;
  50. $time = time();
  51. date_default_timezone_set("PRC");
  52. foreach ($result as $key => $value){
  53. $quantity_tmp = $data['quantity'][$key];
  54. $finished_id_tmp = $data['finish_id'][$key];
  55. $team_tmp = $data['team_id'][$key];
  56. $equipment_id_tmp = $data['equipment_id'][$key];
  57. $finished_num = $quantity_tmp + $value['finished_num'];
  58. DispatchSub::where('id',$value['id'])
  59. ->update([
  60. 'finished_num' => $finished_num,
  61. 'waste_num' => $waste[$key]
  62. ]);
  63. $insert_waste = [];
  64. if(! empty($data['waste'][$key])){
  65. foreach ($data['waste'][$key] as $v){
  66. for($i = 0 ;$i < $v['num']; $i++){
  67. $insert_waste[] = [
  68. 'order_product_id' => $value['order_product_id'],
  69. 'order_no' => $value['order_no'],
  70. 'product_no' => $value['product_no'],
  71. 'product_title' => $value['product_title'],
  72. 'process_id' => $value['process_id'],
  73. 'crt_time' => $time,
  74. 'dispatch_no' => $value['dispatch_no'],
  75. 'status' => 4,
  76. 'team_id' => $team_tmp,
  77. 'finished_id' => $finished_id_tmp,
  78. 'equipment_id' => $equipment_id_tmp,
  79. 'scrapp_id' => $v['scrapp_id']
  80. ];
  81. }
  82. }
  83. }
  84. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  85. $process_model->where('order_product_id',$value['order_product_id'])
  86. ->where('process_id',$value['process_id'])
  87. ->where('dispatch_no',$value['dispatch_no'])
  88. ->take($quantity_tmp)
  89. ->update([
  90. 'finished_time' => $time,
  91. 'status' => 2,
  92. 'finished_id' => $finished_id_tmp,
  93. 'team_id' => $team_tmp,
  94. 'equipment_id' => $equipment_id_tmp
  95. ]);
  96. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  97. }
  98. $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
  99. DB::commit();
  100. }catch (\Exception $e){
  101. DB::rollBack();
  102. return [false,$e->getLine().':'.$e->getMessage()];
  103. }
  104. return [true,'保存成功!'];
  105. }
  106. public function del($data){
  107. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  108. return [true,'删除成功'];
  109. }
  110. public function orderDetail($data){
  111. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  112. date_default_timezone_set("PRC");
  113. $first = DispatchSub::where('id',$data['id'])->first();
  114. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
  115. $result = $process_model->where('del_time',0)
  116. ->where('dispatch_no',$first->dispatch_no)
  117. ->where('order_product_id',$first->order_product_id)
  118. ->where('status',4)
  119. ->select(DB::raw('count(id) as num'),'scrapp_id')
  120. ->groupBy('scrapp_id')
  121. ->get()->toArray();
  122. $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
  123. ->pluck('title','id')
  124. ->toArray();
  125. foreach ($result as $key => $value){
  126. $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
  127. }
  128. return [true,$result];
  129. }
  130. public function is_same_month($timestamp1,$timestamp2){
  131. date_default_timezone_set("PRC");
  132. // 格式化时间戳为年份和月份
  133. $year1 = date('Y', $timestamp1);
  134. $month1 = date('m', $timestamp1);
  135. $year2 = date('Y', $timestamp2);
  136. $month2 = date('m', $timestamp2);
  137. if ($year1 === $year2 && $month1 === $month2) {
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. public function orderRule($data){
  144. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  145. if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
  146. if($this->isEmpty($data,'finish_id') && $this->isEmpty($data,'team_id')) return [false,'人员和班组不能都为空!'];
  147. $result = DispatchSub::whereIn('id',$data['id'])
  148. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title')
  149. ->orderBy('id','desc')
  150. ->get()->toArray();
  151. foreach ($result as $key => $value){
  152. if(($data['quantity'][$key] + $value['finished_num']) > $value['dispatch_quantity']) return [false,'完工数量不能大于派工数量'];
  153. }
  154. return [true, $result];
  155. }
  156. public function orderList($data){
  157. $model = FinishedOrderSub::where('del_time',0)
  158. ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','dispatch_quantity','finished_num','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time','crt_time','dispatch_no')
  159. ->orderBy('id','desc');
  160. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  161. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  162. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  163. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  164. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  165. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  166. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  167. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  168. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  169. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
  170. if(! empty($data['employee_id'])) {
  171. $team_id = Employee::from('employee as a')
  172. ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
  173. ->where('a.id', $data['employee_id'])
  174. ->select('b.team_id')
  175. ->get()->toArray();
  176. $team_id = array_column($team_id,'team_id');
  177. $model->where('team_id',$team_id ?? []);
  178. }
  179. if(isset($data['status'])) $model->where('status',$data['status']);
  180. $list = $this->limit($model,'',$data);
  181. $list = $this->fillData($list);
  182. return [true,$list];
  183. }
  184. public function fillData($data){
  185. if(empty($data['data'])) return $data;
  186. $waste_map = $waste_map_2 = [];
  187. $waste = FinishedOrderScrapp::where('del_time',0)
  188. ->whereIn('finished_order_id',array_column($data['data'],'id'))
  189. ->select('finished_order_id','num','scrapp_id')
  190. ->get()->toArray();
  191. if(! empty($waste)){
  192. foreach ($waste as $value){
  193. $waste_map[$value['finished_order_id']][] = [
  194. 'num' => $value['num'],
  195. 'scrapp_id' => $value['scrapp_id']
  196. ];
  197. if(isset($waste_map_2[$value['finished_order_id']])){
  198. $waste_map_2[$value['finished_order_id']] += $value['num'];
  199. }else{
  200. $waste_map_2[$value['finished_order_id']] = $value['num'];
  201. }
  202. }
  203. }
  204. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  205. ->leftJoin('employee as b','b.id','a.employee_id')
  206. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  207. ->select('b.emp_name','a.team_id')
  208. ->get()
  209. ->toArray();
  210. $team_map = [];
  211. if(! empty($team)){
  212. foreach ($team as $value){
  213. if(isset($team_map[$value['team_id']])){
  214. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  215. }else{
  216. $team_map[$value['team_id']] = $value['emp_name'];
  217. }
  218. }
  219. }
  220. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  221. ->pluck('title','id')
  222. ->toArray();
  223. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  224. ->pluck('title','id')
  225. ->toArray();
  226. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  227. ->pluck('title','id')
  228. ->toArray();
  229. date_default_timezone_set("PRC");
  230. foreach ($data['data'] as $key => $value){
  231. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  232. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  233. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  234. $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
  235. $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
  236. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  237. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  238. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  239. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  240. $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
  241. $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
  242. $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
  243. }
  244. $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
  245. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  246. $data['waste_quantity'] = array_sum(array_column($data['data'], 'waste_quantity'));
  247. $data['not_finished_num'] = array_sum(array_column($data['data'], 'not_finished_num'));
  248. return $data;
  249. }
  250. //反写写完工数量
  251. public function writeFinishedQuantity($sale_orders_product_id){
  252. if(empty($sale_orders_product_id)) return;
  253. $result = DispatchSub::where('del_time',0)
  254. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  255. ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
  256. ->groupby('sale_orders_product_id')
  257. ->pluck('finished_num','sale_orders_product_id')
  258. ->toArray();
  259. if(empty($result)) return;
  260. foreach ($result as $key => $value){
  261. SaleOrdersProduct::where('id',$key)->update([
  262. 'finished_num' => $value
  263. ]);
  264. }
  265. }
  266. }