OrderInv.php 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\InvoiceInterface;use app\admin\model\ReportCode;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Config;
  8. use think\facade\Db;
  9. use think\facade\Cache;
  10. use think\facade\Validate;
  11. class OrderInv extends BaseController{
  12. protected $typename;
  13. protected $invoiceType;
  14. protected $invName;
  15. protected $kingInvoice;
  16. public function __construct(App $app) {
  17. parent::__construct($app);
  18. $invoice =Config::get("invoiceType");
  19. $this->invoiceType = $invoice['invoiceName'];
  20. $this->invName = $invoice['invoiceType'];
  21. $this->typename = $invoice['KingName'];
  22. $this->kingInvoice = $invoice['KingInvoice'];
  23. }
  24. /**
  25. * 新建销售单开票申请
  26. */
  27. public function create(){
  28. $companyType = isset($this->post['companyType'])&&$this->post['companyType']!=='' ? trim($this->post['companyType']):"";
  29. if ($companyType==''){
  30. return error_show(1004,"参数 companyType 不能为空");
  31. }
  32. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  33. if($buy_id==''){
  34. return error_show(1004,"参数 buy_id 不能为空");
  35. }
  36. $buyinfo =Db::name("customer_invoice")->where(['id'=>$buy_id,"is_del"=>0])->find();
  37. if($buyinfo==false){
  38. return error_show(1004,"购买方发票信息未找到");
  39. }
  40. if($this->level==1){
  41. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  42. }
  43. else{
  44. $companyNo = isset($this->post['relaComNo'])&&$this->post['relaComNo']!=='' ? trim($this->post['relaComNo'])
  45. :"";
  46. }
  47. if($companyNo==''){
  48. return error_show(1004,"业务公司不能为空");
  49. }
  50. $company = Db::name("company_info")->where(["companyNo"=>$companyNo])->find();
  51. if($company==false){
  52. return error_show(1004,"销售方信息未找到");
  53. }
  54. // $supplier = Db::name("supplier_info")->where(["code"=>$companyNo])->find();
  55. // if($supplier==false){
  56. // return error_show(1004,"业务公司信息未找到");
  57. // }
  58. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? trim($this->post['invtype']):"";
  59. if($invtype==''){
  60. return error_show(1004,"参数 invtype 不能为空");
  61. }
  62. if(!in_array($invtype,$this->invName)) return error_show(1004,"发票类型有误");
  63. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  64. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  65. $exam_remark = isset($this->post['exam_remark'])&&$this->post['exam_remark']!=='' ? trim($this->post['exam_remark']):"";
  66. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  67. if(empty($orderArr)){
  68. return error_show(1004,"参数 orderArr 不能为空");
  69. }
  70. $temp =[];
  71. $good=[];
  72. $invNo=makeNo("INV");
  73. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  74. if($company['denomination']!=''&& $invfee>intval($company['denomination'])*10000){
  75. return error_show(1004,"开票面额不能超过{$company['denomination']}万元");
  76. }
  77. $platform = array_column($orderArr,'platform_type');
  78. if(count(array_unique($platform))!=1){
  79. return error_show(1004,"订单平台类型不一致");
  80. }
  81. Db::startTrans();
  82. try{
  83. foreach ($orderArr as $value){
  84. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  85. Db::rollback();
  86. return error_show(1004,"参数 sequenceNo 不能为空");
  87. }
  88. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  89. if($qrd['status']==2){
  90. Db::rollback();
  91. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  92. }
  93. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  94. Db::rollback();
  95. return error_show(1004,"参数 inv_fee 不能为空");
  96. }
  97. if($qrd['winv_fee']<$value['inv_fee']){
  98. Db::rollback();
  99. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  100. }
  101. $ainv = Db::name("invoice_pool")
  102. ->alias("a")
  103. ->leftJoin("invoice_good b", "a.invNo=b.invNo and b.is_del=0")
  104. ->where(["a.status" => [0, 1, 2, 3, 4], "b.orderCode" => $value['sequenceNo']])
  105. ->sum("b.goodNum");
  106. if($ainv+$value['num']>($qrd['goodNum']-$qrd['thNum'])) {
  107. Db::rollback();
  108. return error_show(1004,"确认单{$value['sequenceNo']}待开票数量不足");
  109. }
  110. $good_info = Db::name("good")->where(["spuCode"=>$qrd["goodNo"],"status"=>1])->findOrEmpty();
  111. if(empty($good_info)){
  112. Db::rollback();
  113. return error_show(1004,"确认单{$value['sequenceNo']}商品未确认开票类目");
  114. }
  115. $num = Db::name("assoc")
  116. ->alias("a")
  117. ->leftJoin("invoice_good b", "a.viceCode=b.invNo AND b.orderCode=a.orderCode")
  118. ->where(["a.status" => [1, 2], "a.is_del" => 0, "a.type" => 1, "a.orderCode" => $value['sequenceNo']])
  119. ->sum("b.goodNum");
  120. if($qrd['goodNum']-$qrd['thNum']-$num<$value['num']){
  121. Db::rollback();
  122. return error_show(1004,"确认单{$value['sequenceNo']}待开票数量不足");
  123. }
  124. $ascc=[
  125. "assocNo"=>makeNo("AS"),
  126. "apply_id"=>$this->uid,
  127. "apply_name"=>$this->uname,
  128. "type"=>1,
  129. "orderCode"=>$value['sequenceNo'],
  130. "customerNo"=>$qrd['customerNo'],
  131. "viceCode"=>$invNo,
  132. "order_total"=>$qrd['totalPrice'],
  133. "vice_total"=>$invfee,
  134. "cancel_fee"=>$value['inv_fee'],
  135. "status"=>1,
  136. "addtime"=>date("Y-m-d H:i:s"),
  137. "updatetime"=>date("Y-m-d H:i:s")
  138. ];
  139. $invoice_good=[
  140. "invNo"=>$invNo,
  141. "orderCode"=>$value['sequenceNo'],
  142. "goodNo"=>$qrd['goodNo'],
  143. "goodName"=>$good_info['inv_good_name']??$qrd['goodName'] ,
  144. "catName"=>$good_info['inv_cat_name']??$qrd['thirdCat'],
  145. "catNo"=>$good_info['inv_cat_code']??"",
  146. "goodPrice"=>round($value['inv_fee']/$value['num'],8),
  147. "unitName"=>$qrd['goodUnit'],
  148. "goodNum"=>$value['num'],
  149. "specInfo"=>$value['sequenceNo'],
  150. "totalPrice"=>$value['inv_fee'],
  151. "totalTax"=>$value['inv_fee'],
  152. "tax"=>$good_info['inv_tax']??$qrd['tax'],
  153. "is_discount"=>$good_info['is_discount']??0,
  154. "addTax"=>$good_info['addTax']??'',
  155. "inv_tag"=>$good_info['inv_tag']??"",
  156. "taxPrice"=>round($value['inv_fee']*($good_info['inv_tax']??$qrd['tax']),2),
  157. "addtime"=>date("Y-m-d H:i:s"),
  158. "updatetime"=>date("Y-m-d H:i:s")
  159. ];
  160. $update = [
  161. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  162. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  163. "inv_status"=>2,
  164. "status"=>1,
  165. "updatetime"=>date("Y-m-d H:i:s")
  166. ];
  167. $up = Db::name("qrd_info")->where($qrd)->update($update);
  168. if($up==false){
  169. Db::rollback();
  170. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  171. }
  172. $temp[]=$ascc;
  173. $good[]=$invoice_good;
  174. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  175. if($report)$report->setField("invNo",$invNo);
  176. }
  177. $associn = Db::name("assoc")->insertAll($temp);
  178. if($associn==0){
  179. Db::rollback();
  180. return error_show(1005,"确认单申请开票失败");
  181. }
  182. $goodin = Db::name("invoice_good")->insertAll($good);
  183. if($goodin==0){
  184. Db::rollback();
  185. return error_show(1005,"确认单申请开票失败");
  186. }
  187. $inv=[
  188. "invNo"=>$invNo,
  189. "inv_value"=>$invfee,
  190. "inv_out"=> $companyNo,
  191. "inv_company"=> $company['invoice_title']??"",
  192. "apply_id"=>$this->uid,
  193. "apply_name"=>$this->uname,
  194. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通 全电子票
  195. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  196. "platform_type"=>$platform[0]??0, //开票类型 金税开票 金税线下 纯线下
  197. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  198. "company_type"=> $buyinfo['company_type']??'01', //类型01 企业,02 机关事业单位,03 个人, 04 其他
  199. "remark"=>$remark, //申请备注
  200. "exam_remark"=>$exam_remark, //审核备注
  201. "ainv_fee"=>0,//已核销金额
  202. "winv_fee"=>$invfee,//未核销金额
  203. "status"=>1,// 带财务审核
  204. "is_del"=>0,
  205. "email"=>$email,
  206. "addtime"=>date("Y-m-d H:i:s"),
  207. "updatetime"=>date("Y-m-d H:i:s")
  208. ];
  209. $invin = Db::name("invoice_pool")->insert($inv);
  210. if($invin){
  211. $invinfo=[
  212. "buyer_title"=>$buyinfo['invoice_title'],
  213. "buyer_code"=>$buyinfo['invoice_code'],
  214. "buyer_addr"=>$buyinfo['invoice_addr'],
  215. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  216. "buyer_bank"=>$buyinfo['invoice_bank'],
  217. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  218. "seller_title"=>$company['company_name'],
  219. "seller_code"=>$company['company_license'],
  220. "seller_addr"=>$company['company_address'],
  221. "seller_mobile"=>$company['mobile'],
  222. "seller_bank"=>$company['bank_name'],
  223. "seller_bankNo"=>$company['bankNo'],
  224. "invNo"=>$invNo,
  225. "voider"=>$company['voider'],
  226. "payee"=>$company['payee'],
  227. "drawer"=>$company['drawer'],
  228. "reviewer"=>$company['reviewer'],
  229. "ownerPlace"=>$company['ownerPlace'],
  230. "addtime"=>date("Y-m-d H:i:s")
  231. ];
  232. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  233. if($poolinfo){
  234. Db::commit();
  235. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  236. }
  237. }
  238. Db::rollback();
  239. return error_show(1004,"确认单发票申请失败");
  240. }catch (\Exception $e){
  241. Db::rollback();
  242. return error_show(1004,$e->getMessage());
  243. }
  244. }
  245. // 1财务审核通过 2 金税开票中/验票中 3 开票成功 4 财务驳回 5 开票失败 6 发票退票/废弃 7取消申请
  246. // 1财务审核通过 2 待财务上传发票 3 金税开票中/验票中 4 开票成功 5 开票失败 6发票退票/废弃 7取消申请 8 财务驳回 9验票失败
  247. public function status(){
  248. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  249. if($invNo==""){
  250. return error_show(1004,"参数 invNo 不能为空");
  251. }
  252. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  253. if($invinfo==false){
  254. return error_show(1004,"发票申请数据未找到");
  255. }
  256. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  257. if($status===''){
  258. return error_show(1004,"参数 status 不能为空");
  259. }
  260. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  261. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  262. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  263. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  264. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  265. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  266. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  267. $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floatval($this->post['total_fee']):"0";
  268. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  269. $seller_id =isset($this->post['seller_id'])&&$this->post['seller_id']!=''?trim($this->post['seller_id']):"";
  270. $seller_title =isset($this->post['seller_title'])&&$this->post['seller_title']!=''?trim($this->post['seller_title']):"";
  271. $seller_addr =isset($this->post['seller_addr'])&&$this->post['seller_addr']!=''?trim($this->post['seller_addr']):"";
  272. $seller_mobile =isset($this->post['seller_mobile'])&&$this->post['seller_mobile']!=''?trim($this->post['seller_mobile']):"";
  273. $seller_bank =isset($this->post['seller_bank'])&&$this->post['seller_bank']!=''?trim($this->post['seller_bank']):"";
  274. $seller_bankNo =isset($this->post['seller_bankNo'])&&$this->post['seller_bankNo']!=''?trim($this->post['seller_bankNo']):"";
  275. $buyer_id =isset($this->post['buyer_id'])&&$this->post['buyer_id']!=''?trim($this->post['buyer_id']):"";
  276. $buyer_title =isset($this->post['buyer_title'])&&$this->post['buyer_title']!=''?trim($this->post['buyer_title']):"";
  277. $buyer_addr =isset($this->post['buyer_addr'])&&$this->post['buyer_addr']!=''?trim($this->post['buyer_addr']):"";
  278. $buyer_mobile =isset($this->post['buyer_mobile'])&&$this->post['buyer_mobile']!=''?trim($this->post['buyer_mobile']):"";
  279. $buyer_bank =isset($this->post['buyer_bank'])&&$this->post['buyer_bank']!=''?trim($this->post['buyer_bank']):"";
  280. $buyer_bankNo =isset($this->post['buyer_bankNo'])&&$this->post['buyer_bankNo']!=''?trim($this->post['buyer_bankNo']):"";
  281. $tax_fee =isset($this->post['tax_fee'])&&$this->post['tax_fee']!=''?trim($this->post['tax_fee']):"";
  282. $invimg =isset($this->post['invimg'])&&$this->post['invimg']!=''?trim($this->post['invimg']):"";
  283. $update=[
  284. "status"=> $status,
  285. "exam_remark"=>$remark,
  286. "updatetime" => date("Y-m-d H:i:s")
  287. ];
  288. if($status==3){
  289. $open_type = $invinfo['open_type'];
  290. if($open_type!=1){
  291. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  292. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  293. if(in_array($invinfo['inv_type'],['normal','roll',"toll",'electronic'])){
  294. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  295. }
  296. //全电子发票
  297. if(in_array($invinfo['inv_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  298. //全电发票传含税金额
  299. if($total_fee=='') return error_show(1004,"参数 total_fee 不能为空");
  300. }else{
  301. //全电发票无invCode
  302. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  303. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  304. }
  305. }
  306. }
  307. if($status==2){
  308. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  309. $update['open_type']=$open_type;
  310. if($open_type==1)$status=3;
  311. }
  312. if($status==7){
  313. if(!in_array($invinfo['status'],[1,2,3,5,8,9])) return error_show(1004,"开票申请状态有误");
  314. }
  315. $update['status']=$status;
  316. Db::startTrans();
  317. try{
  318. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  319. if($invup){
  320. if(($status==3&& $open_type!=1) || ($invinfo['inv_type']==5 &&$status==3)){
  321. $invpool=[
  322. "invNo"=>$invinfo['invNo'],
  323. "inv_type"=>$this->kingInvoice[$invinfo['inv_type']],
  324. "inv_code"=>$invCode,
  325. "inv_number"=>$invNum,
  326. "check_code"=>$checkCode,
  327. "inv_total"=>$total_fee,
  328. "inv_subtotal"=>$subtotal_fee,
  329. "open_date"=>$open_date,
  330. 'pdfstream'=>'',
  331. "item"=>'',
  332. "status"=>$invinfo['inv_type']==5?1:0,
  333. "addtime"=>date("Y-m-d H:i:s"),
  334. "updatetime"=>date("Y-m-d H:i:s")
  335. ];
  336. if($invinfo['inv_type']==5){
  337. $invpool['seller_id'] = $seller_id??"";
  338. $invpool['seller_title'] = $seller_title??"";
  339. $invpool['seller_addr'] = $seller_addr??"";
  340. $invpool['seller_mobile'] = $seller_mobile??"";
  341. $invpool['seller_bank'] = $seller_bank??"";
  342. $invpool['seller_bankNo'] = $seller_bankNo??"";
  343. $invpool['buyer_id'] = $buyer_id??"";
  344. $invpool['buyer_title'] = $buyer_title??"";
  345. $invpool['buyer_addr'] = $buyer_addr??"";
  346. $invpool['buyer_mobile'] = $buyer_mobile??"";
  347. $invpool['buyer_bank'] = $buyer_bank??"";
  348. $invpool['buyer_bankNo'] = $buyer_bankNo??"";
  349. $invpool['tax_fee'] = $tax_fee??"";
  350. $invpool['pdfstream'] = $invimg ??"";
  351. }
  352. $in = Db::name("invoice_ticket")->insert($invpool);
  353. if($in==false){
  354. Db::rollback();
  355. return error_show(1004,"发票信息添加失败");
  356. }
  357. }
  358. if($status==3 && $open_type==1){
  359. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  360. if(empty($invpoolinfo)){
  361. Db::rollback();
  362. return error_show(1004,"开票信息未找到");
  363. }
  364. $goodinfo =Db::name("invoice_good")
  365. ->where(["invNo"=>$invNo,"is_del"=>0])
  366. ->field("0 as FPHXZ,
  367. catNo as SPBM,
  368. id as ZXBM,
  369. is_discount as YHZCBS,
  370. if(inv_tag=0,'',inv_tag) as LSLBS,
  371. addTax as ZZSTSGL,
  372. goodName as XMMC,
  373. specInfo as GGXH,
  374. unitName as DW,
  375. goodNum as SPSL,
  376. goodPrice as DJ,
  377. totalPrice as JE,
  378. 1 HSBZ,
  379. tax as SL,
  380. taxPrice as SE")
  381. ->select()->toArray();
  382. if(empty($goodinfo)){
  383. Db::rollback();
  384. return error_show(1004,"开票商品信息未找到");
  385. }
  386. $data=[
  387. "invNo"=>$invNo,
  388. "KPLX"=>"0",
  389. "XHFSBH"=>$invpoolinfo['seller_code'],
  390. "XHFMC"=>$invpoolinfo['seller_title'],
  391. "XHFDZ"=>$invpoolinfo['seller_addr'],
  392. "XHFDH"=>$invpoolinfo['seller_mobile'],
  393. "XHFYH"=>$invpoolinfo['seller_bank'],
  394. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  395. "GMFBM"=>"",
  396. "GMFSBH"=>$invpoolinfo['buyer_code'],
  397. "GMFMC"=>$invpoolinfo['buyer_title'],
  398. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  399. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  400. "GMFYH"=>$invpoolinfo['buyer_bank'],
  401. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  402. "GMFLX"=>$invinfo['company_type'],
  403. "GMFSJH"=>"",
  404. "GMFDZYX"=>$invinfo['email'],
  405. "KPR"=>$invpoolinfo['drawer'],
  406. "SKR"=>$invpoolinfo['payee'],
  407. "FHR"=>$invpoolinfo['reviewer'],
  408. "QDBZ"=>"0",
  409. "JSHJ"=>$invinfo['inv_value'],
  410. "HJJE"=>"0",
  411. "HJSE"=>"0",
  412. "BZ"=>$invinfo['remark'],
  413. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  414. "FPLXDM"=>$this->kingInvoice[$invinfo['inv_type']],
  415. "DDQQPCH"=>"",
  416. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  417. "status"=>"0",
  418. "result"=>"",
  419. "addtime"=>date("Y-m-d H:i:s"),
  420. "updatetime"=>date("Y-m-d H:i:s")
  421. ];
  422. $invoice=Db::name("invoice_interface")->insert($data);
  423. if($invoice==false){
  424. Db::rollback();
  425. return error_show(1003,"开票信息生成失败");
  426. }
  427. }
  428. if( $update['status']==4){
  429. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  430. if(!empty($qrdArr)){
  431. foreach ($qrdArr as $value){
  432. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  433. if($qrdinfo==false){
  434. Db::rollback();
  435. return error_show(1003,"确认单信息未找到");
  436. }
  437. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  438. Db::rollback();
  439. return error_show(1003,"确认单信息开票金额不足");
  440. }
  441. $update =[
  442. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  443. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  444. "invtime"=>date("Y-m-d H:i:s"),
  445. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  446. "updatetime"=>date("Y-m-d H:i:s"),
  447. ];
  448. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  449. if($qrdup==false){
  450. Db::rollback();
  451. return error_show(1003,"确认单信息更新失败");
  452. }
  453. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  454. $assocup =Db::name("assoc")->where($value)->update($assoc);
  455. if($assocup==false){
  456. Db::rollback();
  457. return error_show(1003,"确认单关联信息更新失败");
  458. }
  459. }
  460. }
  461. }
  462. if($status==7||$status==8){
  463. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  464. if(!empty($qrdArr)){
  465. foreach ($qrdArr as $value){
  466. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  467. if($qrdinfo==false){
  468. Db::rollback();
  469. return error_show(1003,"确认单信息未找到");
  470. }
  471. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  472. Db::rollback();
  473. return error_show(1003,"确认单信息开票金额不足");
  474. }
  475. $update =[
  476. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  477. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  478. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  479. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0&&$qrdinfo['pay_status']==1 ?0 : 1,
  480. "updatetime"=>date("Y-m-d H:i:s"),
  481. ];
  482. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  483. if($qrdup==false){
  484. Db::rollback();
  485. return error_show(1003,"确认单信息更新失败");
  486. }
  487. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  488. $assocup =Db::name("assoc")->where($value)->update($assoc);
  489. if($assocup==false){
  490. Db::rollback();
  491. return error_show(1003,"确认单关联信息更新失败");
  492. }
  493. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  494. if($report)$report->rmField("invNo",$invinfo['invNo']);
  495. //将数量加回去
  496. Db::name('invoice_good')
  497. ->where([
  498. 'is_del' => 0,
  499. 'invNo' => $invNo,
  500. 'orderCode' => $value['orderCode']
  501. ])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  502. }
  503. }
  504. }
  505. Db::commit();
  506. return app_show(0,"审核成功");
  507. }
  508. Db::rollback();
  509. return error_show(1004,"审核失败");
  510. }catch (\Exception $e){
  511. Db::rollback();
  512. return error_show(1004,$e->getMessage());
  513. }
  514. }
  515. /**
  516. * 发票设置物流
  517. */
  518. public function SetPost(){
  519. // @param invNo postCompany postCode postFee;
  520. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  521. if($invNo==''){
  522. return error_show(1004,"参数invNo不能为空");
  523. }
  524. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  525. if($invinfo==false){
  526. return error_show(1004,"发票申请信息未找到");
  527. }
  528. if($invinfo['status']!=4) return error_show(1004,"发票审核未完成");
  529. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  530. if($post_company==''){
  531. return error_show(1004,"参数 post_company 不能为空");
  532. }
  533. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  534. if($post_code==''){
  535. return error_show(1004,"参数 post_code 不能为空");
  536. }
  537. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floatval($this->post['post_fee']):"0";
  538. Db::startTrans();
  539. try{
  540. $data=[
  541. "post_company"=>$post_company,
  542. "post_code"=>$post_code,
  543. "post_fee"=>$post_fee,
  544. "updatetime"=>date("Y-m-d H:i:s")
  545. ];
  546. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  547. if($up){
  548. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>1,"is_del"=>0])->column("id,orderCode,cancel_fee");
  549. if(!empty($qrdArr)){
  550. foreach ($qrdArr as $value){
  551. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  552. if($qrdinfo==false){
  553. Db::rollback();
  554. return error_show(1003,"确认单信息未找到");
  555. }
  556. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  557. Db::rollback();
  558. return error_show(1003,"确认单信息开票金额不足");
  559. }
  560. $update =[
  561. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  562. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  563. "invtime"=>date("Y-m-d H:i:s"),
  564. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  565. "updatetime"=>date("Y-m-d H:i:s"),
  566. ];
  567. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  568. if($qrdup==false){
  569. Db::rollback();
  570. return error_show(1003,"确认单信息更新失败");
  571. }
  572. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  573. $assocup =Db::name("assoc")->where($value)->update($assoc);
  574. if($assocup==false){
  575. Db::rollback();
  576. return error_show(1003,"确认单关联信息更新失败");
  577. }
  578. }
  579. }
  580. Db::commit();
  581. return app_show(0,"更新成功");
  582. }
  583. Db::rollback();
  584. return error_show(1003,"发票申请信息更新失败");
  585. }catch (\Exception $e){
  586. Db::rollback();
  587. return error_show(1004,$e->getMessage());
  588. }
  589. }
  590. /**
  591. * @return \think\response\Json|void
  592. * @throws \think\db\exception\DbException
  593. */
  594. public function batchCheck(){
  595. $param =$this->request->only(["invNos"=>[],"status"=>'',"open_type"=>0,"remark"=>''],"post");
  596. $valid=Validate::rule([
  597. "invNos|发票编号"=>"require|array",
  598. "status|审核状态"=>"require|number|in:2,8",
  599. "open_type|开票方式"=>"require|number|in:0,1,2,3",
  600. "remark|审核备注"=>"max:255",
  601. ]);
  602. if($valid->check($param)==false) return error_show(1004,$valid->getError());
  603. $list =Db::name("invoice_pool")->where(["invNo"=>$param['invNos'],"status"=>1,"is_del"=>0])->column("invNo,company_type,email,inv_value,inv_type,remark","invNo");
  604. if(empty($list)) return error_show(1004,"未找到有效数据");
  605. Db::startTrans();
  606. try {
  607. if($param['open_type']==1 && $param['status']==2){
  608. $param['status']=3;
  609. $interall=[];
  610. foreach ($list as $key=>$value){
  611. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$key])->findOrEmpty();
  612. if(empty($invpoolinfo)){
  613. Db::rollback();
  614. return error_show(1004,"开票信息未找到");
  615. }
  616. $goodinfo =Db::name("invoice_good")
  617. ->where(["invNo"=>$key,"is_del"=>0])
  618. ->field("0 as FPHXZ,
  619. catNo as SPBM,
  620. id as ZXBM,
  621. is_discount as YHZCBS,
  622. if(inv_tag=0,'',inv_tag) as LSLBS,
  623. addTax as ZZSTSGL,
  624. goodName as XMMC,
  625. specInfo as GGXH,
  626. unitName as DW,
  627. goodNum as SPSL,
  628. goodPrice as DJ,
  629. totalPrice as JE,
  630. 1 HSBZ,
  631. tax as SL,
  632. taxPrice as SE")
  633. ->select()->toArray();
  634. if(empty($goodinfo)){
  635. throw new \Exception("开票商品信息未找到");
  636. }
  637. $data=[
  638. "invNo"=>$key,
  639. "KPLX"=>"0",
  640. "XHFSBH"=>$invpoolinfo['seller_code'],
  641. "XHFMC"=>$invpoolinfo['seller_title'],
  642. "XHFDZ"=>$invpoolinfo['seller_addr'],
  643. "XHFDH"=>$invpoolinfo['seller_mobile'],
  644. "XHFYH"=>$invpoolinfo['seller_bank'],
  645. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  646. "GMFBM"=>"",
  647. "GMFSBH"=>$invpoolinfo['buyer_code'],
  648. "GMFMC"=>$invpoolinfo['buyer_title'],
  649. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  650. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  651. "GMFYH"=>$invpoolinfo['buyer_bank'],
  652. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  653. "GMFLX"=>$value['company_type'],
  654. "GMFSJH"=>"",
  655. "GMFDZYX"=>$value['email'],
  656. "KPR"=>$invpoolinfo['drawer'],
  657. "SKR"=>$invpoolinfo['payee'],
  658. "FHR"=>$invpoolinfo['reviewer'],
  659. "QDBZ"=>"0",
  660. "JSHJ"=>$value['inv_value'],
  661. "HJJE"=>"0",
  662. "HJSE"=>"0",
  663. "BZ"=>$value['remark'],
  664. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  665. "FPLXDM"=>$this->kingInvoice[$value['inv_type']],
  666. "DDQQPCH"=>"",
  667. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  668. "status"=>"0",
  669. "result"=>"",
  670. "addtime"=>date("Y-m-d H:i:s"),
  671. "updatetime"=>date("Y-m-d H:i:s")
  672. ];
  673. $interall[]=$data;
  674. }
  675. $invoice=Db::name("invoice_interface")->insertAll($interall);
  676. if($invoice==false){
  677. throw new \Exception("开票信息生成失败");
  678. }
  679. }
  680. $up =Db::name("invoice_pool")
  681. ->where(["invNo"=>array_keys($list),"status"=>1,"is_del"=>0])
  682. ->update([
  683. "status"=>$param['status'],
  684. "open_type"=>$param['open_type'],
  685. "exam_remark"=>$param['remark'],
  686. "updatetime"=>date("Y-m-d H:i:s")
  687. ]);
  688. if($up==false) throw new \Exception("数据批量审核失败");
  689. Db::commit();
  690. return app_show(0,"数据审核成功");
  691. }catch (\Exception $e){
  692. Db::rollback();
  693. return error_show(1004,$e->getMessage());
  694. }
  695. }
  696. /**
  697. * 发票废弃使用
  698. */
  699. public function Discard(){
  700. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  701. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  702. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  703. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  704. if(!in_array($invinfo['status'],[1,2,3,4,5,8]))return error_show(1004,"发票状态无法废弃");
  705. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  706. if($company==false)return error_show(1004,"开票业务公司未找到");
  707. $invoiceinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  708. if($invoiceinfo==false)return error_show(1004,"发票详细信息未找到");
  709. $invoiceConf=Config::get('invoice');
  710. if(!isset($invoiceConf[$company['company_license']]))return error_show(1004,'金税配置不存在');
  711. $invoice = $invoiceConf[$company['company_license']];
  712. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  713. Db::startTrans();
  714. try{
  715. $result=$tax->DeprecateInvoices($company['company_license'],$this->kingInvoice[$invoiceinfo['inv_type']],$invoiceinfo['inv_code'],$invoiceinfo['inv_number'],1);
  716. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  717. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  718. if($up){
  719. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  720. if(!empty($qrdArr)){
  721. foreach ($qrdArr as $value){
  722. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  723. if($qrdinfo==false){
  724. Db::rollback();
  725. return error_show(1003,"确认单信息未找到");
  726. }
  727. if ($invinfo['status']==3){
  728. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  729. Db::rollback();
  730. return error_show(1003,"确认单信息开票金额不足");
  731. }
  732. $update =[
  733. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  734. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  735. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  736. "updatetime"=>date("Y-m-d H:i:s"),
  737. ];
  738. }else{
  739. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  740. Db::rollback();
  741. return error_show(1003,"确认单信息开票金额不足");
  742. }
  743. $update =[
  744. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  745. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  746. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  747. "updatetime"=>date("Y-m-d H:i:s"),
  748. ];
  749. }
  750. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  751. if($qrdup==false){
  752. Db::rollback();
  753. return error_show(1003,"确认单信息更新失败");
  754. }
  755. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  756. $assocup =Db::name("assoc")->where($value)->update($assoc);
  757. if($assocup==false){
  758. Db::rollback();
  759. return error_show(1003,"确认单关联信息更新失败");
  760. }
  761. }
  762. }
  763. Db::commit();
  764. return app_show(0,"发票废弃成功");
  765. }
  766. }
  767. Db::rollback();
  768. return error_show(1004,"发票废弃失败");
  769. }catch (\Exception $e){
  770. Db::rollback();
  771. return error_show(1004,$e->getMessage());
  772. }
  773. }
  774. /**@param invNo return_reason remark 退票申请
  775. * @return \think\response\Json|void
  776. */
  777. public function ReturnAdd(){
  778. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  779. if($invNo==""){
  780. return error_show(1004,"参数 invNo 不能为空");
  781. }
  782. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  783. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  784. if($return_reason==""){
  785. return error_show(1004,"参数 return_reason 不能为空");
  786. }
  787. if($remark==""){
  788. return error_show(1004,"参数 remark 不能为空");
  789. }
  790. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  791. if($invinfo==false){
  792. return error_show(1004,"发票申请数据未找到");
  793. }
  794. if($invinfo['status']!=4){
  795. return error_show(1004,"发票申请未完成");
  796. }
  797. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  798. if(empty($qrd)){
  799. return error_show(1004,"未找到开票的销售单信息");
  800. }
  801. $isT= Db::name("invoice_return")->where(["invNo"=>$invNo,"status"=>[0,1,2,5]])->findOrEmpty();
  802. if(!empty($isT)) return error_show(1004,"发票退回流程已存在");
  803. $returnCode = makeNo("RIN");
  804. $data=[
  805. "returnCode"=>$returnCode,
  806. "invNo"=>$invNo,
  807. "reason"=>$return_reason,
  808. "remark"=>$remark,
  809. "status"=>0,
  810. "apply_id"=>$this->uid,
  811. "apply_name"=>$this->uname,
  812. "addtime"=>date("Y-m-d H:i:s"),
  813. "updatetime"=>date("Y-m-d H:i:s")
  814. ];
  815. Db::startTrans();
  816. try{
  817. $inter = Db::name("invoice_return")->insert($data);
  818. if($inter){
  819. foreach ($qrd as $value){
  820. $report=ReportCode::where(["qrdNo"=>$value])->find();
  821. if($report)$report->setField("returnInv",$returnCode);
  822. }
  823. Db::commit();
  824. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  825. }else{
  826. Db::rollback();
  827. return error_show(1005,"退票申请新建失败");
  828. }
  829. }catch (\Exception $e){
  830. Db::rollback();
  831. return error_show(1006,$e->getMessage());
  832. }
  833. }
  834. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败 5 退票进行中
  835. // 红冲说明 1100000000 已抵扣 1010000000未抵扣 0000000100尚未寄出
  836. public function ReturnStatus(){
  837. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  838. if($returnCode==""){
  839. return error_show(1005,"参数 returnCode 不能为空");
  840. }
  841. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  842. if ($status==""){
  843. return error_show(1005,"参数 status 不能为空");
  844. }
  845. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  846. $return_reason =isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"2";
  847. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  848. if($return==false){
  849. return error_show(1005,"退票申请信息未找到");
  850. }
  851. $red_desc =isset($this->post['red_desc'])&&$this->post['red_desc']!="" ? trim($this->post['red_desc']):"0000000100";
  852. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  853. if($invinfo==false){
  854. return error_show(1004,"发票申请数据未找到");
  855. }
  856. if($invinfo['status']!=4){
  857. return error_show(1004,"发票申请未完成");
  858. }
  859. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  860. if(empty($qrd)){
  861. return error_show(1004,"未找到开票的销售单信息");
  862. }
  863. $ticket = Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"type"=>0,"status"=>1,"is_del"=>0])
  864. ->findOrEmpty();
  865. if(empty($ticket)){
  866. return error_show(1004,"未找到开票的票面信息");
  867. }
  868. $ticket['inv_out'] = $invinfo['inv_out']??"";
  869. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):0;
  870. if($status==1){
  871. if ($return_type==""){
  872. return error_show(1005,"参数 return_type 不能为空");
  873. }
  874. if ($red_desc==""){
  875. return error_show(1005,"参数 red_desc 不能为空");
  876. }
  877. if($return_type==2){
  878. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  879. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  880. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  881. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  882. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  883. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  884. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  885. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  886. if(in_array($invinfo['inv_type'],['normal','roll',"toll",'electronic'])){
  887. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  888. }
  889. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  890. }
  891. }
  892. //电子普票 只能红冲 不能作废
  893. //电子专票 只能红冲 不能作废
  894. //纸质专票 当月可以作废 次月红冲
  895. //纸质普票 当月作废 次月红冲
  896. //专票 红冲先做红字申请
  897. Db::startTrans();
  898. try{
  899. $isoutmonth =date("Y-m",strtotime($ticket["open_date"]))==date("Y-m")?0:1;
  900. $update=[
  901. "status"=>$status,
  902. "remark"=>$remark,
  903. "updatetime"=>date("Y-m-d H:i:s")
  904. ];
  905. if($status==1) {
  906. $update['is_out_month']=$isoutmonth;
  907. $update['red_desc']=$red_desc;
  908. $update['return_reason']=$return_reason;
  909. $update['return_type']=$return_type;
  910. }
  911. $up =Db::name("invoice_return")->where($return)->update($update);
  912. if($up){
  913. if($status==1){
  914. if($return_type==1){
  915. //普票
  916. if(in_array($ticket['inv_type'],['007','026'])) $result= $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'],$returnCode,$red_desc);
  917. //专票
  918. if(in_array($ticket['inv_type'],['004','028'])) $result= $this->addApplyRed($ticket,$return_reason,$returnCode,$red_desc);
  919. if(isset($result['code'])){
  920. if(isset($result['data']) &&$result['invRed']!='' ){
  921. $upd=Db::name("invoice_return")->where(["returnCode"=>$returnCode])->update(['red_inv'=>$result['invRed'],"updatetime"=>date("Y-m-d H:i:s")]);
  922. if($upd==false) throw new \Exception("退票更新失败");
  923. }
  924. }
  925. }else{
  926. $invpool=[
  927. "invNo"=>$invinfo['invNo'],
  928. "type"=>1,
  929. "inv_type"=>$ticket['inv_type'],
  930. "inv_code"=>$invCode,
  931. "inv_number"=>$invNum,
  932. "check_code"=>$checkCode,
  933. "inv_total"=>0,
  934. "inv_subtotal"=>$subtotal_fee,
  935. "open_date"=>$open_date,
  936. "item"=>'',
  937. 'pdfstream'=>'',
  938. "status"=>0,
  939. "addtime"=>date("Y-m-d H:i:s"),
  940. "updatetime"=>date("Y-m-d H:i:s")
  941. ];
  942. $in = Db::name("invoice_ticket")->insert($invpool);
  943. if($in==false)throw new \Exception("退票信息添加失败");
  944. }
  945. }
  946. if($status==2){
  947. $infoup =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  948. if($infoup==false)throw new \Exception("退票商品信息修改失败");
  949. $poolup=Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0,"status"=>4])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  950. if($poolup==false)throw new \Exception($return['invNo']."开票票数据更新失败");
  951. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>2])->column("id,orderCode,cancel_fee");
  952. if(!empty($qrdArr)){
  953. foreach ($qrdArr as $value){
  954. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  955. if($qrdinfo==false){
  956. throw new Exception("确认单信息未找到");
  957. // return error_show(1003,"确认单信息未找到");
  958. }
  959. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  960. throw new Exception("确认单信息开票金额不足");
  961. // return error_show(1003,"确认单信息开票金额不足");
  962. }
  963. $update =[
  964. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  965. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  966. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  967. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0&&$qrdinfo['pay_status']==1 ?0 : 1,
  968. "updatetime"=>date("Y-m-d H:i:s"),
  969. ];
  970. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  971. if($qrdup==false){
  972. throw new Exception("确认单信息更新失败");
  973. // return error_show(1003,"确认单信息更新失败");
  974. }
  975. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  976. $assocup =Db::name("assoc")->where($value)->update($assoc);
  977. if($assocup==false){
  978. throw new Exception("确认单关联信息更新失败");
  979. // return error_show(1003,"确认单关联信息更新失败");
  980. }
  981. }
  982. }else{
  983. throw new Exception("未找到关联订单数据信息");
  984. }
  985. }
  986. if($status==3 || $status==4){
  987. foreach ($qrd as $value){
  988. $report=ReportCode::where(["qrdNo"=>$value])->find();
  989. if($report)$report->rmField("returnInv",$returnCode);
  990. }
  991. }
  992. Db::commit();
  993. return app_show(0,"退票申请信息更新成功");
  994. }
  995. Db::rollback();
  996. return error_show(1005,"退票申请信息更新失败");
  997. }catch (\Exception $e){
  998. Db::rollback();
  999. return error_show(1005,$e->getMessage());
  1000. }
  1001. }
  1002. //红冲票
  1003. private function addDiscard($ticket=[],$returnCode){
  1004. $invoiceConf=Config::get('invoice');
  1005. if(!isset($invoiceConf[$ticket['seller_id']]))throw new \Exception('金税配置不存在', 1006);
  1006. $invoice = $invoiceConf[$ticket['seller_id']];
  1007. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1008. $result=$tax->DeprecateInvoices($ticket['seller_id'],$ticket['inv_type'],$ticket['inv_code'],$ticket['inv_number'],1);
  1009. if(isset($result['ZTDM']) && ($result['ZTDM']=='050000' || $result['ZTDM']=='053000')){
  1010. if($result['ZTDM']=='053000'){
  1011. $up= Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["discard"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1012. if($up==false)throw new \Exception('发票废弃中,信息更新失败', 1006);
  1013. return ["code"=>1,"message"=>"发票废弃中"];
  1014. }else{
  1015. $ups=Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["status"=>2,"discard"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  1016. if($ups==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  1017. $up =Db::name("invoice_pool")->where(['invNo'=>$ticket['invNo']])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  1018. if($up==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  1019. return ["code"=>0,"message"=>"发票废弃成功"];
  1020. }
  1021. }else{
  1022. throw new \Exception($result['ZTXX'], 1006);
  1023. }
  1024. }
  1025. //退票详情
  1026. /**
  1027. * @param array $ticket
  1028. * @return \think\response\Json|void
  1029. * @throws \think\Exception
  1030. */
  1031. private function addRedOpen($ticket=[],$return_reason,$company_type,$returnCode,$red_desc){
  1032. $goodinfo =Db::name("invoice_good")
  1033. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1034. ->field("0 as FPHXZ,
  1035. catNo as SPBM,
  1036. id as ZXBM,
  1037. is_discount as YHZCBS,
  1038. if(inv_tag=0,'',inv_tag) as LSLBS,
  1039. addTax as ZZSTSGL,
  1040. goodName as XMMC,
  1041. '' as DW,
  1042. '' as SPSL,
  1043. '' as DJ,
  1044. (-totalPrice) as JE,
  1045. 0 HSBZ,
  1046. tax as SL,
  1047. -taxPrice as SE")->select()->toArray();
  1048. if(empty($goodinfo)){
  1049. throw new \Exception("开票商品信息未找到", 10006);
  1050. }
  1051. if(count($goodinfo)>8){
  1052. $len =array_unique(array_column($goodinfo,'SL'));
  1053. $qdmx=["FPHXZ"=>6,
  1054. "SPBM"=>'',
  1055. "XMMC"=>"详见对应正数发票及清单",
  1056. "YHZCBS"=>0,
  1057. "LSLBS"=>'',
  1058. "ZZSTSGL"=>'',
  1059. "DW"=>'',
  1060. "SPSL"=>'',
  1061. "DJ"=>'',
  1062. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1063. "HSBZ"=>'0',
  1064. "SL"=>count($len)>1?'':$len[0],
  1065. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1066. ];
  1067. }else
  1068. {$qdmx=$goodinfo;}
  1069. $invRed=makeNo("IRE");
  1070. $reddata=[
  1071. 'invNo'=>$ticket['invNo'],
  1072. "invRed"=>$invRed,
  1073. "inv_out"=>$ticket['inv_out'],
  1074. "returnCode"=>$returnCode,
  1075. "apply_id"=>$this->uid,
  1076. "apply_name"=>$this->uname,
  1077. "status"=>3,
  1078. "remark"=>'',
  1079. "addtime"=>date("Y-m-d H:i:s"),
  1080. "updatetime"=>date("Y-m-d H:i:s"),
  1081. "SQBSCQQLSH"=>'',
  1082. "YYSBZ"=> "0000000000", //营业税标志
  1083. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1084. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1085. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1086. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1087. "TKSJ"=>date("YmdHis"), //填开时间
  1088. "XHFSBH"=>$ticket['seller_id'],
  1089. "XHFMC"=>$ticket['seller_title'],
  1090. "GMFSBH"=>$ticket['buyer_id'],
  1091. "GMFMC"=> $ticket['buyer_title'],
  1092. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1093. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1094. "SQSM"=>$red_desc, //申请说明 1100000000 已抵扣 1010000000未抵扣 0000000100尚未寄出
  1095. "XXBTSBS"=>"0",
  1096. "red_reason"=>$return_reason,
  1097. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1098. "SQBSCQQPCH"=>'',
  1099. "FPLXDM"=>$ticket['inv_type'],
  1100. ];
  1101. $data=[ "invNo"=>$invRed,
  1102. "KPLX"=>"1",
  1103. "XHFSBH"=>$ticket['seller_id'],
  1104. "XHFMC"=>$ticket['seller_title'],
  1105. "XHFDZ"=>$ticket['seller_addr'],
  1106. "XHFDH"=>$ticket['seller_mobile'],
  1107. "XHFYH"=>$ticket['seller_bank'],
  1108. "XHFZH"=>$ticket['seller_bankNo'],
  1109. "GMFBM"=>"",
  1110. "GMFSBH"=>$ticket['buyer_id'],
  1111. "GMFMC"=>$ticket['buyer_title'],
  1112. "GMFDZ"=>$ticket['buyer_addr'],
  1113. "GMFDH"=>$ticket['buyer_mobile'],
  1114. "GMFYH"=>$ticket['buyer_bank'],
  1115. "GMFZH"=>$ticket['buyer_bankNo'],
  1116. "GMFLX"=>$company_type,
  1117. "GMFSJH"=>"",
  1118. "GMFDZYX"=>'',
  1119. "KPR"=>$ticket['issuer'],
  1120. "SKR"=>$ticket['reciver'],
  1121. "FHR"=>$ticket['reviewer'],
  1122. "QDBZ"=>"0",
  1123. "YFPDM"=>$ticket['inv_code'],
  1124. "YFPHM"=>$ticket['inv_number'],
  1125. "YFPLX"=>$ticket['inv_type'],
  1126. "YFPKPRQ"=>date("Y-m-d",strtotime($ticket['open_date'])),
  1127. "CHYY"=>$return_reason,
  1128. "TSCHBZ"=>"0",
  1129. "JSHJ"=>"-".$ticket['inv_total'],
  1130. "HJJE"=>"-".$ticket['inv_subtotal'],
  1131. "HJSE"=>"-".$ticket['tax_fee'],
  1132. "BZ"=> "对应正数发票代码:{$ticket['inv_code']} 号码:{$ticket['inv_number']}",
  1133. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1134. "FPLXDM"=>$ticket['inv_type'],
  1135. "DDQQPCH"=>"",
  1136. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1137. "status"=>"0",
  1138. "result"=>"",
  1139. "addtime"=>date("Y-m-d H:i:s"),
  1140. "updatetime"=>date("Y-m-d H:i:s")
  1141. ];
  1142. $invoice=Db::name("invoice_interface")->insert($data);
  1143. if($invoice==false){
  1144. throw new Exception("红冲提交失败",1006);
  1145. }
  1146. $upred=Db::name("invoice_red")->save($reddata);
  1147. if($upred==false){
  1148. throw new Exception("红冲提交失败",1006);
  1149. }
  1150. return ["code"=>0,"message"=>"红冲申请提交成功","invRed"=>$invRed];
  1151. }
  1152. /**
  1153. * @param array $ticket
  1154. * @return \think\response\Json|void|array
  1155. * @throws \think\db\exception\DataNotFoundException
  1156. * @throws \think\db\exception\DbException
  1157. * @throws \think\db\exception\ModelNotFoundException
  1158. */
  1159. private function addApplyRed($ticket=[],$return_reason,$returnCode,$red_desc){
  1160. $invoiceConf=Config::get('invoice');
  1161. if(!isset($invoiceConf[$ticket['seller_id']]))throw new \Exception('金税配置不存在', 1006);
  1162. $invoice = $invoiceConf[$ticket['seller_id']];
  1163. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1164. $redticketList=[];
  1165. $redticketArry=[
  1166. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1167. "NSRSBH"=>$ticket['seller_id'],
  1168. "FPLXDM"=>$ticket['inv_type'],
  1169. ];
  1170. $goodinfo =Db::name("invoice_good")
  1171. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1172. ->field("0 as FPHXZ,
  1173. catNo as SPBM,
  1174. id as ZXBM,
  1175. is_discount as YHZCBS,
  1176. if(inv_tag=0,'',inv_tag) as LSLBS,
  1177. addTax as ZZSTSGL,
  1178. goodName as XMMC,
  1179. '' as DW,
  1180. '' as SPSL,
  1181. '' as DJ,
  1182. (-totalPrice) as JE,
  1183. 0 HSBZ,
  1184. tax as SL,
  1185. -taxPrice as SE")->select()->toArray();
  1186. if(empty($goodinfo)){
  1187. throw new \Exception("开票商品信息未找到", 10006);
  1188. }
  1189. if(count($goodinfo)>8){
  1190. $len =array_unique(array_column($goodinfo,'SL'));
  1191. $qdmx=["FPHXZ"=>6,
  1192. "SPBM"=>'',
  1193. "XMMC"=>"详见对应正数发票及清单",
  1194. "YHZCBS"=>0,
  1195. "LSLBS"=>'',
  1196. "ZZSTSGL"=>'',
  1197. "DW"=>'',
  1198. "SPSL"=>'',
  1199. "DJ"=>'',
  1200. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1201. "HSBZ"=>'0',
  1202. "SL"=>count($len)>1?'':$len[0],
  1203. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1204. ];
  1205. }else
  1206. {
  1207. $qdmx=$goodinfo;
  1208. }
  1209. $info=[
  1210. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1211. "YYSBZ"=> "0000000000", //营业税标志
  1212. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1213. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1214. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1215. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1216. "TKSJ"=>date("YmdHis"), //填开时间
  1217. "XHFSBH"=>$ticket['seller_id'],
  1218. "XHFMC"=>$ticket['seller_title'],
  1219. "GMFSBH"=>$ticket['buyer_id'],
  1220. "GMFMC"=> $ticket['buyer_title'],
  1221. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1222. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1223. "SQSM"=>$red_desc, //申请说明
  1224. "XXBTSBS"=>"0",
  1225. //0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1226. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1227. ];
  1228. $invRed=makeNo("IRE");
  1229. $data=[
  1230. 'invNo'=>$ticket['invNo'],
  1231. "invRed"=>$invRed,
  1232. "inv_out"=>$ticket['inv_out'],
  1233. "returnCode"=>$returnCode,
  1234. "apply_id"=>$this->uid,
  1235. "apply_name"=>$this->uname,
  1236. "status"=>0,
  1237. "remark"=>'',
  1238. "addtime"=>date("Y-m-d H:i:s"),
  1239. "updatetime"=>date("Y-m-d H:i:s"),
  1240. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1241. "YYSBZ"=> "0000000000", //营业税标志
  1242. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1243. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1244. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1245. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1246. "TKSJ"=>date("YmdHis"), //填开时间
  1247. "XHFSBH"=>$ticket['seller_id'],
  1248. "XHFMC"=>$ticket['seller_title'],
  1249. "GMFSBH"=>$ticket['buyer_id'],
  1250. "GMFMC"=> $ticket['buyer_title'],
  1251. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1252. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1253. "SQSM"=>$red_desc, //申请说明
  1254. "red_reason"=>$return_reason,
  1255. "XXBTSBS"=>"0",
  1256. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1257. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1258. "FPLXDM"=>$ticket['inv_type'],
  1259. ];
  1260. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1261. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1262. $data['status']=5;
  1263. $data['remark']='红冲申请提交失败';
  1264. if(isset($result['ZTDM'])){
  1265. // $resultChild=$result['HZSQDSCJG'][0];
  1266. if($result['ZTDM']=='060000'){
  1267. $data['status']=1;
  1268. // $data['XXBBH']=$resultChild['XXBBH'];
  1269. $data['remark']='红冲申请审核通过';
  1270. }
  1271. if($result['ZTDM']=='060001'){
  1272. $data['status']=1;
  1273. // $data['XXBBH']=$resultChild['XXBBH'];
  1274. $data['remark']='红冲申请审核中';
  1275. }
  1276. }
  1277. if($data['status']==5){
  1278. throw new \Exception($result['ZTXX'], 10006);
  1279. }
  1280. $up =Db::name("invoice_red")->insert($data);
  1281. if($up) return ["code"=>0,"message"=>"红冲发票申请新建成功","invRed"=>$invRed];
  1282. else throw new \Exception($result['ZTXX'], 1006);
  1283. }
  1284. //获取发票详情
  1285. public function returnRed(){
  1286. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1287. if($returnCode==""){
  1288. return error_show(1005,"参数 returnCode 不能为空");
  1289. }
  1290. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1291. if($return==false){
  1292. return error_show(1005,"退票申请信息未找到");
  1293. }
  1294. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  1295. if($redinv==""){
  1296. return error_show(1005,"参数 redinv 不能为空");
  1297. }
  1298. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  1299. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  1300. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  1301. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1302. if($invinfo==false){
  1303. return error_show(1005,"发票申请信息未找到");
  1304. }
  1305. Db::startTrans();
  1306. try{
  1307. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  1308. $up =Db::name("invoice_return")->where($return)->update($update);
  1309. if($up){
  1310. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  1311. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  1312. if($inv){
  1313. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  1314. if(!empty($qrdArr)){
  1315. foreach ($qrdArr as $value){
  1316. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  1317. if($qrdinfo==false){
  1318. Db::rollback();
  1319. return error_show(1003,"确认单信息未找到");
  1320. }
  1321. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  1322. Db::rollback();
  1323. return error_show(1003,"确认单信息开票金额不足");
  1324. }
  1325. $update =[
  1326. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  1327. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  1328. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ?
  1329. 1 : 2,
  1330. "status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 && $qrdinfo['pay_status']==1 ?0 : 1,
  1331. "updatetime"=>date("Y-m-d H:i:s"),
  1332. ];
  1333. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  1334. if($qrdup==false){
  1335. Db::rollback();
  1336. return error_show(1003,"确认单信息更新失败");
  1337. }
  1338. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  1339. $assocup =Db::name("assoc")->where($value)->update($assoc);
  1340. if($assocup==false){
  1341. Db::rollback();
  1342. return error_show(1003,"确认单关联信息更新失败");
  1343. }
  1344. }
  1345. }
  1346. Db::commit();
  1347. return app_show(0,"退票申请信息更新成功");
  1348. }
  1349. }
  1350. Db::rollback();
  1351. return error_show(1005,"退票申请信息更新失败");
  1352. }catch (\Exception $e){
  1353. Db::rollback();
  1354. return error_show(1005,$e->getMessage());
  1355. }
  1356. }
  1357. //开票异常失败后修改票面信息
  1358. public function returnInfo(){
  1359. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1360. if($returnCode==""){
  1361. return error_show(1005,"参数 returnCode 不能为空");
  1362. }
  1363. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1364. if($return==false){
  1365. return error_show(1005,"退票申请信息未找到");
  1366. }
  1367. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1368. $return['inv_out']= $invinfo['inv_out']??"";
  1369. $return['inv_value']= $invinfo['inv_value']??"";
  1370. $return['inv_apply_id']= $invinfo['apply_id']??"";
  1371. $return['inv_apply_name']= $invinfo['apply_name']??"";
  1372. $return['inv_type']= $invinfo['inv_type']??"";
  1373. $return['open_type']= $invinfo['open_type']??"";
  1374. $return['is_ticket']= $invinfo['is_ticket']??"";
  1375. $return['email']= $invinfo['email']??"";
  1376. $return['post_code']= $invinfo['post_code']??"";
  1377. $return['post_company']= $invinfo['post_company']??"";
  1378. $return['check_remark']= $invinfo['check_remark']??"";
  1379. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  1380. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1381. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1382. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  1383. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  1384. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  1385. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  1386. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  1387. $return['seller_title'] = $poolinfo['seller_title']??'';
  1388. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  1389. $return['seller_code'] = $poolinfo['seller_code']??'';
  1390. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  1391. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  1392. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  1393. $return['voider'] = $poolinfo['voider']??'';
  1394. $return['payee'] = $poolinfo['payee']??'';
  1395. $return['drawer'] = $poolinfo['drawer']??'';
  1396. $return['reviewer'] = $poolinfo['reviewer']??'';
  1397. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  1398. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  1399. $return['goodList']=$orderList;
  1400. $info =Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"is_del"=>0,"type"=>1]) ->field("inv_subtotal,inv_total,check_code,inv_number,inv_code,open_date")->find();
  1401. $return['inv_number']=$info['inv_number']??"";
  1402. $return['inv_code']=$info['inv_code']??"";
  1403. $return['open_date']=date("Y-m-d",strtotime($info['open_date']));
  1404. $return['inv_subtotal']=$info['inv_subtotal']??"";
  1405. $return['inv_total']=$info['inv_total']??"";
  1406. $return['check_code']=$info['check_code']??"";
  1407. if(!in_array($return['status'],[2,3,4,5])){
  1408. $opendate =$info['open_date']??"";;
  1409. if($opendate!=''){
  1410. $open_month =date("Y-m",strtotime($opendate));
  1411. $return['is_out_month']= $open_month==date("Y-m")?0:1;
  1412. }
  1413. }
  1414. return app_show(0,"获取成功",$return);
  1415. }
  1416. /**
  1417. *发票池列表
  1418. */
  1419. public function list(){
  1420. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1421. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1422. $condition=[["a.is_del","=",0]];
  1423. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1424. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1425. $roleid = $this->roleid;
  1426. $check = checkRole($roleid,'84');
  1427. if($check){
  1428. $condition[]=["a.apply_id","=",$this->uid];
  1429. }
  1430. if($start!=""){
  1431. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1432. }
  1433. if($end!=""){
  1434. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1435. }
  1436. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1437. if($status!==""){
  1438. $condition[]=["a.status","=",$status];
  1439. }
  1440. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1441. if($inv_type!==""){
  1442. $condition[]=["a.inv_type","=",$inv_type];
  1443. }
  1444. $inv_lower = isset($this->post['inv_lower'])&&$this->post['inv_lower']!=='' ? floatval($this->post['inv_lower'])
  1445. :'';
  1446. if($inv_lower!==''){
  1447. $condition[]=['a.inv_value','>=',$inv_lower];
  1448. }
  1449. $inv_uper = isset($this->post['inv_uper'])&&$this->post['inv_uper']!=='' ? floatval($this->post['inv_uper'])
  1450. :'';
  1451. if($inv_uper!==''){
  1452. $condition[]=['a.inv_value','<=',$inv_uper];
  1453. }
  1454. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  1455. if($inv_out!==""){
  1456. $condition[]=["a.inv_out","=",$inv_out];
  1457. }
  1458. $platform_type= isset($this->post['platform_type'])&&$this->post['platform_type']!=="" ? intval($this->post['platform_type']):"";
  1459. if($platform_type!==""){
  1460. $condition[]=["a.platform_type","=",$platform_type];
  1461. }
  1462. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1463. if($invNo!==""){
  1464. $condition[]=["a.invNo","like","%$invNo%"];
  1465. }
  1466. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  1467. if($relaComNo!=""){
  1468. $condition[]=["a.inv_out","=",$relaComNo];
  1469. }
  1470. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  1471. if($inv_number!==""){
  1472. $condition[]=["c.inv_number","like","%$inv_number%"];
  1473. }
  1474. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  1475. if($inv_code!==""){
  1476. $condition[]=["c.inv_code","like","%$inv_code%"];
  1477. }
  1478. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  1479. if($inv_company!==""){
  1480. $condition[]=["a.inv_company","like","%$inv_company%"];
  1481. }
  1482. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  1483. if($buyer_name!==""){
  1484. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  1485. }
  1486. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1487. if($apply_id!==""){
  1488. $condition[]=["a.apply_id","=",$apply_id];
  1489. }
  1490. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1491. if($apply_name!==""){
  1492. $condition[]=["a.apply_name","like","%$apply_name%"];
  1493. }
  1494. $count=Db::name("invoice_pool")->alias("a")
  1495. ->rightJoin("invoice_pool_info b","a.invNo=b.invNo")
  1496. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0 and c.is_del=0")
  1497. ->where($condition)->count();
  1498. $total =ceil($count/$size);
  1499. $page= $page>$total ? intval($total):$page;
  1500. $list=Db::name("invoice_pool")->alias("a")
  1501. ->rightJoin("invoice_pool_info b","a.invNo=b.invNo")
  1502. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0 and c.is_del=0")
  1503. ->where($condition)->field("a.id,
  1504. a.invNo,
  1505. a.platform_type,
  1506. a.open_type,
  1507. a.is_ticket,
  1508. a.tag_id,
  1509. a.apply_id,
  1510. a.apply_name,
  1511. a.status,
  1512. a.inv_value,
  1513. a.inv_type,
  1514. a.buyer_check,
  1515. a.seller_check,
  1516. a.exam_remark,
  1517. a.check_remark,
  1518. a.inv_company,
  1519. a.inv_out,
  1520. b.buyer_title,
  1521. b.seller_title,
  1522. a.addtime,
  1523. c.inv_code,
  1524. c.inv_number,
  1525. c.open_date,
  1526. c.type")
  1527. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  1528. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1529. }
  1530. //获取发票详情
  1531. public function info(){
  1532. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  1533. if($invNo==''){
  1534. return error_show(1004,"参数 invNo 不能为空");
  1535. }
  1536. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  1537. if(empty($poolinfo)){
  1538. return error_show(1004,"为找到开票信息");
  1539. }
  1540. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  1541. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  1542. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  1543. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  1544. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  1545. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  1546. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  1547. $poolinfo['seller_title'] = $pool['seller_title']??'';
  1548. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  1549. $poolinfo['seller_code'] = $pool['seller_code']??'';
  1550. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1551. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1552. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1553. $poolinfo['voider'] = $pool['voider']??'';
  1554. $poolinfo['payee'] = $pool['payee']??'';
  1555. $poolinfo['drawer'] = $pool['drawer']??'';
  1556. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1557. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1558. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->column("*","orderCode");
  1559. $poolinfo['goodList']=$orderList;
  1560. $poolinfo['invinfo']=Db::name("invoice_ticket")->withoutField("pdfstream")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])
  1561. ->findOrEmpty();
  1562. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  1563. ->where(["a.viceCode"=>$invNo,"a.is_del"=>0,"a.status"=>[1,2]])
  1564. ->order("a.addtime desc")
  1565. ->field("c.*,a.cancel_fee")
  1566. ->select()->toArray();
  1567. foreach ($orderinfo as &$item){
  1568. $item['ainv_num'] = Db::name("invoice_pool")
  1569. ->alias("a")
  1570. ->leftJoin("invoice_good b", "a.invNo=b.invNo and b.is_del=0")
  1571. ->where(["a.status" => [0, 1, 2, 3], "b.orderCode" => $item['sequenceNo']])
  1572. ->where('a.invNo', '<>', $invNo)
  1573. ->sum("b.goodNum");
  1574. $item['winv_num'] = $item['goodNum'] - $item['ainv_num'];
  1575. $item['inv_num'] = $orderList[$item['sequenceNo']]['goodNum'];
  1576. }
  1577. $poolinfo['orderinfo']=$orderinfo;
  1578. return app_show(0,"获取成功",$poolinfo);
  1579. }
  1580. //批量申请红冲发票
  1581. public function TicketEdit(){
  1582. $post=$this->request->only([
  1583. "invNo"=>"", //开票申请编号
  1584. "inv_code"=>"",//发票编码
  1585. "inv_number"=>"",//发票号码
  1586. "inv_subtotal"=>"",//发票税前额度
  1587. "inv_total"=>"",//发票额度
  1588. "open_date"=>"",//开票日期
  1589. "check_code"=>"",//发票校验码
  1590. ],"post","trim");
  1591. $valid=Validate::rule([
  1592. "invNo|开票申请编号"=>"require|max:255",
  1593. "inv_code|开票代码"=>"number",
  1594. "inv_number|开票编号"=>"number",
  1595. "inv_subtotal|不含税金额"=>"float",
  1596. "inv_total|价税合计"=>"float",
  1597. "open_date|开票日期"=>"date",
  1598. "check_code|校验码"=>"max:255",
  1599. ]);
  1600. if($valid->check($post)==false) return error_show(1004,$valid->getError());
  1601. $ticketinfo =Db::name("invoice_ticket")->where(["invNo"=>$post['invNo'],"is_del"=>0,"type"=>0])->findOrEmpty();
  1602. if(empty($ticketinfo)) return error_show(1004,"未找到开票数据");
  1603. $pool = Db::name("invoice_pool")->where(["invNo"=>$post['invNo'],"is_del"=>0,"status"=>[5,9]])->findOrEmpty();
  1604. if(empty($pool)) return error_show(1004,"开票申请数据不可修改");
  1605. if($pool['open_type']==1)return error_show(1004,"金税开票数据不可修改");
  1606. if($post['inv_number']==='') return error_show(1004,"参数 inv_number不能为空");
  1607. if($post['open_date']==='') return error_show(1004,"参数 open_date 不能为空");
  1608. if(in_array($pool['inv_type'],['normal','roll',"toll",'electronic'])){
  1609. if($post['check_code']==='') return error_show(1004,"参数 check_code不能为空");
  1610. }
  1611. //全电子发票
  1612. if(in_array($pool['inv_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1613. //全电发票传含税金额
  1614. if($post['inv_total']=='') return error_show(1004,"参数 inv_total 不能为空");
  1615. }else{
  1616. //全电发票无invCode
  1617. if($post['inv_code']==='') return error_show(1004,"参数 inv_code不能为空");
  1618. if($post['inv_subtotal']==='') return error_show(1004,"参数 inv_subtotal 不能为空");
  1619. }
  1620. Db::startTrans();
  1621. try{
  1622. $Ticket=[
  1623. "inv_code"=>$post['inv_code'],//发票编码
  1624. "inv_number"=>$post['inv_number'],//发票号码
  1625. "inv_subtotal"=>$post['inv_subtotal'],//发票税前额度
  1626. "inv_total"=>$post['inv_total'],//发票额度
  1627. "open_date"=>$post['open_date'],//开票日期
  1628. "check_code"=>$post['check_code'],//发票校验码
  1629. "status"=>0,//带校验
  1630. "updatetime"=>date("Y-m-d H:i:s")//发票校验码
  1631. ];
  1632. $ticket =Db::name("invoice_ticket")->where($ticketinfo)->update($Ticket);
  1633. if($ticket==false) throw new \Exception("开票信息更新失败");
  1634. $polup =Db::name("invoice_pool")->where($pool)->update(["status"=>3,"check_remark"=>"","updatetime"=>date("Y-m-d H:i:s")]);
  1635. if($polup==false) throw new \Exception("开票申请信息更新失败");
  1636. Db::commit();
  1637. return app_show(0,"开票信息更新成功");
  1638. }catch (Exception $e){
  1639. Db::rollback();
  1640. return error_show(1005,$e->getMessage());
  1641. }
  1642. }
  1643. //红票申请开票
  1644. public function returnList(){
  1645. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1646. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1647. $condition=[["b.is_del","=",0]];
  1648. $roleid = $this->roleid;
  1649. $check = checkRole($roleid,'89');
  1650. if($check){
  1651. $condition[]=["a.apply_id","=",$this->uid];
  1652. }
  1653. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1654. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1655. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  1656. if($companyNo!=""){
  1657. $condition[]=["b.inv_out","=",$companyNo];
  1658. }
  1659. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1660. if($relaComNo!=""){
  1661. $condition[]=["b.inv_out","=",$relaComNo];
  1662. }
  1663. if($start!=""){
  1664. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1665. }
  1666. if($end!=""){
  1667. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1668. }
  1669. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1670. if($inv_type!==""){
  1671. $condition[]=["b.inv_type","=",$inv_type];
  1672. }
  1673. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1674. if($apply_id!==""){
  1675. $condition[]=["a.apply_id","=",$apply_id];
  1676. }
  1677. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1678. if($apply_name!==""){
  1679. $condition[]=["a.apply_name","like","%$apply_name%"];
  1680. }
  1681. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1682. if($status!==""){
  1683. $condition[]=["a.status","=",$status];
  1684. }
  1685. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1686. if($invNo!==""){
  1687. $condition[]=["a.invNo","like","%$invNo%"];
  1688. }
  1689. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1690. if($returnCode!==""){
  1691. $condition[]=["a.returnCode","like","%$returnCode%"];
  1692. }
  1693. $count=Db::name("invoice_return")->alias("a")
  1694. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1695. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1696. ->where($condition)->count();
  1697. $total =ceil($count/$size);
  1698. $page= $page>$total ? intval($total):$page;
  1699. $list=Db::name("invoice_return")->alias("a")
  1700. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1701. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1702. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,c.buyer_bankNo,c.seller_title,c.seller_code,c.seller_addr,c.seller_mobile,c.seller_bank,c.seller_bankNo,c.voider,c.payee,c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->order("id desc")->select()->toArray();
  1703. foreach ($list as &$value){
  1704. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  1705. $value['companyName'] = $company['company_name']??"";
  1706. if(!in_array($value['status'],[2,3,4,5])){
  1707. $opendate =Db::name("invoice_ticket")->where(["invNo"=>$value['invNo'],"is_del"=>0,"type"=>0])->value("open_date","");
  1708. if($opendate!=''){
  1709. $open_month =date("Y-m",strtotime($opendate));
  1710. $value['is_out_month']= $open_month==date("Y-m")?0:1;
  1711. }
  1712. }
  1713. }
  1714. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1715. }
  1716. //红冲申请
  1717. public function applyRed(){
  1718. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1719. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  1720. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  1721. if($invfo==false) return error_show(1004,"发票数据未找到");
  1722. if(!in_array($invfo['status'],[1,2,3,4,5,8]))return error_show(1004,"发票数据状态无法申请");
  1723. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  1724. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  1725. $company =Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->findOrEmpty();
  1726. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  1727. $invoiceConf=Config::get('invoice');
  1728. if(!isset($invoiceConf[$company['company_license']]))throw new \Exception('金税配置不存在', 1006);
  1729. $invoice = $invoiceConf[$company['company_license']];
  1730. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1731. $redticketList=[];
  1732. $redticketArry=[
  1733. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1734. "NSRSBH"=>$company['company_license'],
  1735. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1736. ];
  1737. $goodinfo =Db::name("invoice_good")
  1738. ->where(["invNo"=>$invNo,"is_del"=>0])
  1739. ->field("0 as FPHXZ,
  1740. catNo as SPBM,
  1741. id as ZXBM,
  1742. is_discount as YHZCBS,
  1743. if(inv_tag=0,'',inv_tag) as LSLBS,
  1744. addTax as ZZSTSGL,
  1745. goodName as XMMC,
  1746. '' as DW,
  1747. '' as SPSL,
  1748. '' as DJ,
  1749. (-totalPrice) as JE,
  1750. 0 HSBZ,
  1751. tax as SL,
  1752. -taxPrice as SE")->select()->toArray();
  1753. if(empty($goodinfo)){
  1754. Db::rollback();
  1755. return error_show(1004,"开票商品信息未找到");
  1756. }
  1757. if(count($goodinfo)>8){
  1758. $len =array_unique(array_column($goodinfo,'SL'));
  1759. $qdmx=["FPHXZ"=>6,
  1760. "SPBM"=>'',
  1761. "XMMC"=>"详见对应正数发票及清单",
  1762. "YHZCBS"=>0,
  1763. "LSLBS"=>'',
  1764. "ZZSTSGL"=>'',
  1765. "DW"=>'',
  1766. "SPSL"=>'',
  1767. "DJ"=>'',
  1768. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1769. "HSBZ"=>'0',
  1770. "SL"=>count($len)>1?'':$len[0],
  1771. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1772. ];
  1773. }else
  1774. {
  1775. $qdmx=$goodinfo;
  1776. }
  1777. $info=[
  1778. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1779. "YYSBZ"=> "0000000000", //营业税标志
  1780. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1781. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1782. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1783. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1784. "TKSJ"=>date("YmdHis"), //填开时间
  1785. "XHFSBH"=>$ticket['seller_id'],
  1786. "XHFMC"=>$ticket['seller_title'],
  1787. "GMFSBH"=>$ticket['buyer_id'],
  1788. "GMFMC"=> $ticket['buyer_title'],
  1789. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1790. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1791. "SQSM"=>"1100000000", //申请说明
  1792. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1793. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1794. ];
  1795. $invRed=makeNo("IRE");
  1796. $data=[
  1797. 'invNo'=>$invNo,
  1798. "invRed"=>$invRed,
  1799. "inv_out"=>$invfo['inv_out']??"",
  1800. "apply_id"=>$this->uid,
  1801. "apply_name"=>$this->uname,
  1802. "status"=>0,
  1803. "remark"=>'',
  1804. "addtime"=>date("Y-m-d H:i:s"),
  1805. "updatetime"=>date("Y-m-d H:i:s"),
  1806. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1807. "YYSBZ"=> "0000000000", //营业税标志
  1808. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1809. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1810. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1811. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1812. "TKSJ"=>date("YmdHis"), //填开时间
  1813. "XHFSBH"=>$ticket['seller_id'],
  1814. "XHFMC"=>$ticket['seller_title'],
  1815. "GMFSBH"=>$ticket['buyer_id'],
  1816. "GMFMC"=> $ticket['buyer_title'],
  1817. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1818. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1819. "SQSM"=>"1100000000", //申请说明
  1820. "XXBTSBS"=>"0",
  1821. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1822. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1823. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1824. ];
  1825. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1826. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1827. $data['status']=5;
  1828. $data['remark']='红冲申请提交失败';
  1829. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1830. $resultChild=$result['HZSQDSCJG'][0];
  1831. if($resultChild['ZTDM']=='060000'){
  1832. $data['status']=2;
  1833. $data['XXBBH']=$resultChild['XXBBH'];
  1834. $data['remark']='红冲申请审核通过';
  1835. }
  1836. if($resultChild['ZTDM']=='060001'){
  1837. $data['status']=1;
  1838. $data['XXBBH']=$resultChild['XXBBH'];
  1839. $data['remark']='红冲申请审核中';
  1840. }
  1841. }
  1842. if($data['status']==5){
  1843. return error_show(1004,"红冲发票申请新建失败");
  1844. }
  1845. $up =Db::name("invoice_red")->insert($data);
  1846. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1847. }
  1848. //批量申请红冲发票
  1849. public function batchApplyRed(){
  1850. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1851. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1852. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1853. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1854. $insertAll=[];
  1855. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1856. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1857. if(count($companyCode)>1){
  1858. return error_show(1004,"业务企业公司不统一");
  1859. }
  1860. if(count($inv_type)>1){
  1861. return error_show(1004,"发票类型不统一");
  1862. }
  1863. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1864. if(empty($company)) {
  1865. return error_show(1004,"业务企业信息未找到");
  1866. }
  1867. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1868. if(empty($ticketArr)){
  1869. return error_show(1004,"未找到开票信息");
  1870. }
  1871. $invoiceConf=Config::get('invoice');
  1872. if(!isset($invoiceConf[$company['company_license']]))throw new \Exception('金税配置不存在', 1006);
  1873. $invoice = $invoiceConf[$company['company_license']];
  1874. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1875. Db::startTrans();
  1876. try{
  1877. $redticketArry=[
  1878. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1879. "NSRSBH"=>$company['company_license'],
  1880. "FPLXDM"=>$this->kingInvoice[$inv_type[0]],
  1881. ];
  1882. $redticketList=[];
  1883. foreach ($invfo as $value){
  1884. if(!in_array($value['status'],[1,2,3,4,5,8])){
  1885. Db::rollback();
  1886. return error_show(1004,"发票数据状态无法申请");
  1887. }
  1888. $ticket =$ticketArr[$value['invNo']] ??[];
  1889. if(empty($ticket)){
  1890. Db::rollback();
  1891. return error_show(1004,"未找到开票信息");
  1892. }
  1893. $goodinfo =Db::name("invoice_good")
  1894. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1895. ->field(" 6 as FPHXZ,
  1896. catNo as SPBM,
  1897. id as ZXBM,
  1898. is_discount as YHZCBS,
  1899. if(inv_tag=0,'',inv_tag) as LSLBS,
  1900. addTax as ZZSTSGL,
  1901. goodName as XMMC,
  1902. '' as DW,
  1903. '' as SPSL,
  1904. '' as DJ,
  1905. (-totalPrice) as JE,
  1906. 0 HSBZ,
  1907. tax as SL,
  1908. -taxPrice as SE")
  1909. ->select()->toArray();
  1910. if(empty($goodinfo)){
  1911. Db::rollback();
  1912. return error_show(1004,"开票商品信息未找到");
  1913. }
  1914. $len =array_unique(array_column($goodinfo,'SL'));
  1915. $qdmx=["FPHXZ"=>6,
  1916. "SPBM"=>'',
  1917. "XMMC"=>"详见对应正数发票及清单",
  1918. "YHZCBS"=>0,
  1919. "LSLBS"=>'',
  1920. "ZZSTSGL"=>'',
  1921. "DW"=>'',
  1922. "SPSL"=>'',
  1923. "DJ"=>'',
  1924. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1925. "HSBZ"=>'0',
  1926. "SL"=>count($len)>1?'':$len[0],
  1927. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1928. ];
  1929. $info=[
  1930. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1931. "YYSBZ"=> "0000000000", //营业税标志
  1932. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1933. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1934. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1935. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1936. "TKSJ"=>date("YmdHis"), //填开时间
  1937. "XHFSBH"=>$ticket['seller_id'],
  1938. "XHFMC"=>$ticket['seller_title'],
  1939. "GMFSBH"=>$ticket['buyer_id'],
  1940. "GMFMC"=> $ticket['buyer_title'],
  1941. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1942. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1943. "SQSM"=>"1100000000", //申请说明
  1944. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1945. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1946. ];
  1947. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1948. $data=[
  1949. 'invNo'=>$value['invNo'],
  1950. "invRed"=>makeNo("IRE"),
  1951. "inv_out"=>$value['inv_out']??"",
  1952. "apply_id"=>$this->uid,
  1953. "apply_name"=>$this->uname,
  1954. "status"=>5,
  1955. "remark"=>'红冲申请提交失败',
  1956. "addtime"=>date("Y-m-d H:i:s"),
  1957. "updatetime"=>date("Y-m-d H:i:s"),
  1958. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1959. "YYSBZ"=> "0000000000", //营业税标志
  1960. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1961. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1962. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1963. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1964. "TKSJ"=>date("YmdHis"), //填开时间
  1965. "XHFSBH"=>$ticket['seller_id'],
  1966. "XHFMC"=>$ticket['seller_title'],
  1967. "GMFSBH"=>$ticket['buyer_id'],
  1968. "GMFMC"=> $ticket['buyer_title'],
  1969. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1970. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1971. "SQSM"=>"1100000000", //申请说明
  1972. "XXBTSBS"=>"0",
  1973. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1974. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1975. "FPLXDM"=>$this->kingInvoice[$value['inv_type']],
  1976. ];
  1977. $insertAll[$info['SQBSCQQLSH']]=$data;
  1978. }
  1979. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1980. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1981. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1982. foreach ($result['HZSQDSCJG'] as $value){
  1983. if($value['ZTDM']=='060000'){
  1984. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1985. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1986. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1987. continue;
  1988. // $data['status']=2;
  1989. // $data['remark']='红冲申请审核通过';
  1990. }
  1991. if($value['ZTDM']=='060001'){
  1992. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1993. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1994. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1995. continue;
  1996. }
  1997. unset($insertAll[$value['SQBSCQQLSH']]);
  1998. }
  1999. }
  2000. }else{
  2001. Db::rollback();
  2002. return error_show(1004,"红冲发票申请失败");
  2003. }
  2004. $uns= Db::name("invoice_red")->insertAll($insertAll);
  2005. if($uns){
  2006. Db::commit();
  2007. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  2008. }
  2009. Db::rollback();
  2010. return error_show(1004,"红冲发票申请新建失败");
  2011. }catch (\Exception $e){
  2012. Db::rollback();
  2013. return error_show(1004,$e->getMessage());
  2014. }
  2015. }
  2016. //红票申请开票
  2017. public function redOpen(){
  2018. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  2019. if($invRed==''){
  2020. return error_show(1004,"参数 invRed 不能为空");
  2021. }
  2022. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  2023. if(empty($redinfo)){
  2024. return error_show(1004,"红冲发票申请不存在");
  2025. }
  2026. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  2027. if($redreason==''){
  2028. return error_show(1004,"参数 redreason 不能为空");
  2029. }
  2030. if($redinfo['status']!=2){
  2031. return error_show(1004,"数据状态不可提交申请");
  2032. }
  2033. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  2034. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  2035. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  2036. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  2037. Db::startTrans();
  2038. try{
  2039. $data=[
  2040. "invNo"=>$invRed,
  2041. "KPLX"=>"1",
  2042. "XHFSBH"=>$invpoolinfo['seller_id'],
  2043. "XHFMC"=>$invpoolinfo['seller_title'],
  2044. "XHFDZ"=>$invpoolinfo['seller_addr'],
  2045. "XHFDH"=>$invpoolinfo['seller_mobile'],
  2046. "XHFYH"=>$invpoolinfo['seller_bank'],
  2047. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  2048. "GMFBM"=>"",
  2049. "GMFSBH"=>$invpoolinfo['buyer_id'],
  2050. "GMFMC"=>$invpoolinfo['buyer_title'],
  2051. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  2052. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  2053. "GMFYH"=>$invpoolinfo['buyer_bank'],
  2054. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  2055. "GMFLX"=>$invinfo['company_type'],
  2056. "GMFSJH"=>"",
  2057. "GMFDZYX"=>$invinfo['email'],
  2058. "KPR"=>$invpoolinfo['issuer'],
  2059. "SKR"=>$invpoolinfo['reciver'],
  2060. "FHR"=>$invpoolinfo['reviewer'],
  2061. "QDBZ"=>"0",
  2062. "YFPDM"=>$redinfo['YFPDM'],
  2063. "YFPHM"=>$redinfo['YFPHM'],
  2064. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  2065. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  2066. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  2067. "TSCHBZ"=>"0",
  2068. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  2069. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  2070. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  2071. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  2072. "DDMXXX"=>$redinfo['DDMXXX'],
  2073. "FPLXDM"=>$redinfo['FPLXDM'],
  2074. "DDQQPCH"=>"",
  2075. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  2076. "status"=>"0",
  2077. "result"=>"",
  2078. "addtime"=>date("Y-m-d H:i:s"),
  2079. "updatetime"=>date("Y-m-d H:i:s")
  2080. ];
  2081. $invoice=Db::name("invoice_interface")->insert($data);
  2082. if($invoice==false){
  2083. Db::rollback();
  2084. return error_show(1003,"红冲开票信息生成失败");
  2085. }
  2086. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  2087. if($upred==false){
  2088. Db::rollback();
  2089. return error_show(1003,"红冲发票信息更新失败");
  2090. }
  2091. Db::commit();
  2092. return app_show(0,"红冲发票信息开票提交成功");
  2093. }catch (\Exception $e){
  2094. Db::rollback();
  2095. return error_show(1003,$e->getMessage());
  2096. }
  2097. }
  2098. //红冲票申请列表
  2099. public function redList(){
  2100. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2101. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2102. $condition=[["is_del","=",0],["FPLXDM","in",["004",'028']]];
  2103. $roleid = $this->roleid;
  2104. $check = checkRole($roleid,'110');
  2105. if($check){
  2106. $condition[]=["apply_id","=",$this->uid];
  2107. }
  2108. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  2109. if($invRed!=''){
  2110. $condition[]=["invRed","like","%$invRed%"];
  2111. }
  2112. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  2113. if($apply_id!=''){
  2114. $condition[]=["apply_id","=",$apply_id];
  2115. }
  2116. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  2117. if($invNo!=''){
  2118. $condition[]=["invNo","like","%$invNo%"];
  2119. }
  2120. $companyNo=isset($this->post["companyNo"])&&$this->post["companyNo"]!=''?trim($this->post["companyNo"]):"";
  2121. if($companyNo!=''){
  2122. $condition[]=["inv_out","=",$companyNo];
  2123. }
  2124. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2125. if($relaComNo!=""){
  2126. $condition[]=["inv_out","=",$relaComNo];
  2127. }
  2128. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  2129. if($apply_name!=''){
  2130. $condition[]=["apply_name","like","%$apply_name%"];
  2131. }
  2132. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  2133. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  2134. if($start!=''){
  2135. $condition[]=["addtime",">=",$start];
  2136. }
  2137. if($end!=''){
  2138. $condition[]=["addtime","<=",$end];
  2139. }
  2140. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  2141. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  2142. if($total_low!=''){
  2143. $condition[]=["ABS(HJJE)",">=",$total_low];
  2144. }
  2145. if($total_up!=''){
  2146. $condition[]=["ABS(HJJE)","<=",$total_up];
  2147. }
  2148. $count= Db::name("invoice_red")->where($condition)->count();
  2149. $total =ceil($count/$size);
  2150. $page=$total>=$page?$page :intval($total);
  2151. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  2152. ->toArray();
  2153. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2154. }
  2155. /**
  2156. * @return \think\response\Json|void
  2157. */
  2158. public function ticketInfo(){
  2159. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  2160. if($num==''){
  2161. return error_show(1004,'参数 number 不能为空');
  2162. }
  2163. $condition=[["status","in",[0,1,2]]];
  2164. $condition[]=["inv_number","=",$num];
  2165. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  2166. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  2167. $info =Db::name("invoice_ticket")->json(['item'])->withoutField("pdfstream")->where($condition)->findOrEmpty();
  2168. if(!empty($info)&& isset($info['item'])){
  2169. array_walk($info['item'],function (&$value){
  2170. if(stripos($value["SL"],"%")==false)$value['SL'] = ($value['SL']*100)."%";
  2171. });
  2172. }
  2173. $info['invtype_name'] = $this->typename[$info['inv_type']]??"";
  2174. return empty($info)?error_show(1005,"未找到数据"):app_show(0,"获取成功",$info);
  2175. }
  2176. //退票列表
  2177. public function returnRedList(){
  2178. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2179. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2180. $condition=[];
  2181. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  2182. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  2183. if($start!=""){
  2184. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  2185. }
  2186. if($end!=""){
  2187. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  2188. }
  2189. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  2190. if($inv_type!==""){
  2191. $condition[]=["c.inv_type","=",$inv_type];
  2192. }
  2193. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=="" ? trim($this->post['companyNo']):"";
  2194. if($companyNo!==""){
  2195. $condition[]=["c.inv_out","=",$companyNo];
  2196. }
  2197. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2198. if($relaComNo!=""){
  2199. $condition[]=["c.inv_out","=",$relaComNo];
  2200. }
  2201. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  2202. if($apply_id!==""){
  2203. $condition[]=["a.apply_id","=",$apply_id];
  2204. }
  2205. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  2206. if($apply_name!==""){
  2207. $condition[]=["a.apply_name","like","%$apply_name%"];
  2208. }
  2209. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  2210. if($status!==""){
  2211. $condition[]=["a.status","=",$status];
  2212. }
  2213. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  2214. if($invNo!==""){
  2215. $condition[]=["a.invNo","like","%$invNo%"];
  2216. }
  2217. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  2218. if($returnCode!==""){
  2219. $condition[]=["a.returnCode","like","%$returnCode%"];
  2220. }
  2221. $count=Db::name("invoice_return")->alias("a")
  2222. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  2223. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  2224. ->where($condition)->count();
  2225. $total =ceil($count/$size);
  2226. $page= $page>$total ? intval($total):$page;
  2227. $list=Db::name("invoice_return")->alias("a")
  2228. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  2229. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  2230. ->field("b.*,a.*")->withoutField("pdfstream")->json(["item"])->page($page,$size)->where($condition)->select()
  2231. ->toArray();
  2232. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2233. }
  2234. /**
  2235. * @return \think\response\Json|void
  2236. * @throws \think\db\exception\DataNotFoundException
  2237. * @throws \think\db\exception\DbException
  2238. * @throws \think\db\exception\ModelNotFoundException
  2239. */
  2240. public function tikcetList(){
  2241. $post=$this->request->only(["invNo"=>"","companyNo"=>"","relaComNo"=>"","page"=>1,"size"=>15],"post","trim");
  2242. $where=[];
  2243. $roleid = $this->roleid;
  2244. $check = checkRole($roleid,'88');
  2245. if($check){
  2246. $where[]=["b.apply_id","=",$this->uid];
  2247. }
  2248. if($post["invNo"]!="") $where[]=["a.invNo","=",$post['invNo']];
  2249. if($post["companyNo"]!="") $where[]=["b.inv_out","=",$post['companyNo']];
  2250. if($post["relaComNo"]!="") $where[]=["b.inv_out","=",$post['relaComNo']];
  2251. $count=Db::name("invoice_ticket")->where($where)->count();
  2252. $total =ceil($count/$post['size']);
  2253. $page= $post['page']>$total ? intval($total):intval($post['page']);
  2254. $list = Db::name("invoice_ticket")
  2255. ->alias("a")
  2256. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  2257. ->where($where)
  2258. ->field("a.*")
  2259. ->json(["a.item"])
  2260. ->page($page,intval($post['size']))
  2261. ->order("a.id desc")
  2262. ->select()->toArray();
  2263. foreach ($list as &$value){
  2264. $value['invtype_name'] = $this->typename[$value['inv_type']]??"";
  2265. }
  2266. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2267. }
  2268. /** @param array invNo 发票编号集合
  2269. * @return \think\response\Json|void
  2270. */
  2271. public function downorder(){
  2272. $post=$this->request->only(["invNo"=>[]],"post","trim");
  2273. $condition=[["open_type","in",[0,2,3]],["is_del","=",0],["status","=",1]];
  2274. if(!empty($post['invNo'])){
  2275. $condition[]=['invNo',"in",$post['invNo']];
  2276. }
  2277. $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
  2278. if (empty($invice)){
  2279. return error_show(1003,"未找到数据");
  2280. }
  2281. $data=[];
  2282. foreach ($invice as $value){
  2283. $temp=[];
  2284. $orderNo = Db::name("assoc")->where([["status","=",1],["is_del","=",0],['viceCode',"=",$value['invNo']]])
  2285. ->find();
  2286. if(!$orderNo){
  2287. continue;
  2288. }
  2289. $good = Db::name("qrd_info")->where([['sequenceNo',"=",$orderNo['orderCode']],["status","=",1]])
  2290. ->findOrEmpty();
  2291. if(empty($good)){
  2292. continue;
  2293. }
  2294. if(isset($value['inv_out'])&& $value['inv_out']!=''){
  2295. $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
  2296. }
  2297. $invinfo = Db::name("invoice_pool_info")->where("invNo","=",$value['invNo'])->findOrEmpty();
  2298. // foreach ($good as $val){
  2299. // $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
  2300. $temp['invNo'] = $value['invNo'];
  2301. $temp['qrdNo'] = $orderNo['orderCode'];
  2302. $temp['depart'] = $good['department'];
  2303. $temp['apply_name'] =$value['apply_name'];
  2304. $temp['code'] ="";
  2305. $temp['invoice_title'] =$invinfo['buyer_title']??"";
  2306. $temp['invoice_code'] =$invinfo['buyer_code']." ";
  2307. $temp['invoice_addr'] =$invinfo['buyer_addr']??"";
  2308. $temp['invoice_mobile'] =$invinfo['buyer_mobile']??"";
  2309. $temp['invoice_bank'] =$invinfo['buyer_bank']??"";
  2310. $temp['invoice_bankNo'] =$invinfo['buyer_bankNo']." ";
  2311. $temp['company'] =$good['customerName'];
  2312. $temp['goodname'] =$good['goodName'];
  2313. $temp['tax'] =$good['tax'];
  2314. $temp['inv_name'] =$good['goodName'];
  2315. $temp['diff_weight'] =$good['diff_weight'];
  2316. $temp['diff_price'] =$good['diff_fee'];
  2317. $temp['unit'] =$good['goodUnit'];
  2318. $temp['num'] =$good['goodNum'] - $good['thNum'];
  2319. $temp['price'] =$good['goodPrice'];
  2320. $temp['total_fee'] =$good['totalPrice'];
  2321. $temp['inv_type']=$this->invoiceType[$value['inv_type']]?? "-";
  2322. $temp['remark'] =$value['remark'];
  2323. $temp['kp_companyName'] = $invinfo['seller_title']??"";
  2324. $temp['kp_company_address'] = $invinfo['seller_addr']??"";
  2325. $temp['kp_company_license'] = $invinfo['seller_code']." ";
  2326. $temp['kp_bank_name'] = $invinfo['seller_bank']??"";
  2327. $temp['kp_bankNo'] = $invinfo['seller_bankNo']."";
  2328. $temp['kp_contector'] = $invout['contector']??"";
  2329. $temp['kp_mobile'] = $invinfo['seller_mobile']??"";
  2330. $data[]=$temp;
  2331. }
  2332. // }
  2333. if (empty($data)){
  2334. return error_show(1003,"未找到数据");
  2335. }
  2336. $header=[
  2337. "申请编号",
  2338. "公司订单号",
  2339. "申请部门(所属平台)",
  2340. "申请人",
  2341. "代码",
  2342. "发票开具全称",
  2343. "发票开具企业纳税识别号",
  2344. "发票开具企业地址",
  2345. "发票开具企业地址联系方式",
  2346. "发票开具企业收款银行",
  2347. "发票开具企业收款账户",
  2348. "分公司",
  2349. "采购商品名",
  2350. "税率",
  2351. "发票商品名",
  2352. "工差重量",
  2353. "工差金额",
  2354. "单位",
  2355. "数量",
  2356. "单价",
  2357. "总价",
  2358. "开票种类",
  2359. "备注说明",
  2360. '开票企业名称',
  2361. '开票企业地址',
  2362. '开票企业纳税号',
  2363. '开票企业银行名称',
  2364. '开票企业银行账户',
  2365. '开票企业法人',
  2366. '开票企业联系方式'
  2367. ];
  2368. excelExport("待开票",$header,$data);
  2369. }
  2370. public function importInv(){
  2371. $param =$this->request->only(["data"=>[]],"post");
  2372. if(empty($param['data']))return error_show(1004,"参数 data 不能为空");
  2373. $total=[];
  2374. $list=[];
  2375. foreach ($param['data'] as $key=>$value){
  2376. if($value['invNo']==""){
  2377. return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
  2378. }
  2379. if($value["inv_type"]===""){
  2380. return error_show(1005,"第".($key+1)."行缺少发票类型数据");
  2381. }
  2382. if(!in_array($value['inv_type'],$this->invName)) return error_show(1005,"第".($key+1)."行发票类型数据有误");
  2383. if($value["inv_code"]=="" && !in_array($value['inv_type'],[ "fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2384. return error_show(1005,"第".($key+1)."行缺少发票代码数据");
  2385. }
  2386. if($value["inv_number"]==""){
  2387. return error_show(1005,"第".($key+1)."行缺少发票号码数据");
  2388. }
  2389. if($value["inv_total"]==""&& in_array($value['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2390. return error_show(1005,"第".($key+1)."行缺少发票金额数据");
  2391. }
  2392. if($value["inv_subtotal"]==""){
  2393. return error_show(1005,"第".($key+1)."行缺少发票税后金额数据");
  2394. }
  2395. if($value["open_date"]==""){
  2396. return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
  2397. }
  2398. //电子票校验码必传
  2399. if($value["check_code"]=="" && in_array($value['inv_type'],['normal','roll',"toll",'electronic'])){
  2400. return error_show(1005,"第".($key+1)."行缺少发票校验码数据");
  2401. }
  2402. // if($value['inv_type']==="000"){
  2403. // if($value['seller_id']=="") return error_show(1005,"第".($key+1)."行缺少销售方纳税识别号数据");
  2404. // if($value['seller_title']=="") return error_show(1005,"第".($key+1)."行缺少销售方名称数据");
  2405. // if($value['seller_addr']=="") return error_show(1005,"第".($key+1)."行缺少销售方地址数据");
  2406. // if($value['seller_mobile']=="") return error_show(1005,"第".($key+1)."行缺少销售方联系方式数据");
  2407. // if($value['seller_bank']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行数据");
  2408. // if($value['seller_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行账户数据");
  2409. // if($value['buyer_id']=="") return error_show(1005,"第".($key+1)."行缺少购买方纳税识别号数据");
  2410. // if($value['buyer_title']=="") return error_show(1005,"第".($key+1)."行缺少购买方名称数据");
  2411. // if($value['buyer_addr']=="") return error_show(1005,"第".($key+1)."行缺少购买方地址数据");
  2412. // if($value['buyer_mobile']=="") return error_show(1005,"第".($key+1)."行缺少购买方联系方式数据");
  2413. // if($value['buyer_bank']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行数据");
  2414. // if($value['buyer_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行账户数据");
  2415. // if($value['tax_fee']=="") return error_show(1005,"第".($key+1)."行缺少发票税额数据");
  2416. // }
  2417. !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
  2418. $temp=[];
  2419. $temp['invNo'] = $value["invNo"];
  2420. $temp['type'] = 0;
  2421. $temp['inv_type'] =$value['inv_type'];
  2422. $temp['inv_code'] = $value['inv_code'];
  2423. $temp['inv_number'] = $value['inv_number'];
  2424. $temp['inv_subtotal'] = $value['inv_subtotal'];
  2425. $temp['inv_total'] = $value['inv_total'];
  2426. $temp['check_code'] = $value['check_code']??"";
  2427. $temp['seller_id'] = $value['seller_id']??"";
  2428. $temp['seller_title'] = $value['seller_title']??"";
  2429. $temp['seller_addr'] = $value['seller_addr']??"";
  2430. $temp['seller_mobile'] = $value['seller_mobile']??"";
  2431. $temp['seller_bank'] = $value['seller_bank']??"";
  2432. $temp['seller_bankNo'] = $value['seller_bankNo']??"";
  2433. $temp['buyer_id'] = $value['buyer_id']??"";
  2434. $temp['buyer_title'] = $value['buyer_title']??"";
  2435. $temp['buyer_addr'] = $value['buyer_addr']??"";
  2436. $temp['buyer_mobile'] = $value['buyer_mobile']??"";
  2437. $temp['buyer_bank'] = $value['buyer_bank']??"";
  2438. $temp['buyer_bankNo'] = $value['buyer_bankNo']??"";
  2439. $temp['tax_fee'] = $value['tax_fee']??"";
  2440. $temp['item'] = '';
  2441. $temp['pdfstream'] = '';
  2442. $temp['open_date'] = date("Y-m-d H:i:s",strtotime($value['open_date']));
  2443. $temp['status'] = 0;
  2444. $temp['is_del'] = 0;
  2445. $temp['addtime'] = date("Y-m-d H:i:s");
  2446. $temp['updatetime'] = date("Y-m-d H:i:s");
  2447. $total[$value["invNo"]]=$temp;
  2448. }
  2449. Db::startTrans();
  2450. try{
  2451. $keyarr =array_keys($total);
  2452. $inpool = Db::name("invoice_pool")->where([["invNo","in",$keyarr],["open_type","in",[0,2,3]],["is_del","=",0],["status","=",2]])->column("winv_fee,invNo,inv_value,inv_type","invNo");
  2453. foreach ($total as $key=>$val){
  2454. if(!isset($inpool[$key])) throw new Exception("{$key}发票数据不存在或状态有误");
  2455. if(in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"]) && $inpool[$key]['winv_fee']!=$val['total']){
  2456. throw new Exception("{$key}全电发票面金额小于未开票金额");
  2457. }
  2458. if($inpool[$key]['inv_type']!= $val['inv_type']) throw new Exception("{$key}发票类型有误");
  2459. $update=[
  2460. "status"=>in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])?4:3,
  2461. "open_type"=>$inpool[$key]['open_type']??3,
  2462. "updatetime"=>date("Y-m-d H:i:s")
  2463. ];
  2464. $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save($update);
  2465. if(!$up){
  2466. Db::rollback();
  2467. return error_show(1005,"发票状态更新失败");
  2468. }
  2469. if($update['status']==4){
  2470. $qrdArr=Db::name("assoc")->where(["viceCode"=>$key,"is_del"=>0])->column("id,orderCode,cancel_fee");
  2471. if(!empty($qrdArr)){
  2472. foreach ($qrdArr as $value){
  2473. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  2474. if($qrdinfo==false){
  2475. Db::rollback();
  2476. return error_show(1003,"确认单信息未找到");
  2477. }
  2478. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  2479. Db::rollback();
  2480. return error_show(1003,"确认单信息开票金额不足");
  2481. }
  2482. $update =[
  2483. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  2484. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  2485. "invtime"=>date("Y-m-d H:i:s"),
  2486. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  2487. "updatetime"=>date("Y-m-d H:i:s"),
  2488. ];
  2489. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  2490. if($qrdup==false){
  2491. Db::rollback();
  2492. return error_show(1003,"确认单信息更新失败");
  2493. }
  2494. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  2495. $assocup =Db::name("assoc")->where($value)->update($assoc);
  2496. if($assocup==false){
  2497. Db::rollback();
  2498. return error_show(1003,"确认单关联信息更新失败");
  2499. }
  2500. }
  2501. }
  2502. }
  2503. $val['inv_type'] = $this->kingInvoice[$val['inv_type']];
  2504. $list[]=$val;
  2505. }
  2506. $inticket =Db::name("invoice_ticket")->insertAll($list);
  2507. if(!$inticket){
  2508. Db::rollback();
  2509. return error_show(1005,"发票数据录入失败");
  2510. }
  2511. Db::commit();
  2512. return app_show(0,"发票数据录入成功");
  2513. }catch (\Exception $e){
  2514. Db::rollback();
  2515. return error_show(1008,$e->getMessage());
  2516. }
  2517. }
  2518. //金税开票列表
  2519. public function InvInterfaceList(){
  2520. $param=$this->request->param(["page"=>1,"size"=>15,"kpfs"=>"","kplx"=>"","invNo"=>"","companyNo"=>"",
  2521. "customerName"=>"","fphm"=>"","fplxdm"=>"","status"=>""],"post","trim");
  2522. $where=[];
  2523. $model = new InvoiceInterface();
  2524. $param['kpfs']==''?:$where[]=['KPFS','=',$param['kpfs']];
  2525. $param['kplx'] == ''?:$where[]=['KPLX','=',$param['kplx']];
  2526. $param['fplxdm'] == ''?:$where[]=['FPLXDM','=',$param['fplxdm']];
  2527. $param['status'] === ''?:$where[]=['status','=',$param['status']];
  2528. $param['invNo'] == ''?:$where[]=['invNo','like',"%{$param['invNo']}%"];
  2529. if($param['companyNo']!=''){
  2530. $sbh = Db::name('company_info')->where('companyNo',$param['companyNo'])->value('company_license','');
  2531. $where[]=['XHFSBH','=',$sbh];
  2532. }
  2533. $param['customerName'] == ''?:$where[]=['GMFMC','like',"%{$param['customerName']}%"];
  2534. $param['fphm'] == ''?:$where[]=['YFPHM','like',"%{$param['fphm']}%"];
  2535. $list = $model->where($where)
  2536. ->field("id,invNo,KPLX,KPFS,FPLXDM,XHFMC,XHFSBH,GMFSBH,GMFMC,KPR,SKR,FHR,YFPDM,YFPHM,YFPLX,YFPKPRQ,JSHJ,
  2537. HJJE,HJSE,status,remark,updatetime,addtime")->order("id desc")->paginate(["list_rows"=>$param["size"],"page"=>$param['page']]);
  2538. return app_show(0,'获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  2539. }
  2540. //金税开票列表
  2541. public function InvInterfaceCheck(){
  2542. $param=$this->request->param(["id"=>"","status"=>""],'post','trim');
  2543. $model = new InvoiceInterface();
  2544. $valid=Validate::rule([
  2545. "id|数据信息id"=>"require|number|gt:0",
  2546. "status|状态"=>"require|number|in:0,1"
  2547. ]);
  2548. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  2549. $info= $model->findOrEmpty($param['id']);
  2550. if($info->isEmpty())return error_show(1004,"未找到数据");
  2551. if(in_array($info->status,[0,1,2]))return error_show(1004,'数据处理中');
  2552. $info->status=$param['status'];
  2553. $info->is_check=0;
  2554. if($info->status==0){
  2555. $info->DDQQPCH =randomkeys(26).date('YmdHis');
  2556. }
  2557. $up= $info->save();
  2558. return $up? app_show(0,"修改成功"):error_show(1004,"修改失败");
  2559. }
  2560. }