TestService.php 31 KB

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