FinancialCancel.php 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\FinancialCheck;
  4. use app\cxinv\model\FinancialTz;
  5. use app\cxinv\model\FinancialManager;
  6. use app\cxinv\model\ProductFz;
  7. use think\App;
  8. class FinancialCancel extends Base
  9. {
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $this->model = new \app\cxinv\model\FinancialCancel();
  14. }
  15. public function create(){
  16. $params=$this->request->param(['type'=>"","item_ids"=>[],"remark"=>""],"post",'trim');
  17. $valid = $this->validate($params,[
  18. 'type|类型'=>'require|in:1,2',
  19. 'item_ids|订单id'=>'require|array',
  20. 'remark|备注'=>'max:255',
  21. ]);
  22. if($valid!== true) return error($valid);
  23. $create=[];
  24. foreach ($params['item_ids'] as $k=>$v){
  25. if($params['type']==1){
  26. $ManagerInfo=FinancialManager::where('id',$v)->findOrEmpty();
  27. if($ManagerInfo->isEmpty()) return error("[$v]订单数据不存在");
  28. if($ManagerInfo->status!=2 && $ManagerInfo->status!=3) return error("[$v]订单数据状态不正确");
  29. if($ManagerInfo->type==1||$ManagerInfo->type==3){
  30. //校验账期是否封账
  31. $company_code=$ManagerInfo->buyer_code;
  32. }else{
  33. $company_code=$ManagerInfo->seller_code;
  34. }
  35. $fz = ProductFz::where('company_code',$company_code)->where('fz_date',$ManagerInfo->fz_date)->findOrEmpty();
  36. if($fz->isEmpty()) return error("[$v]账期数据不存在");
  37. if($fz->status!=0) return error("[$v]账期数据状态已封账/未解封");
  38. if($ManagerInfo->status==3){
  39. $TzInfo=FinancialTz::where('manager_id',$v)->findOrEmpty();
  40. if(!$TzInfo->isEmpty()){
  41. if($TzInfo->status!=4) return error("[$v]计提数据单子未处理");
  42. }
  43. }
  44. }else{
  45. $TzInfo=FinancialTz::with(['Manager'])->where('id',$v)->findOrEmpty();
  46. if($TzInfo->isEmpty()) return error("[$v]计提数据不存在");
  47. if($TzInfo->status!=1 && $TzInfo->status!=2) return error("[$v]计提数据状态不正确");
  48. if($TzInfo->status==2){
  49. $fz = ProductFz::where('company_code',$TzInfo->Manager->seller_code)->where('fz_date',$TzInfo->fz_date)->findOrEmpty();
  50. if($fz->isEmpty()) return error("[$v]账期数据不存在");
  51. if($fz->status!=0) return error("[$v]账期数据状态已封账/未解封");
  52. }
  53. if($TzInfo->is_checkOrder==1){
  54. $check=FinancialCheck::where('code',$TzInfo->ktCode)->findOrEmpty();
  55. if(!$check->isEmpty()) return error("[$v]调整单未删除,请先处理关联调整单");
  56. }
  57. }
  58. $create[]=[
  59. 'type'=>$params['type'],
  60. 'item_id'=>$v,
  61. 'remark'=>$params['remark'],
  62. 'apply_id'=>$this->uid,
  63. 'apply_name'=>$this->uname,
  64. ];
  65. }
  66. $this->model->startTrans();
  67. try{
  68. $res=$this->model->saveAll($create);
  69. if($res->isEmpty()) throw new \Exception("删除数据失败");
  70. $this->model->commit();
  71. }catch (\Exception $e){
  72. $this->model->rollback();
  73. return error($e->getMessage());
  74. }
  75. return success("删除数据成功");
  76. }
  77. //列表
  78. public function list(){
  79. $params=$this->request->param(['type'=>1,"item_id"=>"","page"=>1,"size"=>20],"post",'trim');
  80. $where=[];
  81. if($params['type']!='') {
  82. $where[]=['type','=',$params['type']];
  83. $model=($params['type']==1)?'FinancialManager':'FinancialTz';
  84. }
  85. if($params['item_id']!='') $where[]=['item_id','=',$params['item_id']];
  86. $list=$this->model->with([$model])->where($where)->order('id desc')->paginate(['page'=>$params['page'],'list_rows'=>$params['size']]);
  87. return success("获取成功",['list'=>$list->items(),'count'=>$list->total()]);
  88. }
  89. }