ConstructionService.php 30 KB

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