OrderInv.php 86 KB

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