12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Construction;
- use App\Model\ConstructionProductInfo;
- use App\Model\Customer;
- use App\Model\CustomerInfo;
- use App\Model\Product;
- use App\Model\ProductCategory;
- use App\Model\ProductSnInfo;
- use App\Model\ReturnExchangeOrder;
- use App\Model\ReturnExchangeOrderProductInfo;
- use App\Model\SalesOrder;
- use App\Model\SalesOrderProductInfo;
- use App\Model\TSpaceSet;
- use App\Model\Warranty;
- use Illuminate\Support\Facades\DB;
- class TSpaceService extends Service
- {
- public function add($data, $user){
- return [true, ''];
- }
- public function edit($data, $user){
- list($status, $msg) = $this->constructionEditOtherRule($data, $user);
- if(! $status) return [false, $msg];
- DB::beginTransaction();
- try{
- $set = TSpaceSet::where('del_time',0)
- ->where('type', $data['type'])
- ->select('file', 'id')
- ->get()->toArray();
- $set_map = array_column($set,'file','id');
- $time = time();
- $insert = $update = $new = $old = $id = [];
- foreach ($data['data'] as $value){
- $text = "";
- if(! empty($value['text'])) $text = json_encode($value['text']);
- if(! empty($value['id'])){
- $file = $set_map[$value['id']] ?? "";
- if($value['file'] != $file){
- $old[] = $file;
- $new[] = $value['file'];
- }
- $update[] = [
- 'id' => $value['id'],
- 'type' => $data['type'],
- 'text' => $text,
- 'file' => $value['file'],
- 'crt_time' => $time,
- ];
- $id[] = $value['id'];
- }else{
- $new[] = $value['file'];
- $insert[] = [
- 'type' => $data['type'],
- 'text' => $text,
- 'file' => $value['file'],
- 'crt_time' => $time,
- ];
- }
- }
- foreach ($set as $value){
- if(! in_array($value['id'], $id)){
- $update[] = [
- 'id' => $value['id'],
- 'del_time' => $time,
- ];
- $old[] = $value['file'];
- }
- }
- if(! empty($update)){
- foreach ($update as $value){
- TSpaceSet::where('id',$value['id'])
- ->update($value);
- }
- }
- if(! empty($insert)) TSpaceSet::insert($insert);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ['file' => ['new' => $new, 'old' => $old]]];
- }
- public function constructionEditOtherRule($data,$user){
- if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];
- if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];
- if(empty($data['data'])) return [false, '设置内容不能为空'];
- foreach ($data['data'] as $value){
- if(empty($value['file'])) return [false, '文件不能为空'];
- }
- return [true, ''];
- }
- public function detail($data, $user){
- if(empty($data['type'])) return [false,'TSpace首页设置类型不能为空'];
- if(! isset(TSpaceSet::$model_type_title[$data['type']])) return [false,'TSpace首页设置类型错误'];
- $set = TSpaceSet::where('del_time',0)
- ->where('type', $data['type'])
- ->select('file', 'id', 'text')
- ->get()->toArray();
- $fileUploadService = new FileUploadService();
- foreach ($set as $key => $value){
- if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);
- $url = "";
- if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
- $set[$key]['file_url'] = $url;
- }
- return [true, $set];
- }
- public function tSpacelist($data){
- if(empty($data['type'])){
- $type = [];
- }else{
- if(! is_array($data['type'])){
- $type = [$data['type']];
- }else{
- $type = $data['type'];
- }
- }
- $set = TSpaceSet::where('del_time',0)
- ->when(! empty($type), function ($query) use ($type) {
- return $query->whereIn('type',$type);
- })
- ->select('file', 'id', 'text', 'type')
- ->get()->toArray();
- $fileUploadService = new FileUploadService();
- foreach ($set as $key => $value){
- if(! empty($value['text'])) $set[$key]['text'] = json_decode($value['text'], true);
- $url = "";
- if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
- $set[$key]['file_url'] = $url;
- }
- return [true, $set];
- }
- public function warrantyGetProduct($data, $user){
- if(empty($data['sale_order_id'])) return [false, '合同ID不能为空'];
- $return = $this->warrantyGetProductList($data, $user);
- return [true, $return];
- }
- public function warrantyGetProductList($data, $user){
- $return = [];
- $data_id = $data['sale_order_id'];
- //销售订单对应产品总数
- $product = SalesOrderProductInfo::from('sales_order_product_info as a')
- ->join('product as b','a.product_id','b.id')
- ->where('a.del_time',0)
- ->where('a.sales_order_id', $data_id)
- ->where('b.warranty_time', '>', 0)
- ->select('a.number','a.product_id','b.product_category')
- ->get()->toArray();
- if(empty($product)) return $return;
- $product_id = array_column($product,'product_id');
- $map = (new ProductService())->getProductDetail($product_id);
- //合同质保产品
- $sn_product = ProductSnInfo::where("del_time",0)
- ->where('data_id', $data_id)
- ->where('type',ProductSnInfo::type_two)
- ->whereIn('product_id',$product_id)
- ->select('product_id')
- ->get()->toArray();
- $product_map1 = [];
- foreach ($sn_product as $value){
- $key = $value['product_id'];
- if(isset($product_map1[$key])){
- $number = bcadd(1, $product_map1[$key],2);
- $product_map1[$key] = $number;
- }else{
- $product_map1[$key] = 1;
- }
- }
- //合同退货产品
- $product_map2 = [];
- $return_id = ReturnExchangeOrder::where('del_time',0)
- ->where('type', ReturnExchangeOrder::Order_type)
- ->where('model_type', ReturnExchangeOrder::Model_type_one)
- ->where('data_id', $data_id)
- ->select('id')
- ->get()->toArray();
- $save2 = ReturnExchangeOrderProductInfo::where('del_time',0)
- ->whereIn('return_exchange_id', array_column($return_id,'id'))
- ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
- ->select('number', 'return_exchange_id','product_id')
- ->get()->toArray();
- foreach ($save2 as $value){
- $key = $value['product_id'];
- if(isset($product_map2[$key])){
- $number = bcadd($value['number'], $product_map2[$key],2);
- $product_map2[$key] = $number;
- }else{
- $product_map2[$key] = $value['number'];
- }
- }
- $service = new DataSyncToU8Service();
- foreach ($product as $value){
- $bool = $service->forCheck($value['product_category']);
- //车窗膜不管数量算一个
- if($bool) $value['number'] = 1;
- //合同质保产品
- $p1 = $product_map1[$value['product_id']] ?? 0;
- //合同退货产品
- $p2 = $product_map2[$value['product_id']] ?? 0;
- $number = bcsub($value['number'], $p1,2);
- $number = bcsub($number, $p2,2);
- if($number <= 0) continue;
- $tmp = $map[$value['product_id']] ?? [];
- $return[] = [
- 'number' => $number, //可出数量
- 'product_id' => $value['product_id'],
- 'title' => $tmp['title'] ?? "",
- 'code' => $tmp['code'] ?? "",
- 'size' => $tmp['size'] ?? "",
- 'unit' => $tmp['unit'] ?? "",
- ];
- }
- return $return;
- }
- public function needSnFix($data, $user){
- if(empty($data['id'])) return [false, '施工单id不能为空'];
- //产品
- $p_info = ConstructionProductInfo::where('del_time',0)
- ->where('construction_id',$data['id'])
- ->get()->toArray();
- $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
- $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
- $product = [];
- //sn码信息
- $sn_map = (new DataSyncToU8Service())->getSn($data, ProductSnInfo::type_one);
- $service = new DataSyncToU8Service();
- $needSnFix = false;
- foreach ($p_info as $value){
- $tmp = $map[$value['product_id']] ?? [];
- $value['title'] = $tmp['title'] ?? "";
- $value['code'] = $tmp['code'] ?? "";
- $value['size'] = $tmp['size'] ?? "";
- $value['unit'] = $tmp['unit'] ?? "";
- $value['bar_code'] = $tmp['bar_code'] ?? "";
- $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
- $s_t = $sn_map[$value['code']] ?? [];
- $sn = array_column($s_t,'sn');
- $value['product_sn_info'] = $sn;
- $bool = $service->forCheck($tmp['product_category']);
- $count = count($sn);
- if($bool){
- //如果车窗膜已经有sn码 则跳过
- if($count > 0) {
- $product_type_for_sn = 0;
- }else{
- if(! $needSnFix) $needSnFix = true;
- $product_type_for_sn = 1;
- }
- }else{
- $bool = $service->forCheck2($tmp['product_category']);
- if($bool){
- //如果除车窗膜外已经有sn码 则跳过
- if($count > 0) {
- $product_type_for_sn = 0;
- }else{
- if(! $needSnFix) $needSnFix = true;
- $product_type_for_sn = 1;
- }
- }else{
- //其它产品数量达到 则跳过
- if($count >= $p_info['number']) {
- $product_type_for_sn = 0;
- }else{
- if(! $needSnFix) $needSnFix = true;
- $product_type_for_sn = 2;
- }
- }
- }
- $value['product_type_for_sn'] = $product_type_for_sn;
- $product[] = $value;
- }
- if(empty($needSnFix)) return [true, ['needSnFix' => $needSnFix, 'product' => $product]];
- return [true, ['needSnFix' => $needSnFix, 'product' => $product]];
- }
- public function warrantyAdd($data, $user){
- list($status, $msg) = $this->warrantyAddRule($data,$user);
- if(! $status) return [false, $msg];
- list($warranty, $sn) = $msg;
- try {
- DB::beginTransaction();
- Warranty::insert($warranty);
- if(! empty($sn)){
- $warranty_list = Warranty::where('del_time',0)
- ->whereIn("sn",$sn)
- ->select('id','sn')
- ->get()->toArray();
- foreach ($warranty_list as $value){
- ProductSnInfo::where('del_time',0)
- ->where('sn', $value['sn'])
- ->update(['warranty_id' => $value['id']]);
- }
- }
- // if($data['type'] == ProductSnInfo::type_two){
- // //同一个合同,多次填写的客户基本信息按最后一次更新为主
- // Warranty::where('del_time',0)
- // ->where('data_id',$data['data_id'])
- // ->where('type', $data['type'])
- // ->update(['customer_name' => $data['customer_name'], 'customer_contact' => $data['customer_contact']]);
- // }
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- private function warrantyAddRule($data, $user){
- if(empty($data['type'])) return [false, 'type不能为空'];
- if(! isset(ProductSnInfo::$type_name[$data['type']])) return [false,'type不存在'];
- if(empty($data['data_id'])) return [false, 'data_id不能为空'];
- $time = time();
- $warranty = $sn_update = [];
- if($data['type'] == ProductSnInfo::type_one){
- $order = Construction::where('del_time',0)->where('id',$data['data_id'])->first();
- if(empty($order)) return [false, '施工单不存在或已被删除'];
- $order = $order->toArray();
- if($order['state'] != Construction::STATE_FOUR) return [false, '施工单暂未完结,生成质保信息失败'];
- if(empty($order['vin_no'])) return [false, '车架号不能为空'];
- $bool = $this->isValidVin($order['vin_no']);
- if(! $bool) return [false, '车架号错误,请查看原施工单车架号,编辑输入完整车架号'];
- if(empty($order['customer_id'])) return [false, '客户信息不存在'];
- $customer = Customer::where('del_time', 0)->where('id',$order['customer_id'])->first();
- if(empty($customer)) return [false, '客户不存在或已被删除'];
- $customer = $customer->toArray();
- $info = CustomerInfo::where('del_time',0)
- ->where('type',CustomerInfo::type_one)
- ->where('customer_id', $customer['id'])
- ->where('contact_info', "<>", "")
- ->select('contact_info')
- ->first();
- if(empty($info)) return [false, "客户的联系方式不能为空"];
- $info = $info->toArray();
- $customer_info = $info['contact_info'];
- $product_sn = ProductSnInfo::from('product_sn_info as a')
- ->leftJoin('product as b','b.id','a.product_id')
- ->select('a.*','b.title','b.warranty_time')
- ->where('a.del_time',0)
- ->where('a.data_id',$data['data_id'])
- ->where('a.type', ProductSnInfo::type_one)
- ->where('a.warranty_id', 0)
- ->get()->toArray();
- if(empty($product_sn)) return [false, '施工单暂无未生成质保信息的sn码'];
- $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
- //质保单数据 需要更新的sn码信息
- foreach ($product_sn as $value){
- $warranty[] = [
- 'data_id' => $order['id'],
- 'data_title' => $order['order_number'],
- 'type' => ProductSnInfo::type_one,
- 'product_id' => $value['product_id'],
- 'code' => $value['code'],
- 'title' => $value['title'],
- 'sn' => $value['sn'],
- 'customer_id' => $order['customer_id'],
- 'customer_name' => $customer['title'],
- 'customer_contact' => $customer_info,
- 'vin_no' => $order['vin_no'],
- 'warranty_time' => $value['warranty_time'],
- 'construction_site_title' => $construction_site,
- 'start_time' => $order['end_time'],
- 'crt_time' => $time,
- 'crt_id' => $user['id'],
- ];
- $sn_update[] = $value['sn'];
- }
- }elseif($data['type'] == ProductSnInfo::type_two){
- $order = SalesOrder::where('del_time',0)->where('id',$data['data_id'])->first();
- if(empty($order)) return [false, '合同不存在或已被删除'];
- $order = $order->toArray();
- if(empty($order['customer_id'])) return [false, '客户不能为空'];
- $customer_name = Customer::where('id', $order['customer_id'])->value('title');
- if(empty($order['customer_contact'])) return [false, '客户联系方式不能为空'];
- $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
- if(empty($product)) return [false, '合同已全部或暂无可生成质保信息的产品'];
- foreach ($product as $value){
- for($i = 0 ;$i < $value['number']; $i++){
- $warranty[] = [
- 'data_id' => $order['id'],
- 'data_title' => $order['order_number'],
- 'type' => ProductSnInfo::type_two,
- 'product_id' => $value['product_id'],
- 'code' => $value['code'],
- 'title' => $value['title'],
- 'sn' => "",
- 'customer_id' => $order['customer_id'],
- 'customer_name' => $customer_name,
- 'customer_contact' => $order['customer_contact'],
- 'vin_no' => "",
- 'warranty_time' => $value['warranty_time'],
- 'construction_site_title' => "",
- 'start_time' => $time,
- 'crt_time' => $time,
- 'crt_id' => $user['id'],
- ];
- }
- }
- }else{
- return [false, '质保生成类型错误'];
- }
- return [true, [$warranty, $sn_update]];
- }
- public function ori()
- {//生成质保 合同原来的代码
- $order = SalesOrder::where('del_time', 0)->where('id', $data['data_id'])->first();
- if (empty($order)) return [false, '合同不存在或已被删除'];
- $order = $order->toArray();
- if (empty($data['customer_name'])) return [false, '客户名称不能为空'];
- if (empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
- $bool = $this->isValidMobile($data['customer_contact']);
- if (!$bool) return [false, '手机号错误,请输入完整的手机号'];
- $vin_no = "";
- if (!empty($data['vin_no'])) {
- $bool = $this->isValidVin($data['vin_no']);
- if (!$bool) return [false, '车架号错误,请填写完整车架号'];
- $vin_no = $data['vin_no'];
- }
- if (empty($data['product'])) return [false, '合同无生成质保的产品信息'];
- $product = (new SalesOrderService())->getSalesProductForWarranty($data['data_id']);
- if (empty($product)) return [false, '合同暂无能生成质保信息的产品'];
- foreach ($data['product'] as $value) {
- $tmp = $product[$value['product_id']] ?? [];
- if (empty($tmp)) return [false, '存在不能生成质保的产品'];
- if ($tmp['number'] > $value['number']) return [false, '产品编码:' . $tmp['code'] . '本次可生成质保信息的产品最多' . $tmp['number']];
- $warranty[] = [
- 'data_id' => $order['id'],
- 'data_title' => $order['order_number'],
- 'type' => ProductSnInfo::type_two,
- 'product_id' => $tmp['product_id'],
- 'code' => $tmp['code'],
- 'title' => $tmp['title'],
- 'sn' => "",
- 'customer_id' => $order['customer_id'],
- 'customer_name' => $data['customer_name'],
- 'customer_contact' => $data['customer_contact'],
- 'vin_no' => $vin_no,
- 'warranty_time' => $tmp['warranty_time'],
- 'construction_site_title' => "",
- 'start_time' => $time,
- 'crt_time' => $time,
- 'crt_id' => $user['id'],
- ];
- }
- }
- public function saveWarrantyByMyself($data){
- list($status, $msg) = $this->saveWarrantyByMyselfRule($data);
- if(! $status) return [false, $msg];
- list($warranty, $sn) = $msg;
- try {
- DB::beginTransaction();
- $warranty_id = DB::table('warranty')->insertGetId($warranty);
- $sn['warranty_id'] = $warranty_id;
- ProductSnInfo::insert($sn);
- (new DataSyncToU8Service())->updateByVinNo($data);
- if(! empty($data['auto_id'])) (new DataSyncToU8Service())->updateSnInfo(1, [$data['auto_id']], 1);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveWarrantyByMyselfRule($data){
- if(empty($data['product_id'])) return [false, '产品ID不能为空'];
- $product = Product::where('del_time',0)
- ->where('id',$data['product_id'])
- ->first();
- if(empty($product)) return [false, '产品不存在或已被删除'];
- $product = $product->toArray();
- if(empty($data['start_time'])) return [false, '施工日期不能为空'];
- if(empty($product['warranty_time'])) return [false, '质保时长不能为空'];
- if(empty($data['sn'])) return [false, '产品序列号不能为空'];
- list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
- if(! $status) return [false, $msg];
- //校验sn是否被占用
- $submit_info[] = $product['code'] . $data['sn'];
- list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$product['code']], [$data['sn']], $submit_info);
- if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
- if(empty($data['customer_name'])) return [false, '客户名称不能为空'];
- if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
- $bool = $this->isValidMobile($data['customer_contact']);
- if(! $bool) return [false, '手机号错误,请填写完整的11位手机号'];
- if(empty($data['vin_no'])) return [false, '车架号不能为空'];
- $bool = $this->isValidVin($data['vin_no']);
- if(! $bool) return [false, '车架号错误,请填写完整的17位车架号'];
- $time = time();
- //质保单数据 sn码数据
- $warranty = [
- 'data_id' => 0,
- 'data_title' => '',
- 'type' => ProductSnInfo::type_three,
- 'product_id' => $product['id'],
- 'code' => $product['code'],
- 'title' => $product['title'],
- 'sn' => $data['sn'],
- 'customer_id' => 0,
- 'customer_name' => $data['customer_name'],
- 'customer_contact' => $data['customer_contact'],
- 'vin_no' => $data['vin_no'],
- 'warranty_time' => $product['warranty_time'],
- 'construction_site_title' => $data['construction_site_title'] ?? "",
- 'start_time' => $data['start_time'],
- 'crt_time' => $time,
- 'crt_id' => 0,
- 'is_active' => Warranty::type_one,
- 'sn_type' => 1,
- ];
- $sn_update = [
- 'data_id' => 0,
- 'type' => ProductSnInfo::type_three,
- 'product_id' => $product['id'],
- 'code' => $product['code'],
- 'sn' => $data['sn'],
- 'crt_time' => $time,
- 'warranty_id' => 0,
- 'auto_id' => $data['auto_id'] ?? 0,
- ];
- return [true, [$warranty, $sn_update]];
- }
- //获取未激活列表
- public function getWarrantyNotActiveList($data){
- if(empty($data['customer_contact'])) return [false, '手机号不能为空'];
- $model = Warranty::where('del_time',0)
- ->where('customer_contact',$data['customer_contact'])
- ->where('is_active',Warranty::type_zero)
- ->whereIn('type',[ProductSnInfo::type_one, ProductSnInfo::type_two])
- ->where(function ($query) {
- $query->where('type', '!=', ProductSnInfo::type_one)
- ->orWhere(function ($q) {
- $q->where('type', ProductSnInfo::type_one)
- ->where('sn', '<>', '');
- });
- })//sn限制
- ->select('id','data_id','data_title','type','product_id','title','sn','customer_name','customer_contact','vin_no','warranty_time','construction_site_title','start_time');
- $list = $this->limit($model,'', $data);
- $list = $this->fillGetWarrantyNotActiveList($list);
- return [true, $list];
- }
- public function fillGetWarrantyNotActiveList($data){
- if(empty($data['data'])) return $data;
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
- }
- return $data;
- }
- //质保激活
- public function warrantyActivationCustomer($data){
- list($status, $msg) = $this->warrantyActivationCustomerRule($data);
- if(! $status) return [false, $msg];
- $order = $msg;
- try {
- DB::beginTransaction();
- if($order['type'] == ProductSnInfo::type_two){
- $update = [
- 'sn' => $data['sn'] ?? '',
- 'construction_site_title' => $data['construction_site_title'] ?? '',
- 'vin_no' => $data['vin_no'],
- 'is_active' => Warranty::type_one
- ];
- Warranty::where('id',$data['id'])->update($update);
- if(! empty($data['sn'])){
- $order = Warranty::where('id',$data['id'])->first()->toArray();
- $sn_update = [
- 'data_id' => $order['data_id'],
- 'type' => $order['type'],
- 'product_id' => $order['product_id'],
- 'code' => $order['code'],
- 'sn' => $order['sn'],
- 'crt_time' => time(),
- 'warranty_id' => $order['id'],
- ];
- ProductSnInfo::insert($sn_update);
- }
- }elseif($order['type'] == ProductSnInfo::type_one){
- $update = [
- 'construction_site_title' => $data['construction_site_title'] ?? '',
- 'is_active' => Warranty::type_one
- ];
- Warranty::where('id',$data['id'])->update($update);
- }
- (new DataSyncToU8Service())->updateByVinNo($data);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function warrantyActivationCustomerRule($data){
- if(empty($data['id'])) return [false, '质保ID不能为空'];
- $order = Warranty::where('del_time',0)
- ->where('id', $data['id'])
- ->first();
- if(empty($order)) return [false, '质保卡不存在或已被删除'];
- $order = $order->toArray();
- if($order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
- if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
- if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
- if(empty($data['vin_no'])) return [false, '车架号不能为空'];
- if($order['type'] == ProductSnInfo::type_one){
- }else{
- if(! empty($data['sn'])){
- list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
- if(! $status) return [false, $msg];
- $submit_info[] = $order['code'] . $data['sn'];
- list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info);
- if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
- }
- $bool = $this->isValidVin($data['vin_no']);
- if(! $bool) return [false, '车架号错误,请填写完整车架号'];
- }
- return [true, $order];
- }
- //后台列表
- public function warrantyList($data,$user){
- $model = Warranty::where('del_time',0)
- ->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')
- ->orderBy('id','desc');
- if(! empty($data['type'])) $model->where('type', $data['type']);
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['data_title'])) $model->where('data_title', 'LIKE', '%'.$data['data_title'].'%');
- if(! empty($data['sn'])) $model->where('sn', 'LIKE', '%'.$data['sn'].'%');
- if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
- if(! empty($data['customer_contact'])) $model->where('customer_contact', 'LIKE', '%'.$data['customer_contact'].'%');
- if(! empty($data['vin_no'])) $model->where('vin_no', 'LIKE', '%'.$data['vin_no'].'%');
- if(! empty($data['construction_site_title'])) $model->where('construction_site_title', 'LIKE', '%'.$data['construction_site_title'].'%');
- if(! empty($data['warranty_time'])) $model->where('warranty_time', $data['warranty_time']);
- if(isset($data['is_active'])) $model->where('is_active', $data['is_active']);
- if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
- if(! empty($data['id'])) $model->where('id', $data['id']);
- if(! empty($data['sy_code'])) $model->where('sy_code', 'LIKE', '%'.$data['sy_code'].'%');
- $list = $this->limit($model,'', $data);
- $list = $this->fillWarrantyList($list);
- return [true, $list];
- }
- public function fillWarrantyList($data){
- if(empty($data['data'])) return $data;
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
- $data['data'][$key]['type_title'] = ProductSnInfo::$type_name[$value['type']] ?? "";
- }
- return $data;
- }
- //后台质保编辑以及激活
- public function warrantyEditAndActivation($data,$user){
- list($status, $msg) = $this->warrantyEditAndActivationRule($data);
- if(! $status) return [false, $msg];
- $order = $msg;
- try {
- DB::beginTransaction();
- $time = time();
- $sn = $data['sn'] ?? "";
- $order_sn = $order['sn'];
- if(! empty($order_sn) && ! empty($sn) && $order_sn != $sn){
- ProductSnInfo::where('del_time',0)
- ->where('data_id', $order['data_id'])
- ->where('type', $order['type'])
- ->where('sn', $order_sn)
- ->update(['del_time' => $time]);
- $sn_update = [
- 'data_id' => $order['data_id'],
- 'type' => $order['type'],
- 'product_id' => $order['product_id'],
- 'code' => $order['code'],
- 'sn' => $order['sn'],
- 'crt_time' => time(),
- 'warranty_id' => $order['id'],
- ];
- ProductSnInfo::insert($sn_update);
- }
- $update = [
- 'sn' => $sn,
- 'construction_site_title' => $data['construction_site_title'] ?? '',
- 'vin_no' => $data['vin_no'],
- 'start_time' => $data['start_time'],
- 'customer_name' => $data['customer_name'],
- 'customer_contact' => $data['customer_contact'],
- 'warranty_time' => $data['warranty_time'],
- ];
- if(empty($data['is_active'])) {
- $update['is_active'] = Warranty::type_one;
- $update['active_id'] = $user['id'];
- }
- Warranty::where('id',$data['id'])->update($update);
- (new DataSyncToU8Service())->updateByVinNo($data);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function warrantyEditAndActivationRule(&$data){
- if(empty($data['id'])) return [false, '质保ID不能为空'];
- $order = Warranty::where('del_time',0)
- ->where('id', $data['id'])
- ->first();
- if(empty($order)) return [false, '质保卡不存在或已被删除'];
- $order = $order->toArray();
- // if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
- if(empty($data['start_time'])) return [false, '请填写施工日期/生效日期'];
- $data['start_time'] = $this->changeDateToDateMin($data['start_time']);
- if(empty($data['customer_name'])) return [false, '车主姓名不能为空'];
- if(empty($data['customer_contact'])) return [false, '联系方式不能为空'];
- if(empty($data['warranty_time'])) return [false, '质保时长(月)不能为空'];
- if(empty($data['vin_no'])) return [false, '车架号不能为空'];
- $bool = $this->isValidVin($data['vin_no']);
- if(! $bool) return [false, '车架号错误,请填写完整车架号'];
- if(! empty($data['sn'])){
- // list($status, $msg) = (new DataSyncToU8Service())->snForWarranty($data);
- // if(! $status) return [false, $msg];
- $submit_info[] = $order['code'] . $data['sn'];
- $data_id = $order['data_id'];
- if($order['type'] == ProductSnInfo::type_three) $data_id = $order['id'];
- $message = [
- 'id' => $data_id,
- 'data_type' => $order['type']
- ];
- list($status, $msg) = (new DataSyncToU8Service())->snForCheck([$order['code']], [$data['sn']], $submit_info, $message);
- if(! $status) return [false, "产品序列号:" . $data['sn'] . "已存在于门店系统"];
- }
- return [true, $order];
- }
- //后台质保激活
- public function warrantyActivation($data,$user){
- list($status, $msg) = $this->warrantyActivationRule($data);
- if(! $status) return [false, $msg];
- $order = $msg;
- try {
- DB::beginTransaction();
- $update['is_active'] = Warranty::type_one;
- $update['active_id'] = $user['id'];
- Warranty::where('id', $data['id'])->update($update);
- (new DataSyncToU8Service())->updateByVinNo($order);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function warrantyActivationRule($data){
- if(empty($data['id'])) return [false, '质保ID不能为空'];
- $order = Warranty::where('del_time',0)
- ->where('id', $data['id'])
- ->first();
- if(empty($order)) return [false, '质保卡不存在或已被删除'];
- $order = $order->toArray();
- if(! empty($data['is_active']) && $order['is_active'] > Warranty::type_zero) return [false, '质保卡已激活,请勿重复操作'];
- if(empty($order['start_time'])) return [false, '施工日期/生效日期不能为空'];
- if(empty($order['customer_name'])) return [false, '车主姓名不能为空'];
- if(empty($order['customer_contact'])) return [false, '联系方式不能为空'];
- if(empty($order['warranty_time'])) return [false, '质保时长(月)不能为空'];
- if(empty($order['vin_no'])) return [false, '车架号不能为空'];
- $bool = $this->isValidVin($order['vin_no']);
- if(! $bool) return [false, '车架号错误,请填写完整车架号'];
- return [true, $order];
- }
- //质保卡作废
- public function warrantyDel($data,$user){
- list($status, $msg) = $this->warrantyDelRule($data);
- if(! $status) return [false, $msg];
- $order = $msg;
- $time = time();
- try {
- DB::beginTransaction();
- Warranty::where('id', $data['id'])->update(['del_time' => $time]);
- if(! empty($order['sn'])) ProductSnInfo::where('del_time',0)->where('sn', $order['sn'])->update(['del_time' => $time]);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function warrantyDelRule($data){
- if(empty($data['id'])) return [false, '质保ID不能为空'];
- $order = Warranty::where('del_time',0)
- ->where('id', $data['id'])
- ->first();
- if(empty($order)) return [false, '质保卡不存在或已被删除'];
- $order = $order->toArray();
- return [true, $order];
- }
- public function searchWarranty($data){
- list($status, $msg) = $this->searchWarrantyRule($data);
- if(! $status) return [false, $msg];
- list($search_field, $search) = $msg;
- $order_list = Warranty::where('del_time',0)
- ->when($search_field == 1, function ($query) use ($search) {
- return $query->where("customer_contact", $search);
- })
- ->when($search_field == 2, function ($query) use ($search) {
- return $query->where("vin_no", $search);
- })
- ->where(function ($query) {
- $query->where('type', '!=', ProductSnInfo::type_one)
- ->orWhere(function ($q) {
- $q->where('type', ProductSnInfo::type_one)
- ->where('sn', '<>', '');
- });
- })//sn限制
- ->orderBy('id','desc')
- ->get()->toArray();
- if(empty($order_list)) return [false , "暂无质保信息"];
- $first = $order_list[0];
- $order['main'] = [
- 'customer_name' => $first['customer_name'],
- 'customer_contact' => $first['customer_contact'],
- 'vin_no' => $first['vin_no'],
- ];
- foreach ($order_list as $key => $value){
- $order_list[$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
- }
- $order['list'] = $order_list;
- return [true, $order];
- }
- public function searchWarrantyRule($data){
- if(empty($data['from'])) return [false, '查询来源不能为空'];
- $search = $data['customer_contact_or_vin_no'];
- if($data['from'] == 1){
- if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号不能为空'];
- $length = strlen($data['customer_contact_or_vin_no']);
- if($length != 11) return [false, '请输入完整的11位手机号'];
- list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
- if(! $status) return [false, $msg];
- $search_field = 1;
- }elseif($data['from'] == 2){
- if(empty($data['customer_contact_or_vin_no'])) return [false, '手机号或车架号不能为空'];
- $length = strlen($data['customer_contact_or_vin_no']);
- if($length != 11 && $length != 17) return [false, '请输入完整的11位手机号或完整的17位车架号'];
- if($length == 11){
- list($status,$msg) = $this->searchWarrantyCommon($data['customer_contact_or_vin_no']);
- if(! $status) return [false, $msg];
- $search_field = 1;
- }else{
- $search_field = 2;
- }
- }else{
- return [false, '查询来源错误'];
- }
- return [true, [$search_field, $search]];
- }
- public function searchWarrantyCommon($customer_contact){
- $vinCount = Warranty::where('del_time', 0)
- ->where('customer_contact', $customer_contact)
- ->where('vin_no', '<>', '')
- ->distinct()
- ->count('vin_no');
- if($vinCount > 1) return [false, '手机号:' . $customer_contact . '下有个多个车架号,请去质保查询功能按需查询质保信息'];
- return [true, ''];
- }
- public function warrantyAddNew($order, $data, $user){
- try {
- DB::beginTransaction();
- $data['id'] = $order['id'];
- $time = time();
- //保存sn关联关系
- (new DataSyncToU8Service())->saveSn($data, ProductSnInfo::type_one, $time);
- $product_sn = ProductSnInfo::from('product_sn_info as a')
- ->leftJoin('product as b','b.id','a.product_id')
- ->select('a.*','b.title','b.warranty_time')
- ->where('a.del_time',0)
- ->where('a.data_id',$data['id'])
- ->where('a.type', ProductSnInfo::type_one)
- ->where('a.warranty_id', 0)
- ->get()->toArray();
- $construction_site = BasicType::where('id',$order['install_position'])->value('title') ?? "";
- //生成质保单数据 需要更新的sn码信息
- if(! empty($product_sn)){
- $warranty = $sn_update = [];
- foreach ($product_sn as $value){
- $warranty[] = [
- 'data_id' => $order['id'],
- 'data_title' => $order['order_number'],
- 'type' => ProductSnInfo::type_one,
- 'product_id' => $value['product_id'],
- 'code' => $value['code'],
- 'title' => $value['title'],
- 'sn' => $value['sn'],
- 'customer_id' => $order['customer_id'],
- 'customer_name' => $order['customer_title'],
- 'customer_contact' => $order['customer_info'],
- 'vin_no' => $order['vin_no'],
- 'warranty_time' => $value['warranty_time'],
- 'construction_site_title' => $construction_site,
- 'start_time' => $order['end_time'],
- 'crt_time' => $time,
- 'crt_id' => $user['id'],
- 'sn_type' => $value['sn_type'],
- 'sy_code' => ""
- ];
- $sn_update[] = $value['sn'];
- }
- if(! empty($warranty)) Warranty::insert($warranty);
- if(! empty($sn_update)){
- $warranty_list = Warranty::where('del_time',0)
- ->whereIn("sn",$sn_update)
- ->select('id','sn')
- ->get()->toArray();
- foreach ($warranty_list as $value){
- ProductSnInfo::where('del_time',0)
- ->where('sn', $value['sn'])
- ->update(['warranty_id' => $value['id']]);
- }
- }
- }
- //生成溯源码质保卡
- $insert2 = $this->getInsert($data,$order,$construction_site,$time,$user);
- if(! empty($insert2)) Warranty::insert($insert2);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function getInsert($data,$order,$construction_site,$time,$user){
- $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
- $service = new DataSyncToU8Service();
- $insert2 = [];
- foreach ($data['product'] as $value){
- $tmp = $map[$value['product_id']] ?? [];
- $bool = $service->forCheck($tmp['product_category']);
- $value['warranty_time'] = $tmp['warranty_time'];
- //没有sn码信息直接返回
- if(empty($value['product_sn_info'])) {
- if($bool){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }else{
- $bool = $service->forCheck2($tmp['product_category']);
- if($bool){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }else{
- for ($i = 0; $i < intval($value['number']); $i++){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }
- }
- }
- }else{
- $num = count(array_column($value['product_sn_info'],'sn'));
- if($bool){
- if($num < 1){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }
- }else{
- $bool = $service->forCheck2($tmp['product_category']);
- if($bool){
- if($num < 1){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }
- }else{
- $n = bcsub($value['number'], $num,2);
- if(intval($n) > 0){
- for ($i = 0; $i < intval($value['number']); $i++){
- $this->fillInsert($insert2,$order,$value,$construction_site,$time,$user);
- }
- }
- }
- }
- }
- }
- return $insert2;
- }
- public function fillInsert(&$insert2, $order, $value, $construction_site,$time,$user){
- $insert2[] = [
- 'data_id' => $order['id'],
- 'data_title' => $order['order_number'],
- 'type' => ProductSnInfo::type_one,
- 'product_id' => $value['product_id'],
- 'code' => $value['code'],
- 'title' => $value['title'],
- 'sn' => "",
- 'customer_id' => $order['customer_id'],
- 'customer_name' => $order['customer_title'],
- 'customer_contact' => $order['customer_info'],
- 'vin_no' => $order['vin_no'],
- 'warranty_time' => $value['warranty_time'],
- 'construction_site_title' => $construction_site,
- 'start_time' => $order['end_time'],
- 'crt_time' => $time,
- 'crt_id' => $user['id'],
- // 'sn_type' => $value['sn_type'] ?? 0,
- 'sy_code' => $value['sy_code'] ?? "",
- ];
- }
- }
|