TestService.php 30 KB

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