OrderInv.php 122 KB

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