TestService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 = config('ip.zslf');
  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 = config('ip.zslf');
  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 = config('ip.zslf');
  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($common_array){
  361. list($status, $msg) = $this->SetU8($common_array);
  362. if(! $status) return [false , $msg];
  363. $host = $msg;
  364. $key = "lf_u8_long_token_demo_" . $common_array['site'];
  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, $common_array){
  388. list($status, $msg) = $this->getToken($common_array);
  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($common_array){
  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. $api_host = $common_array['api_host'];
  462. $api_port = $common_array['api_port'];
  463. //映射ip是否通畅
  464. $bool = $this->isDomainAvailable($api_host);
  465. if(! $bool) return [false, '用友对外域名不可达'];
  466. $host = $api_host . ":" . $api_port;
  467. return [true, $host];
  468. }
  469. private function getSalesDetail($sale_order, $msg){
  470. list($host, $token) = $msg;
  471. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  472. $url = $host . "/api/System/SqlQuery2";
  473. $json = [
  474. "customSQLFileName"=> "U8SQL",
  475. "customSQLPath"=> "U8API/SO_SOMain/GetWithDetail",
  476. "paramObj"=> [
  477. "@code"=> $sale_order["csocode"]
  478. ]
  479. ];
  480. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  481. if(! $status) return [false, $result];
  482. if(! isset($result['code'])) return [false, '拉取销售订单详情失败,请重新拉取'];
  483. if($result['code'] != 0) return [false, $result['msg']];
  484. return [true, $result['data']['DataTable1']];
  485. }
  486. public function materialAddU8($data,$common_array){
  487. list($status, $msg) = $this->getToken($common_array);
  488. if(! $status) return [false, $msg];
  489. list($host, $token) = $msg;
  490. if(empty($data['iHead'])) return [false, '领料单表头信息不能为空'];
  491. if(empty($data['iBody'])) return [false, '领料单表体信息不能为空'];
  492. $header = ["Authorization: {$token}",'Content-Type:application/json'];
  493. $url = $host . "/api/MaterialRequest/Add";
  494. $json[] = [
  495. "Inum" => "MaterialRequest",
  496. "data" =>[
  497. "iHead" => $data['iHead'],
  498. "iBody" => $data['iBody'],
  499. ],
  500. ];
  501. $json_str = json_encode($json);
  502. list($status, $result) = $this->post_helper($url, $json_str, $header, 30);
  503. if(! $status) return [false, $result];
  504. if(! isset($result['code'])) return [false, '生成领料申请单失败,请重新操作'];
  505. if($result['code'] != 0) return [false, $result['msg']];
  506. return [true, $result['data']];
  507. }
  508. public function productInAddU8($data,$common_array){
  509. list($status, $msg) = $this->getToken($common_array);
  510. if(! $status) return [false, $msg];
  511. list($host, $token) = $msg;
  512. if(empty($data['iHead'])) return [false, '产成品入库单单表头信息不能为空'];
  513. if(empty($data['iBody'])) return [false, '产成品入库单表体信息不能为空'];
  514. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  515. $url = $host . "/api/ProductIn/Add";
  516. $json[] = [
  517. "Inum" => "ProductIn",
  518. "data" =>[
  519. "iHead" => $data['iHead'],
  520. "iBody" => $data['iBody'],
  521. ],
  522. ];
  523. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  524. if(! $status) return [false, $result];
  525. if(! isset($result['code'])) return [false, '生成产成品入库单失败,请重新操作'];
  526. if($result['code'] != 0) return [false, $result['msg']];
  527. return [true, $result['data']];
  528. }
  529. public function dispatchAddU8($data,$common_array){
  530. list($status, $msg) = $this->getToken($common_array);
  531. if(! $status) return [false, $msg];
  532. list($host, $token) = $msg;
  533. if(empty($data['iHead'])) return [false, '发货单单表头信息不能为空'];
  534. if(empty($data['iBody'])) return [false, '发货单表体信息不能为空'];
  535. foreach ($data['iBody'] as $key => $value){
  536. $data['iBody'][$key] = $this->fillYonyouDispatchDetail($value);
  537. }
  538. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  539. $url = $host . "/api/Dispatch/Add";
  540. $json[] = [
  541. "Inum" => "DispatchList",
  542. "data" =>[
  543. "iHead" => $data['iHead'],
  544. "iBody" => $data['iBody'],
  545. ]
  546. ];
  547. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  548. if(! $status) return [false, $result];
  549. if(! isset($result['code'])) return [false, '生成发货单失败,请重新操作'];
  550. if($result['code'] != 0) return [false, $result['msg']];
  551. return [true, $result['data']];
  552. }
  553. public function recordList($data){
  554. $model = RecordTable::where('del_time',0)
  555. ->select('msg','data','type','crt_time','return_data')
  556. ->orderBy('id','desc');
  557. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  558. if(! empty($data['type'])) $model->where('type',$data['type']);
  559. if(! empty($data['site'])) $model->where('site',$data['site']);
  560. $list = $this->limit($model,'',$data);
  561. $list = $this->fillData($list);
  562. return [true,$list];
  563. }
  564. private function fillData($data){
  565. if(empty($data['data'])) return $data;
  566. foreach ($data['data'] as $key => $value){
  567. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  568. $data['data'][$key]['type_name'] = RecordTable::$type[$value['type']] ?? '';
  569. }
  570. return $data;
  571. }
  572. /**
  573. * 补全用友发货单子表金额字段(含换算率)
  574. * 输入示例:
  575. * [
  576. * 'iQuantity' => 1, // 输入单位数量
  577. * 'iunitprice' => 10, // 无税单价(按主计量单位)
  578. * 'iTaxRate' => 0, // 税率
  579. * 'iInvExchRate' => 12 // 换算率(1箱=12个)
  580. * ]
  581. */
  582. function fillYonyouDispatchDetail(array $item)
  583. {
  584. $qty = floatval($item['iQuantity']);
  585. $price = floatval($item['iunitprice']);
  586. $rate = floatval($item['iTaxRate']);
  587. $exchRate = floatval($item['iInvExchRate']);
  588. // 实际参与金额计算的基本数量(用友内部用这个)
  589. $qtyBase = $qty * $exchRate;
  590. // 税率
  591. $taxRate = $rate / 100;
  592. // 原币(人民币)
  593. $imoney = round($qtyBase * $price, 2);
  594. $itax = round($imoney * $taxRate, 2);
  595. $iSum = round($imoney + $itax, 2);
  596. $itaxunitprice = round($price * (1 + $taxRate), 6);
  597. // 本币 = 原币
  598. return array_merge($item, [
  599. 'imoney' => $imoney,
  600. 'itax' => $itax,
  601. 'iSum' => $iSum,
  602. 'itaxunitprice' => $itaxunitprice,
  603. 'idiscount' => 0,
  604. 'inatunitprice' => $price,
  605. 'inatmoney' => $imoney,
  606. 'inattax' => $itax,
  607. 'inatsum' => $iSum,
  608. 'inatdiscount' => 0,
  609. ]);
  610. }
  611. //-----------------------------------朗峰u8-----
  612. public function post_helper($url, $data, $header = [], $timeout = 20){
  613. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  614. $ch = curl_init();
  615. curl_setopt($ch, CURLOPT_URL, $url);
  616. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  617. curl_setopt($ch, CURLOPT_ENCODING, '');
  618. curl_setopt($ch, CURLOPT_POST, 1);
  619. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  620. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  621. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  622. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  623. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  624. $r = curl_exec($ch);
  625. if ($r === false) {
  626. // 获取错误号
  627. $errorNumber = curl_errno($ch);
  628. // 获取错误信息
  629. $errorMessage = curl_error($ch);
  630. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  631. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  632. return [false, $message];
  633. }
  634. curl_close($ch);
  635. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  636. return [true, json_decode($r, true)];
  637. }
  638. public function get_helper($url,$header=[],$timeout = 20){
  639. $ch = curl_init();
  640. curl_setopt_array($ch, array(
  641. CURLOPT_URL => $url,
  642. CURLOPT_RETURNTRANSFER => true,
  643. CURLOPT_ENCODING => '',
  644. CURLOPT_MAXREDIRS => 10,
  645. CURLOPT_TIMEOUT => $timeout,
  646. CURLOPT_FOLLOWLOCATION => true,
  647. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  648. CURLOPT_CUSTOMREQUEST => 'GET',
  649. CURLOPT_SSL_VERIFYPEER => false,
  650. CURLOPT_HTTPHEADER => $header,
  651. ));
  652. $r = curl_exec($ch);
  653. if ($r === false) {
  654. // 获取错误号
  655. $errorNumber = curl_errno($ch);
  656. // 获取错误信息
  657. $errorMessage = curl_error($ch);
  658. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  659. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  660. return [false, $message];
  661. }
  662. curl_close($ch);
  663. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  664. return [true, json_decode($r, true)];
  665. }
  666. public function put_helper($url, $data, $header = [], $timeout = 20){
  667. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  668. $ch = curl_init();
  669. curl_setopt($ch, CURLOPT_URL, $url);
  670. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  671. curl_setopt($ch, CURLOPT_ENCODING, '');
  672. curl_setopt($ch, CURLOPT_POST, 1);
  673. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  674. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  675. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  676. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  677. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  678. $r = curl_exec($ch);
  679. if ($r === false) {
  680. // 获取错误号
  681. $errorNumber = curl_errno($ch);
  682. // 获取错误信息
  683. $errorMessage = curl_error($ch);
  684. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  685. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  686. return [false, $message];
  687. }
  688. curl_close($ch);
  689. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  690. return [true, json_decode($r, true)];
  691. }
  692. }