OrderInv.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. "customerNo"=>$value['customerNo'],
  79. "viceCode"=>$invNo,
  80. "order_total"=>$qrd['totalPrice'],
  81. "vice_total"=>$invfee,
  82. "cancel_fee"=>$value['inv_fee'],
  83. "status"=>1,
  84. "addtime"=>date("Y-m-d H:i:s"),
  85. "updatetime"=>date("Y-m-d H:i:s")
  86. ];
  87. $update = [
  88. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  89. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  90. "inv_status"=>2,
  91. "status"=>1,
  92. "updatetime"=>date("Y-m-d H:i:s")
  93. ];
  94. $up = Db::name("qrd_info")->where($qrd)->update($update);
  95. if($up==false){
  96. Db::rollback();
  97. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  98. }
  99. $temp[]=$ascc;
  100. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  101. if($report)$report->setField("invNo",$invNo);
  102. }
  103. $associn = Db::name("assoc")->insertAll($temp);
  104. if($associn==0){
  105. Db::rollback();
  106. return error_show(1005,"确认单申请开票失败");
  107. }
  108. $inv=[
  109. "invNo"=>$invNo,
  110. "inv_value"=>$invfee,
  111. "inv_out"=> $companyNo,
  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. if($report)$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. if($report)$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. if($report)$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. //红冲票
  508. public function returnRed(){
  509. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  510. if($returnCode==""){
  511. return error_show(1005,"参数 returnCode 不能为空");
  512. }
  513. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  514. if($redinv==""){
  515. return error_show(1005,"参数 redinv 不能为空");
  516. }
  517. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  518. if($return==false){
  519. return error_show(1005,"退票申请信息未找到");
  520. }
  521. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  522. if($invinfo==false){
  523. return error_show(1005,"发票申请信息未找到");
  524. }
  525. Db::startTrans();
  526. try{
  527. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  528. $up =Db::name("invoice_return")->where($return)->update($update);
  529. if($up){
  530. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  531. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  532. if($inv){
  533. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  534. if(!empty($qrdArr)){
  535. foreach ($qrdArr as $value){
  536. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  537. if($qrdinfo==false){
  538. Db::rollback();
  539. return error_show(1003,"确认单信息未找到");
  540. }
  541. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  542. Db::rollback();
  543. return error_show(1003,"确认单信息开票金额不足");
  544. }
  545. $update =[
  546. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  547. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  548. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 ? 1 : 2,
  549. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  550. "updatetime"=>date("Y-m-d H:i:s"),
  551. ];
  552. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  553. if($qrdup==false){
  554. Db::rollback();
  555. return error_show(1003,"确认单信息更新失败");
  556. }
  557. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  558. $assocup =Db::name("assoc")->where($value)->update($assoc);
  559. if($assocup==false){
  560. Db::rollback();
  561. return error_show(1003,"确认单关联信息更新失败");
  562. }
  563. }
  564. }
  565. Db::commit();
  566. return app_show(0,"退票申请信息更新成功");
  567. }
  568. }
  569. Db::rollback();
  570. return error_show(1005,"退票申请信息更新失败");
  571. }catch (\Exception $e){
  572. Db::rollback();
  573. return error_show(1005,$e->getMessage());
  574. }
  575. }
  576. //退票详情
  577. public function returnInfo(){
  578. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  579. if($returnCode==""){
  580. return error_show(1005,"参数 returnCode 不能为空");
  581. }
  582. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  583. if($return==false){
  584. return error_show(1005,"退票申请信息未找到");
  585. }
  586. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  587. $return['inv_out']= $invinfo['inv_out']??"";
  588. $return['inv_value']= $invinfo['inv_value']??"";
  589. $return['inv_apply_id']= $invinfo['apply_id']??"";
  590. $return['inv_apply_name']= $invinfo['apply_name']??"";
  591. $return['inv_type']= $invinfo['inv_type']??"";
  592. $return['open_type']= $invinfo['open_type']??"";
  593. $return['is_ticket']= $invinfo['is_ticket']??"";
  594. $return['email']= $invinfo['email']??"";
  595. $return['post_code']= $invinfo['post_code']??"";
  596. $return['post_company']= $invinfo['post_company']??"";
  597. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])
  598. ->field("buyer_title,buyer_code,buyer_addr,buyer_mobile,buyer_bank,buyer_bankNo,seller_title,seller_addr,seller_code,seller_mobile,seller_bank,seller_bankNo")
  599. ->find();
  600. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  601. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  602. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  603. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  604. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  605. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  606. $return['seller_title'] = $poolinfo['seller_title']??'';
  607. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  608. $return['seller_code'] = $poolinfo['seller_code']??'';
  609. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  610. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  611. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  612. return app_show(0,"获取成功",$return);
  613. }
  614. /**
  615. *发票池列表
  616. */
  617. public function list(){
  618. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  619. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  620. $condition=[["a.is_del","=",0]];
  621. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  622. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  623. if($start!=""){
  624. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  625. }
  626. if($end!=""){
  627. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  628. }
  629. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  630. if($status!==""){
  631. $condition[]=["a.status","=",$status];
  632. }
  633. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  634. if($inv_type!==""){
  635. $condition[]=["a.inv_type","=",$inv_type];
  636. }
  637. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  638. if($inv_out!==""){
  639. $condition[]=["a.inv_out","=",$inv_out];
  640. }
  641. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  642. // if($inv_in!==""){
  643. // $condition[]=["a.inv_in","=",$inv_in];
  644. // }
  645. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  646. if($invNo!==""){
  647. $condition[]=["a.invNo","like","%$invNo%"];
  648. }
  649. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  650. if($apply_id!==""){
  651. $condition[]=["a.apply_id","=",$apply_id];
  652. }
  653. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  654. if($apply_name!==""){
  655. $condition[]=["a.apply_name","like","%$apply_name%"];
  656. }
  657. $count=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  658. ($condition)->count();
  659. $total =ceil($count/$size);
  660. $page= $page>$total ? intval($total):$page;
  661. $list=Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where($condition)
  662. ->page($page,$size)->select()->toArray();
  663. foreach ($list as &$item) {
  664. $company =Db::name("company_info")->where(["companyNo"=>$item['inv_out']])->find();
  665. // $kehu =Db::name("customer_info")->where(["companyNo"=>$item['inv_in']])->find();
  666. $item['companyName'] = $company['company_name']??"";
  667. // $item['customerName'] = $kehu['companyName']??"";
  668. }
  669. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  670. }
  671. //获取发票详情
  672. public function info(){
  673. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  674. if($invNo==''){
  675. return error_show(1004,"参数 invNo 不能为空");
  676. }
  677. $poolinfo =Db::name("invoice_pool")->alias("a")->leftJoin("invoice_pool_info b","a.invNo=b.invNo")->where
  678. ([["a.invNo","=",$invNo],["a.is_del","=",0]])->findOrEmpty();
  679. if(empty($poolinfo)){
  680. return error_show(1004,"为找到开票信息");
  681. }
  682. $company =Db::name("company_info")->where(["companyNo"=>$poolinfo['inv_out']])->find();
  683. // $kehu =Db::name("customer_info")->where(["companyNo"=>$poolinfo['inv_in']])->find();
  684. $poolinfo['companyName'] = $company['company_name']??"";
  685. // $poolinfo['customerName'] = $kehu['companyName']??"";
  686. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])
  687. ->field("buyer_title,buyer_code,buyer_addr,buyer_mobile,buyer_bank,buyer_bankNo,seller_title,seller_addr,seller_code,seller_mobile,seller_bank,seller_bankNo")
  688. ->find();
  689. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  690. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  691. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  692. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  693. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  694. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  695. $poolinfo['seller_title'] = $pool['seller_title']??'';
  696. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  697. $poolinfo['seller_code'] = $pool['seller_code']??'';
  698. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  699. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  700. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  701. return app_show(0,"获取成功",$poolinfo);
  702. }
  703. //退票列表
  704. public function returnList(){
  705. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  706. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  707. $condition=[["b.is_del","=",0]];
  708. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  709. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  710. if($start!=""){
  711. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  712. }
  713. if($end!=""){
  714. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  715. }
  716. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? intval($this->post['inv_type']):"";
  717. if($inv_type!==""){
  718. $condition[]=["b.inv_type","=",$inv_type];
  719. }
  720. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  721. if($apply_id!==""){
  722. $condition[]=["a.apply_id","=",$apply_id];
  723. }
  724. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  725. if($apply_name!==""){
  726. $condition[]=["a.apply_name","like","%$apply_name%"];
  727. }
  728. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  729. if($status!==""){
  730. $condition[]=["a.status","=",$status];
  731. }
  732. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  733. if($invNo!==""){
  734. $condition[]=["a.invNo","like","%$invNo%"];
  735. }
  736. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  737. if($returnCode!==""){
  738. $condition[]=["a.returnCode","like","%$returnCode%"];
  739. }
  740. $count=Db::name("invoice_return")->alias("a")
  741. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  742. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  743. ->where($condition)->count();
  744. $total =ceil($count/$size);
  745. $page= $page>$total ? intval($total):$page;
  746. $list=Db::name("invoice_return")->alias("a")
  747. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  748. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  749. ->where($condition)->page($page,$size)->field("c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  750. 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,
  751. 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,
  752. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code,a.*")->select();
  753. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  754. }
  755. }