OrderInv.php 120 KB

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