JRFIDServerService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Config;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Redis;
  7. class JRFIDServerService extends Service
  8. {
  9. public static function getToken(){
  10. $token_key = config("j_rfid.LoginRedisTopic");
  11. $token = Redis::get($token_key);
  12. if(! $token){
  13. $url = config('ip.zs');
  14. $post = array("name" => "admin","password"=>"admin","rememberMe"=>true);
  15. $header = ['Content-Type:application/json'];
  16. $curl = curl_init();
  17. curl_setopt_array($curl, array(
  18. CURLOPT_URL => $url . 'jbl/api/mes/login',
  19. CURLOPT_RETURNTRANSFER => true,
  20. CURLOPT_ENCODING => '',
  21. CURLOPT_MAXREDIRS => 10,
  22. CURLOPT_TIMEOUT => 0,
  23. CURLOPT_FOLLOWLOCATION => true,
  24. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  25. CURLOPT_CUSTOMREQUEST => 'POST',
  26. CURLOPT_POSTFIELDS => json_encode($post),
  27. CURLOPT_HTTPHEADER => $header,
  28. ));
  29. $response = curl_exec($curl);
  30. curl_close($curl);
  31. $result = json_decode($response,true);
  32. if(empty($result['token'])) {
  33. file_put_contents('big_king_token_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  34. return [false,''];
  35. }else{
  36. $token = $result['token'];
  37. $expire_time = 1728000; //20天
  38. Redis::set($token_key,$token);
  39. Redis::expire($token_key, $expire_time);
  40. return [true,$token];
  41. }
  42. }
  43. return [true,$token];
  44. }
  45. //销售订单(合同)保存
  46. public function U8SaleOrderSave($data,$cmaker = ""){
  47. if(! is_array($data)) $data = [$data];
  48. $id = $data;
  49. //映射ip是否通畅
  50. $bool = $this->isDomainAvailable($this->u8['domain']);
  51. if(! $bool) {
  52. $msg = 'U8程序外部域名不可达';
  53. $this->finalSettle($id, U8Job::two, $msg);
  54. return;
  55. }
  56. //获取数据
  57. $result = $this->getSaleOrderData($id);
  58. if(empty($result)) {
  59. $msg = "同步数据获取失败";
  60. $this->finalSettle($id, U8Job::two, $msg);
  61. return;
  62. }
  63. //u8接口参数组织
  64. $post = $this->post_common;
  65. $post['entity'] = "U8SaleOrderSave";
  66. $time = date("Y-m-d");
  67. $time1 = date("Y-m-d H:i:s");
  68. foreach ($result as $value){
  69. $bodys = [];
  70. // $cdefine31 = "";
  71. // if(! empty($value['cstname']) && $value['cstname'] == "线下销售") $cdefine31 = $value['cstname'];
  72. foreach ($value['product'] as $son){
  73. //子表数据
  74. $bodys[] = [
  75. "cinvcode"=>$son['code'], //存货编码
  76. "iquantity"=>$son['number'], //数量
  77. "inum"=>$son['number'], //件数
  78. "itaxrate"=>$son['itaxrate'], //税率
  79. "iunitprice"=>$son['iunitprice'],//原币单价
  80. "itaxunitprice"=>$son['itaxunitprice'], // 原币含税单价
  81. "isum"=>$son['isum'], //原币价税合计
  82. "imoney"=>$son['imoney'], //原币无税金额
  83. "itax"=>$son['itax'],//原币税额
  84. "cbmemo"=>$son['mark'], //表体备注
  85. // "iappids"=>"", //子表id
  86. // "cinvcode"=>$son['code'], //存货编码
  87. // "iquantity"=>$son['number'], //数量
  88. // "inum"=>$son['number'], //件数
  89. // "ipertaxrate"=>$son['ipertaxrate'], //税率
  90. // "iunitprice"=>$son['iunitprice'], //原币单价
  91. // "itaxprice"=>$son['itaxprice'], //原币含税单价
  92. // "isum"=>$son['isum'], //原币价税合计
  93. // "imoney"=>$son['imoney'], //原币无税金额
  94. // "itax"=>$son['itax'],//原币税额
  95. // "cbmemo"=>$son['mark'], //表体备注
  96. "cdefine22"=>$son['cdefine22'], //手机号码
  97. "cdefine23"=>"",
  98. "cdefine24"=>"",
  99. "cdefine25"=>$son['cdefine25'], //平台类型
  100. "cdefine26"=>"",
  101. "cdefine27"=>"",
  102. "cdefine28"=>$son['cdefine28'], //平台单号
  103. "cdefine29"=>$son['cdefine29'], //分社施工
  104. "cdefine30"=>$son['cdefine30'], //业务员
  105. "cdefine31"=>$son['cdefine31'], //客户名称
  106. "cdefine32"=>$son['cdefine32'], //直播销售
  107. "cdefine33"=>"",
  108. "cdefine34"=>"",
  109. "cdefine35"=>"",
  110. "cdefine36"=>"",
  111. "cdefine37"=>"",
  112. "cfree1"=>"",
  113. "cfree2"=>"",
  114. "cfree3"=>"",
  115. "cfree4"=>"",
  116. "cfree5"=>"",
  117. "cfree6"=>"",
  118. "cfree7"=>"",
  119. "cfree8"=>"",
  120. "cfree9"=>"",
  121. "cfree10"=>""
  122. ];
  123. }
  124. //最终数据
  125. $post['data'] = [
  126. "csocode"=>'',
  127. "ddate"=> $time,
  128. "cmaker"=>$cmaker ?? 'admin',
  129. "dcreatesystime"=>$time1,
  130. "cstcode"=>"",
  131. "cbustype" => $value['cbustype'], //业务类型
  132. "cstname"=>$value['cstname'], //销售类型
  133. "ccuscode"=>"",
  134. "ccusabbname"=>$value['ccusabbname'], //客户简称
  135. "cdepcode"=>"",
  136. "cdepname"=>"", // 部门名称 $value['cdepname']
  137. "cpersoncode"=>"", //业务员编码 暂时不要
  138. "jobnumber"=>$value['jobnumber'],//业务员工号
  139. "itaxrate"=>"0",
  140. "cmemo"=>$value['mark'],//"T9销售订单:". $value['order_number']
  141. "cdefine1"=>"",
  142. "cdefine2"=>"",
  143. "cdefine3"=>"",
  144. "cdefine4"=>"",
  145. "cdefine5"=>"",
  146. "cdefine6"=>"",
  147. "cdefine7"=>"",
  148. "cdefine8"=>"",
  149. "cdefine9"=>"",
  150. "cdefine10"=>"",
  151. "cdefine11"=>"",
  152. "cdefine12"=>"",
  153. "cdefine13"=>"",
  154. "cdefine14"=>"",
  155. "cdefine15"=>"",
  156. "cdefine16"=>"",
  157. "bodys"=>$bodys
  158. ];
  159. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  160. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  161. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  162. //剔除数据
  163. $id = array_diff($id, [$value['id']]);
  164. if(empty($return)) {
  165. $msg = '异常错误,请确认请求接口地址或地址不可达';
  166. $this->finalSettle($value['id'],U8Job::two, $msg);
  167. }else{
  168. if( ! empty($return['flag'])){
  169. $this->finalSettle($value['id'],U8Job::two);
  170. }else{
  171. $this->finalSettle($value['id'], U8Job::two, $return['msg']);
  172. }
  173. }
  174. }
  175. if(! empty($id)){
  176. $msg = "未找到同步数据";
  177. $this->finalSettle($id,U8Job::two, $msg);
  178. }
  179. }
  180. //最终处理
  181. public function finalSettle($data,$data_type, $msg = ''){
  182. if(! is_array($data)) $data = [$data];
  183. $time = time();
  184. $insert = [];
  185. U8Job::where('del_time',0)
  186. ->where('data_type',$data_type)
  187. ->whereIn('data',$data)
  188. ->update(['del_time' => $time]);
  189. foreach ($data as $value){
  190. if(empty($msg)){
  191. $insert[] = [
  192. 'data' => $value,
  193. 'data_type' => $data_type,
  194. 'crt_time' => $time,
  195. 'state' => U8Job::success,
  196. ];
  197. }else{
  198. $insert[] = [
  199. 'data' => $value,
  200. 'data_type' => $data_type,
  201. 'crt_time' => $time,
  202. 'state' => U8Job::failed,
  203. 'msg' => $msg
  204. ];
  205. }
  206. }
  207. U8Job::insert($insert);
  208. }
  209. public function getPurchaseData($id){
  210. $main = PurchaseOrder::whereIn('id',$id)
  211. ->where('del_time',0)
  212. ->get()->toArray();
  213. if(empty($main)) return [];
  214. $supplier = Supplier::whereIn('id',array_unique(array_column($main,'supplier')))
  215. ->pluck('title','id')
  216. ->toArray();
  217. // $depart = Depart::whereIn('id',array_unique(array_column($main,'depart_id')))
  218. // ->pluck('title','id')
  219. // ->toArray();
  220. $emp = Employee::whereIn('id',array_unique(array_column($main,'purchase_id')))
  221. ->pluck('number','id')
  222. ->toArray();
  223. $code_map = BasicType::whereIn('id',array_unique(array_column($main,'purchase_type')))
  224. ->pluck('title','id')
  225. ->toArray();
  226. $sub = PurchaseOrderInfo::whereIn('purchase_order_id',$id)
  227. ->where('del_time',0)
  228. ->get()->toArray();
  229. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  230. ->get()->toArray();
  231. $product_map = array_column($product,null,'id');
  232. $sub_map = [];
  233. foreach ($sub as $value){
  234. $product_tmp = $product_map[$value['product_id']] ?? [];
  235. $value['code'] = $product_tmp['code'];
  236. //计算金额
  237. if($value['rate'] > 0){
  238. // ipertaxrate 税率
  239. // iunitprice 原币单价
  240. // itaxprice 原币含税单价
  241. // isum 原币价税合计
  242. // imoney 原币无税金额
  243. // itax 原币税额
  244. $value['ipertaxrate'] = $value['rate'];
  245. $rate = round($value['rate'] / 100,2);
  246. $value['iunitprice'] = round($value['price'] / (1 + $rate),2);
  247. $value['itaxprice'] = $value['price'];
  248. $value['isum'] = round($value['price'] * $value['number'],2);
  249. $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  250. $value['itax'] = round($value['isum'] - $value['imoney'],2);
  251. }else{
  252. $value['ipertaxrate'] = 0;
  253. $value['iunitprice'] = $value['price'];
  254. $value['itaxprice'] = $value['price'];
  255. $value['isum'] = $value['price'] * $value['number'];
  256. $value['imoney'] = $value['isum'];
  257. $value['itax'] = 0;
  258. }
  259. $sub_map[$value['purchase_order_id']][] = $value;
  260. }
  261. foreach ($main as $key => $value){
  262. $main[$key]['cptname'] = $code_map[$value['purchase_type']] ?? "";
  263. $main[$key]['cvenname'] = $supplier[$value['supplier']] ?? "";
  264. // $main[$key]['cdepname'] = $depart[$value['depart_id']] ?? "";
  265. // $main[$key]['cpersoncode'] = $emp[$value['purchase_id']] ?? "";
  266. $main[$key]['jobnumber'] = $emp[$value['purchase_id']] ?? "";
  267. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  268. }
  269. return $main;
  270. }
  271. public function getSaleOrderData($id){
  272. $main = SalesOrder::whereIn('id',$id)
  273. ->where('del_time',0)
  274. ->get()->toArray();
  275. if(empty($main)) return [];
  276. $main_map = array_column($main,null,'id');
  277. $sub = SalesOrderProductInfo::whereIn('sales_order_id',$id)
  278. ->where('del_time',0)
  279. ->get()->toArray();
  280. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  281. ->get()->toArray();
  282. $product_map = array_column($product,null,'id');
  283. $code_id = array_filter(array_unique(array_merge_recursive(array_column($main,'sale_type'),array_column($main,'plat_type'),array_column($main,'install_position'),array_column($main,'customer_short_name'))));
  284. $code_map = BasicType::whereIn('id',$code_id)
  285. ->pluck('title','id')
  286. ->toArray();
  287. $customer_map = Customer::whereIn('id',array_unique(array_column($main,'customer_id')))
  288. ->pluck('title','id')
  289. ->toArray();
  290. // $depart = Depart::where('parent_id',0)
  291. // ->pluck('title','id')
  292. // ->toArray();
  293. $empList = Employee::whereIn('id',array_unique(array_column($main,'crt_id')))
  294. ->select('number','id','emp_name')
  295. ->get()
  296. ->toArray();
  297. $emp = array_column($empList,'number','id');
  298. $emp2 = array_column($empList,'emp_name','id');
  299. // $see = SeeRange::where('del_time',0)
  300. // ->whereIn('data_id',array_column($main,'id'))
  301. // ->where('data_type',SeeRange::type_seven)
  302. // ->where('type',SeeRange::data_three)
  303. // ->pluck('param_id','data_id')->toArray();//指派的分社
  304. $sub_map = [];
  305. foreach ($sub as $value){
  306. $product_tmp = $product_map[$value['product_id']] ?? [];
  307. $main_tmp = $main_map[$value['sales_order_id']] ?? [];
  308. $position = $code_map[$main_tmp['install_position']] ?? "";
  309. if($main_tmp['model_type'] == SalesOrder::Model_type_four){
  310. //线上订单
  311. $cdefine28 = $main_tmp['plat_order'] ?? "";
  312. }else{
  313. $cdefine28 = $main_tmp['order_number'] ?? "";
  314. }
  315. // "itaxrate"=>$son['itaxrate'], //税率
  316. // "iunitprice"=>$son['iunitprice'],//原币单价
  317. // "itaxunitprice"=>$son['itaxunitprice'], // 原币含税单价
  318. // "isum"=>$son['isum'], //原币价税合计
  319. // "imoney"=>$son['imoney'], //原币无税金额
  320. // "itax"=>$son['itax'],//原币税额
  321. //计算金额
  322. //比如这4个产品合同金额是300.11,那么价税合计就是300.11,
  323. //含税单价就是300.11/4,目前加了税率的没有计算规则。税率不进入计算
  324. $value['itaxrate'] = 0;//税率
  325. $value['iunitprice'] = $value['price'];
  326. $value['itaxunitprice'] = $value['price'];
  327. $value['isum'] = $value['final_amount']; //原币价税合计
  328. $value['imoney'] = $value['isum']; //原币无税金额
  329. $value['itax'] = 0;
  330. // if($value['rate'] > 0){
  331. // $value['itaxrate'] = $value['rate'];
  332. // $rate = round($value['rate'] / 100,2);
  333. // $value['iunitprice'] = round($value['final_amount'] / (1 + $rate),2);
  334. // $value['itaxunitprice'] = $value['final_amount'];
  335. // $value['isum'] = round($value['final_amount'] * $value['number'],2);
  336. // $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  337. // $value['itax'] = round($value['isum'] - $value['imoney'],2);
  338. // }else{
  339. // $value['itaxrate'] = 0;
  340. // $value['iunitprice'] = $value['final_amount'];
  341. // $value['itaxunitprice'] = $value['final_amount'];
  342. // $value['isum'] = $value['final_amount'] * $value['number'];
  343. // $value['imoney'] = $value['isum'];
  344. // $value['itax'] = 0;
  345. // }
  346. $value['cdefine25'] = $code_map[$main_tmp['plat_type']] ?? ""; //平台类型
  347. $value['cdefine28'] = $cdefine28; //平台单号
  348. // $top_depart_id = $see[$value['sales_order_id']] ?? 0;
  349. $value['cdefine29'] = $position ?? "";//安装地点
  350. $value['cdefine32'] = "";//直播销售 暂时没有
  351. $value['cdefine31'] = $customer_map[$main_tmp['customer_id']] ?? "";//客户名称
  352. $value['cdefine22'] = $main_tmp['customer_contact'] ?? "";//手机号码
  353. // $value['cdefine31'] = "";//客户名称(线上的时候就是空 线下的话就是表头的客户简称)
  354. // $value['cdefine22'] = "";//手机号码 暂时没有
  355. $value['cdefine30'] = $emp2[$main_tmp['crt_id']] ?? "";//业务员
  356. $value['code'] = $product_tmp['code'];//存货编码
  357. $sub_map[$value['sales_order_id']][] = $value;
  358. }
  359. foreach ($main as $key => $value){
  360. $customer_short_name = $code_map[$value['customer_short_name']] ?? "";
  361. // if($plat_type == "营销部"){
  362. // $ccusabbname = "营销部客户";
  363. // }else{
  364. // $ccusabbname = $customer_map[$value['customer_id']] ?? "";
  365. // }
  366. $main[$key]['cbustype'] = "普通销售"; //业务类型(本身就是中文)
  367. $main[$key]['cstname'] = SalesOrder::$model_type_title[$value['model_type']] ?? ""; //销售类型
  368. $main[$key]['ccusabbname'] = $customer_short_name;//客户简称
  369. // $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";//部门名称
  370. // $main[$key]['cpersoncode'] = $emp[$value['crt_id']] ?? "";//业务员
  371. $main[$key]['jobnumber'] = $emp[$value['crt_id']] ?? "";
  372. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  373. }
  374. return $main;
  375. }
  376. public function post_helper($url, $data, $header = [], $timeout = 20){
  377. $ch = curl_init();
  378. curl_setopt($ch, CURLOPT_URL, $url);
  379. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  380. curl_setopt($ch, CURLOPT_ENCODING, '');
  381. curl_setopt($ch, CURLOPT_POST, 1);
  382. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  383. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  384. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  385. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  386. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  387. $r = curl_exec($ch);
  388. curl_close($ch);
  389. return json_decode($r, true);
  390. }
  391. }