U8ServerService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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"=>$value['order_number'],
  174. "dpodate"=>date("Y-m-d H:i:s",$value['crt_time']),
  175. "cmemo"=>"T9同步采购单",
  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"=>$value['order_number'],
  294. "ddate"=> $time,
  295. "cmaker"=>"admin",
  296. "dcreatesystime"=>$time1,
  297. "cstcode"=>"",
  298. "cstname"=>$value['cstname'], //销售类型
  299. "ccuscode"=>"",
  300. "ccusabbname"=>$value['ccusabbname'], //客户简称
  301. "cdepcode"=>"",
  302. "cdepname"=>$value['cdepname'], // 部门名称
  303. "cpersoncode"=>"", //业务员编码 暂时不要
  304. "itaxrate"=>"0",
  305. "cmemo"=>"T9同步销售订单",
  306. "cdefine1"=>"",
  307. "cdefine2"=>"",
  308. "cdefine3"=>"",
  309. "cdefine4"=>"",
  310. "cdefine5"=>"",
  311. "cdefine6"=>"",
  312. "cdefine7"=>"",
  313. "cdefine8"=>"",
  314. "cdefine9"=>"",
  315. "cdefine10"=>"",
  316. "cdefine11"=>"",
  317. "cdefine12"=>"",
  318. "cdefine13"=>"",
  319. "cdefine14"=>"",
  320. "cdefine15"=>"",
  321. "cdefine16"=>"",
  322. "bodys"=>$bodys
  323. ];
  324. file_put_contents('record_purchase.txt',"请求参数:" . json_encode($post) . PHP_EOL,8);
  325. $return = $this->post_helper($this->u8_api,json_encode($post), ['Content-Type:application/json']);
  326. file_put_contents('record_purchase.txt',"返回结果:" . json_encode($return). PHP_EOL,8);
  327. //剔除数据
  328. $id = array_diff($id, [$value['id']]);
  329. if(empty($return)) {
  330. $msg = '异常错误,请确认请求接口地址或地址不可达';
  331. $this->finalSettle($value['id'],U8Job::two, $msg);
  332. }else{
  333. if( ! empty($return['flag'])){
  334. $this->finalSettle($value['id'],U8Job::two);
  335. }else{
  336. $this->finalSettle($value['id'], U8Job::two, $return['msg']);
  337. }
  338. }
  339. }
  340. if(! empty($id)){
  341. $msg = "未找到同步数据";
  342. $this->finalSettle($id,U8Job::two, $msg);
  343. }
  344. }
  345. //最终处理
  346. public function finalSettle($data,$data_type, $msg = ''){
  347. if(! is_array($data)) $data = [$data];
  348. $time = time();
  349. $insert = [];
  350. U8Job::where('del_time',0)
  351. ->where('data_type',U8Job::one)
  352. ->whereIn('data',$data)
  353. ->update(['del_time' => $time]);
  354. foreach ($data as $value){
  355. if(empty($msg)){
  356. $insert[] = [
  357. 'data' => $value,
  358. 'data_type' => $data_type,
  359. 'crt_time' => $time,
  360. 'state' => U8Job::success,
  361. ];
  362. }else{
  363. $insert[] = [
  364. 'data' => $value,
  365. 'data_type' => $data_type,
  366. 'crt_time' => $time,
  367. 'state' => U8Job::failed,
  368. 'msg' => $msg
  369. ];
  370. }
  371. }
  372. U8Job::insert($insert);
  373. }
  374. public function getPurchaseData($id){
  375. $main = PurchaseOrder::whereIn('id',$id)
  376. ->where('del_time',0)
  377. ->get()->toArray();
  378. if(empty($main)) return [];
  379. $supplier = Supplier::whereIn('id',array_unique(array_column($main,'supplier')))
  380. ->pluck('title','id')
  381. ->toArray();
  382. $depart = Depart::whereIn('id',array_unique(array_column($main,'top_depart_id')))
  383. ->pluck('title','id')
  384. ->toArray();
  385. $emp = Employee::whereIn('id',array_unique(array_column($main,'purchase_id')))
  386. ->pluck('number','id')
  387. ->toArray();
  388. $sub = PurchaseOrderInfo::whereIn('purchase_order_id',$id)
  389. ->where('del_time',0)
  390. ->get()->toArray();
  391. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  392. ->get()->toArray();
  393. $product_map = array_column($product,null,'id');
  394. $sub_map = [];
  395. foreach ($sub as $value){
  396. $product_tmp = $product_map[$value['product_id']] ?? [];
  397. $value['code'] = $product_tmp['code'];
  398. //计算金额
  399. if($value['rate'] > 0){
  400. $value['ipertaxrate'] = $value['rate'];
  401. $rate = round($value['rate'] / 100,2);
  402. $value['iunitprice'] = round($value['price'] / $rate,2);
  403. $value['itaxprice'] = $value['price'];
  404. $value['isum'] = $value['price'] * $value['number'];
  405. $value['imoney'] = $value['iunitprice'] * $value['number'];
  406. $value['itax'] = $value['isum'] - $value['imoney'];
  407. }else{
  408. $value['ipertaxrate'] = 0;
  409. $value['iunitprice'] = $value['price'];
  410. $value['itaxprice'] = $value['price'];
  411. $value['isum'] = $value['price'] * $value['number'];
  412. $value['imoney'] = $value['isum'];
  413. $value['itax'] = 0;
  414. }
  415. $sub_map[$value['purchase_order_id']][] = $value;
  416. }
  417. foreach ($main as $key => $value){
  418. $main[$key]['cvenname'] = $supplier[$value['supplier']] ?? "";
  419. $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";
  420. $main[$key]['cpersoncode'] = $emp[$value['purchase_id']] ?? "";
  421. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  422. }
  423. return $main;
  424. }
  425. public function getSaleOrderData($id){
  426. $main = SalesOrder::whereIn('id',$id)
  427. ->where('del_time',0)
  428. ->get()->toArray();
  429. if(empty($main)) return [];
  430. $main_map = array_column($main,null,'id');
  431. $sub = SalesOrderProductInfo::whereIn('sales_order_id',$id)
  432. ->where('del_time',0)
  433. ->get()->toArray();
  434. $product = Product::whereIn('id',array_unique(array_column($sub,'product_id')))
  435. ->get()->toArray();
  436. $product_map = array_column($product,null,'id');
  437. $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'))))
  438. ->pluck('title','id')
  439. ->toArray();
  440. $customer_map = Customer::whereIn('id',array_unique(array_column($main,'customer_id')))
  441. ->pluck('title','id')
  442. ->toArray();
  443. $depart = Depart::where('parent_id',0)
  444. ->pluck('title','id')
  445. ->toArray();
  446. $emp = Employee::whereIn('id',array_unique(array_column($main,'crt_id')))
  447. ->pluck('number','id')
  448. ->toArray();
  449. $see = SeeRange::where('del_time',0)
  450. ->whereIn('data_id',array_column($main,'id'))
  451. ->where('data_type',SeeRange::type_seven)
  452. ->where('type',SeeRange::data_three)
  453. ->pluck('param_id','data_id')->toArray();//指派的分社
  454. $sub_map = [];
  455. foreach ($sub as $value){
  456. $product_tmp = $product_map[$value['product_id']] ?? [];
  457. $main_tmp = $main_map[$value['sales_order_id']] ?? [];
  458. //计算金额
  459. if($value['rate'] > 0){
  460. $value['ipertaxrate'] = $value['rate'];
  461. $rate = round($value['rate'] / 100,2);
  462. $value['iunitprice'] = round($value['price'] / $rate,2);
  463. $value['itaxprice'] = $value['price'];
  464. $value['isum'] = $value['price'] * $value['number'];
  465. $value['imoney'] = $value['iunitprice'] * $value['number'];
  466. $value['itax'] = $value['isum'] - $value['imoney'];
  467. }else{
  468. $value['ipertaxrate'] = 0;
  469. $value['iunitprice'] = $value['price'];
  470. $value['itaxprice'] = $value['price'];
  471. $value['isum'] = $value['price'] * $value['number'];
  472. $value['imoney'] = $value['isum'];
  473. $value['itax'] = 0;
  474. }
  475. $value['cdefine25'] = $code_map[$main_tmp['plat_type']] ?? ""; //平台类型
  476. $value['cdefine28'] = $main_tmp['plat_order'] ?? ""; //平台单号
  477. $top_depart_id = $see[$value['sales_order_id']] ?? 0;
  478. $value['cdefine29'] = $depart[$top_depart_id] ?? "";//分社施工
  479. $value['cdefine32'] = "";//直播销售 暂时没有
  480. $value['cdefine31'] = "";//客户名称(线上的时候就是空 线下的话就是表头的客户简称)
  481. $value['cdefine22'] = "";//手机号码 暂时没有
  482. $value['cdefine30'] = $emp[$main_tmp['crt_id']] ?? "";//业务员
  483. $value['code'] = $product_tmp['code'];//存货编码
  484. $sub_map[$value['sales_order_id']][] = $value;
  485. }
  486. foreach ($main as $key => $value){
  487. $main[$key]['cbustype'] = $code_map[$value['business_type']] ?? ""; //业务类型(本身就是中文)
  488. $main[$key]['cstname'] = $code_map[$value['sale_type']] ?? ""; //销售类型
  489. $main[$key]['ccusabbname'] = $customer_map[$value['customer_id']] ?? "";//客户简称
  490. $main[$key]['cdepname'] = $depart[$value['top_depart_id']] ?? "";//部门名称
  491. // $main[$key]['cpersoncode'] = $emp[$value['crt_id']] ?? "";//业务员
  492. $main[$key]['product'] = $sub_map[$value['id']] ?? [];
  493. }
  494. return $main;
  495. }
  496. public function post_helper($url, $data, $header = [], $timeout = 20){
  497. $ch = curl_init();
  498. curl_setopt($ch, CURLOPT_URL, $url);
  499. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  500. curl_setopt($ch, CURLOPT_ENCODING, '');
  501. curl_setopt($ch, CURLOPT_POST, 1);
  502. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  503. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  504. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  505. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  506. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  507. $r = curl_exec($ch);
  508. curl_close($ch);
  509. return json_decode($r, true);
  510. }
  511. }