ConstructionService.php 28 KB

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