ConstructionService.php 41 KB

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