OrderInv.php 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;
  5. use think\App;
  6. use think\Exception;use think\facade\Config;
  7. use think\facade\Db;
  8. use think\facade\Cache;
  9. class OrderInv extends BaseController{
  10. public function __construct(App $app) {
  11. parent::__construct($app);
  12. }
  13. /**
  14. * 新建销售单开票申请
  15. */
  16. public function create(){
  17. $companyType = isset($this->post['companyType'])&&$this->post['companyType']!=='' ? trim($this->post['companyType']):"";
  18. if ($companyType==''){
  19. return error_show(1004,"参数 companyType 不能为空");
  20. }
  21. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  22. if($buy_id==''){
  23. return error_show(1004,"参数 buy_id 不能为空");
  24. }
  25. $buyinfo =Db::name("customer_invoice")->where(['id'=>2054,"is_del"=>0])->find();
  26. if($buyinfo==false){
  27. return error_show(1004,"购买方发票信息未找到");
  28. }
  29. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  30. if($companyNo==''){
  31. return error_show(1004,"参数 companyNo 不能为空");
  32. }
  33. $company = Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->find();
  34. if($company==false){
  35. return error_show(1004,"销售方信息未找到");
  36. }
  37. // $khNo = isset($this->post['khNo'])&&$this->post['khNo']!=='' ? trim($this->post['khNo']):"";
  38. // if($khNo==''){
  39. // return error_show(1004,"参数 khNo 不能为空");
  40. // }
  41. // $customer = Db::name("customer_info")->where(["companyNo"=>$khNo])->find();
  42. // if($customer==false){
  43. // return error_show(1004,"客户信息未找到");
  44. // }
  45. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? intval($this->post['invtype']):"";
  46. if($invtype==''){
  47. return error_show(1004,"参数 invtype 不能为空");
  48. }
  49. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  50. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  51. $exam_remark = isset($this->post['exam_remark'])&&$this->post['exam_remark']!=='' ? trim($this->post['exam_remark']):"";
  52. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  53. if(empty($orderArr)){
  54. return error_show(1004,"参数 orderArr 不能为空");
  55. }
  56. $temp =[];
  57. $good=[];
  58. $invNo=makeNo("INV");
  59. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  60. Db::startTrans();
  61. try{
  62. foreach ($orderArr as $value){
  63. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  64. Db::rollback();
  65. return error_show(1004,"参数 sequenceNo 不能为空");
  66. }
  67. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  68. if($qrd['status']==2){
  69. Db::rollback();
  70. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  71. }
  72. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  73. Db::rollback();
  74. return error_show(1004,"参数 inv_fee 不能为空");
  75. }
  76. if($qrd['winv_fee']<$value['inv_fee']){
  77. Db::rollback();
  78. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  79. }
  80. $good_info = Db::name("good")->where(["spuCode"=>$qrd["goodNo"],"status"=>1])->findOrEmpty();
  81. if(empty($good_info)){
  82. Db::rollback();
  83. return error_show(1004,"确认单{$value['sequenceNo']}商品未确认开票类目");
  84. }
  85. $num = DB::name("assoc")->alias("a")->leftJoin("invoice_good b","a.viceCode=b.invNo")
  86. ->where(["a.status"=>[1,2],"a.is_del"=>0,"a.type"=>1,"a.orderCode"=>$value['sequenceNo']])->sum("b.goodNum");
  87. if($qrd['goodNum']-$num<$value['num']){
  88. Db::rollback();
  89. return error_show(1004,"确认单{$value['sequenceNo']}待开票数量不足");
  90. }
  91. $ascc=[
  92. "assocNo"=>makeNo("AS"),
  93. "apply_id"=>$this->uid,
  94. "apply_name"=>$this->uname,
  95. "type"=>1,
  96. "orderCode"=>$value['sequenceNo'],
  97. "customerNo"=>$qrd['customerNo'],
  98. "viceCode"=>$invNo,
  99. "order_total"=>$qrd['totalPrice'],
  100. "vice_total"=>$invfee,
  101. "cancel_fee"=>$value['inv_fee'],
  102. "status"=>1,
  103. "addtime"=>date("Y-m-d H:i:s"),
  104. "updatetime"=>date("Y-m-d H:i:s")
  105. ];
  106. $invoice_good=[
  107. "invNo"=>$invNo,
  108. "orderCode"=>$value['sequenceNo'],
  109. "goodNo"=>$qrd['goodNo'],
  110. "goodName"=>$good_info['inv_good_name'],
  111. "catName"=>$good_info['inv_cat_name'],
  112. "catNo"=>$good_info['inv_cat_code'],
  113. "goodPrice"=>round($value['inv_fee']/$value['num'],8),
  114. "unitName"=>$qrd['goodUnit'],
  115. "goodNum"=>$value['num'],
  116. "specInfo"=>$value['sequenceNo'],
  117. "totalPrice"=>$value['inv_fee'],
  118. "tax"=>$good_info['inv_tax'],
  119. "is_discount"=>$good_info['is_discount'],
  120. "addTax"=>$good_info['addTax'],
  121. "inv_tag"=>$good_info['inv_tag'],
  122. "taxPrice"=>round($value['inv_fee']*$good_info['inv_tax'],2),
  123. "addtime"=>date("Y-m-d H:i:s"),
  124. "updatetime"=>date("Y-m-d H:i:s")
  125. ];
  126. $update = [
  127. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  128. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  129. "inv_status"=>2,
  130. "status"=>1,
  131. "updatetime"=>date("Y-m-d H:i:s")
  132. ];
  133. $up = Db::name("qrd_info")->where($qrd)->update($update);
  134. if($up==false){
  135. Db::rollback();
  136. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  137. }
  138. $temp[]=$ascc;
  139. $good[]=$invoice_good;
  140. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  141. if($report)$report->setField("invNo",$invNo);
  142. }
  143. $associn = Db::name("assoc")->insertAll($temp);
  144. if($associn==0){
  145. Db::rollback();
  146. return error_show(1005,"确认单申请开票失败");
  147. }
  148. $goodin = Db::name("invoice_good")->insertAll($good);
  149. if($goodin==0){
  150. Db::rollback();
  151. return error_show(1005,"确认单申请开票失败");
  152. }
  153. $inv=[
  154. "invNo"=>$invNo,
  155. "inv_value"=>$invfee,
  156. "inv_out"=> $companyNo,
  157. "inv_company"=> $company['company_name'],
  158. "apply_id"=>$this->uid,
  159. "apply_name"=>$this->uname,
  160. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通
  161. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  162. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  163. "company_type"=> $buyinfo['company_type'], //类型01 企业,02 机关事业单位,03 个人, 04 其他
  164. "remark"=>$remark, //申请备注
  165. "exam_remark"=>$exam_remark, //审核备注
  166. "ainv_fee"=>0,//已核销金额
  167. "winv_fee"=>$invfee,//未核销金额
  168. "status"=>0,// 待财务开票/待金税开票 待财务审核 待填写物流 开票完成 开票失败/开票驳回 财务驳回
  169. "is_del"=>0,
  170. "email"=>$email,
  171. "addtime"=>date("Y-m-d H:i:s"),
  172. "updatetime"=>date("Y-m-d H:i:s")
  173. ];
  174. $invin = Db::name("invoice_pool")->insert($inv);
  175. if($invin){
  176. $invinfo=[
  177. "buyer_title"=>$buyinfo['invoice_title'],
  178. "buyer_code"=>$buyinfo['invoice_code'],
  179. "buyer_addr"=>$buyinfo['invoice_addr'],
  180. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  181. "buyer_bank"=>$buyinfo['invoice_bank'],
  182. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  183. "seller_title"=>$company['company_name'],
  184. "seller_code"=>$company['company_license'],
  185. "seller_addr"=>$company['company_address'],
  186. "seller_mobile"=>$company['mobile'],
  187. "seller_bank"=>$company['bank_name'],
  188. "seller_bankNo"=>$company['bankNo'],
  189. "invNo"=>$invNo,
  190. "voider"=>$company['voider'],
  191. "payee"=>$company['payee'],
  192. "drawer"=>$company['drawer'],
  193. "reviewer"=>$company['reviewer'],
  194. "ownerPlace"=>$company['ownerPlace'],
  195. "addtime"=>date("Y-m-d H:i:s")
  196. ];
  197. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  198. if($poolinfo){
  199. Db::commit();
  200. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  201. }
  202. }
  203. Db::rollback();
  204. return error_show(1004,"确认单发票申请失败");
  205. }catch (\Exception $e){
  206. Db::rollback();
  207. return error_show(1004,$e->getMessage());
  208. }
  209. }
  210. // 0 待财务开票/待金税开票 1待财务审核 2待填写物流 3开票完成 4开票失败/开票驳回 5财务驳回 6退票 7金税开票中 8 电子票待确认
  211. public function status(){
  212. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  213. if($invNo==""){
  214. return error_show(1004,"参数 invNo 不能为空");
  215. }
  216. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  217. if($invinfo==false){
  218. return error_show(1004,"发票申请数据未找到");
  219. }
  220. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  221. if($status===''){
  222. return error_show(1004,"参数 status 不能为空");
  223. }
  224. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  225. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  226. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  227. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  228. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  229. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  230. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  231. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  232. if($status==1){
  233. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  234. if($open_type!=1){
  235. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  236. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  237. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  238. if($invinfo['inv_type']==2 || $invinfo['inv_type']==3){
  239. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  240. }
  241. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  242. $status=7;
  243. }else{
  244. $status=7;
  245. }
  246. }
  247. $update=[
  248. "status"=> $status==2 && ($invinfo['inv_type']==3 || $invinfo['inv_type']==4) ? 3 : $status,
  249. "exam_remark"=>$remark,
  250. "updatetime" => date("Y-m-d H:i:s")
  251. ];
  252. $status==1 ? $update['open_type']=$open_type :"";
  253. Db::startTrans();
  254. try{
  255. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  256. if($invup){
  257. if($status==7&& $open_type!=1){
  258. $invpool=[
  259. "invNo"=>$invinfo['invNo'],
  260. "inv_type"=>invoiceType($invinfo['inv_type']),
  261. "inv_code"=>$invCode,
  262. "inv_number"=>$invNum,
  263. "check_code"=>$checkCode,
  264. "inv_total"=>0,
  265. "inv_subtotal"=>$subtotal_fee,
  266. "open_date"=>$open_date,
  267. 'pdfstream'=>'',
  268. "item"=>'',
  269. "status"=>0,
  270. "addtime"=>date("Y-m-d H:i:s"),
  271. "updatetime"=>date("Y-m-d H:i:s")
  272. ];
  273. $in = Db::name("invoice_ticket")->insert($invpool);
  274. if($in==false){
  275. Db::rollback();
  276. return error_show(1004,"发票信息添加失败");
  277. }
  278. }
  279. if($status==7 && $open_type==1){
  280. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  281. if(empty($invpoolinfo)){
  282. Db::rollback();
  283. return error_show(1004,"开票信息未找到");
  284. }
  285. $goodinfo =Db::name("invoice_good")
  286. ->where(["invNo"=>$invNo,"is_del"=>0])
  287. ->field("0 as FPHXZ,
  288. catNo as SPBM,
  289. id as ZXBM,
  290. is_discount as YHZCBS,
  291. if(inv_tag=0,'',inv_tag) as LSLBS,
  292. addTax as ZZSTSGL,
  293. goodName as XMMC,
  294. specInfo as GGXH,
  295. unitName as DW,
  296. goodNum as SPSL,
  297. goodPrice as DJ,
  298. totalPrice as JE,
  299. 1 HSBZ,
  300. tax as SL,
  301. taxPrice as SE")
  302. ->select()->toJson();
  303. if(empty($goodinfo)){
  304. Db::rollback();
  305. return error_show(1004,"开票商品信息未找到");
  306. }
  307. $data=[
  308. "invNo"=>$invNo,
  309. "KPLX"=>"0",
  310. "XHFSBH"=>$invpoolinfo['seller_code'],
  311. "XHFMC"=>$invpoolinfo['seller_title'],
  312. "XHFDZ"=>$invpoolinfo['seller_addr'],
  313. "XHFDH"=>$invpoolinfo['seller_mobile'],
  314. "XHFYH"=>$invpoolinfo['seller_bank'],
  315. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  316. "GMFBM"=>"",
  317. "GMFSBH"=>$invpoolinfo['buyer_code'],
  318. "GMFMC"=>$invpoolinfo['buyer_title'],
  319. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  320. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  321. "GMFYH"=>$invpoolinfo['buyer_bank'],
  322. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  323. "GMFLX"=>$invinfo['company_type'],
  324. "GMFSJH"=>"",
  325. "GMFDZYX"=>$invinfo['email'],
  326. "KPR"=>$invpoolinfo['drawer'],
  327. "SKR"=>$invpoolinfo['payee'],
  328. "FHR"=>$invpoolinfo['reviewer'],
  329. "QDBZ"=>"0",
  330. "JSHJ"=>$invinfo['inv_value'],
  331. "HJJE"=>"0",
  332. "HJSE"=>"0",
  333. "BZ"=>$invinfo['remark'],
  334. "DDMXXX"=>$goodinfo,
  335. "FPLXDM"=>invoiceType($invinfo['inv_type']),
  336. "DDQQPCH"=>"",
  337. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  338. "status"=>"0",
  339. "result"=>"",
  340. "addtime"=>date("Y-m-d H:i:s"),
  341. "updatetime"=>date("Y-m-d H:i:s")
  342. ];
  343. $invoice=Db::name("invoice_interface")->insert($data);
  344. if($invoice==false){
  345. Db::rollback();
  346. return error_show(1003,"开票信息生成失败");
  347. }
  348. Cache::push("invoiceList",$data["DDQQLSH"]);
  349. }
  350. if( $update['status']==3){
  351. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  352. if(!empty($qrdArr)){
  353. foreach ($qrdArr as $value){
  354. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  355. if($qrdinfo==false){
  356. Db::rollback();
  357. return error_show(1003,"确认单信息未找到");
  358. }
  359. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  360. Db::rollback();
  361. return error_show(1003,"确认单信息开票金额不足");
  362. }
  363. $update =[
  364. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  365. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  366. "invtime"=>date("Y-m-d H:i:s"),
  367. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  368. "updatetime"=>date("Y-m-d H:i:s"),
  369. ];
  370. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  371. if($qrdup==false){
  372. Db::rollback();
  373. return error_show(1003,"确认单信息更新失败");
  374. }
  375. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  376. $assocup =Db::name("assoc")->where($value)->update($assoc);
  377. if($assocup==false){
  378. Db::rollback();
  379. return error_show(1003,"确认单关联信息更新失败");
  380. }
  381. }
  382. }
  383. }
  384. if($status==4 ||$status==5 ){
  385. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  386. if(!empty($qrdArr)){
  387. foreach ($qrdArr as $value){
  388. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  389. if($qrdinfo==false){
  390. Db::rollback();
  391. return error_show(1003,"确认单信息未找到");
  392. }
  393. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  394. Db::rollback();
  395. return error_show(1003,"确认单信息开票金额不足");
  396. }
  397. $update =[
  398. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  399. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  400. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  401. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  402. "updatetime"=>date("Y-m-d H:i:s"),
  403. ];
  404. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  405. if($qrdup==false){
  406. Db::rollback();
  407. return error_show(1003,"确认单信息更新失败");
  408. }
  409. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  410. $assocup =Db::name("assoc")->where($value)->update($assoc);
  411. if($assocup==false){
  412. Db::rollback();
  413. return error_show(1003,"确认单关联信息更新失败");
  414. }
  415. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  416. if($report)$report->rmField("invNo",$invinfo['invNo']);
  417. }
  418. }
  419. }
  420. Db::commit();
  421. return app_show(0,"审核成功");
  422. }
  423. Db::rollback();
  424. return error_show(1004,"审核失败");
  425. }catch (\Exception $e){
  426. Db::rollback();
  427. return error_show(1004,$e->getMessage());
  428. }
  429. }
  430. /**
  431. * 发票设置物流
  432. */
  433. public function SetPost(){
  434. // @param invNo postCompany postCode postFee;
  435. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  436. if($invNo==''){
  437. return error_show(1004,"参数invNo不能为空");
  438. }
  439. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  440. if($invinfo==false){
  441. return error_show(1004,"发票申请信息未找到");
  442. }
  443. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  444. if($post_company==''){
  445. return error_show(1004,"参数 post_company 不能为空");
  446. }
  447. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  448. if($post_code==''){
  449. return error_show(1004,"参数 post_code 不能为空");
  450. }
  451. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floatval($this->post['post_fee']):"0";
  452. Db::startTrans();
  453. try{
  454. $data=[
  455. "post_company"=>$post_company,
  456. "post_code"=>$post_code,
  457. "post_fee"=>$post_fee,
  458. "status"=>3,
  459. "updatetime"=>date("Y-m-d H:i:s")
  460. ];
  461. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  462. if($up){
  463. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  464. if(!empty($qrdArr)){
  465. foreach ($qrdArr as $value){
  466. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  467. if($qrdinfo==false){
  468. Db::rollback();
  469. return error_show(1003,"确认单信息未找到");
  470. }
  471. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  472. Db::rollback();
  473. return error_show(1003,"确认单信息开票金额不足");
  474. }
  475. $update =[
  476. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  477. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  478. "invtime"=>date("Y-m-d H:i:s"),
  479. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  480. "updatetime"=>date("Y-m-d H:i:s"),
  481. ];
  482. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  483. if($qrdup==false){
  484. Db::rollback();
  485. return error_show(1003,"确认单信息更新失败");
  486. }
  487. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  488. $assocup =Db::name("assoc")->where($value)->update($assoc);
  489. if($assocup==false){
  490. Db::rollback();
  491. return error_show(1003,"确认单关联信息更新失败");
  492. }
  493. }
  494. }
  495. Db::commit();
  496. return app_show(0,"更新成功");
  497. }
  498. Db::rollback();
  499. return error_show(1003,"发票申请信息更新失败");
  500. }catch (\Exception $e){
  501. Db::rollback();
  502. return error_show(1004,$e->getMessage());
  503. }
  504. }
  505. /**
  506. * 发票废弃使用
  507. */
  508. public function Discard(){
  509. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  510. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  511. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  512. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  513. if(!in_array($invinfo['status'],[1,2,3,5]))return error_show(1004,"发票状态无法废弃");
  514. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  515. if($company==false)return error_show(1004,"开票业务公司未找到");
  516. $invoiceinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  517. if($invoiceinfo==false)return error_show(1004,"发票详细信息未找到");
  518. Db::startTrans();
  519. try{
  520. $invoice=Config::get("invoice");
  521. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  522. $result=$tax->DeprecateInvoices($company['company_license'],invoiceType($invoiceinfo['inv_type']),$invoiceinfo['inv_code'],$invoiceinfo['inv_number'],1);
  523. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  524. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  525. if($up){
  526. if($invinfo['status']!=5){
  527. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  528. if(!empty($qrdArr)){
  529. foreach ($qrdArr as $value){
  530. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  531. if($qrdinfo==false){
  532. Db::rollback();
  533. return error_show(1003,"确认单信息未找到");
  534. }
  535. if ($invinfo['status']==3){
  536. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  537. Db::rollback();
  538. return error_show(1003,"确认单信息开票金额不足");
  539. }
  540. $update =[
  541. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  542. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  543. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  544. "updatetime"=>date("Y-m-d H:i:s"),
  545. ];
  546. }else{
  547. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  548. Db::rollback();
  549. return error_show(1003,"确认单信息开票金额不足");
  550. }
  551. $update =[
  552. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  553. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  554. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  555. "updatetime"=>date("Y-m-d H:i:s"),
  556. ];
  557. }
  558. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  559. if($qrdup==false){
  560. Db::rollback();
  561. return error_show(1003,"确认单信息更新失败");
  562. }
  563. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  564. $assocup =Db::name("assoc")->where($value)->update($assoc);
  565. if($assocup==false){
  566. Db::rollback();
  567. return error_show(1003,"确认单关联信息更新失败");
  568. }
  569. }
  570. }
  571. }
  572. Db::commit();
  573. return app_show(0,"发票废弃成功");
  574. }
  575. }
  576. Db::rollback();
  577. return error_show(1004,"发票废弃失败");
  578. }catch (\Exception $e){
  579. Db::rollback();
  580. return error_show(1004,$e->getMessage());
  581. }
  582. }
  583. /**@param invNo return_reason remark 退票申请
  584. * @return \think\response\Json|void
  585. */
  586. public function ReturnAdd(){
  587. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  588. if($invNo==""){
  589. return error_show(1004,"参数 invNo 不能为空");
  590. }
  591. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  592. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  593. if($return_reason==""){
  594. return error_show(1004,"参数 return_reason 不能为空");
  595. }
  596. if($remark==""){
  597. return error_show(1004,"参数 remark 不能为空");
  598. }
  599. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  600. if($invinfo==false){
  601. return error_show(1004,"发票申请数据未找到");
  602. }
  603. if($invinfo['status']!=3){
  604. return error_show(1004,"发票申请未完成");
  605. }
  606. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  607. if(empty($qrd)){
  608. return error_show(1004,"未找到开票的销售单信息");
  609. }
  610. $returnCode = makeNo("RIN");
  611. $data=[
  612. "returnCode"=>$returnCode,
  613. "invNo"=>$invNo,
  614. "reason"=>$return_reason,
  615. "remark"=>$remark,
  616. "status"=>0,
  617. "apply_id"=>$this->uid,
  618. "apply_name"=>$this->uname,
  619. "addtime"=>date("Y-m-d H:i:s"),
  620. "updatetime"=>date("Y-m-d H:i:s")
  621. ];
  622. Db::startTrans();
  623. try{
  624. $inter = Db::name("invoice_return")->insert($data);
  625. if($inter){
  626. foreach ($qrd as $value){
  627. $report=ReportCode::where(["qrdNo"=>$value])->find();
  628. if($report)$report->setField("returnInv",$returnCode);
  629. }
  630. Db::commit();
  631. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  632. }else{
  633. Db::rollback();
  634. return error_show(1005,"退票申请新建失败");
  635. }
  636. }catch (\Exception $e){
  637. Db::rollback();
  638. return error_show(1006,$e->getMessage());
  639. }
  640. }
  641. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败 5 退票进行中
  642. public function ReturnStatus(){
  643. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  644. if($returnCode==""){
  645. return error_show(1005,"参数 returnCode 不能为空");
  646. }
  647. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  648. if ($status==""){
  649. return error_show(1005,"参数 status 不能为空");
  650. }
  651. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):"";
  652. if ($return_type==""){
  653. return error_show(1005,"参数 return_type 不能为空");
  654. }
  655. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  656. if($remark==""){
  657. return error_show(1005,"参数 remark 不能为空");
  658. }
  659. $return_reason =isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  660. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  661. if($return==false){
  662. return error_show(1005,"退票申请信息未找到");
  663. }
  664. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  665. if($invinfo==false){
  666. return error_show(1004,"发票申请数据未找到");
  667. }
  668. if($invinfo['status']!=3){
  669. return error_show(1004,"发票申请未完成");
  670. }
  671. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  672. if(empty($qrd)){
  673. return error_show(1004,"未找到开票的销售单信息");
  674. }
  675. $ticket = Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"type"=>0,"status"=>1,"is_del"=>0])
  676. ->findOrEmpty();
  677. if(empty($ticket)){
  678. return error_show(1004,"未找到开票的票面信息");
  679. }
  680. if($return_type==1){
  681. $red =0;
  682. if($ticket["inv_type"]=='004' || $ticket["inv_type"]=='028'){
  683. $red =1;
  684. }
  685. }else{
  686. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  687. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  688. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  689. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  690. // $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floatval($this->post['total_fee']):"";
  691. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  692. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  693. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  694. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  695. if($invinfo['inv_type']==2 || $invinfo['inv_type']==3){
  696. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  697. }
  698. // if($total_fee==='') return error_show(1004,"参数 tatal_fee 不能为空");
  699. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  700. }
  701. Db::startTrans();
  702. try{
  703. $update=["status"=>$status, "return_reason"=>$return_reason,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  704. $up =Db::name("invoice_return")->where($return)->update($update);
  705. if($up){
  706. if($status==1){
  707. if($return_type==1){
  708. if($red==0){
  709. $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'], $returnCode);
  710. }
  711. if($red==1){
  712. if(date("Y-m",strtotime($ticket["open_date"]))==date("Y-m")){
  713. $this->addDiscard($ticket,$returnCode);
  714. }else{
  715. $this->addApplyRed($ticket,$returnCode);
  716. }
  717. }
  718. }else{
  719. $invpool=[
  720. "invNo"=>$invinfo['invNo'],
  721. "type"=>1,
  722. "inv_type"=>$ticket['inv_type'],
  723. "inv_code"=>$invCode,
  724. "inv_number"=>$invNum,
  725. "check_code"=>$checkCode,
  726. "inv_total"=>0,
  727. "inv_subtotal"=>$subtotal_fee,
  728. "open_date"=>$open_date,
  729. "item"=>'',
  730. 'pdfstream'=>'',
  731. "status"=>0,
  732. "addtime"=>date("Y-m-d H:i:s"),
  733. "updatetime"=>date("Y-m-d H:i:s")
  734. ];
  735. $in = Db::name("invoice_ticket")->insert($invpool);
  736. if($in==false){
  737. Db::rollback();
  738. return error_show(1004,"发票信息添加失败");
  739. }
  740. }
  741. }
  742. if($status==2){
  743. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  744. if(!empty($qrdArr)){
  745. foreach ($qrdArr as $value){
  746. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  747. if($qrdinfo==false){
  748. Db::rollback();
  749. return error_show(1003,"确认单信息未找到");
  750. }
  751. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  752. Db::rollback();
  753. return error_show(1003,"确认单信息开票金额不足");
  754. }
  755. $update =[
  756. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  757. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  758. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  759. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  760. "updatetime"=>date("Y-m-d H:i:s"),
  761. ];
  762. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  763. if($qrdup==false){
  764. Db::rollback();
  765. return error_show(1003,"确认单信息更新失败");
  766. }
  767. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  768. $assocup =Db::name("assoc")->where($value)->update($assoc);
  769. if($assocup==false){
  770. Db::rollback();
  771. return error_show(1003,"确认单关联信息更新失败");
  772. }
  773. }
  774. }
  775. }
  776. if($status==3 || $status==4){
  777. foreach ($qrd as $value){
  778. $report=ReportCode::where(["qrdNo"=>$value])->find();
  779. if($report)$report->rmField("returnInv",$returnCode);
  780. }
  781. }
  782. Db::commit();
  783. return app_show(0,"退票申请信息更新成功");
  784. }
  785. Db::rollback();
  786. return error_show(1005,"退票申请信息更新失败");
  787. }catch (\Exception $e){
  788. Db::rollback();
  789. return error_show(1005,$e->getMessage());
  790. }
  791. }
  792. //红冲票
  793. public function returnRed(){
  794. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  795. if($returnCode==""){
  796. return error_show(1005,"参数 returnCode 不能为空");
  797. }
  798. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  799. if($return==false){
  800. return error_show(1005,"退票申请信息未找到");
  801. }
  802. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  803. if($redinv==""){
  804. return error_show(1005,"参数 redinv 不能为空");
  805. }
  806. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  807. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  808. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  809. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  810. if($invinfo==false){
  811. return error_show(1005,"发票申请信息未找到");
  812. }
  813. Db::startTrans();
  814. try{
  815. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  816. $up =Db::name("invoice_return")->where($return)->update($update);
  817. if($up){
  818. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  819. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  820. if($inv){
  821. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  822. if(!empty($qrdArr)){
  823. foreach ($qrdArr as $value){
  824. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  825. if($qrdinfo==false){
  826. Db::rollback();
  827. return error_show(1003,"确认单信息未找到");
  828. }
  829. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  830. Db::rollback();
  831. return error_show(1003,"确认单信息开票金额不足");
  832. }
  833. $update =[
  834. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  835. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  836. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  837. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  838. "updatetime"=>date("Y-m-d H:i:s"),
  839. ];
  840. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  841. if($qrdup==false){
  842. Db::rollback();
  843. return error_show(1003,"确认单信息更新失败");
  844. }
  845. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  846. $assocup =Db::name("assoc")->where($value)->update($assoc);
  847. if($assocup==false){
  848. Db::rollback();
  849. return error_show(1003,"确认单关联信息更新失败");
  850. }
  851. }
  852. }
  853. Db::commit();
  854. return app_show(0,"退票申请信息更新成功");
  855. }
  856. }
  857. Db::rollback();
  858. return error_show(1005,"退票申请信息更新失败");
  859. }catch (\Exception $e){
  860. Db::rollback();
  861. return error_show(1005,$e->getMessage());
  862. }
  863. }
  864. //退票详情
  865. public function returnInfo(){
  866. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  867. if($returnCode==""){
  868. return error_show(1005,"参数 returnCode 不能为空");
  869. }
  870. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  871. if($return==false){
  872. return error_show(1005,"退票申请信息未找到");
  873. }
  874. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  875. $return['inv_out']= $invinfo['inv_out']??"";
  876. $return['inv_value']= $invinfo['inv_value']??"";
  877. $return['inv_apply_id']= $invinfo['apply_id']??"";
  878. $return['inv_apply_name']= $invinfo['apply_name']??"";
  879. $return['inv_type']= $invinfo['inv_type']??"";
  880. $return['open_type']= $invinfo['open_type']??"";
  881. $return['is_ticket']= $invinfo['is_ticket']??"";
  882. $return['email']= $invinfo['email']??"";
  883. $return['post_code']= $invinfo['post_code']??"";
  884. $return['post_company']= $invinfo['post_company']??"";
  885. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  886. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  887. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  888. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  889. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  890. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  891. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  892. $return['seller_title'] = $poolinfo['seller_title']??'';
  893. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  894. $return['seller_code'] = $poolinfo['seller_code']??'';
  895. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  896. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  897. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  898. $return['voider'] = $poolinfo['voider']??'';
  899. $return['payee'] = $poolinfo['payee']??'';
  900. $return['drawer'] = $poolinfo['drawer']??'';
  901. $return['reviewer'] = $poolinfo['reviewer']??'';
  902. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  903. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  904. $return['goodList']=$orderList;
  905. return app_show(0,"获取成功",$return);
  906. }
  907. /**
  908. *发票池列表
  909. */
  910. public function list(){
  911. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  912. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  913. $condition=[["a.is_del","=",0]];
  914. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  915. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  916. if($start!=""){
  917. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  918. }
  919. if($end!=""){
  920. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  921. }
  922. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  923. if($status!==""){
  924. $condition[]=["a.status","=",$status];
  925. }
  926. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  927. if($inv_type!==""){
  928. $condition[]=["a.inv_type","=",$inv_type];
  929. }
  930. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  931. if($inv_out!==""){
  932. $condition[]=["a.inv_out","=",$inv_out];
  933. }
  934. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  935. // if($inv_in!==""){
  936. // $condition[]=["a.inv_in","=",$inv_in];
  937. // }
  938. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  939. if($invNo!==""){
  940. $condition[]=["a.invNo","like","%$invNo%"];
  941. }
  942. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  943. if($inv_number!==""){
  944. $condition[]=["c.inv_number","like","%$inv_number%"];
  945. }
  946. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  947. if($inv_code!==""){
  948. $condition[]=["c.inv_code","like","%$inv_code%"];
  949. }
  950. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  951. if($inv_company!==""){
  952. $condition[]=["a.inv_company","like","%$inv_company%"];
  953. }
  954. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  955. if($buyer_name!==""){
  956. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  957. }
  958. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  959. if($apply_id!==""){
  960. $condition[]=["a.apply_id","=",$apply_id];
  961. }
  962. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  963. if($apply_name!==""){
  964. $condition[]=["a.apply_name","like","%$apply_name%"];
  965. }
  966. $count=Db::name("invoice_pool")->alias("a")
  967. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  968. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  969. ->where($condition)->count();
  970. $total =ceil($count/$size);
  971. $page= $page>$total ? intval($total):$page;
  972. $list=Db::name("invoice_pool")->alias("a")
  973. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  974. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  975. ->where($condition)->field("b.*,a.*,c.inv_code,c.inv_number,c.open_date,c.type")
  976. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  977. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  978. }
  979. //获取发票详情
  980. public function info(){
  981. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  982. if($invNo==''){
  983. return error_show(1004,"参数 invNo 不能为空");
  984. }
  985. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  986. if(empty($poolinfo)){
  987. return error_show(1004,"为找到开票信息");
  988. }
  989. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  990. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  991. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  992. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  993. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  994. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  995. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  996. $poolinfo['seller_title'] = $pool['seller_title']??'';
  997. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  998. $poolinfo['seller_code'] = $pool['seller_code']??'';
  999. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1000. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1001. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1002. $poolinfo['voider'] = $pool['voider']??'';
  1003. $poolinfo['payee'] = $pool['payee']??'';
  1004. $poolinfo['drawer'] = $pool['drawer']??'';
  1005. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1006. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1007. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  1008. $poolinfop['goodList']=$orderList;
  1009. $poolinfop['invinfo']=Db::name("invoice_ticket")->withoutField("pdfstream")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])
  1010. ->findOrEmpty();
  1011. return app_show(0,"获取成功",$poolinfo);
  1012. }
  1013. //退票列表
  1014. public function returnList(){
  1015. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1016. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1017. $condition=[["b.is_del","=",0]];
  1018. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1019. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1020. if($start!=""){
  1021. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1022. }
  1023. if($end!=""){
  1024. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1025. }
  1026. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  1027. if($inv_type!==""){
  1028. $condition[]=["b.inv_type","=",$inv_type];
  1029. }
  1030. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1031. if($apply_id!==""){
  1032. $condition[]=["a.apply_id","=",$apply_id];
  1033. }
  1034. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1035. if($apply_name!==""){
  1036. $condition[]=["a.apply_name","like","%$apply_name%"];
  1037. }
  1038. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1039. if($status!==""){
  1040. $condition[]=["a.status","=",$status];
  1041. }
  1042. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1043. if($invNo!==""){
  1044. $condition[]=["a.invNo","like","%$invNo%"];
  1045. }
  1046. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1047. if($returnCode!==""){
  1048. $condition[]=["a.returnCode","like","%$returnCode%"];
  1049. }
  1050. $count=Db::name("invoice_return")->alias("a")
  1051. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1052. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1053. ->where($condition)->count();
  1054. $total =ceil($count/$size);
  1055. $page= $page>$total ? intval($total):$page;
  1056. $list=Db::name("invoice_return")->alias("a")
  1057. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1058. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1059. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  1060. 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,
  1061. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,
  1062. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->order("id desc")->select()->toArray();
  1063. foreach ($list as &$value){
  1064. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  1065. $value['companyName'] = $company['company_name']??"";
  1066. }
  1067. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1068. }
  1069. //红冲申请
  1070. public function applyRed(){
  1071. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1072. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  1073. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  1074. if($invfo==false) return error_show(1004,"发票数据未找到");
  1075. if(!in_array($invfo['status'],[1,2,3,4,5]))return error_show(1004,"发票数据状态无法申请");
  1076. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  1077. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  1078. $company =Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->findOrEmpty();
  1079. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  1080. $invoice=Config::get("invoice");
  1081. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1082. $redticketList=[];
  1083. $redticketArry=[
  1084. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1085. "NSRSBH"=>$company['company_license'],
  1086. "FPLXDM"=>invoiceType($invfo['inv_type']),
  1087. ];
  1088. $goodinfo =Db::name("invoice_good")
  1089. ->where(["invNo"=>$invNo,"is_del"=>0])
  1090. ->field("0 as FPHXZ,
  1091. catNo as SPBM,
  1092. id as ZXBM,
  1093. is_discount as YHZCBS,
  1094. if(inv_tag=0,'',inv_tag) as LSLBS,
  1095. addTax as ZZSTSGL,
  1096. goodName as XMMC,
  1097. '' as DW,
  1098. '' as SPSL,
  1099. '' as DJ,
  1100. (-totalPrice) as JE,
  1101. 0 HSBZ,
  1102. tax as SL,
  1103. -taxPrice as SE")->select()->toArray();
  1104. if(empty($goodinfo)){
  1105. Db::rollback();
  1106. return error_show(1004,"开票商品信息未找到");
  1107. }
  1108. if(count($goodinfo)>8){
  1109. $len =array_unique(array_column($goodinfo,'SL'));
  1110. $qdmx=["FPHXZ"=>6,
  1111. "SPBM"=>'',
  1112. "XMMC"=>"详见对应正数发票及清单",
  1113. "YHZCBS"=>0,
  1114. "LSLBS"=>'',
  1115. "ZZSTSGL"=>'',
  1116. "DW"=>'',
  1117. "SPSL"=>'',
  1118. "DJ"=>'',
  1119. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1120. "HSBZ"=>'0',
  1121. "SL"=>count($len)>1?'':$len[0],
  1122. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1123. ];
  1124. }else
  1125. {
  1126. $qdmx=$goodinfo;
  1127. }
  1128. $info=[
  1129. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1130. "YYSBZ"=> "0000000000", //营业税标志
  1131. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1132. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1133. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1134. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1135. "TKSJ"=>date("YmdHis"), //填开时间
  1136. "XHFSBH"=>$ticket['seller_id'],
  1137. "XHFMC"=>$ticket['seller_title'],
  1138. "GMFSBH"=>$ticket['buyer_id'],
  1139. "GMFMC"=> $ticket['buyer_title'],
  1140. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1141. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1142. "SQSM"=>"1100000000", //申请说明
  1143. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1144. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1145. ];
  1146. $invRed=makeNo("IRE");
  1147. $data=[
  1148. 'invNo'=>$invNo,
  1149. "invRed"=>$invRed,
  1150. "apply_id"=>$this->uid,
  1151. "apply_name"=>$this->uname,
  1152. "status"=>0,
  1153. "remark"=>'',
  1154. "addtime"=>date("Y-m-d H:i:s"),
  1155. "updatetime"=>date("Y-m-d H:i:s"),
  1156. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1157. "YYSBZ"=> "0000000000", //营业税标志
  1158. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1159. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1160. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1161. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1162. "TKSJ"=>date("YmdHis"), //填开时间
  1163. "XHFSBH"=>$ticket['seller_id'],
  1164. "XHFMC"=>$ticket['seller_title'],
  1165. "GMFSBH"=>$ticket['buyer_id'],
  1166. "GMFMC"=> $ticket['buyer_title'],
  1167. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1168. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1169. "SQSM"=>"1100000000", //申请说明
  1170. "XXBTSBS"=>"0",
  1171. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1172. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1173. "FPLXDM"=>invoiceType($invfo['inv_type']),
  1174. ];
  1175. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1176. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1177. $data['status']=5;
  1178. $data['remark']='红冲申请提交失败';
  1179. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1180. $resultChild=$result['HZSQDSCJG'][0];
  1181. if($resultChild['ZTDM']=='060000'){
  1182. $data['status']=2;
  1183. $data['XXBBH']=$resultChild['XXBBH'];
  1184. $data['remark']='红冲申请审核通过';
  1185. }
  1186. if($resultChild['ZTDM']=='060001'){
  1187. $data['status']=1;
  1188. $data['XXBBH']=$resultChild['XXBBH'];
  1189. $data['remark']='红冲申请审核中';
  1190. }
  1191. }
  1192. if($data['status']==5){
  1193. return error_show(1004,"红冲发票申请新建失败");
  1194. }
  1195. $up =Db::name("invoice_red")->insert($data);
  1196. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1197. }
  1198. //批量申请红冲发票
  1199. public function batchApplyRed(){
  1200. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1201. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1202. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1203. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1204. $insertAll=[];
  1205. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1206. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1207. if(count($companyCode)>1){
  1208. return error_show(1004,"业务企业公司不统一");
  1209. }
  1210. if(count($inv_type)>1){
  1211. return error_show(1004,"发票类型不统一");
  1212. }
  1213. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1214. if(empty($company)) {
  1215. return error_show(1004,"业务企业信息未找到");
  1216. }
  1217. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1218. if(empty($ticketArr)){
  1219. return error_show(1004,"未找到开票信息");
  1220. }
  1221. $invoice=Config::get("invoice");
  1222. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1223. Db::startTrans();
  1224. try{
  1225. $redticketArry=[
  1226. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1227. "NSRSBH"=>$company['company_license'],
  1228. "FPLXDM"=>invoiceType($inv_type[0]),
  1229. ];
  1230. $redticketList=[];
  1231. foreach ($invfo as $value){
  1232. if(!in_array($value['status'],[1,2,3,4,5])){
  1233. Db::rollback();
  1234. return error_show(1004,"发票数据状态无法申请");
  1235. }
  1236. $ticket =$ticketArr[$value['invNo']] ??[];
  1237. if(empty($ticket)){
  1238. Db::rollback();
  1239. return error_show(1004,"未找到开票信息");
  1240. }
  1241. $goodinfo =Db::name("invoice_good")
  1242. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1243. ->field(" 6 as FPHXZ,
  1244. catNo as SPBM,
  1245. id as ZXBM,
  1246. is_discount as YHZCBS,
  1247. if(inv_tag=0,'',inv_tag) as LSLBS,
  1248. addTax as ZZSTSGL,
  1249. goodName as XMMC,
  1250. '' as DW,
  1251. '' as SPSL,
  1252. '' as DJ,
  1253. (-totalPrice) as JE,
  1254. 0 HSBZ,
  1255. tax as SL,
  1256. -taxPrice as SE")
  1257. ->select()->toArray();
  1258. if(empty($goodinfo)){
  1259. Db::rollback();
  1260. return error_show(1004,"开票商品信息未找到");
  1261. }
  1262. $len =array_unique(array_column($goodinfo,'SL'));
  1263. $qdmx=["FPHXZ"=>6,
  1264. "SPBM"=>'',
  1265. "XMMC"=>"详见对应正数发票及清单",
  1266. "YHZCBS"=>0,
  1267. "LSLBS"=>'',
  1268. "ZZSTSGL"=>'',
  1269. "DW"=>'',
  1270. "SPSL"=>'',
  1271. "DJ"=>'',
  1272. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1273. "HSBZ"=>'0',
  1274. "SL"=>count($len)>1?'':$len[0],
  1275. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1276. ];
  1277. $info=[
  1278. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1279. "YYSBZ"=> "0000000000", //营业税标志
  1280. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1281. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1282. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1283. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1284. "TKSJ"=>date("YmdHis"), //填开时间
  1285. "XHFSBH"=>$ticket['seller_id'],
  1286. "XHFMC"=>$ticket['seller_title'],
  1287. "GMFSBH"=>$ticket['buyer_id'],
  1288. "GMFMC"=> $ticket['buyer_title'],
  1289. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1290. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1291. "SQSM"=>"1100000000", //申请说明
  1292. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1293. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1294. ];
  1295. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1296. $data=[
  1297. 'invNo'=>$value['invNo'],
  1298. "invRed"=>makeNo("IRE"),
  1299. "apply_id"=>$this->uid,
  1300. "apply_name"=>$this->uname,
  1301. "status"=>5,
  1302. "remark"=>'红冲申请提交失败',
  1303. "addtime"=>date("Y-m-d H:i:s"),
  1304. "updatetime"=>date("Y-m-d H:i:s"),
  1305. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1306. "YYSBZ"=> "0000000000", //营业税标志
  1307. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1308. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1309. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1310. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1311. "TKSJ"=>date("YmdHis"), //填开时间
  1312. "XHFSBH"=>$ticket['seller_id'],
  1313. "XHFMC"=>$ticket['seller_title'],
  1314. "GMFSBH"=>$ticket['buyer_id'],
  1315. "GMFMC"=> $ticket['buyer_title'],
  1316. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1317. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1318. "SQSM"=>"1100000000", //申请说明
  1319. "XXBTSBS"=>"0",
  1320. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1321. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1322. "FPLXDM"=>invoiceType($value['inv_type']),
  1323. ];
  1324. $insertAll[$info['SQBSCQQLSH']]=$data;
  1325. }
  1326. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1327. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1328. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1329. foreach ($result['HZSQDSCJG'] as $value){
  1330. if($value['ZTDM']=='060000'){
  1331. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1332. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1333. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1334. continue;
  1335. // $data['status']=2;
  1336. // $data['remark']='红冲申请审核通过';
  1337. }
  1338. if($value['ZTDM']=='060001'){
  1339. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1340. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1341. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1342. continue;
  1343. }
  1344. unset($insertAll[$value['SQBSCQQLSH']]);
  1345. }
  1346. }
  1347. }else{
  1348. Db::rollback();
  1349. return error_show(1004,"红冲发票申请失败");
  1350. }
  1351. $uns= Db::name("invoice_red")->insertAll($insertAll);
  1352. if($uns){
  1353. Db::commit();
  1354. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  1355. }
  1356. Db::rollback();
  1357. return error_show(1004,"红冲发票申请新建失败");
  1358. }catch (\Exception $e){
  1359. Db::rollback();
  1360. return error_show(1004,$e->getMessage());
  1361. }
  1362. }
  1363. //红票申请开票
  1364. public function redOpen(){
  1365. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  1366. if($invRed==''){
  1367. return error_show(1004,"参数 invRed 不能为空");
  1368. }
  1369. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  1370. if(empty($redinfo)){
  1371. return error_show(1004,"红冲发票申请不存在");
  1372. }
  1373. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  1374. if($redreason==''){
  1375. return error_show(1004,"参数 redreason 不能为空");
  1376. }
  1377. if($redinfo['status']!=2){
  1378. return error_show(1004,"数据状态不可提交申请");
  1379. }
  1380. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  1381. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  1382. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  1383. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  1384. Db::startTrans();
  1385. try{
  1386. $data=[
  1387. "invNo"=>$invRed,
  1388. "KPLX"=>"1",
  1389. "XHFSBH"=>$invpoolinfo['seller_id'],
  1390. "XHFMC"=>$invpoolinfo['seller_title'],
  1391. "XHFDZ"=>$invpoolinfo['seller_addr'],
  1392. "XHFDH"=>$invpoolinfo['seller_mobile'],
  1393. "XHFYH"=>$invpoolinfo['seller_bank'],
  1394. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  1395. "GMFBM"=>"",
  1396. "GMFSBH"=>$invpoolinfo['buyer_id'],
  1397. "GMFMC"=>$invpoolinfo['buyer_title'],
  1398. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  1399. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  1400. "GMFYH"=>$invpoolinfo['buyer_bank'],
  1401. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  1402. "GMFLX"=>$invinfo['company_type'],
  1403. "GMFSJH"=>"",
  1404. "GMFDZYX"=>$invinfo['email'],
  1405. "KPR"=>$invpoolinfo['issuer'],
  1406. "SKR"=>$invpoolinfo['reciver'],
  1407. "FHR"=>$invpoolinfo['reviewer'],
  1408. "QDBZ"=>"0",
  1409. "YFPDM"=>$redinfo['YFPDM'],
  1410. "YFPHM"=>$redinfo['YFPHM'],
  1411. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  1412. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  1413. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  1414. "TSCHBZ"=>"0",
  1415. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  1416. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  1417. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  1418. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  1419. "DDMXXX"=>$redinfo['DDMXXX'],
  1420. "FPLXDM"=>$redinfo['FPLXDM'],
  1421. "DDQQPCH"=>"",
  1422. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1423. "status"=>"0",
  1424. "result"=>"",
  1425. "addtime"=>date("Y-m-d H:i:s"),
  1426. "updatetime"=>date("Y-m-d H:i:s")
  1427. ];
  1428. $invoice=Db::name("invoice_interface")->insert($data);
  1429. if($invoice==false){
  1430. Db::rollback();
  1431. return error_show(1003,"红冲开票信息生成失败");
  1432. }
  1433. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  1434. if($upred==false){
  1435. Db::rollback();
  1436. return error_show(1003,"红冲发票信息更新失败");
  1437. }
  1438. Db::commit();
  1439. return app_show(0,"红冲发票信息开票提交成功");
  1440. }catch (\Exception $e){
  1441. Db::rollback();
  1442. return error_show(1003,$e->getMessage());
  1443. }
  1444. }
  1445. //红冲票申请列表
  1446. public function redList(){
  1447. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1448. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1449. $condition=[["is_del","=",0]];
  1450. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  1451. if($invRed!=''){
  1452. $condition[]=["invRed","like","%$invRed%"];
  1453. }
  1454. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  1455. if($apply_id!=''){
  1456. $condition[]=["apply_id","=",$apply_id];
  1457. }
  1458. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  1459. if($invNo!=''){
  1460. $condition[]=["invNo","like","%$invNo%"];
  1461. }
  1462. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  1463. if($apply_name!=''){
  1464. $condition[]=["apply_name","like","%$apply_name%"];
  1465. }
  1466. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  1467. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  1468. if($start!=''){
  1469. $condition[]=["addtime",">=",$start];
  1470. }
  1471. if($end!=''){
  1472. $condition[]=["addtime","<=",$end];
  1473. }
  1474. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  1475. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  1476. if($total_low!=''){
  1477. $condition[]=["ABS(HJJE)",">=",$total_low];
  1478. }
  1479. if($total_up!=''){
  1480. $condition[]=["ABS(HJJE)","<=",$total_up];
  1481. }
  1482. $count= Db::name("invoice_red")->where($condition)->count();
  1483. $total =ceil($count/$size);
  1484. $page=$total>=$page?$page :intval($total);
  1485. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  1486. ->toArray();
  1487. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1488. }
  1489. /**
  1490. * @return \think\response\Json|void
  1491. */
  1492. public function ticketInfo(){
  1493. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1494. if($num==''){
  1495. return error_show(1004,'参数 number 不能为空');
  1496. }
  1497. $condition=[];
  1498. $condition[]=["inv_number","=",$num];
  1499. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1500. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  1501. $info =Db::name("invoice_ticket")->json(['item'])->withoutField("pdfstream")->where($condition)->findOrEmpty();
  1502. if(!empty($info)&& isset($info['item'])){
  1503. array_walk($info['item'],function (&$value){
  1504. $value['SL'] = ($value['SL']*100)."%";
  1505. });
  1506. }
  1507. return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
  1508. }
  1509. /**
  1510. * @param array $ticket
  1511. * @return \think\response\Json|void
  1512. * @throws \think\db\exception\DataNotFoundException
  1513. * @throws \think\db\exception\DbException
  1514. * @throws \think\db\exception\ModelNotFoundException
  1515. */
  1516. private function addApplyRed($ticket=[],$returnCode){
  1517. $invoice=Config::get("invoice");
  1518. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1519. $redticketList=[];
  1520. $redticketArry=[
  1521. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1522. "NSRSBH"=>$ticket['seller_id'],
  1523. "FPLXDM"=>$ticket['inv_type'],
  1524. ];
  1525. $goodinfo =Db::name("invoice_good")
  1526. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1527. ->field("0 as FPHXZ,
  1528. catNo as SPBM,
  1529. id as ZXBM,
  1530. is_discount as YHZCBS,
  1531. if(inv_tag=0,'',inv_tag) as LSLBS,
  1532. addTax as ZZSTSGL,
  1533. goodName as XMMC,
  1534. '' as DW,
  1535. '' as SPSL,
  1536. '' as DJ,
  1537. (-totalPrice) as JE,
  1538. 0 HSBZ,
  1539. tax as SL,
  1540. -taxPrice as SE")->select()->toArray();
  1541. if(empty($goodinfo)){
  1542. throw new \Exception("开票商品信息未找到", 10006);
  1543. }
  1544. if(count($goodinfo)>8){
  1545. $len =array_unique(array_column($goodinfo,'SL'));
  1546. $qdmx=["FPHXZ"=>6,
  1547. "SPBM"=>'',
  1548. "XMMC"=>"详见对应正数发票及清单",
  1549. "YHZCBS"=>0,
  1550. "LSLBS"=>'',
  1551. "ZZSTSGL"=>'',
  1552. "DW"=>'',
  1553. "SPSL"=>'',
  1554. "DJ"=>'',
  1555. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1556. "HSBZ"=>'0',
  1557. "SL"=>count($len)>1?'':$len[0],
  1558. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1559. ];
  1560. }else
  1561. {
  1562. $qdmx=$goodinfo;
  1563. }
  1564. $info=[
  1565. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1566. "YYSBZ"=> "0000000000", //营业税标志
  1567. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1568. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1569. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1570. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1571. "TKSJ"=>date("YmdHis"), //填开时间
  1572. "XHFSBH"=>$ticket['seller_id'],
  1573. "XHFMC"=>$ticket['seller_title'],
  1574. "GMFSBH"=>$ticket['buyer_id'],
  1575. "GMFMC"=> $ticket['buyer_title'],
  1576. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1577. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1578. "SQSM"=>"1100000000", //申请说明
  1579. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1580. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1581. ];
  1582. $invRed=makeNo("IRE");
  1583. $data=[
  1584. 'invNo'=>$ticket['invNo'],
  1585. "invRed"=>$invRed,
  1586. "returnCode"=>$returnCode,
  1587. "apply_id"=>$this->uid,
  1588. "apply_name"=>$this->uname,
  1589. "status"=>0,
  1590. "remark"=>'',
  1591. "addtime"=>date("Y-m-d H:i:s"),
  1592. "updatetime"=>date("Y-m-d H:i:s"),
  1593. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1594. "YYSBZ"=> "0000000000", //营业税标志
  1595. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1596. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1597. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1598. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1599. "TKSJ"=>date("YmdHis"), //填开时间
  1600. "XHFSBH"=>$ticket['seller_id'],
  1601. "XHFMC"=>$ticket['seller_title'],
  1602. "GMFSBH"=>$ticket['buyer_id'],
  1603. "GMFMC"=> $ticket['buyer_title'],
  1604. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1605. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1606. "SQSM"=>"1100000000", //申请说明
  1607. "XXBTSBS"=>"0",
  1608. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1609. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1610. "FPLXDM"=>$ticket['inv_type'],
  1611. ];
  1612. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1613. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1614. $data['status']=5;
  1615. $data['remark']='红冲申请提交失败';
  1616. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1617. $resultChild=$result['HZSQDSCJG'][0];
  1618. if($resultChild['ZTDM']=='060000'){
  1619. $data['status']=2;
  1620. $data['XXBBH']=$resultChild['XXBBH'];
  1621. $data['remark']='红冲申请审核通过';
  1622. }
  1623. if($resultChild['ZTDM']=='060001'){
  1624. $data['status']=1;
  1625. $data['XXBBH']=$resultChild['XXBBH'];
  1626. $data['remark']='红冲申请审核中';
  1627. }
  1628. }
  1629. if($data['status']==5){
  1630. throw new \Exception($resultChild['ZTXX'], 10006);
  1631. }
  1632. $up =Db::name("invoice_red")->insert($data);
  1633. if($up) return app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]);
  1634. else throw new \Exception($resultChild['ZTXX'], 1006);
  1635. }
  1636. /**
  1637. * @param array $ticket
  1638. * @return \think\response\Json|void
  1639. * @throws \think\Exception
  1640. */
  1641. private function addRedOpen($ticket=[],$return_reason,$company_type,$returnCode){
  1642. $goodinfo =Db::name("invoice_good")
  1643. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1644. ->field("0 as FPHXZ,
  1645. catNo as SPBM,
  1646. id as ZXBM,
  1647. is_discount as YHZCBS,
  1648. if(inv_tag=0,'',inv_tag) as LSLBS,
  1649. addTax as ZZSTSGL,
  1650. goodName as XMMC,
  1651. '' as DW,
  1652. '' as SPSL,
  1653. '' as DJ,
  1654. (-totalPrice) as JE,
  1655. 0 HSBZ,
  1656. tax as SL,
  1657. -taxPrice as SE")->select()->toArray();
  1658. if(empty($goodinfo)){
  1659. throw new \Exception("开票商品信息未找到", 10006);
  1660. }
  1661. if(count($goodinfo)>8){
  1662. $len =array_unique(array_column($goodinfo,'SL'));
  1663. $qdmx=["FPHXZ"=>6,
  1664. "SPBM"=>'',
  1665. "XMMC"=>"详见对应正数发票及清单",
  1666. "YHZCBS"=>0,
  1667. "LSLBS"=>'',
  1668. "ZZSTSGL"=>'',
  1669. "DW"=>'',
  1670. "SPSL"=>'',
  1671. "DJ"=>'',
  1672. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1673. "HSBZ"=>'0',
  1674. "SL"=>count($len)>1?'':$len[0],
  1675. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1676. ];
  1677. }else
  1678. {
  1679. $qdmx=$goodinfo;
  1680. }
  1681. $invRed=makeNo("IRE");
  1682. $reddata=[
  1683. 'invNo'=>$ticket['invNo'],
  1684. "invRed"=>$invRed,
  1685. "returnCode"=>$returnCode,
  1686. "apply_id"=>$this->uid,
  1687. "apply_name"=>$this->uname,
  1688. "status"=>3,
  1689. "remark"=>'',
  1690. "addtime"=>date("Y-m-d H:i:s"),
  1691. "updatetime"=>date("Y-m-d H:i:s"),
  1692. "SQBSCQQLSH"=>'',
  1693. "YYSBZ"=> "0000000000", //营业税标志
  1694. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1695. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1696. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1697. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1698. "TKSJ"=>date("YmdHis"), //填开时间
  1699. "XHFSBH"=>$ticket['seller_id'],
  1700. "XHFMC"=>$ticket['seller_title'],
  1701. "GMFSBH"=>$ticket['buyer_id'],
  1702. "GMFMC"=> $ticket['buyer_title'],
  1703. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1704. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1705. "SQSM"=>"1100000000", //申请说明
  1706. "XXBTSBS"=>"0",
  1707. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1708. "SQBSCQQPCH"=>'',
  1709. "FPLXDM"=>$ticket['inv_type'],
  1710. ];
  1711. $data=[ "invNo"=>$invRed,
  1712. "KPLX"=>"1",
  1713. "XHFSBH"=>$ticket['seller_id'],
  1714. "XHFMC"=>$ticket['seller_title'],
  1715. "XHFDZ"=>$ticket['seller_addr'],
  1716. "XHFDH"=>$ticket['seller_mobile'],
  1717. "XHFYH"=>$ticket['seller_bank'],
  1718. "XHFZH"=>$ticket['seller_bankNo'],
  1719. "GMFBM"=>"",
  1720. "GMFSBH"=>$ticket['buyer_id'],
  1721. "GMFMC"=>$ticket['buyer_title'],
  1722. "GMFDZ"=>$ticket['buyer_addr'],
  1723. "GMFDH"=>$ticket['buyer_mobile'],
  1724. "GMFYH"=>$ticket['buyer_bank'],
  1725. "GMFZH"=>$ticket['buyer_bankNo'],
  1726. "GMFLX"=>$company_type,
  1727. "GMFSJH"=>"",
  1728. "GMFDZYX"=>'',
  1729. "KPR"=>$ticket['issuer'],
  1730. "SKR"=>$ticket['reciver'],
  1731. "FHR"=>$ticket['reviewer'],
  1732. "QDBZ"=>"0",
  1733. "YFPDM"=>$ticket['inv_code'],
  1734. "YFPHM"=>$ticket['inv_number'],
  1735. "YFPLX"=>$ticket['inv_type'],
  1736. "YFPKPRQ"=>date("Y-m-d",strtotime($ticket['open_date'])),
  1737. "CHYY"=>$return_reason,
  1738. "TSCHBZ"=>"0",
  1739. "JSHJ"=>"-".$ticket['inv_total'],
  1740. "HJJE"=>"-".$ticket['inv_subtotal'],
  1741. "HJSE"=>"-".$ticket['tax_fee'],
  1742. "BZ"=> "对应正数发票代码:{$ticket['inv_code']} 号码:{$ticket['inv_number']}",
  1743. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1744. "FPLXDM"=>$ticket['inv_type'],
  1745. "DDQQPCH"=>"",
  1746. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1747. "status"=>"0",
  1748. "result"=>"",
  1749. "addtime"=>date("Y-m-d H:i:s"),
  1750. "updatetime"=>date("Y-m-d H:i:s")
  1751. ];
  1752. $invoice=Db::name("invoice_interface")->insert($data);
  1753. if($invoice==false){
  1754. throw new Exception("红冲提交失败",1006);
  1755. }
  1756. $upred=Db::name("invoice_red")->save($reddata);
  1757. if($upred==false){
  1758. throw new Exception("红冲提交失败",1006);
  1759. }
  1760. return app_show(0,"红冲已提交");
  1761. }
  1762. private function addDiscard($ticket=[],$returnCode){
  1763. $invoice=Config::get("invoice");
  1764. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1765. $result=$tax->DeprecateInvoices($ticket['seller_id'],$ticket['inv_type'],$ticket['inv_code'],$ticket['inv_number'],1);
  1766. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  1767. Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1768. $up =Db::name("invoice_pool")->where(['invNo'=>$ticket['invNo']])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  1769. if($up){
  1770. return app_show(0,"发票废弃成功");
  1771. }
  1772. }else{
  1773. throw new \Exception($result['ZTXX'], 1006);
  1774. }
  1775. }
  1776. //退票列表
  1777. public function returnRedList(){
  1778. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1779. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1780. $condition=[];
  1781. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1782. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1783. if($start!=""){
  1784. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1785. }
  1786. if($end!=""){
  1787. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1788. }
  1789. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  1790. if($inv_type!==""){
  1791. $condition[]=["b.inv_type","=",$inv_type];
  1792. }
  1793. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1794. if($apply_id!==""){
  1795. $condition[]=["a.apply_id","=",$apply_id];
  1796. }
  1797. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1798. if($apply_name!==""){
  1799. $condition[]=["a.apply_name","like","%$apply_name%"];
  1800. }
  1801. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1802. if($status!==""){
  1803. $condition[]=["a.status","=",$status];
  1804. }
  1805. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1806. if($invNo!==""){
  1807. $condition[]=["a.invNo","like","%$invNo%"];
  1808. }
  1809. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1810. if($returnCode!==""){
  1811. $condition[]=["a.returnCode","like","%$returnCode%"];
  1812. }
  1813. $count=Db::name("invoice_return")->alias("a")
  1814. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  1815. ->where($condition)->count();
  1816. $total =ceil($count/$size);
  1817. $page= $page>$total ? intval($total):$page;
  1818. $list=Db::name("invoice_return")->alias("a")
  1819. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  1820. ->field("b.*,a.*")->withoutField("pdfstream")->json(["item"])->page($page,$size)->where($condition)->select()
  1821. ->toArray();
  1822. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1823. }
  1824. }