ConstructionService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionFile;
  6. use App\Model\ConstructionInfo;
  7. use App\Model\ConstructionProductInfo;
  8. use App\Model\Customer;
  9. use App\Model\Depart;
  10. use App\Model\Employee;
  11. use App\Model\SalesOrder;
  12. use App\Model\SalesOrderInfo;
  13. use App\Model\SalesOrderProductInfo;
  14. use App\Model\ScheduleInfo;
  15. use App\Model\SeeRange;
  16. use App\Model\Storehouse;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 施工订单
  20. */
  21. class ConstructionService extends Service
  22. {
  23. /**
  24. * 施工订单编辑
  25. * @param $data
  26. * @param $user
  27. * @return array
  28. */
  29. public function constructionEdit($data,$user){
  30. list($status,$msg) = $this->constructionRule($data, $user, false);
  31. if(!$status) return [$status,$msg];
  32. $params = $this->getDataFile($data);
  33. (new OperationLogService())->setOperationList($params,$user,2);
  34. try {
  35. DB::beginTransaction();
  36. $model = Construction::where('id', $data['id'])->first();
  37. $model->model_type = $data['model_type'];
  38. $model->order_number = $data['order_number'];
  39. $model->title = $data['title'] ?? '';
  40. $model->customer_id = $data['customer_id'] ?? 0;
  41. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  42. $model->install_method = $data['install_method'] ?? 0;
  43. $model->install_position = $data['install_position'] ?? 0;
  44. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  45. $model->construction_fee = $data['construction_fee'] ?? 0;
  46. $model->service_price = $data['service_price'] ?? 0;
  47. $model->construction_time = $data['construction_time'] ?? 0;
  48. $model->handover_time = $data['handover_time'] ?? 0;
  49. $model->urgency = $data['urgency'] ?? 0;
  50. $model->mark = $data['mark'] ?? '';
  51. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  52. $model->address2 = $data['address2'] ?? '';
  53. $model->introduction = $data['introduction'] ?? '';
  54. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  55. $model->start_time = $data['start_time'] ?? 0;
  56. $model->end_time = $data['end_time'] ?? 0;
  57. $model->schedule_id = $data['schedule_id'] ?? 0;
  58. $model->day_stamp = $data['day_stamp'] ?? 0;
  59. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  60. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  61. $model->save();
  62. $time = time();
  63. ConstructionInfo::where('del_time',0)
  64. ->where('construction_id',$data['id'])
  65. ->update(['del_time' => $time]);
  66. ConstructionProductInfo::where('del_time',0)
  67. ->where('construction_id',$data['id'])
  68. ->update(['del_time' => $time]);
  69. ConstructionFile::where('del_time',0)
  70. ->where('construction_id',$data['id'])
  71. ->update(['del_time' => $time]);
  72. if(! empty($data['construction_contact'])){
  73. $insert = [];
  74. foreach ($data['construction_contact'] as $value){
  75. $insert[] = [
  76. 'construction_id' => $model->id,
  77. 'contact_type' => $value['id'],
  78. 'contact_info' => $value['info'],
  79. 'type' => ConstructionInfo::type_one,
  80. 'crt_time' => $time,
  81. ];
  82. }
  83. ConstructionInfo::insert($insert);
  84. }
  85. if(! empty($data['employee_one'])){
  86. $insert = [];
  87. foreach ($data['employee_one'] as $value){
  88. $insert[] = [
  89. 'construction_id' => $model->id,
  90. 'employee_id' => $value,
  91. 'type' => ConstructionInfo::type_two,
  92. 'crt_time' => $time,
  93. ];
  94. }
  95. ConstructionInfo::insert($insert);
  96. }
  97. if(! empty($data['product'])){
  98. $insert = [];
  99. foreach ($data['product'] as $value){
  100. $insert[] = [
  101. 'construction_id' => $model->id,
  102. 'product_id' => $value['product_id'],
  103. 'number' => $value['number'],
  104. 'cost' => $value['cost'] ?? 0,
  105. 'retail_price' => $value['retail_price'] ?? 0,
  106. 'mark' => $value['mark'] ?? '',
  107. 'crt_time' => $time,
  108. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  109. 'basic_type_id' => $value['basic_type_id'],
  110. 'price' => $value['price'],
  111. 'final_amount' => $value['final_amount'] ?? 0,
  112. ];
  113. }
  114. ConstructionProductInfo::insert($insert);
  115. //锁定库存
  116. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  117. }
  118. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  119. if(! empty($data['file'])){
  120. $insert = [];
  121. foreach ($data['file'] as $value){
  122. $insert[] = [
  123. 'construction_id' => $data['id'],
  124. 'file' => $value['url'],
  125. 'name' => $value['name'],
  126. 'mark' => $value['mark'] ?? "",
  127. 'crt_time' => $time,
  128. ];
  129. }
  130. ConstructionFile::insert($insert);
  131. }
  132. DB::commit();
  133. }catch (\Exception $exception){
  134. DB::rollBack();
  135. return [false,$exception->getMessage()];
  136. }
  137. return [true,''];
  138. }
  139. /**
  140. * 施工订单新增
  141. * @param $data
  142. * @param $user
  143. * @return array
  144. */
  145. public function constructionAdd($data,$user){
  146. list($status,$msg) = $this->constructionRule($data,$user);
  147. if(!$status) return [$status,$msg];
  148. try {
  149. DB::beginTransaction();
  150. $model = new Construction();
  151. $model->model_type = $data['model_type'];
  152. $model->order_number = $data['order_number'];
  153. $model->title = $data['title'] ?? '';
  154. $model->customer_id = $data['customer_id'] ?? 0;
  155. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  156. $model->install_method = $data['install_method'] ?? 0;
  157. $model->install_position = $data['install_position'] ?? 0;
  158. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  159. $model->construction_fee = $data['construction_fee'] ?? 0;
  160. $model->service_price = $data['service_price'] ?? 0;
  161. $model->construction_time = $data['construction_time'] ?? 0;
  162. $model->handover_time = $data['handover_time'] ?? 0;
  163. $model->urgency = $data['urgency'] ?? 0;
  164. $model->mark = $data['mark'] ?? '';
  165. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  166. $model->address2 = $data['address2'] ?? '';
  167. $model->introduction = $data['introduction'] ?? '';
  168. $model->crt_id = $user['id'];
  169. $model->depart_id = $data['depart_id'] ?? 0;
  170. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  171. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  172. $model->start_time = $data['start_time'] ?? 0;
  173. $model->end_time = $data['end_time'] ?? 0;
  174. $model->schedule_id = $data['schedule_id'] ?? 0;
  175. $model->day_stamp = $data['day_stamp'] ?? 0;
  176. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  177. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  178. $model->save();
  179. $time = time();
  180. if(! empty($data['construction_contact'])){
  181. $insert = [];
  182. foreach ($data['construction_contact'] as $value){
  183. $insert[] = [
  184. 'construction_id' => $model->id,
  185. 'contact_type' => $value['id'],
  186. 'contact_info' => $value['info'],
  187. 'type' => ConstructionInfo::type_one,
  188. 'crt_time' => $time,
  189. ];
  190. }
  191. ConstructionInfo::insert($insert);
  192. }
  193. if(! empty($data['employee_one'])){
  194. $insert = [];
  195. foreach ($data['employee_one'] as $value){
  196. $insert[] = [
  197. 'construction_id' => $model->id,
  198. 'employee_id' => $value,
  199. 'type' => ConstructionInfo::type_two,
  200. 'crt_time' => $time,
  201. ];
  202. }
  203. ConstructionInfo::insert($insert);
  204. }
  205. if(! empty($data['product'])){
  206. $insert = [];
  207. foreach ($data['product'] as $value){
  208. $insert[] = [
  209. 'construction_id' => $model->id,
  210. 'product_id' => $value['product_id'],
  211. 'number' => $value['number'],
  212. 'cost' => $value['cost'] ?? 0,
  213. 'retail_price' => $value['retail_price'] ?? 0,
  214. 'mark' => $value['mark'] ?? '',
  215. 'crt_time' => $time,
  216. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  217. 'basic_type_id' => $value['basic_type_id'],
  218. 'price' => $value['price'],
  219. 'final_amount' => $value['final_amount'] ?? 0,
  220. ];
  221. }
  222. ConstructionProductInfo::insert($insert);
  223. //锁定库存
  224. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  225. }
  226. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  227. if(! empty($data['file'])){
  228. $insert = [];
  229. foreach ($data['file'] as $value){
  230. $insert[] = [
  231. 'construction_id' => $data['id'],
  232. 'file' => $value['url'],
  233. 'name' => $value['name'],
  234. 'mark' => $value['mark'] ?? "",
  235. 'crt_time' => $time,
  236. ];
  237. }
  238. ConstructionFile::insert($insert);
  239. }
  240. DB::commit();
  241. }catch (\Exception $exception){
  242. DB::rollBack();
  243. return [false,$exception->getMessage()];
  244. }
  245. (new OperationLogService())->setOperationList($data,$user);
  246. return [true,''];
  247. }
  248. /**
  249. * 施工订单删除
  250. * @param $data
  251. * @return array
  252. */
  253. public function constructionDel($data,$user){
  254. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  255. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  256. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  257. $construction = $construction->toArray();
  258. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  259. $product_save = $this->getSaveDetail($data['id']);
  260. try {
  261. DB::beginTransaction();
  262. Construction::where('id',$data['id'])->update([
  263. 'del_time'=> time()
  264. ]);
  265. ConstructionInfo::where('del_time',0)
  266. ->where('construction_id',$data['id'])
  267. ->update(['del_time' => time()]);
  268. ConstructionFile::where('del_time',0)
  269. ->where('construction_id',$data['id'])
  270. ->update(['del_time' => $time]);
  271. ConstructionProductInfo::where('del_time',0)
  272. ->where('construction_id',$data['id'])
  273. ->update(['del_time' => time()]);
  274. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  275. //锁定库存释放
  276. ProductInventoryService::changeLockNumber($user,[],$product_save);
  277. //排班修改
  278. $schedule = ScheduleInfo::where('del_time',0)
  279. ->where('schedule_id',$construction['schedule_id'])
  280. ->where('start_time',$construction['day_start_stamp'])
  281. ->where('end_time',$construction['day_end_stamp'])
  282. ->where('is_use','>', ScheduleInfo::not_use)
  283. ->first();
  284. if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  285. DB::commit();
  286. }catch (\Exception $exception){
  287. DB::rollBack();
  288. return [false,$exception->getMessage()];
  289. }
  290. return [true,''];
  291. }
  292. /**
  293. * 施工订单详情
  294. * @param $data
  295. * @return array
  296. */
  297. public function detail($data){
  298. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  299. if(! empty($data['id'])){
  300. $construction = Construction::where('del_time',0)
  301. ->where('id',$data['id'])
  302. ->first();
  303. }else{
  304. $construction = Construction::where('del_time',0)
  305. ->where('order_number',$data['order_number'])
  306. ->first();
  307. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  308. }
  309. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  310. $construction = $construction->toArray();
  311. $address = '';
  312. if(! empty($construction['address1'])) {
  313. $tmp = json_decode($construction['address1'],true);
  314. $construction['address1'] = $tmp;
  315. $tmp = implode(' ',$tmp);
  316. $tmp .= ' ' . $construction['address2'];
  317. $address = $tmp;
  318. }
  319. $construction['address'] = $address;
  320. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  321. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  322. $construction['construction_period'] = $start_time . '——' . $end_time;
  323. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  324. $construction['sales_order_number'] = $sales;
  325. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  326. $construction['customer_title'] = $customer_title ?? "";
  327. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  328. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  329. $construction['customer_contact_title'] = $emp_title;
  330. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  331. $array = [
  332. $construction['install_method'],
  333. $construction['install_position'],
  334. $construction['urgency'],
  335. ];
  336. $basic_map = BasicType::whereIn('id',$array)
  337. ->pluck('title','id')
  338. ->toArray();
  339. $construction = [$construction];
  340. foreach ($construction as $key => $value){
  341. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  342. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  343. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  344. }
  345. $construction = $construction[0];
  346. $construction['file'] = [];
  347. $file = ConstructionFile::where('del_time',0)
  348. ->where('construction_id',$construction['id'])
  349. ->select('id','construction_id','file','name','mark')
  350. ->get()->toArray();
  351. foreach ($file as $value){
  352. $construction['file'][] = [
  353. 'url' => $value['file'],
  354. 'name' => $value['name'],
  355. 'mark' => $value['mark'],
  356. ];
  357. }
  358. $construction_info = ConstructionInfo::where('del_time',0)
  359. ->where('construction_id',$construction['id'])
  360. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  361. ->get()->toArray();
  362. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  363. ->pluck('emp_name','id')
  364. ->toArray();
  365. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  366. ->pluck('title','id')
  367. ->toArray();
  368. foreach ($construction_info as $value){
  369. if($value['type'] == ConstructionInfo::type_one){
  370. $tmp = [
  371. 'id' => $value['contact_type'],
  372. 'title' => $basic_map2[$value['contact_type']] ?? '',
  373. 'info' => $value['contact_info']
  374. ];
  375. $construction['construction_contact'][] = $tmp;
  376. }elseif ($value['type'] == ConstructionInfo::type_two){
  377. $tmp = [
  378. 'id' => $value['employee_id'],
  379. 'name' => $emp_map[$value['employee_id']] ?? '',
  380. ];
  381. $construction['employee_one'][] = $tmp;
  382. }
  383. }
  384. $p_info = ConstructionProductInfo::where('del_time',0)
  385. ->where('construction_id',$construction['id'])
  386. ->get()->toArray();
  387. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  388. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  389. foreach ($p_info as $value){
  390. $tmp = $map[$value['product_id']] ?? [];
  391. $value['title'] = $tmp['title'] ?? "";
  392. $value['code'] = $tmp['code'] ?? "";
  393. $value['size'] = $tmp['size'] ?? "";
  394. $value['unit'] = $tmp['unit'] ?? "";
  395. $value['bar_code'] = $tmp['bar_code'] ?? "";
  396. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  397. $construction['product'][] = $value;
  398. }
  399. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  400. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  401. //可见范围
  402. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  403. $construction['depart'] = $return[0] ?? [];
  404. $construction['employee'] = $return[1] ?? [];
  405. return [true, $construction];
  406. }
  407. /**
  408. * 施工订单列表
  409. * @param $data
  410. * @param $user
  411. * @return array
  412. */
  413. public function constructionList($data,$user){
  414. $model = Construction::Clear($user,$data);
  415. $model = $model->where('del_time',0)
  416. ->select('title','id','model_type','order_number','customer_id','customer_contact_id','install_method','install_position','sales_order_id','construction_fee','construction_time','handover_time','urgency','crt_id','crt_time','mark','state','address1','address2','introduction','service_price','storehouse_id','start_time','end_time')
  417. ->orderby('id', 'desc');
  418. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  419. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  420. if(! empty($data['time_type'])) {
  421. if($data['time_type'] == 1) {
  422. $start = strtotime('today');
  423. $end = strtotime('tomorrow') - 1;
  424. }elseif ($data['time_type'] == 2){
  425. $start = strtotime('this week',strtotime('today'));
  426. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  427. }
  428. if(! empty($start) && ! empty($end)) {
  429. $model->where('crt_time','>=',$start);
  430. $model->where('crt_time','<=',$end);
  431. }
  432. }
  433. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  434. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  435. $model->where('start_time','>=',$return[0]);
  436. $model->where('end_time','<=',$return[1]);
  437. }
  438. if(! empty($data['sale_order'])){
  439. $model2 = SalesOrder::Clear($user,$data);
  440. $sale = $model2->where('del_time',0)
  441. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  442. ->select('id')
  443. ->get()->toArray();
  444. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  445. }
  446. if(! empty($data['install_method'])) $model->where('install_method',$data['install_method']);
  447. if(! empty($data['install_position'])) $model->where('install_position',$data['install_position']);
  448. $list = $this->limit($model,'',$data);
  449. $list = $this->fillData($list);
  450. return [true, $list];
  451. }
  452. /**
  453. * 参数规则
  454. * @param $data
  455. * @param $user
  456. * @param $is_add
  457. * @return array
  458. */
  459. public function constructionRule(&$data, $user, $is_add = true){
  460. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  461. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  462. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  463. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  464. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  465. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  466. if(empty($sale)) return [false,'合同不存在或已被删除'];
  467. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  468. $sale = $sale->toArray();
  469. if(empty($data['product'])) return [false,'请选择产品'];
  470. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  471. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  472. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  473. if(! empty($data['construction_fee'])){
  474. $res = $this->checkNumber($data['construction_fee']);
  475. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  476. }
  477. if(! empty($data['service_price'])){
  478. $res = $this->checkNumber($data['service_price']);
  479. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  480. }
  481. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  482. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  483. if($data['model_type'] == Construction::Model_type_one){
  484. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  485. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  486. }else{
  487. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  488. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  489. }
  490. //校验排班 todo
  491. // if(empty($data['schedule_id']) ||empty($data['day_stamp']) || empty($data['day_start_stamp']) || empty($data['day_end_stamp'])) return [false,'排班时间信息不能为空'];
  492. //所属部门 以及 顶级部门
  493. if(empty($data['depart_id'])) {
  494. $data['depart_id'] = $this->getDepart($user);
  495. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  496. }
  497. $product_submit = $product_id = [];
  498. foreach ($data['product'] as $value){
  499. if(empty($value['number'])) return [false,'产品数量不能为空'];
  500. $res = $this->checkNumber($value['number']);
  501. if(! $res) return [false,'请输入正确的产品数量'];
  502. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  503. if(isset($product_submit[$key])){
  504. $product_submit[$key] += $value['number'];
  505. }else{
  506. $product_submit[$key] = $value['number'];
  507. }
  508. $product_id[] = $value['product_id'];
  509. }
  510. //剩余能施工
  511. $id = $data['id'] ?? 0;
  512. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  513. //比较
  514. foreach ($product_submit as $pro => $number){
  515. $tmp = explode(',',$pro);
  516. $p = $tmp[0];
  517. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  518. $s_number = $s_product[$p];
  519. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  520. }
  521. $id = $data['id'] ?? 0;
  522. $product_save = $this->getSaveDetail($id);
  523. //是否校验库存
  524. ProductInventoryService::is_check($user,$data);
  525. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  526. if(! $status) return [false, $msg];
  527. $start_time = date("Y-m-d H:i",$data['start_time']);
  528. $end_time = date("Y-m-d H:i",$data['end_time']);
  529. if($is_add){
  530. $bool = Construction::where('del_time',0)
  531. ->where('order_number',$data['order_number'])
  532. ->exists();
  533. if($bool) return [false,'工单编号已存在,请重新获取'];
  534. $bool = Construction::where('del_time',0)
  535. ->where('sales_order_id',$data['sales_order_id'])
  536. ->where('start_time', '<=', $data['end_time'])
  537. ->where('end_time', '>=', $data['start_time'])
  538. ->exists();
  539. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  540. // $schedule = ScheduleInfo::where('del_time',0)
  541. // ->where('day',$data['day_stamp'])
  542. // ->where('start_time',$data['day_start_stamp'])
  543. // ->where('end_time',$data['day_end_stamp'])
  544. // ->where('is_use',ScheduleInfo::not_use)
  545. // ->first();
  546. // if(! empty($schedule)) return [false,'该时间段排班已满或不存在!'];
  547. // $data['schedule_info_id'] = $schedule->id;
  548. // list($status,$msg) = $this->limitingSendRequestBackg(ScheduleInfo::limit_key . $schedule->id);
  549. // if(! $status) return [false,'操作频繁,请稍等!'];
  550. }else{
  551. if(empty($data['id'])) return [false,'ID不能为空'];
  552. $bool = Construction::where('del_time',0)
  553. ->where('id','<>',$data['id'])
  554. ->where('sales_order_id',$data['sales_order_id'])
  555. ->where('start_time', '<=', $data['end_time'])
  556. ->where('end_time', '>=', $data['start_time'])
  557. ->exists();
  558. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  559. // $construction = Construction::where('id',$data['id'])->first();
  560. // if(empty($construction)) return [false,'施工单不存在或已被删除'];
  561. // $construction = $construction->toArray();
  562. // if($construction['day_stamp'] != $data['day_stamp'] || $construction['day_start_stamp'] != $data['day_start_stamp'] && $construction['day_end_stamp'] != $data['day_end_stamp']) {
  563. // $schedule = ScheduleInfo::where('del_time',0)
  564. // ->where('day',$data['day_stamp'])
  565. // ->where('start_time',$data['day_start_stamp'])
  566. // ->where('end_time',$data['day_end_stamp'])
  567. // ->where('is_use',ScheduleInfo::not_use)
  568. // ->first();
  569. // if(! empty($schedule)) return [false,'该时间段排班已满或不存在!'];
  570. // $data['schedule_info_id'] = $schedule->id;
  571. // list($status,$msg) = $this->limitingSendRequestBackg(ScheduleInfo::limit_key . $schedule->id);
  572. // if(! $status) return [false,'操作频繁,请稍等!'];
  573. // }
  574. }
  575. return [true, [$product_submit, $product_save]];
  576. }
  577. /**
  578. * 数据拼接
  579. * @param $data
  580. * @return array
  581. */
  582. public function fillData($data){
  583. if(empty($data['data'])) return $data;
  584. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  585. $basic_map = BasicType::whereIn('id',$array)
  586. ->pluck('title','id')
  587. ->toArray();
  588. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  589. ->pluck('emp_name','id')
  590. ->toArray();
  591. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  592. ->pluck('title','id')
  593. ->toArray();
  594. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray();
  595. $sales_map = [];
  596. foreach ($sales as $value){
  597. $sales_map[$value['id']] = $value;
  598. }
  599. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  600. ->pluck('title','id')
  601. ->toArray();
  602. //分派的总社或分社
  603. $dispatch = $this->getDispatchData($data['data']);
  604. foreach ($data['data'] as $key => $value){
  605. $address = '';
  606. if(! empty($value['address1'])) {
  607. $tmp = json_decode($value['address1'],true);
  608. $tmp = implode(' ',$tmp);
  609. $tmp .= ' ' . $value['address2'];
  610. $address = $tmp;
  611. }
  612. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  613. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  614. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  615. $data['data'][$key]['address'] = $address;
  616. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  617. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  618. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  619. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  620. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  621. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  622. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  623. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  624. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  625. $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
  626. $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  627. $data['data'][$key]['sales_order_number'] = $tmp_sales['order_number'];
  628. $data['data'][$key]['handover_time'] = $tmp_sales_time;
  629. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  630. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  631. }
  632. return $data;
  633. }
  634. public function getDispatchData($data){
  635. $search_id = [];
  636. foreach ($data as $value){
  637. $search_id[] = $value['sales_order_id'];
  638. }
  639. if(empty($search_id)) return [];
  640. $see = SeeRange::where('del_time',0)
  641. ->whereIn('data_id',$search_id)
  642. ->where('data_type',SeeRange::type_seven)
  643. ->where('type',SeeRange::data_three)
  644. ->select('data_id','param_id')
  645. ->get()->toArray();
  646. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  647. ->pluck('title','id')
  648. ->toArray();
  649. $see_array = [];
  650. foreach ($see as $value){
  651. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  652. }
  653. return $see_array;
  654. }
  655. /**
  656. * 获取施工单号
  657. * @param $data
  658. * @return array
  659. */
  660. public function constructionGet($data){
  661. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  662. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  663. $prefix = Construction::$prefix[$data['model_type']];
  664. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  665. if(! $order_number) return [false,'工单编号生成失败!'];
  666. return [true,['order_number' => $order_number]];
  667. }
  668. /**
  669. * 获取保存详情
  670. * @param $id
  671. * @return array
  672. */
  673. public function getSaveDetail($id){
  674. $product_save = [];
  675. if(empty($id)) return $product_save;
  676. $sub = ConstructionProductInfo::where('construction_id',$id)
  677. ->where('del_time',0)
  678. ->get()->toArray();
  679. foreach ($sub as $value){
  680. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  681. if(isset($product_save[$key])){
  682. $product_save[$key] += $value['number'];
  683. }else{
  684. $product_save[$key] = $value['number'];
  685. }
  686. }
  687. return $product_save;
  688. }
  689. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  690. $construction = Construction::where('del_time',0)
  691. ->where('sales_order_id',$sales_order_id)
  692. ->select('id')->get()->toArray();
  693. $construction_id = array_column($construction,'id');
  694. $product_save = [];
  695. $sub = ConstructionProductInfo::where('del_time',0)
  696. ->whereIn('construction_id',$construction_id)
  697. ->when(! empty($id), function ($query) use ($id) {
  698. return $query->where('construction_id', '<>',$id);
  699. })
  700. ->get()->toArray();
  701. foreach ($sub as $value){
  702. if(isset($product_save[$value['product_id']])){
  703. $product_save[$value['product_id']] += $value['number'];
  704. }else{
  705. $product_save[$value['product_id']] = $value['number'];
  706. }
  707. }
  708. $sales_order_product = [];
  709. $sales_product = SalesOrderProductInfo::where('del_time',0)
  710. ->where('sales_order_id',$sales_order_id)
  711. ->get()->toArray();
  712. foreach ($sales_product as $value){
  713. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  714. if(isset($sales_order_product[$value['product_id']])){
  715. $sales_order_product[$value['product_id']] += $value['number'];
  716. }else{
  717. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  718. }
  719. }
  720. return $sales_order_product;
  721. }
  722. }