OrderInv.php 43 KB

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