ConstructionService.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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\DeliveryNoteDetail;
  12. use App\Model\Depart;
  13. use App\Model\Employee;
  14. use App\Model\EmployeeDepartPermission;
  15. use App\Model\Oa;
  16. use App\Model\OaSub;
  17. use App\Model\OaSubEmployee;
  18. use App\Model\OaSubReportEmployee;
  19. use App\Model\OaSubRule;
  20. use App\Model\Product;
  21. use App\Model\ProductInventorySet;
  22. use App\Model\ReturnExchangeOrder;
  23. use App\Model\ReturnExchangeOrderProductInfo;
  24. use App\Model\SalesOrder;
  25. use App\Model\SalesOrderInfo;
  26. use App\Model\SalesOrderProductInfo;
  27. use App\Model\ScheduleInfo;
  28. use App\Model\SeeRange;
  29. use App\Model\Storehouse;
  30. use Carbon\Carbon;
  31. use Illuminate\Database\Eloquent\Builder;
  32. use Illuminate\Support\Facades\DB;
  33. //use Barryvdh\DomPDF\Facade as PDF;
  34. use Barryvdh\DomPDF\PDF;
  35. /**
  36. * 施工订单
  37. */
  38. class ConstructionService extends Service
  39. {
  40. /**
  41. * 施工订单编辑
  42. * @param $data
  43. * @param $user
  44. * @return array
  45. */
  46. public function constructionEdit($data,$user){
  47. list($status,$msg) = $this->constructionRule($data, $user, false);
  48. if(!$status) return [$status,$msg];
  49. $params = $this->getDataFile($data);
  50. (new OperationLogService())->setOperationList($params,$user,2);
  51. try {
  52. DB::beginTransaction();
  53. $model = Construction::where('id', $data['id'])->first();
  54. $model->model_type = $data['model_type'];
  55. $model->order_number = $data['order_number'];
  56. $model->title = $data['title'] ?? '';
  57. $model->customer_id = $data['customer_id'] ?? 0;
  58. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  59. $model->install_method = $data['install_method'] ?? 0;
  60. $model->install_position = $data['install_position'] ?? 0;
  61. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  62. $model->construction_fee = $data['construction_fee'] ?? 0;
  63. $model->service_price = $data['service_price'] ?? 0;
  64. // $model->construction_time = $data['construction_time'] ?? 0;
  65. $model->handover_time = $data['handover_time'] ?? 0;
  66. $model->urgency = $data['urgency'] ?? 0;
  67. $model->mark = $data['mark'] ?? '';
  68. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  69. $model->address2 = $data['address2'] ?? '';
  70. $model->introduction = $data['introduction'] ?? '';
  71. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  72. $model->start_time = $data['start_time'] ?? 0;
  73. $model->end_time = $data['end_time'] ?? 0;
  74. $model->schedule_id = $data['schedule_id'] ?? 0;
  75. $model->day_stamp = $data['day_stamp'] ?? 0;
  76. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  77. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  78. $model->product_introduction = $data['product_introduction'] ?? "";
  79. $model->vin_no = $data['vin_no'] ?? "";
  80. $model->save();
  81. $time = time();
  82. ConstructionInfo::where('del_time',0)
  83. ->where('construction_id',$data['id'])
  84. ->where('type',ConstructionInfo::type_two)
  85. ->update(['del_time' => $time]);
  86. ConstructionProductInfo::where('del_time',0)
  87. ->where('construction_id',$data['id'])
  88. ->update(['del_time' => $time]);
  89. $old = ConstructionFile::where('del_time',0)
  90. ->where('construction_id',$data['id'])
  91. ->select('file')
  92. ->get()->toArray();
  93. $old = array_column($old,'file');
  94. ConstructionFile::where('del_time',0)
  95. ->where('construction_id',$data['id'])
  96. ->update(['del_time' => $time]);
  97. if(! empty($data['construction_contact'])){
  98. $insert = [];
  99. foreach ($data['construction_contact'] as $value){
  100. $insert[] = [
  101. 'construction_id' => $model->id,
  102. 'contact_type' => $value['id'],
  103. 'contact_info' => $value['info'],
  104. 'type' => ConstructionInfo::type_one,
  105. 'crt_time' => $time,
  106. ];
  107. }
  108. ConstructionInfo::insert($insert);
  109. }
  110. if(! empty($data['employee_one'])){
  111. $insert = [];
  112. foreach ($data['employee_one'] as $value){
  113. $insert[] = [
  114. 'construction_id' => $model->id,
  115. 'employee_id' => $value,
  116. 'type' => ConstructionInfo::type_two,
  117. 'crt_time' => $time,
  118. ];
  119. }
  120. ConstructionInfo::insert($insert);
  121. }
  122. if(! empty($data['product'])){
  123. $insert = [];
  124. foreach ($data['product'] as $value){
  125. $insert[] = [
  126. 'construction_id' => $model->id,
  127. 'product_id' => $value['product_id'],
  128. 'number' => $value['number'],
  129. 'cost' => $value['cost'] ?? 0,
  130. 'retail_price' => $value['retail_price'] ?? 0,
  131. 'mark' => $value['mark'] ?? '',
  132. 'crt_time' => $time,
  133. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  134. 'basic_type_id' => $value['basic_type_id'],
  135. 'price' => $value['price'],
  136. 'final_amount' => $value['final_amount'] ?? 0,
  137. ];
  138. }
  139. ConstructionProductInfo::insert($insert);
  140. //锁定库存
  141. // if($data['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  142. }
  143. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  144. $new = [];
  145. if(! empty($data['file'])){
  146. $insert = [];
  147. foreach ($data['file'] as $value){
  148. $insert[] = [
  149. 'order_number' => $data['order_number'],
  150. 'construction_id' => $data['id'],
  151. 'file' => $value['url'],
  152. 'name' => $value['name'],
  153. 'mark' => $value['mark'] ?? "",
  154. 'crt_time' => $time,
  155. ];
  156. if(in_array($value['url'], $old)) {
  157. foreach ($old as $o_k => $o_v){
  158. if($o_v == $value['url']) unset($old[$o_k]);
  159. }
  160. }else{
  161. $new[] = $value['url'];
  162. }
  163. }
  164. ConstructionFile::insert($insert);
  165. }
  166. DB::commit();
  167. }catch (\Exception $exception){
  168. DB::rollBack();
  169. return [false,$exception->getMessage()];
  170. }
  171. if(! empty($data['check'])) {
  172. list($status,$msg) = (new CheckService())->checkAll([
  173. "id" => $model->id,
  174. "order_number" => $data['order_number'],
  175. "opt_case" => CheckService::five,
  176. "menu_id" => $data['menu_id']
  177. ],$user);
  178. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  179. }
  180. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  181. }
  182. /**
  183. * 施工订单新增
  184. * @param $data
  185. * @param $user
  186. * @return array
  187. */
  188. public function constructionAdd($data,$user){
  189. list($status,$msg) = $this->constructionRule($data,$user);
  190. if(!$status) return [$status,$msg];
  191. try {
  192. DB::beginTransaction();
  193. $model = new Construction();
  194. $model->model_type = $data['model_type'];
  195. $model->order_number = $data['order_number'];
  196. $model->title = $data['title'] ?? '';
  197. $model->customer_id = $data['customer_id'] ?? 0;
  198. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  199. $model->install_method = $data['install_method'] ?? 0;
  200. $model->install_position = $data['install_position'] ?? 0;
  201. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  202. $model->construction_fee = $data['construction_fee'] ?? 0;
  203. $model->service_price = $data['service_price'] ?? 0;
  204. // $model->construction_time = $data['construction_time'] ?? 0;
  205. $model->handover_time = $data['handover_time'] ?? 0;
  206. $model->urgency = $data['urgency'] ?? 0;
  207. $model->mark = $data['mark'] ?? '';
  208. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  209. $model->address2 = $data['address2'] ?? '';
  210. $model->introduction = $data['introduction'] ?? '';
  211. $model->crt_id = $user['id'];
  212. $model->depart_id = $data['depart_id'] ?? 0;
  213. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  214. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  215. $model->start_time = $data['start_time'] ?? 0;
  216. $model->end_time = $data['end_time'] ?? 0;
  217. $model->schedule_id = $data['schedule_id'] ?? 0;
  218. $model->day_stamp = $data['day_stamp'] ?? 0;
  219. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  220. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  221. $model->product_introduction = $data['product_introduction'] ?? "";
  222. $model->vin_no = $data['vin_no'] ?? "";
  223. $model->save();
  224. $time = time();
  225. if(! empty($data['construction_contact'])){
  226. $insert = [];
  227. foreach ($data['construction_contact'] as $value){
  228. $insert[] = [
  229. 'construction_id' => $model->id,
  230. 'contact_type' => $value['id'],
  231. 'contact_info' => $value['info'],
  232. 'type' => ConstructionInfo::type_one,
  233. 'crt_time' => $time,
  234. ];
  235. }
  236. ConstructionInfo::insert($insert);
  237. }
  238. if(! empty($data['employee_one'])){
  239. $insert = [];
  240. foreach ($data['employee_one'] as $value){
  241. $insert[] = [
  242. 'construction_id' => $model->id,
  243. 'employee_id' => $value,
  244. 'type' => ConstructionInfo::type_two,
  245. 'crt_time' => $time,
  246. ];
  247. }
  248. ConstructionInfo::insert($insert);
  249. }
  250. if(! empty($data['product'])){
  251. $insert = [];
  252. foreach ($data['product'] as $value){
  253. $insert[] = [
  254. 'construction_id' => $model->id,
  255. 'product_id' => $value['product_id'],
  256. 'number' => $value['number'],
  257. 'cost' => $value['cost'] ?? 0,
  258. 'retail_price' => $value['retail_price'] ?? 0,
  259. 'mark' => $value['mark'] ?? '',
  260. 'crt_time' => $time,
  261. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  262. 'basic_type_id' => $value['basic_type_id'],
  263. 'price' => $value['price'],
  264. 'final_amount' => $value['final_amount'] ?? 0,
  265. ];
  266. }
  267. ConstructionProductInfo::insert($insert);
  268. //锁定库存
  269. // if($data['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  270. }
  271. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  272. $new = [];
  273. if(! empty($data['file'])){
  274. $insert = [];
  275. foreach ($data['file'] as $value){
  276. $insert[] = [
  277. 'order_number' => $data['order_number'],
  278. 'construction_id' => $model->id,
  279. 'file' => $value['url'],
  280. 'name' => $value['name'],
  281. 'mark' => $value['mark'] ?? "",
  282. 'crt_time' => $time,
  283. ];
  284. if(! empty($value['url'])) $new[] = $value['url'];
  285. }
  286. ConstructionFile::insert($insert);
  287. }
  288. //单据创建时是否校验库存
  289. (new CheckService())->orderInventoryInsert(['order_number' => $data['order_number'], 'is_check_stock' => $data['is_check_stock']]);
  290. DB::commit();
  291. }catch (\Exception $exception){
  292. DB::rollBack();
  293. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  294. return [false, '网络波动,请重新操作!'];
  295. }
  296. return [false,$exception->getMessage()];
  297. }
  298. (new OperationLogService())->setOperationList($data,$user);
  299. if(! empty($data['check'])) {
  300. list($status,$msg) = (new CheckService())->checkAll([
  301. "id" => $model->id,
  302. "order_number" => $data['order_number'],
  303. "opt_case" => CheckService::five,
  304. "menu_id" => $data['menu_id']
  305. ],$user);
  306. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  307. }
  308. return [true, ['file' => ['new' => $new]]];
  309. }
  310. /**
  311. * 施工订单删除
  312. * @param $data
  313. * @return array
  314. */
  315. public function constructionDel($data,$user){
  316. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  317. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  318. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  319. $construction = $construction->toArray();
  320. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  321. $product_save = $this->getSaveDetail($data['id']);
  322. try {
  323. DB::beginTransaction();
  324. Construction::where('id',$data['id'])->update([
  325. 'del_time'=> time()
  326. ]);
  327. ConstructionInfo::where('del_time',0)
  328. ->where('construction_id',$data['id'])
  329. ->update(['del_time' => time()]);
  330. $old = ConstructionFile::where('del_time',0)
  331. ->where('construction_id',$data['id'])
  332. ->select('file')
  333. ->get()->toArray();
  334. $old = array_column($old,'file');
  335. ConstructionFile::where('del_time',0)
  336. ->where('construction_id',$data['id'])
  337. ->update(['del_time' => time()]);
  338. ConstructionProductInfo::where('del_time',0)
  339. ->where('construction_id',$data['id'])
  340. ->update(['del_time' => time()]);
  341. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  342. //锁定库存释放
  343. // if($construction['model_type'] == Construction::Model_type_one) ProductInventoryService::changeLockNumber($user,[],$product_save);
  344. DB::commit();
  345. }catch (\Exception $exception){
  346. DB::rollBack();
  347. return [false,$exception->getMessage()];
  348. }
  349. return [true, ['file' => ['old' => $old]]];
  350. }
  351. /**
  352. * 施工订单详情
  353. * @param $data
  354. * @return array
  355. */
  356. public function detail($data,$user){
  357. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  358. if(! empty($data['id'])){
  359. $construction = Construction::where('del_time',0)
  360. ->where('id',$data['id'])
  361. ->first();
  362. }else{
  363. $construction = Construction::where('del_time',0)
  364. ->where('order_number',$data['order_number'])
  365. ->first();
  366. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  367. }
  368. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  369. $construction = $construction->toArray();
  370. $construction['state_title'] = Construction::$name[$construction['state']] ?? '';
  371. $address_map = config('address');
  372. $address_str = [];
  373. if(! empty($construction['address1'])) {
  374. $tmp = json_decode($construction['address1'],true);
  375. $construction['address1'] = $tmp;
  376. $this->findLabelsByValue($address_map,$tmp,$address_str);
  377. $tmp = implode(' ',$address_str);
  378. $tmp .= ' ' . $construction['address2'];
  379. $address = $tmp;
  380. }else{
  381. $address = $construction['address2'];
  382. }
  383. $construction['address'] = $address;
  384. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  385. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  386. $construction['construction_period'] = $start_time . '——' . $end_time;
  387. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  388. $construction['sales_order_number'] = $sales;
  389. $sales_info = SalesOrderInfo::where('del_time',0)
  390. ->where('sales_order_id',$construction['sales_order_id'])
  391. ->where('type', SalesOrderInfo::type_two)
  392. ->get()->toArray();
  393. $emp = Employee::whereIn('id',array_unique(array_column($sales_info,'data_id')))
  394. ->pluck('emp_name','id')
  395. ->toArray();
  396. $sale_man = $sale_man_tmp = [];
  397. foreach ($sales_info as $value){
  398. if(! in_array($value['data_id'], $sale_man_tmp)){
  399. $tt = $emp[$value['data_id']] ?? '';
  400. $tmp = [
  401. 'id' => $value['data_id'],
  402. 'name' => $tt?? '',
  403. ];
  404. $sale_man[] = $tmp;
  405. $sale_man_tmp[] = $value['data_id'];
  406. }
  407. }
  408. $construction['sale_man'] = $sale_man;
  409. unset($sale_man_tmp);
  410. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  411. $construction['customer_title'] = $customer_title ?? "";
  412. $info = CustomerInfo::from('customer_info as a')
  413. ->leftJoin('basic_type as b','b.id','a.contact_type')
  414. ->where('a.del_time',0)
  415. ->where('a.customer_id',$construction['customer_id'])
  416. ->where('a.contact_type','>',0)
  417. ->select('b.title','a.contact_info')
  418. ->get()->toArray();
  419. $construction['customer_array'] = $info;
  420. $info = CustomerInfo::from('customer_info as a')
  421. ->leftJoin('basic_type as b','b.id','a.contact_type')
  422. ->where('a.del_time',0)
  423. ->where('a.customer_id',$construction['customer_id'])
  424. ->where('a.contact_type','>',0)
  425. ->select('b.title','a.contact_info')
  426. ->get()->toArray();
  427. $construction['customer_array'] = $info;
  428. $construction['storehouse_title'] = $construction['storehouse_id'] > 0 ? Storehouse::where('id',$construction['storehouse_id'])->value('title') : "";
  429. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  430. $construction['customer_contact_title'] = $emp_title;
  431. $construction['employee_two'] = $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  432. $array = [
  433. $construction['install_method'],
  434. $construction['install_position'],
  435. $construction['urgency'],
  436. ];
  437. $basic_map = BasicType::whereIn('id',$array)
  438. ->pluck('title','id')
  439. ->toArray();
  440. $construction = [$construction];
  441. foreach ($construction as $key => $value){
  442. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  443. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  444. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  445. }
  446. $construction = $construction[0];
  447. $construction['file'] = [];
  448. $file = ConstructionFile::where('del_time',0)
  449. ->where('construction_id',$construction['id'])
  450. ->select('id','construction_id','file','name','mark')
  451. ->get()->toArray();
  452. $fileUploadService = new FileUploadService();
  453. foreach ($file as $value){
  454. $construction['file'][] = [
  455. 'url' => $value['file'],
  456. 'name' => $value['name'],
  457. 'mark' => $value['mark'],
  458. 'show_url' => $fileUploadService->getFileShow($value['file']),
  459. ];
  460. }
  461. $construction_info = ConstructionInfo::where('del_time',0)
  462. ->where('construction_id',$construction['id'])
  463. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  464. ->get()->toArray();
  465. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  466. ->pluck('emp_name','id')
  467. ->toArray();
  468. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  469. ->pluck('title','id')
  470. ->toArray();
  471. foreach ($construction_info as $value){
  472. if($value['type'] == ConstructionInfo::type_one){
  473. $tmp = [
  474. 'id' => $value['contact_type'],
  475. 'title' => $basic_map2[$value['contact_type']] ?? '',
  476. 'info' => $value['contact_info']
  477. ];
  478. $construction['construction_contact'][] = $tmp;
  479. }elseif ($value['type'] == ConstructionInfo::type_two){
  480. $tmp = [
  481. 'id' => $value['employee_id'],
  482. 'name' => $emp_map[$value['employee_id']] ?? '',
  483. ];
  484. $construction['employee_one'][] = $tmp;
  485. }elseif ($value['type'] == ConstructionInfo::type_three){
  486. $tmp = [
  487. 'id' => $value['employee_id'],
  488. 'name' => $emp_map[$value['employee_id']] ?? '',
  489. ];
  490. $construction['employee_two'][] = $tmp;
  491. }
  492. }
  493. $p_info = ConstructionProductInfo::where('del_time',0)
  494. ->where('construction_id',$construction['id'])
  495. ->get()->toArray();
  496. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  497. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  498. foreach ($p_info as $value){
  499. $tmp = $map[$value['product_id']] ?? [];
  500. $value['title'] = $tmp['title'] ?? "";
  501. $value['code'] = $tmp['code'] ?? "";
  502. $value['size'] = $tmp['size'] ?? "";
  503. $value['unit'] = $tmp['unit'] ?? "";
  504. $value['bar_code'] = $tmp['bar_code'] ?? "";
  505. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  506. $construction['product'][] = $value;
  507. }
  508. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  509. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  510. //可见范围
  511. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  512. $construction['depart'] = $return[0] ?? [];
  513. $construction['employee'] = $return[1] ?? [];
  514. return [true, $construction];
  515. }
  516. public function constructionCommon($data,$user,$field = []){
  517. if(empty($field)){
  518. $field = ['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'];
  519. }
  520. $model = Construction::Clear($user,$data);
  521. $model = $model->where('del_time',0)
  522. ->orderby('id', 'desc');
  523. if(empty($data['select_field'])){
  524. $model->select($field);
  525. }else{
  526. $model->select('sales_order_id');
  527. }
  528. if(isset($data['state'])) {
  529. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  530. if($data['state'] == Construction::STATE_DIFF_TWO){
  531. $model->where('state', Construction::STATE_TWO);
  532. $model->where('start_time', '>', $today_stamp);
  533. }elseif($data['state'] == Construction::STATE_TWO){
  534. $model->where('state', Construction::STATE_TWO);
  535. $model->where('start_time', '<=', $today_stamp);
  536. }else{
  537. $model->where('state', $data['state']);
  538. }
  539. }
  540. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  541. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  542. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  543. if(! empty($data['time_type'])) {
  544. if($data['time_type'] == 1) {
  545. $start = strtotime('today');
  546. $end = strtotime('tomorrow') - 1;
  547. }elseif ($data['time_type'] == 2){
  548. $start = strtotime('this week',strtotime('today'));
  549. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  550. }
  551. if(! empty($start) && ! empty($end)) {
  552. $model->where('crt_time','>=',$start);
  553. $model->where('crt_time','<=',$end);
  554. }
  555. }
  556. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  557. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  558. $model->where('start_time','>=',$return[0]);
  559. $model->where('end_time','<=',$return[1]);
  560. }
  561. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  562. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  563. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  564. }
  565. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  566. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  567. $model->whereBetween('crt_time',[$return[0],$return[1]]);
  568. }
  569. if(! empty($data['sale_order'])){
  570. $sale_id = (new SalesOrderService())->orderNumberSearch($user, $data, $data['sale_order']);
  571. $model->whereIn('sales_order_id',$sale_id);
  572. }
  573. if(! empty($data['install_method'])) {
  574. $id = (new BasicTypeService())->basicTypeSearchId($data['install_method']);
  575. $model->whereIn('install_method',$id);
  576. }
  577. if(! empty($data['install_position'])) {
  578. $id = (new BasicTypeService())->basicTypeSearchId($data['install_position']);
  579. $model->whereIn('install_position',$id);
  580. }
  581. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  582. if(! empty($data['select_field'])) return $model->get()->toArray();
  583. if(! empty($data['smart_search'])){
  584. $sale_id = (new SalesOrderService())->orderNumberSearch($user, $data, $data['smart_search']);
  585. $service_2 = new CustomerService();
  586. $customer_id = $service_2->titleSearch($user, $data, $data['smart_search']);
  587. $customer_id_2 = $service_2->contactSearch($data['smart_search']);
  588. $customer_id = array_unique(array_merge_recursive($customer_id,$customer_id_2));
  589. $model->where(function (Builder $query) use ($data, $customer_id,$sale_id) {
  590. $query->where('order_number', 'LIKE', '%'.$data['smart_search'].'%')
  591. ->orWhereIn('customer_id', $customer_id)
  592. ->orWhereIn('sales_order_id',$sale_id);
  593. });
  594. }
  595. if(! empty($data['crt_title'])){
  596. $id = (new EmployeeService())->searchByEmpName($data['crt_title']);
  597. $model->whereIn('crt_id', $id);
  598. }
  599. if(! empty($data['js'])){
  600. $js = $this->searchByJs($data['js']);
  601. $model->whereIn('id',$js);
  602. }
  603. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  604. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  605. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  606. }
  607. return $model;
  608. }
  609. public function searchByJs($js){
  610. $id = (new EmployeeService())->searchByEmpName($js);
  611. $construction_id = ConstructionInfo::where('del_time',0)
  612. ->where('type', ConstructionInfo::type_three)
  613. ->whereIn('employee_id', $id)
  614. ->select('construction_id')
  615. ->get()->toArray();
  616. return array_unique(array_column($construction_id,'construction_id'));
  617. }
  618. /**
  619. * 施工订单列表
  620. * @param $data
  621. * @param $user
  622. * @return array
  623. */
  624. public function constructionList($data,$user){
  625. $model = $this->constructionCommon($data, $user);
  626. if(is_array($model)) return $model;
  627. $list = $this->limit($model,'',$data);
  628. $list = $this->fillData($list);
  629. $count = $this->countData("crt_time", $data, $user);
  630. $list['today'] = $count[0] ?? 0;
  631. $list['yesterday'] = $count[1] ?? 0;
  632. return [true, $list];
  633. }
  634. public function countData($column = "", $data, $user){
  635. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  636. // 获取今天的开始和结束时间戳
  637. $todayStart = Carbon::today()->startOfDay()->timestamp;
  638. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  639. // 获取昨天的开始和结束时间戳
  640. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  641. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  642. $data['wx_' . $column] = [$todayStart, $todayEnd];
  643. $model = $this->constructionCommon($data, $user);
  644. // 查询今天的数据条数
  645. $todayCount = $model->count();
  646. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  647. $model = $this->constructionCommon($data, $user);
  648. // 查询昨天的数据条数
  649. $yesterdayCount = $model->count();
  650. return [$todayCount, $yesterdayCount];
  651. }
  652. /**
  653. * 参数规则
  654. * @param $data
  655. * @param $user
  656. * @param $is_add
  657. * @return array
  658. */
  659. public function constructionRule(&$data, $user, $is_add = true){
  660. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  661. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  662. if($data['model_type'] == Construction::Model_type_one && empty($data['storehouse_id'])) return [false,'请选择仓库'];
  663. if(empty($data['menu_id'])) return [false, '菜单信息不能为空'];
  664. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  665. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  666. if(empty($sale)) return [false,'合同不存在或已被删除'];
  667. $sale = $sale->toArray();
  668. if($sale['state'] < SalesOrder::State_two) return [false,'合同未通过确认,不允许新建施工单'];
  669. if($sale['state'] >= SalesOrder::State_seven) return [false,'请确认合同状态,新建施工单失败'];
  670. if($sale['top_depart_id'] == $user['head']['id']){
  671. //总社的订单 需要派单 才能建施工
  672. if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
  673. }
  674. // if(empty($data['customer_id'])) return [false, '订单合同暂无客户,请补充完信息再进行施工单操作'];
  675. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  676. if(! $status) return [false, $msg];
  677. if(empty($data['product'])) return [false,'请选择产品'];
  678. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  679. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  680. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  681. if(! empty($data['construction_fee'])){
  682. $res = $this->checkNumber($data['construction_fee']);
  683. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  684. }
  685. if(! empty($data['service_price'])){
  686. $res = $this->checkNumber($data['service_price']);
  687. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  688. }
  689. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  690. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  691. //所属部门 以及 顶级部门
  692. if(empty($data['depart_id'])) {
  693. $data['depart_id'] = $this->getDepart($user);
  694. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  695. }
  696. $product_submit = $product_id = [];
  697. foreach ($data['product'] as $value){
  698. if(empty($value['number'])) return [false,'产品数量不能为空'];
  699. $res = $this->checkNumber($value['number']);
  700. if(! $res) return [false,'请输入正确的产品数量'];
  701. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  702. if(isset($product_submit[$key])){
  703. $product_submit[$key] += $value['number'];
  704. }else{
  705. $product_submit[$key] = $value['number'];
  706. }
  707. $product_id[] = $value['product_id'];
  708. }
  709. //剩余能施工
  710. $id = $data['id'] ?? 0;
  711. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  712. //比较
  713. foreach ($product_submit as $pro => $number){
  714. $tmp = explode(',',$pro);
  715. $p = $tmp[0];
  716. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  717. $s_number = $s_product[$p];
  718. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据(包含已退货产品)'];
  719. }
  720. $id = $data['id'] ?? 0;
  721. $product_save = $this->getSaveDetail($id);
  722. //是否校验库存
  723. ProductInventoryService::is_check($user,$data);
  724. $data['is_check_stock'] = $user['is_check_stock'];
  725. if($data['model_type'] == Construction::Model_type_one){
  726. //到店安装 才校验库存
  727. // list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  728. // if(! $status) return [false, $msg];
  729. }else{
  730. $data['is_check_stock'] = ProductInventorySet::type_two;
  731. }
  732. if($is_add){
  733. $prefix = Construction::$prefix[$data['model_type']];
  734. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  735. if(! $order_number) return [false,'工单编号生成失败!'];
  736. $data['order_number'] = $order_number;
  737. }else{
  738. if(empty($data['id'])) return [false,'ID不能为空'];
  739. $construction = Construction::where('del_time',0)
  740. ->where('id',$data['id'])->first();
  741. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  742. $construction = $construction->toArray();
  743. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单单据状态,修改失败'];
  744. //订单编辑提交限制
  745. $current_top_depart_id = $this->getMyTopDepart($user);
  746. list($status, $msg) = $this->returnOrderEditErrorCommon($current_top_depart_id, $construction['top_depart_id']);
  747. if(! $status) return [false, $msg];
  748. }
  749. return [true, [$product_submit, $product_save]];
  750. }
  751. /**
  752. * 数据拼接
  753. * @param $data
  754. * @return array
  755. */
  756. public function fillData($data){
  757. if(empty($data['data'])) return $data;
  758. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  759. $basic_map = BasicType::whereIn('id',$array)
  760. ->pluck('title','id')
  761. ->toArray();
  762. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  763. ->pluck('emp_name','id')
  764. ->toArray();
  765. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  766. ->pluck('title','id')
  767. ->toArray();
  768. $sales_map = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  769. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  770. ->pluck('title','id')
  771. ->toArray();
  772. //分派的总社或分社
  773. $dispatch = $this->getDispatchData($data['data']);
  774. //施工产品
  775. $product_map = $this->getProduct($data['data']);
  776. //订单状态数据组织
  777. $state_array = $this->getStateMake($data['data']);
  778. //负责人
  779. $construction_info_map = $this->getFzEmployee(array_column($data['data'],'id'));
  780. $address_map = config('address');
  781. foreach ($data['data'] as $key => $value){
  782. $address_str = [];
  783. $product_tmp = $product_map[$value['id']] ?? [];
  784. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  785. if(! empty($value['address1'])) {
  786. $tmp = json_decode($value['address1'],true);
  787. $this->findLabelsByValue($address_map,$tmp,$address_str);
  788. $tmp = implode(' ',$address_str);
  789. $tmp .= ' ' . $value['address2'];
  790. $address = $tmp;
  791. }else{
  792. $address = $value['address2'];
  793. }
  794. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  795. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  796. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  797. $data['data'][$key]['address'] = $address;
  798. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  799. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  800. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  801. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  802. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  803. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  804. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  805. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  806. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  807. $tmp_sales = $sales_map[$value['sales_order_id']] ?? "";
  808. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  809. $data['data'][$key]['sales_order_number'] = $tmp_sales;
  810. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d",$value['handover_time']) : "";
  811. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  812. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  813. $data['data'][$key]['fz'] = $construction_info_map[$value['id']] ?? "";
  814. // $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  815. // $str = "";
  816. // $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  817. // $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  818. // if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  819. // $data['data'][$key]['pq_period'] = $str;
  820. }
  821. return $data;
  822. }
  823. public function getFzEmployee($construction_id = []){
  824. if(empty($construction_id)) return [];
  825. $construction_info = ConstructionInfo::where('del_time',0)
  826. ->where('type',ConstructionInfo::type_three)
  827. ->whereIn('construction_id',$construction_id)
  828. ->select('construction_id','employee_id')
  829. ->get()->toArray();
  830. $emp_map2 = Employee::whereIn('id',array_unique(array_column($construction_info,'employee_id')))
  831. ->pluck('emp_name','id')
  832. ->toArray();
  833. $construction_info_map = [];
  834. foreach ($construction_info as $value){
  835. $emp = $emp_map2[$value['employee_id']] ?? "";
  836. if(! $emp) continue;
  837. if(isset($construction_info_map[$value['construction_id']])){
  838. $construction_info_map[$value['construction_id']] .= ',' . $emp;
  839. }else{
  840. $construction_info_map[$value['construction_id']] = $emp;
  841. }
  842. }
  843. return $construction_info_map;
  844. }
  845. public function getStateMake($data){
  846. if(empty($data)) return [];
  847. $order_no = [];
  848. foreach ($data as $value){
  849. if(! in_array($value['state'], [Construction::State_minus_one,Construction::STATE_ONE])) continue;
  850. $order_no[] = $value['order_number'];
  851. }
  852. return (new OaService())->getOaTeamDetailList($order_no);
  853. }
  854. public function makeState($value, $state_array){
  855. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  856. if(! empty($state_array[$value['order_number']])){
  857. $return = $state_array[$value['order_number']];
  858. if($value['state'] == Construction::State_minus_one){
  859. $state = "驳回:" . $return;
  860. }else{
  861. $state = "待" . $return . "审核";
  862. }
  863. }elseif($value['state'] == Construction::STATE_ZERO){
  864. $state = "待" . $value['crt_name'] . "提交";
  865. }elseif($value['state'] == Construction::STATE_TWO && $value['start_time'] > $today_stamp){
  866. $state = Construction::$name[Construction::STATE_DIFF_TWO] ?? '';
  867. }else{
  868. $state = Construction::$name[$value['state']] ?? '';
  869. }
  870. return $state;
  871. }
  872. public function getProduct($data){
  873. $search_id = array_column($data,'id');
  874. if(empty($search_id)) return [];
  875. $product = ConstructionProductInfo::where('del_time',0)
  876. ->whereIn('construction_id',$search_id)
  877. ->select('product_id','construction_id')
  878. ->get()->toArray();
  879. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  880. $return = [];
  881. foreach ($product as $value){
  882. $product_tmp = $product_map[$value['product_id']] ?? "";
  883. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  884. }
  885. return $return;
  886. }
  887. public function getDispatchData($data){
  888. $search_id = [];
  889. foreach ($data as $value){
  890. $search_id[] = $value['sales_order_id'];
  891. }
  892. if(empty($search_id)) return [];
  893. $see = SeeRange::where('del_time',0)
  894. ->whereIn('data_id',$search_id)
  895. ->where('data_type',SeeRange::type_seven)
  896. ->where('type',SeeRange::data_three)
  897. ->select('data_id','param_id')
  898. ->get()->toArray();
  899. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  900. ->pluck('title','id')
  901. ->toArray();
  902. $see_array = [];
  903. foreach ($see as $value){
  904. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  905. }
  906. return $see_array;
  907. }
  908. /**
  909. * 获取施工单号
  910. * @param $data
  911. * @return array
  912. */
  913. public function constructionGet($data){
  914. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  915. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  916. $prefix = Construction::$prefix[$data['model_type']];
  917. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  918. if(! $order_number) return [false,'工单编号生成失败!'];
  919. return [true,['order_number' => $order_number]];
  920. }
  921. /**
  922. * 获取保存详情
  923. * @param $id
  924. * @return array
  925. */
  926. public function getSaveDetail($id){
  927. $product_save = [];
  928. if(empty($id)) return $product_save;
  929. $sub = ConstructionProductInfo::where('construction_id',$id)
  930. ->where('del_time',0)
  931. ->get()->toArray();
  932. foreach ($sub as $value){
  933. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  934. if(isset($product_save[$key])){
  935. $product_save[$key] += $value['number'];
  936. }else{
  937. $product_save[$key] = $value['number'];
  938. }
  939. }
  940. return $product_save;
  941. }
  942. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  943. $construction = Construction::where('del_time',0)
  944. ->where('sales_order_id',$sales_order_id)
  945. ->select('id')->get()->toArray();
  946. $construction_id = array_column($construction,'id');
  947. $product_save = [];
  948. $sub = ConstructionProductInfo::where('del_time',0)
  949. ->whereIn('construction_id',$construction_id)
  950. ->when(! empty($id), function ($query) use ($id) {
  951. return $query->where('construction_id', '<>',$id);
  952. })
  953. ->get()->toArray();
  954. foreach ($sub as $value){
  955. if(isset($product_save[$value['product_id']])){
  956. $product_save[$value['product_id']] += $value['number'];
  957. }else{
  958. $product_save[$value['product_id']] = $value['number'];
  959. }
  960. }
  961. $product_save2 = [];
  962. $sub1 = ReturnExchangeOrder::where('del_time',0)
  963. ->where('type',ReturnExchangeOrder::Order_type)
  964. ->where('model_type',ReturnExchangeOrder::Model_type_one)
  965. ->where('data_id',$sales_order_id)
  966. ->get()->toArray();
  967. $sub1_array = ReturnExchangeOrderProductInfo::where('del_time',0)
  968. ->whereIn('return_exchange_id',array_column($sub1,'id'))
  969. ->get()->toArray();
  970. foreach ($sub1_array as $value){
  971. if(isset($product_save2[$value['product_id']])){
  972. $product_save2[$value['product_id']] += $value['number'];
  973. }else{
  974. $product_save2[$value['product_id']] = $value['number'];
  975. }
  976. }
  977. $sales_order_product = [];
  978. $sales_product = SalesOrderProductInfo::where('del_time',0)
  979. ->where('sales_order_id',$sales_order_id)
  980. ->get()->toArray();
  981. foreach ($sales_product as $value){
  982. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  983. $product_save_tmp2 = $product_save2[$value['product_id']] ?? 0;
  984. if(isset($sales_order_product[$value['product_id']])){
  985. $sales_order_product[$value['product_id']] += $value['number'];
  986. }else{
  987. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp - $product_save_tmp2;
  988. }
  989. }
  990. return $sales_order_product;
  991. }
  992. public function deliveryNoteEdit($data,$user){
  993. list($status, $msg) = $this->deliveryNoteEditRule($data, $user);
  994. if(! $status) return [false, $msg];
  995. list($model, $new, $old) = $msg;
  996. try {
  997. if(! isset($data['is_edit']) || $data['is_edit']) {
  998. if(isset($data['is_submit'])) $model->state = 1;
  999. $model->upd_id = $user['id'];
  1000. $model->construction_order_number = $data['construction_order_number'] ?? '';
  1001. $model->sales_order_number = $data['sales_order_number'] ?? '';
  1002. $model->start_time = $data['start_time'] ?? '';
  1003. $model->end_time = $data['end_time'] ?? '';
  1004. $model->vin_no = $data['vin_no'] ?? '';
  1005. $model->system = $data['system'] ?? '';
  1006. $model->mile = $data['mile'] ?? '';
  1007. $model->is_wait = $data['is_wait'] ?? '';
  1008. $model->customer_name = $data['customer_name'] ?? '';
  1009. $model->customer_mobile = $data['customer_mobile'] ?? '';
  1010. $model->sale_man = $data['sale_man'] ?? '';
  1011. $model->install_man = $data['install_man'] ?? '';
  1012. $model->is_brash = $data['is_brash'] ?? '';
  1013. $model->is_chong = $data['is_chong'] ?? '';
  1014. $model->service_mark = $data['service_mark'] ?? '';
  1015. $model->mark = $data['mark'] ?? '';
  1016. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  1017. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  1018. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  1019. $model->other_project_mark = $data['other_project_mark'] ?? '';
  1020. $model->img = $data['img'] ?? '';
  1021. $model->img1 = $data['img1'] ?? '';
  1022. $model->img2 = $data['img2'] ?? '';
  1023. $model->img3 = $data['img3'] ?? '';
  1024. $model->depart_id = $data['depart_id'] ?? 0;
  1025. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  1026. $model->save();
  1027. $id = $model->id;
  1028. $time = time();
  1029. DeliveryNoteDetail::where('del_time',0)
  1030. ->where('delivery_note_id', $id)
  1031. ->update(['del_time' => $time]);
  1032. $insert = [];
  1033. if(! empty($data['sale_man'])){
  1034. $array = explode(',', $data['sale_man']);
  1035. foreach ($array as $value){
  1036. $insert[] = [
  1037. 'delivery_note_id' => $id,
  1038. 'type' => DeliveryNoteDetail::TYPE_ONE,
  1039. 'data_id' => $value,
  1040. 'crt_time' => $time,
  1041. ];
  1042. }
  1043. }
  1044. if(! empty($data['install_man'])){
  1045. $array = explode(',', $data['install_man']);
  1046. foreach ($array as $value){
  1047. $insert[] = [
  1048. 'delivery_note_id' => $id,
  1049. 'type' => DeliveryNoteDetail::TYPE_TWO,
  1050. 'data_id' => $value,
  1051. 'crt_time' => $time,
  1052. ];
  1053. }
  1054. }
  1055. if(! empty($insert)) DeliveryNoteDetail::insert($insert);
  1056. }else{
  1057. if(isset($data['is_submit'])){
  1058. $model->state = 1;
  1059. $model->save();
  1060. }
  1061. }
  1062. if(isset($data['is_submit'])) $this->updateSaleOrder($model->id);
  1063. }catch (\Throwable $e){
  1064. return [false, $e->getMessage()];
  1065. }
  1066. return [true, ['file' => ['old' => $old, 'new' => $new]]];
  1067. }
  1068. public function updateSaleOrder($id){
  1069. $d = DeliveryNote::where('id',$id)->first();
  1070. if(empty($d)) return;
  1071. $d = $d->toArray();
  1072. if(! empty($d['img2'])) $img[] = $d['img2'];
  1073. if(! empty($d['img3'])) $img[] = $d['img3'];
  1074. $sale = SalesOrder::where('del_time',0)
  1075. ->where('order_number',$d['sales_order_number'])
  1076. ->first();
  1077. if(empty($sale)) return;
  1078. $id = $sale->id;
  1079. if($sale->state < SalesOrder::State_seven){
  1080. $sale->state = SalesOrder::State_seven;
  1081. $sale->save();
  1082. }
  1083. $time = time();
  1084. $insert = [];
  1085. if(! empty($img)){
  1086. foreach ($img as $value){
  1087. $insert[] = [
  1088. 'sales_order_id' => $id,
  1089. 'type' => SalesOrderInfo::type_seven,
  1090. 'file' => $value,
  1091. 'crt_time' => $time,
  1092. ];
  1093. }
  1094. }
  1095. if(! empty($insert)) SalesOrderInfo::insert($insert);
  1096. }
  1097. public function deliveryNoteEditRule(&$data, $user){
  1098. if(empty($data['construction_order_number'])) return [false, '施工单编号不能为空'];
  1099. $construction = Construction::where('del_time',0)
  1100. ->where('order_number', $data['construction_order_number'])
  1101. ->first();
  1102. if(empty($construction)) return [false, '施工单:' . $data['construction_order_number'] . '不存在或已被删除'];
  1103. $construction = $construction->toArray();
  1104. $data['depart_id'] = $construction['depart_id'];
  1105. $data['top_depart_id'] = $construction['top_depart_id'];
  1106. //是否交车
  1107. $is_submit = 0;
  1108. if(isset($data['is_submit'])) $is_submit = 1;
  1109. //校验
  1110. if($is_submit && $construction['state'] != Construction::STATE_FOUR) return [false, '施工单:' . $data['construction_order_number'] . '未完结,无法交车'];
  1111. //校验
  1112. if(empty($data['sales_order_number'])) return [false, '销售订单号不能为空'];
  1113. list($status, $msg) = (new SalesOrderService())->detail(['order_number' => $data['sales_order_number']], $user);
  1114. if(! $status) return [false, $msg];
  1115. if($is_submit && $msg['payment_receipt_list']['not_receipt_amount'] > 0 && $msg['model_type'] == SalesOrder::Model_type_one) {
  1116. return [false, '销售订单:' . $data['sales_order_number'] . '还有未回款金额, 交车失败'];
  1117. }
  1118. $new = $old = [];
  1119. if(isset($data['id'])) {
  1120. $model = DeliveryNote::where('id',$data['id'])->first();
  1121. if(empty($model) || $model->del_time > 0) return [false,'交车单不存在或已被删除!'];
  1122. if($is_submit){
  1123. if(! $model->img3 && ! $data['img3']) return [false, '保存并交车时,必须客户确认签字!'];
  1124. }else{
  1125. if($model->img3) return [false,'客户已确认签字,保存失败!'];
  1126. }
  1127. $bool = DeliveryNote::where('del_time',0)
  1128. ->where('construction_order_number',$data['construction_order_number'])
  1129. ->where('id','<>',$data['id'])->first();
  1130. if($bool) return [false,'施工单:' . $data['construction_order_number'] . '已创建交车单!'];
  1131. $is_edit = 0;//不能编辑
  1132. if(! $model->img3){
  1133. $is_edit = 1;
  1134. if($model->img2 != "" && $data['img2'] != $model->img2) {
  1135. $old[] = $model->img2;
  1136. }else{
  1137. if($data['img2'] != "") $new[] = $data['img2'];
  1138. }
  1139. if($model->img3 != "" && $data['img3'] != $model->img3) {
  1140. $old[] = $model->img3;
  1141. }else{
  1142. if($data['img3'] != "") $new[] = $data['img3'];
  1143. }
  1144. }
  1145. $data['is_edit'] = $is_edit;
  1146. } else {
  1147. $bool = DeliveryNote::where('del_time',0)
  1148. ->where('construction_order_number',$data['construction_order_number'])
  1149. ->first();
  1150. if($bool) return [false,'施工单:' . $data['construction_order_number'] . '已创建交车单!'];
  1151. $model = new DeliveryNote();
  1152. $model->crt_id = $user['id'];
  1153. if($data['img2'] != "") $new[] = $data['img2'];
  1154. if($data['img3'] != "") $new[] = $data['img3'];
  1155. if($is_submit && ! $data['img3']) return [false, '保存并交车时,必须客户确认签字!'];
  1156. }
  1157. return [true, [$model, $new, $old]];
  1158. }
  1159. public function deliveryNoteList($data)
  1160. {
  1161. $model = DeliveryNote::where('del_time',0)
  1162. ->select("*")
  1163. ->orderBy("id","desc");
  1164. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  1165. if(! empty($data['sales_order_number'])) $model->where('sales_order_number', 'LIKE', '%'.$data['sales_order_number'].'%');
  1166. if(! empty($data['sale_man_title'])) {
  1167. $id = $this->searchForMan($data['sale_man_title'], DeliveryNoteDetail::TYPE_ONE);
  1168. $model->whereIn('id', $id);
  1169. }
  1170. if(! empty($data['install_man_title'])) {
  1171. $id = $this->searchForMan($data['install_man_title'], DeliveryNoteDetail::TYPE_TWO);
  1172. $model->whereIn('id', $id);
  1173. }
  1174. if(! empty($data['end_time'][0]) && ! empty($data['end_time'][1])) {
  1175. $return = $this->changeDateToTimeStampAboutRange($data['end_time']);
  1176. $model->whereBetween('end_time',[$return[0],$return[1]]);
  1177. }
  1178. $list = $this->limit($model,'', $data);
  1179. $list = $this->fillDeliveryNoteData($list);
  1180. return [true, $list];
  1181. }
  1182. public function fillDeliveryNoteData($data){
  1183. if(empty($data['data'])) return $data;
  1184. $emp = $this->returnMan($data['data']);
  1185. foreach ($data['data'] as $key => $value){
  1186. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1187. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1188. $data['data'][$key]['sale_man_title'] = $this->returnMan2($value['sale_man'],$emp);
  1189. $data['data'][$key]['install_man_title'] = $this->returnMan2($value['install_man'],$emp);
  1190. $data['data'][$key]['state_title'] = DeliveryNote::$name[$value['state']] ?? "";
  1191. }
  1192. return $data;
  1193. }
  1194. public function searchForMan($title, $type){
  1195. $emp = Employee::where('del_time',0)
  1196. ->where('emp_name', 'LIKE', '%'.$title.'%')
  1197. ->select('id')
  1198. ->get()->toArray();
  1199. $emp = array_column($emp,'id');
  1200. if(empty($emp)) return [];
  1201. $delivery_note_id = DeliveryNoteDetail::where('del_time',0)
  1202. ->where('type', $type)
  1203. ->whereIn('data_id', $emp)
  1204. ->select('delivery_note_id')
  1205. ->get()->toArray();
  1206. return array_unique(array_column($delivery_note_id,'delivery_note_id'));
  1207. }
  1208. public function returnMan($data = []){
  1209. if(empty($data)) return [];
  1210. $man = [];
  1211. foreach ($data as $value){
  1212. if(! empty($value['sale_man'])){
  1213. $tmp = explode(',', $value['sale_man']);
  1214. foreach ($tmp as $t){
  1215. if(! in_array($t, $man)) $man[] = $t;
  1216. }
  1217. }
  1218. if(! empty($value['install_man'])){
  1219. $tmp = explode(',', $value['install_man']);
  1220. foreach ($tmp as $t){
  1221. if(! in_array($t, $man)) $man[] = $t;
  1222. }
  1223. }
  1224. if(! in_array($value['crt_id'], $man)) $man[] = $value['crt_id'];
  1225. }
  1226. $emp = Employee::whereIn('id', $man)
  1227. ->pluck('emp_name','id')
  1228. ->toArray();
  1229. return $emp;
  1230. }
  1231. public function returnMan2($value,$emp){
  1232. $str_man = "";
  1233. if(! empty($value)){
  1234. $tmp = explode(',', $value);
  1235. foreach ($tmp as $t){
  1236. $t2 = $emp[$t] ?? "";
  1237. if($t2) $str_man .= $t2 . ",";
  1238. }
  1239. $str_man = rtrim($str_man, ',');
  1240. }
  1241. return $str_man;
  1242. }
  1243. public function deliveryNoteDetail($data){
  1244. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  1245. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  1246. if(empty($model)) return [false,'单据不存在或已被删除'];
  1247. $detail = $model->toArray();
  1248. $emp = $this->returnMan([$detail]);
  1249. $fileUploadService = new FileUploadService();
  1250. $detail['img2_url'] = $fileUploadService->getFileShow($detail['img2']);
  1251. $detail['img3_url'] = $fileUploadService->getFileShow($detail['img3']);
  1252. $detail['sale_man_title'] = $this->returnMan2($detail['sale_man'], $emp);
  1253. $detail['install_man_title'] = $this->returnMan2($detail['install_man'], $emp);
  1254. $detail['crt_name'] = $emp[$detail['crt_id']] ?? "";
  1255. $detail['crt_time'] = $detail['crt_time'] ? date('Y-m-d H:i:s',$detail['crt_time']) : '';
  1256. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])
  1257. ->where('del_time',0)
  1258. ->select('file as url','name','mark')->get()->toArray();
  1259. $detail['file'] = $img_list;
  1260. return [true, $detail];
  1261. }
  1262. public function deliveryNoteDel($data,$user){
  1263. if(empty($data['id'])) return [false, '请选择删除的交车单数据'];
  1264. $model = DeliveryNote::where('id', $data['id'])->first();
  1265. if($model->del_time > 0) return [false, '交车单不存在或已被删除!'];
  1266. if($model->state > DeliveryNote::STATE_ZERO) return [false, '交车单已交车,删除失败'];
  1267. $old = [];
  1268. if($model->img2 != "" ) $old[] = $model->img2;
  1269. if($model->img3 != "" ) $old[] = $model->img3;
  1270. DeliveryNote::where('id',$data['id'])->update(
  1271. [
  1272. 'del_time' => time(),
  1273. 'upd_id' => $user['id'],
  1274. ]
  1275. );
  1276. DeliveryNoteDetail::where('del_time',0)
  1277. ->where('delivery_note_id', $data['id'])->update(['del_time' => time(),]);
  1278. return [true, ['file' => ['old' => $old]]];
  1279. }
  1280. public function constructionPdf($data, $user){
  1281. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  1282. $construction = Construction::where('del_time',0)
  1283. ->where('id',$data['id'])
  1284. ->first();
  1285. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  1286. $construction = $construction->toArray();
  1287. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  1288. $construction['customer_title'] = $customer_title ?? "";
  1289. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  1290. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  1291. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  1292. $construction_info = ConstructionInfo::where('del_time',0)
  1293. ->where('construction_id',$construction['id'])
  1294. ->where('type',CustomerInfo::type_two)
  1295. ->select('id','employee_id')
  1296. ->get()->toArray();
  1297. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  1298. ->select('emp_name','id','mobile')
  1299. ->get()->toArray();
  1300. $emp_map = [];
  1301. foreach ($emp_list as $value){
  1302. $emp_map[$value['id']] = [
  1303. 'name' => $value['emp_name'],
  1304. 'mobile' => $value['mobile'],
  1305. ];
  1306. }
  1307. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1308. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1309. ->select('a.employee_id', 'b.title')
  1310. ->whereIn('employee_id',array_column($emp_list,'id'))
  1311. ->get()->toArray();
  1312. $emp_message_map = [];
  1313. foreach ($emp_message as $value){
  1314. if(isset($emp_message_map[$value['employee_id']])){
  1315. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1316. }else{
  1317. $emp_message_map[$value['employee_id']] = $value['title'];
  1318. }
  1319. }
  1320. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  1321. $construction['crt_name'] = $crt_name;
  1322. $xt_name = "";
  1323. foreach ($construction_info as $value){
  1324. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  1325. }
  1326. $xt_name = rtrim($xt_name, " | ");
  1327. $construction['xt_name'] = $xt_name;
  1328. $p_info = ConstructionProductInfo::where('del_time',0)
  1329. ->where('construction_id',$construction['id'])
  1330. ->get()->toArray();
  1331. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  1332. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  1333. ->pluck('title','id')->toArray();
  1334. $product = [];
  1335. $count = 0;
  1336. foreach ($p_info as $value){
  1337. $tmp = $map[$value['product_id']] ?? [];
  1338. $value['count'] = $count+1;
  1339. $value['title'] = $tmp['title'] ?? "";
  1340. $value['code'] = $tmp['code'] ?? "";
  1341. $value['size'] = $tmp['size'] ?? "";
  1342. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  1343. $product[] = $value;
  1344. }
  1345. //工单进展
  1346. $detail = $this->getOaJz($data,$user);
  1347. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  1348. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  1349. $file_name = time().rand(1000,9999);
  1350. $filename = '施工单_' . $file_name.'.' . 'pdf';
  1351. $pdf->save(storage_path('app/public/export/' . $filename));
  1352. return [true, ['file' => $filename]];
  1353. }
  1354. public function getOaJz($data,$user)
  1355. {
  1356. $oa = Oa::where('menu_id', 34)
  1357. ->where('del_time',0)
  1358. ->where('channel',$user['depart_top'][0]['depart_id'])
  1359. ->first();
  1360. if(empty($oa)) return [];
  1361. $oa = $oa->toArray();
  1362. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  1363. $oa_sub_ids = [];
  1364. foreach ($list as $v) {
  1365. $oa_sub_ids[] = $v['id'];
  1366. }
  1367. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  1368. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  1369. ->select('emp_name','id','mobile')
  1370. ->get()->toArray();
  1371. $emp_map = [];
  1372. foreach ($emp_list as $value){
  1373. $emp_map[$value['id']] = [
  1374. 'name' => $value['emp_name'],
  1375. 'mobile' => $value['mobile'],
  1376. ];
  1377. }
  1378. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1379. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1380. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  1381. ->select('a.employee_id', 'b.title')
  1382. ->get()->toArray();
  1383. $emp_message_map = [];
  1384. foreach ($emp_message as $value){
  1385. if(isset($emp_message_map[$value['employee_id']])){
  1386. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1387. }else{
  1388. $emp_message_map[$value['employee_id']] = $value['title'];
  1389. }
  1390. }
  1391. $oaEmployeeKey = [];
  1392. foreach ($oaEmployee as $v) {
  1393. $str = "";
  1394. $tmp = $emp_map[$v['employee_id']] ?? [];
  1395. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  1396. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  1397. $oaEmployeeKey[$v['oa_sub_id']][] = [
  1398. 'id' => $v['employee_id'],
  1399. 'emp_name' => $str,
  1400. ];
  1401. }
  1402. $return = [];
  1403. foreach ($list as $v) {
  1404. $return[$v['sort']][] = [
  1405. 'emp_id' => $oaEmployeeKey[$v['id']],
  1406. 'index' => $v['h5_key'],
  1407. ];
  1408. }
  1409. $detail = [];
  1410. foreach ($return as $v) {
  1411. $children = "";
  1412. foreach ($v as $vv) {
  1413. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1414. }
  1415. $detail[] = rtrim($children,',');
  1416. }
  1417. return $detail;
  1418. }
  1419. public function constructionOperation($data, $user){
  1420. if(empty($data['order_number'])) return [false, '施工单号不能为空'];
  1421. if(empty($data['type'])) return [false, '操作类型不能为空'];
  1422. $order = Construction::where('del_time',0)
  1423. ->where('order_number',$data['order_number'])
  1424. ->first();
  1425. if(empty($order)) return [false,'施工单不存在或已被删除,操作失败'];
  1426. $order = $order->toArray();
  1427. $send_data = [];
  1428. if($data['type'] == 1){
  1429. if($order['state'] != Construction::STATE_TWO) return [false, '请确认施工单单据状态,操作完结失败'];
  1430. $state = Construction::STATE_THREE;
  1431. //提醒分配人
  1432. $info = ConstructionInfo::where('del_time',0)
  1433. ->where('construction_id',$order['id'])
  1434. ->where('type', ConstructionInfo::type_three)
  1435. ->select('opt_id')
  1436. ->get()->toArray();
  1437. $opt_id = array_unique(array_column($info,'opt_id'));
  1438. if(! empty($opt_id)){
  1439. $emp_map = Employee::whereIn('id', $opt_id)
  1440. ->pluck('emp_name','id')
  1441. ->toArray();
  1442. foreach ($opt_id as $value){
  1443. $emp_tmp = $emp_map[$value] ?? "";
  1444. $send_data[] = [
  1445. 'employee_id' => $value,
  1446. 'type' => 2,
  1447. 'state' => 0,
  1448. 'menu_id' => 34,
  1449. 'order_number' => $order['order_number'],
  1450. 'tmp_data' => [
  1451. $order['order_number'],
  1452. "施工单",
  1453. '待确认',
  1454. $emp_tmp,
  1455. date('Y-m-d H:i:s'),
  1456. ],
  1457. ];
  1458. }
  1459. }
  1460. }else{
  1461. if($order['state'] != Construction::STATE_THREE) return [false, '请确认施工单单据状态,操作待确认失败'];
  1462. $state = Construction::STATE_FOUR;
  1463. //提醒创建人
  1464. $emp_tmp = Employee::where('id', $order['crt_id'])->value('emp_name');
  1465. $send_data[] = [
  1466. 'employee_id' => $order['crt_id'],
  1467. 'type' => 2,
  1468. 'state' => 0,
  1469. 'menu_id' => 34,
  1470. 'order_number' => $order['order_number'],
  1471. 'tmp_data' => [
  1472. $order['order_number'],
  1473. "施工单",
  1474. '已完结',
  1475. $emp_tmp,
  1476. date('Y-m-d H:i:s'),
  1477. ],
  1478. ];
  1479. }
  1480. Construction::where('order_number',$data['order_number'])
  1481. ->update(['state' => $state]);
  1482. (new OaService())->sendWxOaCheckMessage($send_data);
  1483. return [true, ''];
  1484. }
  1485. }