TSpaceService.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. $message = [
  716. 'id' => $order['data_id'],
  717. 'data_type' => $order['type']
  718. ];
  719. list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info, $message);
  720. if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
  721. }
  722. return [true, $order];
  723. }
  724. //后台质保激活
  725. public function warrantyActivation($data,$user){
  726. list($status, $msg) = $this->warrantyActivationRule($data);
  727. if(! $status) return [false, $msg];
  728. $order = $msg;
  729. try {
  730. DB::beginTransaction();
  731. $update['is_active'] = Warranty::type_one;
  732. $update['active_id'] = $user['id'];
  733. Warranty::where('id', $data['id'])->update($update);
  734. (new DataSyncToU8Service())->updateByVinNo($order);
  735. DB::commit();
  736. }catch (\Throwable $exception){
  737. DB::rollBack();
  738. return [false, $exception->getMessage()];
  739. }
  740. return [true, ''];
  741. }
  742. public function warrantyActivationRule($data){
  743. if(empty($data['id'])) return [false, '质保ID不能为空'];
  744. $order = Warranty::where('del_time',0)
  745. ->where('id', $data['id'])
  746. ->first();
  747. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  748. $order = $order->toArray();
  749. if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
  750. if(empty($order['start_time'])) return [false, '施工日期/生效日期不能为空'];
  751. if(empty($order['customer_name'])) return [false, '车主姓名不能为空'];
  752. if(empty($order['customer_contact'])) return [false, '联系方式不能为空'];
  753. if(empty($order['warranty_time'])) return [false, '质保时长(月)不能为空'];
  754. if(empty($order['vin_no'])) return [false, '车架号不能为空'];
  755. $bool = $this->isValidVin($order['vin_no']);
  756. if(! $bool) return [false, '车架号错误,请填写完整车架号'];
  757. return [true, $order];
  758. }
  759. //质保卡作废
  760. public function warrantyDel($data,$user){
  761. list($status, $msg) = $this->warrantyDelRule($data);
  762. if(! $status) return [false, $msg];
  763. $order = $msg;
  764. $time = time();
  765. try {
  766. DB::beginTransaction();
  767. Warranty::where('id', $data['id'])->update(['del_time' => $time]);
  768. if(! empty($order['sn'])) ProductSnInfo::where('del_time',0)->where('sn', $order['sn'])->update(['del_time' => $time]);
  769. DB::commit();
  770. }catch (\Throwable $exception){
  771. DB::rollBack();
  772. return [false, $exception->getMessage()];
  773. }
  774. return [true, ''];
  775. }
  776. public function warrantyDelRule($data){
  777. if(empty($data['id'])) return [false, '质保ID不能为空'];
  778. $order = Warranty::where('del_time',0)
  779. ->where('id', $data['id'])
  780. ->first();
  781. if(empty($order)) return [false, '质保卡不存在或已被删除'];
  782. $order = $order->toArray();
  783. return [true, $order];
  784. }
  785. public function searchWarranty($data){
  786. list($status, $msg) = $this->searchWarrantyRule($data);
  787. if(! $status) return [false, $msg];
  788. list($search_field, $search) = $msg;
  789. $order_list = Warranty::where('del_time',0)
  790. ->when($search_field == 1, function ($query) use ($search) {
  791. return $query->where("customer_contact", $search);
  792. })
  793. ->when($search_field == 2, function ($query) use ($search) {
  794. return $query->where("vin_no", $search);
  795. })
  796. ->where(function ($query) {
  797. $query->where('type', '!=', ProductSnInfo::type_one)
  798. ->orWhere(function ($q) {
  799. $q->where('type', ProductSnInfo::type_one)
  800. ->where('sn', '<>', '');
  801. });
  802. })//sn限制
  803. ->orderBy('id','desc')
  804. ->get()->toArray();
  805. if(empty($order_list)) return [false , "暂无质保信息"];
  806. $first = $order_list[0];
  807. $order['main'] = [
  808. 'customer_name' => $first['customer_name'],
  809. 'customer_contact' => $first['customer_contact'],
  810. 'vin_no' => $first['vin_no'],
  811. ];
  812. foreach ($order_list as $key => $value){
  813. $order_list[$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  814. }
  815. $order['list'] = $order_list;
  816. return [true, $order];
  817. }
  818. public function searchWarrantyRule($data){
  819. if(empty($data['from'])) return [false, '查询来源不能为空'];
  820. $search = $data['customer_contact_or_vin_no'];
  821. if($data['from'] == 1){
  822. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号不能为空'];
  823. $length = strlen($data['customer_contact_or_vin_no']);
  824. if($length != 11) return [false, '请输入完整的11位手机号'];
  825. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  826. if(! $status) return [false, $msg];
  827. $search_field = 1;
  828. }elseif($data['from'] == 2){
  829. if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号或车架号不能为空'];
  830. $length = strlen($data['customer_contact_or_vin_no']);
  831. if($length != 11 && $length != 17) return [false, '请输入完整的11位手机号或完整的17位车架号'];
  832. if($length == 11){
  833. list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
  834. if(! $status) return [false, $msg];
  835. $search_field = 1;
  836. }else{
  837. $search_field = 2;
  838. }
  839. }else{
  840. return [false, '查询来源错误'];
  841. }
  842. return [true, [$search_field, $search]];
  843. }
  844. public function searchWarrantyCommon($customer_contact){
  845. $vinCount = Warranty::where('del_time', 0)
  846. ->where('customer_contact', $customer_contact)
  847. ->where('vin_no', '<>', '')
  848. ->distinct()
  849. ->count('vin_no');
  850. if($vinCount > 1) return [false, '手机号:' . $customer_contact . '下有个多个车架号,请去质保查询功能按需查询质保信息'];
  851. return [true, ''];
  852. }
  853. public function warrantyAddNew($order, $data, $user){
  854. try {
  855. DB::beginTransaction();
  856. $data['id'] = $order['id'];
  857. $time = time();
  858. //保存sn关联关系
  859. (new DataSyncToU8Service())->saveSn($data, ProductSnInfo::type_one, $time);
  860. $product_sn = ProductSnInfo::from('product_sn_info as a')
  861. ->leftJoin('product as b','b.id','a.product_id')
  862. ->select('a.*','b.title','b.warranty_time')
  863. ->where('a.del_time',0)
  864. ->where('a.data_id',$data['id'])
  865. ->where('a.type', ProductSnInfo::type_one)
  866. ->where('a.warranty_id', 0)
  867. ->get()->toArray();
  868. $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
  869. //生成质保单数据 需要更新的sn码信息
  870. if(! empty($product_sn)){
  871. $warranty = $sn_update = [];
  872. foreach ($product_sn as $value){
  873. $warranty[] = [
  874. 'data_id' => $order['id'],
  875. 'data_title' => $order['order_number'],
  876. 'type' => ProductSnInfo::type_one,
  877. 'product_id' => $value['product_id'],
  878. 'code' => $value['code'],
  879. 'title' => $value['title'],
  880. 'sn' => $value['sn'],
  881. 'customer_id' => $order['customer_id'],
  882. 'customer_name' => $order['customer_title'],
  883. 'customer_contact' => $order['customer_info'],
  884. 'vin_no' => $order['vin_no'],
  885. 'warranty_time' => $value['warranty_time'],
  886. 'construction_site_title' => $construction_site,
  887. 'start_time' => $order['end_time'],
  888. 'crt_time' => $time,
  889. 'crt_id' => $user['id'],
  890. 'sn_type' => $value['sn_type'],
  891. 'sy_code' => ""
  892. ];
  893. $sn_update[] = $value['sn'];
  894. }
  895. if(! empty($warranty)) Warranty::insert($warranty);
  896. if(! empty($sn_update)){
  897. $warranty_list = Warranty::where('del_time',0)
  898. ->whereIn("sn",$sn_update)
  899. ->select('id','sn')
  900. ->get()->toArray();
  901. foreach ($warranty_list as $value){
  902. ProductSnInfo::where('del_time',0)
  903. ->where('sn', $value['sn'])
  904. ->update(['warranty_id' => $value['id']]);
  905. }
  906. }
  907. }
  908. //生成溯源码质保卡
  909. $insert2 = $this->getInsert($data,$order,$construction_site,$time,$user);
  910. if(! empty($insert2)) Warranty::insert($insert2);
  911. DB::commit();
  912. }catch (\Throwable $exception){
  913. DB::rollBack();
  914. return [false, $exception->getMessage()];
  915. }
  916. return [true, ''];
  917. }
  918. public function getInsert($data,$order,$construction_site,$time,$user){
  919. $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
  920. $service = new DataSyncToU8Service();
  921. $insert2 = [];
  922. foreach ($data['product'] as $value){
  923. $tmp = $map[$value['product_id']] ?? [];
  924. $bool = $service->forCheck($tmp['product_category']);
  925. $value['warranty_time'] = $tmp['warranty_time'];
  926. //没有sn码信息直接返回
  927. if(empty($value['product_sn_info'])) {
  928. if($bool){
  929. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  930. }else{
  931. $bool = $service->forCheck2($tmp['product_category']);
  932. if($bool){
  933. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  934. }else{
  935. for ($i = 0; $i < intval($value['number']); $i++){
  936. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  937. }
  938. }
  939. }
  940. }else{
  941. $num = count(array_column($value['product_sn_info'],'sn'));
  942. if($bool){
  943. if($num < 1){
  944. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  945. }
  946. }else{
  947. $bool = $service->forCheck2($tmp['product_category']);
  948. if($bool){
  949. if($num < 1){
  950. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  951. }
  952. }else{
  953. $n = bcsub($value['number'], $num,2);
  954. if(intval($n) > 0){
  955. for ($i = 0; $i < intval($value['number']); $i++){
  956. $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
  957. }
  958. }
  959. }
  960. }
  961. }
  962. }
  963. return $insert2;
  964. }
  965. public function fillInsert(&$insert2, $order, $value, $construction_site,$time,$user){
  966. $insert2[] = [
  967. 'data_id' => $order['id'],
  968. 'data_title' => $order['order_number'],
  969. 'type' => ProductSnInfo::type_one,
  970. 'product_id' => $value['product_id'],
  971. 'code' => $value['code'],
  972. 'title' => $value['title'],
  973. 'sn' => "",
  974. 'customer_id' => $order['customer_id'],
  975. 'customer_name' => $order['customer_title'],
  976. 'customer_contact' => $order['customer_info'],
  977. 'vin_no' => $order['vin_no'],
  978. 'warranty_time' => $value['warranty_time'],
  979. 'construction_site_title' => $construction_site,
  980. 'start_time' => $order['end_time'],
  981. 'crt_time' => $time,
  982. 'crt_id' => $user['id'],
  983. // 'sn_type' => $value['sn_type'] ?? 0,
  984. 'sy_code' => $value['sy_code'] ?? "",
  985. ];
  986. }
  987. }