ConstructionService.php 56 KB

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