U8ServerService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\Depart;
  6. use App\Model\Employee;
  7. use App\Model\Product;
  8. use App\Model\PurchaseOrder;
  9. use App\Model\PurchaseOrderInfo;
  10. use App\Model\SalesOrder;
  11. use App\Model\SalesOrderProductInfo;
  12. use App\Model\SeeRange;
  13. use App\Model\Setting;
  14. use App\Model\Supplier;
  15. use App\Model\U8Job;
  16. use Illuminate\Support\Facades\Config;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. class U8ServerService extends Service
  21. {
  22. public $db = null;
  23. public $error = null; // 错误信息
  24. public $u8 = []; //u8 配置连接参数
  25. public $u8_api = ""; //u8接口请求地址
  26. public $post_common = [];//u8接口公用参数
  27. public function __construct($is_need_connect = 0)
  28. {
  29. //u8 配置连接参数 u8_api设置
  30. list($status,$msg) = $this->settingConnection();
  31. if(! $status) {
  32. $this->error = $msg;
  33. return;
  34. }
  35. //是否需要连接u8数据库
  36. if($is_need_connect){
  37. //构建数据库连接对象
  38. list($status,$msg) = $this->settingDb();
  39. if(! $status) {
  40. $this->error = $msg;
  41. }
  42. }
  43. }
  44. //设置u8连接参数
  45. private function settingConnection(){
  46. $u8 = Setting::where('setting_name','u8')->where('setting_value','<>','')->first();
  47. if(empty($u8)) return [false, 'u8配置参数不存在!'];
  48. $u8 = $u8->toArray();
  49. // 使用 eval() 函数执行字符串并转换为数组
  50. $u8 = eval("return {$u8['setting_value']};");
  51. if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
  52. if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
  53. if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
  54. if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
  55. if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
  56. if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
  57. if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
  58. if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
  59. if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
  60. if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
  61. $this->u8 = $u8;
  62. $this->u8_api = "https://" . $u8['domain'] . ":" . $u8['u8_api_port'] . "/U8Sys/U8API";
  63. $this->post_common = [
  64. "password"=>"cloud@123456",
  65. "entity"=>"", //调用方法
  66. "login"=>[
  67. "sAccID"=> $u8['sAccID'],
  68. "sDate"=> date("Y-m-d"),
  69. "sServer"=> $u8['sServer'],
  70. "sUserID"=> $u8['sUserID'],
  71. "sSerial"=> "",
  72. "sPassword"=> $u8['sPassword']
  73. ]
  74. ];
  75. return [true, ''];
  76. }
  77. //设置u8数据库连接
  78. private function settingDb(){
  79. if(empty($this->db)){
  80. $u8 = $this->u8;
  81. $config = [
  82. 'driver' => 'sqlsrv',
  83. 'host' => $u8['domain'],
  84. 'port' => $u8['u8_database_port'],
  85. 'database' => $u8['database'],
  86. 'username' => $u8['database_account'],
  87. 'password' => $u8['database_password'],
  88. ];
  89. // 数据库配置设置
  90. Config::set('database.connections.sqlsrvs', $config);
  91. // 连接
  92. try {
  93. $pdo = DB::connection('sqlsrvs')->getPdo();
  94. if ($pdo instanceof \PDO) {
  95. // 连接成功的逻辑代码
  96. $this->db = DB::connection('sqlsrvs');
  97. } else {
  98. return [false, '连接失败!'];
  99. }
  100. } catch (\Throwable $e) {
  101. return [false, $e->getMessage()];
  102. }
  103. }
  104. return [true, ''];
  105. }
  106. //采购订单保存
  107. public function U8PO_PomainSave($data){
  108. $id = $data;
  109. //映射ip是否通畅
  110. $bool = $this->isDomainAvailable($this->u8['domain']);
  111. if(! $bool) {
  112. $msg = 'U8程序外部域名不可达';
  113. $this->finalSettle($id, U8Job::one,$msg);
  114. return;
  115. }
  116. //获取数据
  117. $result = $this->getPurchaseData($id);
  118. if(empty($result)) {
  119. $msg = "同步数据获取失败";
  120. $this->finalSettle($id, U8Job::one, $msg);
  121. return;
  122. }
  123. //u8接口参数组织
  124. $post = $this->post_common;
  125. $post['entity'] = "U8PO_PomainSave";
  126. $time = date("Y-m-d");
  127. foreach ($result as $value){
  128. $bodys = [];
  129. foreach ($value['product'] as $son){
  130. //子表数据
  131. $bodys[] = [
  132. "iappids"=>"", //子表id
  133. "cinvcode"=>$son['code'], //存货编码
  134. "iquantity"=>$son['number'], //数量
  135. "inum"=>$son['number'], //件数
  136. "ipertaxrate"=>$son['ipertaxrate'], //税率
  137. "iunitprice"=>$son['iunitprice'], //原币单价
  138. "itaxprice"=>$son['itaxprice'], //原币含税单价
  139. "isum"=>$son['isum'], //原币价税合计
  140. "imoney"=>$son['imoney'], //原币无税金额
  141. "itax"=>$son['itax'],//原币税额
  142. "cbmemo"=>$son['mark'], //表体备注
  143. "cdefine22"=>"",
  144. "cdefine23"=>"",
  145. "cdefine24"=>"",
  146. "cdefine25"=>"",
  147. "cdefine26"=>"",
  148. "cdefine27"=>"",
  149. "cdefine28"=>"",
  150. "cdefine29"=>"",
  151. "cdefine30"=>"",
  152. "cdefine31"=>"",
  153. "cdefine32"=>"",
  154. "cdefine33"=>"",
  155. "cdefine34"=>"",
  156. "cdefine35"=>"",
  157. "cdefine36"=>"",
  158. "cdefine37"=>"",
  159. "cfree1"=>"",
  160. "cfree2"=>"",
  161. "cfree3"=>"",
  162. "cfree4"=>"",
  163. "cfree5"=>"",
  164. "cfree6"=>"",
  165. "cfree7"=>"",
  166. "cfree8"=>"",
  167. "cfree9"=>"",
  168. "cfree10"=>""
  169. ];
  170. }
  171. //最终数据
  172. $post['data'] = [
  173. "cpoid"=>"",
  174. "dpodate"=>date("Y-m-d",$value['crt_time']),
  175. "cmemo"=>"T9采购单:" . $value['order_number'],
  176. "cmaker"=>"admin",
  177. "cmaketime"=>$time,
  178. "IsExamine"=>false,
  179. "cvencode"=>"", //供应商编码
  180. "cvenname"=>$value['cvenname'], //供应商名称
  181. "cdepcode"=>"", //部门编号
  182. "cdepname"=>$value['cdepname'], //部门名称
  183. "cpersoncode"=>$value['cpersoncode'], //业务员编码
  184. "cdefine1"=>"",
  185. "cdefine2"=>"",
  186. "cdefine3"=>"",
  187. "cdefine4"=>"",
  188. "cdefine5"=>"",
  189. "cdefine6"=>"",
  190. "cdefine7"=>"",
  191. "cdefine8"=>"",
  192. "cdefine9"=>"",
  193. "cdefine10"=>"",
  194. "cdefine11"=>"",
  195. "cdefine12"=>"",
  196. "cdefine13"=>"",
  197. "cdefine14"=>"",
  198. "cdefine15"=>"",
  199. "cdefine16"=>"",
  200. "bodys"=>$bodys
  201. ];
  202. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  203. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  204. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  205. //剔除数据
  206. $id = array_diff($id, [$value['id']]);
  207. if(empty($return)) {
  208. $msg = '异常错误,请确认请求接口地址或地址不可达';
  209. $this->finalSettle($value['id'], U8Job::one, $msg);
  210. }else{
  211. if( ! empty($return['flag'])){
  212. $this->finalSettle($value['id'], U8Job::one);
  213. }else{
  214. $this->finalSettle($value['id'], U8Job::one, $return['msg']);
  215. }
  216. }
  217. }
  218. if(! empty($id)){
  219. $msg = "未找到同步数据";
  220. $this->finalSettle($id, U8Job::one, $msg);
  221. }
  222. }
  223. //销售订单(合同)保存
  224. public function U8SaleOrderSave($data){
  225. $id = $data;
  226. //映射ip是否通畅
  227. $bool = $this->isDomainAvailable($this->u8['domain']);
  228. if(! $bool) {
  229. $msg = 'U8程序外部域名不可达';
  230. $this->finalSettle($id, U8Job::two, $msg);
  231. return;
  232. }
  233. //获取数据
  234. $result = $this->getSaleOrderData($id);
  235. if(empty($result)) {
  236. $msg = "同步数据获取失败";
  237. $this->finalSettle($id, U8Job::two, $msg);
  238. return;
  239. }
  240. //u8接口参数组织
  241. $post = $this->post_common;
  242. $post['entity'] = "U8SaleOrderSave";
  243. $time = date("Y-m-d");
  244. $time1 = date("Y-m-d H:i:s");
  245. foreach ($result as $value){
  246. $bodys = [];
  247. $cdefine31 = "";
  248. if(! empty($value['cstname']) && $value['cstname'] == "线下销售") $cdefine31 = $value['cstname'];
  249. foreach ($value['product'] as $son){
  250. //子表数据
  251. $bodys[] = [
  252. "iappids"=>"", //子表id
  253. "cinvcode"=>$son['code'], //存货编码
  254. "iquantity"=>$son['number'], //数量
  255. "inum"=>$son['number'], //件数
  256. "ipertaxrate"=>$son['ipertaxrate'], //税率
  257. "iunitprice"=>$son['iunitprice'], //原币单价
  258. "itaxprice"=>$son['itaxprice'], //原币含税单价
  259. "isum"=>$son['isum'], //原币价税合计
  260. "imoney"=>$son['imoney'], //原币无税金额
  261. "itax"=>$son['itax'],//原币税额
  262. "cbmemo"=>$son['mark'], //表体备注
  263. "cdefine22"=>$son['cdefine22'], //手机号码
  264. "cdefine23"=>"",
  265. "cdefine24"=>"",
  266. "cdefine25"=>$son['cdefine25'], //平台类型
  267. "cdefine26"=>"",
  268. "cdefine27"=>"",
  269. "cdefine28"=>$son['cdefine28'], //平台单号
  270. "cdefine29"=>$son['cdefine29'], //分社施工
  271. "cdefine30"=>$son['cdefine30'], //业务员
  272. "cdefine31"=>$cdefine31, //客户名称
  273. "cdefine32"=>$son['cdefine32'], //直播销售
  274. "cdefine33"=>"",
  275. "cdefine34"=>"",
  276. "cdefine35"=>"",
  277. "cdefine36"=>"",
  278. "cdefine37"=>"",
  279. "cfree1"=>"",
  280. "cfree2"=>"",
  281. "cfree3"=>"",
  282. "cfree4"=>"",
  283. "cfree5"=>"",
  284. "cfree6"=>"",
  285. "cfree7"=>"",
  286. "cfree8"=>"",
  287. "cfree9"=>"",
  288. "cfree10"=>""
  289. ];
  290. }
  291. //最终数据
  292. $post['data'] = [
  293. "csocode"=>'',
  294. "ddate"=> $time,
  295. "cmaker"=>"admin",
  296. "dcreatesystime"=>$time1,
  297. "cstcode"=>"",
  298. "cbustype" => $value['cbustype'], //业务类型
  299. "cstname"=>$value['cstname'], //销售类型
  300. "ccuscode"=>"",
  301. "ccusabbname"=>$value['ccusabbname'], //客户简称
  302. "cdepcode"=>"",
  303. "cdepname"=>$value['cdepname'], // 部门名称
  304. "cpersoncode"=>"", //业务员编码 暂时不要
  305. "itaxrate"=>"0",
  306. "cmemo"=>"T9销售订单:". $value['order_number'],
  307. "cdefine1"=>"",
  308. "cdefine2"=>"",
  309. "cdefine3"=>"",
  310. "cdefine4"=>"",
  311. "cdefine5"=>"",
  312. "cdefine6"=>"",
  313. "cdefine7"=>"",
  314. "cdefine8"=>"",
  315. "cdefine9"=>"",
  316. "cdefine10"=>"",
  317. "cdefine11"=>"",
  318. "cdefine12"=>"",
  319. "cdefine13"=>"",
  320. "cdefine14"=>"",
  321. "cdefine15"=>"",
  322. "cdefine16"=>"",
  323. "bodys"=>$bodys
  324. ];
  325. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  326. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  327. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  328. //剔除数据
  329. $id = array_diff($id, [$value['id']]);
  330. if(empty($return)) {
  331. $msg = '异常错误,请确认请求接口地址或地址不可达';
  332. $this->finalSettle($value['id'],U8Job::two, $msg);
  333. }else{
  334. if( ! empty($return['flag'])){
  335. $this->finalSettle($value['id'],U8Job::two);
  336. }else{
  337. $this->finalSettle($value['id'], U8Job::two, $return['msg']);
  338. }
  339. }
  340. }
  341. if(! empty($id)){
  342. $msg = "未找到同步数据";
  343. $this->finalSettle($id,U8Job::two, $msg);
  344. }
  345. }
  346. //最终处理
  347. public function finalSettle($data,$data_type, $msg = ''){
  348. if(! is_array($data)) $data = [$data];
  349. $time = time();
  350. $insert = [];
  351. U8Job::where('del_time',0)
  352. ->where('data_type',U8Job::one)
  353. ->whereIn('data',$data)
  354. ->update(['del_time' => $time]);
  355. foreach ($data as $value){
  356. if(empty($msg)){
  357. $insert[] = [
  358. 'data' => $value,
  359. 'data_type' => $data_type,
  360. 'crt_time' => $time,
  361. 'state' => U8Job::success,
  362. ];
  363. }else{
  364. $insert[] = [
  365. 'data' => $value,
  366. 'data_type' => $data_type,
  367. 'crt_time' => $time,
  368. 'state' => U8Job::failed,
  369. 'msg' => $msg
  370. ];
  371. }
  372. }
  373. U8Job::insert($insert);
  374. }
  375. public function getPurchaseData($id){
  376. $main = PurchaseOrder::whereIn('id',$id)
  377. ->where('del_time',0)
  378. ->get()->toArray();
  379. if(empty($main)) return [];
  380. $supplier = Supplier::whereIn('id',array_unique(array_column($main,'supplier')))
  381. ->pluck('title','id')
  382. ->toArray();
  383. $depart = Depart::whereIn('id',array_unique(array_column($main,'depart_id')))
  384. ->pluck('title','id')
  385. ->toArray();
  386. $emp = Employee::whereIn('id',array_unique(array_column($main,'purchase_id')))
  387. ->pluck('number','id')
  388. ->toArray();
  389. $sub = PurchaseOrderInfo::whereIn('purchase_order_id',$id)
  390. ->where('del_time',0)
  391. ->get()->toArray();
  392. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  393. ->get()->toArray();
  394. $product_map = array_column($product,null,'id');
  395. $sub_map = [];
  396. foreach ($sub as $value){
  397. $product_tmp = $product_map[$value['product_id']] ?? [];
  398. $value['code'] = $product_tmp['code'];
  399. //计算金额
  400. if($value['rate'] > 0){
  401. // ipertaxrate 税率
  402. // iunitprice 原币单价
  403. // itaxprice 原币含税单价
  404. // isum 原币价税合计
  405. // imoney 原币无税金额
  406. // itax 原币税额
  407. $value['ipertaxrate'] = $value['rate'];
  408. $rate = round($value['rate'] / 100,2);
  409. $value['iunitprice'] = round($value['price'] / (1 + $rate),2);
  410. $value['itaxprice'] = $value['price'];
  411. $value['isum'] = round($value['price'] * $value['number'],2);
  412. $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  413. $value['itax'] = round($value['isum'] - $value['imoney'],2);
  414. }else{
  415. $value['ipertaxrate'] = 0;
  416. $value['iunitprice'] = $value['price'];
  417. $value['itaxprice'] = $value['price'];
  418. $value['isum'] = $value['price'] * $value['number'];
  419. $value['imoney'] = $value['isum'];
  420. $value['itax'] = 0;
  421. }
  422. $sub_map[$value['purchase_order_id']][] = $value;
  423. }
  424. foreach ($main as $key => $value){
  425. $main[$key]['cptname'] = $supplier[$value['supplier']] ?? "";
  426. $main[$key]['cvenname'] = $supplier[$value['supplier']] ?? "";
  427. $main[$key]['cdepname'] = $depart[$value['depart_id']] ?? "";
  428. $main[$key]['cpersoncode'] = $emp[$value['purchase_id']] ?? "";
  429. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  430. }
  431. return $main;
  432. }
  433. public function getSaleOrderData($id){
  434. $main = SalesOrder::whereIn('id',$id)
  435. ->where('del_time',0)
  436. ->get()->toArray();
  437. if(empty($main)) return [];
  438. $main_map = array_column($main,null,'id');
  439. $sub = SalesOrderProductInfo::whereIn('sales_order_id',$id)
  440. ->where('del_time',0)
  441. ->get()->toArray();
  442. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  443. ->get()->toArray();
  444. $product_map = array_column($product,null,'id');
  445. $code_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($main,'business_type'),array_column($main,'sale_type'),array_column($main,'plat_type'))))
  446. ->pluck('title','id')
  447. ->toArray();
  448. $customer_map = Customer::whereIn('id',array_unique(array_column($main,'customer_id')))
  449. ->pluck('title','id')
  450. ->toArray();
  451. $depart = Depart::where('parent_id',0)
  452. ->pluck('title','id')
  453. ->toArray();
  454. $emp = Employee::whereIn('id',array_unique(array_column($main,'crt_id')))
  455. ->pluck('number','id')
  456. ->toArray();
  457. $see = SeeRange::where('del_time',0)
  458. ->whereIn('data_id',array_column($main,'id'))
  459. ->where('data_type',SeeRange::type_seven)
  460. ->where('type',SeeRange::data_three)
  461. ->pluck('param_id','data_id')->toArray();//指派的分社
  462. $sub_map = [];
  463. foreach ($sub as $value){
  464. $product_tmp = $product_map[$value['product_id']] ?? [];
  465. $main_tmp = $main_map[$value['sales_order_id']] ?? [];
  466. //计算金额
  467. if($value['rate'] > 0){
  468. $value['ipertaxrate'] = $value['rate'];
  469. $rate = round($value['rate'] / 100,2);
  470. $value['iunitprice'] = round($value['price'] / (1 + $rate),2);
  471. $value['itaxprice'] = $value['price'];
  472. $value['isum'] = round($value['price'] * $value['number'],2);
  473. $value['imoney'] = round($value['iunitprice'] * $value['number'],2);
  474. $value['itax'] = round($value['isum'] - $value['imoney'],2);
  475. }else{
  476. $value['ipertaxrate'] = 0;
  477. $value['iunitprice'] = $value['price'];
  478. $value['itaxprice'] = $value['price'];
  479. $value['isum'] = $value['price'] * $value['number'];
  480. $value['imoney'] = $value['isum'];
  481. $value['itax'] = 0;
  482. }
  483. $value['cdefine25'] = $code_map[$main_tmp['plat_type']] ?? ""; //平台类型
  484. $value['cdefine28'] = $main_tmp['plat_order'] ?? ""; //平台单号
  485. $top_depart_id = $see[$value['sales_order_id']] ?? 0;
  486. $value['cdefine29'] = $depart[$top_depart_id] ?? "";//分社施工
  487. $value['cdefine32'] = "";//直播销售 暂时没有
  488. $value['cdefine31'] = "";//客户名称(线上的时候就是空 线下的话就是表头的客户简称)
  489. $value['cdefine22'] = "";//手机号码 暂时没有
  490. $value['cdefine30'] = $emp[$main_tmp['crt_id']] ?? "";//业务员
  491. $value['code'] = $product_tmp['code'];//存货编码
  492. $sub_map[$value['sales_order_id']][] = $value;
  493. }
  494. foreach ($main as $key => $value){
  495. $main[$key]['cbustype'] = $code_map[$value['business_type']] ?? ""; //业务类型(本身就是中文)
  496. $main[$key]['cstname'] = $code_map[$value['sale_type']] ?? ""; //销售类型
  497. $main[$key]['ccusabbname'] = $customer_map[$value['customer_id']] ?? "";//客户简称
  498. $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";//部门名称
  499. // $main[$key]['cpersoncode'] = $emp[$value['crt_id']] ?? "";//业务员
  500. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  501. }
  502. return $main;
  503. }
  504. public function post_helper($url, $data, $header = [], $timeout = 20){
  505. $ch = curl_init();
  506. curl_setopt($ch, CURLOPT_URL, $url);
  507. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  508. curl_setopt($ch, CURLOPT_ENCODING, '');
  509. curl_setopt($ch, CURLOPT_POST, 1);
  510. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  511. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  512. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  513. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  514. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  515. $r = curl_exec($ch);
  516. curl_close($ch);
  517. return json_decode($r, true);
  518. }
  519. }