post_helper($url,$json, $header, 40,'LFMY'); if(! $status) return [false, $result]; return [true, $result]; } public function testdwyget($data){ if(empty($data['url']) || empty($data['header'])) return [false,'API请求参数不能为空']; $url = $data['url']; // $oldUrl = config('ip.zslf'); // $newUrl = config('ip.zslfip'); // $url = str_replace($oldUrl, $newUrl, $url); $header = $data['header']; list($status,$result) = $this->get_helper($url,$header); if(! $status) return [false, $result]; return [true, $result]; } public function testdwyput($data){ if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空']; $url = $data['url']; // $oldUrl = config('ip.zslf'); // $newUrl = config('ip.zslfip'); // $url = str_replace($oldUrl, $newUrl, $url); $post = $data['post']; $header = $data['header']; $json = json_encode($post); $json = str_replace('"workflowSearchBean":{}','"workflowSearchBean":[]',json_encode($post)); $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post)); list($status, $result) = $this->put_helper($url,$json, $header,40); if(! $status) return [false, $result]; return [true, $result]; } //-----------------------------------朗峰u8----- public function getToken($common_array){ list($status, $msg) = $this->SetU8($common_array); if(! $status) return [false , $msg]; $host = $msg; $key = "lf_u8_long_token_demo_" . $common_array['site'] . '_' . $common_array['database']; if(! Cache::has($key)){ $url = $host . "/api/System/GetToken"; $date = date("Y-m-d"); $json = [ "U8DbName"=> $common_array['database'], "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, $common_array['title'] . "获取token"); 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]]; } public function salesOrderGet($data, $common_array){ list($status, $msg) = $this->getToken($common_array); if(! $status) return [false, $msg]; list($host, $token) = $msg; $today = (new \DateTime())->setTime(0, 0, 0)->format('Y-m-d H:i:s') . '.000'; $fourDaysAgo = (new \DateTime())->sub(new \DateInterval('P4D'))->setTime(0, 0, 0)->format('Y-m-d H:i:s') . '.000'; $page = $data['pageSize'] ?? 10; $version = $data['version'] ?? 0; $start_time = $data['start_time'] ?? $today; $end_time = $data['end_time'] ?? $today; $order_number = $data['order_number'] ?? ''; $header = ["Authorization: {$token}",'Content-Type:application/json']; $url = $host . "/api/System/SqlQuery"; $json = [ 'customSQLFileName' => "U8SQL", 'customSQLPath' => 'U8API/SO_SOMain/Get', 'paramObj' => [ "@pagesize" => $page, "@where" => "AND m.dDate >= '$start_time' AND m.dDate <= '$end_time'", "@code" => $order_number, "@version" => $version, ] ]; $json = json_encode($json); list($status, $result) = $this->post_helper($url,$json, $header, 60, $common_array['title'] . '获取销售订单'); 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, []]; $r_data = $result['data']; $return = $this->returnOrders($common_array, $r_data, $msg); return [true, $return]; } private function returnOrders($common_array, $r_data, $msg){ $site = $common_array['site']; $title = $common_array['title']; $return = []; if($site == 'LFMY'){ foreach ($r_data as $value){ list($status, $detail) = $this->getSalesDetail($value, $msg, $title); 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'], 'brand_name' => $d_value['cdefine28'] ?? '', 'safe' => $d_value['cdefine29'] ?? '', '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'] ?? 0, 'not_tax_price' => $d_value['itaxunitprice'] ?? 0, 'tax_price' => $d_value['itaxunitprice'] ?? 0, 'qty' => $d_value['iquantity'], 'money' => $d_value['isum'] ?? 0, 'tax_amount' => $d_value['isum'] ?? 0, 'total_tax_amount' => $d_value['isum'] ?? 0, 'tax_rate' => $d_value['itaxrate'] ?? 0, 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])), 'remark' => $d_value['cmemo'] ?? '', ]; $t = $d_value['isum'] ?? 0; $total_qty = bcadd($total_qty,$d_value['iquantity'],3); $money = bcadd($money,$t,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'] ?? '', 'version' => $value['version'], 'detail' => $return_detail, ]; } }elseif ($site == 'HCLT'){ foreach ($r_data as $value){ list($status, $detail) = $this->getSalesDetail($value, $msg, $title); if(! $status) return [false, $detail]; $return_detail = []; $total_qty = $money = 0; foreach ($detail as $d_value){ $return_detail[] = [ 'material_code' => $d_value['cinvcode'] ?? '', 'specs' => $d_value['cinvstd'] ?? '', 'unit' => $d_value['cinvm_unit'] ?? '', 'qty' => $d_value['iquantity'], 'not_tax_price' => $d_value['iunitprice'] ?? 0, 'tax_price' => $d_value['itaxunitprice'] ?? 0, 'not_tax_amount' => $d_value['imoney'] ?? 0, 'tax_amount' => $d_value['itax'] ?? 0, 'total_tax_amount' => $d_value['isum'] ?? 0, 'tax_rate' => $d_value['itaxrate'] ?? 0, 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])), 'customer_brand' => $d_value['cfree1'] ?? '', 'color' => $d_value['cfree2'] ?? '', 'plan_no' => $d_value['cdefine22'] ?? '', 'contract_no' => $d_value['cdefine23'] ?? '', 'technical_require' => $d_value['cdefine28'] ?? '', 'quality_require' => $d_value['cdefine29'] ?? '', 'package_require' => $d_value['cdefine30'] ?? '', 'shipping_mark' => $d_value['cdefine31'] ?? '', ]; $t = $d_value['isum'] ?? 0; $total_qty = bcadd($total_qty,$d_value['iquantity'],3); $money = bcadd($money,$t,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'] ?? '', 'version' => $value['version'], 'detail' => $return_detail, ]; } }elseif ($site == 'JLWM'){ foreach ($r_data as $value){ list($status, $detail) = $this->getSalesDetail($value, $msg, $title); if(! $status) return [false, $detail]; $return_detail = []; $total_qty = $money = 0; foreach ($detail as $d_value){ $return_detail[] = [ 'material_code' => $d_value['cinvcode'] ?? '', 'contract_no' => $d_value['cdefine23'] ?? '', 'specs' => $d_value['cinvstd'] ?? '', 'unit' => $d_value['cinvm_unit'] ?? '', 'qty' => $d_value['iquantity'] ?? '', 'not_tax_price' => $d_value['iunitprice'] ?? 0, 'tax_price' => $d_value['itaxunitprice'] ?? 0, 'not_tax_amount' => $d_value['imoney'] ?? 0, 'tax_amount' => $d_value['itax'] ?? 0, 'total_tax_amount' => $d_value['isum'] ?? 0, 'tax_rate' => $d_value['itaxrate'] ?? 0, 'expected_delivery_date' => date('Y-m-d',strtotime($d_value['dpredate'])), 'customer_model' => $d_value['cfree4'] ?? '', 'length' => $d_value['cfree1'] ?? '', 'width' => $d_value['cfree2'] ?? '', 'thickness' => $d_value['cfree3'] ?? '', 'color_or_model' => $d_value['cdefine28'] ?? '', ]; $t = $d_value['isum'] ?? 0; $total_qty = bcadd($total_qty,$d_value['iquantity'],3); $money = bcadd($money,$t,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'] ?? '', 'version' => $value['version'], 'detail' => $return_detail, ]; } } return $return; } private function SetU8($common_array){ // $api_host = env('API_HOST'); // if(empty($api_host)) return [false, '用友对外域名不存在']; // $api_port = env('API_PORT'); // if(empty($api_port)) return [false, '用友对外域名端口不存在']; $api_host = $common_array['api_host']; $api_port = $common_array['api_port']; //映射ip是否通畅 $bool = $this->isDomainAvailable($api_host); if(! $bool) return [false, '用友对外域名不可达']; $host = $api_host . ":" . $api_port; return [true, $host]; } private function getSalesDetail($sale_order, $msg, $title){ 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, $title . '获取销售订单详情'); 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,$common_array){ list($status, $msg) = $this->getToken($common_array); if(! $status) return [false, $msg]; list($host, $token) = $msg; $title = $common_array['title']; 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'], ], ]; $json_str = json_encode($json); list($status, $result) = $this->post_helper($url, $json_str, $header, 60, $title . '生成领料申请单'); 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,$common_array){ list($status, $msg) = $this->getToken($common_array); if(! $status) return [false, $msg]; list($host, $token) = $msg; $title = $common_array['title']; 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, 60, $title . '生成产成品入库单'); 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,$common_array){ list($status, $msg) = $this->getToken($common_array); if(! $status) return [false, $msg]; list($host, $token) = $msg; $title = $common_array['title']; 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, 60, $title . '生成发货单'); 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 recordList($data){ $model = RecordTable::where('del_time',0) ->select('msg','data','type','crt_time','return_data') ->orderBy('id','desc'); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]); if(! empty($data['type'])) $model->where('type',$data['type']); if(! empty($data['site'])) $model->where('site',$data['site']); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true,$list]; } private function fillData($data){ if(empty($data['data'])) return $data; foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['type_name'] = RecordTable::$type[$value['type']] ?? ''; } return $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, $title = ""){ Log::channel('apiLog')->info($title . 'POST', ["api" => $url , "param" => json_decode($data,true) ,"header" => $header]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $r = curl_exec($ch); if ($r === false) { // 获取错误号 $errorNumber = curl_errno($ch); // 获取错误信息 $errorMessage = curl_error($ch); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info($title . 'POST结果', ["message" => $message ]); return [false, $message]; } curl_close($ch); $return = json_decode($r, true); unset($r); Log::channel('apiLog')->info($title . 'POST结果', ["message" => $return ]); return [true, $return]; } public function get_helper($url,$header=[],$timeout = 20){ $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => $timeout, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTPHEADER => $header, )); $r = curl_exec($ch); if ($r === false) { // 获取错误号 $errorNumber = curl_errno($ch); // 获取错误信息 $errorMessage = curl_error($ch); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]); return [false, $message]; } curl_close($ch); Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]); return [true, json_decode($r, true)]; } public function put_helper($url, $data, $header = [], $timeout = 20){ Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $r = curl_exec($ch); if ($r === false) { // 获取错误号 $errorNumber = curl_errno($ch); // 获取错误信息 $errorMessage = curl_error($ch); $message = "cURL Error #{$errorNumber}: {$errorMessage}"; Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]); return [false, $message]; } curl_close($ch); Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]); return [true, json_decode($r, true)]; } }