OrderInv.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. class OrderInv extends BaseController{
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. }
  11. /**
  12. * 新建销售单开票申请
  13. */
  14. public function create(){
  15. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  16. if($buy_id==''){
  17. return error_show(1004,"参数 buy_id 不能为空");
  18. }
  19. $buyinfo =Db::name("customer_invoice")->where(['id'=>$buy_id,"is_del"=>0])->find();
  20. if($buyinfo==false){
  21. return error_show(1004,"购买方发票信息未找到");
  22. }
  23. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  24. if($companyNo==''){
  25. return error_show(1004,"参数 companyNo 不能为空");
  26. }
  27. $company = Db::name("company_info")->where(["companyNo"=>$companyNo])->find();
  28. if($company==false){
  29. return error_show(1004,"销售方信息未找到");
  30. }
  31. $khNo = isset($this->post['khNo'])&&$this->post['khNo']!=='' ? trim($this->post['khNo']):"";
  32. if($khNo==''){
  33. return error_show(1004,"参数 khNo 不能为空");
  34. }
  35. $customer = Db::name("customer_info")->where(["companyNo"=>$khNo])->find();
  36. if($customer==false){
  37. return error_show(1004,"客户信息未找到");
  38. }
  39. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? intval($this->post['invtype']):"";
  40. if($invtype==''){
  41. return error_show(1004,"参数 invtype 不能为空");
  42. }
  43. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  44. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  45. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  46. if(empty($orderArr)){
  47. return error_show(1004,"参数 orderArr 不能为空");
  48. }
  49. $temp =[];
  50. $invNo=makeNo("INV");
  51. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  52. Db::startTrans();
  53. try{
  54. foreach ($orderArr as $value){
  55. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  56. Db::rollback();
  57. return error_show(1004,"参数 sequenceNo 不能为空");
  58. }
  59. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  60. if($qrd['status']==2){
  61. Db::rollback();
  62. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  63. }
  64. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  65. Db::rollback();
  66. return error_show(1004,"参数 inv_fee 不能为空");
  67. }
  68. if($qrd['winv_fee']<$value['inv_fee']){
  69. Db::rollback();
  70. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  71. }
  72. $ascc=[
  73. "assocNo"=>makeNo("AS"),
  74. "apply_id"=>$this->uid,
  75. "apply_name"=>$this->uname,
  76. "type"=>1,
  77. "orderCode"=>$value['sequenceNo'],
  78. "viceCode"=>$invNo,
  79. "order_total"=>$qrd['totalPrice'],
  80. "vice_total"=>$invfee,
  81. "cancel_fee"=>$value['inv_fee'],
  82. "status"=>1,
  83. "addtime"=>date("Y-m-d H:i:s"),
  84. "updatetime"=>date("Y-m-d H:i:s")
  85. ];
  86. $update = [
  87. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  88. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  89. "inv_status"=>2,
  90. "status"=>1,
  91. "updatetime"=>date("Y-m-d H:i:s")
  92. ];
  93. $up = Db::name("qrd_info")->where($qrd)->update($update);
  94. if($up==false){
  95. Db::rollback();
  96. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  97. }
  98. $temp[]=$ascc;
  99. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  100. $report->setField("invNo",$invNo);
  101. }
  102. $associn = Db::name("assoc")->insertAll($temp);
  103. if($associn==0){
  104. Db::rollback();
  105. return error_show(1005,"确认单申请开票失败");
  106. }
  107. $inv=[
  108. "invNo"=>$invNo,
  109. "inv_value"=>$invfee,
  110. "inv_out"=> $companyNo,
  111. "inv_in"=>$khNo,
  112. "apply_id"=>$this->uid,
  113. "apply_name"=>$this->uname,
  114. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通
  115. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  116. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  117. "remark"=>$remark, //申请备注
  118. "exam_remark"=>'', //审核备注
  119. "ainv_fee"=>0,//已核销金额
  120. "winv_fee"=>$invfee,//未核销金额
  121. "status"=>0,// 待财务开票/待金税开票 待财务审核 待填写物流 开票完成 开票失败/开票驳回 财务驳回
  122. "is_del"=>0,
  123. "email"=>$email,
  124. "addtime"=>date("Y-m-d H:i:s"),
  125. "updatetime"=>date("Y-m-d H:i:s")
  126. ];
  127. $invin = Db::name("invoice_pool")->insert($inv);
  128. if($invin){
  129. $invinfo=[
  130. "buyer_title"=>$buyinfo['invoice_title'],
  131. "buyer_code"=>$buyinfo['invoice_code'],
  132. "buyer_addr"=>$buyinfo['invoice_addr'],
  133. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  134. "buyer_bank"=>$buyinfo['invoice_bank'],
  135. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  136. "seller_title"=>$company['company_name'],
  137. "seller_code"=>$company['company_license'],
  138. "seller_addr"=>$company['company_address'],
  139. "seller_mobile"=>$company['mobile'],
  140. "seller_bank"=>$company['bank_name'],
  141. "seller_bankNo"=>$company['bankNo'],
  142. "invNo"=>$invNo,
  143. "voider"=>$company['voider'],
  144. "payee"=>$company['payee'],
  145. "drawer"=>$company['drawer'],
  146. "reviewer"=>$company['reviewer'],
  147. "ownerPlace"=>$company['ownerPlace'],
  148. "addtime"=>date("Y-m-d H:i:s")
  149. ];
  150. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  151. if($poolinfo){
  152. Db::commit();
  153. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  154. }
  155. }
  156. Db::rollback();
  157. return error_show(1004,"确认单发票申请失败");
  158. }catch (Exception $e){
  159. Db::rollback();
  160. return error_show(1004,$e->getMessage());
  161. }
  162. }
  163. // 0 待财务开票/待金税开票 1待财务审核 2待填写物流 3开票完成 4开票失败/开票驳回 5财务驳回 6退票
  164. public function status(){
  165. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  166. if($invNo==""){
  167. return error_show(1004,"参数 invNo 不能为空");
  168. }
  169. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  170. if($invinfo==false){
  171. return error_show(1004,"发票申请数据未找到");
  172. }
  173. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  174. if($status===''){
  175. return error_show(1004,"参数 status 不能为空");
  176. }
  177. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  178. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  179. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  180. // $invArr=isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  181. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  182. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  183. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  184. $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floor($this->post['total_fee']):"";
  185. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floor($this->post['subtotal_fee']):"";
  186. if($status==1){
  187. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  188. if($open_type!=1){
  189. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  190. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  191. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  192. if($total_fee==='') return error_show(1004,"参数 tatal_fee 不能为空");
  193. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  194. if($total_fee!=$invinfo['inv_value'])return error_show(1004,"发票金额不足");
  195. }
  196. }
  197. $update=[
  198. "status"=>$status,
  199. "exam_remark"=>$remark,
  200. "updatetime" => date("Y-m-d H:i:s")
  201. ];
  202. $status==1 ? $update['open_type']=$open_type :"";
  203. Db::startTrans();
  204. try{
  205. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  206. if($invup){
  207. if($status==1 && $open_type!=1){
  208. $temp=[];
  209. $invpool=[
  210. "invNo"=>$invinfo['invNo'],
  211. "inv_type"=>$open_type,
  212. "inv_code"=>$invCode,
  213. "inv_number"=>$invNum,
  214. "inv_total"=>$total_fee,
  215. "inv_subtotal"=>$subtotal_fee,
  216. "open_date"=>$open_date,
  217. "status"=>1,
  218. "addtime"=>date("Y-m-d H:i:s"),
  219. "updatetime"=>date("Y-m-d H:i:s")
  220. ];
  221. // foreach ($invArr as $value){
  222. // $invpool=[
  223. // "invNo"=>$invinfo['invNo'],
  224. // "in_type"=>$open_type,
  225. // "inv_code"=>$value['inv_code'],
  226. // "inv_number"=>$value['inv_number'],
  227. // "inv_total"=>$value['inv_total'],
  228. // "inv_subtotal"=>$value['inv_subtotal'],
  229. // "open_date"=>$value['open_date'],
  230. // "status"=>1,
  231. // "addtime"=>date("Y-m-d H:i:s"),
  232. // "updatetime"=>date("Y-m-d H:i:s")
  233. // ];
  234. // $temp[]=$invpool;
  235. // }
  236. // $in = Db::name("invoice_ticket")->insertAll($temp);
  237. $in = Db::name("invoice_ticket")->insert($invpool);
  238. if($in==false){
  239. Db::rollback();
  240. return error_show(1004,"发票信息添加失败");
  241. }
  242. }
  243. if($status==4 ||$status==5 ){
  244. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  245. if(!empty($qrdArr)){
  246. foreach ($qrdArr as $value){
  247. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  248. if($qrdinfo==false){
  249. Db::rollback();
  250. return error_show(1003,"确认单信息未找到");
  251. }
  252. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  253. Db::rollback();
  254. return error_show(1003,"确认单信息开票金额不足");
  255. }
  256. $update =[
  257. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  258. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  259. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  260. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  261. "updatetime"=>date("Y-m-d H:i:s"),
  262. ];
  263. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  264. if($qrdup==false){
  265. Db::rollback();
  266. return error_show(1003,"确认单信息更新失败");
  267. }
  268. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  269. $assocup =Db::name("assoc")->where($value)->update($assoc);
  270. if($assocup==false){
  271. Db::rollback();
  272. return error_show(1003,"确认单关联信息更新失败");
  273. }
  274. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  275. $report->rmField("invNo",$invinfo['invNo']);
  276. }
  277. }
  278. }
  279. Db::commit();
  280. return app_show(0,"审核成功");
  281. }
  282. Db::rollback();
  283. return error_show(1004,"审核失败");
  284. }catch (Exception $e){
  285. Db::rollback();
  286. return error_show(1004,$e->getMessage());
  287. }
  288. }
  289. /**
  290. * 发票设置物流
  291. */
  292. public function SetPost(){
  293. // @param invNo postCompany postCode postFee;
  294. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  295. if($invNo==''){
  296. return error_show(1004,"参数invNo不能为空");
  297. }
  298. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  299. if($invinfo==false){
  300. return error_show(1004,"发票申请信息未找到");
  301. }
  302. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  303. if($post_company==''){
  304. return error_show(1004,"参数 post_company 不能为空");
  305. }
  306. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  307. if($post_code==''){
  308. return error_show(1004,"参数 post_code 不能为空");
  309. }
  310. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floor($this->post['post_fee']):"0";
  311. Db::startTrans();
  312. try{
  313. $data=[
  314. "post_company"=>$post_company,
  315. "post_code"=>$post_code,
  316. "post_fee"=>$post_fee,
  317. "status"=>3,
  318. "updatetime"=>date("Y-m-d H:i:s")
  319. ];
  320. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  321. if($up){
  322. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  323. if(!empty($qrdArr)){
  324. foreach ($qrdArr as $value){
  325. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  326. if($qrdinfo==false){
  327. Db::rollback();
  328. return error_show(1003,"确认单信息未找到");
  329. }
  330. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  331. Db::rollback();
  332. return error_show(1003,"确认单信息开票金额不足");
  333. }
  334. $update =[
  335. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  336. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  337. "inv_tme"=>date("Y-m-d H:i:s"),
  338. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  339. "updatetime"=>date("Y-m-d H:i:s"),
  340. ];
  341. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  342. if($qrdup==false){
  343. Db::rollback();
  344. return error_show(1003,"确认单信息更新失败");
  345. }
  346. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  347. $assocup =Db::name("assoc")->where($value)->update($assoc);
  348. if($assocup==false){
  349. Db::rollback();
  350. return error_show(1003,"确认单关联信息更新失败");
  351. }
  352. }
  353. }
  354. Db::commit();
  355. return app_show(0,"更新成功");
  356. }
  357. Db::rollback();
  358. return error_show(1003,"发票申请信息更新失败");
  359. }catch (Exception $e){
  360. Db::rollback();
  361. return error_show(1004,$e->getMessage());
  362. }
  363. }
  364. /**
  365. * 发票废弃使用
  366. */
  367. public function Discard(){
  368. }
  369. /**@param invNo return_reason remark 退票申请
  370. * @return \think\response\Json|void
  371. */
  372. public function ReturnAdd(){
  373. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  374. if($invNo==""){
  375. return error_show(1004,"参数 invNo 不能为空");
  376. }
  377. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  378. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  379. if($return_reason==""){
  380. return error_show(1004,"参数 return_reason 不能为空");
  381. }
  382. if($remark==""){
  383. return error_show(1004,"参数 remark 不能为空");
  384. }
  385. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  386. if($invinfo==false){
  387. return error_show(1004,"发票申请数据未找到");
  388. }
  389. if($invinfo['status']!=3){
  390. return error_show(1004,"发票申请未完成");
  391. }
  392. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  393. if(empty($qrd)){
  394. return error_show(1004,"未找到开票的销售单信息");
  395. }
  396. $returnCode = makeNo("RIN");
  397. $data=[
  398. "returnCode"=>$returnCode,
  399. "invNo"=>$invNo,
  400. "return_reason"=>$return_reason,
  401. "remark"=>$remark,
  402. "status"=>0,
  403. "apply_id"=>$this->uid,
  404. "apply_name"=>$this->uname,
  405. "addtime"=>date("Y-m-d H:i:s"),
  406. "updatetime"=>date("Y-m-d H:i:s")
  407. ];
  408. $inter = Db::name("invoice_return")->insert($data);
  409. if($inter){
  410. foreach ($qrd as $value){
  411. $report=ReportCode::where(["qrdNo"=>$value])->find();
  412. $report->setField("returnInv",$returnCode);
  413. }
  414. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  415. }else{
  416. return error_show(1005,"退票申请新建失败");
  417. }
  418. }
  419. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败
  420. public function ReturnStatus(){
  421. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  422. if($returnCode==""){
  423. return error_show(1005,"参数 returnCode 不能为空");
  424. }
  425. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  426. if ($status==""){
  427. return error_show(1005,"参数 status 不能为空");
  428. }
  429. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):"";
  430. if ($return_type==""){
  431. return error_show(1005,"参数 return_type 不能为空");
  432. }
  433. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  434. if($remark==""){
  435. return error_show(1005,"参数 remark 不能为空");
  436. }
  437. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  438. if($return==false){
  439. return error_show(1005,"退票申请信息未找到");
  440. }
  441. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  442. if($invinfo==false){
  443. return error_show(1004,"发票申请数据未找到");
  444. }
  445. if($invinfo['status']!=3){
  446. return error_show(1004,"发票申请未完成");
  447. }
  448. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  449. if(empty($qrd)){
  450. return error_show(1004,"未找到开票的销售单信息");
  451. }
  452. Db::startTrans();
  453. try{
  454. $update=["status"=>$status,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  455. $up =Db::name("invoice_return")->where($return)->update($update);
  456. if($up){
  457. if($status==2){
  458. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  459. if(!empty($qrdArr)){
  460. foreach ($qrdArr as $value){
  461. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  462. if($qrdinfo==false){
  463. Db::rollback();
  464. return error_show(1003,"确认单信息未找到");
  465. }
  466. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  467. Db::rollback();
  468. return error_show(1003,"确认单信息开票金额不足");
  469. }
  470. $update =[
  471. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  472. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  473. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  474. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  475. "updatetime"=>date("Y-m-d H:i:s"),
  476. ];
  477. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  478. if($qrdup==false){
  479. Db::rollback();
  480. return error_show(1003,"确认单信息更新失败");
  481. }
  482. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  483. $assocup =Db::name("assoc")->where($value)->update($assoc);
  484. if($assocup==false){
  485. Db::rollback();
  486. return error_show(1003,"确认单关联信息更新失败");
  487. }
  488. }
  489. }
  490. }
  491. if($status==3 || $status==4){
  492. foreach ($qrd as $value){
  493. $report=ReportCode::where(["qrdNo"=>$value])->find();
  494. $report->rmField("returnInv",$returnCode);
  495. }
  496. }
  497. Db::commit();
  498. return app_show(0,"退票申请信息更新成功");
  499. }
  500. Db::rollback();
  501. return error_show(1005,"退票申请信息更新失败");
  502. }catch (\Exception $e){
  503. Db::rollback();
  504. return error_show(1005,$e->getMessage());
  505. }
  506. }
  507. public function returnRed(){
  508. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  509. if($returnCode==""){
  510. return error_show(1005,"参数 returnCode 不能为空");
  511. }
  512. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  513. if($redinv==""){
  514. return error_show(1005,"参数 redinv 不能为空");
  515. }
  516. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  517. if($return==false){
  518. return error_show(1005,"退票申请信息未找到");
  519. }
  520. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  521. if($invinfo==false){
  522. return error_show(1005,"发票申请信息未找到");
  523. }
  524. Db::startTrans();
  525. try{
  526. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  527. $up =Db::name("invoice_return")->where($return)->update($update);
  528. if($up){
  529. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  530. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  531. if($inv){
  532. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  533. if(!empty($qrdArr)){
  534. foreach ($qrdArr as $value){
  535. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  536. if($qrdinfo==false){
  537. Db::rollback();
  538. return error_show(1003,"确认单信息未找到");
  539. }
  540. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  541. Db::rollback();
  542. return error_show(1003,"确认单信息开票金额不足");
  543. }
  544. $update =[
  545. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  546. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  547. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  548. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  549. "updatetime"=>date("Y-m-d H:i:s"),
  550. ];
  551. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  552. if($qrdup==false){
  553. Db::rollback();
  554. return error_show(1003,"确认单信息更新失败");
  555. }
  556. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  557. $assocup =Db::name("assoc")->where($value)->update($assoc);
  558. if($assocup==false){
  559. Db::rollback();
  560. return error_show(1003,"确认单关联信息更新失败");
  561. }
  562. }
  563. }
  564. Db::commit();
  565. return app_show(0,"退票申请信息更新成功");
  566. }
  567. }
  568. Db::rollback();
  569. return error_show(1005,"退票申请信息更新失败");
  570. }catch (\Exception $e){
  571. Db::rollback();
  572. return error_show(1005,$e->getMessage());
  573. }
  574. }
  575. /**
  576. *
  577. */
  578. public function list(){
  579. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  580. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  581. $condition=[["a.is_del","=",0]];
  582. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  583. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  584. if($start!=""){
  585. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  586. }
  587. if($end!=""){
  588. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  589. }
  590. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  591. if($status!==""){
  592. $condition[]=["a.status","=",$status];
  593. }
  594. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  595. if($inv_type!==""){
  596. $condition[]=["a.inv_type","=",$inv_type];
  597. }
  598. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  599. if($inv_out!==""){
  600. $condition[]=["a.inv_out","=",$inv_out];
  601. }
  602. $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  603. if($inv_in!==""){
  604. $condition[]=["a.inv_in","=",$inv_in];
  605. }
  606. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  607. if($invNo!==""){
  608. $condition[]=["a.invNo","like","%$invNo%"];
  609. }
  610. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  611. if($apply_id!==""){
  612. $condition[]=["a.apply_id","=",$apply_id];
  613. }
  614. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  615. if($apply_name!==""){
  616. $condition[]=["a.apply_name","like","%$apply_name%"];
  617. }
  618. $count=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  619. ($condition)->count();
  620. $total =ceil($count/$size);
  621. $page= $page>$total ? intval($total):$page;
  622. $list=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where($condition)
  623. ->page($page,$size)->select()->toArray();
  624. foreach ($list as &$item) {
  625. $company =Db::name("company_info")->where(["companyNo"=>$item['inv_out']])->find();
  626. $kehu =Db::name("customer_info")->where(["companyNo"=>$item['inv_in']])->find();
  627. $item['companyName'] = $company['company_name']??"";
  628. $item['customerName'] = $kehu['companyName']??"";
  629. }
  630. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  631. }
  632. //获取发票详情
  633. public function info(){
  634. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  635. if($invNo==''){
  636. return error_show(1004,"参数 invNo 不能为空");
  637. }
  638. $poolinfo =Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  639. ([["a.invNo","=",$invNo],["a.is_del","=",0]])->findOrEmpty();
  640. if(empty($poolinfo)){
  641. return error_show(1004,"为找到开票信息");
  642. }
  643. $company =Db::name("company_info")->where(["companyNo"=>$poolinfo['inv_out']])->find();
  644. $kehu =Db::name("customer_info")->where(["companyNo"=>$poolinfo['inv_in']])->find();
  645. $poolinfo['companyName'] = $company['company_name']??"";
  646. $poolinfo['customerName'] = $kehu['companyName']??"";
  647. return app_show(0,"获取成功",$poolinfo);
  648. }
  649. //退票列表
  650. public function returnList(){
  651. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  652. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  653. $condition=[["b.is_del","=",0]];
  654. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  655. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  656. if($start!=""){
  657. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  658. }
  659. if($end!=""){
  660. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  661. }
  662. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  663. if($inv_type!==""){
  664. $condition[]=["b.inv_type","=",$inv_type];
  665. }
  666. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  667. if($apply_id!==""){
  668. $condition[]=["a.apply_id","=",$apply_id];
  669. }
  670. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  671. if($apply_name!==""){
  672. $condition[]=["a.apply_name","like","%$apply_name%"];
  673. }
  674. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  675. if($status!==""){
  676. $condition[]=["a.status","=",$status];
  677. }
  678. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  679. if($invNo!==""){
  680. $condition[]=["a.invNo","like","%$invNo%"];
  681. }
  682. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  683. if($returnCode!==""){
  684. $condition[]=["a.returnCode","like","%$returnCode%"];
  685. }
  686. $count=Db::name("invoice_return")->alias("a")
  687. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  688. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  689. ->where($condition)->count();
  690. $total =ceil($count/$size);
  691. $page= $page>$total ? intval($total):$page;
  692. $list=Db::name("invoice_return")->alias("a")
  693. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  694. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  695. ->where($condition)->page($page,$size)->field("c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  696. 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,
  697. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_in,b.inv_type,b.open_type,is_ticket,b.email,
  698. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code,a.*")->select();
  699. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  700. }
  701. }