ConstructionService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 constructionDetail($data){
  245. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  246. $construction = Construction::where('del_time',0)
  247. ->where('id',$data['id'])
  248. ->first();
  249. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  250. $construction = $construction->toArray();
  251. $address = '';
  252. if(! empty($construction['address1'])) {
  253. $tmp = json_decode($construction['address1'],true);
  254. $construction['address1'] = $tmp;
  255. $tmp = implode(' ',$tmp);
  256. $tmp .= ' ' . $construction['address2'];
  257. $address = $tmp;
  258. }
  259. $construction['address'] = $address;
  260. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  261. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  262. $construction['construction_period'] = $start_time . '——' . $end_time;
  263. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  264. $construction['sales_order_number'] = $sales;
  265. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  266. $construction['customer_title'] = $customer_title ?? "";
  267. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  268. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  269. $construction['customer_contact_title'] = $emp_title;
  270. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  271. $array = [
  272. $construction['install_method'],
  273. $construction['install_position'],
  274. $construction['urgency'],
  275. ];
  276. $basic_map = BasicType::whereIn('id',$array)
  277. ->pluck('title','id')
  278. ->toArray();
  279. $construction = [$construction];
  280. foreach ($construction as $key => $value){
  281. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  282. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  283. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  284. }
  285. $construction = $construction[0];
  286. $construction_info = ConstructionInfo::where('del_time',0)
  287. ->where('construction_id',$construction['id'])
  288. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  289. ->get()->toArray();
  290. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  291. ->pluck('emp_name','id')
  292. ->toArray();
  293. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  294. ->pluck('title','id')
  295. ->toArray();
  296. foreach ($construction_info as $value){
  297. if($value['type'] == ConstructionInfo::type_one){
  298. $tmp = [
  299. 'id' => $value['contact_type'],
  300. 'title' => $basic_map2[$value['contact_type']] ?? '',
  301. 'info' => $value['contact_info']
  302. ];
  303. $construction['construction_contact'][] = $tmp;
  304. }elseif ($value['type'] == ConstructionInfo::type_two){
  305. $tmp = [
  306. 'id' => $value['employee_id'],
  307. 'name' => $emp_map[$value['employee_id']] ?? '',
  308. ];
  309. $construction['employee_one'][] = $tmp;
  310. }
  311. }
  312. $p_info = ConstructionProductInfo::where('del_time',0)
  313. ->where('construction_id',$construction['id'])
  314. ->get()->toArray();
  315. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  316. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  317. foreach ($p_info as $value){
  318. $tmp = $map[$value['product_id']] ?? [];
  319. $value['title'] = $tmp['title'] ?? "";
  320. $value['code'] = $tmp['code'] ?? "";
  321. $value['size'] = $tmp['size'] ?? "";
  322. $value['unit'] = $tmp['unit'] ?? "";
  323. $value['bar_code'] = $tmp['bar_code'] ?? "";
  324. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  325. $construction['product'][] = $value;
  326. }
  327. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  328. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  329. //可见范围
  330. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  331. $construction['depart'] = $return[0] ?? [];
  332. $construction['employee'] = $return[1] ?? [];
  333. return [true, $construction];
  334. }
  335. /**
  336. * 施工订单列表
  337. * @param $data
  338. * @param $user
  339. * @return array
  340. */
  341. public function constructionList($data,$user){
  342. $model = Construction::Clear($user,$data);
  343. $model = $model->where('del_time',0)
  344. ->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')
  345. ->orderby('id', 'desc');
  346. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  347. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  348. if(! empty($data['time_type'])) {
  349. if($data['time_type'] == 1) {
  350. $start = strtotime('today');
  351. $end = strtotime('tomorrow') - 1;
  352. }elseif ($data['time_type'] == 2){
  353. $start = strtotime('this week',strtotime('today'));
  354. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  355. }
  356. if(! empty($start) && ! empty($end)) {
  357. $model->where('crt_time','>=',$start);
  358. $model->where('crt_time','<=',$end);
  359. }
  360. }
  361. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  362. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  363. $model->where('start_time','>=',$return[0]);
  364. $model->where('end_time','<=',$return[1]);
  365. }
  366. $list = $this->limit($model,'',$data);
  367. $list = $this->fillData($list);
  368. return [true, $list];
  369. }
  370. /**
  371. * 参数规则
  372. * @param $data
  373. * @param $user
  374. * @param $is_add
  375. * @return array
  376. */
  377. public function constructionRule(&$data, $user, $is_add = true){
  378. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  379. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  380. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  381. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  382. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  383. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  384. if(empty($sale)) return [false,'合同不存在或已被删除'];
  385. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  386. $sale = $sale->toArray();
  387. if(empty($data['product'])) return [false,'请选择产品'];
  388. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  389. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  390. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  391. if(! empty($data['construction_fee'])){
  392. $res = $this->checkNumber($data['construction_fee']);
  393. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  394. }
  395. if(! empty($data['service_price'])){
  396. $res = $this->checkNumber($data['service_price']);
  397. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  398. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  399. }
  400. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  401. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  402. if($data['model_type'] == Construction::Model_type_one){
  403. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  404. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  405. }else{
  406. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  407. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  408. }
  409. //所属部门 以及 顶级部门
  410. if(empty($data['depart_id'])) {
  411. $data['depart_id'] = $this->getDepart($user);
  412. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  413. }
  414. $product_submit = $product_id = [];
  415. foreach ($data['product'] as $value){
  416. if(empty($value['number'])) return [false,'产品数量不能为空'];
  417. $res = $this->checkNumber($value['number']);
  418. if(! $res) return [false,'请输入正确的产品数量'];
  419. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  420. if(isset($product_submit[$key])){
  421. $product_submit[$key] += $value['number'];
  422. }else{
  423. $product_submit[$key] = $value['number'];
  424. }
  425. $product_id[] = $value['product_id'];
  426. }
  427. //剩余能施工
  428. $id = $data['id'] ?? 0;
  429. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  430. //比较
  431. foreach ($product_submit as $pro => $number){
  432. $tmp = explode(',',$pro);
  433. $p = $tmp[0];
  434. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  435. $s_number = $s_product[$p];
  436. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  437. }
  438. $id = $data['id'] ?? 0;
  439. $product_save = $this->getSaveDetail($id);
  440. //是否校验库存
  441. ProductInventoryService::is_check($user,$data);
  442. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  443. if(! $status) return [false, $msg];
  444. $start_time = date("Y-m-d H:i",$data['start_time']);
  445. $end_time = date("Y-m-d H:i",$data['end_time']);
  446. if($is_add){
  447. $bool = Construction::where('del_time',0)
  448. ->where('order_number',$data['order_number'])
  449. ->exists();
  450. if($bool) return [false,'工单编号已存在,请重新获取'];
  451. $bool = Construction::where('del_time',0)
  452. ->where('sales_order_id',$data['sales_order_id'])
  453. ->where('start_time', '<=', $data['end_time'])
  454. ->where('end_time', '>=', $data['start_time'])
  455. ->exists();
  456. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  457. }else{
  458. if(empty($data['id'])) return [false,'ID不能为空'];
  459. $bool = Construction::where('del_time',0)
  460. ->where('id','<>',$data['id'])
  461. ->where('sales_order_id',$data['sales_order_id'])
  462. ->where('start_time', '<=', $data['end_time'])
  463. ->where('end_time', '>=', $data['start_time'])
  464. ->exists();
  465. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  466. }
  467. return [true, [$product_submit, $product_save]];
  468. }
  469. /**
  470. * 数据拼接
  471. * @param $data
  472. * @return array
  473. */
  474. public function fillData($data){
  475. if(empty($data['data'])) return $data;
  476. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  477. $basic_map = BasicType::whereIn('id',$array)
  478. ->pluck('title','id')
  479. ->toArray();
  480. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  481. ->pluck('emp_name','id')
  482. ->toArray();
  483. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  484. ->pluck('title','id')
  485. ->toArray();
  486. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  487. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  488. ->pluck('title','id')
  489. ->toArray();
  490. //分派的总社或分社
  491. $dispatch = $this->getDispatchData($data['data']);
  492. foreach ($data['data'] as $key => $value){
  493. $address = '';
  494. if(! empty($value['address1'])) {
  495. $tmp = json_decode($value['address1'],true);
  496. $tmp = implode(' ',$tmp);
  497. $tmp .= ' ' . $value['address2'];
  498. $address = $tmp;
  499. }
  500. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  501. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  502. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  503. $data['data'][$key]['address'] = $address;
  504. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  505. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  506. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  507. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  508. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  509. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  510. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  511. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  512. $data['data'][$key]['sales_order_number'] = $sales[$value['sales_order_id']] ?? '';
  513. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  514. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  515. }
  516. return $data;
  517. }
  518. public function getDispatchData($data){
  519. $search_id = [];
  520. foreach ($data as $value){
  521. $search_id[] = $value['sales_order_id'];
  522. }
  523. if(empty($search_id)) return [];
  524. $see = SeeRange::where('del_time',0)
  525. ->whereIn('data_id',$search_id)
  526. ->where('data_type',SeeRange::type_seven)
  527. ->where('type',SeeRange::data_three)
  528. ->select('data_id','param_id')
  529. ->get()->toArray();
  530. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  531. ->pluck('title','id')
  532. ->toArray();
  533. $see_array = [];
  534. foreach ($see as $value){
  535. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  536. }
  537. return $see_array;
  538. }
  539. /**
  540. * 获取施工单号
  541. * @param $data
  542. * @return array
  543. */
  544. public function constructionGet($data){
  545. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  546. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  547. $prefix = Construction::$prefix[$data['model_type']];
  548. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  549. if(! $order_number) return [false,'工单编号生成失败!'];
  550. return [true,['order_number' => $order_number]];
  551. }
  552. /**
  553. * 获取保存详情
  554. * @param $id
  555. * @return array
  556. */
  557. public function getSaveDetail($id){
  558. $product_save = [];
  559. if(empty($id)) return $product_save;
  560. $sub = ConstructionProductInfo::where('construction_id',$id)
  561. ->where('del_time',0)
  562. ->get()->toArray();
  563. foreach ($sub as $value){
  564. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  565. if(isset($product_save[$key])){
  566. $product_save[$key] += $value['number'];
  567. }else{
  568. $product_save[$key] = $value['number'];
  569. }
  570. }
  571. return $product_save;
  572. }
  573. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  574. $construction = Construction::where('del_time',0)
  575. ->where('sales_order_id',$sales_order_id)
  576. ->select('id')->get()->toArray();
  577. $construction_id = array_column($construction,'id');
  578. $product_save = [];
  579. $sub = ConstructionProductInfo::where('del_time',0)
  580. ->whereIn('construction_id',$construction_id)
  581. ->when(! empty($id), function ($query) use ($id) {
  582. return $query->where('construction_id', '<>',$id);
  583. })
  584. ->get()->toArray();
  585. foreach ($sub as $value){
  586. if(isset($product_save[$value['product_id']])){
  587. $product_save[$value['product_id']] += $value['number'];
  588. }else{
  589. $product_save[$value['product_id']] = $value['number'];
  590. }
  591. }
  592. $sales_order_product = [];
  593. $sales_product = SalesOrderProductInfo::where('del_time',0)
  594. ->where('sales_order_id',$sales_order_id)
  595. ->get()->toArray();
  596. foreach ($sales_product as $value){
  597. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  598. if(isset($sales_order_product[$value['product_id']])){
  599. $sales_order_product[$value['product_id']] += $value['number'];
  600. }else{
  601. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  602. }
  603. }
  604. return $sales_order_product;
  605. }
  606. }