ConstructionService.php 68 KB

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