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