ConstructionService.php 28 KB

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