ConstructionService.php 38 KB

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