DataSyncToU8Service.php 12 KB

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