ConstructionService.php 68 KB

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