ConstructionService.php 26 KB

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