OrderInv.php 82 KB

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