ConstructionService.php 70 KB

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