ConstructionService.php 39 KB

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