OrderInv.php 113 KB

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