ConstructionService.php 83 KB

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