OrderInv.php 84 KB

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