OrderInv.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. "item"=>'',
  257. "status"=>0,
  258. "addtime"=>date("Y-m-d H:i:s"),
  259. "updatetime"=>date("Y-m-d H:i:s")
  260. ];
  261. $in = Db::name("invoice_ticket")->insert($invpool);
  262. if($in==false){
  263. Db::rollback();
  264. return error_show(1004,"发票信息添加失败");
  265. }
  266. }
  267. if($status==7 && $open_type==1){
  268. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  269. if(empty($invpoolinfo)){
  270. Db::rollback();
  271. return error_show(1004,"开票信息未找到");
  272. }
  273. $goodinfo =Db::name("invoice_good")
  274. ->where(["invNo"=>$invNo,"is_del"=>0])
  275. ->field("0 as FPHXZ,
  276. catNo as SPBM,
  277. goodNo as ZXBM,
  278. is_discount as YHZCBS,
  279. inv_tag as LSLBS,
  280. addTax as ZZSTSGL,
  281. goodName as XMMC,
  282. unitName as DW,
  283. goodNum as SPSL,
  284. goodPrice as DJ,
  285. totalPrice as JE,
  286. 1 HSBZ,
  287. tax as SL,
  288. taxPrice as SE")
  289. ->select()->toJson();
  290. if(empty($goodinfo)){
  291. Db::rollback();
  292. return error_show(1004,"开票商品信息未找到");
  293. }
  294. $data=[
  295. "invNo"=>$invNo,
  296. "KPLX"=>"0",
  297. "XHFSBH"=>$invpoolinfo['seller_code'],
  298. "XHFMC"=>$invpoolinfo['seller_title'],
  299. "XHFDZ"=>$invpoolinfo['seller_addr'],
  300. "XHFDH"=>$invpoolinfo['seller_mobile'],
  301. "XHFYH"=>$invpoolinfo['seller_bank'],
  302. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  303. "GMFBM"=>"",
  304. "GMFSBH"=>$invpoolinfo['buyer_code'],
  305. "GMFMC"=>$invpoolinfo['buyer_title'],
  306. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  307. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  308. "GMFYH"=>$invpoolinfo['buyer_bank'],
  309. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  310. "GMFLX"=>$invinfo['company_type'],
  311. "GMFSJH"=>"",
  312. "GMFDZYX"=>$invinfo['email'],
  313. "KPR"=>$invpoolinfo['drawer'],
  314. "SKR"=>$invpoolinfo['payee'],
  315. "FHR"=>$invpoolinfo['reviewer'],
  316. "QDBZ"=>"",
  317. "JSHJ"=>$invinfo['inv_value'],
  318. "HJJE"=>"0",
  319. "HJSE"=>"0",
  320. "BZ"=>"",
  321. "DDMXXX"=>$goodinfo,
  322. "FPLXDM"=>invoiceType($invinfo['inv_type']),
  323. "DDQQPCH"=>"",
  324. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  325. "status"=>"0",
  326. "addtime"=>date("Y-m-d H:i:s"),
  327. "updatetime"=>date("Y-m-d H:i:s")
  328. ];
  329. $invoice=Db::name("invoice_interface")->insert($data);
  330. if($invoice==false){
  331. Db::rollback();
  332. return error_show(1003,"开票信息生成失败");
  333. }
  334. Cache::push("invoiceList",$data["DDQQLSH"]);
  335. }
  336. if($status==4 ||$status==5 ){
  337. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  338. if(!empty($qrdArr)){
  339. foreach ($qrdArr as $value){
  340. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  341. if($qrdinfo==false){
  342. Db::rollback();
  343. return error_show(1003,"确认单信息未找到");
  344. }
  345. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  346. Db::rollback();
  347. return error_show(1003,"确认单信息开票金额不足");
  348. }
  349. $update =[
  350. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  351. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  352. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  353. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  354. "updatetime"=>date("Y-m-d H:i:s"),
  355. ];
  356. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  357. if($qrdup==false){
  358. Db::rollback();
  359. return error_show(1003,"确认单信息更新失败");
  360. }
  361. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  362. $assocup =Db::name("assoc")->where($value)->update($assoc);
  363. if($assocup==false){
  364. Db::rollback();
  365. return error_show(1003,"确认单关联信息更新失败");
  366. }
  367. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  368. if($report)$report->rmField("invNo",$invinfo['invNo']);
  369. }
  370. }
  371. }
  372. Db::commit();
  373. return app_show(0,"审核成功");
  374. }
  375. Db::rollback();
  376. return error_show(1004,"审核失败");
  377. }catch (\Exception $e){
  378. Db::rollback();
  379. return error_show(1004,$e->getMessage());
  380. }
  381. }
  382. /**
  383. * 发票设置物流
  384. */
  385. public function SetPost(){
  386. // @param invNo postCompany postCode postFee;
  387. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  388. if($invNo==''){
  389. return error_show(1004,"参数invNo不能为空");
  390. }
  391. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  392. if($invinfo==false){
  393. return error_show(1004,"发票申请信息未找到");
  394. }
  395. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  396. if($post_company==''){
  397. return error_show(1004,"参数 post_company 不能为空");
  398. }
  399. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  400. if($post_code==''){
  401. return error_show(1004,"参数 post_code 不能为空");
  402. }
  403. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floor($this->post['post_fee']):"0";
  404. Db::startTrans();
  405. try{
  406. $data=[
  407. "post_company"=>$post_company,
  408. "post_code"=>$post_code,
  409. "post_fee"=>$post_fee,
  410. "status"=>3,
  411. "updatetime"=>date("Y-m-d H:i:s")
  412. ];
  413. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  414. if($up){
  415. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  416. if(!empty($qrdArr)){
  417. foreach ($qrdArr as $value){
  418. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  419. if($qrdinfo==false){
  420. Db::rollback();
  421. return error_show(1003,"确认单信息未找到");
  422. }
  423. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  424. Db::rollback();
  425. return error_show(1003,"确认单信息开票金额不足");
  426. }
  427. $update =[
  428. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  429. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  430. "inv_tme"=>date("Y-m-d H:i:s"),
  431. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  432. "updatetime"=>date("Y-m-d H:i:s"),
  433. ];
  434. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  435. if($qrdup==false){
  436. Db::rollback();
  437. return error_show(1003,"确认单信息更新失败");
  438. }
  439. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  440. $assocup =Db::name("assoc")->where($value)->update($assoc);
  441. if($assocup==false){
  442. Db::rollback();
  443. return error_show(1003,"确认单关联信息更新失败");
  444. }
  445. }
  446. }
  447. Db::commit();
  448. return app_show(0,"更新成功");
  449. }
  450. Db::rollback();
  451. return error_show(1003,"发票申请信息更新失败");
  452. }catch (Exception $e){
  453. Db::rollback();
  454. return error_show(1004,$e->getMessage());
  455. }
  456. }
  457. /**
  458. * 发票废弃使用
  459. */
  460. public function Discard(){
  461. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  462. }
  463. /**@param invNo return_reason remark 退票申请
  464. * @return \think\response\Json|void
  465. */
  466. public function ReturnAdd(){
  467. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  468. if($invNo==""){
  469. return error_show(1004,"参数 invNo 不能为空");
  470. }
  471. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  472. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  473. if($return_reason==""){
  474. return error_show(1004,"参数 return_reason 不能为空");
  475. }
  476. if($remark==""){
  477. return error_show(1004,"参数 remark 不能为空");
  478. }
  479. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  480. if($invinfo==false){
  481. return error_show(1004,"发票申请数据未找到");
  482. }
  483. if($invinfo['status']!=3){
  484. return error_show(1004,"发票申请未完成");
  485. }
  486. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  487. if(empty($qrd)){
  488. return error_show(1004,"未找到开票的销售单信息");
  489. }
  490. $returnCode = makeNo("RIN");
  491. $data=[
  492. "returnCode"=>$returnCode,
  493. "invNo"=>$invNo,
  494. "return_reason"=>$return_reason,
  495. "remark"=>$remark,
  496. "status"=>0,
  497. "apply_id"=>$this->uid,
  498. "apply_name"=>$this->uname,
  499. "addtime"=>date("Y-m-d H:i:s"),
  500. "updatetime"=>date("Y-m-d H:i:s")
  501. ];
  502. $inter = Db::name("invoice_return")->insert($data);
  503. if($inter){
  504. foreach ($qrd as $value){
  505. $report=ReportCode::where(["qrdNo"=>$value])->find();
  506. if($report)$report->setField("returnInv",$returnCode);
  507. }
  508. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  509. }else{
  510. return error_show(1005,"退票申请新建失败");
  511. }
  512. }
  513. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败
  514. public function ReturnStatus(){
  515. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  516. if($returnCode==""){
  517. return error_show(1005,"参数 returnCode 不能为空");
  518. }
  519. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  520. if ($status==""){
  521. return error_show(1005,"参数 status 不能为空");
  522. }
  523. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):"";
  524. if ($return_type==""){
  525. return error_show(1005,"参数 return_type 不能为空");
  526. }
  527. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  528. if($remark==""){
  529. return error_show(1005,"参数 remark 不能为空");
  530. }
  531. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  532. if($return==false){
  533. return error_show(1005,"退票申请信息未找到");
  534. }
  535. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  536. if($invinfo==false){
  537. return error_show(1004,"发票申请数据未找到");
  538. }
  539. if($invinfo['status']!=3){
  540. return error_show(1004,"发票申请未完成");
  541. }
  542. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  543. if(empty($qrd)){
  544. return error_show(1004,"未找到开票的销售单信息");
  545. }
  546. Db::startTrans();
  547. try{
  548. $update=["status"=>$status,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  549. $up =Db::name("invoice_return")->where($return)->update($update);
  550. if($up){
  551. if($status==2){
  552. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  553. if(!empty($qrdArr)){
  554. foreach ($qrdArr as $value){
  555. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  556. if($qrdinfo==false){
  557. Db::rollback();
  558. return error_show(1003,"确认单信息未找到");
  559. }
  560. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  561. Db::rollback();
  562. return error_show(1003,"确认单信息开票金额不足");
  563. }
  564. $update =[
  565. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  566. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  567. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  568. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  569. "updatetime"=>date("Y-m-d H:i:s"),
  570. ];
  571. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  572. if($qrdup==false){
  573. Db::rollback();
  574. return error_show(1003,"确认单信息更新失败");
  575. }
  576. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  577. $assocup =Db::name("assoc")->where($value)->update($assoc);
  578. if($assocup==false){
  579. Db::rollback();
  580. return error_show(1003,"确认单关联信息更新失败");
  581. }
  582. }
  583. }
  584. }
  585. if($status==3 || $status==4){
  586. foreach ($qrd as $value){
  587. $report=ReportCode::where(["qrdNo"=>$value])->find();
  588. if($report)$report->rmField("returnInv",$returnCode);
  589. }
  590. }
  591. Db::commit();
  592. return app_show(0,"退票申请信息更新成功");
  593. }
  594. Db::rollback();
  595. return error_show(1005,"退票申请信息更新失败");
  596. }catch (\Exception $e){
  597. Db::rollback();
  598. return error_show(1005,$e->getMessage());
  599. }
  600. }
  601. //红冲票
  602. public function returnRed(){
  603. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  604. if($returnCode==""){
  605. return error_show(1005,"参数 returnCode 不能为空");
  606. }
  607. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  608. if($redinv==""){
  609. return error_show(1005,"参数 redinv 不能为空");
  610. }
  611. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  612. if($return==false){
  613. return error_show(1005,"退票申请信息未找到");
  614. }
  615. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  616. if($invinfo==false){
  617. return error_show(1005,"发票申请信息未找到");
  618. }
  619. Db::startTrans();
  620. try{
  621. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  622. $up =Db::name("invoice_return")->where($return)->update($update);
  623. if($up){
  624. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  625. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  626. if($inv){
  627. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  628. if(!empty($qrdArr)){
  629. foreach ($qrdArr as $value){
  630. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  631. if($qrdinfo==false){
  632. Db::rollback();
  633. return error_show(1003,"确认单信息未找到");
  634. }
  635. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  636. Db::rollback();
  637. return error_show(1003,"确认单信息开票金额不足");
  638. }
  639. $update =[
  640. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  641. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  642. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  643. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  644. "updatetime"=>date("Y-m-d H:i:s"),
  645. ];
  646. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  647. if($qrdup==false){
  648. Db::rollback();
  649. return error_show(1003,"确认单信息更新失败");
  650. }
  651. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  652. $assocup =Db::name("assoc")->where($value)->update($assoc);
  653. if($assocup==false){
  654. Db::rollback();
  655. return error_show(1003,"确认单关联信息更新失败");
  656. }
  657. }
  658. }
  659. Db::commit();
  660. return app_show(0,"退票申请信息更新成功");
  661. }
  662. }
  663. Db::rollback();
  664. return error_show(1005,"退票申请信息更新失败");
  665. }catch (\Exception $e){
  666. Db::rollback();
  667. return error_show(1005,$e->getMessage());
  668. }
  669. }
  670. //退票详情
  671. public function returnInfo(){
  672. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  673. if($returnCode==""){
  674. return error_show(1005,"参数 returnCode 不能为空");
  675. }
  676. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  677. if($return==false){
  678. return error_show(1005,"退票申请信息未找到");
  679. }
  680. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  681. $return['inv_out']= $invinfo['inv_out']??"";
  682. $return['inv_value']= $invinfo['inv_value']??"";
  683. $return['inv_apply_id']= $invinfo['apply_id']??"";
  684. $return['inv_apply_name']= $invinfo['apply_name']??"";
  685. $return['inv_type']= $invinfo['inv_type']??"";
  686. $return['open_type']= $invinfo['open_type']??"";
  687. $return['is_ticket']= $invinfo['is_ticket']??"";
  688. $return['email']= $invinfo['email']??"";
  689. $return['post_code']= $invinfo['post_code']??"";
  690. $return['post_company']= $invinfo['post_company']??"";
  691. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  692. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  693. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  694. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  695. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  696. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  697. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  698. $return['seller_title'] = $poolinfo['seller_title']??'';
  699. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  700. $return['seller_code'] = $poolinfo['seller_code']??'';
  701. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  702. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  703. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  704. $return['voider'] = $poolinfo['voider']??'';
  705. $return['payee'] = $poolinfo['payee']??'';
  706. $return['drawer'] = $poolinfo['drawer']??'';
  707. $return['reviewer'] = $poolinfo['reviewer']??'';
  708. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  709. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  710. $return['goodList']=$orderList;
  711. return app_show(0,"获取成功",$return);
  712. }
  713. /**
  714. *发票池列表
  715. */
  716. public function list(){
  717. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  718. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  719. $condition=[["a.is_del","=",0]];
  720. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  721. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  722. if($start!=""){
  723. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  724. }
  725. if($end!=""){
  726. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  727. }
  728. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  729. if($status!==""){
  730. $condition[]=["a.status","=",$status];
  731. }
  732. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  733. if($inv_type!==""){
  734. $condition[]=["a.inv_type","=",$inv_type];
  735. }
  736. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  737. if($inv_out!==""){
  738. $condition[]=["a.inv_out","=",$inv_out];
  739. }
  740. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  741. // if($inv_in!==""){
  742. // $condition[]=["a.inv_in","=",$inv_in];
  743. // }
  744. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  745. if($invNo!==""){
  746. $condition[]=["a.invNo","like","%$invNo%"];
  747. }
  748. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  749. if($apply_id!==""){
  750. $condition[]=["a.apply_id","=",$apply_id];
  751. }
  752. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  753. if($apply_name!==""){
  754. $condition[]=["a.apply_name","like","%$apply_name%"];
  755. }
  756. $count=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  757. ($condition)->count();
  758. $total =ceil($count/$size);
  759. $page= $page>$total ? intval($total):$page;
  760. $list=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where($condition)
  761. ->page($page,$size)->select()->toArray();
  762. foreach ($list as &$item) {
  763. $company =Db::name("company_info")->where(["companyNo"=>$item['inv_out']])->find();
  764. // $kehu =Db::name("customer_info")->where(["companyNo"=>$item['inv_in']])->find();
  765. $item['companyName'] = $company['company_name']??"";
  766. // $item['customerName'] = $kehu['companyName']??"";
  767. }
  768. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  769. }
  770. //获取发票详情
  771. public function info(){
  772. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  773. if($invNo==''){
  774. return error_show(1004,"参数 invNo 不能为空");
  775. }
  776. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  777. if(empty($poolinfo)){
  778. return error_show(1004,"为找到开票信息");
  779. }
  780. $company =Db::name("company_info")->where(["companyNo"=>$poolinfo['inv_out']])->find();
  781. $poolinfo['companyName'] = $company['company_name']??"";
  782. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  783. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  784. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  785. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  786. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  787. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  788. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  789. $poolinfo['seller_title'] = $pool['seller_title']??'';
  790. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  791. $poolinfo['seller_code'] = $pool['seller_code']??'';
  792. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  793. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  794. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  795. $poolinfo['voider'] = $pool['voider']??'';
  796. $poolinfo['payee'] = $pool['payee']??'';
  797. $poolinfo['drawer'] = $pool['drawer']??'';
  798. $poolinfo['reviewer'] = $pool['reviewer']??'';
  799. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  800. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  801. $poolinfop['goodList']=$orderList;
  802. return app_show(0,"获取成功",$poolinfo);
  803. }
  804. //退票列表
  805. public function returnList(){
  806. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  807. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  808. $condition=[["b.is_del","=",0]];
  809. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  810. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  811. if($start!=""){
  812. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  813. }
  814. if($end!=""){
  815. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  816. }
  817. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  818. if($inv_type!==""){
  819. $condition[]=["b.inv_type","=",$inv_type];
  820. }
  821. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  822. if($apply_id!==""){
  823. $condition[]=["a.apply_id","=",$apply_id];
  824. }
  825. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  826. if($apply_name!==""){
  827. $condition[]=["a.apply_name","like","%$apply_name%"];
  828. }
  829. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  830. if($status!==""){
  831. $condition[]=["a.status","=",$status];
  832. }
  833. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  834. if($invNo!==""){
  835. $condition[]=["a.invNo","like","%$invNo%"];
  836. }
  837. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  838. if($returnCode!==""){
  839. $condition[]=["a.returnCode","like","%$returnCode%"];
  840. }
  841. $count=Db::name("invoice_return")->alias("a")
  842. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  843. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  844. ->where($condition)->count();
  845. $total =ceil($count/$size);
  846. $page= $page>$total ? intval($total):$page;
  847. $list=Db::name("invoice_return")->alias("a")
  848. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  849. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  850. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  851. 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,
  852. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,
  853. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->select()->toArray();
  854. foreach ($list as &$value){
  855. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  856. $value['companyName'] = $company['company_name']??"";
  857. }
  858. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  859. }
  860. }