ConstructionService.php 58 KB

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