DataSyncToU8Service.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Construction;
  5. use App\Model\Product;
  6. use App\Model\ProductCategory;
  7. use App\Model\ProductSnInfo;
  8. use App\Model\PurchaseOrder;
  9. use App\Model\Setting;
  10. use App\Model\U8Job;
  11. use App\Model\Warranty;
  12. class DataSyncToU8Service extends Service
  13. {
  14. public function add($data,$user){
  15. list($status,$msg) = $this->orderRule($data);
  16. if(!$status) return [$status,$msg];
  17. //操作人
  18. $data['user_name'] = $user['emp_name'];
  19. // dd((new U8ServerService())->U8PO_PomainSave($data['id']));
  20. try{
  21. $job = ProcessDataJob::dispatch($data)->onQueue($data['job']);
  22. if(! $job) return [false,'任务没有进入队列!'];
  23. }catch (\Throwable $e){
  24. return [false,$e->getMessage()];
  25. }
  26. return [true,''];
  27. }
  28. public function orderRule(&$data){
  29. if(empty($data['type'])) return [false,'type不能为空!'];
  30. if(! in_array($data['type'],U8Job::$type)) return [false,'type不能存在!'];
  31. if(empty($data['id'])) return [false,'同步数据不能为空!'];
  32. $data['job'] = U8Job::$job[$data['type']] ?? "";
  33. if(empty($data['job'])) return [false,'未找到同步任务!'];
  34. if($data['type'] == U8Job::one){
  35. //采购同步校验
  36. $bool = PurchaseOrder::whereIn('id',$data['id'])
  37. ->where('del_time',0)
  38. ->where('supplier',0)
  39. ->exists();
  40. if($bool) return [false,'同步的采购单供应商不能为空!'];
  41. }
  42. return [true, ''];
  43. }
  44. public function snListAccording($data,$user){
  45. //特殊的门店
  46. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  47. $bt_top_depart_id = $setting['setting_value'] ?? [];
  48. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  49. //当前门店
  50. $current_top_depart_id = $user['depart_top'][0] ?? [];
  51. $current_top_depart_id = $current_top_depart_id['depart_id'] ?? 0;
  52. if(in_array($current_top_depart_id, $bt_top_depart_id)){
  53. //总社 直接读取sn码表
  54. $sn_type = 1;//总社
  55. }else{
  56. $sn_type = 2;//分社
  57. //发货单 sn码
  58. }
  59. return [true, ['sn_type' => $sn_type]];
  60. }
  61. public function snListAccordingForOrder($data_id){
  62. //特殊的门店
  63. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  64. $bt_top_depart_id = $setting['setting_value'] ?? [];
  65. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  66. //当前门店
  67. $current_top_depart_id = Construction::where('id', $data_id)->value('top_depart_id');
  68. if(in_array($current_top_depart_id, $bt_top_depart_id)){
  69. //总社 直接读取sn码表
  70. $sn_type = 1;//总社
  71. }else{
  72. $sn_type = 2;//分社
  73. //发货单 sn码
  74. }
  75. return $sn_type;
  76. }
  77. public function getSnFromU8($data, $user){
  78. if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];
  79. if(empty($data['code'])) return [false, '产品编码不能为空'];
  80. $data['depart_title'] = $this->getMyTopDepart($user,true);
  81. $service = new U8ServerService(true);
  82. if(! empty($service->error)) return [false, $service->error];
  83. list($status,$return) = $service->getSnList($data, $user);
  84. // list($status,$return) = $this->getSnList($data, $user);
  85. return [$status, $return];
  86. }
  87. //todo 废弃一
  88. public function getSnList($data, $user){
  89. $header = ['Content-Type:application/json'];
  90. $construction_id = $data['construction_id'] ?? 0;
  91. $post = [
  92. 'urlFromT9' => 'getSnList',
  93. 'code' => $data['code'],
  94. 'sn' => $data['sn'] ?? "",
  95. 'sn_type' => $data['sn_type'],
  96. 'depart_title' => $this->getMyTopDepart($user,true),
  97. 'construction_id' => $construction_id,
  98. 'page_size' => $data['page_size'] ?? 10,
  99. 'page_index' => $data['page_index'] ?? 1,
  100. ];
  101. $post = json_encode($post);
  102. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnList", $post, $header);
  103. if(! $status) return [false, "用友数据获取失败"];
  104. if($msg['code'] != 200) return [false, $msg['msg']];
  105. return [true, $msg['data'] ?? []];
  106. }
  107. /**
  108. * 校验sn码 施工单
  109. * @param $data
  110. * @param false $is_edit 是否补录
  111. * @return array|string
  112. */
  113. public function checkSnConstructionRule(&$data){
  114. //产品字典
  115. $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
  116. $data_id = $data['id'] ?? 0;
  117. $sn_map = [];
  118. if($data_id) $sn_map = $this->getSn($data, ProductSnInfo::type_one);
  119. $code = $sn = [];
  120. foreach ($data['product'] as $value){
  121. if(empty($value['code'])) return [false, '产品编码不能为空'];
  122. $code[] = $value['code'];
  123. //校验是否有不需要剔除的sn
  124. if($data_id){
  125. $sn_tmp = $sn_map[$value['code']] ?? [];
  126. foreach ($sn_tmp as $val){
  127. if(! empty($val['warranty_id']) && ! in_array($val['sn'], $value['product_sn_info'])) return [false, "产品编码:" . $value['code'] . "选择的sn码". $val['sn'] . "已生成质保信息,不允许删除"];
  128. }
  129. }
  130. //没有sn码信息直接跳过
  131. if(empty($value['product_sn_info'])) continue;
  132. $tmp = $map[$value['product_id']] ?? [];
  133. if(empty($tmp['warranty_time'])) return [false, "产品编码:" . $value['code'] . "质保时长(月)暂未设置"];
  134. $bool = $this->forCheck($tmp['product_category']);
  135. //非车窗膜需校验sn码
  136. if(! $bool){
  137. if(count($value['product_sn_info']) > $value['number']) return [false, "产品编码:" . $value['code'] . "选择的sn码数量不能超过产品数量"];
  138. }
  139. foreach ($value['product_sn_info'] as $sn_val){
  140. $sn[] = $sn_val;
  141. }
  142. }
  143. if(empty($sn)) return [true, ''];
  144. if(empty($data['sn_for_u8'])) return [false, "校验用友数据信息不能为空"];
  145. foreach ($data['sn_for_u8'] as $key => $value){
  146. if(empty($value['auto_id'])) return [false, 'AutoID不能为空'];
  147. if(empty($value['product_id'])) return [false, '产品ID不能为空'];
  148. $tmp = $map[$value['product_id']] ?? [];
  149. $bool = $this->forCheck($tmp['product_category']);
  150. if($bool) unset($data['sn_for_u8'][$key]);
  151. }
  152. //获取在库的sn码信息
  153. list($status, $msg) = $this->getSnForMap($code, $sn);
  154. if(! $status) return [false, $msg];
  155. //sn码map
  156. $sn_list = $msg;
  157. $sn_map = [];
  158. foreach ($sn_list as $value){
  159. $key = $value['code'] . $value['sn'];
  160. $sn_map[$key] = "";
  161. }
  162. //校验用友
  163. $submit_info = [];
  164. foreach ($data['product'] as $value){
  165. if(empty($value['product_sn_info'])) continue;
  166. foreach ($value['product_sn_info'] as $sn_val){
  167. $key = $value['code'] . $sn_val;
  168. $submit_info[] = $key;
  169. if(! isset($sn_map[$key])) return [false, "产品编码:" . $value['code'] . "的产品序列码:" . $sn_val . "在用友中不存在"];
  170. }
  171. }
  172. //校验sn是否被占用
  173. list($status, $msg) = $this->snForCheck($code, $sn, $submit_info, $data);
  174. if(! $status) return [false, $msg];
  175. return [true, ''];
  176. }
  177. public function forCheck($product_category){
  178. $category = json_decode($product_category,true);
  179. if(in_array(ProductCategory::Special_for_sn, $category)) return true;
  180. return false;
  181. }
  182. public function forCheck2($product_category){
  183. $category = json_decode($product_category,true);
  184. $id = ProductCategory::where('parent_id',ProductCategory::Special_for_roll_p)
  185. ->where('id','<>',ProductCategory::Special_for_roll3)
  186. ->select('id')
  187. ->get()->toArray();
  188. $id = array_column($id,'id');
  189. if(empty($id)) return false;
  190. foreach ($id as $value){
  191. if(in_array($value, $category)) return [true, ''];
  192. }
  193. return false;
  194. }
  195. //保存sn
  196. public function saveSn($data, $type, $time){
  197. $data_id = $data['id'] ?? 0;
  198. ProductSnInfo::where('del_time',0)
  199. ->where('data_id', $data_id)
  200. ->where('type', $type)
  201. ->update(['del_time' => $time]);
  202. $sn_type = $this->snListAccordingForOrder($data_id);
  203. $update = [];
  204. if(! empty($data['product'])){
  205. $insert = [];
  206. foreach ($data['product'] as $value){
  207. //没有sn码信息直接返回
  208. if(empty($value['product_sn_info'])) continue;
  209. foreach ($value['product_sn_info'] as $sn_val){
  210. $insert[] = [
  211. 'product_id' => $value['product_id'],
  212. 'code' => $value['code'],
  213. 'sn' => $sn_val['sn'],
  214. 'auto_id' => $sn_val['auto_id'],
  215. 'crt_time' => $time,
  216. 'data_id' => $data_id,
  217. 'type' => $type,
  218. 'sn_type' => $sn_type,
  219. ];
  220. if(! empty($sn_val['for_update'])) $update[] = $sn_val['auto_id'];
  221. }
  222. }
  223. if(! empty($update)) $this->updateSnInfo($sn_type, $update,$data_id);
  224. if(! empty($insert)) ProductSnInfo::insert($insert);
  225. }
  226. }
  227. //获取sn详情
  228. public function getSn($data, $type){
  229. $data_id = $data['id'] ?? 0;
  230. $map = [];
  231. $sn = ProductSnInfo::where('del_time',0)
  232. ->where('data_id', $data_id)
  233. ->where('type', $type)
  234. ->select('product_id','code','sn','warranty_id')
  235. ->get()->toArray();
  236. foreach ($sn as $value){
  237. $map[$value['code']][] = [
  238. 'sn' => $value['sn'],
  239. 'warranty_id' => $value['warranty_id'],
  240. ];
  241. }
  242. return $map;
  243. }
  244. //获取在库的sn码信息通过sn码和产品编码
  245. public function getSnForMap($code, $sn){
  246. $service = new U8ServerService(true);
  247. if(! empty($service->error)) return [false, $service->error];
  248. list($status, $msg) = $service->getSnListFormU8ForMap(['code' => $code, 'sn' => $sn]);
  249. return [$status, $msg];
  250. // todo 废弃二
  251. $header = ['Content-Type:application/json'];
  252. $post = [
  253. 'urlFromT9' => 'getSnMap',
  254. 'code' => $code,
  255. 'sn' => $sn,
  256. ];
  257. $post = json_encode($post);
  258. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnforMap", $post, $header);
  259. if(! $status) return [false, "用友数据获取失败"];
  260. if($msg['code'] != 200) return [false, $msg['msg']];
  261. return [true, $msg['data'] ?? []];
  262. }
  263. //获取在库的sn码信息通过sn码 客户手动创建质保
  264. public function getSnForWarranty($data){
  265. if(empty($data['sn'])) return [false, "产品序列码不能为空"];
  266. list($status, $msg) = $this->snForWarranty($data);
  267. if(! $status) return [false, $msg];
  268. $return = $msg;
  269. $product = Product::where('del_time',0)
  270. ->where('code', $return['code'])
  271. ->select('id','title','code','warranty_time')
  272. ->first();
  273. if(empty($product)) return [false, '根据产品序列码查找到的产品编码:' . $return['code'] . '在门店系统中未找到该产品'];
  274. $product = $product->toArray();
  275. $product['sn'] = $data['sn'];
  276. $product['auto_id'] = $return['auto_id'] ?? 0;
  277. return [true, $product];
  278. }
  279. //更新sn信息
  280. public function updateSnInfo($sn_type, $sn_for_u8,$data_id){
  281. $service = new U8ServerService(true);
  282. if(! empty($service->error)) return [false, $service->error];
  283. list($status, $msg) = $service->saveSnUseDataDetail($sn_type, $sn_for_u8, $data_id);
  284. return [$status, $msg];
  285. //todo 废弃三
  286. $header = ['Content-Type:application/json'];
  287. $post = [
  288. 'urlFromT9' => 'saveSnUseData',
  289. 'sn_type' => $sn_type,
  290. 'sn_for_u8' => $sn_for_u8,
  291. 'data_id' => $data_id,
  292. ];
  293. $post = json_encode($post);
  294. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/saveSnUseData", $post, $header);
  295. if(! $status) return [false, "用友数据获取失败"];
  296. if($msg['code'] != 200) return [false, $msg['msg']];
  297. return [true, ''];
  298. }
  299. public function snForWarranty($data){
  300. $service = new U8ServerService(true);
  301. if(! empty($service->error)) return [false, $service->error];
  302. list($status, $msg) = $service->getSnForWarrantyData($data);
  303. if(! $status) return [false, $msg];
  304. if(empty($msg)) return [false, '产品序列码不存在'];
  305. return [$status, $msg];
  306. //todo 废弃四
  307. $header = ['Content-Type:application/json'];
  308. $post = [
  309. 'urlFromT9' => 'getSnForWarranty',
  310. 'sn' => $data['sn'],
  311. ];
  312. $post = json_encode($post);
  313. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnForWarranty", $post, $header);
  314. if(! $status) return [false, "用友数据获取失败"];
  315. if($msg['code'] != 200) return [false, $msg['msg']];
  316. $return = $msg['data'] ?? [];
  317. if(empty($return)) return [false, '产品序列码不存在'];
  318. return [true, $return];
  319. }
  320. public function hasWarrantyInfo($data){
  321. if(empty($data['customer_contact'])) return [false, "手机号码不能为空"];
  322. $bool = Warranty::where('del_time',0)
  323. ->where('customer_contact', $data['customer_contact'])
  324. ->exists();
  325. return [true, ['hasWarrantyInfo' => $bool]];
  326. }
  327. //校验sn是否被占用
  328. public function snForCheck($code = [], $sn = [], $submit_info = [], $data = []){
  329. $save = ProductSnInfo::where('del_time',0)
  330. ->whereIn("code", $code)
  331. ->whereIn('sn', $sn)
  332. ->select('code','sn','data_id','type','product_id','warranty_id')
  333. ->get()->toArray();
  334. $category_map = Product::whereIn('id',array_unique(array_column($save,'product_id')))
  335. ->pluck('product_category','id')
  336. ->toArray();
  337. $data_id = $data['id'] ?? 0;
  338. $data_type = $data['data_type'] ?? ProductSnInfo::type_one;
  339. foreach ($save as $value){
  340. //车窗膜不管sn使用次数
  341. $category = $category_map[$value['product_id']] ?? "";
  342. $bool = $this->forCheck($category);
  343. if($bool) continue;
  344. $key = $value['code'] . $value['sn'];
  345. if(in_array($key, $submit_info)) {
  346. if(! $data_id){
  347. return [false, '产品编码:' . $value['code'] . '的sn码已在(' .ProductSnInfo::$type_name[$value['type']]. ')中使用'];
  348. }else{
  349. if($value['type'] == ProductSnInfo::type_three){
  350. if($value['warranty_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];
  351. }else{
  352. if($value['data_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];
  353. }
  354. }
  355. }
  356. }
  357. return [true, ''];
  358. }
  359. public function updateByVinNo($data){
  360. if(empty($data['vin_no']) || empty($data['customer_name']) || empty($data['customer_contact'])) return;
  361. $vin_no = $data['vin_no'];
  362. $customer_name = $data['customer_name'];
  363. $customer_contact = $data['customer_contact'];
  364. Warranty::where('del_time',0)
  365. ->where('vin_no',$vin_no)
  366. ->update(["customer_name" => $customer_name, 'customer_contact' => $customer_contact]);
  367. }
  368. }