TSpaceService.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionProductInfo;
  6. use App\Model\Customer;
  7. use App\Model\CustomerInfo;
  8. use App\Model\Product;
  9. use App\Model\ProductCategory;
  10. use App\Model\ProductSnInfo;
  11. use App\Model\ReturnExchangeOrder;
  12. use App\Model\ReturnExchangeOrderProductInfo;
  13. use App\Model\SalesOrder;
  14. use App\Model\SalesOrderProductInfo;
  15. use App\Model\TSpaceSet;
  16. use App\Model\Warranty;
  17. use Illuminate\Support\Facades\DB;
  18. class TSpaceService extends Service
  19. {
  20. public function add($data, $user){
  21. return [true, ''];
  22. }
  23. public function edit($data, $user){
  24. list($status, $msg) = $this->constructionEditOtherRule($data, $user);
  25. if(! $status) return [false, $msg];
  26. DB::beginTransaction();
  27. try{
  28. $set = TSpaceSet::where('del_time',0)
  29. ->where('type', $data['type'])
  30. ->select('file', 'id')
  31. ->get()->toArray();
  32. $set_map = array_column($set,'file','id');
  33. $time = time();
  34. $insert = $update = $new = $old = $id = [];
  35. foreach ($data['data'] as $value){
  36. $text = "";
  37. if(! empty($value['text'])) $text = json_encode($value['text']);
  38. if(! empty($value['id'])){
  39. $file = $set_map[$value['id']] ?? "";
  40. if($value['file'] != $file){
  41. $old[] = $file;
  42. $new[] = $value['file'];
  43. }
  44. $update[] = [
  45. 'id' => $value['id'],
  46. 'type' => $data['type'],
  47. 'text' => $text,
  48. 'file' => $value['file'],
  49. 'crt_time' => $time,
  50. ];
  51. $id[] = $value['id'];
  52. }else{
  53. $new[] = $value['file'];
  54. $insert[] = [
  55. 'type' => $data['type'],
  56. 'text' => $text,
  57. 'file' => $value['file'],
  58. 'crt_time' => $time,
  59. ];
  60. }
  61. }
  62. foreach ($set as $value){
  63. if(! in_array($value['id'], $id)){
  64. $update[] = [
  65. 'id' => $value['id'],
  66. 'del_time' => $time,
  67. ];
  68. $old[] = $value['file'];
  69. }
  70. }
  71. if(! empty($update)){
  72. foreach ($update as $value){
  73. TSpaceSet::where('id',$value['id'])
  74. ->update($value);
  75. }
  76. }
  77. if(! empty($insert)) TSpaceSet::insert($insert);
  78. DB::commit();
  79. }catch (\Exception $exception){
  80. DB::rollBack();
  81. return [false, $exception->getMessage()];
  82. }
  83. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  84. }
  85. public function constructionEditOtherRule($data,$user){
  86. if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];
  87. if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];
  88. if(empty($data['data'])) return [false, '设置内容不能为空'];
  89. foreach ($data['data'] as $value){
  90. if(empty($value['file'])) return [false, '文件不能为空'];
  91. }
  92. return [true, ''];
  93. }
  94. public function detail($data, $user){
  95. if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];
  96. if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];
  97. $set = TSpaceSet::where('del_time',0)
  98. ->where('type', $data['type'])
  99. ->select('file', 'id', 'text')
  100. ->get()->toArray();
  101. $fileUploadService = new FileUploadService();
  102. foreach ($set as $key => $value){
  103. if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);
  104. $url = "";
  105. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  106. $set[$key]['file_url'] = $url;
  107. }
  108. return [true, $set];
  109. }
  110. public function tSpacelist($data){
  111. if(empty($data['type'])){
  112. $type = [];
  113. }else{
  114. if(! is_array($data['type'])){
  115. $type = [$data['type']];
  116. }else{
  117. $type = $data['type'];
  118. }
  119. }
  120. $set = TSpaceSet::where('del_time',0)
  121. ->when(! empty($type), function ($query) use ($type) {
  122. return $query->whereIn('type',$type);
  123. })
  124. ->select('file', 'id', 'text', 'type')
  125. ->get()->toArray();
  126. $fileUploadService = new FileUploadService();
  127. foreach ($set as $key => $value){
  128. if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);
  129. $url = "";
  130. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  131. $set[$key]['file_url'] = $url;
  132. }
  133. return [true, $set];
  134. }
  135. public function warrantyGetProduct($data, $user){
  136. if(empty($data['sale_order_id'])) return [false, '合同ID不能为空'];
  137. $return = $this->warrantyGetProductList($data, $user);
  138. return [true, $return];
  139. }
  140. public function warrantyGetProductList($data, $user){
  141. $return = [];
  142. $data_id = $data['sale_order_id'];
  143. //销售订单对应产品总数
  144. $product = SalesOrderProductInfo::from('sales_order_product_info as a')
  145. ->join('product as b','a.product_id','b.id')
  146. ->where('a.del_time',0)
  147. ->where('a.sales_order_id', $data_id)
  148. ->where('b.warranty_time', '>', 0)
  149. ->select('a.number','a.product_id','b.product_category')
  150. ->get()->toArray();
  151. if(empty($product)) return $return;
  152. $product_id = array_column($product,'product_id');
  153. $map = (new ProductService())->getProductDetail($product_id);
  154. //合同质保产品
  155. $sn_product = ProductSnInfo::where("del_time",0)
  156. ->where('data_id', $data_id)
  157. ->where('type',ProductSnInfo::type_two)
  158. ->whereIn('product_id',$product_id)
  159. ->select('product_id')
  160. ->get()->toArray();
  161. $product_map1 = [];
  162. foreach ($sn_product as $value){
  163. $key = $value['product_id'];
  164. if(isset($product_map1[$key])){
  165. $number = bcadd(1, $product_map1[$key],2);
  166. $product_map1[$key] = $number;
  167. }else{
  168. $product_map1[$key] = 1;
  169. }
  170. }
  171. //合同退货产品
  172. $product_map2 = [];
  173. $return_id = ReturnExchangeOrder::where('del_time',0)
  174. ->where('type', ReturnExchangeOrder::Order_type)
  175. ->where('model_type', ReturnExchangeOrder::Model_type_one)
  176. ->where('data_id', $data_id)
  177. ->select('id')
  178. ->get()->toArray();
  179. $save2 = ReturnExchangeOrderProductInfo::where('del_time',0)
  180. ->whereIn('return_exchange_id', array_column($return_id,'id'))
  181. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  182. ->select('number', 'return_exchange_id','product_id')
  183. ->get()->toArray();
  184. foreach ($save2 as $value){
  185. $key = $value['product_id'];
  186. if(isset($product_map2[$key])){
  187. $number = bcadd($value['number'], $product_map2[$key],2);
  188. $product_map2[$key] = $number;
  189. }else{
  190. $product_map2[$key] = $value['number'];
  191. }
  192. }
  193. $service = new DataSyncToU8Service();
  194. foreach ($product as $value){
  195. $bool = $service->forCheck($value['product_category']);
  196. //车窗膜不管数量算一个
  197. if($bool) $value['number'] = 1;
  198. //合同质保产品
  199. $p1 = $product_map1[$value['product_id']] ?? 0;
  200. //合同退货产品
  201. $p2 = $product_map2[$value['product_id']] ?? 0;
  202. $number = bcsub($value['number'], $p1,2);
  203. $number = bcsub($number, $p2,2);
  204. if($number <= 0) continue;
  205. $tmp = $map[$value['product_id']] ?? [];
  206. $return[] = [
  207. 'number' => $number, //可出数量
  208. 'product_id' => $value['product_id'],
  209. 'title' => $tmp['title'] ?? "",
  210. 'code' => $tmp['code'] ?? "",
  211. 'size' => $tmp['size'] ?? "",
  212. 'unit' => $tmp['unit'] ?? "",
  213. ];
  214. }
  215. return $return;
  216. }
  217. public function needSnFix($data, $user){
  218. if(empty($data['id'])) return [false, '施工单id不能为空'];
  219. //产品
  220. $p_info = ConstructionProductInfo::where('del_time',0)
  221. ->where('construction_id',$data['id'])
  222. ->get()->toArray();
  223. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  224. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  225. $product = [];
  226. //sn码信息
  227. $sn_map = (new DataSyncToU8Service())->getSn($data, ProductSnInfo::type_one);
  228. $service = new DataSyncToU8Service();
  229. $needSnFix = false;
  230. foreach ($p_info as $value){
  231. $tmp = $map[$value['product_id']] ?? [];
  232. $value['title'] = $tmp['title'] ?? "";
  233. $value['code'] = $tmp['code'] ?? "";
  234. $value['size'] = $tmp['size'] ?? "";
  235. $value['unit'] = $tmp['unit'] ?? "";
  236. $value['bar_code'] = $tmp['bar_code'] ?? "";
  237. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  238. $s_t = $sn_map[$value['code']] ?? [];
  239. $sn = array_column($s_t,'sn');
  240. $value['product_sn_info'] = $sn;
  241. $bool = $service->forCheck($tmp['product_category']);
  242. $count = count($sn);
  243. if($bool){
  244. //如果车窗膜已经有sn码 则跳过
  245. if($count > 0) {
  246. $product_type_for_sn = 0;
  247. }else{
  248. if(! $needSnFix) $needSnFix = true;
  249. $product_type_for_sn = 1;
  250. }
  251. }else{
  252. $bool = $service->forCheck2($tmp['product_category']);
  253. if($bool){
  254. //如果除车窗膜外已经有sn码 则跳过
  255. if($count > 0) {
  256. $product_type_for_sn = 0;
  257. }else{
  258. if(! $needSnFix) $needSnFix = true;
  259. $product_type_for_sn = 1;
  260. }
  261. }else{
  262. //其它产品数量达到 则跳过
  263. if($count >= $p_info['number']) {
  264. $product_type_for_sn = 0;
  265. }else{
  266. if(! $needSnFix) $needSnFix = true;
  267. $product_type_for_sn = 2;
  268. }
  269. }
  270. }
  271. $value['product_type_for_sn'] = $product_type_for_sn;
  272. $product[] = $value;
  273. }
  274. if(empty($needSnFix)) return [true, ['needSnFix' => $needSnFix, 'product' => $product]];
  275. return [true, ['needSnFix' => $needSnFix, 'product' => $product]];
  276. }
  277. public function warrantyAdd($data, $user){
  278. list($status, $msg) = $this->warrantyAddRule($data,$user);
  279. if(! $status) return [false, $msg];
  280. list($warranty, $sn) = $msg;
  281. try {
  282. DB::beginTransaction();
  283. Warranty::insert($warranty);
  284. if(! empty($sn)){
  285. $warranty_list = Warranty::where('del_time',0)
  286. ->whereIn("sn",$sn)
  287. ->select('id','sn')
  288. ->get()->toArray();
  289. foreach ($warranty_list as $value){
  290. ProductSnInfo::where('del_time',0)
  291. ->where('sn', $value['sn'])
  292. ->update(['warranty_id' => $value['id']]);
  293. }
  294. }
  295. // if($data['type'] == ProductSnInfo::type_two){
  296. // //同一个合同,多次填写的客户基本信息按最后一次更新为主
  297. // Warranty::where('del_time',0)
  298. // ->where('data_id',$data['data_id'])
  299. // ->where('type', $data['type'])
  300. // ->update(['customer_name' => $data['customer_name'], 'customer_contact' => $data['customer_contact']]);
  301. // }
  302. DB::commit();
  303. }catch (\Throwable $exception){
  304. DB::rollBack();
  305. return [false, $exception->getMessage()];
  306. }
  307. return [true, ''];
  308. }
  309. private function warrantyAddRule($data, $user){
  310. if(empty($data['type'])) return [false, 'type不能为空'];
  311. if(! isset(ProductSnInfo::$type_name[$data['type']])) return [false,'type不存在'];
  312. if(empty($data['data_id'])) return [false, 'data_id不能为空'];
  313. $time = time();
  314. $warranty = $sn_update = [];
  315. if($data['type'] == ProductSnInfo::type_one){
  316. $order = Construction::where('del_time',0)->where('id',$data['data_id'])->first();
  317. if(empty($order)) return [false, '施工单不存在或已被删除'];
  318. $order = $order->toArray();
  319. if($order['state'] != Construction::STATE_FOUR) return [false, '施工单暂未完结,生成质保信息失败'];
  320. if(empty($order['vin_no'])) return [false, '车架号不能为空'];
  321. $bool = $this->isValidVin($order['vin_no']);
  322. if(! $bool) return [false, '车架号错误,请查看原施工单车架号,编辑输入完整车架号'];
  323. if(empty($order['customer_id'])) return [false, '客户信息不存在'];
  324. $customer = Customer::where('del_time', 0)->where('id',$order['customer_id'])->first();
  325. if(empty($customer)) return [false, '客户不存在或已被删除'];
  326. $customer = $customer->toArray();
  327. $info = CustomerInfo::where('del_time',0)
  328. ->where('type',CustomerInfo::type_one)
  329. ->where('customer_id', $customer['id'])
  330. ->where('contact_info', "<>", "")
  331. ->select('contact_info')
  332. ->first();
  333. if(empty($info)) return [false, "客户的联系方式不能为空"];
  334. $info = $info->toArray();
  335. $customer_info = $info['contact_info'];
  336. $product_sn = ProductSnInfo::from('product_sn_info as a')
  337. ->leftJoin('product as b','b.id','a.product_id')
  338. ->select('a.*','b.title','b.warranty_time')
  339. ->where('a.del_time',0)
  340. ->where('a.data_id',$data['data_id'])
  341. ->where('a.type', ProductSnInfo::type_one)
  342. ->where('a.warranty_id', 0)
  343. ->get()->toArray();
  344. if(empty($product_sn)) return [false, '施工单暂无未生成质保信息的sn码'];
  345. $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
  346. //质保单数据 需要更新的sn码信息
  347. foreach ($product_sn as $value){
  348. $warranty[] = [
  349. 'data_id' => $order['id'],
  350. 'data_title' => $order['order_number'],
  351. 'type' => ProductSnInfo::type_one,
  352. 'product_id' => $value['product_id'],
  353. 'code' => $value['code'],
  354. 'title' => $value['title'],
  355. 'sn' => $value['sn'],
  356. 'customer_id' => $order['customer_id'],
  357. 'customer_name' => $customer['title'],
  358. 'customer_contact' => $customer_info,
  359. 'vin_no' => $order['vin_no'],
  360. 'warranty_time' => $value['warranty_time'],
  361. 'construction_site_title' => $construction_site,
  362. 'start_time' => $order['end_time'],
  363. 'crt_time' => $time,
  364. 'crt_id' => $user['id'],
  365. ];
  366. $sn_update[] = $value['sn'];
  367. }
  368. }elseif($data['type'] == ProductSnInfo::type_two){
  369. $order = SalesOrder::where('del_time',0)->where('id',$data['data_id'])->first();
  370. if(empty($order)) return [false, '合同不存在或已被删除'];
  371. $order = $order->toArray();
  372. if(empty($order['customer_id'])) return [false, '客户不能为空'];
  373. $customer_name = Customer::where('id', $order['customer_id'])->value('title');
  374. if(empty($order['customer_contact'])) return [false, '客户联系方式不能为空'];
  375. $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
  376. if(empty($product)) return [false, '合同已全部或暂无可生成质保信息的产品'];
  377. foreach ($product as $value){
  378. for($i = 0 ;$i < $value['number']; $i++){
  379. $warranty[] = [
  380. 'data_id' => $order['id'],
  381. 'data_title' => $order['order_number'],
  382. 'type' => ProductSnInfo::type_two,
  383. 'product_id' => $value['product_id'],
  384. 'code' => $value['code'],
  385. 'title' => $value['title'],
  386. 'sn' => "",
  387. 'customer_id' => $order['customer_id'],
  388. 'customer_name' => $customer_name,
  389. 'customer_contact' => $order['customer_contact'],
  390. 'vin_no' => "",
  391. 'warranty_time' => $value['warranty_time'],
  392. 'construction_site_title' => "",
  393. 'start_time' => $time,
  394. 'crt_time' => $time,
  395. 'crt_id' => $user['id'],
  396. ];
  397. }
  398. }
  399. }else{
  400. return [false, '质保生成类型错误'];
  401. }
  402. return [true, [$warranty, $sn_update]];
  403. }
  404. public function ori()
  405. {//生成质保 合同原来的代码
  406. $order = SalesOrder::where('del_time', 0)->where('id', $data['data_id'])->first();
  407. if (empty($order)) return [false, '合同不存在或已被删除'];
  408. $order = $order->toArray();
  409. if (empty($data['customer_name'])) return [false, '客户名称不能为空'];
  410. if (empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  411. $bool = $this->isValidMobile($data['customer_contact']);
  412. if (!$bool) return [false, '手机号错误,请输入完整的手机号'];
  413. $vin_no = "";
  414. if (!empty($data['vin_no'])) {
  415. $bool = $this->isValidVin($data['vin_no']);
  416. if (!$bool) return [false, '车架号错误,请填写完整车架号'];
  417. $vin_no = $data['vin_no'];
  418. }
  419. if (empty($data['product'])) return [false, '合同无生成质保的产品信息'];
  420. $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
  421. if (empty($product)) return [false, '合同暂无能生成质保信息的产品'];
  422. foreach ($data['product'] as $value) {
  423. $tmp = $product[$value['product_id']] ?? [];
  424. if (empty($tmp)) return [false, '存在不能生成质保的产品'];
  425. if ($tmp['number'] > $value['number']) return [false, '产品编码:' . $tmp['code'] . '本次可生成质保信息的产品最多' . $tmp['number']];
  426. $warranty[] = [
  427. 'data_id' => $order['id'],
  428. 'data_title' => $order['order_number'],
  429. 'type' => ProductSnInfo::type_two,
  430. 'product_id' => $tmp['product_id'],
  431. 'code' => $tmp['code'],
  432. 'title' => $tmp['title'],
  433. 'sn' => "",
  434. 'customer_id' => $order['customer_id'],
  435. 'customer_name' => $data['customer_name'],
  436. 'customer_contact' => $data['customer_contact'],
  437. 'vin_no' => $vin_no,
  438. 'warranty_time' => $tmp['warranty_time'],
  439. 'construction_site_title' => "",
  440. 'start_time' => $time,
  441. 'crt_time' => $time,
  442. 'crt_id' => $user['id'],
  443. ];
  444. }
  445. }
  446. public function saveWarrantyByMyself($data){
  447. list($status, $msg) = $this->saveWarrantyByMyselfRule($data);
  448. if(! $status) return [false, $msg];
  449. list($warranty, $sn) = $msg;
  450. try {
  451. DB::beginTransaction();
  452. $warranty_id = DB::table('warranty')->insertGetId($warranty);
  453. $sn['warranty_id'] = $warranty_id;
  454. ProductSnInfo::insert($sn);
  455. (new DataSyncToU8Service())->updateByVinNo($data);
  456. if(! empty($data['auto_id'])) (new DataSyncToU8Service())->updateSnInfo(1, [$data['auto_id']], 1);
  457. DB::commit();
  458. }catch (\Throwable $exception){
  459. DB::rollBack();
  460. return [false, $exception->getMessage()];
  461. }
  462. return [true, ''];
  463. }
  464. private function saveWarrantyByMyselfRule($data){
  465. if(empty($data['product_id'])) return [false, '产品ID不能为空'];
  466. $product = Product::where('del_time',0)
  467. ->where('id',$data['product_id'])
  468. ->first();
  469. if(empty($product)) return [false, '产品不存在或已被删除'];
  470. $product = $product->toArray();
  471. if(empty($data['start_time'])) return [false, '施工日期不能为空'];
  472. if(empty($product['warranty_time'])) return [false, '质保时长不能为空'];
  473. if(empty($data['sn'])) return [false, '产品序列号不能为空'];
  474. list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  475. if(! $status) return [false, $msg];
  476. //校验sn是否被占用
  477. $submit_info[] = $product['code'] . $data['sn'];
  478. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$product['code']], [$data['sn']], $submit_info);
  479. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  480. if(empty($data['customer_name'])) return [false, '客户名称不能为空'];
  481. if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  482. $bool = $this->isValidMobile($data['customer_contact']);
  483. if(! $bool) return [false, '手机号错误,请填写完整的11位手机号'];
  484. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  485. $bool = $this->isValidVin($data['vin_no']);
  486. if(! $bool) return [false, '车架号错误,请填写完整的17位车架号'];
  487. $time = time();
  488. //质保单数据 sn码数据
  489. $warranty = [
  490. 'data_id' => 0,
  491. 'data_title' => '',
  492. 'type' => ProductSnInfo::type_three,
  493. 'product_id' => $product['id'],
  494. 'code' => $product['code'],
  495. 'title' => $product['title'],
  496. 'sn' => $data['sn'],
  497. 'customer_id' => 0,
  498. 'customer_name' => $data['customer_name'],
  499. 'customer_contact' => $data['customer_contact'],
  500. 'vin_no' => $data['vin_no'],
  501. 'warranty_time' => $product['warranty_time'],
  502. 'construction_site_title' => $data['construction_site_title'] ?? "",
  503. 'start_time' => $data['start_time'],
  504. 'crt_time' => $time,
  505. 'crt_id' => 0,
  506. 'is_active' => Warranty::type_one,
  507. 'sn_type' => 1,
  508. ];
  509. $sn_update = [
  510. 'data_id' => 0,
  511. 'type' => ProductSnInfo::type_three,
  512. 'product_id' => $product['id'],
  513. 'code' => $product['code'],
  514. 'sn' => $data['sn'],
  515. 'crt_time' => $time,
  516. 'warranty_id' => 0,
  517. 'auto_id' => $data['auto_id'] ?? 0,
  518. ];
  519. return [true, [$warranty, $sn_update]];
  520. }
  521. //获取未激活列表
  522. public function getWarrantyNotActiveList($data){
  523. if(empty($data['customer_contact'])) return [false, '手机号不能为空'];
  524. $model = Warranty::where('del_time',0)
  525. ->where('customer_contact',$data['customer_contact'])
  526. ->where('is_active',Warranty::type_zero)
  527. ->whereIn('type',[ProductSnInfo::type_one, ProductSnInfo::type_two])
  528. ->where(function ($query) {
  529. $query->where('type', '!=', ProductSnInfo::type_one)
  530. ->orWhere(function ($q) {
  531. $q->where('type', ProductSnInfo::type_one)
  532. ->where('sn', '<>', '');
  533. });
  534. })//sn限制
  535. ->select('id','data_id','data_title','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time');
  536. $list = $this->limit($model,'', $data);
  537. $list = $this->fillGetWarrantyNotActiveList($list);
  538. return [true, $list];
  539. }
  540. public function fillGetWarrantyNotActiveList($data){
  541. if(empty($data['data'])) return $data;
  542. foreach ($data['data'] as $key => $value){
  543. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  544. }
  545. return $data;
  546. }
  547. //质保激活
  548. public function warrantyActivationCustomer($data){
  549. list($status, $msg) = $this->warrantyActivationCustomerRule($data);
  550. if(! $status) return [false, $msg];
  551. $order = $msg;
  552. try {
  553. DB::beginTransaction();
  554. if($order['type'] == ProductSnInfo::type_two){
  555. $update = [
  556. 'sn' => $data['sn'] ?? '',
  557. 'construction_site_title' => $data['construction_site_title'] ?? '',
  558. 'vin_no' => $data['vin_no'],
  559. 'is_active' => Warranty::type_one
  560. ];
  561. Warranty::where('id',$data['id'])->update($update);
  562. if(! empty($data['sn'])){
  563. $order = Warranty::where('id',$data['id'])->first()->toArray();
  564. $sn_update = [
  565. 'data_id' => $order['data_id'],
  566. 'type' => $order['type'],
  567. 'product_id' => $order['product_id'],
  568. 'code' => $order['code'],
  569. 'sn' => $order['sn'],
  570. 'crt_time' => time(),
  571. 'warranty_id' => $order['id'],
  572. ];
  573. ProductSnInfo::insert($sn_update);
  574. }
  575. }elseif($order['type'] == ProductSnInfo::type_one){
  576. $update = [
  577. 'construction_site_title' => $data['construction_site_title'] ?? '',
  578. 'is_active' => Warranty::type_one
  579. ];
  580. Warranty::where('id',$data['id'])->update($update);
  581. }
  582. (new DataSyncToU8Service())->updateByVinNo($data);
  583. DB::commit();
  584. }catch (\Throwable $exception){
  585. DB::rollBack();
  586. return [false, $exception->getMessage()];
  587. }
  588. return [true, ''];
  589. }
  590. public function warrantyActivationCustomerRule($data){
  591. if(empty($data['id'])) return [false, '质保ID不能为空'];
  592. $order = Warranty::where('del_time',0)
  593. ->where('id', $data['id'])
  594. ->first();
  595. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  596. $order = $order->toArray();
  597. if($order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  598. if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
  599. if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
  600. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  601. if($order['type'] == ProductSnInfo::type_one){
  602. }else{
  603. if(! empty($data['sn'])){
  604. list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  605. if(! $status) return [false, $msg];
  606. $submit_info[] = $order['code'] . $data['sn'];
  607. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info);
  608. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  609. }
  610. $bool = $this->isValidVin($data['vin_no']);
  611. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  612. }
  613. return [true, $order];
  614. }
  615. //后台列表
  616. public function warrantyList($data,$user){
  617. $model = Warranty::where('del_time',0)
  618. ->select('id','data_id','data_title','code','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time','crt_id','is_active','active_id','sy_code')
  619. ->orderBy('id','desc');
  620. if(! empty($data['type'])) $model->where('type', $data['type']);
  621. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  622. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  623. if(! empty($data['data_title'])) $model->where('data_title', 'LIKE', '%'.$data['data_title'].'%');
  624. if(! empty($data['sn'])) $model->where('sn', 'LIKE', '%'.$data['sn'].'%');
  625. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  626. if(! empty($data['customer_contact'])) $model->where('customer_contact', 'LIKE', '%'.$data['customer_contact'].'%');
  627. if(! empty($data['vin_no'])) $model->where('vin_no', 'LIKE', '%'.$data['vin_no'].'%');
  628. if(! empty($data['construction_site_title'])) $model->where('construction_site_title', 'LIKE', '%'.$data['construction_site_title'].'%');
  629. if(! empty($data['warranty_time'])) $model->where('warranty_time', $data['warranty_time']);
  630. if(isset($data['is_active'])) $model->where('is_active', $data['is_active']);
  631. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  632. if(! empty($data['id'])) $model->where('id', $data['id']);
  633. if(! empty($data['sy_code'])) $model->where('sy_code', 'LIKE', '%'.$data['sy_code'].'%');
  634. $list = $this->limit($model,'', $data);
  635. $list = $this->fillWarrantyList($list);
  636. return [true, $list];
  637. }
  638. public function fillWarrantyList($data){
  639. if(empty($data['data'])) return $data;
  640. foreach ($data['data'] as $key => $value){
  641. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  642. $data['data'][$key]['type_title'] = ProductSnInfo::$type_name[$value['type']] ?? "";
  643. }
  644. return $data;
  645. }
  646. //后台质保编辑以及激活
  647. public function warrantyEditAndActivation($data,$user){
  648. list($status, $msg) = $this->warrantyEditAndActivationRule($data);
  649. if(! $status) return [false, $msg];
  650. $order = $msg;
  651. try {
  652. DB::beginTransaction();
  653. $time = time();
  654. $sn = $data['sn'] ?? "";
  655. $order_sn = $order['sn'];
  656. if(! empty($order_sn) && ! empty($sn) && $order_sn != $sn){
  657. ProductSnInfo::where('del_time',0)
  658. ->where('data_id', $order['data_id'])
  659. ->where('type', $order['type'])
  660. ->where('sn', $order_sn)
  661. ->update(['del_time' => $time]);
  662. $sn_update = [
  663. 'data_id' => $order['data_id'],
  664. 'type' => $order['type'],
  665. 'product_id' => $order['product_id'],
  666. 'code' => $order['code'],
  667. 'sn' => $order['sn'],
  668. 'crt_time' => time(),
  669. 'warranty_id' => $order['id'],
  670. ];
  671. ProductSnInfo::insert($sn_update);
  672. }
  673. $update = [
  674. 'sn' => $sn,
  675. 'construction_site_title' => $data['construction_site_title'] ?? '',
  676. 'vin_no' => $data['vin_no'],
  677. 'start_time' => $data['start_time'],
  678. 'customer_name' => $data['customer_name'],
  679. 'customer_contact' => $data['customer_contact'],
  680. 'warranty_time' => $data['warranty_time'],
  681. ];
  682. if(empty($data['is_active'])) {
  683. $update['is_active'] = Warranty::type_one;
  684. $update['active_id'] = $user['id'];
  685. }
  686. Warranty::where('id',$data['id'])->update($update);
  687. (new DataSyncToU8Service())->updateByVinNo($data);
  688. DB::commit();
  689. }catch (\Throwable $exception){
  690. DB::rollBack();
  691. return [false, $exception->getMessage()];
  692. }
  693. return [true, ''];
  694. }
  695. public function warrantyEditAndActivationRule(&$data){
  696. if(empty($data['id'])) return [false, '质保ID不能为空'];
  697. $order = Warranty::where('del_time',0)
  698. ->where('id', $data['id'])
  699. ->first();
  700. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  701. $order = $order->toArray();
  702. // if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  703. if(empty($data['start_time'])) return [false, '请填写施工日期/生效日期'];
  704. $data['start_time'] = $this->changeDateToDateMin($data['start_time']);
  705. if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
  706. if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
  707. if(empty($data['warranty_time'])) return [false, '质保时长(月)不能为空'];
  708. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  709. $bool = $this->isValidVin($data['vin_no']);
  710. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  711. if(! empty($data['sn'])){
  712. // list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  713. // if(! $status) return [false, $msg];
  714. $submit_info[] = $order['code'] . $data['sn'];
  715. $data_id = $order['data_id'];
  716. if($order['type'] == ProductSnInfo::type_three) $data_id = $order['id'];
  717. $message = [
  718. 'id' => $data_id,
  719. 'data_type' => $order['type']
  720. ];
  721. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info, $message);
  722. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  723. }
  724. return [true, $order];
  725. }
  726. //后台质保激活
  727. public function warrantyActivation($data,$user){
  728. list($status, $msg) = $this->warrantyActivationRule($data);
  729. if(! $status) return [false, $msg];
  730. $order = $msg;
  731. try {
  732. DB::beginTransaction();
  733. $update['is_active'] = Warranty::type_one;
  734. $update['active_id'] = $user['id'];
  735. Warranty::where('id', $data['id'])->update($update);
  736. (new DataSyncToU8Service())->updateByVinNo($order);
  737. DB::commit();
  738. }catch (\Throwable $exception){
  739. DB::rollBack();
  740. return [false, $exception->getMessage()];
  741. }
  742. return [true, ''];
  743. }
  744. public function warrantyActivationRule($data){
  745. if(empty($data['id'])) return [false, '质保ID不能为空'];
  746. $order = Warranty::where('del_time',0)
  747. ->where('id', $data['id'])
  748. ->first();
  749. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  750. $order = $order->toArray();
  751. if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  752. if(empty($order['start_time'])) return [false, '施工日期/生效日期不能为空'];
  753. if(empty($order['customer_name'])) return [false, '车主姓名不能为空'];
  754. if(empty($order['customer_contact'])) return [false, '联系方式不能为空'];
  755. if(empty($order['warranty_time'])) return [false, '质保时长(月)不能为空'];
  756. if(empty($order['vin_no'])) return [false, '车架号不能为空'];
  757. $bool = $this->isValidVin($order['vin_no']);
  758. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  759. return [true, $order];
  760. }
  761. //质保卡作废
  762. public function warrantyDel($data,$user){
  763. list($status, $msg) = $this->warrantyDelRule($data);
  764. if(! $status) return [false, $msg];
  765. $order = $msg;
  766. $time = time();
  767. try {
  768. DB::beginTransaction();
  769. Warranty::where('id', $data['id'])->update(['del_time' => $time]);
  770. if(! empty($order['sn'])) ProductSnInfo::where('del_time',0)->where('sn', $order['sn'])->update(['del_time' => $time]);
  771. DB::commit();
  772. }catch (\Throwable $exception){
  773. DB::rollBack();
  774. return [false, $exception->getMessage()];
  775. }
  776. return [true, ''];
  777. }
  778. public function warrantyDelRule($data){
  779. if(empty($data['id'])) return [false, '质保ID不能为空'];
  780. $order = Warranty::where('del_time',0)
  781. ->where('id', $data['id'])
  782. ->first();
  783. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  784. $order = $order->toArray();
  785. return [true, $order];
  786. }
  787. public function searchWarranty($data){
  788. list($status, $msg) = $this->searchWarrantyRule($data);
  789. if(! $status) return [false, $msg];
  790. list($search_field, $search) = $msg;
  791. $order_list = Warranty::where('del_time',0)
  792. ->when($search_field == 1, function ($query) use ($search) {
  793. return $query->where("customer_contact", $search);
  794. })
  795. ->when($search_field == 2, function ($query) use ($search) {
  796. return $query->where("vin_no", $search);
  797. })
  798. ->where(function ($query) {
  799. $query->where('type', '!=', ProductSnInfo::type_one)
  800. ->orWhere(function ($q) {
  801. $q->where('type', ProductSnInfo::type_one)
  802. ->where('sn', '<>', '');
  803. });
  804. })//sn限制
  805. ->orderBy('id','desc')
  806. ->get()->toArray();
  807. if(empty($order_list)) return [false , "暂无质保信息"];
  808. $first = $order_list[0];
  809. $order['main'] = [
  810. 'customer_name' => $first['customer_name'],
  811. 'customer_contact' => $first['customer_contact'],
  812. 'vin_no' => $first['vin_no'],
  813. ];
  814. foreach ($order_list as $key => $value){
  815. $order_list[$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  816. }
  817. $order['list'] = $order_list;
  818. return [true, $order];
  819. }
  820. public function searchWarrantyRule($data){
  821. if(empty($data['from'])) return [false, '查询来源不能为空'];
  822. $search = $data['customer_contact_or_vin_no'];
  823. if($data['from'] == 1){
  824. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号不能为空'];
  825. $length = strlen($data['customer_contact_or_vin_no']);
  826. if($length != 11) return [false, '请输入完整的11位手机号'];
  827. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  828. if(! $status) return [false, $msg];
  829. $search_field = 1;
  830. }elseif($data['from'] == 2){
  831. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号或车架号不能为空'];
  832. $length = strlen($data['customer_contact_or_vin_no']);
  833. if($length != 11 && $length != 17) return [false, '请输入完整的11位手机号或完整的17位车架号'];
  834. if($length == 11){
  835. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  836. if(! $status) return [false, $msg];
  837. $search_field = 1;
  838. }else{
  839. $search_field = 2;
  840. }
  841. }else{
  842. return [false, '查询来源错误'];
  843. }
  844. return [true, [$search_field, $search]];
  845. }
  846. public function searchWarrantyCommon($customer_contact){
  847. $vinCount = Warranty::where('del_time', 0)
  848. ->where('customer_contact', $customer_contact)
  849. ->where('vin_no', '<>', '')
  850. ->distinct()
  851. ->count('vin_no');
  852. if($vinCount > 1) return [false, '手机号:' . $customer_contact . '下有个多个车架号,请去质保查询功能按需查询质保信息'];
  853. return [true, ''];
  854. }
  855. public function warrantyAddNew($order, $data, $user){
  856. try {
  857. DB::beginTransaction();
  858. $data['id'] = $order['id'];
  859. $time = time();
  860. //保存sn关联关系
  861. (new DataSyncToU8Service())->saveSn($data, ProductSnInfo::type_one, $time);
  862. $product_sn = ProductSnInfo::from('product_sn_info as a')
  863. ->leftJoin('product as b','b.id','a.product_id')
  864. ->select('a.*','b.title','b.warranty_time')
  865. ->where('a.del_time',0)
  866. ->where('a.data_id',$data['id'])
  867. ->where('a.type', ProductSnInfo::type_one)
  868. ->where('a.warranty_id', 0)
  869. ->get()->toArray();
  870. $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
  871. //生成质保单数据 需要更新的sn码信息
  872. if(! empty($product_sn)){
  873. $warranty = $sn_update = [];
  874. foreach ($product_sn as $value){
  875. $warranty[] = [
  876. 'data_id' => $order['id'],
  877. 'data_title' => $order['order_number'],
  878. 'type' => ProductSnInfo::type_one,
  879. 'product_id' => $value['product_id'],
  880. 'code' => $value['code'],
  881. 'title' => $value['title'],
  882. 'sn' => $value['sn'],
  883. 'customer_id' => $order['customer_id'],
  884. 'customer_name' => $order['customer_title'],
  885. 'customer_contact' => $order['customer_info'],
  886. 'vin_no' => $order['vin_no'],
  887. 'warranty_time' => $value['warranty_time'],
  888. 'construction_site_title' => $construction_site,
  889. 'start_time' => $order['end_time'],
  890. 'crt_time' => $time,
  891. 'crt_id' => $user['id'],
  892. 'sn_type' => $value['sn_type'],
  893. 'sy_code' => ""
  894. ];
  895. $sn_update[] = $value['sn'];
  896. }
  897. if(! empty($warranty)) Warranty::insert($warranty);
  898. if(! empty($sn_update)){
  899. $warranty_list = Warranty::where('del_time',0)
  900. ->whereIn("sn",$sn_update)
  901. ->select('id','sn')
  902. ->get()->toArray();
  903. foreach ($warranty_list as $value){
  904. ProductSnInfo::where('del_time',0)
  905. ->where('sn', $value['sn'])
  906. ->update(['warranty_id' => $value['id']]);
  907. }
  908. }
  909. }
  910. //生成溯源码质保卡
  911. $insert2 = $this->getInsert($data,$order,$construction_site,$time,$user);
  912. if(! empty($insert2)) Warranty::insert($insert2);
  913. DB::commit();
  914. }catch (\Throwable $exception){
  915. DB::rollBack();
  916. return [false, $exception->getMessage()];
  917. }
  918. return [true, ''];
  919. }
  920. public function getInsert($data,$order,$construction_site,$time,$user){
  921. $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
  922. $service = new DataSyncToU8Service();
  923. $insert2 = [];
  924. foreach ($data['product'] as $value){
  925. $tmp = $map[$value['product_id']] ?? [];
  926. $bool = $service->forCheck($tmp['product_category']);
  927. $value['warranty_time'] = $tmp['warranty_time'];
  928. //没有sn码信息直接返回
  929. if(empty($value['product_sn_info'])) {
  930. if($bool){
  931. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  932. }else{
  933. $bool = $service->forCheck2($tmp['product_category']);
  934. if($bool){
  935. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  936. }else{
  937. for ($i = 0; $i < intval($value['number']); $i++){
  938. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  939. }
  940. }
  941. }
  942. }else{
  943. $num = count(array_column($value['product_sn_info'],'sn'));
  944. if($bool){
  945. if($num < 1){
  946. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  947. }
  948. }else{
  949. $bool = $service->forCheck2($tmp['product_category']);
  950. if($bool){
  951. if($num < 1){
  952. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  953. }
  954. }else{
  955. $n = bcsub($value['number'], $num,2);
  956. if(intval($n) > 0){
  957. for ($i = 0; $i < intval($value['number']); $i++){
  958. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  959. }
  960. }
  961. }
  962. }
  963. }
  964. }
  965. return $insert2;
  966. }
  967. public function fillInsert(&$insert2, $order, $value, $construction_site,$time,$user){
  968. $insert2[] = [
  969. 'data_id' => $order['id'],
  970. 'data_title' => $order['order_number'],
  971. 'type' => ProductSnInfo::type_one,
  972. 'product_id' => $value['product_id'],
  973. 'code' => $value['code'],
  974. 'title' => $value['title'],
  975. 'sn' => "",
  976. 'customer_id' => $order['customer_id'],
  977. 'customer_name' => $order['customer_title'],
  978. 'customer_contact' => $order['customer_info'],
  979. 'vin_no' => $order['vin_no'],
  980. 'warranty_time' => $value['warranty_time'],
  981. 'construction_site_title' => $construction_site,
  982. 'start_time' => $order['end_time'],
  983. 'crt_time' => $time,
  984. 'crt_id' => $user['id'],
  985. // 'sn_type' => $value['sn_type'] ?? 0,
  986. 'sy_code' => $value['sy_code'] ?? "",
  987. ];
  988. }
  989. }