ConstructionService.php 28 KB

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