ConstructionService.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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\EmployeeDepartPermission;
  14. use App\Model\Oa;
  15. use App\Model\OaSub;
  16. use App\Model\OaSubEmployee;
  17. use App\Model\OaSubReportEmployee;
  18. use App\Model\OaSubRule;
  19. use App\Model\Product;
  20. use App\Model\ReturnExchangeOrder;
  21. use App\Model\ReturnExchangeOrderProductInfo;
  22. use App\Model\SalesOrder;
  23. use App\Model\SalesOrderInfo;
  24. use App\Model\SalesOrderProductInfo;
  25. use App\Model\ScheduleInfo;
  26. use App\Model\SeeRange;
  27. use App\Model\Storehouse;
  28. use Illuminate\Support\Facades\DB;
  29. //use Barryvdh\DomPDF\Facade as PDF;
  30. use Barryvdh\DomPDF\PDF;
  31. /**
  32. * 施工订单
  33. */
  34. class ConstructionService extends Service
  35. {
  36. /**
  37. * 施工订单编辑
  38. * @param $data
  39. * @param $user
  40. * @return array
  41. */
  42. public function constructionEdit($data,$user){
  43. list($status,$msg) = $this->constructionRule($data, $user, false);
  44. if(!$status) return [$status,$msg];
  45. $params = $this->getDataFile($data);
  46. (new OperationLogService())->setOperationList($params,$user,2);
  47. try {
  48. DB::beginTransaction();
  49. $model = Construction::where('id', $data['id'])->first();
  50. $model->model_type = $data['model_type'];
  51. $model->order_number = $data['order_number'];
  52. $model->title = $data['title'] ?? '';
  53. $model->customer_id = $data['customer_id'] ?? 0;
  54. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  55. $model->install_method = $data['install_method'] ?? 0;
  56. $model->install_position = $data['install_position'] ?? 0;
  57. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  58. $model->construction_fee = $data['construction_fee'] ?? 0;
  59. $model->service_price = $data['service_price'] ?? 0;
  60. // $model->construction_time = $data['construction_time'] ?? 0;
  61. $model->handover_time = $data['handover_time'] ?? 0;
  62. $model->urgency = $data['urgency'] ?? 0;
  63. $model->mark = $data['mark'] ?? '';
  64. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  65. $model->address2 = $data['address2'] ?? '';
  66. $model->introduction = $data['introduction'] ?? '';
  67. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  68. $model->start_time = $data['start_time'] ?? 0;
  69. $model->end_time = $data['end_time'] ?? 0;
  70. $model->schedule_id = $data['schedule_id'] ?? 0;
  71. $model->day_stamp = $data['day_stamp'] ?? 0;
  72. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  73. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  74. $model->product_introduction = $data['product_introduction'] ?? "";
  75. $model->vin_no = $data['vin_no'] ?? "";
  76. $model->save();
  77. $time = time();
  78. ConstructionInfo::where('del_time',0)
  79. ->where('construction_id',$data['id'])
  80. ->where('type',ConstructionInfo::type_two)
  81. ->update(['del_time' => $time]);
  82. ConstructionProductInfo::where('del_time',0)
  83. ->where('construction_id',$data['id'])
  84. ->update(['del_time' => $time]);
  85. $old = ConstructionFile::where('del_time',0)
  86. ->where('construction_id',$data['id'])
  87. ->select('file')
  88. ->get()->toArray();
  89. $old = array_column($old,'file');
  90. ConstructionFile::where('del_time',0)
  91. ->where('construction_id',$data['id'])
  92. ->update(['del_time' => $time]);
  93. if(! empty($data['construction_contact'])){
  94. $insert = [];
  95. foreach ($data['construction_contact'] as $value){
  96. $insert[] = [
  97. 'construction_id' => $model->id,
  98. 'contact_type' => $value['id'],
  99. 'contact_info' => $value['info'],
  100. 'type' => ConstructionInfo::type_one,
  101. 'crt_time' => $time,
  102. ];
  103. }
  104. ConstructionInfo::insert($insert);
  105. }
  106. if(! empty($data['employee_one'])){
  107. $insert = [];
  108. foreach ($data['employee_one'] as $value){
  109. $insert[] = [
  110. 'construction_id' => $model->id,
  111. 'employee_id' => $value,
  112. 'type' => ConstructionInfo::type_two,
  113. 'crt_time' => $time,
  114. ];
  115. }
  116. ConstructionInfo::insert($insert);
  117. }
  118. if(! empty($data['product'])){
  119. $insert = [];
  120. foreach ($data['product'] as $value){
  121. $insert[] = [
  122. 'construction_id' => $model->id,
  123. 'product_id' => $value['product_id'],
  124. 'number' => $value['number'],
  125. 'cost' => $value['cost'] ?? 0,
  126. 'retail_price' => $value['retail_price'] ?? 0,
  127. 'mark' => $value['mark'] ?? '',
  128. 'crt_time' => $time,
  129. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  130. 'basic_type_id' => $value['basic_type_id'],
  131. 'price' => $value['price'],
  132. 'final_amount' => $value['final_amount'] ?? 0,
  133. ];
  134. }
  135. ConstructionProductInfo::insert($insert);
  136. //锁定库存
  137. if($data['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  138. }
  139. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  140. $new = [];
  141. if(! empty($data['file'])){
  142. $insert = [];
  143. foreach ($data['file'] as $value){
  144. $insert[] = [
  145. 'order_number' => $data['order_number'],
  146. 'construction_id' => $data['id'],
  147. 'file' => $value['url'],
  148. 'name' => $value['name'],
  149. 'mark' => $value['mark'] ?? "",
  150. 'crt_time' => $time,
  151. ];
  152. if(in_array($value['url'], $old)) {
  153. foreach ($old as $o_k => $o_v){
  154. if($o_v == $value['url']) unset($old[$o_k]);
  155. }
  156. }else{
  157. $new[] = $value['url'];
  158. }
  159. }
  160. ConstructionFile::insert($insert);
  161. }
  162. DB::commit();
  163. }catch (\Exception $exception){
  164. DB::rollBack();
  165. return [false,$exception->getMessage()];
  166. }
  167. if(! empty($data['check'])) {
  168. list($status,$msg) = (new CheckService())->checkAll([
  169. "id" => $model->id,
  170. "order_number" => $data['order_number'],
  171. "opt_case" => CheckService::five,
  172. "menu_id" => $data['menu_id']
  173. ],$user);
  174. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  175. }
  176. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  177. }
  178. /**
  179. * 施工订单新增
  180. * @param $data
  181. * @param $user
  182. * @return array
  183. */
  184. public function constructionAdd($data,$user){
  185. list($status,$msg) = $this->constructionRule($data,$user);
  186. if(!$status) return [$status,$msg];
  187. try {
  188. DB::beginTransaction();
  189. $model = new Construction();
  190. $model->model_type = $data['model_type'];
  191. $model->order_number = $data['order_number'];
  192. $model->title = $data['title'] ?? '';
  193. $model->customer_id = $data['customer_id'] ?? 0;
  194. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  195. $model->install_method = $data['install_method'] ?? 0;
  196. $model->install_position = $data['install_position'] ?? 0;
  197. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  198. $model->construction_fee = $data['construction_fee'] ?? 0;
  199. $model->service_price = $data['service_price'] ?? 0;
  200. // $model->construction_time = $data['construction_time'] ?? 0;
  201. $model->handover_time = $data['handover_time'] ?? 0;
  202. $model->urgency = $data['urgency'] ?? 0;
  203. $model->mark = $data['mark'] ?? '';
  204. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  205. $model->address2 = $data['address2'] ?? '';
  206. $model->introduction = $data['introduction'] ?? '';
  207. $model->crt_id = $user['id'];
  208. $model->depart_id = $data['depart_id'] ?? 0;
  209. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  210. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  211. $model->start_time = $data['start_time'] ?? 0;
  212. $model->end_time = $data['end_time'] ?? 0;
  213. $model->schedule_id = $data['schedule_id'] ?? 0;
  214. $model->day_stamp = $data['day_stamp'] ?? 0;
  215. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  216. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  217. $model->product_introduction = $data['product_introduction'] ?? "";
  218. $model->vin_no = $data['vin_no'] ?? "";
  219. $model->save();
  220. $time = time();
  221. if(! empty($data['construction_contact'])){
  222. $insert = [];
  223. foreach ($data['construction_contact'] as $value){
  224. $insert[] = [
  225. 'construction_id' => $model->id,
  226. 'contact_type' => $value['id'],
  227. 'contact_info' => $value['info'],
  228. 'type' => ConstructionInfo::type_one,
  229. 'crt_time' => $time,
  230. ];
  231. }
  232. ConstructionInfo::insert($insert);
  233. }
  234. if(! empty($data['employee_one'])){
  235. $insert = [];
  236. foreach ($data['employee_one'] as $value){
  237. $insert[] = [
  238. 'construction_id' => $model->id,
  239. 'employee_id' => $value,
  240. 'type' => ConstructionInfo::type_two,
  241. 'crt_time' => $time,
  242. ];
  243. }
  244. ConstructionInfo::insert($insert);
  245. }
  246. if(! empty($data['product'])){
  247. $insert = [];
  248. foreach ($data['product'] as $value){
  249. $insert[] = [
  250. 'construction_id' => $model->id,
  251. 'product_id' => $value['product_id'],
  252. 'number' => $value['number'],
  253. 'cost' => $value['cost'] ?? 0,
  254. 'retail_price' => $value['retail_price'] ?? 0,
  255. 'mark' => $value['mark'] ?? '',
  256. 'crt_time' => $time,
  257. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  258. 'basic_type_id' => $value['basic_type_id'],
  259. 'price' => $value['price'],
  260. 'final_amount' => $value['final_amount'] ?? 0,
  261. ];
  262. }
  263. ConstructionProductInfo::insert($insert);
  264. //锁定库存
  265. if($data['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  266. }
  267. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  268. $new = [];
  269. if(! empty($data['file'])){
  270. $insert = [];
  271. foreach ($data['file'] as $value){
  272. $insert[] = [
  273. 'order_number' => $data['order_number'],
  274. 'construction_id' => $model->id,
  275. 'file' => $value['url'],
  276. 'name' => $value['name'],
  277. 'mark' => $value['mark'] ?? "",
  278. 'crt_time' => $time,
  279. ];
  280. if(! empty($value['url'])) $new[] = $value['url'];
  281. }
  282. ConstructionFile::insert($insert);
  283. }
  284. DB::commit();
  285. }catch (\Exception $exception){
  286. DB::rollBack();
  287. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  288. return [false, '施工单编号已存在,请重新获取!'];
  289. }
  290. return [false,$exception->getMessage()];
  291. }
  292. (new OperationLogService())->setOperationList($data,$user);
  293. if(! empty($data['check'])) {
  294. list($status,$msg) = (new CheckService())->checkAll([
  295. "id" => $model->id,
  296. "order_number" => $data['order_number'],
  297. "opt_case" => CheckService::five,
  298. "menu_id" => $data['menu_id']
  299. ],$user);
  300. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  301. }
  302. return [true, ['file' => ['new' => $new]]];
  303. }
  304. /**
  305. * 施工订单删除
  306. * @param $data
  307. * @return array
  308. */
  309. public function constructionDel($data,$user){
  310. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  311. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  312. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  313. $construction = $construction->toArray();
  314. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  315. $product_save = $this->getSaveDetail($data['id']);
  316. try {
  317. DB::beginTransaction();
  318. Construction::where('id',$data['id'])->update([
  319. 'del_time'=> time()
  320. ]);
  321. ConstructionInfo::where('del_time',0)
  322. ->where('construction_id',$data['id'])
  323. ->update(['del_time' => time()]);
  324. $old = ConstructionFile::where('del_time',0)
  325. ->where('construction_id',$data['id'])
  326. ->select('file')
  327. ->get()->toArray();
  328. $old = array_column($old,'file');
  329. ConstructionFile::where('del_time',0)
  330. ->where('construction_id',$data['id'])
  331. ->update(['del_time' => time()]);
  332. ConstructionProductInfo::where('del_time',0)
  333. ->where('construction_id',$data['id'])
  334. ->update(['del_time' => time()]);
  335. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  336. //锁定库存释放
  337. if($construction['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,[],$product_save);
  338. //排班修改
  339. // $schedule = ScheduleInfo::where('del_time',0)
  340. // ->where('schedule_id',$construction['schedule_id'])
  341. // ->where('start_time',$construction['day_start_stamp'])
  342. // ->where('end_time',$construction['day_end_stamp'])
  343. // ->where('is_use','>', ScheduleInfo::not_use)
  344. // ->first();
  345. // if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  346. DB::commit();
  347. }catch (\Exception $exception){
  348. DB::rollBack();
  349. return [false,$exception->getMessage()];
  350. }
  351. return [true, ['file' => ['old' => $old]]];
  352. }
  353. /**
  354. * 施工订单详情
  355. * @param $data
  356. * @return array
  357. */
  358. public function detail($data){
  359. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  360. if(! empty($data['id'])){
  361. $construction = Construction::where('del_time',0)
  362. ->where('id',$data['id'])
  363. ->first();
  364. }else{
  365. $construction = Construction::where('del_time',0)
  366. ->where('order_number',$data['order_number'])
  367. ->first();
  368. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  369. }
  370. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  371. $construction = $construction->toArray();
  372. $construction['state_title'] = Construction::$name[$construction['state']] ?? '';
  373. $address_map = config('address');
  374. $address_str = [];
  375. if(! empty($construction['address1'])) {
  376. $tmp = json_decode($construction['address1'],true);
  377. $construction['address1'] = $tmp;
  378. $this->findLabelsByValue($address_map,$tmp,$address_str);
  379. $tmp = implode(' ',$address_str);
  380. $tmp .= ' ' . $construction['address2'];
  381. $address = $tmp;
  382. }else{
  383. $address = $construction['address2'];
  384. }
  385. $construction['address'] = $address;
  386. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  387. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  388. $construction['construction_period'] = $start_time . '——' . $end_time;
  389. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  390. $construction['sales_order_number'] = $sales;
  391. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  392. $construction['customer_title'] = $customer_title ?? "";
  393. $info = CustomerInfo::from('customer_info as a')
  394. ->leftJoin('basic_type as b','b.id','a.contact_type')
  395. ->where('a.del_time',0)
  396. ->where('a.customer_id',$construction['customer_id'])
  397. ->where('a.contact_type','>',0)
  398. ->select('b.title','a.contact_info')
  399. ->get()->toArray();
  400. $construction['customer_array'] = $info;
  401. $info = CustomerInfo::from('customer_info as a')
  402. ->leftJoin('basic_type as b','b.id','a.contact_type')
  403. ->where('a.del_time',0)
  404. ->where('a.customer_id',$construction['customer_id'])
  405. ->where('a.contact_type','>',0)
  406. ->select('b.title','a.contact_info')
  407. ->get()->toArray();
  408. $construction['customer_array'] = $info;
  409. $construction['storehouse_title'] = $construction['storehouse_id'] > 0 ? Storehouse::where('id',$construction['storehouse_id'])->value('title') : "";
  410. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  411. $construction['customer_contact_title'] = $emp_title;
  412. $construction['employee_two'] = $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  413. $array = [
  414. $construction['install_method'],
  415. $construction['install_position'],
  416. $construction['urgency'],
  417. ];
  418. $basic_map = BasicType::whereIn('id',$array)
  419. ->pluck('title','id')
  420. ->toArray();
  421. $construction = [$construction];
  422. foreach ($construction as $key => $value){
  423. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  424. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  425. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  426. }
  427. $construction = $construction[0];
  428. $construction['file'] = [];
  429. $file = ConstructionFile::where('del_time',0)
  430. ->where('construction_id',$construction['id'])
  431. ->select('id','construction_id','file','name','mark')
  432. ->get()->toArray();
  433. $fileUploadService = new FileUploadService();
  434. foreach ($file as $value){
  435. $construction['file'][] = [
  436. 'url' => $value['file'],
  437. 'name' => $value['name'],
  438. 'mark' => $value['mark'],
  439. 'show_url' => $fileUploadService->getFileShow($value['file']),
  440. ];
  441. }
  442. $construction_info = ConstructionInfo::where('del_time',0)
  443. ->where('construction_id',$construction['id'])
  444. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  445. ->get()->toArray();
  446. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  447. ->pluck('emp_name','id')
  448. ->toArray();
  449. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  450. ->pluck('title','id')
  451. ->toArray();
  452. foreach ($construction_info as $value){
  453. if($value['type'] == ConstructionInfo::type_one){
  454. $tmp = [
  455. 'id' => $value['contact_type'],
  456. 'title' => $basic_map2[$value['contact_type']] ?? '',
  457. 'info' => $value['contact_info']
  458. ];
  459. $construction['construction_contact'][] = $tmp;
  460. }elseif ($value['type'] == ConstructionInfo::type_two){
  461. $tmp = [
  462. 'id' => $value['employee_id'],
  463. 'name' => $emp_map[$value['employee_id']] ?? '',
  464. ];
  465. $construction['employee_one'][] = $tmp;
  466. }elseif ($value['type'] == ConstructionInfo::type_three){
  467. $tmp = [
  468. 'id' => $value['employee_id'],
  469. 'name' => $emp_map[$value['employee_id']] ?? '',
  470. ];
  471. $construction['employee_two'][] = $tmp;
  472. }
  473. }
  474. $p_info = ConstructionProductInfo::where('del_time',0)
  475. ->where('construction_id',$construction['id'])
  476. ->get()->toArray();
  477. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  478. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  479. foreach ($p_info as $value){
  480. $tmp = $map[$value['product_id']] ?? [];
  481. $value['title'] = $tmp['title'] ?? "";
  482. $value['code'] = $tmp['code'] ?? "";
  483. $value['size'] = $tmp['size'] ?? "";
  484. $value['unit'] = $tmp['unit'] ?? "";
  485. $value['bar_code'] = $tmp['bar_code'] ?? "";
  486. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  487. $construction['product'][] = $value;
  488. }
  489. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  490. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  491. //可见范围
  492. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  493. $construction['depart'] = $return[0] ?? [];
  494. $construction['employee'] = $return[1] ?? [];
  495. return [true, $construction];
  496. }
  497. /**
  498. * 施工订单列表
  499. * @param $data
  500. * @param $user
  501. * @return array
  502. */
  503. public function constructionList($data,$user){
  504. $model = Construction::Clear($user,$data);
  505. $model = $model->where('del_time',0)
  506. ->orderby('id', 'desc');
  507. if(empty($data['select_field'])){
  508. $model->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','vin_no');
  509. }else{
  510. $model->select('sales_order_id');
  511. }
  512. if(isset($data['state'])) {
  513. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  514. if($data['state'] == Construction::STATE_DIFF_TWO){
  515. $model->where('state', Construction::STATE_TWO);
  516. $model->where('start_time', '>', $today_stamp);
  517. }elseif($data['state'] == Construction::STATE_TWO){
  518. $model->where('state', Construction::STATE_TWO);
  519. $model->where('start_time', '<=', $today_stamp);
  520. }else{
  521. $model->where('state', $data['state']);
  522. }
  523. }
  524. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  525. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  526. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  527. if(! empty($data['time_type'])) {
  528. if($data['time_type'] == 1) {
  529. $start = strtotime('today');
  530. $end = strtotime('tomorrow') - 1;
  531. }elseif ($data['time_type'] == 2){
  532. $start = strtotime('this week',strtotime('today'));
  533. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  534. }
  535. if(! empty($start) && ! empty($end)) {
  536. $model->where('crt_time','>=',$start);
  537. $model->where('crt_time','<=',$end);
  538. }
  539. }
  540. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  541. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  542. $model->where('start_time','>=',$return[0]);
  543. $model->where('end_time','<=',$return[1]);
  544. }
  545. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  546. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  547. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  548. }
  549. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  550. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  551. $model->whereBetween('crt_time',[$return[0],$return[1]]);
  552. }
  553. if(! empty($data['sale_order'])){
  554. $model2 = SalesOrder::Clear($user,$data);
  555. $sale = $model2->where('del_time',0)
  556. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  557. ->select('id')
  558. ->get()->toArray();
  559. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  560. }
  561. if(! empty($data['install_method'])) {
  562. $id = (new BasicTypeService())->basicTypeSearchId($data['install_method']);
  563. $model->whereIn('install_method',$id);
  564. }
  565. if(! empty($data['install_position'])) {
  566. $id = (new BasicTypeService())->basicTypeSearchId($data['install_position']);
  567. $model->whereIn('install_position',$id);
  568. }
  569. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  570. if(! empty($data['select_field'])) return $model->get()->toArray();
  571. $list = $this->limit($model,'',$data);
  572. $list = $this->fillData($list);
  573. return [true, $list];
  574. }
  575. /**
  576. * 参数规则
  577. * @param $data
  578. * @param $user
  579. * @param $is_add
  580. * @return array
  581. */
  582. public function constructionRule(&$data, $user, $is_add = true){
  583. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  584. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  585. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  586. if($data['model_type'] == Construction::Model_type_one && empty($data['storehouse_id'])) return [false,'请选择仓库'];
  587. if(empty($data['menu_id'])) return [false, '菜单信息不能为空'];
  588. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  589. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  590. if(empty($sale)) return [false,'合同不存在或已被删除'];
  591. $sale = $sale->toArray();
  592. if($sale['state'] < SalesOrder::State_two) return [false,'合同未通过确认,不允许新建施工单'];
  593. if($sale['state'] >= SalesOrder::State_seven) return [false,'请确认合同状态,新建施工单失败'];
  594. if($sale['top_depart_id'] == $user['head']['id']){
  595. //总社的订单 需要派单 才能建施工
  596. if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
  597. }
  598. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  599. if(! $status) return [false, $msg];
  600. if(empty($data['product'])) return [false,'请选择产品'];
  601. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  602. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  603. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  604. if(! empty($data['construction_fee'])){
  605. $res = $this->checkNumber($data['construction_fee']);
  606. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  607. }
  608. if(! empty($data['service_price'])){
  609. $res = $this->checkNumber($data['service_price']);
  610. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  611. }
  612. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  613. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  614. if($data['model_type'] == Construction::Model_type_one){
  615. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  616. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  617. }else{
  618. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  619. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  620. }
  621. //所属部门 以及 顶级部门
  622. if(empty($data['depart_id'])) {
  623. $data['depart_id'] = $this->getDepart($user);
  624. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  625. }
  626. $product_submit = $product_id = [];
  627. foreach ($data['product'] as $value){
  628. if(empty($value['number'])) return [false,'产品数量不能为空'];
  629. $res = $this->checkNumber($value['number']);
  630. if(! $res) return [false,'请输入正确的产品数量'];
  631. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  632. if(isset($product_submit[$key])){
  633. $product_submit[$key] += $value['number'];
  634. }else{
  635. $product_submit[$key] = $value['number'];
  636. }
  637. $product_id[] = $value['product_id'];
  638. }
  639. //剩余能施工
  640. $id = $data['id'] ?? 0;
  641. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  642. //比较
  643. foreach ($product_submit as $pro => $number){
  644. $tmp = explode(',',$pro);
  645. $p = $tmp[0];
  646. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  647. $s_number = $s_product[$p];
  648. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据(包含已退货产品)'];
  649. }
  650. $id = $data['id'] ?? 0;
  651. $product_save = $this->getSaveDetail($id);
  652. //是否校验库存
  653. ProductInventoryService::is_check($user,$data);
  654. if($data['model_type'] == Construction::Model_type_one){
  655. //到店安装 才校验库存
  656. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  657. if(! $status) return [false, $msg];
  658. }
  659. if($is_add){
  660. $bool = Construction::where('del_time',0)
  661. ->where('order_number',$data['order_number'])
  662. ->exists();
  663. if($bool) return [false,'工单编号已存在,请重新获取'];
  664. }else{
  665. if(empty($data['id'])) return [false,'ID不能为空'];
  666. $construction = Construction::where('del_time',0)
  667. ->where('id',$data['id'])->first();
  668. if(empty($construction)) return [false, '施工单单据不存在或已被删除'];
  669. if($construction->state > Construction::STATE_ZERO) return [false,'请确认施工单单据状态,修改失败'];
  670. }
  671. return [true, [$product_submit, $product_save]];
  672. }
  673. /**
  674. * 数据拼接
  675. * @param $data
  676. * @return array
  677. */
  678. public function fillData($data){
  679. if(empty($data['data'])) return $data;
  680. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  681. $basic_map = BasicType::whereIn('id',$array)
  682. ->pluck('title','id')
  683. ->toArray();
  684. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  685. ->pluck('emp_name','id')
  686. ->toArray();
  687. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  688. ->pluck('title','id')
  689. ->toArray();
  690. $sales_map = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  691. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  692. ->pluck('title','id')
  693. ->toArray();
  694. //分派的总社或分社
  695. $dispatch = $this->getDispatchData($data['data']);
  696. //施工产品
  697. $product_map = $this->getProduct($data['data']);
  698. //订单状态数据组织
  699. $state_array = $this->getStateMake($data['data']);
  700. //负责人
  701. $construction_info_map = $this->getFzEmployee(array_column($data['data'],'id'));
  702. $address_map = config('address');
  703. foreach ($data['data'] as $key => $value){
  704. $address_str = [];
  705. $product_tmp = $product_map[$value['id']] ?? [];
  706. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  707. if(! empty($value['address1'])) {
  708. $tmp = json_decode($value['address1'],true);
  709. $this->findLabelsByValue($address_map,$tmp,$address_str);
  710. $tmp = implode(' ',$address_str);
  711. $tmp .= ' ' . $value['address2'];
  712. $address = $tmp;
  713. }else{
  714. $address = $value['address2'];
  715. }
  716. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  717. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  718. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  719. $data['data'][$key]['address'] = $address;
  720. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  721. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  722. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  723. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  724. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  725. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  726. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  727. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  728. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  729. $tmp_sales = $sales_map[$value['sales_order_id']] ?? "";
  730. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  731. $data['data'][$key]['sales_order_number'] = $tmp_sales;
  732. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d",$value['handover_time']) : "";
  733. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  734. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  735. $data['data'][$key]['fz'] = $construction_info_map[$value['id']] ?? "";
  736. // $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  737. // $str = "";
  738. // $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  739. // $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  740. // if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  741. // $data['data'][$key]['pq_period'] = $str;
  742. }
  743. return $data;
  744. }
  745. public function getFzEmployee($construction_id = []){
  746. if(empty($construction_id)) return [];
  747. $construction_info = ConstructionInfo::where('del_time',0)
  748. ->where('type',ConstructionInfo::type_three)
  749. ->whereIn('construction_id',$construction_id)
  750. ->select('construction_id','employee_id')
  751. ->get()->toArray();
  752. $emp_map2 = Employee::whereIn('id',array_unique(array_column($construction_info,'employee_id')))
  753. ->pluck('emp_name','id')
  754. ->toArray();
  755. $construction_info_map = [];
  756. foreach ($construction_info as $value){
  757. $emp = $emp_map2[$value['employee_id']] ?? "";
  758. if(! $emp) continue;
  759. if(isset($construction_info_map[$value['construction_id']])){
  760. $construction_info_map[$value['construction_id']] .= ',' . $emp;
  761. }else{
  762. $construction_info_map[$value['construction_id']] = $emp;
  763. }
  764. }
  765. return $construction_info_map;
  766. }
  767. public function getStateMake($data){
  768. if(empty($data)) return [];
  769. $order_no = [];
  770. foreach ($data as $value){
  771. if(! in_array($value['state'], [Construction::State_minus_one,Construction::STATE_ONE])) continue;
  772. $order_no[] = $value['order_number'];
  773. }
  774. return (new OaService())->getOaTeamDetailList($order_no);
  775. }
  776. public function makeState($value, $state_array){
  777. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  778. if(! empty($state_array[$value['order_number']])){
  779. $return = $state_array[$value['order_number']];
  780. if($value['state'] == Construction::State_minus_one){
  781. $state = "驳回:" . $return;
  782. }else{
  783. $state = "待" . $return . "审核";
  784. }
  785. }elseif($value['state'] == Construction::STATE_ZERO){
  786. $state = "待" . $value['crt_name'] . "提交";
  787. }elseif($value['state'] == Construction::STATE_TWO && $value['start_time'] > $today_stamp){
  788. $state = Construction::$name[Construction::STATE_DIFF_TWO] ?? '';
  789. }else{
  790. $state = Construction::$name[$value['state']] ?? '';
  791. }
  792. return $state;
  793. }
  794. public function getProduct($data){
  795. $search_id = array_column($data,'id');
  796. if(empty($search_id)) return [];
  797. $product = ConstructionProductInfo::where('del_time',0)
  798. ->whereIn('construction_id',$search_id)
  799. ->select('product_id','construction_id')
  800. ->get()->toArray();
  801. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  802. $return = [];
  803. foreach ($product as $value){
  804. $product_tmp = $product_map[$value['product_id']] ?? "";
  805. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  806. }
  807. return $return;
  808. }
  809. public function getDispatchData($data){
  810. $search_id = [];
  811. foreach ($data as $value){
  812. $search_id[] = $value['sales_order_id'];
  813. }
  814. if(empty($search_id)) return [];
  815. $see = SeeRange::where('del_time',0)
  816. ->whereIn('data_id',$search_id)
  817. ->where('data_type',SeeRange::type_seven)
  818. ->where('type',SeeRange::data_three)
  819. ->select('data_id','param_id')
  820. ->get()->toArray();
  821. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  822. ->pluck('title','id')
  823. ->toArray();
  824. $see_array = [];
  825. foreach ($see as $value){
  826. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  827. }
  828. return $see_array;
  829. }
  830. /**
  831. * 获取施工单号
  832. * @param $data
  833. * @return array
  834. */
  835. public function constructionGet($data){
  836. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  837. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  838. $prefix = Construction::$prefix[$data['model_type']];
  839. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  840. if(! $order_number) return [false,'工单编号生成失败!'];
  841. return [true,['order_number' => $order_number]];
  842. }
  843. /**
  844. * 获取保存详情
  845. * @param $id
  846. * @return array
  847. */
  848. public function getSaveDetail($id){
  849. $product_save = [];
  850. if(empty($id)) return $product_save;
  851. $sub = ConstructionProductInfo::where('construction_id',$id)
  852. ->where('del_time',0)
  853. ->get()->toArray();
  854. foreach ($sub as $value){
  855. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  856. if(isset($product_save[$key])){
  857. $product_save[$key] += $value['number'];
  858. }else{
  859. $product_save[$key] = $value['number'];
  860. }
  861. }
  862. return $product_save;
  863. }
  864. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  865. $construction = Construction::where('del_time',0)
  866. ->where('sales_order_id',$sales_order_id)
  867. ->select('id')->get()->toArray();
  868. $construction_id = array_column($construction,'id');
  869. $product_save = [];
  870. $sub = ConstructionProductInfo::where('del_time',0)
  871. ->whereIn('construction_id',$construction_id)
  872. ->when(! empty($id), function ($query) use ($id) {
  873. return $query->where('construction_id', '<>',$id);
  874. })
  875. ->get()->toArray();
  876. foreach ($sub as $value){
  877. if(isset($product_save[$value['product_id']])){
  878. $product_save[$value['product_id']] += $value['number'];
  879. }else{
  880. $product_save[$value['product_id']] = $value['number'];
  881. }
  882. }
  883. $product_save2 = [];
  884. $sub1 = ReturnExchangeOrder::where('del_time',0)
  885. ->where('type',ReturnExchangeOrder::Order_type)
  886. ->where('model_type',ReturnExchangeOrder::Model_type_one)
  887. ->where('data_id',$sales_order_id)
  888. ->get()->toArray();
  889. $sub1_array = ReturnExchangeOrderProductInfo::where('del_time',0)
  890. ->whereIn('return_exchange_id',array_column($sub1,'id'))
  891. ->get()->toArray();
  892. foreach ($sub1_array as $value){
  893. if(isset($product_save2[$value['product_id']])){
  894. $product_save2[$value['product_id']] += $value['number'];
  895. }else{
  896. $product_save2[$value['product_id']] = $value['number'];
  897. }
  898. }
  899. $sales_order_product = [];
  900. $sales_product = SalesOrderProductInfo::where('del_time',0)
  901. ->where('sales_order_id',$sales_order_id)
  902. ->get()->toArray();
  903. foreach ($sales_product as $value){
  904. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  905. $product_save_tmp2 = $product_save2[$value['product_id']] ?? 0;
  906. if(isset($sales_order_product[$value['product_id']])){
  907. $sales_order_product[$value['product_id']] += $value['number'];
  908. }else{
  909. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp - $product_save_tmp2;
  910. }
  911. }
  912. return $sales_order_product;
  913. }
  914. public function deliveryNoteEdit($data,$user){
  915. $id = $user['id'];
  916. if(isset($data['id'])) {
  917. $model = DeliveryNote::where('id',$data['id'])->first();
  918. if($model->img3) return [false,'客户已签字,无法编辑!'];
  919. if(DeliveryNote::where('del_time',0)->where('construction_order_number',$data['construction_order_number'])->where('id','<>',$data['id'])->first()) return [false,'施工单客户确认单已存在!'];
  920. }
  921. else {
  922. $model = new DeliveryNote();
  923. $model->crt_id = $id;
  924. }
  925. try {
  926. $model->upd_id = $id;
  927. $model->construction_order_number = $data['construction_order_number'] ?? '';
  928. $model->start_time = $data['start_time'] ?? '';
  929. $model->end_time = $data['end_time'] ?? '';
  930. $model->vin_no = $data['vin_no'] ?? '';
  931. $model->system = $data['system'] ?? '';
  932. $model->mile = $data['mile'] ?? '';
  933. $model->is_wait = $data['is_wait'] ?? '';
  934. $model->customer_name = $data['customer_name'] ?? '';
  935. $model->customer_mobile = $data['customer_mobile'] ?? '';
  936. $model->sale_man = $data['sale_man'] ?? '';
  937. $model->install_man = $data['install_man'] ?? '';
  938. $model->is_brash = $data['is_brash'] ?? '';
  939. $model->is_chong = $data['is_chong'] ?? '';
  940. $model->service_mark = $data['service_mark'] ?? '';
  941. $model->mark = $data['mark'] ?? '';
  942. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  943. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  944. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  945. $model->other_project_mark = $data['other_project_mark'] ?? '';
  946. $model->img = $data['img'] ?? '';
  947. $model->img1 = $data['img1'] ?? '';
  948. $model->img2 = $data['img2'] ?? '';
  949. $model->img3 = $data['img3'] ?? '';
  950. $model->save();
  951. return [true,'保存成功!'];
  952. }catch (\Throwable $e){
  953. return [true,'保存成功!'];
  954. }
  955. }
  956. public function deliveryNoteList($data)
  957. {
  958. $list = DeliveryNote::where('del_time',0);
  959. $list = $this->limit($list,'*',$data);
  960. return [true,$list];
  961. }
  962. public function deliveryNoteDetail($data){
  963. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  964. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  965. if(empty($model)) return [false,'数据不存在!'];
  966. $employee_key_list = Employee::pluck('emp_name','id')->toArray();
  967. $detail = $model->toArray();
  968. $detail['install_man_title'] = $employee_key_list[$detail['install_man']] ?? "";
  969. $detail['sale_man_title'] = $employee_key_list[$detail['sale_man']] ?? "";
  970. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])->where('del_time',0)->select('file as url','name','mark')->get()->toArray();
  971. $detail['file'] = $img_list;
  972. return [true,$detail];
  973. }
  974. public function deliveryNoteDel($data,$user){
  975. DeliveryNote::where('id',$data['id'])->update(
  976. [
  977. 'del_time' => time(),
  978. 'upd_id' => $user['id'],
  979. ]
  980. );
  981. return [true,'删除成功!'];
  982. }
  983. public function constructionPdf($data, $user){
  984. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  985. $construction = Construction::where('del_time',0)
  986. ->where('id',$data['id'])
  987. ->first();
  988. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  989. $construction = $construction->toArray();
  990. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  991. $construction['customer_title'] = $customer_title ?? "";
  992. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  993. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  994. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  995. $construction_info = ConstructionInfo::where('del_time',0)
  996. ->where('construction_id',$construction['id'])
  997. ->where('type',CustomerInfo::type_two)
  998. ->select('id','employee_id')
  999. ->get()->toArray();
  1000. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  1001. ->select('emp_name','id','mobile')
  1002. ->get()->toArray();
  1003. $emp_map = [];
  1004. foreach ($emp_list as $value){
  1005. $emp_map[$value['id']] = [
  1006. 'name' => $value['emp_name'],
  1007. 'mobile' => $value['mobile'],
  1008. ];
  1009. }
  1010. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1011. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1012. ->select('a.employee_id', 'b.title')
  1013. ->whereIn('employee_id',array_column($emp_list,'id'))
  1014. ->get()->toArray();
  1015. $emp_message_map = [];
  1016. foreach ($emp_message as $value){
  1017. if(isset($emp_message_map[$value['employee_id']])){
  1018. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1019. }else{
  1020. $emp_message_map[$value['employee_id']] = $value['title'];
  1021. }
  1022. }
  1023. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  1024. $construction['crt_name'] = $crt_name;
  1025. $xt_name = "";
  1026. foreach ($construction_info as $value){
  1027. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  1028. }
  1029. $xt_name = rtrim($xt_name, " | ");
  1030. $construction['xt_name'] = $xt_name;
  1031. $p_info = ConstructionProductInfo::where('del_time',0)
  1032. ->where('construction_id',$construction['id'])
  1033. ->get()->toArray();
  1034. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  1035. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  1036. ->pluck('title','id')->toArray();
  1037. $product = [];
  1038. $count = 0;
  1039. foreach ($p_info as $value){
  1040. $tmp = $map[$value['product_id']] ?? [];
  1041. $value['count'] = $count+1;
  1042. $value['title'] = $tmp['title'] ?? "";
  1043. $value['code'] = $tmp['code'] ?? "";
  1044. $value['size'] = $tmp['size'] ?? "";
  1045. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  1046. $product[] = $value;
  1047. }
  1048. //工单进展
  1049. $detail = $this->getOaJz($data,$user);
  1050. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  1051. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  1052. $file_name = time().rand(1000,9999);
  1053. $filename = '施工单_' . $file_name.'.' . 'pdf';
  1054. $pdf->save(storage_path('app/public/export/' . $filename));
  1055. return [true, ['file' => $filename]];
  1056. }
  1057. public function getOaJz($data,$user)
  1058. {
  1059. $oa = Oa::where('menu_id', 34)
  1060. ->where('del_time',0)
  1061. ->where('channel',$user['depart_top'][0]['depart_id'])
  1062. ->first();
  1063. if(empty($oa)) return [];
  1064. $oa = $oa->toArray();
  1065. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  1066. $oa_sub_ids = [];
  1067. foreach ($list as $v) {
  1068. $oa_sub_ids[] = $v['id'];
  1069. }
  1070. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  1071. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  1072. ->select('emp_name','id','mobile')
  1073. ->get()->toArray();
  1074. $emp_map = [];
  1075. foreach ($emp_list as $value){
  1076. $emp_map[$value['id']] = [
  1077. 'name' => $value['emp_name'],
  1078. 'mobile' => $value['mobile'],
  1079. ];
  1080. }
  1081. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1082. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1083. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  1084. ->select('a.employee_id', 'b.title')
  1085. ->get()->toArray();
  1086. $emp_message_map = [];
  1087. foreach ($emp_message as $value){
  1088. if(isset($emp_message_map[$value['employee_id']])){
  1089. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1090. }else{
  1091. $emp_message_map[$value['employee_id']] = $value['title'];
  1092. }
  1093. }
  1094. $oaEmployeeKey = [];
  1095. foreach ($oaEmployee as $v) {
  1096. $str = "";
  1097. $tmp = $emp_map[$v['employee_id']] ?? [];
  1098. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  1099. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  1100. $oaEmployeeKey[$v['oa_sub_id']][] = [
  1101. 'id' => $v['employee_id'],
  1102. 'emp_name' => $str,
  1103. ];
  1104. }
  1105. $return = [];
  1106. foreach ($list as $v) {
  1107. $return[$v['sort']][] = [
  1108. 'emp_id' => $oaEmployeeKey[$v['id']],
  1109. 'index' => $v['h5_key'],
  1110. ];
  1111. }
  1112. $detail = [];
  1113. foreach ($return as $v) {
  1114. $children = "";
  1115. foreach ($v as $vv) {
  1116. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1117. }
  1118. $detail[] = rtrim($children,',');
  1119. }
  1120. return $detail;
  1121. }
  1122. public function constructionOperation($data, $user){
  1123. if(empty($data['order_number'])) return [false, '施工单号不能为空'];
  1124. if(empty($data['type'])) return [false, '操作类型不能为空'];
  1125. $order = Construction::where('del_time',0)
  1126. ->where('order_number',$data['order_number'])
  1127. ->first();
  1128. if(empty($order)) return [false,'施工单不存在或已被删除,操作失败'];
  1129. $order = $order->toArray();
  1130. if($data['type'] == 1){
  1131. if($order['state'] != Construction::STATE_TWO) return [false, '请确认施工单单据状态,操作完结失败'];
  1132. $state = Construction::STATE_THREE;
  1133. }else{
  1134. if($order['state'] != Construction::STATE_THREE) return [false, '请确认施工单单据状态,操作待确认失败'];
  1135. $state = Construction::STATE_FOUR;
  1136. }
  1137. Construction::where('order_number',$data['order_number'])
  1138. ->update(['state' => $state]);
  1139. return [true, ''];
  1140. }
  1141. }