ConstructionService.php 34 KB

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