OrderInv.php 34 KB

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