12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468 |
- <?php
- declare (strict_types = 1);
- namespace app\Admin\controller;
- use app\BaseController;
- use think\facade\Db;
- use think\Request;
- class Receipt extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function list()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $condition = [['is_del',"=",0],['refund_status',"=",0]];
- $guserinfo =GetUserInfo($token);
- if(isset($guserinfo['code']) && $guserinfo['code']!=0){
- return error_show($guserinfo['code'],$guserinfo['message']);
- }
- $check = checkRole($guserinfo['data']['roleid'],52);
- if($check){
- $condition[]=["apply_id","=", $guserinfo['data']['id']];
- }
- $no = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
- if ($no != "") {
- $condition[] = ["inv_in", "=", "{$no}"];
- }
- $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']) : "";
- if ($status !== "") {
- $condition[] = ["status", "=", "{$status}"];
- }
- $type_check = isset($post['type_check']) && $post['type_check'] != "" ? $post['type_check'] : "";
- if ($type_check != "") {
- $condition[] = ["type_check", "=", "{$type_check}"];
- }
- $starttime = isset($post['starttime']) && $post['starttime'] != "" ? $post['starttime'] : "";
- if ($starttime != "") {
- $starttime = $starttime." 00:00:00";
- $condition[] = ["addtime", ">=", "{$starttime}"];
- }
- $endtime = isset($post['endtime']) && $post['endtime'] != "" ? $post['endtime'] : "";
- if ($endtime != "") {
- $endtime = $endtime." 23:59:59";
- $condition[] = ["addtime", "<=", "{$endtime}"];
- }
- $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
- $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
- $count = Db::name("invoice_pool")->where($condition)->count();
- $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
- $page = $page >= $total ? intval($total) : $page;
- $list = Db::name("invoice_pool")->where($condition)->order("addtime desc")->page(intval($page), $size)->select();
- $data=[];
- foreach ($list as $value){
- $customer= Db::name("customer_info")->where("companyNo","=",$value['inv_in'])->field("companyName,contactor")
- ->find();
- $value['company'] = isset($customer['companyName']) ? $customer['companyName']:"";
- $value['contactor'] = isset($customer['contactor']) ? $customer['contactor']:"";
- $out = Db::name('company_info')->where('companyNo',"=",$value['inv_out'])->find();
- $value['kp_company'] =isset($out['company_name']) ? $out['company_name']:"";
- $post = Db::name("inv_post")->where("invNo","=",$value['invNo'])->find();
- $value['post_company']=isset($post['post_company'])&&$post['post_company']!="" ? $post['post_company']:"";
- $value['post_code']=isset($post['postNo'])&&$post['postNo']!="" ? $post['postNo']:"";
- $data[]=$value;
- }
- return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- // $companyNo = isset($post['tradeNo'])&&$post['tradeNo']!=""?trim($post['tradeNo']):"";
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $type = isset($post['type'])&&$post['type']!=""?intval($post['type']):1;
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $good = isset($post['good'])&&is_array($post['good'])?$post['good']:"";
- if($good==""&& empty($good)){
- return error_show(1004,"商品信息不能为空");
- }
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- $orderinfo=[];
- $orderNo=makeNo('ORD');
- $total=0;
- $fund_fee=0;
- $wfund_fee=0;
- $inv_fee=0;
- $winv_fee=0;
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- Db::startTrans();
- try{
- foreach ($good as $value){
- $temp=[];
- if(!isset($value['num'])||$value['num']==0){
- return error_show(1004,"商品数量不能为空");
- }
- if(!isset($value['total_fee'])||$value['total_fee']==0){
- return error_show(1004,"商品总金额不能为空");
- }
- $temp['orderNo'] = $orderNo;
- $temp['goodNo']= $value['sequenceNo'];
- $goocp = Db::name("qrd_list")->where("sequenceNo","=",$value['sequenceNo'])->find();
- $temp['good_name']= $goocp['goodname'];
- $temp['qrdNo'] = $goocp['qrdNo'];
- $temp['brand']= $goocp['brand'];
- $temp['unit']= $value['unit'];
- $temp['good_num']= $value['num'];
- $temp['price']= $goocp['price'];
- $temp['total_fee']= $value['total_fee'];
- $temp['wfund_fee'] =$value['total_fee'];
- $temp['winv_fee'] = $value['total_fee'];
- $temp['other_fee']= 0;
- $temp['status']= 1;
- $temp['remark']= isset($value['remark']) ? $value['remark'] : "";
- $temp['addtime']= date("Y-m-d H:i:s");
- $temp['updatetime']= date("Y-m-d H:i:s");
- $total+= $value['total_fee'];
- $fund_fee+= $goocp['apay_fee'];
- $winv_fee+= $goocp['winv_fee'];
- $inv_fee+= $goocp['ainv_fee'];
- $wfund_fee+= $goocp['wpay_fee'];
- $orderinfo[]=$temp;
- }
- if(!empty($invoice) && $invoice!=''){
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if($inv_add==1){
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- }else{
- Db::name('customer_invoice')->where('invoice_code',"=",$invoice['invoice_code'])->save($invoice);
- }
- }
- $orderins = Db::name("order_info")->insertAll($orderinfo);
- //if($total<0){
- // Db::rollback();
- // return error_show(1005,"发票核销数据为不能为负值");
- // }
- if($orderins) {
- $order = [
- "orderNo" => $orderNo,
- "companyNo" => $companyNo,
- "sales_id" => $effetc['data']['user']['id'],
- "sales_name" => $username,
- "total_amount" => $total,
- "inv_status" => 1,
- "fund_status" => 1,
- "fund_fee"=>0,
- "wfund_fee"=>$total,
- "inv_fee"=>0,
- "inv_used"=>$total,
- "winv_fee"=>0,
- "status" => 0,
- "type" => $type,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $orderpool = Db::name("order_pool")->insert($order, true);
- if ($orderpool > 0) {
- $invNo = makeNo("INV");
- $inv = [
- "invNo" => $invNo,
- "inv_value" => $total,
- "inv_out" => $inv_out,
- "inv_in" => $companyNo,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "status" => 0,
- "worder_fee"=>0,
- "order_used"=>$total,
- "wtrade_fee"=>$total,
- "remark" => $remark,
- "invoice_title" => $invoice['invoice_title'],
- "invoice_addr" => $invoice['invoice_addr'],
- "invoice_mobile" => $invoice['invoice_mobile'],
- "invoice_code" => $invoice['invoice_code'],
- "invoice_bank" => $invoice['invoice_bank'],
- "invoice_bankNo" => $invoice['invoice_bankNo'],
- "inv_add" => $inv_add,
- "is_order" =>1,
- "inv_type"=>$inv_type,
- "type_check"=>$type,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $invinsert = Db::name("invoice_pool")->insert($inv);
- if ($invinsert) {
- if($type==2){
- Db::commit();
- return app_show(0,"发票新建成功");
- }
- $assocNo = makeNo("ASC");
- $asslog = [
- "assocNo"=>$assocNo,
- "type"=>1,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "companyNo"=>$companyNo,
- "codeNo"=>$orderNo,
- "rela_form"=>1,
- "total_fee"=>$total,
- "balance"=>0,
- "cancel_total"=>$total,
- "status"=>1,
- "source"=>2,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $logassc = Db::name("assoc_key")->insert($asslog);
- if($logassc){
- $assrela =[
- "assocNo"=>$assocNo,
- "viceNo"=>$invNo,
- "vice_total"=>$total,
- "vice_fee"=>0,
- "cancel_fee"=>$total,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $relas = Db::name("assoc_rela")->insert($assrela);
- if($relas){
- $resula= $this->updateQrd($orderNo,$total);
- if($resula){
- Db::commit();
- return app_show(0,"发票新建申请成功");
- }
- }
- }
- }
- }
- }
- Db::rollback();
- return error_show(1004,"发票新建申请失败");
- }catch (\Exception $e) {
- Db::rollback();
- return error_show(1004, $e->getMessage());
- }
- }
- /**
- * 保存新建的资源
- * @param \think\Request $request
- * @return \think\Response
- */
- public function save()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
- $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $type = isset($post['type'])&&$post['type']!=""?intval($post['type']):1;
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if($invNo==""){
- return error_show(1004,"参数invNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $invinfo =Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->find();
- if(empty($invinfo)){
- return error_show(1004,"发票信息未找到");
- }
- if($invinfo['status']>=3){
- return error_show(1004,"发票信息已通过财务审核,无法修改!");
- }
- $good = isset($post['good'])&&is_array($post['good'])?$post['good']:"";
- if($good==""&& empty($good)){
- return error_show(1004,"商品信息不能为空");
- }
- $assc = Db::name("assoc_list")->where([["viceNo","=",$invNo],["is_del","=",0],["type","=",1]])->find();
- if(empty($assc)){
- return error_show(1005,"未找到对应的关联数据");
- }
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- $total=0;
- $fund_fee=0;
- $wfund_fee=0;
- $inv_fee=0;
- $winv_fee=0;
- Db::startTrans();
- try{
- if(!empty($invoice) && $invoice!='') {
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if ($inv_add == 1) {
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- } else {
- Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- }
- }
- // $reset = $this->qrd_delet($assc['codeNo'],$assc['cancel_total']);
- // if($reset){
- // Db::name("order_info")->where("orderNo","=",$assc['codeNo'])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- // }else{
- // Db::rollback();
- // return error_show(1004,"确认单数据编辑失败");
- // }
- foreach ($good as $value){
- // $temp['goodNo']= $value['sequenceNo'];
- // $temp['orderNo']= $assc['codeNo'];
- //$goocp = Db::name("qrd_list")->where("sequenceNo","=",$value['sequenceNo'])->find();
- $goocp = Db::name("order_info")->where([["orderNo","=",$assc['codeNo']],["goodNo","=",$value['sequenceNo']]])
- ->find();
- // $goocp['good_name']= $value['remark'];
- // $temp['qrdNo'] = $goocp['qrdNo'];
- // $temp['brand']= $goocp['brand'];
- // $temp['unit']= $goocp['unit'];
- // $temp['good_num']= $value['num'];
- // $temp['price']= $goocp['price'];
- // $temp['total_fee']= $value['total_fee'];
- // $temp['wfund_fee'] = $value['total_fee'];
- // $temp['winv_fee'] = $value['total_fee'];
- // $temp['other_fee']= 0;
- // $temp['status']=1;
- $goocp['remark']= isset($value['remark']) ? $value['remark'] : "";
- $goocp['updatetime']= date("Y-m-d H:i:s");
- // $temp['addtime']= date("Y-m-d H:i:s");
- // $fund_fee+= $goocp['apay_fee'];
- // $winv_fee+= $goocp['winv_fee'];
- // $inv_fee+= $goocp['ainv_fee'];
- // $wfund_fee+= $goocp['wpay_fee'];
- Db::name("order_info")->save($goocp);
- $total+= $value['total_fee'];
- }
- $order = [
- "companyNo" => $companyNo,
- "total_amount" => $total,
- "fund_fee"=>0,
- "wfund_fee"=>$total,
- "inv_fee"=>0,
- "inv_used"=>$total,
- "winv_fee"=>0,
- "updatetime" => date("Y-m-d H:i:s")
- ];
- // if($total<0){
- // Db::rollback();
- // return error_show(1005,"发票核销数据为不能为负值");
- // }
- $orderpool = Db::name("order_pool")->where("orderNo","=",$assc['codeNo'])->save($order);
- if ($orderpool) {
- $invinfo['inv_value'] = $total;
- $invinfo['inv_in'] = $companyNo;
- $invinfo['inv_out'] = $inv_out;
- $invinfo['worder_fee'] = 0;
- $invinfo['inv_type'] = $inv_type;
- $invinfo['type_check'] = $type;
- $invinfo['wtrade_fee'] = $total;
- $invinfo['order_used'] = $total;
- $invinfo['invoice_title'] = $invoice['invoice_title'];
- $invinfo['invoice_addr'] = $invoice['invoice_addr'];
- $invinfo['invoice_mobile'] = $invoice['invoice_mobile'];
- $invinfo['invoice_code'] = $invoice['invoice_code'];
- $invinfo['invoice_bank'] = $invoice['invoice_bank'];
- $invinfo['invoice_bankNo'] = $invoice['invoice_bankNo'];
- $invinfo['inv_add'] = $inv_add;
- $invinfo['remark']=$remark;
- $invinfo['updatetime'] =date("Y-m-d H:i:s");
- $invinsert = Db::name("invoice_pool")->save($invinfo);
- if($invinsert){
- $asslog = [
- "cancel_fee" => $total,
- "vice_fee" => 0,
- "vice_total" => 0,
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $logassc = Db::name("assoc_rela")->where("id","=", $assc['id'])->save($asslog);
- if ($logassc) {
- $keyas=[
- "total_fee"=>$total,
- "balance"=>0,
- "cancel_total"=>$total,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $relas = Db::name("assoc_key")->where("id","=",$assc['aid'])->save($keyas);
- if($relas){
- // $resula= $this->updateQrd($assc['codeNo'],$total);
- // if($resula){
- Db::commit();
- return app_show(0,"发票编辑成功");
- // }
- }
- }
- }
- }
- Db::rollback();
- return error_show(1004,"发票编辑失败");
- }catch (\Exception $e) {
- Db::rollback();
- return error_show(1004, $e->getMessage());
- }
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function CreateByTrade(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- $traNo = isset($post['traNo'])&&$post['traNo']!=""?$post['traNo']:"";
- if($traNo==""){
- return error_show(1004,"参数traNo 不能为空");
- }
- $trade = Db::name("trade_pool")->where([["traNo","in",$traNo],["is_del","=",0],['status',"=",3]])->select()
- ->toArray();
- if(empty($trade)){
- return error_show(1004,"未找到资金信息");
- }
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- Db::startTrans();
- try{
- if(!empty($invoice) && $invoice!='') {
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if ($inv_add == 1) {
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- } else {
- Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- }
- }
- $total=0;
- $relas=[];
- $assocNo = makeNo("ASC");
- $invNo = makeNo("INV");
- foreach ($trade as $value){
- if($value['winv_fee']<=0){
- continue;
- }
- $relas[] =[
- "assocNo"=>$assocNo,
- "viceNo"=>$value['traNo'],
- "vice_total"=>$value['total_fee'],
- "vice_fee"=>0,
- "cancel_fee"=>$value['winv_fee'],
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $total+=$value['winv_fee'];
- $value['inv_used']+=$value['winv_fee'];
- $value['winv_fee']=0;
- $value['updatetime']=date("Y-m-d H:i:s");
- Db::name("trade_pool")->save($value);
- }
- $inv = [
- "invNo" => $invNo,
- "inv_value" => $total,
- "inv_out" => $inv_out,
- "inv_in" => $companyNo,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "status" => 0,
- "worder_fee"=>$total,
- "order_used"=>0,
- "fund_used"=>$total,
- "wtrade_fee"=>0,
- "remark" => $remark,
- "invoice_title" => $invoice['invoice_title'],
- "invoice_addr" => $invoice['invoice_addr'],
- "invoice_mobile" => $invoice['invoice_mobile'],
- "invoice_code" => $invoice['invoice_code'],
- "invoice_bank" => $invoice['invoice_bank'],
- "invoice_bankNo" => $invoice['invoice_bankNo'],
- "inv_add" => $inv_add,
- "is_order" =>0,
- "inv_type"=>$inv_type,
- "type_check"=>3,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $invinsert = Db::name("invoice_pool")->insert($inv);
- if ($invinsert) {
- $asslog = [
- "assocNo"=>$assocNo,
- "type"=>2,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "companyNo"=>$companyNo,
- "codeNo"=>$invNo,
- "rela_form"=>4,
- "total_fee"=>$total,
- "balance"=>0,
- "cancel_total"=>$total,
- "status"=>1,
- "source"=>2,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $logassc = Db::name("assoc_key")->insert($asslog);
- if($logassc){
- $relas = Db::name("assoc_rela")->insertAll($relas);
- if($relas){
- Db::commit();
- return app_show(0,"发票新建申请成功");
- }
- }
- }
- Db::rollback();
- return error_show(1005,"发票新建申请失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function EditByTrain(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if($invNo==""){
- return error_show(1004,"参数invNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- $invinfo =Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->find();
- if(empty($invinfo)){
- return error_show(1004,"发票信息未找到");
- }
- if($invinfo['status']>=3){
- return error_show(1004,"发票信息已通过财务审核,无法修改!");
- }
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- Db::startTrans();
- try{
- if(!empty($invoice) && $invoice!='') {
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if ($inv_add == 1) {
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- } else {
- Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- }
- }
- $invpool=[
- "invoice_title" => $invoice['invoice_title'],
- "invoice_addr" => $invoice['invoice_addr'],
- "invoice_mobile" => $invoice['invoice_mobile'],
- "invoice_code" => $invoice['invoice_code'],
- "invoice_bank" => $invoice['invoice_bank'],
- "invoice_bankNo" => $invoice['invoice_bankNo'],
- "inv_add" => $inv_add,
- "inv_out" => $inv_out,
- "remark"=>$remark,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $up= Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->save($invpool);
- if($up){
- Db::commit();
- return app_show(0,"更新成功");
- }else{
- Db::rollback();
- return error_show(1005,"更新失败");
- }
- }catch(\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function read()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invid = isset($post['id'])&&$post['id']!=""?intval($post['id']):"";
- if($invid==""){
- return error_show(1004,"参数id 不能为空");
- }
- $inv = Db::name("invoice_pool")->where("id","=",$invid)->find();
- if(empty($inv)){
- return error_show(1005,"未找到发票信息");
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$inv['inv_in'])->find();
- $inv['customer']=empty($customer)? []:$customer;
- $kp_company = Db::name("company_info")->where("companyNo","=",$inv['inv_out'])->find();
- $inv['kp_company']=empty($kp_company)? []:$kp_company;
- $inv['order']=[];
- $assoc = Db::name("assoc_list")->where([["type","=",1],['viceNo',"=",$inv["invNo"]],["is_del","=",0]])
- ->find();
- if(!empty($assoc)){
- $order= Db::name("order_pool")->where([['orderNo',"=",$assoc['codeNo']]])->find();
- $list=[];
- if($order['type']==1){
- $good = Db::name('order_info')->where([['orderNo',"=",$assoc['codeNo']],["status","=",1]])->select();
- foreach ($good as $key=>$value){
- $info = Db::name("qrd_list")->where("sequenceNo","=",$value['goodNo'])->find();
- $value['num'] = intval($info['num']);
- $value['ainv_fee'] =sprintf("%.2f", $info['ainv_fee']);
- $value['winv_fee'] = sprintf("%.2f",$info['winv_fee']);
- $value['total'] = sprintf("%.2f",$info['total_fee']);
- $value['inv_num'] = intval($info['inv_num']);
- $list[]=$value;
- }
- }
- $order['good'] = $list;
- $inv['order']=$order;
- }
- $inv['trade']=[];
- $assoc= Db::name("assoc_list")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$inv["invNo"]]])->select()->toArray();
- if(!empty($assoc)) {
- foreach ($assoc as $value) {
- $temp = Db::name("trade_list")->alias("a")->leftJoin("cfp_customer_info b","a.companyNo=b.companyNo")
- ->where([["a.is_del", "=", 0], ["a.traNo", "=", $value["viceNo"]]])->field("a.*,b.companyName")
- ->find();
- $inv['trade'][]=$temp;
- }
- }
- return app_show(0,"获取成功",$inv);
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function add()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
- if($total==""){
- return error_show(1004,"参数total 不能为空");
- }
- //if($total<0){
- // Db::rollback();
- // return error_show(1005,"发票核销数据为不能为负值");
- // }
- Db::startTrans();
- try{
- if(!empty($invoice) && $invoice!='') {
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if ($inv_add == 1) {
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- } else {
- Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- }
- }
- $invNo = makeNo("INV");
- $inv = [
- "invNo" => $invNo,
- "inv_value" => $total,
- "inv_out" => $inv_out,
- "inv_in" => $companyNo,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "status" => 0,
- "trade_status" => 1,
- "order_status" => 1,
- "worder_fee"=>$total,
- "wtrade_fee"=>$total,
- "invoice_title" => $invoice['invoice_title'],
- "invoice_addr" => $invoice['invoice_addr'],
- "invoice_mobile" => $invoice['invoice_mobile'],
- "invoice_code" => $invoice['invoice_code'],
- "invoice_bank" => $invoice['invoice_bank'],
- "invoice_bankNo" => $invoice['invoice_bankNo'],
- "inv_add" => $inv_add,
- "inv_type"=>$inv_type,
- "type_check"=>2,
- "is_order"=>0,
- "remark"=>$remark,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $invinsert = Db::name("invoice_pool")->insert($inv);
- if($invinsert){
- Db::commit();
- return app_show(0,"发票创建成功");
- }
- Db::rollback();
- return error_show(1004,"发票新建失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function edit()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- // $companyNo = isset($post['tradeNo'])&&$post['tradeNo']!=""?trim($post['tradeNo']):"";
- $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
- $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if($invNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
- if($inv_out==""){
- return error_show(1004,"参数inv_out 不能为空");
- }
- $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
- if(empty($out)){
- return error_show(1004,"未找到开票企业信息");
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $invinfo =Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
- if(empty($invinfo)){
- return error_show(1004,"发票信息未找到");
- }
- if($invinfo['status']>=3){
- return error_show(1004,"发票信息已通过财务审核,无法修改!");
- }
- $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
- if($total==""){
- return error_show(1004,"参数total 不能为空");
- }
- //if($total<0){
- // Db::rollback();
- // return error_show(1005,"发票核销数据为不能为负值");
- // }
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $customer['invoice_addr'] = $post['invoice_addr']:"";
- $invoice=[];
- isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- $post['invoice_bankNo']:"";
- isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- $post['invoice_mobile']:"";
- isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- $invinfo['inv_value']=$total;
- $invinfo['worder_fee']=$total;
- $invinfo['wtrade_fee']=$total;
- $invinfo['inv_in']=$companyNo;
- $invinfo['inv_out']=$inv_out;
- $invinfo['invoice_title'] = $invoice['invoice_title'];
- $invinfo['invoice_addr'] = $invoice['invoice_addr'];
- $invinfo['invoice_mobile'] = $invoice['invoice_mobile'];
- $invinfo['invoice_code'] = $invoice['invoice_code'];
- $invinfo['invoice_bank'] = $invoice['invoice_bank'];
- $invinfo['invoice_bankNo'] = $invoice['invoice_bankNo'];
- $invinfo['inv_type']=$inv_type;
- $invinfo['inv_add']=$inv_add;
- $invinfo['remark']=$remark;
- $invinfo['updatetime']= date("Y-m-d H:i:s");
- Db::startTrans();
- try{
- if(!empty($invoice) && $invoice!='') {
- $invoice['updatetime'] = date("Y-m-d H:i:s");
- $invoice['checkNo'] = $companyNo;
- if ($inv_add == 1) {
- $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- if($cust){
- Db::rollback();
- return error_show(1005,"企业财务信息已存在");
- }
- $invoice['addtime'] = date("Y-m-d H:i:s");
- Db::name('customer_invoice')->insert($invoice);
- } else {
- Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- }
- }
- $invinsert = Db::name("invoice_pool")->save($invinfo);
- if($invinsert){
- Db::commit();
- return app_show(0,"发票编辑成功");
- }
- Db::rollback();
- return error_show(1004,"发票编辑失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function status( )
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invNo=isset($post['invNo'])&& $post['invNo']!=""? trim($post['invNo']):"";
- if($invNo==""){
- return error_show(1004,"参数invNo 不能为空");
- }
- $invinfo = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
- if(empty($invinfo)){
- return error_show(1003,"未找到发票数据");
- }
- $status = isset($post['status'])&&$post['status']!==""? intval($post['status']):"";
- $remark = isset($post['remark'])&&$post['remark']!==""? trim($post['remark']):"";
- if($status===""){
- return error_show(1005,"参数status 不能为空");
- }
- if($status==$invinfo['status']){
- return error_show(1005,"状态已修改");
- }
- if(!in_array($status,[0,1,2,3,4,5])){
- return error_show(1005,"参数status 无效");
- }
- if($status===0&&$invinfo['status']>=3){
- return error_show(1005,"发票已通过审核无法取消申请!");
- }
- // if($status===1&&$invinfo['status']>=3){
- // return error_show(1005,"发票已通过审核无法取消申请!");
- // }
- Db::startTrans();
- if($status==4){
- $invlist = isset($post['inv_info'])&&$post['inv_info']!="" ? $post['inv_info'] :"";
- if($invlist==""){
- Db::rollback();
- return error_show(1004,"参数inv_info 不能为空");
- }
- }
- $invinfo['status']=$status;
- $invinfo['exam_remark']=$remark;
- $invinfo['updatetime']=date("Y-m-d H:i:s");
- $update =Db::name("invoice_pool")->save($invinfo);
- if($update){
- if($status==5) {
- if ($invinfo['type_check'] == 1) {
- $assinfo = Db::name("assoc_list")->where([["viceNo", "=", $invNo], ["status", "=", 1], ["source",
- "=", 2], ["is_del", "=", "0"]])->find();
- $assoc = Db::name("assoc_key")->where("assocNo", "=", $assinfo['assocNo'])->save
- (["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
- if ($assoc) {
- $invinfo['aorder_fee'] += $assinfo['cancel_fee'];
- $invinfo['order_used'] -= $assinfo['cancel_fee'];
- $invinfo['is_order'] = 1;
- $invinfo['order_status'] = $invinfo['worder_fee'] == 0 ? 3 : 2;
- Db::name("invoice_pool")->save($invinfo);
- $orderpool = Db::name("order_pool")->where([["orderNo", "=", $assinfo['codeNo']],
- ["status", "=", 0], ["is_del", "=", 0]])->find();
- $orderpool['status'] = 1;
- $orderpool['inv_fee'] += $assinfo['cancel_total'];
- $orderpool['inv_used'] -= $assinfo['cancel_total'];
- $orderpool['inv_status'] = $orderpool['winv_fee'] == 0 ? 3 : 2;
- $orderpool['updatetime'] = date("Y-m-d H:i:s");
- $pool = Db::name("order_pool")->save($orderpool);
- if ($pool) {
- $qrd = $this->qrd_sure($assinfo['codeNo'], $assinfo['cancel_fee']);
- if ($qrd) {
- Db::commit();
- return app_show(0, "状态修改成功");
- }
- }
- }
- Db::rollback();
- return error_show(1005, "状态修改失败");
- }
- if ($invinfo['type_check'] == 3) {
- $assoc = Db::name("assoc_list")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->select()->toArray();
- if (!empty($assoc)) {
- $taotal = 0;
- foreach ($assoc as $value) {
- $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value["viceNo"]]])->find();
- $temp['inv_fee'] += $value['cancel_fee'];
- $temp['inv_used']-= $value['cancel_fee'];
- $temp['inv_status']= $temp['winv_fee']==0 ? 3 :2;
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $taotal = $value['cancel_total'];
- Db::name("trade_pool")->save($temp);
- }
- $invinfo['atrade_fee'] += $taotal;
- $invinfo['fund_used'] -= $taotal;
- $invinfo['trade_status'] = $invinfo['wtrade_fee'] == 0 ? 3 : 2;
- Db::name("invoice_pool")->save($invinfo);
- $ass_up = Db::name("assoc_key")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->save(["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
- if ($ass_up) {
- Db::commit();
- return app_show(0, "状态修改成功");
- }
- }
- Db::rollback();
- return error_show(1005, "状态修改失败");
- }
- }
- Db::commit();
- return app_show(0,"状态修改成功");
- }else{
- Db::rollback();
- return error_show(1005,"状态修改失败");
- }
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invNo =isset($post['invNo']) && $post['invNo']!="" ? trim($post['invNo']):"";
- if($invNo==""){
- return error_show(1003,"参数invNo 不能为空");
- }
- $inv = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
- if(empty($inv)){
- return error_show(1004,"未找到发票数据");
- }
- if($inv['status']>2){
- return error_show(1004,"财务审核已通过无法取消");
- }
- Db::startTrans();
- if($inv['type_check']==1){
- $assoc= Db::name("assoc_list")->where([["type","=",1],["is_del","=",0],["rela_form","=",1],["viceNo","=",
- $invNo]])
- ->find();
- if(!empty($assoc)) {
- $update = Db::name("order_pool")->where('orderNo', "=", $assoc['codeNo'])->save(['is_del' => 1,
- "updatetime" => date("Y-m-d H:i:s")]);
- if (!$update) {
- Db::rollback();
- return error_show(1004, "订单删除失败");
- }
- $ass_up = Db::name("assoc_key")->where("id", "=", $assoc['aid'])->save(["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")]);
- if (!$ass_up) {
- Db::rollback();
- return error_show(1004, "订单发票关联表删除失败");
- }
- $del= $this->qrd_delet($assoc['codeNo'],$assoc['cancel_total']);
- if(!$del){
- Db::rollback();
- return error_show(1006,"确认单数据修改失败");
- }
- }
- }
- if($inv['type_check']==3){
- $assoc= Db::name("assoc_list")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$invNo]])->select()->toArray();
- if(!empty($assoc)) {
- foreach ($assoc as $value){
- $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=",$value["viceNo"]]])->find();
- $temp['winv_fee'] += $value['cancel_fee'];
- $temp['inv_used'] -= $value['cancel_fee'];
- $temp['updatetime'] = date("Y-m-d H:i:s");
- Db::name("trade_pool")->save($temp);
- }
- $ass_up = Db::name("assoc_key")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$invNo]])->save(["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")]);
- if (!$ass_up) {
- Db::rollback();
- return error_show(1004, "订单发票关联表删除失败");
- }
- }
- }
- $inv['is_del']=1;
- $inv['updatetime']=date("Y-m-d H:i:s");
- $update = Db::name("invoice_pool")->save($inv);
- if($update){
- Db::commit();
- return app_show(0,"取消申请成功");
- }else{
- Db::rollback();
- return error_show(1003,"取消申请失败");
- }
- // return $update? app_show(0,"取消申请成功"):error_show(1003,"取消申请失败");
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function InvoPost(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invNo=isset($post['invNo'])&& $post['invNo']!=""? trim($post['invNo']):"";
- if($invNo==""){
- return error_show(1004,"参数invNo 不能为空");
- }
- $invinfo = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
- if(empty($invinfo)){
- return error_show(1003,"未找到发票数据");
- }
- $postcode = isset($post["code"])&&$post['code']!="" ? $post["code"] :"";
- $postname = isset($post['company']) && $post['company']!="" ? $post['company']:"";
- if ($postcode==""){
- return error_show(1005,"参数code 不能为空");
- }
- if ($postname==""){
- return error_show(1005,"参数company 不能为空");
- }
- // if($invinfo['status']!=4){
- // return error_show(1005,"发票数据还未上传");
- // }
- $data=[
- "postNo"=>$postcode,
- "post_company"=>$postname,
- "invNo"=>$invNo,
- "contactor"=>"",
- "mobile"=>"",
- "status"=>1,
- "addtime"=>date("y-m-d H:i:s"),
- "updatetime"=>date("y-m-d H:i:s"),
- ];
- Db::startTrans();
- $post= Db::name("inv_post")->insert($data);
- if($post){
- $invinfo['status']=5;
- $invinfo["updatetime"]=date("Y-m-d H:i:s");
- $sav= Db::name("invoice_pool")->save($invinfo);
- if($sav){
- if ($invinfo['type_check'] == 1) {
- $assinfo = Db::name("assoc_list")->where([["viceNo", "=", $invNo], ["status", "=", 1], ["source",
- "=", 2], ["is_del", "=", "0"]])->find();
- $assoc = Db::name("assoc_key")->where("assocNo", "=", $assinfo['assocNo'])->save
- (["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
- if ($assoc) {
- $invinfo['aorder_fee'] += $assinfo['cancel_fee'];
- $invinfo['order_used'] -= $assinfo['cancel_fee'];
- $invinfo['is_order'] = 1;
- $invinfo['order_status'] = $invinfo['worder_fee'] == 0 ? 3 : 2;
- Db::name("invoice_pool")->save($invinfo);
- $orderpool = Db::name("order_pool")->where([["orderNo", "=", $assinfo['codeNo']],
- ["status", "=", 0], ["is_del", "=", 0]])->find();
- $orderpool['status'] = 1;
- $orderpool['inv_fee'] += $assinfo['cancel_total'];
- $orderpool['inv_used'] -= $assinfo['cancel_total'];
- $orderpool['inv_status'] = $orderpool['winv_fee'] == 0 ? 3 : 2;
- $orderpool['updatetime'] = date("Y-m-d H:i:s");
- $pool = Db::name("order_pool")->save($orderpool);
- if ($pool) {
- $qrd = $this->qrd_sure($assinfo['codeNo'], $assinfo['cancel_fee']);
- if ($qrd) {
- Db::commit();
- return app_show(0, "状态修改成功");
- }
- }
- }
- Db::rollback();
- return error_show(1005, "状态修改失败");
- }
- if ($invinfo['type_check'] == 3) {
- $assoc = Db::name("assoc_list")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->select()->toArray();
- if (!empty($assoc)) {
- $taotal = 0;
- foreach ($assoc as $value) {
- $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value["viceNo"]]])->find();
- $temp['inv_fee'] += $value['cancel_fee'];
- $temp['inv_used']-= $value['cancel_fee'];
- $temp['inv_status']= $temp['winv_fee']==0 ? 3 :2;
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $taotal = $value['cancel_total'];
- Db::name("trade_pool")->save($temp);
- }
- $invinfo['atrade_fee'] += $taotal;
- $invinfo['fund_used'] -= $taotal;
- $invinfo['trade_status'] = $invinfo['wtrade_fee'] == 0 ? 3 : 2;
- Db::name("invoice_pool")->save($invinfo);
- $ass_up = Db::name("assoc_key")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->save(["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
- if ($ass_up) {
- Db::commit();
- return app_show(0, "状态修改成功");
- }
- }
- Db::rollback();
- return error_show(1005, "状态修改失败");
- }
- Db::commit();
- return app_show(0,"添加成功");
- }else{
- Db::rollback();
- return error_show(1003,"添加失败");
- }
- }
- Db::rollback();
- return error_show(1003,"添加失败");
- }
- /**
- * @return \think\response\Json|void
- */
- public function GetReceipt(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invNo=isset($post['inv_in'])&& $post['inv_in']!=""? trim($post['inv_in']):"";
- if($invNo==""){
- return error_show(1004,"参数inv_in 不能为空");
- }
- $type = isset($post['type_check'])&& $post['type_check']!=""? trim($post['type_check']) : 2;
- $condition = [["inv_in","=",$invNo],["type_check","=",$type],["lstatus","=",1]];
- $list = Db::name("inv_list")->where($condition)->select();
- return app_show(0,"获取成功",$list);
- }
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function recepitlist()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $condition = [['is_del',"=",0],['refund_status',"=",0]];
- $guserinfo =GetUserInfo($token);
- if(isset($guserinfo['code']) && $guserinfo['code']!=0){
- return error_show($guserinfo['code'],$guserinfo['message']);
- }
- $check = checkRole($guserinfo['data']['roleid'],55);
- if($check){
- $condition[]=["apply_id","=", $guserinfo['data']['id']];
- }
- $no = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
- if ($no != "") {
- $condition[] = ["inv_in", "=", "{$no}"];
- }
- $invNo = isset($post['invNo']) && $post['invNo'] != "" ? trim($post['invNo']) : "";
- if ($invNo != "") {
- $condition[] = ["invNo", "like", "%{$invNo}%"];
- }
- $type_check = isset($post['type_check']) && $post['type_check'] != "" ? $post['type_check'] : "";
- if ($type_check != "") {
- $condition[] = ["type_check", "=", "{$type_check}"];
- }
- $status = isset($post['status']) && $post['status'] != "" ? $post['status'] : "";
- if ($status != "") {
- $condition[] = ["status", "=", "{$status}"];
- }
- $order_status = isset($post['order_status']) && $post['order_status'] != "" ? intval($post['order_status']) :
- "";
- if ($order_status != "") {
- if($order_status==4){
- $condition[] = ["order_status", "in", [1,2]];
- }else{
- $condition[] = ["order_status", "=", "{$order_status}"];
- }
- // $condition[] = ["order_status", "=", "{$order_status}"];
- }
- $trade_status = isset($post['trade_status']) && $post['trade_status'] != "" ? intval($post['trade_status']) : "";
- if ($trade_status != "") {
- if($trade_status==4){
- $condition[] = ["trade_status", "in", [1,2]];
- }else{
- $condition[] = ["trade_status", "=", "{$trade_status}"];
- }
- }
- $total_low = isset($post['total_low']) && $post['total_low'] != "" ? $post['total_low'] : "";
- if ($total_low != "") {
- $condition[] = ["inv_value", ">=", "{$total_low}"];
- }
- $total_up = isset($post['total_up']) && $post['total_up'] != "" ? $post['total_up'] : "";
- if ($total_up != "") {
- $condition[] = ["inv_value", "<=", "{$total_up}"];
- }
- $starttime = isset($post['starttime']) && $post['starttime'] != "" ? $post['starttime'] : "";
- if ($starttime != "") {
- $condition[] = ["addtime", ">=", "{$starttime}"];
- }
- $endtime = isset($post['endtime']) && $post['endtime'] != "" ? $post['endtime'] : "";
- if ($endtime != "") {
- $condition[] = ["addtime", "<=", "{$endtime}"];
- }
- $type = isset($post['type_check'])&& $post['type_check']!=""? $post['type_check'] : "";
- if ($type != "") {
- $condition[] = ["type_check", "=", "{$type}"];
- }
- $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
- $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
- $count = Db::name("invoice_pool")->where($condition)->count();
- $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
- $page = $page >= $total ? intval($total) : $page;
- $list = Db::name("invoice_pool")->where($condition)->order("addtime desc")->page(intval($page), $size)->select();
- $data=[];
- foreach ($list as $value){
- $customer= Db::name("customer_info")->where("companyNo","=",$value['inv_in'])->find();
- $value['company'] = isset($customer['companyName']) ? $customer['companyName']:"";
- $value['contactor'] = isset($customer['contactor']) ? $customer['contactor']:"";
- // $value['invoice_bankNo'] = isset($customer['invoice_bankNo']) ? $customer['invoice_bankNo']:"";
- $value['mobile'] = isset($customer['mobile']) ? $customer['mobile']:"";
- $out = Db::name('company_info')->where('companyNo',"=",$value['inv_out'])->find();
- $value['kp_company'] =isset($out['company_name']) ? $out['company_name']:"";
- $data[]=$value;
- }
- return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
- }
- /**
- * @param $orderNo
- * @param $balance
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function updateQrd($orderNo,$balance)
- {
- $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->find();
- if (empty($orderinfo)) {
- return false;
- }
- $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
- if (empty($orderlist)) {
- return false;
- }
- foreach ($orderlist as $value) {
- if($balance==0){
- break;
- }
- if($value['winv_fee'] == 0){
- continue;
- }
- $order_fee=0;
- if($value['winv_fee']<=$balance){
- $order_fee = $value['winv_fee'];
- $balance -=$value['winv_fee'];
- $value['inv_fee'] += $value['winv_fee'];
- $value['winv_fee'] = 0;
- }else{
- $value['inv_fee'] += $balance;
- $value['winv_fee']-= $balance;
- $order_fee = $balance;
- $balance=0;
- }
- $value['updatetime'] = date("Y-m-d H:i:s");
- $orde = Db::name("order_info")->save($value);
- if($orde===false){
- return false;
- }
- $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
- if (($order_fee <= $qrdinfo['Number1618249205231'])||($order_fee <=
- $qrdinfo['Number1618249202608']&&$orderinfo['type']==4)) {
- $qrdinfo['inving_fee'] += $order_fee;
- if($orderinfo['type']==4){
- $qrdinfo['Number1618249202608'] -= $order_fee;
- }else{
- $qrdinfo['Number1618249205231'] -= $order_fee;
- $qrdinfo['number1618249258956'] += $value['good_num'];
- }
- $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
- $qinfo = Db::name("qrd_info")->save($qrdinfo);
- if ($qinfo) {
- $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
- if(($qrdp['winv_fee'] >= $order_fee)|| ($qrdp['ainv_fee'] >= $order_fee &&$orderinfo['type']==4
- )) {
- if($orderinfo['type']==4){
- $qrdp['ainv_fee'] -= $order_fee;
- $qrdp['inv_fee'] += $order_fee;
- }else{
- $qrdp['winv_fee'] -= $order_fee;
- $qrdp['inv_fee'] += $order_fee;
- }
- // $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
- $qrd = Db::name("qrd")->save($qrdp);
- if(!$qrd){
- return false;
- }
- }else {
- return false;
- }
- }else {
- return false;
- }
- } else {
- return false;
- }
- }
- return true;
- }
- /**
- * @param $orderNo
- * @param $balance
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function qrd_sure($orderNo,$balance){
- $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->find();
- if (empty($orderinfo)) {
- return false;
- }
- $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
- if (empty($orderlist)) {
- return false;
- }
- foreach ($orderlist as $value) {
- if($balance==0){
- break;
- }
- if($value['inv_fee'] == 0){
- continue;
- }
- $order_fee=0;
- if($value['inv_fee']<=$balance){
- $order_fee = $value['inv_fee'];
- $balance -=$value['inv_fee'];
- $value['ainv_fee'] += $value['inv_fee'];
- $value['inv_fee'] = 0;
- }else{
- $value['ainv_fee'] += $balance;
- $value['inv_fee']-= $balance;
- $order_fee = $balance;
- $balance=0;
- }
- $value['updatetime'] = date("Y-m-d H:i:s");
- $orde = Db::name("order_info")->save($value);
- if($orde===false){
- return false;
- }
- $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
- if (($order_fee <= $qrdinfo['inving_fee'])||($order_fee <= $qrdinfo['inving_fee']&&$orderinfo['type']==4
- )) {
- if($orderinfo['type']==4 ){
- $qrdinfo['inving_fee'] -= $order_fee;
- //$qrdinfo['number1618249149738'] -= $order_fee;
- $qrdinfo['Number1618249205231'] += $order_fee;
- //$qrdinfo['number1618249258956'] += $value['good_num'];
- $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618249202608']>0 ? "2" : "1";
- }else{
- $qrdinfo['inving_fee'] -= $order_fee;
- //$qrdinfo['number1618249149738'] -= $order_fee;
- $qrdinfo['Number1618249202608'] += $order_fee;
- //$qrdinfo['number1618249258956'] += $value['good_num'];
- $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618249205231']>0 ? "2" : "3";
- }
- $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
- $qinfo = Db::name("qrd_info")->save($qrdinfo);
- if ($qinfo) {
- $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
- if ($qrdp['inv_fee'] >= $order_fee || ($qrdp['inv_fee'] >= $order_fee &&$orderinfo['type']==4
- )) {
- if($orderinfo['type']==4){
- $qrdp['inv_fee'] -= $order_fee;
- $qrdp['winv_fee'] += $order_fee;
- $qrdp['inv_status'] = $qrdp['ainv_fee'] == 0 ? 1 : 2;
- }else{
- $qrdp['inv_fee'] -= $order_fee;
- $qrdp['ainv_fee'] += $order_fee;
- $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
- }
- $qrd = Db::name("qrd")->save($qrdp);
- if(!$qrd){
- return false;
- }
- }else {
- return false;
- }
- }else {
- return false;
- }
- } else {
- return false;
- }
- }
- return true;
- }
- /**
- * @param $orderNo
- * @param $balance
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function qrd_delet($orderNo,$balance){
- $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 1]])->find();
- if (empty($orderinfo)) {
- return false;
- }
- $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
- if (empty($orderlist)) {
- return false;
- }
- foreach ($orderlist as $value) {
- if($balance==0){
- break;
- }
- if($value['inv_fee'] == 0){
- continue;
- }
- $order_fee=0;
- if($value['inv_fee']<=$balance){
- $order_fee = $value['inv_fee'];
- $balance -=$value['inv_fee'];
- $value['winv_fee'] += $value['inv_fee'];
- $value['inv_fee'] = 0;
- }else{
- $value['inv_fee'] -= $balance;
- $value['winv_fee']+= $balance;
- $order_fee = $balance;
- $balance=0;
- }
- $value['updatetime'] = date("Y-m-d H:i:s");
- $orde = Db::name("order_info")->save($value);
- if(!$orde){
- return false;
- }
- $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
- if ($order_fee <= $qrdinfo['inving_fee']) {
- $qrdinfo['inving_fee'] -= $order_fee;
- if($orderinfo['type']==4){
- $qrdinfo['Number1618249202608'] += $order_fee;
- // $qrdinfo['number1618249258956'] += $value['good_num'];
- }else{
- $qrdinfo['Number1618249205231'] += $order_fee;
- $qrdinfo['number1618249258956'] -= $value['good_num'];
- }
- $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
- // $qrdinfo['ShortText1618559043560'] = $value['total_fee'] < $qrdinfo['number1618249149738'] ? "2" : "3";
- $qinfo = Db::name("qrd_info")->save($qrdinfo);
- if ($qinfo) {
- $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
- if ($qrdp['inv_fee'] >= $order_fee) {
- if($orderinfo['type']==4){
- $qrdp['ainv_fee'] += $order_fee;
- }else{
- $qrdp['winv_fee'] += $order_fee;
- }
- $qrdp['inv_fee'] -= $order_fee;
- // $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
- $qrd = Db::name("qrd")->save($qrdp);
- if(!$qrd){
- return false;
- }
- }else {
- return false;
- }
- }else {
- return false;
- }
- } else {
- return false;
- }
- }
- return true;
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function DownReceipt(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $condition=[["type_check","=",1],["is_del","=",0],["status","=",3]];
- $inid = isset($post['invid'])&& $post['invid']!="" ? $post['invid'] :"";
- if($inid!=""){
- $condition[]=['id',"in",$inid];
- }
- $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
- if (empty($invice)){
- return error_show(1003,"未找到数据");
- }
- $data=[];
- foreach ($invice as $value){
- $temp=[];
- $orderNo = Db::name("assoc_list")->where([["rela_form","=",1],["is_del","=",0],['viceNo',"=",$value['invNo']]])->find();
- if(!$orderNo){
- continue;
- }
- $good = Db::name("order_info")->where([['orderNo',"=",$orderNo['codeNo']],["status","=",1]])->select()->toArray();
- if(empty($good)){
- continue;
- }
- if(isset($value['inv_out'])&& $value['inv_out']!=''){
- $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
- }
- foreach ($good as $val){
- $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
- $temp['invNo'] = $value['invNo'];
- $temp['qrdNo'] = $val['qrdNo'];
- $temp['depart'] = $qrd['bumen'];
- $temp['apply_name'] =$value['apply_name'];
- $temp['code'] ="";
- $temp['invoice_title'] =$value['invoice_title'];
- $temp['invoice_code'] =$value['invoice_code']." ";
- $temp['invoice_addr'] =$value['invoice_addr'];
- $temp['invoice_mobile'] =$value['invoice_mobile'];
- $temp['invoice_bank'] =$value['invoice_bank'];
- $temp['invoice_bankNo'] =$value['invoice_bankNo']." ";
- $temp['company'] =$qrd['kehu'];
- $temp['goodname'] =$qrd['goodname'];
- $temp['tax'] =$qrd['taxPoint'];
- $temp['inv_name'] =$val['remark'];
- $temp['diff_weight'] =$qrd['diff_weight'];
- $temp['diff_price'] =$qrd['diff_price'];
- $temp['unit'] =$val['unit'];
- $temp['num'] =$val['good_num'];
- $temp['price'] =$val['price'];
- $temp['total_fee'] =$val['total_fee'];
- switch ($value['inv_type']){
- case 1:
- $temp['inv_type'] = "增值税专用发票";
- break;
- case 2:
- $temp['inv_type'] = "增值税普通发票";
- break;
- case 3:
- $temp['inv_type'] = "增值税电子专用发票";
- break;
- case 4:
- $temp['inv_type'] = "增值税电子普通发票";
- break;
- default:
- $temp['inv_type'] = "-";
- break;
- }
- // $temp['inv_type'] = $value['inv_type']==1?"增值税专用发票": "增值税普通发票";
- // $temp['inv_value'] =$value['inv_value'];
- $temp['remark'] =$value['remark'];
- $temp['kp_companyName'] = isset($invout['company_name']) ? $invout['company_name']:"";
- $temp['kp_company_address'] = isset($invout['company_address']) ? $invout['company_address']:"";
- $temp['kp_company_license'] = isset($invout['company_license']) ? $invout['company_license'].' ':"";
- $temp['kp_bank_name'] = isset($invout['bank_name']) ? $invout['bank_name']:"";
- $temp['kp_bankNo'] = isset($invout['bankNo']) ? $invout['bankNo'].' ':"";
- $temp['kp_contector'] = isset($invout['contector']) ? $invout['contector']:"";
- $temp['kp_mobile'] = isset($invout['mobile']) ? $invout['mobile'].' ':"";
- $data[]=$temp;
- }
- }
- if (empty($data)){
- return error_show(1003,"未找到数据");
- }
- $header=[
- "申请编号",
- "公司订单号",
- "申请部门(所属平台)",
- "申请人",
- "代码",
- "发票开具全称",
- "发票开具企业纳税识别号",
- "发票开具企业地址",
- "发票开具企业地址联系方式",
- "发票开具企业收款银行",
- "发票开具企业收款账户",
- "分公司",
- "采购商品名",
- "税率",
- "发票商品名",
- "工差重量",
- "工差金额",
- "单位",
- "数量",
- "单价",
- "总价",
- "开票种类",
- "备注说明",
- '开票企业名称',
- '开票企业地址',
- '开票企业纳税号',
- '开票企业银行名称',
- '开票企业银行账户',
- '开票企业法人',
- '开票企业联系方式'
- ];
- $li = excelExport("待开票",$header,$data);
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function ImportReceipt(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invlist = isset($post['data'])&& $post['data']!="" ? $post['data'] :"";
- if($invlist==""){
- return error_show(1005, '参数data 不能为空');
- }
- $list=[];
- $total =[];
- foreach ($invlist as $key=>$value){
- if($value['invNo']==""){
- return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
- }
- if($value["inv_type"]===""){
- return error_show(1005,"第".($key+1)."行缺少发票类型数据");
- }
- if($value["inv_code"]==""){
- return error_show(1005,"第".($key+1)."行缺少发票代码数据");
- }
- if($value["inv_number"]==""){
- return error_show(1005,"第".($key+1)."行缺少发票号码数据");
- }
- if($value["inv_total"]==""){
- return error_show(1005,"第".($key+1)."行缺少发票金额数据");
- }
- if($value["createtime"]==""){
- return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
- }
- !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
- // $inv=Db::name("invoice_list")->where([["inv_code","=",$value['inv_code']],["inv_number","=",$value['inv_number']],["is_del","=",0]])->find();
- // if($inv){
- // return error_show(1005,"第".($key+1)."行发票已存在");
- // }
- $temp=[];
- $temp['invNo'] = $value["invNo"];
- $temp['inv_type'] = $value['inv_type'];
- $temp['inv_code'] = $value['inv_code'];
- $temp['inv_number'] = $value['inv_number'];
- $temp['inv_total'] = $value['inv_total'];
- $temp['createtime'] = $value['createtime'];
- $temp['status'] = 1;
- $temp['is_del'] = 0;
- $temp['addtime'] = date("Y-m-d H:i:s");
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $total[$value["invNo"]]['total']+=$value['inv_total'];
- $list[]=$temp;
- }
- Db::startTrans();
- try{
- foreach ($total as $key=>$value){
- $inpool = Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->find();
- $invtotal=Db::name("invoice_list")->where([["invNo","=",$key],["is_del","=",0]])->sum('inv_total');
- if($inpool['inv_value']!=$value['total']+$invtotal){
- Db::rollback();
- if($inpool['inv_value']>$value['total']+$invtotal){
- return error_show(1005,"发票上传数据{$key}不足票面总额");
- }else{
- return error_show(1005,"发票上传数据{$key}超出票面总额");
- }
- }
- $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save(["status"=>4,"updatetime"=>date("Y-m-d H:i:s")]);
- if(!$up){
- Db::rollback();
- return error_show(1005,"发票状态更新失败");
- }
- }
- $insert = Db::name("invoice_list")->insertAll($list);
- if(!$insert){
- Db::rollback();
- return error_show(1005,"发票数据录入失败");
- }else{
- Db::commit();
- return app_show(0,"发票数据录入成功");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- public function RefundAdd()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
- $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
- // $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
- $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- $userinfo = GetUserInfo($token);
- $username="";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- if(isset($userinfo['code'])&&$userinfo['code']==0){
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1004,"未找到客户企业信息");
- }
- $invoice=[];
- // isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
- // isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
- // isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
- // $post['invoice_bankNo']:"";
- // isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
- // isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
- // $post['invoice_mobile']:"";
- // isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
- // $post['invoice_title']:"";
- // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
- // return error_show(1004,"企业信息联系电话格式错误");
- // }
- $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
- if($total==""){
- return error_show(1004,"参数total 不能为空");
- }
- Db::startTrans();
- try{
- // if(!empty($invoice) && $invoice!='') {
- // $invoice['updatetime'] = date("Y-m-d H:i:s");
- // $invoice['checkNo'] = $companyNo;
- // if ($inv_add == 1) {
- // $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
- // if($cust){
- // Db::rollback();
- // return error_show(1005,"企业财务信息已存在");
- // }
- // $invoice['addtime'] = date("Y-m-d H:i:s");
- // Db::name('customer_invoice')->insert($invoice);
- // } else {
- // Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
- // }
- // }
- $invNo = makeNo("INV");
- $inv = [
- "invNo" => $invNo,
- "inv_value" => $total,
- "inv_out" => "",
- "inv_in" => $companyNo,
- "apply_id" =>$effetc['data']['user']['id'],
- "apply_name" => $username,
- "status" => 0,
- "trade_status" => 1,
- "order_status" => 1,
- "worder_fee"=>$total,
- "wtrade_fee"=>$total,
- "invoice_title" => "",
- "invoice_addr" => "",
- "invoice_mobile" => "",
- "invoice_code" => "",
- "invoice_bank" => "",
- "invoice_bankNo" => "",
- "inv_add" => 0,
- "inv_type"=>$inv_type,
- "type_check"=>5,
- "is_order"=>0,
- "remark"=>$remark,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $invinsert = Db::name("invoice_pool")->insert($inv);
- if($invinsert){
- Db::commit();
- return app_show(0,"发票创建成功");
- }
- Db::rollback();
- return error_show(1004,"发票新建失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function SaveRefundInv(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $invNo = isset($post['invNo']) && $post['invNo']!="" ? $post['invNo']:"";
- if($invNo==""){
- return error_show(1004,"参数invNo 不能为空");
- }
- $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:"";
- if($companyNo==""){
- return error_show(1004,"参数companyNo 不能为空");
- }
- $invfo = Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->find();
- if(empty($invfo)){
- return error_show(1005,"未找到发票数据");
- }
- // $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:"";
- // if($total_fee==""){
- // return error_show(1004,"参数total_fee 不能为空");
- // }
- // if($trade['balance']<$total_fee){
- // return error_show(1005,"资金余额不足");
- // }
- $refundid = isset($post['refundid']) && $post['refundid']!="" ? $post['refundid'] :"";
- if($refundid==""){
- return error_show(1006,"参数refundid 不能为空");
- }
- $refund =Db::name("refund")->where([["is_del","=",0],['refund_status',"=",3],['id',"in",$refundid]])->select()->toArray();
- if(empty($refund)){
- return error_show(1006,"退款资金数据未找到");
- }
- $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
- if(empty($customer)){
- return error_show(1005,"未找到用户数据");
- }
- $refund_fee = array_sum(array_column($refund,"refund_fee"));
- if($refund_fee!=$invfo['inv_value']){
- return error_show(1005,"发票额度和退票额度不符");
- }
- // if($refund_fee<$trade['balance']){
- // return error_show(1005,"资金退款余额不足");
- // }
- Db::startTrans();
- try{
- $invfo['inv_in'] = $companyNo;
- $invfo['updatetime'] = date("Y-m-d H:i:s");
- $tra = Db::name("invoice_pool")->save($invfo);
- if($tra){
- foreach ($refund as $value){
- $value['backNo']= $invfo['invNo'];
- $value['back_fee']= $value['refund_fee'];
- $value['refund_status']= 4;
- $value['updatetime']= date("Y-m-d H:i:s");
- $ref=Db::name("refund")->save($value);
- if($ref){
- $tadpool = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",
- $value['relaNo']],['refund_status',"=",2]])->save(["refund_status"=>3,
- "updatetime"=>date("Y-m-d H:i:s")]);
- if(!$tadpool){
- Db::rollback();
- return error_show(1007,"资金退款状态更新失败");
- }
- }else{
- Db::rollback();
- return error_show(1007,"资金退款状态更新失败");
- }
- }
- Db::commit();
- return app_show(0,"欠款资金认领成功");
- }
- Db::rollback();
- return error_show(1007,"资金退款认领失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1006,$e->getMessage());
- }
- }
- /**
- * 退票关联订单
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function InvRefundCreate(){
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $guserinfo =GetUserInfo($token);
- if(isset($guserinfo['code']) && $guserinfo['code']!=0){
- return error_show($guserinfo['code'],$guserinfo['message']);
- }
- $invNo = isset($post['invNo'])&&$post['invNo']!="" ? trim($post['invNo']) : "";
- if($invNo==""){
- return error_show(1005,"参数invNo 不能为空");
- }
- $invinfo = Db::name("invoice_pool")->where([['invNo',"=",$invNo],['is_del',"=",0]])->find();
- if(empty($invinfo)){
- return error_show(1005,"未找到发票数据");
- }
- if($invinfo['type_check']!=5){
- return error_show(1005,"数据不是退票数据");
- }
- $type = isset($post['type']) && $post['type']!=="" ? $post['type'] :1;
- $trade = isset($post['trade'])&& $post['trade']!=="" ? $post['trade'] :"";
- $good = isset($post['good'])&& $post['good']!=="" ? $post['good'] :"";
- if($type==1 &&$trade==""){
- return error_show(1005,"参数trade 不能为空");
- }
- if($type!=1 &&$good==""){
- return error_show(1005,"参数good 不能为空");
- }
- $companyNo = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
- $userinfo = GetUserInfo($token);
- $username = "";
- if ($companyNo == "") {
- return error_show(1004, "参数companyNo 不能为空");
- }
- if (isset($userinfo['code']) && $userinfo['code'] == 0) {
- $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : "";
- }
- $customer = Db::name("customer_info")->where("companyNo", "=", $companyNo)->find();
- if (empty($customer)) {
- return error_show(1004, "未找到客户企业信息");
- }
- Db::startTrans();
- try{
- if($type!=1) {
- $orderinfo = [];
- $orderNo = makeNo('ORD');
- $total = 0;
- $fund_fee = 0;
- $wfund_fee = 0;
- $inv_fee = 0;
- $winv_fee = 0;
- foreach ($good as $value) {
- $temp = [];
- $temp['orderNo'] = $orderNo;
- $temp['goodNo'] = $value['goodNo'];
- $goocp = Db::name("qrd_list")->where("sequenceNo", "=", $value['goodNo'])->find();
- if (!$goocp) {
- continue;
- }
- $temp['good_name'] = $goocp['goodname'];
- $temp['qrdNo'] = $goocp['qrdNo'];
- $temp['brand'] = $goocp['brand'];
- $temp['unit'] = $goocp['unit'];
- $temp['good_num'] = $value['num'];
- $temp['price'] = $goocp['price'];
- $temp['total_fee'] = $value['total_fee'];
- $temp['wfund_fee'] = $value['total_fee'];
- $temp['winv_fee'] = $value['total_fee'];
- $temp['other_fee'] = 0;
- $temp['status'] = 1;
- $temp['remark'] = "";
- $temp['addtime'] = date("Y-m-d H:i:s");
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $total += $value['total_fee'];
- $fund_fee += $value['apay_fee'];
- $winv_fee += $value['winv_fee'];
- $inv_fee += $value['ainv_fee'];
- $wfund_fee += $value['wpay_fee'];
- $orderinfo[] = $temp;
- }
- if ($total != $invinfo['worder_fee']) {
- Db::rollback();
- return error_show(1004, "商品总额于发票面额不符");
- }
- $orderins = Db::name("order_info")->insertAll($orderinfo);
- if ($orderins) {
- $order = [
- "orderNo" => $orderNo,
- "companyNo" => $companyNo,
- "sales_id" => $effetc['data']['user']['id'],
- "sales_name" => $username,
- "total_amount" => $total,
- "inv_status" => 1,
- "fund_status" => 1,
- "fund_fee" => 0,
- "wfund_fee" => $total,
- "inv_fee" => 0,
- "winv_fee" => $total,
- "status" => 0,
- "type" => 3,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $orderpool = Db::name("order_pool")->insert($order, true);
- if ($orderpool > 0) {
- $rela = [];
- $assNo = makeNo("ASC");
- $cancel = 0;
- $balance = $invinfo["worder_fee"] > $total ? $total : $invinfo["worder_fee"];
- $total -= $balance;
- $sav = [
- "worder_fee" => $invinfo['worder_fee'] - $balance,
- "order_used" => $invinfo['order_used'] + $balance,
- "is_order" => 1,
- "updatetime" => date("Y-m-d H:i:s")
- ];
- Db::name("invoice_pool")->where([["invNo", "=", $invinfo['invNo']]])->save($sav);
- $rela = [
- "assocNo" => $assNo,
- "viceNo" => $orderNo,
- "vice_total" => $order['total_amount'],
- "vice_fee" => $total,
- "cancel_fee" => $balance,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $assoc = Db::name("assoc_rela")->insert($rela);
- if ($assoc) {
- $data = [
- "assocNo" => $assNo,
- "companyNo" => $companyNo,
- "type" => 1,
- "rela_form" => 1,
- "codeNo" => $invinfo["invNo"],
- "total_fee" => $invinfo['inv_value'],
- "balance" => $invinfo['worder_fee'] - $balance,
- "cancel_total" => $balance,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $realkey = Db::name("assoc_key")->insert($data);
- if ($realkey) {
- Db::name("invoice_pool")->where("invNo", "=", $invNo)->save(["wfund_fee" => $total, 'fund_used' => $balance, "updatetime" => date("Y-m-d H:i:s")]);
- $qrdup = $this->updateQrd($orderNo, $balance);
- if (!$qrdup) {
- Db::rollback();
- return error_show(1004, "新建关联申请失败");
- }
- Db::commit();
- return app_show(0, "新建关联成功");
- }
- }
- Db::rollback();
- return error_show(1004, "新建关联申请失败");
- }
- }
- Db::rollback();
- return error_show(1004, "新建关联申请失败");
- }else{
- $rela = [];
- $trade = 0;
- $assNo = makeNo("ASC");
- foreach ($trade as $value) {
- $balance = $invinfo['wtrade_fee'] > $value['cancel'] ? $value['cancel'] : $invinfo['wtrade_fee'];
- if ($invinfo['wtrade_fee'] <= 0) {
- Db::rollback();
- return error_show(1005, "资金总额大于发票额度");
- }
- if ($value['inv_fee'] <= 0) {
- Db::rollback();
- return error_show(1005, "发票核销金额为0");
- }
- $invinfo['wtrade_fee'] -= $balance;
- $invinfo['fund_used'] += $balance;
- $trade += $balance;
- $rela[] = [
- "assocNo" => $assNo,
- "viceNo" => $value['traNo'],
- "vice_total" => $value['total_fee'],
- "vice_fee" => $value['inv_fee'] - $balance,
- "cancel_fee" => $balance,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $value['inv_fee'] -= $balance;
- $value['inv_used'] += $balance;
- $value['updatetime'] = date("Y-m-d H:i:s");
- Db::name("trade_pool")->save($value);
- }
- $assoc = Db::name("assoc_rela")->insertAll($rela);
- if ($assoc) {
- $data = [
- "assocNo" => $assNo,
- "companyNo" => $companyNo,
- "type" => 2,
- "rela_form" => $type,
- "codeNo" => $invinfo["invNo"],
- "total_fee" => $invinfo['inv_value'],
- "balance" => $invinfo['wtrade_fee'],
- "cancel_total" => $trade,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $realkey = Db::name("assoc_key")->insert($data);
- if ($realkey) {
- $invinfo['updatetime'] = date("Y-m-d H:i:s");
- $order_up = Db::name("invoice_pool")->save($invinfo);
- Db::commit();
- return app_show(0, "新建关联成功");
- }
- }
- Db::rollback();
- return error_show(1004, "新建关联申请失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- }
|