ConstructionService.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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. $sales_info = SalesOrderInfo::where('del_time',0)
  394. ->where('sales_order_id',$construction['sales_order_id'])
  395. ->where('type', SalesOrderInfo::type_two)
  396. ->get()->toArray();
  397. $emp = Employee::whereIn('id',array_unique(array_column($sales_info,'data_id')))
  398. ->pluck('emp_name','id')
  399. ->toArray();
  400. $sale_man = [];
  401. foreach ($sales_info as $value){
  402. $tt = $emp[$value['data_id']] ?? '';
  403. $tmp = [
  404. 'id' => $value['data_id'],
  405. 'name' => $tt?? '',
  406. ];
  407. $sale_man[] = $tmp;
  408. }
  409. $construction['sale_man'] = $sale_man;
  410. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  411. $construction['customer_title'] = $customer_title ?? "";
  412. $info = CustomerInfo::from('customer_info as a')
  413. ->leftJoin('basic_type as b','b.id','a.contact_type')
  414. ->where('a.del_time',0)
  415. ->where('a.customer_id',$construction['customer_id'])
  416. ->where('a.contact_type','>',0)
  417. ->select('b.title','a.contact_info')
  418. ->get()->toArray();
  419. $construction['customer_array'] = $info;
  420. $info = CustomerInfo::from('customer_info as a')
  421. ->leftJoin('basic_type as b','b.id','a.contact_type')
  422. ->where('a.del_time',0)
  423. ->where('a.customer_id',$construction['customer_id'])
  424. ->where('a.contact_type','>',0)
  425. ->select('b.title','a.contact_info')
  426. ->get()->toArray();
  427. $construction['customer_array'] = $info;
  428. $construction['storehouse_title'] = $construction['storehouse_id'] > 0 ? Storehouse::where('id',$construction['storehouse_id'])->value('title') : "";
  429. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  430. $construction['customer_contact_title'] = $emp_title;
  431. $construction['employee_two'] = $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  432. $array = [
  433. $construction['install_method'],
  434. $construction['install_position'],
  435. $construction['urgency'],
  436. ];
  437. $basic_map = BasicType::whereIn('id',$array)
  438. ->pluck('title','id')
  439. ->toArray();
  440. $construction = [$construction];
  441. foreach ($construction as $key => $value){
  442. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  443. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  444. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  445. }
  446. $construction = $construction[0];
  447. $construction['file'] = [];
  448. $file = ConstructionFile::where('del_time',0)
  449. ->where('construction_id',$construction['id'])
  450. ->select('id','construction_id','file','name','mark')
  451. ->get()->toArray();
  452. $fileUploadService = new FileUploadService();
  453. foreach ($file as $value){
  454. $construction['file'][] = [
  455. 'url' => $value['file'],
  456. 'name' => $value['name'],
  457. 'mark' => $value['mark'],
  458. 'show_url' => $fileUploadService->getFileShow($value['file']),
  459. ];
  460. }
  461. $construction_info = ConstructionInfo::where('del_time',0)
  462. ->where('construction_id',$construction['id'])
  463. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  464. ->get()->toArray();
  465. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  466. ->pluck('emp_name','id')
  467. ->toArray();
  468. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  469. ->pluck('title','id')
  470. ->toArray();
  471. foreach ($construction_info as $value){
  472. if($value['type'] == ConstructionInfo::type_one){
  473. $tmp = [
  474. 'id' => $value['contact_type'],
  475. 'title' => $basic_map2[$value['contact_type']] ?? '',
  476. 'info' => $value['contact_info']
  477. ];
  478. $construction['construction_contact'][] = $tmp;
  479. }elseif ($value['type'] == ConstructionInfo::type_two){
  480. $tmp = [
  481. 'id' => $value['employee_id'],
  482. 'name' => $emp_map[$value['employee_id']] ?? '',
  483. ];
  484. $construction['employee_one'][] = $tmp;
  485. }elseif ($value['type'] == ConstructionInfo::type_three){
  486. $tmp = [
  487. 'id' => $value['employee_id'],
  488. 'name' => $emp_map[$value['employee_id']] ?? '',
  489. ];
  490. $construction['employee_two'][] = $tmp;
  491. }
  492. }
  493. $p_info = ConstructionProductInfo::where('del_time',0)
  494. ->where('construction_id',$construction['id'])
  495. ->get()->toArray();
  496. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  497. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  498. foreach ($p_info as $value){
  499. $tmp = $map[$value['product_id']] ?? [];
  500. $value['title'] = $tmp['title'] ?? "";
  501. $value['code'] = $tmp['code'] ?? "";
  502. $value['size'] = $tmp['size'] ?? "";
  503. $value['unit'] = $tmp['unit'] ?? "";
  504. $value['bar_code'] = $tmp['bar_code'] ?? "";
  505. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  506. $construction['product'][] = $value;
  507. }
  508. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  509. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  510. //可见范围
  511. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  512. $construction['depart'] = $return[0] ?? [];
  513. $construction['employee'] = $return[1] ?? [];
  514. return [true, $construction];
  515. }
  516. public function constructionCommon($data,$user){
  517. $model = Construction::Clear($user,$data);
  518. $model = $model->where('del_time',0)
  519. ->orderby('id', 'desc');
  520. if(empty($data['select_field'])){
  521. $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');
  522. }else{
  523. $model->select('sales_order_id');
  524. }
  525. if(isset($data['state'])) {
  526. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  527. if($data['state'] == Construction::STATE_DIFF_TWO){
  528. $model->where('state', Construction::STATE_TWO);
  529. $model->where('start_time', '>', $today_stamp);
  530. }elseif($data['state'] == Construction::STATE_TWO){
  531. $model->where('state', Construction::STATE_TWO);
  532. $model->where('start_time', '<=', $today_stamp);
  533. }else{
  534. $model->where('state', $data['state']);
  535. }
  536. }
  537. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  538. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  539. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  540. if(! empty($data['time_type'])) {
  541. if($data['time_type'] == 1) {
  542. $start = strtotime('today');
  543. $end = strtotime('tomorrow') - 1;
  544. }elseif ($data['time_type'] == 2){
  545. $start = strtotime('this week',strtotime('today'));
  546. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  547. }
  548. if(! empty($start) && ! empty($end)) {
  549. $model->where('crt_time','>=',$start);
  550. $model->where('crt_time','<=',$end);
  551. }
  552. }
  553. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  554. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  555. $model->where('start_time','>=',$return[0]);
  556. $model->where('end_time','<=',$return[1]);
  557. }
  558. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  559. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  560. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  561. }
  562. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  563. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  564. $model->whereBetween('crt_time',[$return[0],$return[1]]);
  565. }
  566. if(! empty($data['sale_order'])){
  567. $model2 = SalesOrder::Clear($user,$data);
  568. $sale = $model2->where('del_time',0)
  569. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  570. ->select('id')
  571. ->get()->toArray();
  572. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  573. }
  574. if(! empty($data['install_method'])) {
  575. $id = (new BasicTypeService())->basicTypeSearchId($data['install_method']);
  576. $model->whereIn('install_method',$id);
  577. }
  578. if(! empty($data['install_position'])) {
  579. $id = (new BasicTypeService())->basicTypeSearchId($data['install_position']);
  580. $model->whereIn('install_position',$id);
  581. }
  582. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  583. if(! empty($data['select_field'])) return $model->get()->toArray();
  584. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  585. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  586. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  587. }
  588. return $model;
  589. }
  590. /**
  591. * 施工订单列表
  592. * @param $data
  593. * @param $user
  594. * @return array
  595. */
  596. public function constructionList($data,$user){
  597. $model = $this->constructionCommon($data, $user);
  598. if(is_array($model)) return $model;
  599. $list = $this->limit($model,'',$data);
  600. $list = $this->fillData($list);
  601. $count = $this->countData("crt_time", $data, $user);
  602. $list['today'] = $count[0] ?? 0;
  603. $list['yesterday'] = $count[1] ?? 0;
  604. return [true, $list];
  605. }
  606. public function countData($column = "", $data, $user){
  607. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  608. // 获取今天的开始和结束时间戳
  609. $todayStart = Carbon::today()->startOfDay()->timestamp;
  610. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  611. // 获取昨天的开始和结束时间戳
  612. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  613. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  614. $data['wx_' . $column] = [$todayStart, $todayEnd];
  615. $model = $this->constructionCommon($data, $user);
  616. // 查询今天的数据条数
  617. $todayCount = $model->count();
  618. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  619. $model = $this->constructionCommon($data, $user);
  620. // 查询昨天的数据条数
  621. $yesterdayCount = $model->count();
  622. return [$todayCount, $yesterdayCount];
  623. }
  624. /**
  625. * 参数规则
  626. * @param $data
  627. * @param $user
  628. * @param $is_add
  629. * @return array
  630. */
  631. public function constructionRule(&$data, $user, $is_add = true){
  632. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  633. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  634. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  635. if($data['model_type'] == Construction::Model_type_one && empty($data['storehouse_id'])) return [false,'请选择仓库'];
  636. if(empty($data['menu_id'])) return [false, '菜单信息不能为空'];
  637. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  638. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  639. if(empty($sale)) return [false,'合同不存在或已被删除'];
  640. $sale = $sale->toArray();
  641. if($sale['state'] < SalesOrder::State_two) return [false,'合同未通过确认,不允许新建施工单'];
  642. if($sale['state'] >= SalesOrder::State_seven) return [false,'请确认合同状态,新建施工单失败'];
  643. if($sale['top_depart_id'] == $user['head']['id']){
  644. //总社的订单 需要派单 才能建施工
  645. if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
  646. }
  647. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  648. if(! $status) return [false, $msg];
  649. if(empty($data['product'])) return [false,'请选择产品'];
  650. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  651. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  652. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  653. if(! empty($data['construction_fee'])){
  654. $res = $this->checkNumber($data['construction_fee']);
  655. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  656. }
  657. if(! empty($data['service_price'])){
  658. $res = $this->checkNumber($data['service_price']);
  659. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  660. }
  661. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  662. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  663. if($data['model_type'] == Construction::Model_type_one){
  664. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  665. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  666. }else{
  667. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  668. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  669. }
  670. //所属部门 以及 顶级部门
  671. if(empty($data['depart_id'])) {
  672. $data['depart_id'] = $this->getDepart($user);
  673. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  674. }
  675. $product_submit = $product_id = [];
  676. foreach ($data['product'] as $value){
  677. if(empty($value['number'])) return [false,'产品数量不能为空'];
  678. $res = $this->checkNumber($value['number']);
  679. if(! $res) return [false,'请输入正确的产品数量'];
  680. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  681. if(isset($product_submit[$key])){
  682. $product_submit[$key] += $value['number'];
  683. }else{
  684. $product_submit[$key] = $value['number'];
  685. }
  686. $product_id[] = $value['product_id'];
  687. }
  688. //剩余能施工
  689. $id = $data['id'] ?? 0;
  690. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  691. //比较
  692. foreach ($product_submit as $pro => $number){
  693. $tmp = explode(',',$pro);
  694. $p = $tmp[0];
  695. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  696. $s_number = $s_product[$p];
  697. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据(包含已退货产品)'];
  698. }
  699. $id = $data['id'] ?? 0;
  700. $product_save = $this->getSaveDetail($id);
  701. //是否校验库存
  702. ProductInventoryService::is_check($user,$data);
  703. $data['is_check_stock'] = $user['is_check_stock'];
  704. if($data['model_type'] == Construction::Model_type_one){
  705. //到店安装 才校验库存
  706. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  707. if(! $status) return [false, $msg];
  708. }else{
  709. $data['is_check_stock'] = ProductInventorySet::type_two;
  710. }
  711. if($is_add){
  712. $bool = Construction::where('del_time',0)
  713. ->where('order_number',$data['order_number'])
  714. ->exists();
  715. if($bool) return [false,'工单编号已存在,请重新获取'];
  716. }else{
  717. if(empty($data['id'])) return [false,'ID不能为空'];
  718. $construction = Construction::where('del_time',0)
  719. ->where('id',$data['id'])->first();
  720. if(empty($construction)) return [false, '施工单单据不存在或已被删除'];
  721. if($construction->state > Construction::STATE_ZERO) return [false,'请确认施工单单据状态,修改失败'];
  722. }
  723. return [true, [$product_submit, $product_save]];
  724. }
  725. /**
  726. * 数据拼接
  727. * @param $data
  728. * @return array
  729. */
  730. public function fillData($data){
  731. if(empty($data['data'])) return $data;
  732. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  733. $basic_map = BasicType::whereIn('id',$array)
  734. ->pluck('title','id')
  735. ->toArray();
  736. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  737. ->pluck('emp_name','id')
  738. ->toArray();
  739. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  740. ->pluck('title','id')
  741. ->toArray();
  742. $sales_map = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  743. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  744. ->pluck('title','id')
  745. ->toArray();
  746. //分派的总社或分社
  747. $dispatch = $this->getDispatchData($data['data']);
  748. //施工产品
  749. $product_map = $this->getProduct($data['data']);
  750. //订单状态数据组织
  751. $state_array = $this->getStateMake($data['data']);
  752. //负责人
  753. $construction_info_map = $this->getFzEmployee(array_column($data['data'],'id'));
  754. $address_map = config('address');
  755. foreach ($data['data'] as $key => $value){
  756. $address_str = [];
  757. $product_tmp = $product_map[$value['id']] ?? [];
  758. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  759. if(! empty($value['address1'])) {
  760. $tmp = json_decode($value['address1'],true);
  761. $this->findLabelsByValue($address_map,$tmp,$address_str);
  762. $tmp = implode(' ',$address_str);
  763. $tmp .= ' ' . $value['address2'];
  764. $address = $tmp;
  765. }else{
  766. $address = $value['address2'];
  767. }
  768. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  769. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  770. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  771. $data['data'][$key]['address'] = $address;
  772. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  773. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  774. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  775. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  776. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  777. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  778. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  779. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  780. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  781. $tmp_sales = $sales_map[$value['sales_order_id']] ?? "";
  782. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  783. $data['data'][$key]['sales_order_number'] = $tmp_sales;
  784. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d",$value['handover_time']) : "";
  785. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  786. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  787. $data['data'][$key]['fz'] = $construction_info_map[$value['id']] ?? "";
  788. // $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  789. // $str = "";
  790. // $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  791. // $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  792. // if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  793. // $data['data'][$key]['pq_period'] = $str;
  794. }
  795. return $data;
  796. }
  797. public function getFzEmployee($construction_id = []){
  798. if(empty($construction_id)) return [];
  799. $construction_info = ConstructionInfo::where('del_time',0)
  800. ->where('type',ConstructionInfo::type_three)
  801. ->whereIn('construction_id',$construction_id)
  802. ->select('construction_id','employee_id')
  803. ->get()->toArray();
  804. $emp_map2 = Employee::whereIn('id',array_unique(array_column($construction_info,'employee_id')))
  805. ->pluck('emp_name','id')
  806. ->toArray();
  807. $construction_info_map = [];
  808. foreach ($construction_info as $value){
  809. $emp = $emp_map2[$value['employee_id']] ?? "";
  810. if(! $emp) continue;
  811. if(isset($construction_info_map[$value['construction_id']])){
  812. $construction_info_map[$value['construction_id']] .= ',' . $emp;
  813. }else{
  814. $construction_info_map[$value['construction_id']] = $emp;
  815. }
  816. }
  817. return $construction_info_map;
  818. }
  819. public function getStateMake($data){
  820. if(empty($data)) return [];
  821. $order_no = [];
  822. foreach ($data as $value){
  823. if(! in_array($value['state'], [Construction::State_minus_one,Construction::STATE_ONE])) continue;
  824. $order_no[] = $value['order_number'];
  825. }
  826. return (new OaService())->getOaTeamDetailList($order_no);
  827. }
  828. public function makeState($value, $state_array){
  829. $today_stamp = strtotime(date("Y-m-d H:i:00"));
  830. if(! empty($state_array[$value['order_number']])){
  831. $return = $state_array[$value['order_number']];
  832. if($value['state'] == Construction::State_minus_one){
  833. $state = "驳回:" . $return;
  834. }else{
  835. $state = "待" . $return . "审核";
  836. }
  837. }elseif($value['state'] == Construction::STATE_ZERO){
  838. $state = "待" . $value['crt_name'] . "提交";
  839. }elseif($value['state'] == Construction::STATE_TWO && $value['start_time'] > $today_stamp){
  840. $state = Construction::$name[Construction::STATE_DIFF_TWO] ?? '';
  841. }else{
  842. $state = Construction::$name[$value['state']] ?? '';
  843. }
  844. return $state;
  845. }
  846. public function getProduct($data){
  847. $search_id = array_column($data,'id');
  848. if(empty($search_id)) return [];
  849. $product = ConstructionProductInfo::where('del_time',0)
  850. ->whereIn('construction_id',$search_id)
  851. ->select('product_id','construction_id')
  852. ->get()->toArray();
  853. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  854. $return = [];
  855. foreach ($product as $value){
  856. $product_tmp = $product_map[$value['product_id']] ?? "";
  857. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  858. }
  859. return $return;
  860. }
  861. public function getDispatchData($data){
  862. $search_id = [];
  863. foreach ($data as $value){
  864. $search_id[] = $value['sales_order_id'];
  865. }
  866. if(empty($search_id)) return [];
  867. $see = SeeRange::where('del_time',0)
  868. ->whereIn('data_id',$search_id)
  869. ->where('data_type',SeeRange::type_seven)
  870. ->where('type',SeeRange::data_three)
  871. ->select('data_id','param_id')
  872. ->get()->toArray();
  873. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  874. ->pluck('title','id')
  875. ->toArray();
  876. $see_array = [];
  877. foreach ($see as $value){
  878. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  879. }
  880. return $see_array;
  881. }
  882. /**
  883. * 获取施工单号
  884. * @param $data
  885. * @return array
  886. */
  887. public function constructionGet($data){
  888. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  889. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  890. $prefix = Construction::$prefix[$data['model_type']];
  891. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  892. if(! $order_number) return [false,'工单编号生成失败!'];
  893. return [true,['order_number' => $order_number]];
  894. }
  895. /**
  896. * 获取保存详情
  897. * @param $id
  898. * @return array
  899. */
  900. public function getSaveDetail($id){
  901. $product_save = [];
  902. if(empty($id)) return $product_save;
  903. $sub = ConstructionProductInfo::where('construction_id',$id)
  904. ->where('del_time',0)
  905. ->get()->toArray();
  906. foreach ($sub as $value){
  907. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  908. if(isset($product_save[$key])){
  909. $product_save[$key] += $value['number'];
  910. }else{
  911. $product_save[$key] = $value['number'];
  912. }
  913. }
  914. return $product_save;
  915. }
  916. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  917. $construction = Construction::where('del_time',0)
  918. ->where('sales_order_id',$sales_order_id)
  919. ->select('id')->get()->toArray();
  920. $construction_id = array_column($construction,'id');
  921. $product_save = [];
  922. $sub = ConstructionProductInfo::where('del_time',0)
  923. ->whereIn('construction_id',$construction_id)
  924. ->when(! empty($id), function ($query) use ($id) {
  925. return $query->where('construction_id', '<>',$id);
  926. })
  927. ->get()->toArray();
  928. foreach ($sub as $value){
  929. if(isset($product_save[$value['product_id']])){
  930. $product_save[$value['product_id']] += $value['number'];
  931. }else{
  932. $product_save[$value['product_id']] = $value['number'];
  933. }
  934. }
  935. $product_save2 = [];
  936. $sub1 = ReturnExchangeOrder::where('del_time',0)
  937. ->where('type',ReturnExchangeOrder::Order_type)
  938. ->where('model_type',ReturnExchangeOrder::Model_type_one)
  939. ->where('data_id',$sales_order_id)
  940. ->get()->toArray();
  941. $sub1_array = ReturnExchangeOrderProductInfo::where('del_time',0)
  942. ->whereIn('return_exchange_id',array_column($sub1,'id'))
  943. ->get()->toArray();
  944. foreach ($sub1_array as $value){
  945. if(isset($product_save2[$value['product_id']])){
  946. $product_save2[$value['product_id']] += $value['number'];
  947. }else{
  948. $product_save2[$value['product_id']] = $value['number'];
  949. }
  950. }
  951. $sales_order_product = [];
  952. $sales_product = SalesOrderProductInfo::where('del_time',0)
  953. ->where('sales_order_id',$sales_order_id)
  954. ->get()->toArray();
  955. foreach ($sales_product as $value){
  956. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  957. $product_save_tmp2 = $product_save2[$value['product_id']] ?? 0;
  958. if(isset($sales_order_product[$value['product_id']])){
  959. $sales_order_product[$value['product_id']] += $value['number'];
  960. }else{
  961. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp - $product_save_tmp2;
  962. }
  963. }
  964. return $sales_order_product;
  965. }
  966. public function deliveryNoteEdit($data,$user){
  967. $id = $user['id'];
  968. if(isset($data['id'])) {
  969. $model = DeliveryNote::where('id',$data['id'])->first();
  970. if($model->del_time > 0) return [false,'交车单不存在或已被删除!'];
  971. if($model->img3) return [false,'客户已签字,无法编辑!'];
  972. $bool = DeliveryNote::where('del_time',0)
  973. ->where('construction_order_number',$data['construction_order_number'])
  974. ->where('id','<>',$data['id'])->first();
  975. if($bool) return [false,'施工单已创建交车单!'];
  976. $new = $old = [];
  977. if($model->img2 != "" && $data['img2'] != $model->img2) {
  978. $old[] = $model->img2;
  979. }else{
  980. if($data['img2'] != "") $new[] = $data['img2'];
  981. }
  982. if($model->img3 != "" && $data['img3'] != $model->img3) {
  983. $old[] = $model->img3;
  984. }else{
  985. if($data['img3'] != "") $new[] = $data['img3'];
  986. }
  987. } else {
  988. $bool = DeliveryNote::where('del_time',0)
  989. ->where('construction_order_number',$data['construction_order_number'])
  990. ->first();
  991. if($bool) return [false,'施工单已创建交车单!'];
  992. $model = new DeliveryNote();
  993. $model->crt_id = $id;
  994. $new = [];
  995. if($data['img2'] != "") $new[] = $data['img2'];
  996. if($data['img3'] != "") $new[] = $data['img3'];
  997. }
  998. try {
  999. $model->upd_id = $id;
  1000. $model->construction_order_number = $data['construction_order_number'] ?? '';
  1001. $model->sales_order_number = $data['sales_order_number'] ?? '';
  1002. $model->start_time = $data['start_time'] ?? '';
  1003. $model->end_time = $data['end_time'] ?? '';
  1004. $model->vin_no = $data['vin_no'] ?? '';
  1005. $model->system = $data['system'] ?? '';
  1006. $model->mile = $data['mile'] ?? '';
  1007. $model->is_wait = $data['is_wait'] ?? '';
  1008. $model->customer_name = $data['customer_name'] ?? '';
  1009. $model->customer_mobile = $data['customer_mobile'] ?? '';
  1010. $model->sale_man = $data['sale_man'] ?? '';
  1011. $model->install_man = $data['install_man'] ?? '';
  1012. $model->is_brash = $data['is_brash'] ?? '';
  1013. $model->is_chong = $data['is_chong'] ?? '';
  1014. $model->service_mark = $data['service_mark'] ?? '';
  1015. $model->mark = $data['mark'] ?? '';
  1016. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  1017. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  1018. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  1019. $model->other_project_mark = $data['other_project_mark'] ?? '';
  1020. $model->img = $data['img'] ?? '';
  1021. $model->img1 = $data['img1'] ?? '';
  1022. $model->img2 = $data['img2'] ?? '';
  1023. $model->img3 = $data['img3'] ?? '';
  1024. $model->save();
  1025. }catch (\Throwable $e){
  1026. return [false, $e->getMessage()];
  1027. }
  1028. return [true, ['file' => ['old' => $old ?? [], 'new' => $new]]];
  1029. }
  1030. public function deliveryNoteList($data)
  1031. {
  1032. $model = DeliveryNote::where('del_time',0)
  1033. ->select("*")
  1034. ->orderBy("id","desc");
  1035. $list = $this->limit($model,'', $data);
  1036. return [true,$list];
  1037. }
  1038. public function fillDeliveryNoteData($data){
  1039. if(empty($data['data'])) return $data;
  1040. $emp = $this->returnMan($data['data']);
  1041. foreach ($data['data'] as $key => $value){
  1042. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1043. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1044. $data['data'][$key]['sale_man_title'] = $this->returnMan2($value['sale_man'],$emp);
  1045. $data['data'][$key]['install_man_title'] = $this->returnMan2($value['install_man'],$emp);
  1046. }
  1047. return $data;
  1048. }
  1049. public function returnMan($data = []){
  1050. if(empty($data)) return [];
  1051. $man = [];
  1052. foreach ($data as $value){
  1053. if(! empty($value['sale_man'])){
  1054. $tmp = explode(',', $value['sale_man']);
  1055. foreach ($tmp as $t){
  1056. if(! in_array($t, $man)) $man[] = $t;
  1057. }
  1058. }
  1059. if(! empty($value['install_man'])){
  1060. $tmp = explode(',', $value['install_man']);
  1061. foreach ($tmp as $t){
  1062. if(! in_array($t, $man)) $man[] = $t;
  1063. }
  1064. }
  1065. if(! in_array($value['crt_id'], $man)) $man[] = $value['crt_id'];
  1066. }
  1067. $emp = Employee::whereIn('id', $man)
  1068. ->pluck('emp_name','id')
  1069. ->toArray();
  1070. return $emp;
  1071. }
  1072. public function returnMan2($value,$emp){
  1073. $str_man = "";
  1074. if(! empty($value)){
  1075. $tmp = explode(',', $value);
  1076. foreach ($tmp as $t){
  1077. $t2 = $emp[$t] ?? "";
  1078. if($t2) $str_man .= $t2 . ",";
  1079. }
  1080. $str_man = rtrim($str_man, ',');
  1081. }
  1082. return $str_man;
  1083. }
  1084. public function deliveryNoteDetail($data){
  1085. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  1086. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  1087. if(empty($model)) return [false,'单据不存在或已被删除'];
  1088. $detail = $model->toArray();
  1089. $emp = $this->returnMan([$detail]);
  1090. $fileUploadService = new FileUploadService();
  1091. $detail['img2_url'] = $fileUploadService->getFileShow($detail['img2']);
  1092. $detail['img3_url'] = $fileUploadService->getFileShow($detail['img3']);
  1093. $detail['sale_man_title'] = $this->returnMan2($detail['sale_man'], $emp);
  1094. $detail['install_man_title'] = $this->returnMan2($detail['install_man'], $emp);
  1095. $detail['crt_name'] = $emp[$detail['crt_id']] ?? "";
  1096. $detail['crt_time'] = $detail['crt_time'] ? date('Y-m-d H:i:s',$detail['crt_time']) : '';
  1097. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])
  1098. ->where('del_time',0)
  1099. ->select('file as url','name','mark')->get()->toArray();
  1100. $detail['file'] = $img_list;
  1101. return [true, $detail];
  1102. }
  1103. public function deliveryNoteDel($data,$user){
  1104. if(empty($data['id'])) return [false, '请选择删除的交车单数据'];
  1105. $model = DeliveryNote::where('id', $data['id'])->first();
  1106. if($model->del_time > 0) return [false, '交车单不存在或已被删除!'];
  1107. $old = [];
  1108. if($model->img2 != "" ) $old[] = $model->img2;
  1109. if($model->img3 != "" ) $old[] = $model->img3;
  1110. DeliveryNote::where('id',$data['id'])->update(
  1111. [
  1112. 'del_time' => time(),
  1113. 'upd_id' => $user['id'],
  1114. ]
  1115. );
  1116. return [true, ['file' => ['old' => $old]]];
  1117. }
  1118. public function constructionPdf($data, $user){
  1119. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  1120. $construction = Construction::where('del_time',0)
  1121. ->where('id',$data['id'])
  1122. ->first();
  1123. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  1124. $construction = $construction->toArray();
  1125. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  1126. $construction['customer_title'] = $customer_title ?? "";
  1127. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  1128. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  1129. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  1130. $construction_info = ConstructionInfo::where('del_time',0)
  1131. ->where('construction_id',$construction['id'])
  1132. ->where('type',CustomerInfo::type_two)
  1133. ->select('id','employee_id')
  1134. ->get()->toArray();
  1135. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  1136. ->select('emp_name','id','mobile')
  1137. ->get()->toArray();
  1138. $emp_map = [];
  1139. foreach ($emp_list as $value){
  1140. $emp_map[$value['id']] = [
  1141. 'name' => $value['emp_name'],
  1142. 'mobile' => $value['mobile'],
  1143. ];
  1144. }
  1145. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1146. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1147. ->select('a.employee_id', 'b.title')
  1148. ->whereIn('employee_id',array_column($emp_list,'id'))
  1149. ->get()->toArray();
  1150. $emp_message_map = [];
  1151. foreach ($emp_message as $value){
  1152. if(isset($emp_message_map[$value['employee_id']])){
  1153. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1154. }else{
  1155. $emp_message_map[$value['employee_id']] = $value['title'];
  1156. }
  1157. }
  1158. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  1159. $construction['crt_name'] = $crt_name;
  1160. $xt_name = "";
  1161. foreach ($construction_info as $value){
  1162. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  1163. }
  1164. $xt_name = rtrim($xt_name, " | ");
  1165. $construction['xt_name'] = $xt_name;
  1166. $p_info = ConstructionProductInfo::where('del_time',0)
  1167. ->where('construction_id',$construction['id'])
  1168. ->get()->toArray();
  1169. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  1170. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  1171. ->pluck('title','id')->toArray();
  1172. $product = [];
  1173. $count = 0;
  1174. foreach ($p_info as $value){
  1175. $tmp = $map[$value['product_id']] ?? [];
  1176. $value['count'] = $count+1;
  1177. $value['title'] = $tmp['title'] ?? "";
  1178. $value['code'] = $tmp['code'] ?? "";
  1179. $value['size'] = $tmp['size'] ?? "";
  1180. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  1181. $product[] = $value;
  1182. }
  1183. //工单进展
  1184. $detail = $this->getOaJz($data,$user);
  1185. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  1186. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  1187. $file_name = time().rand(1000,9999);
  1188. $filename = '施工单_' . $file_name.'.' . 'pdf';
  1189. $pdf->save(storage_path('app/public/export/' . $filename));
  1190. return [true, ['file' => $filename]];
  1191. }
  1192. public function getOaJz($data,$user)
  1193. {
  1194. $oa = Oa::where('menu_id', 34)
  1195. ->where('del_time',0)
  1196. ->where('channel',$user['depart_top'][0]['depart_id'])
  1197. ->first();
  1198. if(empty($oa)) return [];
  1199. $oa = $oa->toArray();
  1200. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  1201. $oa_sub_ids = [];
  1202. foreach ($list as $v) {
  1203. $oa_sub_ids[] = $v['id'];
  1204. }
  1205. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  1206. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  1207. ->select('emp_name','id','mobile')
  1208. ->get()->toArray();
  1209. $emp_map = [];
  1210. foreach ($emp_list as $value){
  1211. $emp_map[$value['id']] = [
  1212. 'name' => $value['emp_name'],
  1213. 'mobile' => $value['mobile'],
  1214. ];
  1215. }
  1216. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  1217. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  1218. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  1219. ->select('a.employee_id', 'b.title')
  1220. ->get()->toArray();
  1221. $emp_message_map = [];
  1222. foreach ($emp_message as $value){
  1223. if(isset($emp_message_map[$value['employee_id']])){
  1224. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  1225. }else{
  1226. $emp_message_map[$value['employee_id']] = $value['title'];
  1227. }
  1228. }
  1229. $oaEmployeeKey = [];
  1230. foreach ($oaEmployee as $v) {
  1231. $str = "";
  1232. $tmp = $emp_map[$v['employee_id']] ?? [];
  1233. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  1234. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  1235. $oaEmployeeKey[$v['oa_sub_id']][] = [
  1236. 'id' => $v['employee_id'],
  1237. 'emp_name' => $str,
  1238. ];
  1239. }
  1240. $return = [];
  1241. foreach ($list as $v) {
  1242. $return[$v['sort']][] = [
  1243. 'emp_id' => $oaEmployeeKey[$v['id']],
  1244. 'index' => $v['h5_key'],
  1245. ];
  1246. }
  1247. $detail = [];
  1248. foreach ($return as $v) {
  1249. $children = "";
  1250. foreach ($v as $vv) {
  1251. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1252. }
  1253. $detail[] = rtrim($children,',');
  1254. }
  1255. return $detail;
  1256. }
  1257. public function constructionOperation($data, $user){
  1258. if(empty($data['order_number'])) return [false, '施工单号不能为空'];
  1259. if(empty($data['type'])) return [false, '操作类型不能为空'];
  1260. $order = Construction::where('del_time',0)
  1261. ->where('order_number',$data['order_number'])
  1262. ->first();
  1263. if(empty($order)) return [false,'施工单不存在或已被删除,操作失败'];
  1264. $order = $order->toArray();
  1265. if($data['type'] == 1){
  1266. if($order['state'] != Construction::STATE_TWO) return [false, '请确认施工单单据状态,操作完结失败'];
  1267. $state = Construction::STATE_THREE;
  1268. }else{
  1269. if($order['state'] != Construction::STATE_THREE) return [false, '请确认施工单单据状态,操作待确认失败'];
  1270. $state = Construction::STATE_FOUR;
  1271. }
  1272. Construction::where('order_number',$data['order_number'])
  1273. ->update(['state' => $state]);
  1274. return [true, ''];
  1275. }
  1276. }