OrderInv.php 43 KB

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