OrderInv.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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\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"=>1,// 待财务开票/待金税开票 待财务审核 待填写物流 开票完成 开票失败/开票驳回 财务驳回
  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. if($status==1){
  234. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  235. if($open_type!=1){
  236. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  237. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  238. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  239. if($total_fee==='') return error_show(1004,"参数 tatal_fee 不能为空");
  240. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  241. if($total_fee!=$invinfo['inv_value'])return error_show(1004,"发票金额不足");
  242. }else{
  243. $status=7;
  244. }
  245. }
  246. $update=[
  247. "status"=>$status,
  248. "exam_remark"=>$remark,
  249. "updatetime" => date("Y-m-d H:i:s")
  250. ];
  251. $status==1 ? $update['open_type']=$open_type :"";
  252. Db::startTrans();
  253. try{
  254. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  255. if($invup){
  256. if($status==1&& $open_type!=1){
  257. $invpool=[
  258. "invNo"=>$invinfo['invNo'],
  259. "inv_type"=>$open_type,
  260. "inv_code"=>$invCode,
  261. "inv_number"=>$invNum,
  262. "inv_total"=>$total_fee,
  263. "inv_subtotal"=>$subtotal_fee,
  264. "open_date"=>$open_date,
  265. "item"=>'',
  266. "status"=>0,
  267. "addtime"=>date("Y-m-d H:i:s"),
  268. "updatetime"=>date("Y-m-d H:i:s")
  269. ];
  270. $in = Db::name("invoice_ticket")->insert($invpool);
  271. if($in==false){
  272. Db::rollback();
  273. return error_show(1004,"发票信息添加失败");
  274. }
  275. }
  276. if($status==7 && $open_type==1){
  277. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  278. if(empty($invpoolinfo)){
  279. Db::rollback();
  280. return error_show(1004,"开票信息未找到");
  281. }
  282. $goodinfo =Db::name("invoice_good")
  283. ->where(["invNo"=>$invNo,"is_del"=>0])
  284. ->field("0 as FPHXZ,
  285. catNo as SPBM,
  286. id as ZXBM,
  287. is_discount as YHZCBS,
  288. if(inv_tag=0,'',inv_tag) as LSLBS,
  289. addTax as ZZSTSGL,
  290. goodName as XMMC,
  291. specInfo as GGXH,
  292. unitName as DW,
  293. goodNum as SPSL,
  294. goodPrice as DJ,
  295. totalPrice as JE,
  296. 1 HSBZ,
  297. tax as SL,
  298. taxPrice as SE")
  299. ->select()->toJson();
  300. if(empty($goodinfo)){
  301. Db::rollback();
  302. return error_show(1004,"开票商品信息未找到");
  303. }
  304. $data=[
  305. "invNo"=>$invNo,
  306. "KPLX"=>"0",
  307. "XHFSBH"=>$invpoolinfo['seller_code'],
  308. "XHFMC"=>$invpoolinfo['seller_title'],
  309. "XHFDZ"=>$invpoolinfo['seller_addr'],
  310. "XHFDH"=>$invpoolinfo['seller_mobile'],
  311. "XHFYH"=>$invpoolinfo['seller_bank'],
  312. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  313. "GMFBM"=>"",
  314. "GMFSBH"=>$invpoolinfo['buyer_code'],
  315. "GMFMC"=>$invpoolinfo['buyer_title'],
  316. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  317. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  318. "GMFYH"=>$invpoolinfo['buyer_bank'],
  319. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  320. "GMFLX"=>$invinfo['company_type'],
  321. "GMFSJH"=>"",
  322. "GMFDZYX"=>$invinfo['email'],
  323. "KPR"=>$invpoolinfo['drawer'],
  324. "SKR"=>$invpoolinfo['payee'],
  325. "FHR"=>$invpoolinfo['reviewer'],
  326. "QDBZ"=>"0",
  327. "JSHJ"=>$invinfo['inv_value'],
  328. "HJJE"=>"0",
  329. "HJSE"=>"0",
  330. "BZ"=>$invinfo['remark'],
  331. "DDMXXX"=>$goodinfo,
  332. "FPLXDM"=>invoiceType($invinfo['inv_type']),
  333. "DDQQPCH"=>"",
  334. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  335. "status"=>"0",
  336. "result"=>"",
  337. "addtime"=>date("Y-m-d H:i:s"),
  338. "updatetime"=>date("Y-m-d H:i:s")
  339. ];
  340. $invoice=Db::name("invoice_interface")->insert($data);
  341. if($invoice==false){
  342. Db::rollback();
  343. return error_show(1003,"开票信息生成失败");
  344. }
  345. Cache::push("invoiceList",$data["DDQQLSH"]);
  346. }
  347. if($status==4 ||$status==5 ){
  348. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  349. if(!empty($qrdArr)){
  350. foreach ($qrdArr as $value){
  351. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  352. if($qrdinfo==false){
  353. Db::rollback();
  354. return error_show(1003,"确认单信息未找到");
  355. }
  356. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  357. Db::rollback();
  358. return error_show(1003,"确认单信息开票金额不足");
  359. }
  360. $update =[
  361. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  362. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  363. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  364. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  365. "updatetime"=>date("Y-m-d H:i:s"),
  366. ];
  367. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  368. if($qrdup==false){
  369. Db::rollback();
  370. return error_show(1003,"确认单信息更新失败");
  371. }
  372. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  373. $assocup =Db::name("assoc")->where($value)->update($assoc);
  374. if($assocup==false){
  375. Db::rollback();
  376. return error_show(1003,"确认单关联信息更新失败");
  377. }
  378. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  379. if($report)$report->rmField("invNo",$invinfo['invNo']);
  380. }
  381. }
  382. }
  383. Db::commit();
  384. return app_show(0,"审核成功");
  385. }
  386. Db::rollback();
  387. return error_show(1004,"审核失败");
  388. }catch (\Exception $e){
  389. Db::rollback();
  390. return error_show(1004,$e->getMessage());
  391. }
  392. }
  393. /**
  394. * 发票设置物流
  395. */
  396. public function SetPost(){
  397. // @param invNo postCompany postCode postFee;
  398. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  399. if($invNo==''){
  400. return error_show(1004,"参数invNo不能为空");
  401. }
  402. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  403. if($invinfo==false){
  404. return error_show(1004,"发票申请信息未找到");
  405. }
  406. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  407. if($post_company==''){
  408. return error_show(1004,"参数 post_company 不能为空");
  409. }
  410. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  411. if($post_code==''){
  412. return error_show(1004,"参数 post_code 不能为空");
  413. }
  414. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floatval($this->post['post_fee']):"0";
  415. Db::startTrans();
  416. try{
  417. $data=[
  418. "post_company"=>$post_company,
  419. "post_code"=>$post_code,
  420. "post_fee"=>$post_fee,
  421. "status"=>3,
  422. "updatetime"=>date("Y-m-d H:i:s")
  423. ];
  424. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  425. if($up){
  426. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  427. if(!empty($qrdArr)){
  428. foreach ($qrdArr as $value){
  429. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  430. if($qrdinfo==false){
  431. Db::rollback();
  432. return error_show(1003,"确认单信息未找到");
  433. }
  434. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  435. Db::rollback();
  436. return error_show(1003,"确认单信息开票金额不足");
  437. }
  438. $update =[
  439. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  440. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  441. "invtime"=>date("Y-m-d H:i:s"),
  442. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  443. "updatetime"=>date("Y-m-d H:i:s"),
  444. ];
  445. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  446. if($qrdup==false){
  447. Db::rollback();
  448. return error_show(1003,"确认单信息更新失败");
  449. }
  450. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  451. $assocup =Db::name("assoc")->where($value)->update($assoc);
  452. if($assocup==false){
  453. Db::rollback();
  454. return error_show(1003,"确认单关联信息更新失败");
  455. }
  456. }
  457. }
  458. Db::commit();
  459. return app_show(0,"更新成功");
  460. }
  461. Db::rollback();
  462. return error_show(1003,"发票申请信息更新失败");
  463. }catch (\Exception $e){
  464. Db::rollback();
  465. return error_show(1004,$e->getMessage());
  466. }
  467. }
  468. /**
  469. * 发票废弃使用
  470. */
  471. public function Discard(){
  472. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  473. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  474. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  475. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  476. if(!in_array($invinfo['status'],[1,2,3,5]))return error_show(1004,"发票状态无法废弃");
  477. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  478. if($company==false)return error_show(1004,"开票业务公司未找到");
  479. $invoice =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  480. if($invoice==false)return error_show(1004,"发票详细信息未找到");
  481. Db::startTrans();
  482. try{
  483. $invoice=Config::get("invoice");
  484. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  485. $result=$tax->DeprecateInvoices($company['company_license'],invoiceType($invinfo['inv_type']),$invoice['inv_code'],$invoice['inv_number'],1);
  486. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  487. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  488. if($up){
  489. if($invinfo['status']!=5){
  490. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  491. if(!empty($qrdArr)){
  492. foreach ($qrdArr as $value){
  493. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  494. if($qrdinfo==false){
  495. Db::rollback();
  496. return error_show(1003,"确认单信息未找到");
  497. }
  498. if ($invinfo['status']==3){
  499. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  500. Db::rollback();
  501. return error_show(1003,"确认单信息开票金额不足");
  502. }
  503. $update =[
  504. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  505. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  506. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  507. "updatetime"=>date("Y-m-d H:i:s"),
  508. ];
  509. }else{
  510. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  511. Db::rollback();
  512. return error_show(1003,"确认单信息开票金额不足");
  513. }
  514. $update =[
  515. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  516. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  517. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  518. "updatetime"=>date("Y-m-d H:i:s"),
  519. ];
  520. }
  521. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  522. if($qrdup==false){
  523. Db::rollback();
  524. return error_show(1003,"确认单信息更新失败");
  525. }
  526. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  527. $assocup =Db::name("assoc")->where($value)->update($assoc);
  528. if($assocup==false){
  529. Db::rollback();
  530. return error_show(1003,"确认单关联信息更新失败");
  531. }
  532. }
  533. }
  534. }
  535. Db::commit();
  536. return app_show(0,"发票废弃成功");
  537. }
  538. }
  539. Db::rollback();
  540. return error_show(1004,"发票废弃失败");
  541. }catch (\Exception $e){
  542. Db::rollback();
  543. return error_show(1004,$e->getMessage());
  544. }
  545. }
  546. /**@param invNo return_reason remark 退票申请
  547. * @return \think\response\Json|void
  548. */
  549. public function ReturnAdd(){
  550. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  551. if($invNo==""){
  552. return error_show(1004,"参数 invNo 不能为空");
  553. }
  554. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  555. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  556. if($return_reason==""){
  557. return error_show(1004,"参数 return_reason 不能为空");
  558. }
  559. if($remark==""){
  560. return error_show(1004,"参数 remark 不能为空");
  561. }
  562. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  563. if($invinfo==false){
  564. return error_show(1004,"发票申请数据未找到");
  565. }
  566. if($invinfo['status']!=3){
  567. return error_show(1004,"发票申请未完成");
  568. }
  569. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  570. if(empty($qrd)){
  571. return error_show(1004,"未找到开票的销售单信息");
  572. }
  573. $returnCode = makeNo("RIN");
  574. $data=[
  575. "returnCode"=>$returnCode,
  576. "invNo"=>$invNo,
  577. "return_reason"=>$return_reason,
  578. "remark"=>$remark,
  579. "status"=>0,
  580. "apply_id"=>$this->uid,
  581. "apply_name"=>$this->uname,
  582. "addtime"=>date("Y-m-d H:i:s"),
  583. "updatetime"=>date("Y-m-d H:i:s")
  584. ];
  585. $inter = Db::name("invoice_return")->insert($data);
  586. if($inter){
  587. foreach ($qrd as $value){
  588. $report=ReportCode::where(["qrdNo"=>$value])->find();
  589. if($report)$report->setField("returnInv",$returnCode);
  590. }
  591. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  592. }else{
  593. return error_show(1005,"退票申请新建失败");
  594. }
  595. }
  596. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败
  597. public function ReturnStatus(){
  598. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  599. if($returnCode==""){
  600. return error_show(1005,"参数 returnCode 不能为空");
  601. }
  602. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  603. if ($status==""){
  604. return error_show(1005,"参数 status 不能为空");
  605. }
  606. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):"";
  607. if ($return_type==""){
  608. return error_show(1005,"参数 return_type 不能为空");
  609. }
  610. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  611. if($remark==""){
  612. return error_show(1005,"参数 remark 不能为空");
  613. }
  614. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  615. if($return==false){
  616. return error_show(1005,"退票申请信息未找到");
  617. }
  618. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  619. if($invinfo==false){
  620. return error_show(1004,"发票申请数据未找到");
  621. }
  622. if($invinfo['status']!=3){
  623. return error_show(1004,"发票申请未完成");
  624. }
  625. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  626. if(empty($qrd)){
  627. return error_show(1004,"未找到开票的销售单信息");
  628. }
  629. Db::startTrans();
  630. try{
  631. $update=["status"=>$status,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  632. $up =Db::name("invoice_return")->where($return)->update($update);
  633. if($up){
  634. if($status==2){
  635. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  636. if(!empty($qrdArr)){
  637. foreach ($qrdArr as $value){
  638. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  639. if($qrdinfo==false){
  640. Db::rollback();
  641. return error_show(1003,"确认单信息未找到");
  642. }
  643. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  644. Db::rollback();
  645. return error_show(1003,"确认单信息开票金额不足");
  646. }
  647. $update =[
  648. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  649. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  650. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  651. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  652. "updatetime"=>date("Y-m-d H:i:s"),
  653. ];
  654. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  655. if($qrdup==false){
  656. Db::rollback();
  657. return error_show(1003,"确认单信息更新失败");
  658. }
  659. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  660. $assocup =Db::name("assoc")->where($value)->update($assoc);
  661. if($assocup==false){
  662. Db::rollback();
  663. return error_show(1003,"确认单关联信息更新失败");
  664. }
  665. }
  666. }
  667. }
  668. if($status==3 || $status==4){
  669. foreach ($qrd as $value){
  670. $report=ReportCode::where(["qrdNo"=>$value])->find();
  671. if($report)$report->rmField("returnInv",$returnCode);
  672. }
  673. }
  674. Db::commit();
  675. return app_show(0,"退票申请信息更新成功");
  676. }
  677. Db::rollback();
  678. return error_show(1005,"退票申请信息更新失败");
  679. }catch (\Exception $e){
  680. Db::rollback();
  681. return error_show(1005,$e->getMessage());
  682. }
  683. }
  684. //红冲票
  685. public function returnRed(){
  686. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  687. if($returnCode==""){
  688. return error_show(1005,"参数 returnCode 不能为空");
  689. }
  690. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  691. if($return==false){
  692. return error_show(1005,"退票申请信息未找到");
  693. }
  694. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  695. if($redinv==""){
  696. return error_show(1005,"参数 redinv 不能为空");
  697. }
  698. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  699. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  700. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  701. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  702. if($invinfo==false){
  703. return error_show(1005,"发票申请信息未找到");
  704. }
  705. Db::startTrans();
  706. try{
  707. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  708. $up =Db::name("invoice_return")->where($return)->update($update);
  709. if($up){
  710. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  711. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  712. if($inv){
  713. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  714. if(!empty($qrdArr)){
  715. foreach ($qrdArr as $value){
  716. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  717. if($qrdinfo==false){
  718. Db::rollback();
  719. return error_show(1003,"确认单信息未找到");
  720. }
  721. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  722. Db::rollback();
  723. return error_show(1003,"确认单信息开票金额不足");
  724. }
  725. $update =[
  726. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  727. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  728. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  729. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  730. "updatetime"=>date("Y-m-d H:i:s"),
  731. ];
  732. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  733. if($qrdup==false){
  734. Db::rollback();
  735. return error_show(1003,"确认单信息更新失败");
  736. }
  737. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  738. $assocup =Db::name("assoc")->where($value)->update($assoc);
  739. if($assocup==false){
  740. Db::rollback();
  741. return error_show(1003,"确认单关联信息更新失败");
  742. }
  743. }
  744. }
  745. Db::commit();
  746. return app_show(0,"退票申请信息更新成功");
  747. }
  748. }
  749. Db::rollback();
  750. return error_show(1005,"退票申请信息更新失败");
  751. }catch (\Exception $e){
  752. Db::rollback();
  753. return error_show(1005,$e->getMessage());
  754. }
  755. }
  756. //退票详情
  757. public function returnInfo(){
  758. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  759. if($returnCode==""){
  760. return error_show(1005,"参数 returnCode 不能为空");
  761. }
  762. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  763. if($return==false){
  764. return error_show(1005,"退票申请信息未找到");
  765. }
  766. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  767. $return['inv_out']= $invinfo['inv_out']??"";
  768. $return['inv_value']= $invinfo['inv_value']??"";
  769. $return['inv_apply_id']= $invinfo['apply_id']??"";
  770. $return['inv_apply_name']= $invinfo['apply_name']??"";
  771. $return['inv_type']= $invinfo['inv_type']??"";
  772. $return['open_type']= $invinfo['open_type']??"";
  773. $return['is_ticket']= $invinfo['is_ticket']??"";
  774. $return['email']= $invinfo['email']??"";
  775. $return['post_code']= $invinfo['post_code']??"";
  776. $return['post_company']= $invinfo['post_company']??"";
  777. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  778. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  779. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  780. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  781. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  782. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  783. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  784. $return['seller_title'] = $poolinfo['seller_title']??'';
  785. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  786. $return['seller_code'] = $poolinfo['seller_code']??'';
  787. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  788. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  789. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  790. $return['voider'] = $poolinfo['voider']??'';
  791. $return['payee'] = $poolinfo['payee']??'';
  792. $return['drawer'] = $poolinfo['drawer']??'';
  793. $return['reviewer'] = $poolinfo['reviewer']??'';
  794. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  795. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  796. $return['goodList']=$orderList;
  797. return app_show(0,"获取成功",$return);
  798. }
  799. /**
  800. *发票池列表
  801. */
  802. public function list(){
  803. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  804. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  805. $condition=[["a.is_del","=",0]];
  806. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  807. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  808. if($start!=""){
  809. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  810. }
  811. if($end!=""){
  812. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  813. }
  814. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  815. if($status!==""){
  816. $condition[]=["a.status","=",$status];
  817. }
  818. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  819. if($inv_type!==""){
  820. $condition[]=["a.inv_type","=",$inv_type];
  821. }
  822. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  823. if($inv_out!==""){
  824. $condition[]=["a.inv_out","=",$inv_out];
  825. }
  826. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  827. // if($inv_in!==""){
  828. // $condition[]=["a.inv_in","=",$inv_in];
  829. // }
  830. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  831. if($invNo!==""){
  832. $condition[]=["a.invNo","like","%$invNo%"];
  833. }
  834. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  835. if($inv_number!==""){
  836. $condition[]=["c.inv_number","like","%$inv_number%"];
  837. }
  838. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  839. if($inv_code!==""){
  840. $condition[]=["c.inv_code","like","%$inv_code%"];
  841. }
  842. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  843. if($inv_company!==""){
  844. $condition[]=["a.inv_company","like","%$inv_company%"];
  845. }
  846. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  847. if($buyer_name!==""){
  848. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  849. }
  850. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  851. if($apply_id!==""){
  852. $condition[]=["a.apply_id","=",$apply_id];
  853. }
  854. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  855. if($apply_name!==""){
  856. $condition[]=["a.apply_name","like","%$apply_name%"];
  857. }
  858. $count=Db::name("invoice_pool")->alias("a")
  859. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  860. ->leftJoin("invoice_ticket c","a.invNo=c.invNo")
  861. ->where($condition)->count();
  862. $total =ceil($count/$size);
  863. $page= $page>$total ? intval($total):$page;
  864. $list=Db::name("invoice_pool")->alias("a")
  865. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  866. ->leftJoin("invoice_ticket c","a.invNo=c.invNo")
  867. ->where($condition)->field("b.*,a.*,c.inv_code,c.inv_number,c.open_date")
  868. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  869. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  870. }
  871. //获取发票详情
  872. public function info(){
  873. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  874. if($invNo==''){
  875. return error_show(1004,"参数 invNo 不能为空");
  876. }
  877. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  878. if(empty($poolinfo)){
  879. return error_show(1004,"为找到开票信息");
  880. }
  881. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  882. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  883. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  884. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  885. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  886. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  887. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  888. $poolinfo['seller_title'] = $pool['seller_title']??'';
  889. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  890. $poolinfo['seller_code'] = $pool['seller_code']??'';
  891. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  892. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  893. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  894. $poolinfo['voider'] = $pool['voider']??'';
  895. $poolinfo['payee'] = $pool['payee']??'';
  896. $poolinfo['drawer'] = $pool['drawer']??'';
  897. $poolinfo['reviewer'] = $pool['reviewer']??'';
  898. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  899. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  900. $poolinfop['goodList']=$orderList;
  901. $poolinfop['invinfo']=Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->findOrEmpty();
  902. return app_show(0,"获取成功",$poolinfo);
  903. }
  904. //退票列表
  905. public function returnList(){
  906. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  907. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  908. $condition=[["b.is_del","=",0]];
  909. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  910. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  911. if($start!=""){
  912. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  913. }
  914. if($end!=""){
  915. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  916. }
  917. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  918. if($inv_type!==""){
  919. $condition[]=["b.inv_type","=",$inv_type];
  920. }
  921. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  922. if($apply_id!==""){
  923. $condition[]=["a.apply_id","=",$apply_id];
  924. }
  925. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  926. if($apply_name!==""){
  927. $condition[]=["a.apply_name","like","%$apply_name%"];
  928. }
  929. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  930. if($status!==""){
  931. $condition[]=["a.status","=",$status];
  932. }
  933. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  934. if($invNo!==""){
  935. $condition[]=["a.invNo","like","%$invNo%"];
  936. }
  937. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  938. if($returnCode!==""){
  939. $condition[]=["a.returnCode","like","%$returnCode%"];
  940. }
  941. $count=Db::name("invoice_return")->alias("a")
  942. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  943. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  944. ->where($condition)->count();
  945. $total =ceil($count/$size);
  946. $page= $page>$total ? intval($total):$page;
  947. $list=Db::name("invoice_return")->alias("a")
  948. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  949. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  950. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  951. 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,
  952. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,
  953. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->order("id desc")->select()->toArray();
  954. foreach ($list as &$value){
  955. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  956. $value['companyName'] = $company['company_name']??"";
  957. }
  958. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  959. }
  960. //红冲申请
  961. public function applyRed(){
  962. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  963. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  964. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  965. if($invfo==false) return error_show(1004,"发票数据未找到");
  966. if(!in_array($invfo['status'],[1,2,3,4,5]))return error_show(1004,"发票数据状态无法申请");
  967. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  968. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  969. $company =Db::name("company_info")->where(["companyNo"=>$invfo['inv_out']])->findOrEmpty();
  970. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  971. $invoice=Config::get("invoice");
  972. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  973. $redticketList=[];
  974. $redticketArry=[
  975. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  976. "NSRSBH"=>$company['company_license'],
  977. "FPLXDM"=>invoiceType($invfo['inv_type']),
  978. ];
  979. $goodinfo =Db::name("invoice_good")
  980. ->where(["invNo"=>$invNo,"is_del"=>0])
  981. ->field("0 as FPHXZ,
  982. catNo as SPBM,
  983. id as ZXBM,
  984. is_discount as YHZCBS,
  985. if(inv_tag=0,'',inv_tag) as LSLBS,
  986. addTax as ZZSTSGL,
  987. goodName as XMMC,
  988. unitName as DW,
  989. -goodNum as SPSL,
  990. goodPrice as DJ,
  991. -totalPrice as JE,
  992. 1 HSBZ,
  993. tax as SL,
  994. -taxPrice as SE")->select()->toArray();
  995. if(empty($goodinfo)){
  996. Db::rollback();
  997. return error_show(1004,"开票商品信息未找到");
  998. }
  999. $info=[
  1000. "SQBSCQQLSH"=>randomkeys(29).date("YmdHis"),
  1001. "YYSBZ"=> "0000000000", //营业税标志
  1002. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1003. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1004. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1005. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1006. "TKSJ"=>date("YmdHis"), //填开时间
  1007. "XHFSBH"=>$ticket['seller_id'],
  1008. "XHFMC"=>$ticket['seller_title'],
  1009. "GMFSBH"=>$ticket['buyer_id'],
  1010. "GMFMC"=> $ticket['buyer_title'],
  1011. "HJJE"=> "-".$ticket['inv_total'],//合计金额(不含 税)
  1012. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1013. "SQSM"=>"", //申请说明
  1014. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1015. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1016. ];
  1017. $invRed=makeNo("IRE");
  1018. $data=[
  1019. 'invNo'=>$invNo,
  1020. "invRed"=>$invRed,
  1021. "apply_id"=>$this->uid,
  1022. "apply_name"=>$this->uname,
  1023. "status"=>0,
  1024. "remark"=>'',
  1025. "addtime"=>date("Y-m-d H:i:s"),
  1026. "updatetime"=>date("Y-m-d H:i:s"),
  1027. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1028. "YYSBZ"=> "0000000000", //营业税标志
  1029. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1030. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1031. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1032. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1033. "TKSJ"=>date("YmdHis"), //填开时间
  1034. "XHFSBH"=>$ticket['seller_id'],
  1035. "XHFMC"=>$ticket['seller_title'],
  1036. "GMFSBH"=>$ticket['buyer_id'],
  1037. "GMFMC"=> $ticket['buyer_title'],
  1038. "HJJE"=> "-".$ticket['inv_total'],//合计金额(不含 税)
  1039. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1040. "SQSM"=>"", //申请说明
  1041. "XXBTSBS"=>"0",
  1042. "DDMXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1043. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1044. "FPLXDM"=>invoiceType($invfo['inv_type']),
  1045. ];
  1046. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$goodinfo];
  1047. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1048. $data['status']=5;
  1049. $data['remark']='红冲申请提交失败';
  1050. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1051. $resultChild=$result['HZSQDSCJG'][0];
  1052. if($resultChild['ZTDM']=='060000'){
  1053. $data['status']=2;
  1054. $data['remark']='红冲申请审核通过';
  1055. }
  1056. if($resultChild['ZTDM']=='060001'){
  1057. $data['status']=1;
  1058. $data['remark']='红冲申请审核中';
  1059. }
  1060. }
  1061. $up =Db::name("invoice_red")->insert($data);
  1062. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1063. }
  1064. //批量申请红冲发票
  1065. public function batchApplyRed(){
  1066. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1067. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1068. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1069. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1070. $insertAll=[];
  1071. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1072. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1073. if(count($companyCode)>1){
  1074. return error_show(1004,"业务企业公司不统一");
  1075. }
  1076. if(count($inv_type)>1){
  1077. return error_show(1004,"发票类型不统一");
  1078. }
  1079. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1080. if(empty($company)) {
  1081. return error_show(1004,"业务企业信息未找到");
  1082. }
  1083. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1084. if(empty($ticketArr)){
  1085. return error_show(1004,"未找到开票信息");
  1086. }
  1087. $invoice=Config::get("invoice");
  1088. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1089. Db::startTrans();
  1090. try{
  1091. $redticketArry=[
  1092. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1093. "NSRSBH"=>$company['company_license'],
  1094. "FPLXDM"=>invoiceType($inv_type[0]),
  1095. ];
  1096. $redticketList=[];
  1097. foreach ($invfo as $value){
  1098. if(!in_array($value['status'],[1,2,3,4,5])){
  1099. Db::rollback();
  1100. return error_show(1004,"发票数据状态无法申请");
  1101. }
  1102. $ticket =$ticketArr[$value['invNo']] ??[];
  1103. if(empty($ticket)){
  1104. Db::rollback();
  1105. return error_show(1004,"未找到开票信息");
  1106. }
  1107. $goodinfo =Db::name("invoice_good")
  1108. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1109. ->field(" 6 as FPHXZ,
  1110. catNo as SPBM,
  1111. id as ZXBM,
  1112. is_discount as YHZCBS,
  1113. if(inv_tag=0,'',inv_tag) as LSLBS,
  1114. addTax as ZZSTSGL,
  1115. goodName as XMMC,
  1116. '' as DW,
  1117. '' as SPSL,
  1118. '' as DJ,
  1119. (-totalPrice+taxPrice) as JE,
  1120. 1 HSBZ,
  1121. tax as SL,
  1122. -taxPrice as SE")
  1123. ->select()->toArray();
  1124. if(empty($goodinfo)){
  1125. Db::rollback();
  1126. return error_show(1004,"开票商品信息未找到");
  1127. }
  1128. $len =array_unique(array_column($goodinfo,'SL'));
  1129. $qdmx=["FPHXZ"=>6,
  1130. "SPBM"=>'',
  1131. "XMMC"=>"详见对应正数发票及清单",
  1132. "YHZCBS"=>0,
  1133. "LSLBS"=>'',
  1134. "ZZSTSGL"=>'',
  1135. "DW"=>'',
  1136. "SPSL"=>'',
  1137. "DJ"=>'',
  1138. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1139. "HSBZ"=>'0',
  1140. "SL"=>count($len)>1?'':$len[0],
  1141. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1142. ];
  1143. $info=[
  1144. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1145. "YYSBZ"=> "0000000000", //营业税标志
  1146. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1147. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1148. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1149. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1150. "TKSJ"=>date("YmdHis"), //填开时间
  1151. "XHFSBH"=>$ticket['seller_id'],
  1152. "XHFMC"=>$ticket['seller_title'],
  1153. "GMFSBH"=>$ticket['buyer_id'],
  1154. "GMFMC"=> $ticket['buyer_title'],
  1155. "HJJE"=> "-".$ticket['inv_total'],//合计金额(不含 税)
  1156. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1157. "SQSM"=>"1100000000", //申请说明
  1158. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1159. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1160. ];
  1161. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1162. $qdmx['HSBZ']=1;
  1163. $data=[
  1164. 'invNo'=>$value['invNo'],
  1165. "invRed"=>makeNo("IRE"),
  1166. "apply_id"=>$this->uid,
  1167. "apply_name"=>$this->uname,
  1168. "status"=>5,
  1169. "remark"=>'红冲申请提交失败',
  1170. "addtime"=>date("Y-m-d H:i:s"),
  1171. "updatetime"=>date("Y-m-d H:i:s"),
  1172. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1173. "YYSBZ"=> "0000000000", //营业税标志
  1174. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1175. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1176. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1177. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1178. "TKSJ"=>date("YmdHis"), //填开时间
  1179. "XHFSBH"=>$ticket['seller_id'],
  1180. "XHFMC"=>$ticket['seller_title'],
  1181. "GMFSBH"=>$ticket['buyer_id'],
  1182. "GMFMC"=> $ticket['buyer_title'],
  1183. "HJJE"=> "-".$ticket['inv_total'],//合计金额(不含 税)
  1184. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1185. "SQSM"=>"1100000000", //申请说明
  1186. "XXBTSBS"=>"0",
  1187. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1188. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1189. "FPLXDM"=>invoiceType($value['inv_type']),
  1190. ];
  1191. $insertAll[$info['SQBSCQQLSH']]=$data;
  1192. }
  1193. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1194. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1195. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1196. foreach ($result['HZSQDSCJG'] as $value){
  1197. if($value['ZTDM']=='060000'){
  1198. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1199. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1200. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1201. continue;
  1202. // $data['status']=2;
  1203. // $data['remark']='红冲申请审核通过';
  1204. }
  1205. if($value['ZTDM']=='060001'){
  1206. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1207. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1208. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1209. continue;
  1210. }
  1211. unset($insertAll[$value['SQBSCQQLSH']]);
  1212. }
  1213. }
  1214. }else{
  1215. Db::rollback();
  1216. return error_show(1004,"红冲发票申请失败");
  1217. }
  1218. $uns= Db::name("invoice_red")->insertAll($insertAll);
  1219. if($uns){
  1220. Db::commit();
  1221. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  1222. }
  1223. Db::rollback();
  1224. return error_show(1004,"红冲发票申请新建失败");
  1225. }catch (\Exception $e){
  1226. Db::rollback();
  1227. return error_show(1004,$e->getMessage());
  1228. }
  1229. }
  1230. //红票申请开票
  1231. public function redOpen(){
  1232. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  1233. if($invRed==''){
  1234. return error_show(1004,"参数 invRed 不能为空");
  1235. }
  1236. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  1237. if(empty($redinfo)){
  1238. return error_show(1004,"红冲发票申请不存在");
  1239. }
  1240. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  1241. if($redreason==''){
  1242. return error_show(1004,"参数 redreason 不能为空");
  1243. }
  1244. if($redinfo['status']!=2){
  1245. return error_show(1004,"数据状态不可提交申请");
  1246. }
  1247. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  1248. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  1249. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  1250. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  1251. Db::startTrans();
  1252. try{
  1253. $data=[
  1254. "invNo"=>$invRed,
  1255. "KPLX"=>"1",
  1256. "XHFSBH"=>$invpoolinfo['seller_id'],
  1257. "XHFMC"=>$invpoolinfo['seller_title'],
  1258. "XHFDZ"=>$invpoolinfo['seller_addr'],
  1259. "XHFDH"=>$invpoolinfo['seller_mobile'],
  1260. "XHFYH"=>$invpoolinfo['seller_bank'],
  1261. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  1262. "GMFBM"=>"",
  1263. "GMFSBH"=>$invpoolinfo['buyer_id'],
  1264. "GMFMC"=>$invpoolinfo['buyer_title'],
  1265. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  1266. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  1267. "GMFYH"=>$invpoolinfo['buyer_bank'],
  1268. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  1269. "GMFLX"=>$invinfo['company_type'],
  1270. "GMFSJH"=>"",
  1271. "GMFDZYX"=>$invinfo['email'],
  1272. "KPR"=>$invpoolinfo['issuer'],
  1273. "SKR"=>$invpoolinfo['reciver'],
  1274. "FHR"=>$invpoolinfo['reviewer'],
  1275. "QDBZ"=>"0",
  1276. "YFPDM"=>$redinfo['YFPDM'],
  1277. "YFPHM"=>$redinfo['YFPHM'],
  1278. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  1279. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  1280. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  1281. "TSCHBZ"=>"0",
  1282. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  1283. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  1284. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  1285. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  1286. "DDMXXX"=>$redinfo['DDMXXX'],
  1287. "FPLXDM"=>$redinfo['FPLXDM'],
  1288. "DDQQPCH"=>"",
  1289. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1290. "status"=>"0",
  1291. "result"=>"",
  1292. "addtime"=>date("Y-m-d H:i:s"),
  1293. "updatetime"=>date("Y-m-d H:i:s")
  1294. ];
  1295. $invoice=Db::name("invoice_interface")->insert($data);
  1296. if($invoice==false){
  1297. Db::rollback();
  1298. return error_show(1003,"红冲开票信息生成失败");
  1299. }
  1300. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  1301. if($upred==false){
  1302. Db::rollback();
  1303. return error_show(1003,"红冲发票信息更新失败");
  1304. }
  1305. Db::commit();
  1306. return app_show(0,"红冲发票信息开票提交成功");
  1307. }catch (\Exception $e){
  1308. Db::rollback();
  1309. return error_show(1003,$e->getMessage());
  1310. }
  1311. }
  1312. //红冲票申请列表
  1313. public function redList(){
  1314. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1315. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1316. $condition=[["is_del","=",0]];
  1317. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  1318. if($invRed!=''){
  1319. $condition[]=["invRed","like","%$invRed%"];
  1320. }
  1321. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  1322. if($apply_id!=''){
  1323. $condition[]=["apply_id","=",$apply_id];
  1324. }
  1325. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  1326. if($invNo!=''){
  1327. $condition[]=["invNo","like","%$invNo%"];
  1328. }
  1329. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  1330. if($apply_name!=''){
  1331. $condition[]=["apply_name","like","%$apply_name%"];
  1332. }
  1333. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  1334. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  1335. if($start!=''){
  1336. $condition[]=["addtime",">=",$start];
  1337. }
  1338. if($end!=''){
  1339. $condition[]=["addtime","<=",$end];
  1340. }
  1341. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  1342. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  1343. if($total_low!=''){
  1344. $condition[]=["ABS(HJJE)",">=",$total_low];
  1345. }
  1346. if($total_up!=''){
  1347. $condition[]=["ABS(HJJE)","<=",$total_up];
  1348. }
  1349. $count= Db::name("invoice_red")->where($condition)->count();
  1350. $total =ceil($count/$size);
  1351. $page=$total>=$page?$page :intval($total);
  1352. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  1353. ->toArray();
  1354. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1355. }
  1356. /**
  1357. * @return \think\response\Json|void
  1358. */
  1359. public function ticketInfo(){
  1360. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1361. if($num==''){
  1362. return error_show(1004,'参数 number 不能为空');
  1363. }
  1364. $condition=[];
  1365. $condition[]=["inv_number","=",$num];
  1366. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1367. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  1368. $info =Db::name("invoice_ticket")->json(['item'])->where($condition)->findOrEmpty();
  1369. if(!empty($info)){
  1370. array_walk($info['item'],function (&$value){
  1371. $value['SL'] = ($value['SL']*100)."%";
  1372. });
  1373. }
  1374. return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
  1375. }
  1376. }