DataSyncToU8Service.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. list($status,$return) = $this->getSnList($data, $user);
  81. return [$status, $return];
  82. }
  83. public function getSnList($data, $user){
  84. $header = ['Content-Type:application/json'];
  85. $construction_id = $data['construction_id'] ?? 0;
  86. $post = [
  87. 'urlFromT9' => 'getSnList',
  88. 'code' => $data['code'],
  89. 'sn' => $data['sn'] ?? "",
  90. 'sn_type' => $data['sn_type'],
  91. 'depart_title' => $this->getMyTopDepart($user,true),
  92. 'construction_id' => $construction_id,
  93. 'page_size' => $data['page_size'] ?? 10,
  94. 'page_index' => $data['page_index'] ?? 1,
  95. ];
  96. $post = json_encode($post);
  97. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnList", $post, $header);
  98. if(! $status) return [false, "用友数据获取失败"];
  99. if($msg['code'] != 200) return [false, $msg['msg']];
  100. return [true, $msg['data'] ?? []];
  101. }
  102. /**
  103. * 校验sn码 施工单
  104. * @param $data
  105. * @param false $is_edit 是否补录
  106. * @return array|string
  107. */
  108. public function checkSnConstructionRule(&$data){
  109. //产品字典
  110. $map = (new ProductService())->getProductDetail(array_column($data['product'],'product_id'));
  111. $data_id = $data['id'] ?? 0;
  112. $sn_map = [];
  113. if($data_id) $sn_map = $this->getSn($data, ProductSnInfo::type_one);
  114. $code = $sn = [];
  115. foreach ($data['product'] as $value){
  116. if(empty($value['code'])) return [false, '产品编码不能为空'];
  117. $code[] = $value['code'];
  118. //校验是否有不需要剔除的sn
  119. if($data_id){
  120. $sn_tmp = $sn_map[$value['code']] ?? [];
  121. foreach ($sn_tmp as $val){
  122. if(! empty($val['warranty_id']) && ! in_array($val['sn'], $value['product_sn_info'])) return [false, "产品编码:" . $value['code'] . "选择的sn码". $val['sn'] . "已生成质保信息,不允许删除"];
  123. }
  124. }
  125. //没有sn码信息直接跳过
  126. if(empty($value['product_sn_info'])) continue;
  127. $tmp = $map[$value['product_id']] ?? [];
  128. if(empty($tmp['warranty_time'])) return [false, "产品编码:" . $value['code'] . "质保时长(月)暂未设置"];
  129. $bool = $this->forCheck($tmp['product_category']);
  130. //非车窗膜需校验sn码
  131. if(! $bool){
  132. if(count($value['product_sn_info']) > $value['number']) return [false, "产品编码:" . $value['code'] . "选择的sn码数量不能超过产品数量"];
  133. }
  134. foreach ($value['product_sn_info'] as $sn_val){
  135. $sn[] = $sn_val;
  136. }
  137. }
  138. if(empty($sn)) return [true, ''];
  139. if(empty($data['sn_for_u8'])) return [false, "校验用友数据信息不能为空"];
  140. foreach ($data['sn_for_u8'] as $key => $value){
  141. if(empty($value['auto_id'])) return [false, 'AutoID不能为空'];
  142. if(empty($value['product_id'])) return [false, '产品ID不能为空'];
  143. $tmp = $map[$value['product_id']] ?? [];
  144. $bool = $this->forCheck($tmp['product_category']);
  145. if($bool) unset($data['sn_for_u8'][$key]);
  146. }
  147. //获取在库的sn码信息
  148. list($status, $msg) = $this->getSnForMap($code, $sn);
  149. if(! $status) return [false, $msg];
  150. //sn码map
  151. $sn_list = $msg['data'];
  152. $sn_map = [];
  153. foreach ($sn_list as $value){
  154. $key = $value['code'] . $value['sn'];
  155. $sn_map[$key] = "";
  156. }
  157. //校验用友
  158. $submit_info = [];
  159. foreach ($data['product'] as $value){
  160. if(empty($value['product_sn_info'])) continue;
  161. foreach ($value['product_sn_info'] as $sn_val){
  162. $key = $value['code'] . $sn_val;
  163. $submit_info[] = $key;
  164. if(! isset($sn_map[$key])) return [false, "产品编码:" . $value['code'] . "的产品序列码:" . $sn_val . "在用友中不存在"];
  165. }
  166. }
  167. //校验sn是否被占用
  168. list($status, $msg) = $this->snForCheck($code, $sn, $submit_info, $data);
  169. if(! $status) return [false, $msg];
  170. return [true, ''];
  171. }
  172. public function forCheck($product_category){
  173. $category = json_decode($product_category,true);
  174. if(in_array(ProductCategory::Special_for_sn, $category)) return true;
  175. return false;
  176. }
  177. public function forCheck2($product_category){
  178. $category = json_decode($product_category,true);
  179. if(in_array(ProductCategory::Special_for_roll, $category)) return true;
  180. return false;
  181. }
  182. //保存sn
  183. public function saveSn($data, $type, $time){
  184. $data_id = $data['id'] ?? 0;
  185. ProductSnInfo::where('del_time',0)
  186. ->where('data_id', $data_id)
  187. ->where('type', $type)
  188. ->update(['del_time' => $time]);
  189. $sn_type = $this->snListAccordingForOrder($data_id);
  190. $update = [];
  191. if(! empty($data['product'])){
  192. $insert = [];
  193. foreach ($data['product'] as $value){
  194. //没有sn码信息直接返回
  195. if(empty($value['product_sn_info'])) continue;
  196. foreach ($value['product_sn_info'] as $sn_val){
  197. $insert[] = [
  198. 'product_id' => $value['product_id'],
  199. 'code' => $value['code'],
  200. 'sn' => $sn_val['sn'],
  201. 'auto_id' => $sn_val['auto_id'],
  202. 'crt_time' => $time,
  203. 'data_id' => $data_id,
  204. 'type' => $type,
  205. 'sn_type' => $sn_type,
  206. ];
  207. if(! empty($sn_val['for_update'])) $update[] = $sn_val['auto_id'];
  208. }
  209. }
  210. if(! empty($update)) $this->updateSnInfo($sn_type, $update,$data_id);
  211. if(! empty($insert)) ProductSnInfo::insert($insert);
  212. }
  213. }
  214. //获取sn详情
  215. public function getSn($data, $type){
  216. $data_id = $data['id'] ?? 0;
  217. $map = [];
  218. $sn = ProductSnInfo::where('del_time',0)
  219. ->where('data_id', $data_id)
  220. ->where('type', $type)
  221. ->select('product_id','code','sn','warranty_id')
  222. ->get()->toArray();
  223. foreach ($sn as $value){
  224. $map[$value['code']][] = [
  225. 'sn' => $value['sn'],
  226. 'warranty_id' => $value['warranty_id'],
  227. ];
  228. }
  229. return $map;
  230. }
  231. //获取在库的sn码信息通过sn码和产品编码
  232. public function getSnForMap($code, $sn){
  233. $header = ['Content-Type:application/json'];
  234. $post = [
  235. 'urlFromT9' => 'getSnMap',
  236. 'code' => $code,
  237. 'sn' => $sn,
  238. ];
  239. $post = json_encode($post);
  240. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnforMap", $post, $header);
  241. if(! $status) return [false, "用友数据获取失败"];
  242. if($msg['code'] != 200) return [false, $msg['msg']];
  243. return [true, $msg];
  244. }
  245. //获取在库的sn码信息通过sn码 客户手动创建质保
  246. public function getSnForWarranty($data){
  247. if(empty($data['sn'])) return [false, "产品序列码不能为空"];
  248. list($status, $msg) = $this->snForWarranty($data);
  249. if(! $status) return [false, $msg];
  250. $return = $msg;
  251. $product = Product::where('del_time',0)
  252. ->where('code', $return['code'])
  253. ->select('id','title','code','warranty_time')
  254. ->first();
  255. if(empty($product)) return [false, '根据产品序列码查找到的产品编码:' . $return['code'] . '在门店系统中未找到该产品'];
  256. $product = $product->toArray();
  257. $product['sn'] = $data['sn'];
  258. $product['auto_id'] = $return['auto_id'] ?? 0;
  259. return [true, $product];
  260. }
  261. //更新sn信息
  262. public function updateSnInfo($sn_type, $sn_for_u8,$data_id){
  263. $header = ['Content-Type:application/json'];
  264. $post = [
  265. 'urlFromT9' => 'saveSnUseData',
  266. 'sn_type' => $sn_type,
  267. 'sn_for_u8' => $sn_for_u8,
  268. 'data_id' => $data_id,
  269. ];
  270. $post = json_encode($post);
  271. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/saveSnUseData", $post, $header);
  272. if(! $status) return [false, "用友数据获取失败"];
  273. if($msg['code'] != 200) return [false, $msg['msg']];
  274. return [true, ''];
  275. }
  276. public function snForWarranty($data){
  277. $header = ['Content-Type:application/json'];
  278. $post = [
  279. 'urlFromT9' => 'getSnForWarranty',
  280. 'sn' => $data['sn'],
  281. ];
  282. $post = json_encode($post);
  283. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/getSnForWarranty", $post, $header);
  284. if(! $status) return [false, "用友数据获取失败"];
  285. if($msg['code'] != 200) return [false, $msg['msg']];
  286. $return = $msg['data'] ?? [];
  287. if(empty($return)) return [false, '产品序列码不存在'];
  288. return [true, $return];
  289. }
  290. public function hasWarrantyInfo($data){
  291. if(empty($data['customer_contact'])) return [false, "手机号码不能为空"];
  292. $bool = Warranty::where('del_time',0)
  293. ->where('customer_contact', $data['customer_contact'])
  294. ->exists();
  295. return [true, ['hasWarrantyInfo' => $bool]];
  296. }
  297. //校验sn是否被占用
  298. public function snForCheck($code = [], $sn = [], $submit_info = [], $data = []){
  299. $save = ProductSnInfo::where('del_time',0)
  300. ->whereIn("code", $code)
  301. ->whereIn('sn', $sn)
  302. ->select('code','sn','data_id','type','product_id','warranty_id')
  303. ->get()->toArray();
  304. $category_map = Product::whereIn('id',array_unique(array_column($save,'product_id')))
  305. ->pluck('product_category','id')
  306. ->toArray();
  307. $data_id = $data['id'] ?? 0;
  308. $data_type = $data['data_type'] ?? ProductSnInfo::type_one;
  309. foreach ($save as $value){
  310. //车窗膜不管sn使用次数
  311. $category = $category_map[$value['product_id']] ?? "";
  312. $bool = $this->forCheck($category);
  313. if($bool) continue;
  314. $key = $value['code'] . $value['sn'];
  315. if(in_array($key, $submit_info)) {
  316. if(! $data_id){
  317. return [false, '产品编码:' . $value['code'] . '的sn码已在(' .ProductSnInfo::$type_name[$value['type']]. ')中使用'];
  318. }else{
  319. if($value['type'] == ProductSnInfo::type_three){
  320. if($value['warranty_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];
  321. }else{
  322. if($value['data_id'] != $data_id || $value['type'] != $data_type) return [false, '产品编码:' . $value['code'] . '的sn码已被' .ProductSnInfo::$type_name[$data_type] . '使用'];
  323. }
  324. }
  325. }
  326. }
  327. return [true, ''];
  328. }
  329. public function updateByVinNo($data){
  330. if(empty($data['vin_no']) || empty($data['customer_name']) || empty($data['customer_contact'])) return;
  331. $vin_no = $data['vin_no'];
  332. $customer_name = $data['customer_name'];
  333. $customer_contact = $data['customer_contact'];
  334. Warranty::where('del_time',0)
  335. ->where('vin_no',$vin_no)
  336. ->update(["customer_name" => $customer_name, 'customer_contact' => $customer_contact]);
  337. }
  338. }