OrderInv.php 122 KB

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