OrderInv.php 84 KB

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