ConstructionService.php 41 KB

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