OrderInv.php 120 KB

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