TestService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Config;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class TestService extends Service
  8. {
  9. public function testdwy($data){
  10. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  11. $url = $data['url'];
  12. $post = $data['post'];
  13. $header = $data['header'];
  14. $json = json_encode($post);
  15. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',$json);
  16. $json = str_replace('"loginBindingParameters":[]','"loginBindingParameters":{}',$json);
  17. list($status, $result) = $this->post_helper($url,$json, $header, 40);
  18. if(! $status) return [false, $result];
  19. return [true, $result];
  20. }
  21. public function testdwyget($data){
  22. if(empty($data['url']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  23. $url = $data['url'];
  24. $header = $data['header'];
  25. list($status,$result) = $this->get_helper($url,$header);
  26. if(! $status) return [false, $result];
  27. return [true, $result];
  28. }
  29. public function testdwyput($data){
  30. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  31. $url = $data['url'];
  32. $post = $data['post'];
  33. $header = $data['header'];
  34. $json = json_encode($post);
  35. $json = str_replace('"workflowSearchBean":{}','"workflowSearchBean":[]',json_encode($post));
  36. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  37. list($status, $result) = $this->put_helper($url,$json, $header,40);
  38. if(! $status) return [false, $result];
  39. return [true, $result];
  40. }
  41. public function updateTopStock($data){
  42. list($status, $msg) = $this->rule();
  43. // if(! $status) return [false, 'IP未入白名单'];
  44. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getStock") return [false,'API请求参数不能为空'];
  45. if(empty($data['code'])) return [false,'API请求参数不能为空'];
  46. if(empty($data['warehouse'])) return [false,'API请求参数不能为空'];
  47. list($status, $msg) = $this->connectYy();
  48. if(! $status) return [false, $msg];
  49. //数据库
  50. $db = $msg[0];
  51. $u8 = $msg[1];
  52. list($status, $msg) = $this->getStock($db, $u8,$data['code'],$data['warehouse']);
  53. return [$status, $msg];
  54. }
  55. public function getSnList($data){
  56. list($status, $msg) = $this->rule();
  57. // if(! $status) return [false, 'IP未入白名单'];
  58. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnList") return [false,'API请求参数不能为空'];
  59. if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];
  60. list($status, $msg) = $this->connectYy();
  61. if(! $status) return [false, $msg];
  62. //数据库
  63. $db = $msg[0];
  64. $u8 = $msg[1];
  65. if($data['sn_type'] == 1){
  66. list($status, $msg) = $this->getSnListFormU8One($db, $u8,$data);
  67. }else{
  68. //发货出库单 sn码
  69. list($status, $msg) = $this->getSnListFormU8Two($db, $u8,$data);
  70. }
  71. return [$status, $msg];
  72. }
  73. public function getSnforMap($data){
  74. list($status, $msg) = $this->rule();
  75. // if(! $status) return [false, 'IP未入白名单'];
  76. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnMap") return [false,'API请求参数不能为空'];
  77. if(empty($data['sn'])) return [false, 'sn码不能为空'];
  78. if(empty($data['code'])) return [false, '产品编码不能为空'];
  79. list($status, $msg) = $this->connectYy();
  80. if(! $status) return [false, $msg];
  81. //数据库
  82. $db = $msg[0];
  83. $u8 = $msg[1];
  84. list($status, $msg) = $this->getSnListFormU8ForMap($db, $u8,$data);
  85. return [$status, $msg];
  86. }
  87. public function getSnForWarranty($data){
  88. list($status, $msg) = $this->rule();
  89. // if(! $status) return [false, 'IP未入白名单'];
  90. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnForWarranty") return [false,'API请求参数不能为空'];
  91. if(empty($data['sn'])) return [false, 'sn码不能为空'];
  92. list($status, $msg) = $this->connectYy();
  93. if(! $status) return [false, $msg];
  94. //数据库
  95. $db = $msg[0];
  96. $u8 = $msg[1];
  97. list($status, $msg) = $this->getSnForWarrantyData($db, $u8,$data);
  98. return [$status, $msg];
  99. }
  100. public function rule(){
  101. // 获取用户的IP地址
  102. $userIP = $_SERVER['REMOTE_ADDR'];
  103. // 获取设置的IP地址
  104. $ip = env("AliYUN");
  105. $allowedIPs = [$ip];
  106. $allowedIPs = array_filter($allowedIPs);
  107. if(empty($allowedIPs)) return [false, $userIP];
  108. // 校验用户IP是否在允许的范围内
  109. $isValidIP = false;
  110. foreach ($allowedIPs as $allowedIP) {
  111. if (strpos($allowedIP, '/') !== false) {
  112. // IP段表示法校验
  113. list($subnet, $mask) = explode('/', $allowedIP);
  114. if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  115. $isValidIP = true;
  116. break;
  117. }
  118. } else {
  119. // 单个IP地址校验
  120. if ($allowedIP === $userIP) {
  121. $isValidIP = true;
  122. break;
  123. }
  124. }
  125. }
  126. return [$isValidIP, $userIP];
  127. }
  128. public function connectYy(){
  129. $model_box = DB::connection("mysqlT9");
  130. $u8 = $model_box->table('setting')
  131. ->where('setting_name','u8')
  132. ->where('setting_value','<>','')
  133. ->first();
  134. if(empty($u8)) return [false, 'u8配置参数不存在!'];
  135. $u8 = $u8->setting_value;
  136. // 使用 eval() 函数执行字符串并转换为数组
  137. $u8 = eval("return {$u8};");
  138. if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
  139. if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
  140. if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
  141. if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
  142. if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
  143. if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
  144. if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
  145. if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
  146. if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
  147. if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
  148. $config = [
  149. 'driver' => 'sqlsrv',
  150. 'host' => $u8['domain'],
  151. 'port' => $u8['u8_database_port'],
  152. 'database' => $u8['database'],
  153. 'username' => $u8['database_account'],
  154. 'password' => $u8['database_password'],
  155. ];
  156. // 数据库配置设置
  157. Config::set('database.connections.sqlsrvs', $config);
  158. // 连接
  159. try {
  160. $pdo = DB::connection('sqlsrvs')->getPdo();
  161. if ($pdo instanceof \PDO) {
  162. // 连接成功的逻辑代码
  163. $db = DB::connection('sqlsrvs');
  164. return [true, [$db, $u8]];
  165. } else {
  166. return [false, '连接失败!'];
  167. }
  168. } catch (\Throwable $e) {
  169. return [false, $e->getMessage()];
  170. }
  171. }
  172. public function getStock($db, $u8, $code = [], $warehouse = ""){
  173. if(empty($code) || empty($warehouse)) return [false, '存货以及仓库不能为空'];
  174. //映射ip是否通畅
  175. $bool = $this->isDomainAvailable($u8['domain']);
  176. if(! $bool) return [false, 'U8程序外部域名不可达'];
  177. $result = $db->table('CurrentStock')
  178. ->where("cWhCode", $warehouse)
  179. ->whereIn("cInvCode", $code)
  180. ->select('iQuantity as number', 'cInvCode as product_no')
  181. ->get()->toArray();
  182. $return = [];
  183. foreach ($result as $value){
  184. $return[] = [
  185. 'number' => floatval($value->number),
  186. 'product_no' => $value->product_no
  187. ];
  188. }
  189. return [true, $return];
  190. }
  191. public function getSnListFormU8One($db, $u8, $data){
  192. if(empty($data['code'])) return [false, '存货不能为空'];
  193. $sn = $data['sn'] ?? "";
  194. $construction_id = $data['construction_id'] ?? 0;
  195. $warehouse = [
  196. '001',
  197. '003',
  198. '010',
  199. ];
  200. //映射ip是否通畅
  201. $bool = $this->isDomainAvailable($u8['domain']);
  202. if(! $bool) return [false, 'U8程序外部域名不可达'];
  203. // $db->enableQueryLog();
  204. //检索条件 在库的
  205. $model = $db->table('ST_SNState')
  206. ->select('cinvCode as code','cInvSN as sn','AutoID as auto_id')
  207. ->whereIn("cWhCode", $warehouse)
  208. ->where("cInvCode", $data['code'])
  209. ->where("iSNState", 2)
  210. ->when(! empty($sn), function ($query) use ($sn) {
  211. return $query->where('cInvSN', 'LIKE', '%'.$sn.'%');
  212. })
  213. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  214. // return $query->whereNull('cSNDefine1')->orWhere('cSNDefine1', '')->orWhere('cSNDefine1', $construction_id);
  215. // })
  216. ->when(empty($construction_id), function ($query) {
  217. return $query->where(function ($q) {
  218. $q->whereNull('cSNDefine1')
  219. ->orWhere('cSNDefine1', '');
  220. });
  221. })
  222. ->orderBy('cSNDefine1','desc')
  223. ->orderBy('AutoID','asc');
  224. $list = $this->limit($model, '', $data);
  225. // dd($db->getQueryLog());
  226. return [true, $list];
  227. }
  228. public function getSnListFormU8Two($db, $u8, $data){
  229. if(empty($data['code']) || empty($data['depart_title'])) return [false, '存货以及门店信息不能为空'];
  230. $sn = $data['sn'] ?? "";
  231. $construction_id = $data['construction_id'] ?? 0;
  232. // $db->enableQueryLog();
  233. //映射ip是否通畅
  234. $bool = $this->isDomainAvailable($u8['domain']);
  235. if(! $bool) return [false, 'U8程序外部域名不可达'];
  236. $model = $db->table('rdrecord32 as a')
  237. ->leftJoin('rdrecords32 as b','b.ID','a.ID')
  238. ->leftJoin('ST_SNDetail_SaleOut as c','c.iVouchsID','b.AutoID')
  239. ->select("c.cInvCode as code",'c.cinvSN as sn','c.AutoID as auto_id') //,'c.cSNDefine1','a.ID'
  240. ->whereNotNull('a.cHandler')
  241. ->where("b.cInvCode", $data['code'])
  242. ->where("b.iQuantity", '>', 0)
  243. ->where("b.cDefine31", $data['depart_title'])
  244. ->whereNotNull("c.cinvSN")
  245. ->when(! empty($sn), function ($query) use ($sn) {
  246. return $query->where('c.cInvSN', 'LIKE', '%'.$sn.'%');
  247. })
  248. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  249. // return $query->whereNull('c.cSNDefine1')->orWhere('c.cSNDefine1', '')->orWhere('c.cSNDefine1', $construction_id);
  250. // })
  251. ->when(empty($construction_id), function ($query) {
  252. return $query->where(function ($q) {
  253. $q->whereNull('c.cSNDefine1')
  254. ->orWhere('c.cSNDefine1', '');
  255. });
  256. })
  257. ->orderBy('c.cSNDefine1','desc')
  258. ->orderBy('a.ID','desc')
  259. ->orderBy('c.AutoID','asc');
  260. $list = $this->limit($model, '', $data);
  261. // $logs = $db->getQueryLog();dd($logs);
  262. return [true, $list];
  263. }
  264. public function getSnListFormU8ForMap($db, $u8, $data){
  265. //映射ip是否通畅
  266. $bool = $this->isDomainAvailable($u8['domain']);
  267. if(! $bool) return [false, 'U8程序外部域名不可达'];
  268. $warehouse = [
  269. '001',
  270. '003',
  271. '010',
  272. ];
  273. $list = $db->table('ST_SNState')
  274. ->select("cInvCode as code",'cinvSN as sn')
  275. ->whereIn("cWhCode", $warehouse)
  276. ->whereIn("cInvCode", $data['code'])
  277. ->whereIn('cInvSN', $data['sn'])
  278. // ->where("iSNState", 2)
  279. ->get()->toArray();
  280. return [true, $list];
  281. }
  282. public function getSnForWarrantyData($db, $u8, $data){
  283. //映射ip是否通畅
  284. $bool = $this->isDomainAvailable($u8['domain']);
  285. if(! $bool) return [false, 'U8程序外部域名不可达'];
  286. $warehouse = [
  287. '001',
  288. '003',
  289. '010',
  290. ];
  291. $list = $db->table('ST_SNState')
  292. ->select("cInvCode as code",'cinvSN as sn','AutoID as auto_id')
  293. ->whereIn("cWhCode", $warehouse)
  294. ->where('cInvSN', $data['sn'])
  295. // ->where("iSNState", 2)
  296. ->first();
  297. return [true, $list];
  298. }
  299. public function saveSnUseData($data){
  300. list($status, $msg) = $this->rule();
  301. // if(! $status) return [false, 'IP未入白名单'];
  302. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "saveSnUseData") return [false,'API请求参数不能为空'];
  303. if(empty($data['sn_type'])) return [false,'sn码来源依据不能为空'];
  304. // if(empty($data['sn_for_u8'])) return [false, 'sn码更新信息不能为空'];
  305. if(empty($data['data_id'])) return [false, 'dataID信息不能为空'];
  306. list($status, $msg) = $this->connectYy();
  307. if(! $status) return [false, $msg];
  308. //数据库
  309. $db = $msg[0];
  310. $u8 = $msg[1];
  311. list($status, $msg) = $this->saveSnUseDataDetail($db, $u8,$data);
  312. return [$status, $msg];
  313. }
  314. public function saveSnUseDataDetail($db, $u8, $data){
  315. //映射ip是否通畅 data_id = 1
  316. $bool = $this->isDomainAvailable($u8['domain']);
  317. if(! $bool) return [false, 'U8程序外部域名不可达'];
  318. $sn_for_u8 = $data['sn_for_u8'] ?? [];
  319. try {
  320. DB::beginTransaction();
  321. if(empty($sn_for_u8)){//删除
  322. if($data['sn_type'] == 1){
  323. $db->table('ST_SNState')
  324. ->where('cSNDefine1', $data['data_id'])
  325. ->update(['cSNDefine1' => ""]);
  326. }else{
  327. $db->table('ST_SNDetail_SaleOut')
  328. ->where('cSNDefine1', $data['data_id'])
  329. ->update(['cSNDefine1' => ""]);
  330. }
  331. }else{//增加或更新
  332. if($data['sn_type'] == 1){
  333. $db->table('ST_SNState')
  334. ->whereIn('AutoID', $sn_for_u8)
  335. ->update(['cSNDefine1' => $data['data_id']]);
  336. }else{
  337. $db->table('ST_SNDetail_SaleOut')
  338. ->whereIn('AutoID', $sn_for_u8)
  339. ->update(['cSNDefine1' => $data['data_id']]);
  340. }
  341. }
  342. DB::commit();
  343. }catch (\Exception $exception){
  344. DB::rollBack();
  345. return [false, $exception->getMessage()];
  346. }
  347. return [true, ''];
  348. }
  349. public function getToken(){
  350. list($status, $msg) = $this->SetU8();
  351. if(! $status) return [false , $msg];
  352. $host = $msg;
  353. $key = "lf_u8_long_token_demo";
  354. if(! Cache::has($key)){
  355. $url = $host . "/api/System/GetToken";
  356. $date = date("Y-m-d");
  357. $json = [
  358. "U8DbName"=> "UFDATA_001_2025",
  359. "sUserId"=> "demo",
  360. "sPassword"=> "DEMO",
  361. "LoginDateTime"=> $date,
  362. "bPersist"=> true
  363. ];
  364. $header = ['Content-Type:application/json'];
  365. list($status, $result) = $this->post_helper($url,json_encode($json), $header, 30);
  366. if(! $status) return [false, $result];
  367. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  368. if($result['code'] != 0) return [false, $result['msg']];
  369. $token = $result['data']['Token'] ?? "";
  370. Cache::forever($key, $token);
  371. }else{
  372. $token = Cache::get($key);
  373. }
  374. return [true, [$host, $token]];
  375. }
  376. //-----------------------------------朗峰u8-----
  377. public function salesOrderGet($data){
  378. list($status, $msg) = $this->getToken();
  379. if(! $status) return [false, $msg];
  380. list($host, $token) = $msg;
  381. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  382. $url = $host . "/api/System/SqlQuery";
  383. $json = '{
  384. "customSQLFileName": "U8SQL",
  385. "customSQLPath": "U8API/SO_SOMain/Get",
  386. "paramObj": {
  387. "@pagesize": 3,
  388. }
  389. }';
  390. list($status, $result) = $this->post_helper($url,$json, $header, 30);
  391. if(! $status) return [false, $result];
  392. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  393. if($result['code'] != 0) return [false, $result['msg']];
  394. if(empty($result['data'])) return [true, []];
  395. $return = [];
  396. $r_data = $result['data'];
  397. foreach ($r_data as $value){
  398. list($status, $detail) = $this->getSalesDetail($value, $msg);
  399. if(! $status) return [false, $detail];
  400. $return_detail = [];
  401. $total_qty = $money = 0;
  402. foreach ($detail as $d_value){
  403. $return_detail[] = [
  404. 'item_no' => $d_value['irowno'],
  405. 'material_code' => $d_value['cinvcode'],
  406. 'decor' => $d_value['cdefine30'] ?? '',
  407. 'craft_type_code' => $d_value['cdefine31'] ?? '',
  408. 'decor_b' => $d_value['cdefine32'] ?? '',
  409. 'craft_type_code_b' => $d_value['cdefine33'] ?? '',
  410. 'unit' => $d_value['cinvm_unit'],
  411. 'price' => $d_value['itaxunitprice'],
  412. 'not_tax_price' => $d_value['itaxunitprice'],
  413. 'tax_price' => $d_value['itaxunitprice'],
  414. 'qty' => $d_value['iquantity'],
  415. 'money' => $d_value['isum'],
  416. 'tax_amount' => $d_value['isum'],
  417. 'total_tax_amount' => $d_value['isum'],
  418. 'tax_rate' => $d_value['itaxrate'],
  419. 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])),
  420. 'remark' => $d_value['cmemo'] ?? '',
  421. ];
  422. $total_qty = bcadd($total_qty,$d_value['iquantity'],3);
  423. $money = bcadd($money,$d_value['isum'],3);
  424. }
  425. $return[] = [
  426. 'no' => $value['csocode'],
  427. 'order_date' => date("Y-m-d",strtotime($value['ddate'])),
  428. 'customer' => $value['ccuscode'] ?? '',
  429. 'salesman' => $value['cpersoncode'] ?? '',
  430. 'sale_department' => $value['cdepcode'] ?? '',
  431. 'total_qty' => $total_qty,
  432. 'total_money' => $money,
  433. 'remark' => $value['cmemo'] ?? '',
  434. 'detail' => $return_detail,
  435. ];
  436. }
  437. return [true, $return];
  438. }
  439. private function SetU8(){
  440. $api_host = env('API_HOST');
  441. if(empty($api_host)) return [false, '用友对外域名不存在'];
  442. $api_port = env('API_PORT');
  443. if(empty($api_port)) return [false, '用友对外域名端口不存在'];
  444. //映射ip是否通畅
  445. $bool = $this->isDomainAvailable($api_host);
  446. if(! $bool) return [false, '用友对外域名不可达'];
  447. $host = $api_host . ":" . $api_port;
  448. return [true, $host];
  449. }
  450. private function getSalesDetail($sale_order, $msg){
  451. list($host, $token) = $msg;
  452. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  453. $url = $host . "/api/System/SqlQuery2";
  454. $json = [
  455. "customSQLFileName"=> "U8SQL",
  456. "customSQLPath"=> "U8API/SO_SOMain/GetWithDetail",
  457. "paramObj"=> [
  458. "@code"=> $sale_order["csocode"]
  459. ]
  460. ];
  461. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  462. if(! $status) return [false, $result];
  463. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  464. if($result['code'] != 0) return [false, $result['msg']];
  465. return [true, $result['data']['DataTable1']];
  466. }
  467. public function materialAddU8($data){
  468. list($status, $msg) = $this->getToken();
  469. if(! $status) return [false, $msg];
  470. list($host, $token) = $msg;
  471. if(empty($data['iHead'])) return [false, '领料单表头信息不能为空'];
  472. if(empty($data['iBody'])) return [false, '领料单表体信息不能为空'];
  473. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  474. $url = $host . "/api/MaterialRequest/Add";
  475. $json[] = [
  476. "Inum" => "MaterialRequest",
  477. "data" =>[
  478. "iHead" => $data['iHead'],
  479. "iBody" => $data['iBody'],
  480. ],
  481. ];
  482. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  483. if(! $status) return [false, $result];
  484. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  485. if($result['code'] != 0) return [false, $result['msg']];
  486. return [true, $result['data']];
  487. }
  488. public function productInAddU8($data){
  489. list($status, $msg) = $this->getToken();
  490. if(! $status) return [false, $msg];
  491. list($host, $token) = $msg;
  492. if(empty($data['iHead'])) return [false, '产成品入库单单表头信息不能为空'];
  493. if(empty($data['iBody'])) return [false, '产成品入库单表体信息不能为空'];
  494. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  495. $url = $host . "/api/ProductIn/Add";
  496. $json[] = [
  497. "Inum" => "ProductIn",
  498. "data" =>[
  499. "iHead" => $data['iHead'],
  500. "iBody" => $data['iBody'],
  501. ],
  502. ];
  503. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  504. if(! $status) return [false, $result];
  505. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  506. if($result['code'] != 0) return [false, $result['msg']];
  507. return [true, $result['data']];
  508. }
  509. public function dispatchAddU8($data){
  510. list($status, $msg) = $this->getToken();
  511. if(! $status) return [false, $msg];
  512. list($host, $token) = $msg;
  513. if(empty($data['iHead'])) return [false, '发货单单表头信息不能为空'];
  514. if(empty($data['iBody'])) return [false, '发货单表体信息不能为空'];
  515. foreach ($data['iBody'] as $key => $value){
  516. $data['iBody'][$key] = $this->fillYonyouDispatchDetail($value);
  517. }
  518. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  519. $url = $host . "/api/Dispatch/Add";
  520. $json[] = [
  521. "Inum" => "DispatchList",
  522. "data" =>[
  523. "iHead" => $data['iHead'],
  524. "iBody" => $data['iBody'],
  525. ]
  526. ];
  527. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  528. if(! $status) return [false, $result];
  529. if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
  530. if($result['code'] != 0) return [false, $result['msg']];
  531. return [true, $result['data']];
  532. }
  533. /**
  534. * 补全用友发货单子表金额字段(含换算率)
  535. * 输入示例:
  536. * [
  537. * 'iQuantity' => 1, // 输入单位数量
  538. * 'iunitprice' => 10, // 无税单价(按主计量单位)
  539. * 'iTaxRate' => 0, // 税率
  540. * 'iInvExchRate' => 12 // 换算率(1箱=12个)
  541. * ]
  542. */
  543. function fillYonyouDispatchDetail(array $item)
  544. {
  545. $qty = floatval($item['iQuantity']);
  546. $price = floatval($item['iunitprice']);
  547. $rate = floatval($item['iTaxRate']);
  548. $exchRate = floatval($item['iInvExchRate']);
  549. // 实际参与金额计算的基本数量(用友内部用这个)
  550. $qtyBase = $qty * $exchRate;
  551. // 税率
  552. $taxRate = $rate / 100;
  553. // 原币(人民币)
  554. $imoney = round($qtyBase * $price, 2);
  555. $itax = round($imoney * $taxRate, 2);
  556. $iSum = round($imoney + $itax, 2);
  557. $itaxunitprice = round($price * (1 + $taxRate), 6);
  558. // 本币 = 原币
  559. return array_merge($item, [
  560. 'imoney' => $imoney,
  561. 'itax' => $itax,
  562. 'iSum' => $iSum,
  563. 'itaxunitprice' => $itaxunitprice,
  564. 'idiscount' => 0,
  565. 'inatunitprice' => $price,
  566. 'inatmoney' => $imoney,
  567. 'inattax' => $itax,
  568. 'inatsum' => $iSum,
  569. 'inatdiscount' => 0,
  570. ]);
  571. }
  572. //-----------------------------------朗峰u8-----
  573. public function post_helper($url, $data, $header = [], $timeout = 20){
  574. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  575. $ch = curl_init();
  576. curl_setopt($ch, CURLOPT_URL, $url);
  577. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  578. curl_setopt($ch, CURLOPT_ENCODING, '');
  579. curl_setopt($ch, CURLOPT_POST, 1);
  580. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  581. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  582. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  583. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  584. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  585. $r = curl_exec($ch);
  586. if ($r === false) {
  587. // 获取错误号
  588. $errorNumber = curl_errno($ch);
  589. // 获取错误信息
  590. $errorMessage = curl_error($ch);
  591. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  592. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  593. return [false, $message];
  594. }
  595. curl_close($ch);
  596. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  597. return [true, json_decode($r, true)];
  598. }
  599. public function get_helper($url,$header=[],$timeout = 20){
  600. $ch = curl_init();
  601. curl_setopt_array($ch, array(
  602. CURLOPT_URL => $url,
  603. CURLOPT_RETURNTRANSFER => true,
  604. CURLOPT_ENCODING => '',
  605. CURLOPT_MAXREDIRS => 10,
  606. CURLOPT_TIMEOUT => $timeout,
  607. CURLOPT_FOLLOWLOCATION => true,
  608. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  609. CURLOPT_CUSTOMREQUEST => 'GET',
  610. CURLOPT_SSL_VERIFYPEER => false,
  611. CURLOPT_HTTPHEADER => $header,
  612. ));
  613. $r = curl_exec($ch);
  614. if ($r === false) {
  615. // 获取错误号
  616. $errorNumber = curl_errno($ch);
  617. // 获取错误信息
  618. $errorMessage = curl_error($ch);
  619. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  620. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  621. return [false, $message];
  622. }
  623. curl_close($ch);
  624. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  625. return [true, json_decode($r, true)];
  626. }
  627. public function put_helper($url, $data, $header = [], $timeout = 20){
  628. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  629. $ch = curl_init();
  630. curl_setopt($ch, CURLOPT_URL, $url);
  631. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  632. curl_setopt($ch, CURLOPT_ENCODING, '');
  633. curl_setopt($ch, CURLOPT_POST, 1);
  634. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  635. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  636. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  637. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  638. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  639. $r = curl_exec($ch);
  640. if ($r === false) {
  641. // 获取错误号
  642. $errorNumber = curl_errno($ch);
  643. // 获取错误信息
  644. $errorMessage = curl_error($ch);
  645. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  646. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  647. return [false, $message];
  648. }
  649. curl_close($ch);
  650. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  651. return [true, json_decode($r, true)];
  652. }
  653. }