OrderInv.php 88 KB

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