DataSyncToU8Service.php 8.9 KB

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