OrderInv.php 88 KB

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