ConstructionService.php 56 KB

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