DataSyncToU8Service.php 7.2 KB

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