TestService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. $page = $data['pageSize'] ?? 10;
  394. $version = $data['version'] ?? 0;
  395. $start_time = $data['start_time'] ?? $today;
  396. $end_time = $data['end_time'] ?? $today;
  397. $order_number = $data['order_number'] ?? '';
  398. $header = ["Authorization: {$token}",'Content-Type:application/json'];
  399. $url = $host . "/api/System/SqlQuery";
  400. $json = [
  401. 'customSQLFileName' => "U8SQL",
  402. 'customSQLPath' => 'U8API/SO_SOMain/Get',
  403. 'paramObj' => [
  404. "@pagesize" => $page,
  405. "@where" => "AND m.dDate >= '$start_time' AND m.dDate <= '$end_time'",
  406. "@code" => $order_number,
  407. "@version" => $version,
  408. ]
  409. ];
  410. $json = json_encode($json);
  411. list($status, $result) = $this->post_helper($url,$json, $header, 30);
  412. if(! $status) return [false, $result];
  413. if(! isset($result['code'])) return [false, '拉取销售订单失败,请重新拉取'];
  414. if($result['code'] != 0) return [false, $result['msg']];
  415. if(empty($result['data'])) return [true, []];
  416. $r_data = $result['data'];
  417. $return = $this->returnOrders($common_array['site'], $r_data, $msg);
  418. return [true, $return];
  419. }
  420. private function returnOrders($site, $r_data, $msg){
  421. $return = [];
  422. if($site == 'LFMY'){
  423. foreach ($r_data as $value){
  424. list($status, $detail) = $this->getSalesDetail($value, $msg);
  425. if(! $status) return [false, $detail];
  426. $return_detail = [];
  427. $total_qty = $money = 0;
  428. foreach ($detail as $d_value){
  429. $return_detail[] = [
  430. 'item_no' => $d_value['irowno'],
  431. 'material_code' => $d_value['cinvcode'],
  432. 'brand_name' => $d_value['cdefine28'] ?? '',
  433. 'safe' => $d_value['cdefine29'] ?? '',
  434. 'decor' => $d_value['cdefine30'] ?? '',
  435. 'craft_type_code' => $d_value['cdefine31'] ?? '',
  436. 'decor_b' => $d_value['cdefine32'] ?? '',
  437. 'craft_type_code_b' => $d_value['cdefine33'] ?? '',
  438. 'unit' => $d_value['cinvm_unit'],
  439. 'price' => $d_value['itaxunitprice'] ?? 0,
  440. 'not_tax_price' => $d_value['itaxunitprice'] ?? 0,
  441. 'tax_price' => $d_value['itaxunitprice'] ?? 0,
  442. 'qty' => $d_value['iquantity'],
  443. 'money' => $d_value['isum'] ?? 0,
  444. 'tax_amount' => $d_value['isum'] ?? 0,
  445. 'total_tax_amount' => $d_value['isum'] ?? 0,
  446. 'tax_rate' => $d_value['itaxrate'] ?? 0,
  447. 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])),
  448. 'remark' => $d_value['cmemo'] ?? '',
  449. ];
  450. $t = $d_value['isum'] ?? 0;
  451. $total_qty = bcadd($total_qty,$d_value['iquantity'],3);
  452. $money = bcadd($money,$t,3);
  453. }
  454. $return[] = [
  455. 'no' => $value['csocode'],
  456. 'order_date' => date("Y-m-d",strtotime($value['ddate'])),
  457. 'customer' => $value['ccuscode'] ?? '',
  458. 'salesman' => $value['cpersoncode'] ?? '',
  459. 'sale_department' => $value['cdepcode'] ?? '',
  460. 'total_qty' => $total_qty,
  461. 'total_money' => $money,
  462. 'remark' => $value['cmemo'] ?? '',
  463. 'version' => $value['version'],
  464. 'detail' => $return_detail,
  465. ];
  466. }
  467. }elseif ($site == 'HCLT'){
  468. foreach ($r_data as $value){
  469. list($status, $detail) = $this->getSalesDetail($value, $msg);
  470. if(! $status) return [false, $detail];
  471. $return_detail = [];
  472. $total_qty = $money = 0;
  473. foreach ($detail as $d_value){
  474. $return_detail[] = [
  475. 'material_code' => $d_value['cinvcode'] ?? '',
  476. 'specs' => $d_value['cinvstd'] ?? '',
  477. 'unit' => $d_value['cinvm_unit'] ?? '',
  478. 'qty' => $d_value['iquantity'],
  479. 'not_tax_price' => $d_value['iunitprice'] ?? 0,
  480. 'tax_price' => $d_value['itaxunitprice'] ?? 0,
  481. 'not_tax_amount' => $d_value['imoney'] ?? 0,
  482. 'tax_amount' => $d_value['itax'] ?? 0,
  483. 'total_tax_amount' => $d_value['isum'] ?? 0,
  484. 'tax_rate' => $d_value['itaxrate'] ?? 0,
  485. 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])),
  486. 'customer_brand' => $d_value['cfree1'] ?? '',
  487. 'color' => $d_value['cfree2'] ?? '',
  488. 'plan_no' => $d_value['cdefine22'] ?? '',
  489. 'contract_no' => $d_value['cdefine23'] ?? '',
  490. 'technical_require' => $d_value['cdefine28'] ?? '',
  491. 'quality_require' => $d_value['cdefine29'] ?? '',
  492. 'package_require' => $d_value['cdefine30'] ?? '',
  493. 'shipping_mark' => $d_value['cdefine31'] ?? '',
  494. ];
  495. $t = $d_value['isum'] ?? 0;
  496. $total_qty = bcadd($total_qty,$d_value['iquantity'],3);
  497. $money = bcadd($money,$t,3);
  498. }
  499. $return[] = [
  500. 'no' => $value['csocode'],
  501. 'order_date' => date("Y-m-d",strtotime($value['ddate'])),
  502. 'customer' => $value['ccuscode'] ?? '',
  503. 'salesman' => $value['cpersoncode'] ?? '',
  504. 'sale_department' => $value['cdepcode'] ?? '',
  505. 'total_qty' => $total_qty,
  506. 'total_money' => $money,
  507. 'remark' => $value['cmemo'] ?? '',
  508. 'version' => $value['version'],
  509. 'detail' => $return_detail,
  510. ];
  511. }
  512. }elseif ($site == 'JLWM'){
  513. foreach ($r_data as $value){
  514. list($status, $detail) = $this->getSalesDetail($value, $msg);
  515. if(! $status) return [false, $detail];
  516. $return_detail = [];
  517. $total_qty = $money = 0;
  518. foreach ($detail as $d_value){
  519. $return_detail[] = [
  520. 'material_code' => $d_value['cinvcode'] ?? '',
  521. 'specs' => $d_value['cinvstd'] ?? '',
  522. 'unit' => $d_value['cinvm_unit'] ?? '',
  523. 'qty' => $d_value['iquantity'] ?? '',
  524. 'not_tax_price' => $d_value['iunitprice'] ?? 0,
  525. 'tax_price' => $d_value['itaxunitprice'] ?? 0,
  526. 'not_tax_amount' => $d_value['imoney'] ?? 0,
  527. 'tax_amount' => $d_value['itax'] ?? 0,
  528. 'total_tax_amount' => $d_value['isum'] ?? 0,
  529. 'tax_rate' => $d_value['itaxrate'] ?? 0,
  530. 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])),
  531. 'customer_model' => $d_value['cfree4'] ?? '',
  532. 'length' => $d_value['cfree1'] ?? '',
  533. 'width' => $d_value['cfree2'] ?? '',
  534. 'thickness' => $d_value['cfree3'] ?? '',
  535. 'color_or_model' => $d_value['cdefine28'] ?? '',
  536. ];
  537. $t = $d_value['isum'] ?? 0;
  538. $total_qty = bcadd($total_qty,$d_value['iquantity'],3);
  539. $money = bcadd($money,$t,3);
  540. }
  541. $return[] = [
  542. 'no' => $value['csocode'],
  543. 'order_date' => date("Y-m-d",strtotime($value['ddate'])),
  544. 'customer' => $value['ccuscode'] ?? '',
  545. 'salesman' => $value['cpersoncode'] ?? '',
  546. 'sale_department' => $value['cdepcode'] ?? '',
  547. 'total_qty' => $total_qty,
  548. 'total_money' => $money,
  549. 'remark' => $value['cmemo'] ?? '',
  550. 'version' => $value['version'],
  551. 'detail' => $return_detail,
  552. ];
  553. }
  554. }
  555. return $return;
  556. }
  557. private function SetU8($common_array){
  558. // $api_host = env('API_HOST');
  559. // if(empty($api_host)) return [false, '用友对外域名不存在'];
  560. // $api_port = env('API_PORT');
  561. // if(empty($api_port)) return [false, '用友对外域名端口不存在'];
  562. $api_host = $common_array['api_host'];
  563. $api_port = $common_array['api_port'];
  564. //映射ip是否通畅
  565. $bool = $this->isDomainAvailable($api_host);
  566. if(! $bool) return [false, '用友对外域名不可达'];
  567. $host = $api_host . ":" . $api_port;
  568. return [true, $host];
  569. }
  570. private function getSalesDetail($sale_order, $msg){
  571. list($host, $token) = $msg;
  572. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  573. $url = $host . "/api/System/SqlQuery2";
  574. $json = [
  575. "customSQLFileName"=> "U8SQL",
  576. "customSQLPath"=> "U8API/SO_SOMain/GetWithDetail",
  577. "paramObj"=> [
  578. "@code"=> $sale_order["csocode"]
  579. ]
  580. ];
  581. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  582. if(! $status) return [false, $result];
  583. if(! isset($result['code'])) return [false, '拉取销售订单详情失败,请重新拉取'];
  584. if($result['code'] != 0) return [false, $result['msg']];
  585. return [true, $result['data']['DataTable1']];
  586. }
  587. public function materialAddU8($data,$common_array){
  588. list($status, $msg) = $this->getToken($common_array);
  589. if(! $status) return [false, $msg];
  590. list($host, $token) = $msg;
  591. if(empty($data['iHead'])) return [false, '领料单表头信息不能为空'];
  592. if(empty($data['iBody'])) return [false, '领料单表体信息不能为空'];
  593. $header = ["Authorization: {$token}",'Content-Type:application/json'];
  594. $url = $host . "/api/MaterialRequest/Add";
  595. $json[] = [
  596. "Inum" => "MaterialRequest",
  597. "data" =>[
  598. "iHead" => $data['iHead'],
  599. "iBody" => $data['iBody'],
  600. ],
  601. ];
  602. $json_str = json_encode($json);
  603. list($status, $result) = $this->post_helper($url, $json_str, $header, 30);
  604. if(! $status) return [false, $result];
  605. if(! isset($result['code'])) return [false, '生成领料申请单失败,请重新操作'];
  606. if($result['code'] != 0) return [false, $result['msg']];
  607. return [true, $result['data']];
  608. }
  609. public function productInAddU8($data,$common_array){
  610. list($status, $msg) = $this->getToken($common_array);
  611. if(! $status) return [false, $msg];
  612. list($host, $token) = $msg;
  613. if(empty($data['iHead'])) return [false, '产成品入库单单表头信息不能为空'];
  614. if(empty($data['iBody'])) return [false, '产成品入库单表体信息不能为空'];
  615. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  616. $url = $host . "/api/ProductIn/Add";
  617. $json[] = [
  618. "Inum" => "ProductIn",
  619. "data" =>[
  620. "iHead" => $data['iHead'],
  621. "iBody" => $data['iBody'],
  622. ],
  623. ];
  624. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  625. if(! $status) return [false, $result];
  626. if(! isset($result['code'])) return [false, '生成产成品入库单失败,请重新操作'];
  627. if($result['code'] != 0) return [false, $result['msg']];
  628. return [true, $result['data']];
  629. }
  630. public function dispatchAddU8($data,$common_array){
  631. list($status, $msg) = $this->getToken($common_array);
  632. if(! $status) return [false, $msg];
  633. list($host, $token) = $msg;
  634. if(empty($data['iHead'])) return [false, '发货单单表头信息不能为空'];
  635. if(empty($data['iBody'])) return [false, '发货单表体信息不能为空'];
  636. foreach ($data['iBody'] as $key => $value){
  637. $data['iBody'][$key] = $this->fillYonyouDispatchDetail($value);
  638. }
  639. $header = ["Authorization: {$token}",'Content-Type:application/json'];;
  640. $url = $host . "/api/Dispatch/Add";
  641. $json[] = [
  642. "Inum" => "DispatchList",
  643. "data" =>[
  644. "iHead" => $data['iHead'],
  645. "iBody" => $data['iBody'],
  646. ]
  647. ];
  648. list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
  649. if(! $status) return [false, $result];
  650. if(! isset($result['code'])) return [false, '生成发货单失败,请重新操作'];
  651. if($result['code'] != 0) return [false, $result['msg']];
  652. return [true, $result['data']];
  653. }
  654. public function recordList($data){
  655. $model = RecordTable::where('del_time',0)
  656. ->select('msg','data','type','crt_time','return_data')
  657. ->orderBy('id','desc');
  658. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  659. if(! empty($data['type'])) $model->where('type',$data['type']);
  660. if(! empty($data['site'])) $model->where('site',$data['site']);
  661. $list = $this->limit($model,'',$data);
  662. $list = $this->fillData($list);
  663. return [true,$list];
  664. }
  665. private function fillData($data){
  666. if(empty($data['data'])) return $data;
  667. foreach ($data['data'] as $key => $value){
  668. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  669. $data['data'][$key]['type_name'] = RecordTable::$type[$value['type']] ?? '';
  670. }
  671. return $data;
  672. }
  673. /**
  674. * 补全用友发货单子表金额字段(含换算率)
  675. * 输入示例:
  676. * [
  677. * 'iQuantity' => 1, // 输入单位数量
  678. * 'iunitprice' => 10, // 无税单价(按主计量单位)
  679. * 'iTaxRate' => 0, // 税率
  680. * 'iInvExchRate' => 12 // 换算率(1箱=12个)
  681. * ]
  682. */
  683. function fillYonyouDispatchDetail(array $item)
  684. {
  685. $qty = floatval($item['iQuantity']);
  686. $price = floatval($item['iunitprice']);
  687. $rate = floatval($item['iTaxRate']);
  688. $exchRate = floatval($item['iInvExchRate']);
  689. // 实际参与金额计算的基本数量(用友内部用这个)
  690. $qtyBase = $qty * $exchRate;
  691. // 税率
  692. $taxRate = $rate / 100;
  693. // 原币(人民币)
  694. $imoney = round($qtyBase * $price, 2);
  695. $itax = round($imoney * $taxRate, 2);
  696. $iSum = round($imoney + $itax, 2);
  697. $itaxunitprice = round($price * (1 + $taxRate), 6);
  698. // 本币 = 原币
  699. return array_merge($item, [
  700. 'imoney' => $imoney,
  701. 'itax' => $itax,
  702. 'iSum' => $iSum,
  703. 'itaxunitprice' => $itaxunitprice,
  704. 'idiscount' => 0,
  705. 'inatunitprice' => $price,
  706. 'inatmoney' => $imoney,
  707. 'inattax' => $itax,
  708. 'inatsum' => $iSum,
  709. 'inatdiscount' => 0,
  710. ]);
  711. }
  712. //-----------------------------------朗峰u8-----
  713. public function post_helper($url, $data, $header = [], $timeout = 20){
  714. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  715. $ch = curl_init();
  716. curl_setopt($ch, CURLOPT_URL, $url);
  717. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  718. curl_setopt($ch, CURLOPT_ENCODING, '');
  719. curl_setopt($ch, CURLOPT_POST, 1);
  720. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  721. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  722. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  723. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  724. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  725. $r = curl_exec($ch);
  726. if ($r === false) {
  727. // 获取错误号
  728. $errorNumber = curl_errno($ch);
  729. // 获取错误信息
  730. $errorMessage = curl_error($ch);
  731. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  732. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  733. return [false, $message];
  734. }
  735. curl_close($ch);
  736. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  737. return [true, json_decode($r, true)];
  738. }
  739. public function get_helper($url,$header=[],$timeout = 20){
  740. $ch = curl_init();
  741. curl_setopt_array($ch, array(
  742. CURLOPT_URL => $url,
  743. CURLOPT_RETURNTRANSFER => true,
  744. CURLOPT_ENCODING => '',
  745. CURLOPT_MAXREDIRS => 10,
  746. CURLOPT_TIMEOUT => $timeout,
  747. CURLOPT_FOLLOWLOCATION => true,
  748. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  749. CURLOPT_CUSTOMREQUEST => 'GET',
  750. CURLOPT_SSL_VERIFYPEER => false,
  751. CURLOPT_HTTPHEADER => $header,
  752. ));
  753. $r = curl_exec($ch);
  754. if ($r === false) {
  755. // 获取错误号
  756. $errorNumber = curl_errno($ch);
  757. // 获取错误信息
  758. $errorMessage = curl_error($ch);
  759. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  760. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  761. return [false, $message];
  762. }
  763. curl_close($ch);
  764. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  765. return [true, json_decode($r, true)];
  766. }
  767. public function put_helper($url, $data, $header = [], $timeout = 20){
  768. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  769. $ch = curl_init();
  770. curl_setopt($ch, CURLOPT_URL, $url);
  771. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  772. curl_setopt($ch, CURLOPT_ENCODING, '');
  773. curl_setopt($ch, CURLOPT_POST, 1);
  774. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  775. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  776. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  777. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  778. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  779. $r = curl_exec($ch);
  780. if ($r === false) {
  781. // 获取错误号
  782. $errorNumber = curl_errno($ch);
  783. // 获取错误信息
  784. $errorMessage = curl_error($ch);
  785. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  786. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  787. return [false, $message];
  788. }
  789. curl_close($ch);
  790. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  791. return [true, json_decode($r, true)];
  792. }
  793. }