ConstructionService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionInfo;
  6. use App\Model\ConstructionProductInfo;
  7. use App\Model\Customer;
  8. use App\Model\Depart;
  9. use App\Model\Employee;
  10. use App\Model\SalesOrder;
  11. use App\Model\SalesOrderProductInfo;
  12. use App\Model\SeeRange;
  13. use App\Model\Storehouse;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 施工订单
  17. */
  18. class ConstructionService extends Service
  19. {
  20. /**
  21. * 施工订单编辑
  22. * @param $data
  23. * @param $user
  24. * @return array
  25. */
  26. public function constructionEdit($data,$user){
  27. list($status,$msg) = $this->constructionRule($data, $user, false);
  28. if(!$status) return [$status,$msg];
  29. $params = $this->getDataFile($data);
  30. (new OperationLogService())->setOperationList($params,$user,2);
  31. try {
  32. DB::beginTransaction();
  33. $model = Construction::where('id', $data['id'])->first();
  34. $model->model_type = $data['model_type'];
  35. $model->order_number = $data['order_number'];
  36. $model->title = $data['title'] ?? '';
  37. $model->customer_id = $data['customer_id'] ?? 0;
  38. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  39. $model->install_method = $data['install_method'] ?? 0;
  40. $model->install_position = $data['install_position'] ?? 0;
  41. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  42. $model->construction_fee = $data['construction_fee'] ?? 0;
  43. $model->service_price = $data['service_price'] ?? 0;
  44. $model->construction_time = $data['construction_time'] ?? 0;
  45. $model->handover_time = $data['handover_time'] ?? 0;
  46. $model->urgency = $data['urgency'] ?? 0;
  47. $model->mark = $data['mark'] ?? '';
  48. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  49. $model->address2 = $data['address2'] ?? '';
  50. $model->introduction = $data['introduction'] ?? '';
  51. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  52. $model->start_time = $data['start_time'] ?? 0;
  53. $model->end_time = $data['end_time'] ?? 0;
  54. $model->save();
  55. $time = time();
  56. ConstructionInfo::where('del_time',0)
  57. ->where('construction_id',$data['id'])
  58. ->update(['del_time' => $time]);
  59. ConstructionProductInfo::where('del_time',0)
  60. ->where('construction_id',$data['id'])
  61. ->update(['del_time' => $time]);
  62. if(! empty($data['construction_contact'])){
  63. $insert = [];
  64. foreach ($data['construction_contact'] as $value){
  65. $insert[] = [
  66. 'construction_id' => $model->id,
  67. 'contact_type' => $value['id'],
  68. 'contact_info' => $value['info'],
  69. 'type' => ConstructionInfo::type_one,
  70. 'crt_time' => $time,
  71. ];
  72. }
  73. ConstructionInfo::insert($insert);
  74. }
  75. if(! empty($data['employee_one'])){
  76. $insert = [];
  77. foreach ($data['employee_one'] as $value){
  78. $insert[] = [
  79. 'construction_id' => $model->id,
  80. 'employee_id' => $value,
  81. 'type' => ConstructionInfo::type_two,
  82. 'crt_time' => $time,
  83. ];
  84. }
  85. ConstructionInfo::insert($insert);
  86. }
  87. if(! empty($data['product'])){
  88. $insert = [];
  89. foreach ($data['product'] as $value){
  90. $insert[] = [
  91. 'construction_id' => $model->id,
  92. 'product_id' => $value['product_id'],
  93. 'number' => $value['number'],
  94. 'cost' => $value['cost'] ?? 0,
  95. 'retail_price' => $value['retail_price'] ?? 0,
  96. 'mark' => $value['mark'] ?? '',
  97. 'crt_time' => $time,
  98. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  99. 'basic_type_id' => $value['basic_type_id'],
  100. 'price' => $value['price'],
  101. 'final_amount' => $value['final_amount'] ?? 0,
  102. ];
  103. }
  104. ConstructionProductInfo::insert($insert);
  105. //锁定库存
  106. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  107. }
  108. DB::commit();
  109. }catch (\Exception $exception){
  110. DB::rollBack();
  111. return [false,$exception->getMessage()];
  112. }
  113. return [true,''];
  114. }
  115. /**
  116. * 施工订单新增
  117. * @param $data
  118. * @param $user
  119. * @return array
  120. */
  121. public function constructionAdd($data,$user){
  122. list($status,$msg) = $this->constructionRule($data,$user);
  123. if(!$status) return [$status,$msg];
  124. try {
  125. DB::beginTransaction();
  126. $model = new Construction();
  127. $model->model_type = $data['model_type'];
  128. $model->order_number = $data['order_number'];
  129. $model->title = $data['title'] ?? '';
  130. $model->customer_id = $data['customer_id'] ?? 0;
  131. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  132. $model->install_method = $data['install_method'] ?? 0;
  133. $model->install_position = $data['install_position'] ?? 0;
  134. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  135. $model->construction_fee = $data['construction_fee'] ?? 0;
  136. $model->service_price = $data['service_price'] ?? 0;
  137. $model->construction_time = $data['construction_time'] ?? 0;
  138. $model->handover_time = $data['handover_time'] ?? 0;
  139. $model->urgency = $data['urgency'] ?? 0;
  140. $model->mark = $data['mark'] ?? '';
  141. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  142. $model->address2 = $data['address2'] ?? '';
  143. $model->introduction = $data['introduction'] ?? '';
  144. $model->crt_id = $user['id'];
  145. $model->depart_id = $data['depart_id'] ?? 0;
  146. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  147. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  148. $model->start_time = $data['start_time'] ?? 0;
  149. $model->end_time = $data['end_time'] ?? 0;
  150. $model->save();
  151. $time = time();
  152. if(! empty($data['construction_contact'])){
  153. $insert = [];
  154. foreach ($data['construction_contact'] as $value){
  155. $insert[] = [
  156. 'construction_id' => $model->id,
  157. 'contact_type' => $value['id'],
  158. 'contact_info' => $value['info'],
  159. 'type' => ConstructionInfo::type_one,
  160. 'crt_time' => $time,
  161. ];
  162. }
  163. ConstructionInfo::insert($insert);
  164. }
  165. if(! empty($data['employee_one'])){
  166. $insert = [];
  167. foreach ($data['employee_one'] as $value){
  168. $insert[] = [
  169. 'construction_id' => $model->id,
  170. 'employee_id' => $value,
  171. 'type' => ConstructionInfo::type_two,
  172. 'crt_time' => $time,
  173. ];
  174. }
  175. ConstructionInfo::insert($insert);
  176. }
  177. if(! empty($data['product'])){
  178. $insert = [];
  179. foreach ($data['product'] as $value){
  180. $insert[] = [
  181. 'construction_id' => $model->id,
  182. 'product_id' => $value['product_id'],
  183. 'number' => $value['number'],
  184. 'cost' => $value['cost'] ?? 0,
  185. 'retail_price' => $value['retail_price'] ?? 0,
  186. 'mark' => $value['mark'] ?? '',
  187. 'crt_time' => $time,
  188. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  189. 'basic_type_id' => $value['basic_type_id'],
  190. 'price' => $value['price'],
  191. 'final_amount' => $value['final_amount'] ?? 0,
  192. ];
  193. }
  194. ConstructionProductInfo::insert($insert);
  195. //锁定库存
  196. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  197. }
  198. DB::commit();
  199. }catch (\Exception $exception){
  200. DB::rollBack();
  201. return [false,$exception->getMessage()];
  202. }
  203. (new OperationLogService())->setOperationList($data,$user);
  204. return [true,''];
  205. }
  206. /**
  207. * 施工订单删除
  208. * @param $data
  209. * @return array
  210. */
  211. public function constructionDel($data,$user){
  212. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  213. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  214. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  215. $construction = $construction->toArray();
  216. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  217. $product_save = $this->getSaveDetail($data['id']);
  218. try {
  219. DB::beginTransaction();
  220. Construction::where('id',$data['id'])->update([
  221. 'del_time'=> time()
  222. ]);
  223. ConstructionInfo::where('del_time',0)
  224. ->where('construction_id',$data['id'])
  225. ->update(['del_time' => time()]);
  226. ConstructionProductInfo::where('del_time',0)
  227. ->where('construction_id',$data['id'])
  228. ->update(['del_time' => time()]);
  229. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  230. //锁定库存释放
  231. ProductInventoryService::changeLockNumber($user,[],$product_save);
  232. DB::commit();
  233. }catch (\Exception $exception){
  234. DB::rollBack();
  235. return [false,$exception->getMessage()];
  236. }
  237. return [true,''];
  238. }
  239. /**
  240. * 施工订单详情
  241. * @param $data
  242. * @return array
  243. */
  244. public function detail($data){
  245. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  246. if(! empty($data['id'])){
  247. $construction = Construction::where('del_time',0)
  248. ->where('id',$data['id'])
  249. ->first();
  250. }else{
  251. $construction = Construction::where('del_time',0)
  252. ->where('order_number',$data['order_number'])
  253. ->first();
  254. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  255. }
  256. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  257. $construction = $construction->toArray();
  258. $address = '';
  259. if(! empty($construction['address1'])) {
  260. $tmp = json_decode($construction['address1'],true);
  261. $construction['address1'] = $tmp;
  262. $tmp = implode(' ',$tmp);
  263. $tmp .= ' ' . $construction['address2'];
  264. $address = $tmp;
  265. }
  266. $construction['address'] = $address;
  267. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  268. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  269. $construction['construction_period'] = $start_time . '——' . $end_time;
  270. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  271. $construction['sales_order_number'] = $sales;
  272. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  273. $construction['customer_title'] = $customer_title ?? "";
  274. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  275. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  276. $construction['customer_contact_title'] = $emp_title;
  277. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  278. $array = [
  279. $construction['install_method'],
  280. $construction['install_position'],
  281. $construction['urgency'],
  282. ];
  283. $basic_map = BasicType::whereIn('id',$array)
  284. ->pluck('title','id')
  285. ->toArray();
  286. $construction = [$construction];
  287. foreach ($construction as $key => $value){
  288. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  289. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  290. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  291. }
  292. $construction = $construction[0];
  293. $construction_info = ConstructionInfo::where('del_time',0)
  294. ->where('construction_id',$construction['id'])
  295. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  296. ->get()->toArray();
  297. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  298. ->pluck('emp_name','id')
  299. ->toArray();
  300. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  301. ->pluck('title','id')
  302. ->toArray();
  303. foreach ($construction_info as $value){
  304. if($value['type'] == ConstructionInfo::type_one){
  305. $tmp = [
  306. 'id' => $value['contact_type'],
  307. 'title' => $basic_map2[$value['contact_type']] ?? '',
  308. 'info' => $value['contact_info']
  309. ];
  310. $construction['construction_contact'][] = $tmp;
  311. }elseif ($value['type'] == ConstructionInfo::type_two){
  312. $tmp = [
  313. 'id' => $value['employee_id'],
  314. 'name' => $emp_map[$value['employee_id']] ?? '',
  315. ];
  316. $construction['employee_one'][] = $tmp;
  317. }
  318. }
  319. $p_info = ConstructionProductInfo::where('del_time',0)
  320. ->where('construction_id',$construction['id'])
  321. ->get()->toArray();
  322. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  323. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  324. foreach ($p_info as $value){
  325. $tmp = $map[$value['product_id']] ?? [];
  326. $value['title'] = $tmp['title'] ?? "";
  327. $value['code'] = $tmp['code'] ?? "";
  328. $value['size'] = $tmp['size'] ?? "";
  329. $value['unit'] = $tmp['unit'] ?? "";
  330. $value['bar_code'] = $tmp['bar_code'] ?? "";
  331. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  332. $construction['product'][] = $value;
  333. }
  334. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  335. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  336. //可见范围
  337. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  338. $construction['depart'] = $return[0] ?? [];
  339. $construction['employee'] = $return[1] ?? [];
  340. return [true, $construction];
  341. }
  342. /**
  343. * 施工订单列表
  344. * @param $data
  345. * @param $user
  346. * @return array
  347. */
  348. public function constructionList($data,$user){
  349. $model = Construction::Clear($user,$data);
  350. $model = $model->where('del_time',0)
  351. ->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')
  352. ->orderby('id', 'desc');
  353. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  354. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  355. if(! empty($data['time_type'])) {
  356. if($data['time_type'] == 1) {
  357. $start = strtotime('today');
  358. $end = strtotime('tomorrow') - 1;
  359. }elseif ($data['time_type'] == 2){
  360. $start = strtotime('this week',strtotime('today'));
  361. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  362. }
  363. if(! empty($start) && ! empty($end)) {
  364. $model->where('crt_time','>=',$start);
  365. $model->where('crt_time','<=',$end);
  366. }
  367. }
  368. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  369. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  370. $model->where('start_time','>=',$return[0]);
  371. $model->where('end_time','<=',$return[1]);
  372. }
  373. if(! empty($data['sale_order'])){
  374. $model2 = SalesOrder::Clear($user,$data);
  375. $sale = $model2->where('del_time',0)
  376. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  377. ->select('id')
  378. ->get()->toArray();
  379. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  380. }
  381. if(! empty($data['install_method'])) $model->where('install_method',$data['install_method']);
  382. if(! empty($data['install_position'])) $model->where('install_position',$data['install_position']);
  383. $list = $this->limit($model,'',$data);
  384. $list = $this->fillData($list);
  385. return [true, $list];
  386. }
  387. /**
  388. * 参数规则
  389. * @param $data
  390. * @param $user
  391. * @param $is_add
  392. * @return array
  393. */
  394. public function constructionRule(&$data, $user, $is_add = true){
  395. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  396. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  397. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  398. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  399. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  400. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  401. if(empty($sale)) return [false,'合同不存在或已被删除'];
  402. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  403. $sale = $sale->toArray();
  404. if(empty($data['product'])) return [false,'请选择产品'];
  405. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  406. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  407. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  408. if(! empty($data['construction_fee'])){
  409. $res = $this->checkNumber($data['construction_fee']);
  410. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  411. }
  412. if(! empty($data['service_price'])){
  413. $res = $this->checkNumber($data['service_price']);
  414. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  415. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  416. }
  417. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  418. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  419. if($data['model_type'] == Construction::Model_type_one){
  420. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  421. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  422. }else{
  423. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  424. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  425. }
  426. //所属部门 以及 顶级部门
  427. if(empty($data['depart_id'])) {
  428. $data['depart_id'] = $this->getDepart($user);
  429. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  430. }
  431. $product_submit = $product_id = [];
  432. foreach ($data['product'] as $value){
  433. if(empty($value['number'])) return [false,'产品数量不能为空'];
  434. $res = $this->checkNumber($value['number']);
  435. if(! $res) return [false,'请输入正确的产品数量'];
  436. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  437. if(isset($product_submit[$key])){
  438. $product_submit[$key] += $value['number'];
  439. }else{
  440. $product_submit[$key] = $value['number'];
  441. }
  442. $product_id[] = $value['product_id'];
  443. }
  444. //剩余能施工
  445. $id = $data['id'] ?? 0;
  446. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  447. //比较
  448. foreach ($product_submit as $pro => $number){
  449. $tmp = explode(',',$pro);
  450. $p = $tmp[0];
  451. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  452. $s_number = $s_product[$p];
  453. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  454. }
  455. $id = $data['id'] ?? 0;
  456. $product_save = $this->getSaveDetail($id);
  457. //是否校验库存
  458. ProductInventoryService::is_check($user,$data);
  459. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  460. if(! $status) return [false, $msg];
  461. $start_time = date("Y-m-d H:i",$data['start_time']);
  462. $end_time = date("Y-m-d H:i",$data['end_time']);
  463. if($is_add){
  464. $bool = Construction::where('del_time',0)
  465. ->where('order_number',$data['order_number'])
  466. ->exists();
  467. if($bool) return [false,'工单编号已存在,请重新获取'];
  468. $bool = Construction::where('del_time',0)
  469. ->where('sales_order_id',$data['sales_order_id'])
  470. ->where('start_time', '<=', $data['end_time'])
  471. ->where('end_time', '>=', $data['start_time'])
  472. ->exists();
  473. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  474. }else{
  475. if(empty($data['id'])) return [false,'ID不能为空'];
  476. $bool = Construction::where('del_time',0)
  477. ->where('id','<>',$data['id'])
  478. ->where('sales_order_id',$data['sales_order_id'])
  479. ->where('start_time', '<=', $data['end_time'])
  480. ->where('end_time', '>=', $data['start_time'])
  481. ->exists();
  482. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  483. }
  484. return [true, [$product_submit, $product_save]];
  485. }
  486. /**
  487. * 数据拼接
  488. * @param $data
  489. * @return array
  490. */
  491. public function fillData($data){
  492. if(empty($data['data'])) return $data;
  493. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  494. $basic_map = BasicType::whereIn('id',$array)
  495. ->pluck('title','id')
  496. ->toArray();
  497. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  498. ->pluck('emp_name','id')
  499. ->toArray();
  500. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  501. ->pluck('title','id')
  502. ->toArray();
  503. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray();
  504. $sales_map = [];
  505. foreach ($sales as $value){
  506. $sales_map[$value['id']] = $value;
  507. }
  508. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  509. ->pluck('title','id')
  510. ->toArray();
  511. //分派的总社或分社
  512. $dispatch = $this->getDispatchData($data['data']);
  513. foreach ($data['data'] as $key => $value){
  514. $address = '';
  515. if(! empty($value['address1'])) {
  516. $tmp = json_decode($value['address1'],true);
  517. $tmp = implode(' ',$tmp);
  518. $tmp .= ' ' . $value['address2'];
  519. $address = $tmp;
  520. }
  521. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  522. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  523. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  524. $data['data'][$key]['address'] = $address;
  525. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  526. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  527. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  528. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  529. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  530. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  531. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  532. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  533. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  534. $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
  535. $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  536. $data['data'][$key]['sales_order_number'] = $tmp_sales['order_number'];
  537. $data['data'][$key]['handover_time'] = $tmp_sales_time;
  538. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  539. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  540. }
  541. return $data;
  542. }
  543. public function getDispatchData($data){
  544. $search_id = [];
  545. foreach ($data as $value){
  546. $search_id[] = $value['sales_order_id'];
  547. }
  548. if(empty($search_id)) return [];
  549. $see = SeeRange::where('del_time',0)
  550. ->whereIn('data_id',$search_id)
  551. ->where('data_type',SeeRange::type_seven)
  552. ->where('type',SeeRange::data_three)
  553. ->select('data_id','param_id')
  554. ->get()->toArray();
  555. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  556. ->pluck('title','id')
  557. ->toArray();
  558. $see_array = [];
  559. foreach ($see as $value){
  560. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  561. }
  562. return $see_array;
  563. }
  564. /**
  565. * 获取施工单号
  566. * @param $data
  567. * @return array
  568. */
  569. public function constructionGet($data){
  570. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  571. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  572. $prefix = Construction::$prefix[$data['model_type']];
  573. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  574. if(! $order_number) return [false,'工单编号生成失败!'];
  575. return [true,['order_number' => $order_number]];
  576. }
  577. /**
  578. * 获取保存详情
  579. * @param $id
  580. * @return array
  581. */
  582. public function getSaveDetail($id){
  583. $product_save = [];
  584. if(empty($id)) return $product_save;
  585. $sub = ConstructionProductInfo::where('construction_id',$id)
  586. ->where('del_time',0)
  587. ->get()->toArray();
  588. foreach ($sub as $value){
  589. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  590. if(isset($product_save[$key])){
  591. $product_save[$key] += $value['number'];
  592. }else{
  593. $product_save[$key] = $value['number'];
  594. }
  595. }
  596. return $product_save;
  597. }
  598. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  599. $construction = Construction::where('del_time',0)
  600. ->where('sales_order_id',$sales_order_id)
  601. ->select('id')->get()->toArray();
  602. $construction_id = array_column($construction,'id');
  603. $product_save = [];
  604. $sub = ConstructionProductInfo::where('del_time',0)
  605. ->whereIn('construction_id',$construction_id)
  606. ->when(! empty($id), function ($query) use ($id) {
  607. return $query->where('construction_id', '<>',$id);
  608. })
  609. ->get()->toArray();
  610. foreach ($sub as $value){
  611. if(isset($product_save[$value['product_id']])){
  612. $product_save[$value['product_id']] += $value['number'];
  613. }else{
  614. $product_save[$value['product_id']] = $value['number'];
  615. }
  616. }
  617. $sales_order_product = [];
  618. $sales_product = SalesOrderProductInfo::where('del_time',0)
  619. ->where('sales_order_id',$sales_order_id)
  620. ->get()->toArray();
  621. foreach ($sales_product as $value){
  622. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  623. if(isset($sales_order_product[$value['product_id']])){
  624. $sales_order_product[$value['product_id']] += $value['number'];
  625. }else{
  626. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  627. }
  628. }
  629. return $sales_order_product;
  630. }
  631. }