OrderInv.php 115 KB

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