Ver código fonte

对接朗峰

cqp 1 semana atrás
pai
commit
af9a5b9b03

+ 40 - 0
app/Http/Controllers/Api/TestController.php

@@ -90,4 +90,44 @@ class TestController extends BaseController
     public function test(){
         dd(env("maycur_appcode"),env("maycur_appsercet"),getenv("maycur_appcode"),getenv("maycur_appsercet"));
     }
+
+    public function salesOrderGet(Request $request){
+        list($bool, $data) = (new TestService())->salesOrderGet($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function materialAddU8(Request $request){
+        list($bool, $data) = (new TestService())->materialAddU8($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function productInAddU8(Request $request){
+        list($bool, $data) = (new TestService())->ProductInAddU8($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function dispatchAddU8(Request $request){
+        list($bool, $data) = (new TestService())->DispatchAddU8($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 262 - 0
app/Service/TestService.php

@@ -2,6 +2,7 @@
 
 namespace App\Service;
 
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Config;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -414,6 +415,267 @@ class TestService extends Service
         return [true, ''];
     }
 
+    public function getToken(){
+        list($status, $msg) = $this->SetU8();
+        if(! $status) return [false , $msg];
+        $host = $msg;
+        $key = "lf_u8_long_token_demo";
+        if(! Cache::has($key)){
+            $url = $host . "/api/System/GetToken";
+            $date = date("Y-m-d");
+            $json = [
+                "U8DbName"=> "UFDATA_001_2025",
+                "sUserId"=> "demo",
+                "sPassword"=> "DEMO",
+                "LoginDateTime"=> $date,
+                "bPersist"=> true
+            ];
+            $header = ['Content-Type:application/json'];
+            list($status, $result) = $this->post_helper($url,json_encode($json), $header, 30);
+            if(! $status) return [false, $result];
+            if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+            if($result['code'] != 0) return [false, $result['msg']];
+
+            $token = $result['data']['Token'] ?? "";
+
+            Cache::forever($key, $token);
+        }else{
+            $token = Cache::get($key);
+        }
+
+        return [true, [$host, $token]];
+    }
+
+    //-----------------------------------朗峰u8-----
+
+    public function salesOrderGet($data){
+        list($status, $msg) = $this->getToken();
+        if(! $status) return [false, $msg];
+        list($host, $token) = $msg;
+
+        $header = ["Authorization: {$token}",'Content-Type:application/json'];;
+        $url = $host . "/api/System/SqlQuery";
+        $json = '{
+                    "customSQLFileName": "U8SQL",
+                    "customSQLPath": "U8API/SO_SOMain/Get",
+                    "paramObj": {
+                        "@pagesize": 3,
+                    }
+                }';
+        list($status, $result) = $this->post_helper($url,$json, $header, 30);
+        if(! $status) return [false, $result];
+        if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+        if($result['code'] != 0) return [false, $result['msg']];
+        if(empty($result['data'])) return [true, []];
+
+        $return = [];
+        $r_data = $result['data'];
+        foreach ($r_data as $value){
+            list($status, $detail) = $this->getSalesDetail($value, $msg);
+            if(! $status) return [false, $detail];
+            $return_detail = [];
+            $total_qty = $money = 0;
+            foreach ($detail as $d_value){
+                $return_detail[] = [
+                    'item_no' => $d_value['irowno'],
+                    'material_code' => $d_value['cinvcode'],
+                    'decor' => $d_value['cdefine30'] ?? '',
+                    'craft_type_code' => $d_value['cdefine31'] ?? '',
+                    'decor_b' => $d_value['cdefine32'] ?? '',
+                    'craft_type_code_b' => $d_value['cdefine33'] ?? '',
+                    'unit' => $d_value['cinvm_unit'],
+                    'price' => $d_value['itaxunitprice'],
+                    'not_tax_price' => $d_value['itaxunitprice'],
+                    'tax_price' => $d_value['itaxunitprice'],
+                    'qty' => $d_value['iquantity'],
+                    'money' => $d_value['isum'],
+                    'tax_amount' => $d_value['isum'],
+                    'total_tax_amount' => $d_value['isum'],
+                    'tax_rate' => $d_value['itaxrate'],
+                    'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])),
+                    'remark' => $d_value['cmemo'] ?? '',
+                ];
+                $total_qty = bcadd($total_qty,$d_value['iquantity'],3);
+                $money = bcadd($money,$d_value['isum'],3);
+            }
+
+            $return[] = [
+                'no' => $value['csocode'],
+                'order_date' => date("Y-m-d",strtotime($value['ddate'])),
+                'customer' => $value['ccuscode'] ?? '',
+                'salesman' => $value['cpersoncode'] ?? '',
+                'sale_department' => $value['cdepcode'] ?? '',
+                'total_qty' => $total_qty,
+                'total_money' => $money,
+                'remark' => $value['cmemo'] ?? '',
+                'detail' => $return_detail,
+            ];
+        }
+
+        return [true, $return];
+    }
+
+    private function SetU8(){
+        $api_host = env('API_HOST');
+        if(empty($api_host)) return [false, '用友对外域名不存在'];
+        $api_port = env('API_PORT');
+        if(empty($api_port)) return [false, '用友对外域名端口不存在'];
+        //映射ip是否通畅
+        $bool = $this->isDomainAvailable($api_host);
+        if(! $bool) return [false, '用友对外域名不可达'];
+        $host = $api_host . ":" . $api_port;
+
+        return [true, $host];
+    }
+
+    private function getSalesDetail($sale_order, $msg){
+        list($host, $token) = $msg;
+
+        $header = ["Authorization: {$token}",'Content-Type:application/json'];;
+        $url = $host . "/api/System/SqlQuery2";
+        $json = [
+            "customSQLFileName"=> "U8SQL",
+            "customSQLPath"=> "U8API/SO_SOMain/GetWithDetail",
+            "paramObj"=> [
+                "@code"=> $sale_order["csocode"]
+            ]
+        ];
+        list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
+        if(! $status) return [false, $result];
+        if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+        if($result['code'] != 0) return [false, $result['msg']];
+        
+        return [true, $result['data']['DataTable1']];
+    }
+
+    public function materialAddU8($data){
+        list($status, $msg) = $this->getToken();
+        if(! $status) return [false, $msg];
+        list($host, $token) = $msg;
+
+        if(empty($data['iHead'])) return [false, '领料单表头信息不能为空'];
+        if(empty($data['iBody'])) return [false, '领料单表体信息不能为空'];
+
+        $header = ["Authorization: {$token}",'Content-Type:application/json'];;
+        $url = $host . "/api/MaterialRequest/Add";
+        $json[] = [
+            "Inum" => "MaterialRequest",
+            "data" =>[
+                "iHead" => $data['iHead'],
+                "iBody" => $data['iBody'],
+            ],
+        ];
+
+        list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
+        if(! $status) return [false, $result];
+        if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+        if($result['code'] != 0) return [false, $result['msg']];
+
+        return [true, $result['data']];
+    }
+
+    public function productInAddU8($data){
+        list($status, $msg) = $this->getToken();
+        if(! $status) return [false, $msg];
+        list($host, $token) = $msg;
+
+        if(empty($data['iHead'])) return [false, '产成品入库单单表头信息不能为空'];
+        if(empty($data['iBody'])) return [false, '产成品入库单表体信息不能为空'];
+
+        $header = ["Authorization: {$token}",'Content-Type:application/json'];;
+        $url = $host . "/api/ProductIn/Add";
+        $json[] = [
+            "Inum" => "ProductIn",
+            "data" =>[
+                "iHead" => $data['iHead'],
+                "iBody" => $data['iBody'],
+            ],
+        ];
+
+        list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
+        if(! $status) return [false, $result];
+        if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+        if($result['code'] != 0) return [false, $result['msg']];
+
+        return [true, $result['data']];
+    }
+
+    public function dispatchAddU8($data){
+        list($status, $msg) = $this->getToken();
+        if(! $status) return [false, $msg];
+        list($host, $token) = $msg;
+
+        if(empty($data['iHead'])) return [false, '发货单单表头信息不能为空'];
+        if(empty($data['iBody'])) return [false, '发货单表体信息不能为空'];
+
+        foreach ($data['iBody'] as $key => $value){
+            $data['iBody'][$key] = $this->fillYonyouDispatchDetail($value);
+        }
+
+        $header = ["Authorization: {$token}",'Content-Type:application/json'];;
+        $url = $host . "/api/Dispatch/Add";
+        $json[] = [
+            "Inum" => "DispatchList",
+            "data" =>[
+                "iHead" => $data['iHead'],
+                "iBody" => $data['iBody'],
+            ]
+        ];
+
+        list($status, $result) = $this->post_helper($url, json_encode($json), $header, 30);
+        if(! $status) return [false, $result];
+        if(! isset($result['code'])) return [false, '异常错误,请联系开发者'];
+        if($result['code'] != 0) return [false, $result['msg']];
+
+        return [true, $result['data']];
+    }
+
+    /**
+     * 补全用友发货单子表金额字段(含换算率)
+     * 输入示例:
+     * [
+     *   'iQuantity'     => 1,     // 输入单位数量
+     *   'iunitprice'    => 10,    // 无税单价(按主计量单位)
+     *   'iTaxRate'      => 0,     // 税率
+     *   'iInvExchRate'  => 12     // 换算率(1箱=12个)
+     * ]
+     */
+    function fillYonyouDispatchDetail(array $item)
+    {
+        $qty       = floatval($item['iQuantity']);
+        $price     = floatval($item['iunitprice']);
+        $rate      = floatval($item['iTaxRate']);
+        $exchRate  = floatval($item['iInvExchRate']);
+
+        // 实际参与金额计算的基本数量(用友内部用这个)
+        $qtyBase = $qty * $exchRate;
+
+        // 税率
+        $taxRate = $rate / 100;
+
+        // 原币(人民币)
+        $imoney = round($qtyBase * $price, 2);
+        $itax   = round($imoney * $taxRate, 2);
+        $iSum   = round($imoney + $itax, 2);
+
+        $itaxunitprice = round($price * (1 + $taxRate), 6);
+
+        // 本币 = 原币
+        return array_merge($item, [
+            'imoney'          => $imoney,
+            'itax'            => $itax,
+            'iSum'            => $iSum,
+            'itaxunitprice'   => $itaxunitprice,
+            'idiscount'       => 0,
+            'inatunitprice'   => $price,
+            'inatmoney'       => $imoney,
+            'inattax'         => $itax,
+            'inatsum'         => $iSum,
+            'inatdiscount'    => 0,
+        ]);
+    }
+
+    //-----------------------------------朗峰u8-----
     public function post_helper($url, $data, $header = [], $timeout = 20){
         Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
 

+ 6 - 0
routes/api.php

@@ -58,6 +58,12 @@ Route::any('getSnList', 'Api\TestController@getSnList');
 Route::any('getSnforMap', 'Api\TestController@getSnforMap');
 Route::any('getSnForWarranty', 'Api\TestController@getSnForWarranty');
 Route::any('saveSnUseData', 'Api\TestController@saveSnUseData');
+
+Route::any('salesOrderGet', 'Api\TestController@salesOrderGet');
+Route::any('materialAddU8', 'Api\TestController@materialAddU8');
+Route::any('productInAddU8', 'Api\TestController@productInAddU8');
+Route::any('dispatchAddU8', 'Api\TestController@dispatchAddU8');
+
 Route::group(['middleware'=> ['CheckJRFIDLogin']],function ($route){
     //站点获取登录后
     $route->any('getSiteByLogin', 'Api\JRFIDController@getSite2');