123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace App\Service;
- use App\Jobs\ProcessDataJob;
- use App\Model\ProductCategory;
- use App\Model\ProductSnInfo;
- use App\Model\PurchaseOrder;
- use App\Model\Setting;
- use App\Model\U8Job;
- class DataSyncToU8Service extends Service
- {
- public function add($data,$user){
- list($status,$msg) = $this->orderRule($data);
- if(!$status) return [$status,$msg];
- //操作人
- $data['user_name'] = $user['emp_name'];
- // dd((new U8ServerService())->U8PO_PomainSave($data['id']));
- try{
- $job = ProcessDataJob::dispatch($data)->onQueue($data['job']);
- if(! $job) return [false,'任务没有进入队列!'];
- }catch (\Throwable $e){
- return [false,$e->getMessage()];
- }
- return [true,''];
- }
- public function orderRule(&$data){
- if(empty($data['type'])) return [false,'type不能为空!'];
- if(! in_array($data['type'],U8Job::$type)) return [false,'type不能存在!'];
- if(empty($data['id'])) return [false,'同步数据不能为空!'];
- $data['job'] = U8Job::$job[$data['type']] ?? "";
- if(empty($data['job'])) return [false,'未找到同步任务!'];
- if($data['type'] == U8Job::one){
- //采购同步校验
- $bool = PurchaseOrder::whereIn('id',$data['id'])
- ->where('del_time',0)
- ->where('supplier',0)
- ->exists();
- if($bool) return [false,'同步的采购单供应商不能为空!'];
- }
- return [true, ''];
- }
- public function snListAccording($data,$user){
- //特殊的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店
- $current_top_depart_id = $user['depart_top'][0] ?? [];
- $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
- if(in_array($current_top_depart_id, $bt_top_depart_id)){
- //总社 直接读取sn码表
- $sn_type = 1;//总社
- }else{
- $sn_type = 2;//分社
- //发货单 sn码
- }
- return [true, 'sn_type' => $sn_type];
- }
- public function getSnFromU8($data, $user){
- if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];
- if(empty($data['code'])) return [false, '产品编码不能为空'];
- list($status,$return) = $this->getSnList($data, $user);
- return [$status, $return];
- }
- public function getSnList($data, $user){
- $header = ['Content-Type:application/json'];
- $post = [
- 'urlFromT9' => 'getSnList',
- 'code' => $data['code'],
- 'sn' => $data['sn'] ?? "",
- 'sn_type' => $data['sn_type'],
- 'depart_title' => $this->getMyTopDepart($user,true),
- 'page_size' => $data['page_size'] ?? 10,
- 'page_index' => $data['page_index'] ?? 1,
- ];
- $post = json_encode($post);
- list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnList", $post, $header);
- if($msg['code'] != 200) return [false, $msg['msg']];
- return [true, $msg['data'] ?? []];
- }
- /**
- * 校验sn码 施工单
- * @param $data
- * @param false $is_edit 是否补录
- * @return array|string
- */
- public function checkSnConstructionRule($data, $is_edit = false){
- //产品字典
- $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
- $sn_map = [];
- if($is_edit) $sn_map = $this->getSn($data, ProductSnInfo::type_one);
- $code = $sn = [];
- foreach ($data['product'] as $value){
- if(empty($value['code'])) return [false, '产品编码不能为空'];
- $code[] = $value['code'];
- //补录校验
- if($is_edit){
- $sn_tmp = $sn_map[$value['code']] ?? [];
- foreach ($sn_tmp as $val){
- if(! empty($val['warranty_id']) && ! in_array($val['sn'], $value['product_sn_info'])) return [false, "产品编码:" . $value['code'] . "选择的sn码". $val['sn'] . "已生成质保,不允许删除"];
- }
- }
- //没有sn码信息直接跳过
- if(empty($value['product_sn_info'])) continue;
- $tmp = $map[$value['product_id']] ?? [];
- $category = json_decode($tmp['product_category'],true);
- //非车窗膜需校验sn码
- if(! in_array(ProductCategory::Special_for_sn, $category)){
- if(count($value['product_sn_info']) > $value['number']) return [false, "产品编码:" . $value['code'] . "选择的sn码数量不能超过产品数量"];
- }
- foreach ($value['product_sn_info'] as $sn_val){
- $sn[] = $sn_val;
- }
- }
- if(empty($sn)) return [true, ''];
- //获取在库的sn码信息
- list($status, $msg) = $this->getSnForMap($code, $sn);
- if(! $status) return [false, $msg];
- //sn码map
- $sn_list = $msg['data'];
- $sn_map = [];
- foreach ($sn_list as $value){
- $key = $value['code'] . $value['sn'];
- $sn_map[$key] = "";
- }
- //校验用友
- $submit_info = [];
- foreach ($data['product'] as $value){
- if(empty($value['product_sn_info'])) continue;
- foreach ($value['product_sn_info'] as $sn_val){
- $key = $value['code'] . $sn_val;
- $submit_info[] = $key;
- if(! isset($sn_map[$key])) return [false, "产品编码:" . $value['code'] . "的sn序列码:" . $sn_val . "在用友中不存在"];
- }
- }
- //校验sn是否被占用
- list($status, $msg) = $this->snForCheck($code, $sn, $submit_info, $data);
- if(! $status) return [false, $msg];
- return [true, ''];
- }
- //保存sn
- public function saveSn($data, $type, $time){
- $data_id = $data['id'] ?? 0;
- ProductSnInfo::where('del_time',0)
- ->where('data_id', $data_id)
- ->where('type', $type)
- ->update(['del_time' => $time]);
- if(! empty($data['product'])){
- $insert = [];
- foreach ($data['product'] as $value){
- //没有sn码信息直接返回
- if(empty($value['product_sn_info'])) continue;
- foreach ($value['product_sn_info'] as $sn_val){
- $insert[] = [
- 'product_id' => $value['product_id'],
- 'code' => $value['code'],
- 'sn' => $sn_val,
- 'crt_time' => $time,
- 'data_id' => $data_id,
- 'type' => $type,
- ];
- }
- }
- if(! empty($insert)) ProductSnInfo::insert($insert);
- }
- }
- //获取sn详情
- public function getSn($data, $type){
- $data_id = $data['id'] ?? 0;
- $map = [];
- $sn = ProductSnInfo::where('del_time',0)
- ->where('data_id', $data_id)
- ->where('type', $type)
- ->select('product_id','code','sn','warranty_id')
- ->get()->toArray();
- foreach ($sn as $value){
- $map[$value['code']][] = [
- 'sn' => $value['sn'],
- 'warranty_id' => $value['warranty_id'],
- ];
- }
- return $map;
- }
- //获取在库的sn码信息
- private function getSnForMap($code, $sn){
- $header = ['Content-Type:application/json'];
- $post = [
- 'urlFromT9' => 'getSnMap',
- 'code' => $code,
- 'sn' => $sn,
- ];
- $post = json_encode($post);
- list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnforMap", $post, $header);
- if($msg['code'] != 200) return [false, $msg['msg']];
- return [true, $msg];
- }
- //校验sn是否被占用
- private function snForCheck($code = [], $sn = [], $submit_info = [], $data){
- $save = ProductSnInfo::where('del_time',0)
- ->whereIn("code", $code)
- ->whereIn('sn', $sn)
- ->select('code','sn','data_id','type')
- ->get()->toArray();
- $data_id = $data['id'] ?? 0;
- foreach ($save as $value){
- $key = $value['code'] . $value['sn'];
- if(in_array($key, $submit_info)) {
- if(! $data_id){
- return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$value['type']]] . '使用';
- }else{
- if($value['data_id'] != $data_id || $value['type'] != ProductSnInfo::type_one) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$value['type']]] . '使用';
- }
- }
- }
- return [true, ''];
- }
- }
|