ConstructionService.php 36 KB

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