OrderInv.php 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;
  5. use think\App;
  6. use think\Exception;use think\facade\Config;
  7. use think\facade\Db;
  8. use think\facade\Cache;
  9. class OrderInv extends BaseController{
  10. public function __construct(App $app) {
  11. parent::__construct($app);
  12. }
  13. /**
  14. * 新建销售单开票申请
  15. */
  16. public function create(){
  17. $companyType = isset($this->post['companyType'])&&$this->post['companyType']!=='' ? trim($this->post['companyType']):"";
  18. if ($companyType==''){
  19. return error_show(1004,"参数 companyType 不能为空");
  20. }
  21. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  22. if($buy_id==''){
  23. return error_show(1004,"参数 buy_id 不能为空");
  24. }
  25. $buyinfo =Db::name("customer_invoice")->where(['id'=>2054,"is_del"=>0])->find();
  26. if($buyinfo==false){
  27. return error_show(1004,"购买方发票信息未找到");
  28. }
  29. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  30. if($companyNo==''){
  31. return error_show(1004,"参数 companyNo 不能为空");
  32. }
  33. $company = Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->find();
  34. if($company==false){
  35. return error_show(1004,"销售方信息未找到");
  36. }
  37. // $khNo = isset($this->post['khNo'])&&$this->post['khNo']!=='' ? trim($this->post['khNo']):"";
  38. // if($khNo==''){
  39. // return error_show(1004,"参数 khNo 不能为空");
  40. // }
  41. // $customer = Db::name("customer_info")->where(["companyNo"=>$khNo])->find();
  42. // if($customer==false){
  43. // return error_show(1004,"客户信息未找到");
  44. // }
  45. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? intval($this->post['invtype']):"";
  46. if($invtype==''){
  47. return error_show(1004,"参数 invtype 不能为空");
  48. }
  49. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  50. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  51. $exam_remark = isset($this->post['exam_remark'])&&$this->post['exam_remark']!=='' ? trim($this->post['exam_remark']):"";
  52. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  53. if(empty($orderArr)){
  54. return error_show(1004,"参数 orderArr 不能为空");
  55. }
  56. $temp =[];
  57. $good=[];
  58. $invNo=makeNo("INV");
  59. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  60. Db::startTrans();
  61. try{
  62. foreach ($orderArr as $value){
  63. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  64. Db::rollback();
  65. return error_show(1004,"参数 sequenceNo 不能为空");
  66. }
  67. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  68. if($qrd['status']==2){
  69. Db::rollback();
  70. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  71. }
  72. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  73. Db::rollback();
  74. return error_show(1004,"参数 inv_fee 不能为空");
  75. }
  76. if($qrd['winv_fee']<$value['inv_fee']){
  77. Db::rollback();
  78. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  79. }
  80. $good_info = Db::name("good")->where(["spuCode"=>$qrd["goodNo"],"status"=>1])->findOrEmpty();
  81. if(empty($good_info)){
  82. Db::rollback();
  83. return error_show(1004,"确认单{$value['sequenceNo']}商品未确认开票类目");
  84. }
  85. $num = DB::name("assoc")->alias("a")->leftJoin("invoice_good b","a.viceCode=b.invNo")
  86. ->where(["a.status"=>[1,2],"a.is_del"=>0,"a.type"=>1,"a.orderCode"=>$value['sequenceNo']])->sum("b.goodNum");
  87. if($qrd['goodNum']-$num<$value['num']){
  88. Db::rollback();
  89. return error_show(1004,"确认单{$value['sequenceNo']}待开票数量不足");
  90. }
  91. $ascc=[
  92. "assocNo"=>makeNo("AS"),
  93. "apply_id"=>$this->uid,
  94. "apply_name"=>$this->uname,
  95. "type"=>1,
  96. "orderCode"=>$value['sequenceNo'],
  97. "customerNo"=>$qrd['customerNo'],
  98. "viceCode"=>$invNo,
  99. "order_total"=>$qrd['totalPrice'],
  100. "vice_total"=>$invfee,
  101. "cancel_fee"=>$value['inv_fee'],
  102. "status"=>1,
  103. "addtime"=>date("Y-m-d H:i:s"),
  104. "updatetime"=>date("Y-m-d H:i:s")
  105. ];
  106. $invoice_good=[
  107. "invNo"=>$invNo,
  108. "orderCode"=>$value['sequenceNo'],
  109. "goodNo"=>$qrd['goodNo'],
  110. "goodName"=>$good_info['inv_good_name'],
  111. "catName"=>$good_info['inv_cat_name'],
  112. "catNo"=>$good_info['inv_cat_code'],
  113. "goodPrice"=>round($value['inv_fee']/$value['num'],8),
  114. "unitName"=>$qrd['goodUnit'],
  115. "goodNum"=>$value['num'],
  116. "specInfo"=>$value['sequenceNo'],
  117. "totalPrice"=>$value['inv_fee'],
  118. "tax"=>$good_info['inv_tax'],
  119. "is_discount"=>$good_info['is_discount'],
  120. "addTax"=>$good_info['addTax'],
  121. "inv_tag"=>$good_info['inv_tag'],
  122. "taxPrice"=>round($value['inv_fee']*$good_info['inv_tax'],2),
  123. "addtime"=>date("Y-m-d H:i:s"),
  124. "updatetime"=>date("Y-m-d H:i:s")
  125. ];
  126. $update = [
  127. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  128. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  129. "inv_status"=>2,
  130. "status"=>1,
  131. "updatetime"=>date("Y-m-d H:i:s")
  132. ];
  133. $up = Db::name("qrd_info")->where($qrd)->update($update);
  134. if($up==false){
  135. Db::rollback();
  136. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  137. }
  138. $temp[]=$ascc;
  139. $good[]=$invoice_good;
  140. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  141. if($report)$report->setField("invNo",$invNo);
  142. }
  143. $associn = Db::name("assoc")->insertAll($temp);
  144. if($associn==0){
  145. Db::rollback();
  146. return error_show(1005,"确认单申请开票失败");
  147. }
  148. $goodin = Db::name("invoice_good")->insertAll($good);
  149. if($goodin==0){
  150. Db::rollback();
  151. return error_show(1005,"确认单申请开票失败");
  152. }
  153. $inv=[
  154. "invNo"=>$invNo,
  155. "inv_value"=>$invfee,
  156. "inv_out"=> $companyNo,
  157. "inv_company"=> $company['company_name'],
  158. "apply_id"=>$this->uid,
  159. "apply_name"=>$this->uname,
  160. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通
  161. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  162. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  163. "company_type"=> $buyinfo['company_type'], //类型01 企业,02 机关事业单位,03 个人, 04 其他
  164. "remark"=>$remark, //申请备注
  165. "exam_remark"=>$exam_remark, //审核备注
  166. "ainv_fee"=>0,//已核销金额
  167. "winv_fee"=>$invfee,//未核销金额
  168. "status"=>0,// 待财务开票/待金税开票 待财务审核 待填写物流 开票完成 开票失败/开票驳回 财务驳回
  169. "is_del"=>0,
  170. "email"=>$email,
  171. "addtime"=>date("Y-m-d H:i:s"),
  172. "updatetime"=>date("Y-m-d H:i:s")
  173. ];
  174. $invin = Db::name("invoice_pool")->insert($inv);
  175. if($invin){
  176. $invinfo=[
  177. "buyer_title"=>$buyinfo['invoice_title'],
  178. "buyer_code"=>$buyinfo['invoice_code'],
  179. "buyer_addr"=>$buyinfo['invoice_addr'],
  180. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  181. "buyer_bank"=>$buyinfo['invoice_bank'],
  182. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  183. "seller_title"=>$company['company_name'],
  184. "seller_code"=>$company['company_license'],
  185. "seller_addr"=>$company['company_address'],
  186. "seller_mobile"=>$company['mobile'],
  187. "seller_bank"=>$company['bank_name'],
  188. "seller_bankNo"=>$company['bankNo'],
  189. "invNo"=>$invNo,
  190. "voider"=>$company['voider'],
  191. "payee"=>$company['payee'],
  192. "drawer"=>$company['drawer'],
  193. "reviewer"=>$company['reviewer'],
  194. "ownerPlace"=>$company['ownerPlace'],
  195. "addtime"=>date("Y-m-d H:i:s")
  196. ];
  197. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  198. if($poolinfo){
  199. Db::commit();
  200. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  201. }
  202. }
  203. Db::rollback();
  204. return error_show(1004,"确认单发票申请失败");
  205. }catch (\Exception $e){
  206. Db::rollback();
  207. return error_show(1004,$e->getMessage());
  208. }
  209. }
  210. // 0 待财务开票/待金税开票 1待财务审核 2待填写物流 3开票完成 4开票失败/开票驳回 5财务驳回 6退票 7金税开票中
  211. public function status(){
  212. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  213. if($invNo==""){
  214. return error_show(1004,"参数 invNo 不能为空");
  215. }
  216. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  217. if($invinfo==false){
  218. return error_show(1004,"发票申请数据未找到");
  219. }
  220. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  221. if($status===''){
  222. return error_show(1004,"参数 status 不能为空");
  223. }
  224. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  225. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  226. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  227. // $invArr=isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  228. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  229. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  230. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  231. // $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floatval($this->post['total_fee']):"";
  232. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  233. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  234. if($status==1){
  235. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  236. if($open_type!=1){
  237. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  238. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  239. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  240. if($invinfo['inv_type']==2 || $invinfo['inv_type']==3){
  241. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  242. }
  243. // if($total_fee==='') return error_show(1004,"参数 tatal_fee 不能为空");
  244. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  245. // if($total_fee!=$invinfo['inv_value'])return error_show(1004,"发票金额不足");
  246. $status=7;
  247. }else{
  248. $status=7;
  249. }
  250. }
  251. $update=[
  252. "status"=> $status==2 && ($invinfo['inv_type']==3 || $invinfo['inv_type']==4) ? 3 : $status,
  253. "exam_remark"=>$remark,
  254. "updatetime" => date("Y-m-d H:i:s")
  255. ];
  256. $status==1 ? $update['open_type']=$open_type :"";
  257. Db::startTrans();
  258. try{
  259. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  260. if($invup){
  261. if($status==7&& $open_type!=1){
  262. $invpool=[
  263. "invNo"=>$invinfo['invNo'],
  264. "inv_type"=>invoiceType($invinfo['inv_type']),
  265. "inv_code"=>$invCode,
  266. "inv_number"=>$invNum,
  267. "check_code"=>$checkCode,
  268. "inv_total"=>0,
  269. "inv_subtotal"=>$subtotal_fee,
  270. "open_date"=>$open_date,
  271. "item"=>'',
  272. "status"=>0,
  273. "addtime"=>date("Y-m-d H:i:s"),
  274. "updatetime"=>date("Y-m-d H:i:s")
  275. ];
  276. $in = Db::name("invoice_ticket")->insert($invpool);
  277. if($in==false){
  278. Db::rollback();
  279. return error_show(1004,"发票信息添加失败");
  280. }
  281. }
  282. if($status==7 && $open_type==1){
  283. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  284. if(empty($invpoolinfo)){
  285. Db::rollback();
  286. return error_show(1004,"开票信息未找到");
  287. }
  288. $goodinfo =Db::name("invoice_good")
  289. ->where(["invNo"=>$invNo,"is_del"=>0])
  290. ->field("0 as FPHXZ,
  291. catNo as SPBM,
  292. id as ZXBM,
  293. is_discount as YHZCBS,
  294. if(inv_tag=0,'',inv_tag) as LSLBS,
  295. addTax as ZZSTSGL,
  296. goodName as XMMC,
  297. specInfo as GGXH,
  298. unitName as DW,
  299. goodNum as SPSL,
  300. goodPrice as DJ,
  301. totalPrice as JE,
  302. 1 HSBZ,
  303. tax as SL,
  304. taxPrice as SE")
  305. ->select()->toJson();
  306. if(empty($goodinfo)){
  307. Db::rollback();
  308. return error_show(1004,"开票商品信息未找到");
  309. }
  310. $data=[
  311. "invNo"=>$invNo,
  312. "KPLX"=>"0",
  313. "XHFSBH"=>$invpoolinfo['seller_code'],
  314. "XHFMC"=>$invpoolinfo['seller_title'],
  315. "XHFDZ"=>$invpoolinfo['seller_addr'],
  316. "XHFDH"=>$invpoolinfo['seller_mobile'],
  317. "XHFYH"=>$invpoolinfo['seller_bank'],
  318. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  319. "GMFBM"=>"",
  320. "GMFSBH"=>$invpoolinfo['buyer_code'],
  321. "GMFMC"=>$invpoolinfo['buyer_title'],
  322. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  323. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  324. "GMFYH"=>$invpoolinfo['buyer_bank'],
  325. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  326. "GMFLX"=>$invinfo['company_type'],
  327. "GMFSJH"=>"",
  328. "GMFDZYX"=>$invinfo['email'],
  329. "KPR"=>$invpoolinfo['drawer'],
  330. "SKR"=>$invpoolinfo['payee'],
  331. "FHR"=>$invpoolinfo['reviewer'],
  332. "QDBZ"=>"0",
  333. "JSHJ"=>$invinfo['inv_value'],
  334. "HJJE"=>"0",
  335. "HJSE"=>"0",
  336. "BZ"=>$invinfo['remark'],
  337. "DDMXXX"=>$goodinfo,
  338. "FPLXDM"=>invoiceType($invinfo['inv_type']),
  339. "DDQQPCH"=>"",
  340. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  341. "status"=>"0",
  342. "result"=>"",
  343. "addtime"=>date("Y-m-d H:i:s"),
  344. "updatetime"=>date("Y-m-d H:i:s")
  345. ];
  346. $invoice=Db::name("invoice_interface")->insert($data);
  347. if($invoice==false){
  348. Db::rollback();
  349. return error_show(1003,"开票信息生成失败");
  350. }
  351. Cache::push("invoiceList",$data["DDQQLSH"]);
  352. }
  353. if( $update['status']==3){
  354. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  355. if(!empty($qrdArr)){
  356. foreach ($qrdArr as $value){
  357. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  358. if($qrdinfo==false){
  359. Db::rollback();
  360. return error_show(1003,"确认单信息未找到");
  361. }
  362. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  363. Db::rollback();
  364. return error_show(1003,"确认单信息开票金额不足");
  365. }
  366. $update =[
  367. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  368. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  369. "invtime"=>date("Y-m-d H:i:s"),
  370. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  371. "updatetime"=>date("Y-m-d H:i:s"),
  372. ];
  373. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  374. if($qrdup==false){
  375. Db::rollback();
  376. return error_show(1003,"确认单信息更新失败");
  377. }
  378. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  379. $assocup =Db::name("assoc")->where($value)->update($assoc);
  380. if($assocup==false){
  381. Db::rollback();
  382. return error_show(1003,"确认单关联信息更新失败");
  383. }
  384. }
  385. }
  386. }
  387. if($status==4 ||$status==5 ){
  388. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  389. if(!empty($qrdArr)){
  390. foreach ($qrdArr as $value){
  391. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  392. if($qrdinfo==false){
  393. Db::rollback();
  394. return error_show(1003,"确认单信息未找到");
  395. }
  396. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  397. Db::rollback();
  398. return error_show(1003,"确认单信息开票金额不足");
  399. }
  400. $update =[
  401. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  402. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  403. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  404. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  405. "updatetime"=>date("Y-m-d H:i:s"),
  406. ];
  407. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  408. if($qrdup==false){
  409. Db::rollback();
  410. return error_show(1003,"确认单信息更新失败");
  411. }
  412. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  413. $assocup =Db::name("assoc")->where($value)->update($assoc);
  414. if($assocup==false){
  415. Db::rollback();
  416. return error_show(1003,"确认单关联信息更新失败");
  417. }
  418. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  419. if($report)$report->rmField("invNo",$invinfo['invNo']);
  420. }
  421. }
  422. }
  423. Db::commit();
  424. return app_show(0,"审核成功");
  425. }
  426. Db::rollback();
  427. return error_show(1004,"审核失败");
  428. }catch (\Exception $e){
  429. Db::rollback();
  430. return error_show(1004,$e->getMessage());
  431. }
  432. }
  433. /**
  434. * 发票设置物流
  435. */
  436. public function SetPost(){
  437. // @param invNo postCompany postCode postFee;
  438. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  439. if($invNo==''){
  440. return error_show(1004,"参数invNo不能为空");
  441. }
  442. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  443. if($invinfo==false){
  444. return error_show(1004,"发票申请信息未找到");
  445. }
  446. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  447. if($post_company==''){
  448. return error_show(1004,"参数 post_company 不能为空");
  449. }
  450. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  451. if($post_code==''){
  452. return error_show(1004,"参数 post_code 不能为空");
  453. }
  454. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floatval($this->post['post_fee']):"0";
  455. Db::startTrans();
  456. try{
  457. $data=[
  458. "post_company"=>$post_company,
  459. "post_code"=>$post_code,
  460. "post_fee"=>$post_fee,
  461. "status"=>3,
  462. "updatetime"=>date("Y-m-d H:i:s")
  463. ];
  464. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  465. if($up){
  466. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  467. if(!empty($qrdArr)){
  468. foreach ($qrdArr as $value){
  469. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  470. if($qrdinfo==false){
  471. Db::rollback();
  472. return error_show(1003,"确认单信息未找到");
  473. }
  474. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  475. Db::rollback();
  476. return error_show(1003,"确认单信息开票金额不足");
  477. }
  478. $update =[
  479. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  480. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  481. "invtime"=>date("Y-m-d H:i:s"),
  482. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  483. "updatetime"=>date("Y-m-d H:i:s"),
  484. ];
  485. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  486. if($qrdup==false){
  487. Db::rollback();
  488. return error_show(1003,"确认单信息更新失败");
  489. }
  490. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  491. $assocup =Db::name("assoc")->where($value)->update($assoc);
  492. if($assocup==false){
  493. Db::rollback();
  494. return error_show(1003,"确认单关联信息更新失败");
  495. }
  496. }
  497. }
  498. Db::commit();
  499. return app_show(0,"更新成功");
  500. }
  501. Db::rollback();
  502. return error_show(1003,"发票申请信息更新失败");
  503. }catch (\Exception $e){
  504. Db::rollback();
  505. return error_show(1004,$e->getMessage());
  506. }
  507. }
  508. /**
  509. * 发票废弃使用
  510. */
  511. public function Discard(){
  512. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  513. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  514. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  515. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  516. if(!in_array($invinfo['status'],[1,2,3,5]))return error_show(1004,"发票状态无法废弃");
  517. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  518. if($company==false)return error_show(1004,"开票业务公司未找到");
  519. $invoiceinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  520. if($invoiceinfo==false)return error_show(1004,"发票详细信息未找到");
  521. Db::startTrans();
  522. try{
  523. $invoice=Config::get("invoice");
  524. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  525. $result=$tax->DeprecateInvoices($company['company_license'],invoiceType($invoiceinfo['inv_type']),$invoiceinfo['inv_code'],$invoiceinfo['inv_number'],1);
  526. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  527. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  528. if($up){
  529. if($invinfo['status']!=5){
  530. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  531. if(!empty($qrdArr)){
  532. foreach ($qrdArr as $value){
  533. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  534. if($qrdinfo==false){
  535. Db::rollback();
  536. return error_show(1003,"确认单信息未找到");
  537. }
  538. if ($invinfo['status']==3){
  539. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  540. Db::rollback();
  541. return error_show(1003,"确认单信息开票金额不足");
  542. }
  543. $update =[
  544. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  545. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  546. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  547. "updatetime"=>date("Y-m-d H:i:s"),
  548. ];
  549. }else{
  550. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  551. Db::rollback();
  552. return error_show(1003,"确认单信息开票金额不足");
  553. }
  554. $update =[
  555. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  556. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  557. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  558. "updatetime"=>date("Y-m-d H:i:s"),
  559. ];
  560. }
  561. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  562. if($qrdup==false){
  563. Db::rollback();
  564. return error_show(1003,"确认单信息更新失败");
  565. }
  566. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  567. $assocup =Db::name("assoc")->where($value)->update($assoc);
  568. if($assocup==false){
  569. Db::rollback();
  570. return error_show(1003,"确认单关联信息更新失败");
  571. }
  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. "status"=>0,
  734. "addtime"=>date("Y-m-d H:i:s"),
  735. "updatetime"=>date("Y-m-d H:i:s")
  736. ];
  737. $in = Db::name("invoice_ticket")->insert($invpool);
  738. if($in==false){
  739. Db::rollback();
  740. return error_show(1004,"发票信息添加失败");
  741. }
  742. }
  743. }
  744. if($status==2){
  745. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  746. if(!empty($qrdArr)){
  747. foreach ($qrdArr as $value){
  748. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  749. if($qrdinfo==false){
  750. Db::rollback();
  751. return error_show(1003,"确认单信息未找到");
  752. }
  753. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  754. Db::rollback();
  755. return error_show(1003,"确认单信息开票金额不足");
  756. }
  757. $update =[
  758. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  759. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  760. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  761. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  762. "updatetime"=>date("Y-m-d H:i:s"),
  763. ];
  764. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  765. if($qrdup==false){
  766. Db::rollback();
  767. return error_show(1003,"确认单信息更新失败");
  768. }
  769. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  770. $assocup =Db::name("assoc")->where($value)->update($assoc);
  771. if($assocup==false){
  772. Db::rollback();
  773. return error_show(1003,"确认单关联信息更新失败");
  774. }
  775. }
  776. }
  777. }
  778. if($status==3 || $status==4){
  779. foreach ($qrd as $value){
  780. $report=ReportCode::where(["qrdNo"=>$value])->find();
  781. if($report)$report->rmField("returnInv",$returnCode);
  782. }
  783. }
  784. Db::commit();
  785. return app_show(0,"退票申请信息更新成功");
  786. }
  787. Db::rollback();
  788. return error_show(1005,"退票申请信息更新失败");
  789. }catch (\Exception $e){
  790. Db::rollback();
  791. return error_show(1005,$e->getMessage());
  792. }
  793. }
  794. //红冲票
  795. public function returnRed(){
  796. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  797. if($returnCode==""){
  798. return error_show(1005,"参数 returnCode 不能为空");
  799. }
  800. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  801. if($return==false){
  802. return error_show(1005,"退票申请信息未找到");
  803. }
  804. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  805. if($redinv==""){
  806. return error_show(1005,"参数 redinv 不能为空");
  807. }
  808. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  809. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  810. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  811. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  812. if($invinfo==false){
  813. return error_show(1005,"发票申请信息未找到");
  814. }
  815. Db::startTrans();
  816. try{
  817. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  818. $up =Db::name("invoice_return")->where($return)->update($update);
  819. if($up){
  820. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  821. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  822. if($inv){
  823. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  824. if(!empty($qrdArr)){
  825. foreach ($qrdArr as $value){
  826. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  827. if($qrdinfo==false){
  828. Db::rollback();
  829. return error_show(1003,"确认单信息未找到");
  830. }
  831. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  832. Db::rollback();
  833. return error_show(1003,"确认单信息开票金额不足");
  834. }
  835. $update =[
  836. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  837. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  838. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  839. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  840. "updatetime"=>date("Y-m-d H:i:s"),
  841. ];
  842. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  843. if($qrdup==false){
  844. Db::rollback();
  845. return error_show(1003,"确认单信息更新失败");
  846. }
  847. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  848. $assocup =Db::name("assoc")->where($value)->update($assoc);
  849. if($assocup==false){
  850. Db::rollback();
  851. return error_show(1003,"确认单关联信息更新失败");
  852. }
  853. }
  854. }
  855. Db::commit();
  856. return app_show(0,"退票申请信息更新成功");
  857. }
  858. }
  859. Db::rollback();
  860. return error_show(1005,"退票申请信息更新失败");
  861. }catch (\Exception $e){
  862. Db::rollback();
  863. return error_show(1005,$e->getMessage());
  864. }
  865. }
  866. //退票详情
  867. public function returnInfo(){
  868. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  869. if($returnCode==""){
  870. return error_show(1005,"参数 returnCode 不能为空");
  871. }
  872. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  873. if($return==false){
  874. return error_show(1005,"退票申请信息未找到");
  875. }
  876. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  877. $return['inv_out']= $invinfo['inv_out']??"";
  878. $return['inv_value']= $invinfo['inv_value']??"";
  879. $return['inv_apply_id']= $invinfo['apply_id']??"";
  880. $return['inv_apply_name']= $invinfo['apply_name']??"";
  881. $return['inv_type']= $invinfo['inv_type']??"";
  882. $return['open_type']= $invinfo['open_type']??"";
  883. $return['is_ticket']= $invinfo['is_ticket']??"";
  884. $return['email']= $invinfo['email']??"";
  885. $return['post_code']= $invinfo['post_code']??"";
  886. $return['post_company']= $invinfo['post_company']??"";
  887. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  888. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  889. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  890. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  891. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  892. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  893. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  894. $return['seller_title'] = $poolinfo['seller_title']??'';
  895. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  896. $return['seller_code'] = $poolinfo['seller_code']??'';
  897. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  898. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  899. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  900. $return['voider'] = $poolinfo['voider']??'';
  901. $return['payee'] = $poolinfo['payee']??'';
  902. $return['drawer'] = $poolinfo['drawer']??'';
  903. $return['reviewer'] = $poolinfo['reviewer']??'';
  904. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  905. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  906. $return['goodList']=$orderList;
  907. return app_show(0,"获取成功",$return);
  908. }
  909. /**
  910. *发票池列表
  911. */
  912. public function list(){
  913. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  914. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  915. $condition=[["a.is_del","=",0]];
  916. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  917. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  918. if($start!=""){
  919. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  920. }
  921. if($end!=""){
  922. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  923. }
  924. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  925. if($status!==""){
  926. $condition[]=["a.status","=",$status];
  927. }
  928. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  929. if($inv_type!==""){
  930. $condition[]=["a.inv_type","=",$inv_type];
  931. }
  932. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  933. if($inv_out!==""){
  934. $condition[]=["a.inv_out","=",$inv_out];
  935. }
  936. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  937. // if($inv_in!==""){
  938. // $condition[]=["a.inv_in","=",$inv_in];
  939. // }
  940. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  941. if($invNo!==""){
  942. $condition[]=["a.invNo","like","%$invNo%"];
  943. }
  944. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  945. if($inv_number!==""){
  946. $condition[]=["c.inv_number","like","%$inv_number%"];
  947. }
  948. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  949. if($inv_code!==""){
  950. $condition[]=["c.inv_code","like","%$inv_code%"];
  951. }
  952. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  953. if($inv_company!==""){
  954. $condition[]=["a.inv_company","like","%$inv_company%"];
  955. }
  956. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  957. if($buyer_name!==""){
  958. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  959. }
  960. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  961. if($apply_id!==""){
  962. $condition[]=["a.apply_id","=",$apply_id];
  963. }
  964. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  965. if($apply_name!==""){
  966. $condition[]=["a.apply_name","like","%$apply_name%"];
  967. }
  968. $count=Db::name("invoice_pool")->alias("a")
  969. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  970. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  971. ->where($condition)->count();
  972. $total =ceil($count/$size);
  973. $page= $page>$total ? intval($total):$page;
  974. $list=Db::name("invoice_pool")->alias("a")
  975. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  976. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  977. ->where($condition)->field("b.*,a.*,c.inv_code,c.inv_number,c.open_date,c.type")
  978. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  979. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  980. }
  981. //获取发票详情
  982. public function info(){
  983. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  984. if($invNo==''){
  985. return error_show(1004,"参数 invNo 不能为空");
  986. }
  987. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  988. if(empty($poolinfo)){
  989. return error_show(1004,"为找到开票信息");
  990. }
  991. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  992. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  993. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  994. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  995. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  996. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  997. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  998. $poolinfo['seller_title'] = $pool['seller_title']??'';
  999. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  1000. $poolinfo['seller_code'] = $pool['seller_code']??'';
  1001. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1002. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1003. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1004. $poolinfo['voider'] = $pool['voider']??'';
  1005. $poolinfo['payee'] = $pool['payee']??'';
  1006. $poolinfo['drawer'] = $pool['drawer']??'';
  1007. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1008. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1009. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  1010. $poolinfop['goodList']=$orderList;
  1011. $poolinfop['invinfo']=Db::name("invoice_ticket")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])->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'])->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.*")->json(["item"])->page($page,$size)->where($condition)->select()
  1822. ->toArray();
  1823. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1824. }
  1825. }