ConstructionService.php 74 KB

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