OrderInv.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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\Db;
  7. use think\facade\Cache;
  8. class OrderInv extends BaseController{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. }
  12. /**
  13. * 新建销售单开票申请
  14. */
  15. public function create(){
  16. $companyType = isset($this->post['companyType'])&&$this->post['companyType']!=='' ? trim($this->post['companyType']):"";
  17. if ($companyType==''){
  18. return error_show(1004,"参数 companyType 不能为空");
  19. }
  20. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  21. if($buy_id==''){
  22. return error_show(1004,"参数 buy_id 不能为空");
  23. }
  24. $buyinfo =Db::name("customer_invoice")->where(['id'=>$buy_id,"is_del"=>0])->find();
  25. if($buyinfo==false){
  26. return error_show(1004,"购买方发票信息未找到");
  27. }
  28. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  29. if($companyNo==''){
  30. return error_show(1004,"参数 companyNo 不能为空");
  31. }
  32. $company = Db::name("company_info")->where(["companyNo"=>$companyNo])->find();
  33. if($company==false){
  34. return error_show(1004,"销售方信息未找到");
  35. }
  36. // $khNo = isset($this->post['khNo'])&&$this->post['khNo']!=='' ? trim($this->post['khNo']):"";
  37. // if($khNo==''){
  38. // return error_show(1004,"参数 khNo 不能为空");
  39. // }
  40. // $customer = Db::name("customer_info")->where(["companyNo"=>$khNo])->find();
  41. // if($customer==false){
  42. // return error_show(1004,"客户信息未找到");
  43. // }
  44. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? intval($this->post['invtype']):"";
  45. if($invtype==''){
  46. return error_show(1004,"参数 invtype 不能为空");
  47. }
  48. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  49. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  50. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  51. if(empty($orderArr)){
  52. return error_show(1004,"参数 orderArr 不能为空");
  53. }
  54. $temp =[];
  55. $good=[];
  56. $invNo=makeNo("INV");
  57. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  58. Db::startTrans();
  59. try{
  60. foreach ($orderArr as $value){
  61. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  62. Db::rollback();
  63. return error_show(1004,"参数 sequenceNo 不能为空");
  64. }
  65. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  66. if($qrd['status']==2){
  67. Db::rollback();
  68. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  69. }
  70. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  71. Db::rollback();
  72. return error_show(1004,"参数 inv_fee 不能为空");
  73. }
  74. if($qrd['winv_fee']<$value['inv_fee']){
  75. Db::rollback();
  76. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  77. }
  78. $good_info = Db::name("good")->where(["spuCode"=>$qrd["goodNo"],"status"=>1])->findOrEmpty();
  79. if(empty($good_info)){
  80. Db::rollback();
  81. return error_show(1004,"确认单{$value['sequenceNo']}商品未确认开票类目");
  82. }
  83. $ascc=[
  84. "assocNo"=>makeNo("AS"),
  85. "apply_id"=>$this->uid,
  86. "apply_name"=>$this->uname,
  87. "type"=>1,
  88. "orderCode"=>$value['sequenceNo'],
  89. "customerNo"=>$qrd['customerNo'],
  90. "viceCode"=>$invNo,
  91. "order_total"=>$qrd['totalPrice'],
  92. "vice_total"=>$invfee,
  93. "cancel_fee"=>$value['inv_fee'],
  94. "status"=>1,
  95. "addtime"=>date("Y-m-d H:i:s"),
  96. "updatetime"=>date("Y-m-d H:i:s")
  97. ];
  98. $invoice_good=[
  99. "invNo"=>$invNo,
  100. "orderCode"=>$value['sequenceNo'],
  101. "goodNo"=>$qrd['goodNo'],
  102. "goodName"=>$good_info['inv_good_name'],
  103. "catName"=>$good_info['inv_cat_name'],
  104. "catNo"=>$good_info['inv_cat_code'],
  105. "goodPrice"=>round($value['inv_fee']/$value['num'],8),
  106. "unitName"=>$qrd['goodUnit'],
  107. "goodNum"=>$value['num'],
  108. "specInfo"=>'',
  109. "totalPrice"=>$value['inv_fee'],
  110. "tax"=>$good_info['inv_tax'],
  111. "is_discount"=>$good_info['is_discount'],
  112. "addTax"=>$good_info['addTax'],
  113. "inv_tag"=>$good_info['inv_tag'],
  114. "taxPrice"=>round($value['inv_fee']*$good_info['inv_tax'],2),
  115. "addtime"=>date("Y-m-d H:i:s"),
  116. "updatetime"=>date("Y-m-d H:i:s")
  117. ];
  118. $update = [
  119. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  120. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  121. "inv_status"=>2,
  122. "status"=>1,
  123. "updatetime"=>date("Y-m-d H:i:s")
  124. ];
  125. $up = Db::name("qrd_info")->where($qrd)->update($update);
  126. if($up==false){
  127. Db::rollback();
  128. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  129. }
  130. $temp[]=$ascc;
  131. $good[]=$invoice_good;
  132. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  133. if($report)$report->setField("invNo",$invNo);
  134. }
  135. $associn = Db::name("assoc")->insertAll($temp);
  136. if($associn==0){
  137. Db::rollback();
  138. return error_show(1005,"确认单申请开票失败");
  139. }
  140. $goodin = Db::name("invoice_good")->insertAll($good);
  141. if($goodin==0){
  142. Db::rollback();
  143. return error_show(1005,"确认单申请开票失败");
  144. }
  145. $inv=[
  146. "invNo"=>$invNo,
  147. "inv_value"=>$invfee,
  148. "inv_out"=> $companyNo,
  149. "apply_id"=>$this->uid,
  150. "apply_name"=>$this->uname,
  151. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通
  152. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  153. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  154. "company_type"=> $buyinfo['company_type'], //类型01 企业,02 机关事业单位,03 个人, 04 其他
  155. "remark"=>$remark, //申请备注
  156. "exam_remark"=>'', //审核备注
  157. "ainv_fee"=>0,//已核销金额
  158. "winv_fee"=>$invfee,//未核销金额
  159. "status"=>0,// 待财务开票/待金税开票 待财务审核 待填写物流 开票完成 开票失败/开票驳回 财务驳回
  160. "is_del"=>0,
  161. "email"=>$email,
  162. "addtime"=>date("Y-m-d H:i:s"),
  163. "updatetime"=>date("Y-m-d H:i:s")
  164. ];
  165. $invin = Db::name("invoice_pool")->insert($inv);
  166. if($invin){
  167. $invinfo=[
  168. "buyer_title"=>$buyinfo['invoice_title'],
  169. "buyer_code"=>$buyinfo['invoice_code'],
  170. "buyer_addr"=>$buyinfo['invoice_addr'],
  171. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  172. "buyer_bank"=>$buyinfo['invoice_bank'],
  173. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  174. "seller_title"=>$company['company_name'],
  175. "seller_code"=>$company['company_license'],
  176. "seller_addr"=>$company['company_address'],
  177. "seller_mobile"=>$company['mobile'],
  178. "seller_bank"=>$company['bank_name'],
  179. "seller_bankNo"=>$company['bankNo'],
  180. "invNo"=>$invNo,
  181. "voider"=>$company['voider'],
  182. "payee"=>$company['payee'],
  183. "drawer"=>$company['drawer'],
  184. "reviewer"=>$company['reviewer'],
  185. "ownerPlace"=>$company['ownerPlace'],
  186. "addtime"=>date("Y-m-d H:i:s")
  187. ];
  188. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  189. if($poolinfo){
  190. Db::commit();
  191. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  192. }
  193. }
  194. Db::rollback();
  195. return error_show(1004,"确认单发票申请失败");
  196. }catch (\Exception $e){
  197. Db::rollback();
  198. return error_show(1004,$e->getMessage());
  199. }
  200. }
  201. // 0 待财务开票/待金税开票 1待财务审核 2待填写物流 3开票完成 4开票失败/开票驳回 5财务驳回 6退票 7金税开票中
  202. public function status(){
  203. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  204. if($invNo==""){
  205. return error_show(1004,"参数 invNo 不能为空");
  206. }
  207. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  208. if($invinfo==false){
  209. return error_show(1004,"发票申请数据未找到");
  210. }
  211. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  212. if($status===''){
  213. return error_show(1004,"参数 status 不能为空");
  214. }
  215. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  216. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  217. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  218. // $invArr=isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  219. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  220. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  221. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  222. $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floor($this->post['total_fee']):"";
  223. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floor($this->post['subtotal_fee']):"";
  224. if($status==1){
  225. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  226. if($open_type!=1){
  227. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  228. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  229. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  230. if($total_fee==='') return error_show(1004,"参数 tatal_fee 不能为空");
  231. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  232. if($total_fee!=$invinfo['inv_value'])return error_show(1004,"发票金额不足");
  233. }else{
  234. $status=7;
  235. }
  236. }
  237. $update=[
  238. "status"=>$status,
  239. "exam_remark"=>$remark,
  240. "updatetime" => date("Y-m-d H:i:s")
  241. ];
  242. $status==1 ? $update['open_type']=$open_type :"";
  243. Db::startTrans();
  244. try{
  245. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  246. if($invup){
  247. if($status==1&& $open_type!=1){
  248. $invpool=[
  249. "invNo"=>$invinfo['invNo'],
  250. "inv_type"=>$open_type,
  251. "inv_code"=>$invCode,
  252. "inv_number"=>$invNum,
  253. "inv_total"=>$total_fee,
  254. "inv_subtotal"=>$subtotal_fee,
  255. "open_date"=>$open_date,
  256. "status"=>1,
  257. "addtime"=>date("Y-m-d H:i:s"),
  258. "updatetime"=>date("Y-m-d H:i:s")
  259. ];
  260. $in = Db::name("invoice_ticket")->insert($invpool);
  261. if($in==false){
  262. Db::rollback();
  263. return error_show(1004,"发票信息添加失败");
  264. }
  265. }
  266. if($status==7 && $open_type==1){
  267. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  268. if(empty($invpoolinfo)){
  269. Db::rollback();
  270. return error_show(1004,"开票信息未找到");
  271. }
  272. $goodinfo =Db::name("invoice_good")
  273. ->where(["invNo"=>$invNo,"is_del"=>0])
  274. ->field("0 as FPHXZ,
  275. catNo as SPBM,
  276. goodNo as ZXBM,
  277. is_discount as YHZCBS,
  278. inv_tag as LSLBS,
  279. addTax as ZZSTSGL,
  280. goodName as XMMC,
  281. unitName as DW,
  282. goodNum as SPSL,
  283. goodPrice as DJ,
  284. totalPrice as JE,
  285. 1 HSBZ,
  286. tax as SL,
  287. taxPrice as SE")
  288. ->select()->toJson();
  289. if(empty($goodinfo)){
  290. Db::rollback();
  291. return error_show(1004,"开票商品信息未找到");
  292. }
  293. $data=[
  294. "invNo"=>$invNo,
  295. "KPLX"=>"0",
  296. "XHFSBH"=>$invpoolinfo['seller_code'],
  297. "XHFMC"=>$invpoolinfo['seller_title'],
  298. "XHFDZ"=>$invpoolinfo['seller_addr'],
  299. "XHFDH"=>$invpoolinfo['seller_mobile'],
  300. "XHFYH"=>$invpoolinfo['seller_bank'],
  301. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  302. "GMFBM"=>"",
  303. "GMFSBH"=>$invpoolinfo['buyer_code'],
  304. "GMFMC"=>$invpoolinfo['buyer_title'],
  305. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  306. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  307. "GMFYH"=>$invpoolinfo['buyer_bank'],
  308. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  309. "GMFLX"=>$invinfo['company_type'],
  310. "GMFSJH"=>"",
  311. "GMFDZYX"=>$invinfo['email'],
  312. "KPR"=>$invpoolinfo['drawer'],
  313. "SKR"=>$invpoolinfo['payee'],
  314. "FHR"=>$invpoolinfo['reviewer'],
  315. "QDBZ"=>"",
  316. "JSHJ"=>$invinfo['inv_value'],
  317. "HJJE"=>"0",
  318. "HJSE"=>"0",
  319. "BZ"=>"",
  320. "DDMXXX"=>$goodinfo,
  321. "FPLXDM"=>invoiceType($invinfo['inv_type']),
  322. "DDQQPCH"=>"",
  323. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  324. "status"=>"0",
  325. "addtime"=>date("Y-m-d H:i:s"),
  326. "updatetime"=>date("Y-m-d H:i:s")
  327. ];
  328. $invoice=Db::name("invoice_interface")->insert($data);
  329. if($invoice==false){
  330. Db::rollback();
  331. return error_show(1003,"开票信息生成失败");
  332. }
  333. Cache::push("invoiceList",$data["DDQQLSH"]);
  334. }
  335. if($status==4 ||$status==5 ){
  336. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  337. if(!empty($qrdArr)){
  338. foreach ($qrdArr as $value){
  339. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  340. if($qrdinfo==false){
  341. Db::rollback();
  342. return error_show(1003,"确认单信息未找到");
  343. }
  344. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  345. Db::rollback();
  346. return error_show(1003,"确认单信息开票金额不足");
  347. }
  348. $update =[
  349. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  350. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  351. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  352. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  353. "updatetime"=>date("Y-m-d H:i:s"),
  354. ];
  355. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  356. if($qrdup==false){
  357. Db::rollback();
  358. return error_show(1003,"确认单信息更新失败");
  359. }
  360. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  361. $assocup =Db::name("assoc")->where($value)->update($assoc);
  362. if($assocup==false){
  363. Db::rollback();
  364. return error_show(1003,"确认单关联信息更新失败");
  365. }
  366. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  367. if($report)$report->rmField("invNo",$invinfo['invNo']);
  368. }
  369. }
  370. }
  371. Db::commit();
  372. return app_show(0,"审核成功");
  373. }
  374. Db::rollback();
  375. return error_show(1004,"审核失败");
  376. }catch (\Exception $e){
  377. Db::rollback();
  378. return error_show(1004,$e->getMessage());
  379. }
  380. }
  381. /**
  382. * 发票设置物流
  383. */
  384. public function SetPost(){
  385. // @param invNo postCompany postCode postFee;
  386. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  387. if($invNo==''){
  388. return error_show(1004,"参数invNo不能为空");
  389. }
  390. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  391. if($invinfo==false){
  392. return error_show(1004,"发票申请信息未找到");
  393. }
  394. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  395. if($post_company==''){
  396. return error_show(1004,"参数 post_company 不能为空");
  397. }
  398. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  399. if($post_code==''){
  400. return error_show(1004,"参数 post_code 不能为空");
  401. }
  402. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floor($this->post['post_fee']):"0";
  403. Db::startTrans();
  404. try{
  405. $data=[
  406. "post_company"=>$post_company,
  407. "post_code"=>$post_code,
  408. "post_fee"=>$post_fee,
  409. "status"=>3,
  410. "updatetime"=>date("Y-m-d H:i:s")
  411. ];
  412. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  413. if($up){
  414. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  415. if(!empty($qrdArr)){
  416. foreach ($qrdArr as $value){
  417. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  418. if($qrdinfo==false){
  419. Db::rollback();
  420. return error_show(1003,"确认单信息未找到");
  421. }
  422. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  423. Db::rollback();
  424. return error_show(1003,"确认单信息开票金额不足");
  425. }
  426. $update =[
  427. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  428. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  429. "inv_tme"=>date("Y-m-d H:i:s"),
  430. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  431. "updatetime"=>date("Y-m-d H:i:s"),
  432. ];
  433. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  434. if($qrdup==false){
  435. Db::rollback();
  436. return error_show(1003,"确认单信息更新失败");
  437. }
  438. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  439. $assocup =Db::name("assoc")->where($value)->update($assoc);
  440. if($assocup==false){
  441. Db::rollback();
  442. return error_show(1003,"确认单关联信息更新失败");
  443. }
  444. }
  445. }
  446. Db::commit();
  447. return app_show(0,"更新成功");
  448. }
  449. Db::rollback();
  450. return error_show(1003,"发票申请信息更新失败");
  451. }catch (Exception $e){
  452. Db::rollback();
  453. return error_show(1004,$e->getMessage());
  454. }
  455. }
  456. /**
  457. * 发票废弃使用
  458. */
  459. public function Discard(){
  460. }
  461. /**@param invNo return_reason remark 退票申请
  462. * @return \think\response\Json|void
  463. */
  464. public function ReturnAdd(){
  465. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  466. if($invNo==""){
  467. return error_show(1004,"参数 invNo 不能为空");
  468. }
  469. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  470. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  471. if($return_reason==""){
  472. return error_show(1004,"参数 return_reason 不能为空");
  473. }
  474. if($remark==""){
  475. return error_show(1004,"参数 remark 不能为空");
  476. }
  477. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  478. if($invinfo==false){
  479. return error_show(1004,"发票申请数据未找到");
  480. }
  481. if($invinfo['status']!=3){
  482. return error_show(1004,"发票申请未完成");
  483. }
  484. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  485. if(empty($qrd)){
  486. return error_show(1004,"未找到开票的销售单信息");
  487. }
  488. $returnCode = makeNo("RIN");
  489. $data=[
  490. "returnCode"=>$returnCode,
  491. "invNo"=>$invNo,
  492. "return_reason"=>$return_reason,
  493. "remark"=>$remark,
  494. "status"=>0,
  495. "apply_id"=>$this->uid,
  496. "apply_name"=>$this->uname,
  497. "addtime"=>date("Y-m-d H:i:s"),
  498. "updatetime"=>date("Y-m-d H:i:s")
  499. ];
  500. $inter = Db::name("invoice_return")->insert($data);
  501. if($inter){
  502. foreach ($qrd as $value){
  503. $report=ReportCode::where(["qrdNo"=>$value])->find();
  504. if($report)$report->setField("returnInv",$returnCode);
  505. }
  506. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  507. }else{
  508. return error_show(1005,"退票申请新建失败");
  509. }
  510. }
  511. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败
  512. public function ReturnStatus(){
  513. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  514. if($returnCode==""){
  515. return error_show(1005,"参数 returnCode 不能为空");
  516. }
  517. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  518. if ($status==""){
  519. return error_show(1005,"参数 status 不能为空");
  520. }
  521. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):"";
  522. if ($return_type==""){
  523. return error_show(1005,"参数 return_type 不能为空");
  524. }
  525. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  526. if($remark==""){
  527. return error_show(1005,"参数 remark 不能为空");
  528. }
  529. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  530. if($return==false){
  531. return error_show(1005,"退票申请信息未找到");
  532. }
  533. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  534. if($invinfo==false){
  535. return error_show(1004,"发票申请数据未找到");
  536. }
  537. if($invinfo['status']!=3){
  538. return error_show(1004,"发票申请未完成");
  539. }
  540. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  541. if(empty($qrd)){
  542. return error_show(1004,"未找到开票的销售单信息");
  543. }
  544. Db::startTrans();
  545. try{
  546. $update=["status"=>$status,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  547. $up =Db::name("invoice_return")->where($return)->update($update);
  548. if($up){
  549. if($status==2){
  550. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  551. if(!empty($qrdArr)){
  552. foreach ($qrdArr as $value){
  553. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  554. if($qrdinfo==false){
  555. Db::rollback();
  556. return error_show(1003,"确认单信息未找到");
  557. }
  558. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  559. Db::rollback();
  560. return error_show(1003,"确认单信息开票金额不足");
  561. }
  562. $update =[
  563. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  564. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  565. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  566. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  567. "updatetime"=>date("Y-m-d H:i:s"),
  568. ];
  569. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  570. if($qrdup==false){
  571. Db::rollback();
  572. return error_show(1003,"确认单信息更新失败");
  573. }
  574. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  575. $assocup =Db::name("assoc")->where($value)->update($assoc);
  576. if($assocup==false){
  577. Db::rollback();
  578. return error_show(1003,"确认单关联信息更新失败");
  579. }
  580. }
  581. }
  582. }
  583. if($status==3 || $status==4){
  584. foreach ($qrd as $value){
  585. $report=ReportCode::where(["qrdNo"=>$value])->find();
  586. if($report)$report->rmField("returnInv",$returnCode);
  587. }
  588. }
  589. Db::commit();
  590. return app_show(0,"退票申请信息更新成功");
  591. }
  592. Db::rollback();
  593. return error_show(1005,"退票申请信息更新失败");
  594. }catch (\Exception $e){
  595. Db::rollback();
  596. return error_show(1005,$e->getMessage());
  597. }
  598. }
  599. //红冲票
  600. public function returnRed(){
  601. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  602. if($returnCode==""){
  603. return error_show(1005,"参数 returnCode 不能为空");
  604. }
  605. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  606. if($redinv==""){
  607. return error_show(1005,"参数 redinv 不能为空");
  608. }
  609. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  610. if($return==false){
  611. return error_show(1005,"退票申请信息未找到");
  612. }
  613. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  614. if($invinfo==false){
  615. return error_show(1005,"发票申请信息未找到");
  616. }
  617. Db::startTrans();
  618. try{
  619. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  620. $up =Db::name("invoice_return")->where($return)->update($update);
  621. if($up){
  622. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  623. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  624. if($inv){
  625. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  626. if(!empty($qrdArr)){
  627. foreach ($qrdArr as $value){
  628. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  629. if($qrdinfo==false){
  630. Db::rollback();
  631. return error_show(1003,"确认单信息未找到");
  632. }
  633. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  634. Db::rollback();
  635. return error_show(1003,"确认单信息开票金额不足");
  636. }
  637. $update =[
  638. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  639. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  640. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  641. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  642. "updatetime"=>date("Y-m-d H:i:s"),
  643. ];
  644. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  645. if($qrdup==false){
  646. Db::rollback();
  647. return error_show(1003,"确认单信息更新失败");
  648. }
  649. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  650. $assocup =Db::name("assoc")->where($value)->update($assoc);
  651. if($assocup==false){
  652. Db::rollback();
  653. return error_show(1003,"确认单关联信息更新失败");
  654. }
  655. }
  656. }
  657. Db::commit();
  658. return app_show(0,"退票申请信息更新成功");
  659. }
  660. }
  661. Db::rollback();
  662. return error_show(1005,"退票申请信息更新失败");
  663. }catch (\Exception $e){
  664. Db::rollback();
  665. return error_show(1005,$e->getMessage());
  666. }
  667. }
  668. //退票详情
  669. public function returnInfo(){
  670. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  671. if($returnCode==""){
  672. return error_show(1005,"参数 returnCode 不能为空");
  673. }
  674. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  675. if($return==false){
  676. return error_show(1005,"退票申请信息未找到");
  677. }
  678. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  679. $return['inv_out']= $invinfo['inv_out']??"";
  680. $return['inv_value']= $invinfo['inv_value']??"";
  681. $return['inv_apply_id']= $invinfo['apply_id']??"";
  682. $return['inv_apply_name']= $invinfo['apply_name']??"";
  683. $return['inv_type']= $invinfo['inv_type']??"";
  684. $return['open_type']= $invinfo['open_type']??"";
  685. $return['is_ticket']= $invinfo['is_ticket']??"";
  686. $return['email']= $invinfo['email']??"";
  687. $return['post_code']= $invinfo['post_code']??"";
  688. $return['post_company']= $invinfo['post_company']??"";
  689. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  690. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  691. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  692. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  693. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  694. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  695. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  696. $return['seller_title'] = $poolinfo['seller_title']??'';
  697. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  698. $return['seller_code'] = $poolinfo['seller_code']??'';
  699. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  700. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  701. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  702. $return['voider'] = $poolinfo['voider']??'';
  703. $return['payee'] = $poolinfo['payee']??'';
  704. $return['drawer'] = $poolinfo['drawer']??'';
  705. $return['reviewer'] = $poolinfo['reviewer']??'';
  706. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  707. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  708. $return['goodList']=$orderList;
  709. return app_show(0,"获取成功",$return);
  710. }
  711. /**
  712. *发票池列表
  713. */
  714. public function list(){
  715. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  716. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  717. $condition=[["a.is_del","=",0]];
  718. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  719. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  720. if($start!=""){
  721. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  722. }
  723. if($end!=""){
  724. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  725. }
  726. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  727. if($status!==""){
  728. $condition[]=["a.status","=",$status];
  729. }
  730. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  731. if($inv_type!==""){
  732. $condition[]=["a.inv_type","=",$inv_type];
  733. }
  734. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  735. if($inv_out!==""){
  736. $condition[]=["a.inv_out","=",$inv_out];
  737. }
  738. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  739. // if($inv_in!==""){
  740. // $condition[]=["a.inv_in","=",$inv_in];
  741. // }
  742. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  743. if($invNo!==""){
  744. $condition[]=["a.invNo","like","%$invNo%"];
  745. }
  746. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  747. if($apply_id!==""){
  748. $condition[]=["a.apply_id","=",$apply_id];
  749. }
  750. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  751. if($apply_name!==""){
  752. $condition[]=["a.apply_name","like","%$apply_name%"];
  753. }
  754. $count=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  755. ($condition)->count();
  756. $total =ceil($count/$size);
  757. $page= $page>$total ? intval($total):$page;
  758. $list=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where($condition)
  759. ->page($page,$size)->select()->toArray();
  760. foreach ($list as &$item) {
  761. $company =Db::name("company_info")->where(["companyNo"=>$item['inv_out']])->find();
  762. // $kehu =Db::name("customer_info")->where(["companyNo"=>$item['inv_in']])->find();
  763. $item['companyName'] = $company['company_name']??"";
  764. // $item['customerName'] = $kehu['companyName']??"";
  765. }
  766. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  767. }
  768. //获取发票详情
  769. public function info(){
  770. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  771. if($invNo==''){
  772. return error_show(1004,"参数 invNo 不能为空");
  773. }
  774. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  775. if(empty($poolinfo)){
  776. return error_show(1004,"为找到开票信息");
  777. }
  778. $company =Db::name("company_info")->where(["companyNo"=>$poolinfo['inv_out']])->find();
  779. $poolinfo['companyName'] = $company['company_name']??"";
  780. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  781. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  782. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  783. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  784. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  785. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  786. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  787. $poolinfo['seller_title'] = $pool['seller_title']??'';
  788. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  789. $poolinfo['seller_code'] = $pool['seller_code']??'';
  790. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  791. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  792. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  793. $poolinfo['voider'] = $pool['voider']??'';
  794. $poolinfo['payee'] = $pool['payee']??'';
  795. $poolinfo['drawer'] = $pool['drawer']??'';
  796. $poolinfo['reviewer'] = $pool['reviewer']??'';
  797. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  798. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  799. $poolinfop['goodList']=$orderList;
  800. return app_show(0,"获取成功",$poolinfo);
  801. }
  802. //退票列表
  803. public function returnList(){
  804. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  805. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  806. $condition=[["b.is_del","=",0]];
  807. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  808. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  809. if($start!=""){
  810. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  811. }
  812. if($end!=""){
  813. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  814. }
  815. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  816. if($inv_type!==""){
  817. $condition[]=["b.inv_type","=",$inv_type];
  818. }
  819. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  820. if($apply_id!==""){
  821. $condition[]=["a.apply_id","=",$apply_id];
  822. }
  823. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  824. if($apply_name!==""){
  825. $condition[]=["a.apply_name","like","%$apply_name%"];
  826. }
  827. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  828. if($status!==""){
  829. $condition[]=["a.status","=",$status];
  830. }
  831. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  832. if($invNo!==""){
  833. $condition[]=["a.invNo","like","%$invNo%"];
  834. }
  835. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  836. if($returnCode!==""){
  837. $condition[]=["a.returnCode","like","%$returnCode%"];
  838. }
  839. $count=Db::name("invoice_return")->alias("a")
  840. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  841. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  842. ->where($condition)->count();
  843. $total =ceil($count/$size);
  844. $page= $page>$total ? intval($total):$page;
  845. $list=Db::name("invoice_return")->alias("a")
  846. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  847. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  848. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  849. 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,
  850. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,
  851. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->select()->toArray();
  852. foreach ($list as &$value){
  853. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  854. $value['companyName'] = $company['company_name']??"";
  855. }
  856. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  857. }
  858. }