ConstructionService.php 75 KB

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