TSpaceService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. foreach ($product as $value){
  194. $category = json_decode($value['product_category'],true);
  195. //车窗膜不管数量算一个
  196. if(in_array(ProductCategory::Special_for_sn, $category)) $value['number'] = 1;
  197. //合同质保产品
  198. $p1 = $product_map1[$value['product_id']] ?? 0;
  199. //合同退货产品
  200. $p2 = $product_map2[$value['product_id']] ?? 0;
  201. $number = bcsub($value['number'], $p1,2);
  202. $number = bcsub($number, $p2,2);
  203. if($number <= 0) continue;
  204. $tmp = $map[$value['product_id']] ?? [];
  205. $return[] = [
  206. 'number' => $number, //可出数量
  207. 'product_id' => $value['product_id'],
  208. 'title' => $tmp['title'] ?? "",
  209. 'code' => $tmp['code'] ?? "",
  210. 'size' => $tmp['size'] ?? "",
  211. 'unit' => $tmp['unit'] ?? "",
  212. ];
  213. }
  214. return $return;
  215. }
  216. public function warrantyAdd($data, $user){
  217. list($status, $msg) = $this->warrantyAddRule($data,$user);
  218. if(! $status) return [false, $msg];
  219. list($warranty, $sn) = $msg;
  220. try {
  221. DB::beginTransaction();
  222. Warranty::insert($warranty);
  223. if(! empty($sn)){
  224. $warranty_list = Warranty::where('del_time',0)
  225. ->whereIn("sn",$sn)
  226. ->select('id','sn')
  227. ->get()->toArray();
  228. foreach ($warranty_list as $value){
  229. ProductSnInfo::where('del_time',0)
  230. ->where('sn', $value['sn'])
  231. ->update(['warranty_id' => $value['id']]);
  232. }
  233. }
  234. // if($data['type'] == ProductSnInfo::type_two){
  235. // //同一个合同,多次填写的客户基本信息按最后一次更新为主
  236. // Warranty::where('del_time',0)
  237. // ->where('data_id',$data['data_id'])
  238. // ->where('type', $data['type'])
  239. // ->update(['customer_name' => $data['customer_name'], 'customer_contact' => $data['customer_contact']]);
  240. // }
  241. DB::commit();
  242. }catch (\Throwable $exception){
  243. DB::rollBack();
  244. return [false, $exception->getMessage()];
  245. }
  246. return [true, ''];
  247. }
  248. private function warrantyAddRule($data, $user){
  249. if(empty($data['type'])) return [false, 'type不能为空'];
  250. if(! isset(ProductSnInfo::$type_name[$data['type']])) return [false,'type不存在'];
  251. if(empty($data['data_id'])) return [false, 'data_id不能为空'];
  252. $time = time();
  253. $warranty = $sn_update = [];
  254. if($data['type'] == ProductSnInfo::type_one){
  255. $order = Construction::where('del_time',0)->where('id',$data['data_id'])->first();
  256. if(empty($order)) return [false, '施工单不存在或已被删除'];
  257. $order = $order->toArray();
  258. if($order['state'] != Construction::STATE_FOUR) return [false, '施工单暂未完结,生成质保信息失败'];
  259. if(empty($order['vin_no'])) return [false, '车架号不能为空'];
  260. $bool = $this->isValidVin($order['vin_no']);
  261. if(! $bool) return [false, '车架号错误,请查看原施工单车架号,编辑输入完整车架号'];
  262. if(empty($order['customer_id'])) return [false, '客户信息不存在'];
  263. $customer = Customer::where('del_time', 0)->where('id',$order['customer_id'])->first();
  264. if(empty($customer)) return [false, '客户不存在或已被删除'];
  265. $customer = $customer->toArray();
  266. $info = CustomerInfo::where('del_time',0)
  267. ->where('type',CustomerInfo::type_one)
  268. ->where('customer_id', $customer['id'])
  269. ->where('contact_info', "<>", "")
  270. ->select('contact_info')
  271. ->first();
  272. if(empty($info)) return [false, "客户的联系方式不能为空"];
  273. $info = $info->toArray();
  274. $customer_info = $info['contact_info'];
  275. $product_sn = ProductSnInfo::from('product_sn_info as a')
  276. ->leftJoin('product as b','b.id','a.product_id')
  277. ->select('a.*','b.title','b.warranty_time')
  278. ->where('a.del_time',0)
  279. ->where('a.data_id',$data['data_id'])
  280. ->where('a.type', ProductSnInfo::type_one)
  281. ->where('a.warranty_id', 0)
  282. ->get()->toArray();
  283. if(empty($product_sn)) return [false, '施工单暂无未生成质保信息的sn码'];
  284. $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
  285. //质保单数据 需要更新的sn码信息
  286. foreach ($product_sn as $value){
  287. $warranty[] = [
  288. 'data_id' => $order['id'],
  289. 'data_title' => $order['order_number'],
  290. 'type' => ProductSnInfo::type_one,
  291. 'product_id' => $value['product_id'],
  292. 'code' => $value['code'],
  293. 'title' => $value['title'],
  294. 'sn' => $value['sn'],
  295. 'customer_id' => $order['customer_id'],
  296. 'customer_name' => $customer['title'],
  297. 'customer_contact' => $customer_info,
  298. 'vin_no' => $order['vin_no'],
  299. 'warranty_time' => $value['warranty_time'],
  300. 'construction_site_title' => $construction_site,
  301. 'start_time' => $order['end_time'],
  302. 'crt_time' => $time,
  303. 'crt_id' => $user['id'],
  304. ];
  305. $sn_update[] = $value['sn'];
  306. }
  307. }elseif($data['type'] == ProductSnInfo::type_two){
  308. $order = SalesOrder::where('del_time',0)->where('id',$data['data_id'])->first();
  309. if(empty($order)) return [false, '合同不存在或已被删除'];
  310. $order = $order->toArray();
  311. if(empty($order['customer_id'])) return [false, '客户不能为空'];
  312. $customer_name = Customer::where('id', $order['customer_id'])->value('title');
  313. if(empty($order['customer_contact'])) return [false, '客户联系方式不能为空'];
  314. $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
  315. if(empty($product)) return [false, '合同已全部或暂无可生成质保信息的产品'];
  316. foreach ($product as $value){
  317. for($i = 0 ;$i < $value['number']; $i++){
  318. $warranty[] = [
  319. 'data_id' => $order['id'],
  320. 'data_title' => $order['order_number'],
  321. 'type' => ProductSnInfo::type_two,
  322. 'product_id' => $value['product_id'],
  323. 'code' => $value['code'],
  324. 'title' => $value['title'],
  325. 'sn' => "",
  326. 'customer_id' => $order['customer_id'],
  327. 'customer_name' => $customer_name,
  328. 'customer_contact' => $order['customer_contact'],
  329. 'vin_no' => "",
  330. 'warranty_time' => $value['warranty_time'],
  331. 'construction_site_title' => "",
  332. 'start_time' => $time,
  333. 'crt_time' => $time,
  334. 'crt_id' => $user['id'],
  335. ];
  336. }
  337. }
  338. }else{
  339. return [false, '质保生成类型错误'];
  340. }
  341. return [true, [$warranty, $sn_update]];
  342. }
  343. public function ori()
  344. {//生成质保 合同原来的代码
  345. $order = SalesOrder::where('del_time', 0)->where('id', $data['data_id'])->first();
  346. if (empty($order)) return [false, '合同不存在或已被删除'];
  347. $order = $order->toArray();
  348. if (empty($data['customer_name'])) return [false, '客户名称不能为空'];
  349. if (empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  350. $bool = $this->isValidMobile($data['customer_contact']);
  351. if (!$bool) return [false, '手机号错误,请输入完整的手机号'];
  352. $vin_no = "";
  353. if (!empty($data['vin_no'])) {
  354. $bool = $this->isValidVin($data['vin_no']);
  355. if (!$bool) return [false, '车架号错误,请填写完整车架号'];
  356. $vin_no = $data['vin_no'];
  357. }
  358. if (empty($data['product'])) return [false, '合同无生成质保的产品信息'];
  359. $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
  360. if (empty($product)) return [false, '合同暂无能生成质保信息的产品'];
  361. foreach ($data['product'] as $value) {
  362. $tmp = $product[$value['product_id']] ?? [];
  363. if (empty($tmp)) return [false, '存在不能生成质保的产品'];
  364. if ($tmp['number'] > $value['number']) return [false, '产品编码:' . $tmp['code'] . '本次可生成质保信息的产品最多' . $tmp['number']];
  365. $warranty[] = [
  366. 'data_id' => $order['id'],
  367. 'data_title' => $order['order_number'],
  368. 'type' => ProductSnInfo::type_two,
  369. 'product_id' => $tmp['product_id'],
  370. 'code' => $tmp['code'],
  371. 'title' => $tmp['title'],
  372. 'sn' => "",
  373. 'customer_id' => $order['customer_id'],
  374. 'customer_name' => $data['customer_name'],
  375. 'customer_contact' => $data['customer_contact'],
  376. 'vin_no' => $vin_no,
  377. 'warranty_time' => $tmp['warranty_time'],
  378. 'construction_site_title' => "",
  379. 'start_time' => $time,
  380. 'crt_time' => $time,
  381. 'crt_id' => $user['id'],
  382. ];
  383. }
  384. }
  385. public function saveWarrantyByMyself($data){
  386. list($status, $msg) = $this->saveWarrantyByMyselfRule($data);
  387. if(! $status) return [false, $msg];
  388. list($warranty, $sn) = $msg;
  389. try {
  390. DB::beginTransaction();
  391. $warranty_id = DB::table('warranty')->insertGetId($warranty);
  392. $sn['warranty_id'] = $warranty_id;
  393. ProductSnInfo::insert($sn);
  394. (new DataSyncToU8Service())->updateByVinNo($data);
  395. DB::commit();
  396. }catch (\Throwable $exception){
  397. DB::rollBack();
  398. return [false, $exception->getMessage()];
  399. }
  400. return [true, ''];
  401. }
  402. private function saveWarrantyByMyselfRule($data){
  403. if(empty($data['product_id'])) return [false, '产品ID不能为空'];
  404. $product = Product::where('del_time',0)
  405. ->where('id',$data['product_id'])
  406. ->first();
  407. if(empty($product)) return [false, '产品不存在或已被删除'];
  408. $product = $product->toArray();
  409. if(empty($data['start_time'])) return [false, '施工日期不能为空'];
  410. if(empty($product['warranty_time'])) return [false, '质保时长不能为空'];
  411. if(empty($data['sn'])) return [false, '产品序列号不能为空'];
  412. list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  413. if(! $status) return [false, $msg];
  414. //校验sn是否被占用
  415. $submit_info[] = $product['code'] . $data['sn'];
  416. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$product['code']], [$data['sn']], $submit_info);
  417. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  418. if(empty($data['customer_name'])) return [false, '客户名称不能为空'];
  419. if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
  420. $bool = $this->isValidMobile($data['customer_contact']);
  421. if(! $bool) return [false, '手机号错误,请填写完整的11位手机号'];
  422. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  423. $bool = $this->isValidVin($data['vin_no']);
  424. if(! $bool) return [false, '车架号错误,请填写完整的17位车架号'];
  425. $time = time();
  426. //质保单数据 sn码数据
  427. $warranty = [
  428. 'data_id' => 0,
  429. 'data_title' => '',
  430. 'type' => ProductSnInfo::type_three,
  431. 'product_id' => $product['id'],
  432. 'code' => $product['code'],
  433. 'title' => $product['title'],
  434. 'sn' => $data['sn'],
  435. 'customer_id' => 0,
  436. 'customer_name' => $data['customer_name'],
  437. 'customer_contact' => $data['customer_contact'],
  438. 'vin_no' => $data['vin_no'],
  439. 'warranty_time' => $product['warranty_time'],
  440. 'construction_site_title' => $data['construction_site_title'] ?? "",
  441. 'start_time' => $data['start_time'],
  442. 'crt_time' => $time,
  443. 'crt_id' => 0,
  444. 'is_active' => Warranty::type_one,
  445. ];
  446. $sn_update = [
  447. 'data_id' => 0,
  448. 'type' => ProductSnInfo::type_three,
  449. 'product_id' => $product['id'],
  450. 'code' => $product['code'],
  451. 'sn' => $data['sn'],
  452. 'crt_time' => $time,
  453. 'warranty_id' => 0,
  454. ];
  455. return [true, [$warranty, $sn_update]];
  456. }
  457. //获取未激活列表
  458. public function getWarrantyNotActiveList($data){
  459. if(empty($data['customer_contact'])) return [false, '手机号不能为空'];
  460. $model = Warranty::where('del_time',0)
  461. ->where('customer_contact',$data['customer_contact'])
  462. ->where('is_active',Warranty::type_zero)
  463. ->whereIn('type',[ProductSnInfo::type_one, ProductSnInfo::type_two])
  464. ->select('id','data_id','data_title','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time');
  465. $list = $this->limit($model,'', $data);
  466. $list = $this->fillGetWarrantyNotActiveList($list);
  467. return [true, $list];
  468. }
  469. public function fillGetWarrantyNotActiveList($data){
  470. if(empty($data['data'])) return $data;
  471. foreach ($data['data'] as $key => $value){
  472. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  473. }
  474. return $data;
  475. }
  476. //质保激活
  477. public function warrantyActivationCustomer($data){
  478. list($status, $msg) = $this->warrantyActivationCustomerRule($data);
  479. if(! $status) return [false, $msg];
  480. $order = $msg;
  481. try {
  482. DB::beginTransaction();
  483. if($order['type'] == ProductSnInfo::type_two){
  484. $update = [
  485. 'sn' => $data['sn'] ?? '',
  486. 'construction_site_title' => $data['construction_site_title'] ?? '',
  487. 'vin_no' => $data['vin_no'],
  488. 'is_active' => Warranty::type_one
  489. ];
  490. Warranty::where('id',$data['id'])->update($update);
  491. if(! empty($data['sn'])){
  492. $order = Warranty::where('id',$data['id'])->first()->toArray();
  493. $sn_update = [
  494. 'data_id' => $order['data_id'],
  495. 'type' => $order['type'],
  496. 'product_id' => $order['product_id'],
  497. 'code' => $order['code'],
  498. 'sn' => $order['sn'],
  499. 'crt_time' => time(),
  500. 'warranty_id' => $order['id'],
  501. ];
  502. ProductSnInfo::insert($sn_update);
  503. }
  504. }
  505. (new DataSyncToU8Service())->updateByVinNo($data);
  506. DB::commit();
  507. }catch (\Throwable $exception){
  508. DB::rollBack();
  509. return [false, $exception->getMessage()];
  510. }
  511. return [true, ''];
  512. }
  513. public function warrantyActivationCustomerRule($data){
  514. if(empty($data['id'])) return [false, '质保ID不能为空'];
  515. $order = Warranty::where('del_time',0)
  516. ->where('id', $data['id'])
  517. ->first();
  518. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  519. $order = $order->toArray();
  520. if($order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  521. if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
  522. if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
  523. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  524. if($order['type'] == ProductSnInfo::type_one){
  525. }else{
  526. if(! empty($data['sn'])){
  527. list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  528. if(! $status) return [false, $msg];
  529. $submit_info[] = $order['code'] . $data['sn'];
  530. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info);
  531. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  532. }
  533. $bool = $this->isValidVin($data['vin_no']);
  534. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  535. }
  536. return [true, $order];
  537. }
  538. //后台列表
  539. public function warrantyList($data,$user){
  540. $model = Warranty::where('del_time',0)
  541. ->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')
  542. ->orderBy('id','desc');
  543. if(! empty($data['type'])) $model->where('type', $data['type']);
  544. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  545. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  546. if(! empty($data['data_title'])) $model->where('data_title', 'LIKE', '%'.$data['data_title'].'%');
  547. if(! empty($data['sn'])) $model->where('sn', 'LIKE', '%'.$data['sn'].'%');
  548. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  549. if(! empty($data['customer_contact'])) $model->where('customer_contact', 'LIKE', '%'.$data['customer_contact'].'%');
  550. if(! empty($data['vin_no'])) $model->where('vin_no', 'LIKE', '%'.$data['vin_no'].'%');
  551. if(! empty($data['construction_site_title'])) $model->where('construction_site_title', 'LIKE', '%'.$data['construction_site_title'].'%');
  552. if(! empty($data['warranty_time'])) $model->where('warranty_time', $data['warranty_time']);
  553. if(isset($data['is_active'])) $model->where('is_active', $data['is_active']);
  554. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  555. if(! empty($data['id'])) $model->where('id', $data['id']);
  556. $list = $this->limit($model,'', $data);
  557. $list = $this->fillWarrantyList($list);
  558. return [true, $list];
  559. }
  560. public function fillWarrantyList($data){
  561. if(empty($data['data'])) return $data;
  562. foreach ($data['data'] as $key => $value){
  563. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  564. $data['data'][$key]['type_title'] = ProductSnInfo::$type_name[$value['type']] ?? "";
  565. }
  566. return $data;
  567. }
  568. //后台质保编辑以及激活
  569. public function warrantyEditAndActivation($data,$user){
  570. list($status, $msg) = $this->warrantyEditAndActivationRule($data);
  571. if(! $status) return [false, $msg];
  572. $order = $msg;
  573. try {
  574. DB::beginTransaction();
  575. $time = time();
  576. $sn = $data['sn'] ?? "";
  577. $order_sn = $order['sn'];
  578. if(! empty($order_sn) && ! empty($sn) && $order_sn != $sn){
  579. ProductSnInfo::where('del_time',0)
  580. ->where('data_id', $order['data_id'])
  581. ->where('type', $order['type'])
  582. ->where('sn', $order_sn)
  583. ->update(['del_time' => $time]);
  584. $sn_update = [
  585. 'data_id' => $order['data_id'],
  586. 'type' => $order['type'],
  587. 'product_id' => $order['product_id'],
  588. 'code' => $order['code'],
  589. 'sn' => $order['sn'],
  590. 'crt_time' => time(),
  591. 'warranty_id' => $order['id'],
  592. ];
  593. ProductSnInfo::insert($sn_update);
  594. }
  595. $update = [
  596. 'sn' => $sn,
  597. 'construction_site_title' => $data['construction_site_title'] ?? '',
  598. 'vin_no' => $data['vin_no'],
  599. 'start_time' => $data['start_time'],
  600. 'customer_name' => $data['customer_name'],
  601. 'customer_contact' => $data['customer_contact'],
  602. 'warranty_time' => $data['warranty_time'],
  603. ];
  604. if(empty($data['is_active'])) {
  605. $update['is_active'] = Warranty::type_one;
  606. $update['active_id'] = $user['id'];
  607. }
  608. Warranty::where('id',$data['id'])->update($update);
  609. (new DataSyncToU8Service())->updateByVinNo($data);
  610. DB::commit();
  611. }catch (\Throwable $exception){
  612. DB::rollBack();
  613. return [false, $exception->getMessage()];
  614. }
  615. return [true, ''];
  616. }
  617. public function warrantyEditAndActivationRule(&$data){
  618. if(empty($data['id'])) return [false, '质保ID不能为空'];
  619. $order = Warranty::where('del_time',0)
  620. ->where('id', $data['id'])
  621. ->first();
  622. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  623. $order = $order->toArray();
  624. // if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  625. if(empty($data['start_time'])) return [false, '请填写施工日期/生效日期'];
  626. $data['start_time'] = $this->changeDateToDateMin($data['start_time']);
  627. if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
  628. if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
  629. if(empty($data['warranty_time'])) return [false, '质保时长(月)不能为空'];
  630. if(empty($data['vin_no'])) return [false, '车架号不能为空'];
  631. $bool = $this->isValidVin($data['vin_no']);
  632. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  633. if(! empty($data['sn'])){
  634. list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
  635. if(! $status) return [false, $msg];
  636. $submit_info[] = $order['code'] . $data['sn'];
  637. $message = [
  638. 'id' => $order['data_id'],
  639. 'data_type' => $order['type']
  640. ];
  641. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info, $message);
  642. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  643. }
  644. return [true, $order];
  645. }
  646. //后台质保激活
  647. public function warrantyActivation($data,$user){
  648. list($status, $msg) = $this->warrantyActivationRule($data);
  649. if(! $status) return [false, $msg];
  650. $order = $msg;
  651. try {
  652. DB::beginTransaction();
  653. $update['is_active'] = Warranty::type_one;
  654. $update['active_id'] = $user['id'];
  655. Warranty::where('id', $data['id'])->update($update);
  656. (new DataSyncToU8Service())->updateByVinNo($order);
  657. DB::commit();
  658. }catch (\Throwable $exception){
  659. DB::rollBack();
  660. return [false, $exception->getMessage()];
  661. }
  662. return [true, ''];
  663. }
  664. public function warrantyActivationRule($data){
  665. if(empty($data['id'])) return [false, '质保ID不能为空'];
  666. $order = Warranty::where('del_time',0)
  667. ->where('id', $data['id'])
  668. ->first();
  669. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  670. $order = $order->toArray();
  671. if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  672. if(empty($order['start_time'])) return [false, '施工日期/生效日期不能为空'];
  673. if(empty($order['customer_name'])) return [false, '车主姓名不能为空'];
  674. if(empty($order['customer_contact'])) return [false, '联系方式不能为空'];
  675. if(empty($order['warranty_time'])) return [false, '质保时长(月)不能为空'];
  676. if(empty($order['vin_no'])) return [false, '车架号不能为空'];
  677. $bool = $this->isValidVin($order['vin_no']);
  678. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  679. return [true, $order];
  680. }
  681. //质保卡作废
  682. public function warrantyDel($data,$user){
  683. list($status, $msg) = $this->warrantyDelRule($data);
  684. if(! $status) return [false, $msg];
  685. $order = $msg;
  686. $time = time();
  687. try {
  688. DB::beginTransaction();
  689. Warranty::where('id', $data['id'])->update(['del_time' => $time]);
  690. if(! empty($order['sn'])) ProductSnInfo::where('del_time',0)->where('sn', $order['sn'])->update(['del_time' => $time]);
  691. DB::commit();
  692. }catch (\Throwable $exception){
  693. DB::rollBack();
  694. return [false, $exception->getMessage()];
  695. }
  696. return [true, ''];
  697. }
  698. public function warrantyDelRule($data){
  699. if(empty($data['id'])) return [false, '质保ID不能为空'];
  700. $order = Warranty::where('del_time',0)
  701. ->where('id', $data['id'])
  702. ->first();
  703. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  704. $order = $order->toArray();
  705. return [true, $order];
  706. }
  707. public function searchWarranty($data){
  708. list($status, $msg) = $this->searchWarrantyRule($data);
  709. if(! $status) return [false, $msg];
  710. list($search_field, $search) = $msg;
  711. $order_list = Warranty::where('del_time',0)
  712. ->when($search_field == 1, function ($query) use ($search) {
  713. return $query->where("customer_contact", $search);
  714. })
  715. ->when($search_field == 2, function ($query) use ($search) {
  716. return $query->where("vin_no", $search);
  717. })
  718. ->orderBy('id','desc')
  719. ->get()->toArray();
  720. if(empty($order_list)) return [false , "暂无质保信息"];
  721. $first = $order_list[0];
  722. $order['main'] = [
  723. 'customer_name' => $first['customer_name'],
  724. 'customer_contact' => $first['customer_contact'],
  725. 'vin_no' => $first['vin_no'],
  726. ];
  727. foreach ($order_list as $key => $value){
  728. $order_list[$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  729. }
  730. $order['list'] = $order_list;
  731. return [true, $order];
  732. }
  733. public function searchWarrantyRule($data){
  734. if(empty($data['from'])) return [false, '查询来源不能为空'];
  735. $search = $data['customer_contact_or_vin_no'];
  736. if($data['from'] == 1){
  737. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号不能为空'];
  738. $length = strlen($data['customer_contact_or_vin_no']);
  739. if($length != 11) return [false, '请输入完整的11位手机号'];
  740. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  741. if(! $status) return [false, $msg];
  742. $search_field = 1;
  743. }elseif($data['from'] == 2){
  744. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号或车架号不能为空'];
  745. $length = strlen($data['customer_contact_or_vin_no']);
  746. if($length != 11 && $length != 17) return [false, '请输入完整的11位手机号或完整的17位车架号'];
  747. if($length == 11){
  748. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  749. if(! $status) return [false, $msg];
  750. $search_field = 1;
  751. }else{
  752. $search_field = 2;
  753. }
  754. }else{
  755. return [false, '查询来源错误'];
  756. }
  757. return [true, [$search_field, $search]];
  758. }
  759. public function searchWarrantyCommon($customer_contact){
  760. $vinCount = Warranty::where('del_time', 0)
  761. ->where('customer_contact', $customer_contact)
  762. ->where('vin_no', '<>', '')
  763. ->distinct()
  764. ->count('vin_no');
  765. if($vinCount > 1) return [false, '手机号:' . $customer_contact . '下有个多个车架号,请去质保查询功能按需查询质保信息'];
  766. return [true, ''];
  767. }
  768. }