OrderInv.php 114 KB

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