Ver código fonte

记账记录

afa 5 meses atrás
pai
commit
ca74b956c6

+ 65 - 25
app/admin/controller/user/MoneyLog.php

@@ -7,14 +7,14 @@ use app\common\controller\Backend;
 use app\admin\traits\Actions;
 use think\annotation\route\Group;
 use think\annotation\route\Route;
+use think\facade\Db;
 use app\common\model\MoneyLog as MoneyLogModel;
 
 #[Group("user/money_log")]
 class MoneyLog extends Backend
 {
     use Actions{
-        index as private _index;
-        del as private _del;
+        edit as private _edit;
         multi as private _multi;
         import as private _import;
     }
@@ -23,46 +23,86 @@ class MoneyLog extends Backend
     {
         parent::_initialize();
         $this->model = new MoneyLogModel();
-        $this->assign('typeList',  site_config('addonsd.bank_account'));
+        //$this->assign('typeList',  site_config('addonsd.money_in_type'));
+        $list = site_config('addonsd.money_out_type');
+        //dump(  $list);die;
+        $this->assign('bankList',  site_config('addonsd.bank_account'));
         $this->relationField=['users'];
     }
 
-    //查看
-    #[Route("GET,JSON","index")]
+    //查看 
+    #[Route('GET,JSON','index')]
     public function index()
     {
-        return $this->_index();
-    }
-
-    //添加
-    #[Route("GET,POST","add")]
-    public function add()
-    {
-        //通过定义postParams来增加或覆盖post提交的表单
-        $this->postParams=[];
-        //通过定义callback回调函数来执行添加后的操作
-        $this->callback=function ($model){};
-        return $this->_add();
+        if (false === $this->request->isAjax()) {
+            return $this->fetch();
+        }
+        if($this->request->post('selectpage')){
+            return $this->selectpage();
+        }
+        [$where, $order, $limit, $with] = $this->buildparams();
+        $list = $this->model
+            ->withJoin($with,'left')
+            //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
+            ->where('money_log.status', 1)
+            ->where($where)
+            ->order($order)
+            ->paginate($limit);
+        $result = ['total' => $list->total(), 'rows' => $list->items()];
+        return json($result);
     }
+   
 
     //修改
     #[Route("GET,POST","edit")]
-    public function edit()
+     public function edit()
     {
-        //通过定义postParams来增加或覆盖post提交的表单
-        $this->postParams=[];
-        //通过定义callback回调函数来执行修改后的操作
-        $this->callback=function ($model){};
+        if(!$this->request->isPost()){
+            $catelist= site_config('addonsd.money_in_type');;
+            $this->assign('parentList',$catelist);
+        }
         return $this->_edit();
     }
 
+  
+
     //删除
     #[Route("GET,POST","del")]
     public function del()
     {
-        //通过定义callback回调函数来执行删除后的操作
-        $this->callback=function ($ids){};
-        return $this->_del();
+         $ids = $this->request->param("ids");
+        if (empty($ids)) {
+            $this->error(__('参数%s不能为空', ['s'=>'ids']));
+        }
+        $pk = $this->model->getPk();
+        $list = $this->model->where($pk, 'in', $ids)->select();
+        $count = 0;
+        Db::startTrans();
+        try {
+            foreach ($list as $item) {
+                if(count($this->volidateFields)>0){
+                    foreach ($this->volidateFields as $field=>$value){
+                        if($item[$field]!=$value){
+                            $this->error(__('没有操作权限'));
+                        }
+                    }
+                }
+                $item->status=0;
+                $count += $item->save();
+            }
+            if($this->callback){
+                $callback=$this->callback;
+                $callback($ids);
+            }
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($count) {
+            $this->success();
+        }
+        $this->error(__('没有记录被删除'));
     }
 
     //更新

+ 21 - 26
app/admin/view/user/money_log/add.html

@@ -11,7 +11,16 @@
             <template #default>
                 {:token_field()}
             </template>
-    
+            <template v-slot:type_name="{rows}">
+                <el-form-item label="{:__('父级')}:" prop="type_name">
+                    <el-select placeholder="{:__('请选择父级')}" v-model="rows.type_name" :clearable="true" style="width: 100%">
+                        <el-option key="all" label="无" value="0"></el-option>
+                        {foreach name="parentList" item="vo"}
+                        <el-option v-if="rows.type_name=='{$vo}'" key="{$key}"  value="{$vo}"></el-option>
+                        {/foreach}
+                    </el-select>
+                </el-form-item>
+            </template>
         </yun-form>
     </el-card>
 </template>
@@ -24,37 +33,23 @@ export default{
     data:{
         columns:[
             {field:"id",title:"ID",edit:"hidden"},
-            {field:"user_id",title:"user_id",edit:"number"},
-            {field:"type",title:"记账类型:收入 1;支出 2",edit:"select",rules:"required",searchList:{1:"是",0:"否"}},
-            {field:"type_name",title:"账变名称,记录汉字",edit:"text",rules:"required"},
-            {field:"bank_account",title:"记账账户,记录key",edit:"text",rules:"required"},
+            {field:"type",title:"记账类型", edit: {form:'radio',change:'changeType'},rules:"required",searchList:{1:"收入", 2:"支出"} },
+           // {field:"type_name",title:"账变名称",edit:"text",rules:"required"},
+            {field: 'type_name',title:__('账变名称'),edit: {form:'slot',value:'0'}},
+            {field:"bank_account",title:"记账账户",edit:"radio",rules:"required", searchList: Yunqi.data.bankList},
             {field:"change",title:"变动金额",edit:"text",rules:"required"},
-            {field:"remark",title:"备注",edit:"text",rules:"required"},
-            {field:"create_date",title:"创建日期,编辑后同步修改",edit:"text",rules:"required"},
-            {field:"createtime",title:"创建时间"},
-            {field:"updatetime",title:"修改时间"},
+            {field:"create_date",title:"记账日期",edit:"date",rules:"required"},
+            {field:"remark",title:"备注",edit:"textarea"},
+ 
         ],
         row:Yunqi.data.row || {}
     },
-    //页面加载完成时执行
-    onLoad:function(query){
-        console.log(query);
-    },
-    //页面初始显示或在框架内显示时执行
-    onShow:function(){
-
-    },
-    //页面在框架内隐藏时执行
-    onHide:function(){
+ 
 
-    },
-    //页面在框架内关闭时执行
-    onUnload:function(){
-
-    },
     methods: {
-        onFormRender:function(rows){
-            //表单渲染完成后执行
+        changeType:function(data,row){
+            
+             row.type_name='0'
         },
         onSubmit:function(rows){
             //表单提交前执行,返回false可以阻止表单提交

+ 4 - 3
app/admin/view/user/money_log/index.html

@@ -4,9 +4,9 @@
                 :columns="columns"
                 ref="yuntable"
                 @render="onTableRender"
-                toolbar="refresh,del,import"
+                toolbar="refresh,edit,del,import"
                 :auth="{
-           
+                    edit:{:$auth->check('app\\admin\\controller\\user\\MoneyLog','edit')},
                     del:{:$auth->check('app\\admin\\controller\\user\\MoneyLog','del')},
                     multi:{:$auth->check('app\\admin\\controller\\user\\MoneyLog','multi')},
                     import:{:$auth->check('app\\admin\\controller\\user\\MoneyLog','import')},
@@ -24,6 +24,7 @@ export default{
     data:{
         extend:{
             index_url: 'user/money_log/index',
+            edit_url: 'user/money_log/edit',
             del_url: 'user/money_log/del',
             multi_url: 'user/money_log/multi',
             import_url: 'user/money_log/import',
@@ -37,7 +38,7 @@ export default{
             {field:"users.nickname",title:"记账人"}, 
             {field:"type",title:"记账类型",operate:"select",searchList:{1:"收入", 2:"支出"},formatter:Yunqi.formatter.tag},
             {field:"type_name",title:"账变名称",operate:false},
-            {field: 'bank_account',title: __('记账账户'),operate: "select", searchList: Yunqi.data.typeList},
+            {field: 'bank_account',title: __('记账账户'),operate: "select", searchList: Yunqi.data.bankList},
             {field:"change",title:"变动金额",operate:false},
             {field:"remark",title:"备注",operate:false},
             {field:"create_date",title:"记账日期",operate:false},

+ 6 - 7
app/api/controller/MoneyLog.php

@@ -19,9 +19,8 @@ class MoneyLog extends Base
       {
 
             $time = $this->request->post('time/s', date('Y-m'));
-            $result['count'] = $monuyModel::getCountMonthBalance($this->userinfo['id'], $time);
-            $result['list']  = $monuyModel->where('user_id', $this->userinfo['id'])
-                              ->where('status', MonuyModel::STATUS_NORMAL)
+            $result['count'] = $monuyModel::getCountMonthBalance($time);
+            $result['list']  = $monuyModel->where('status', MonuyModel::STATUS_NORMAL)
                               ->whereMonth('create_date', $time)
                               ->order('id desc')
                               ->paginate(10);
@@ -36,12 +35,12 @@ class MoneyLog extends Base
             $time = $this->request->post('time/s', date('Y-m'));
             //月统计 
             if($type == 1){
-                  $result = $monuyModel::getCountMonthBalance($this->userinfo['id'], $time);
+                  $result = $monuyModel::getCountMonthBalance($time);
                   //日均支出
                   $day = getDaysOfMonth($time);
                   $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
             }else{
-                  $result = $monuyModel::getCountYearBalance($this->userinfo['id'], $time);
+                  $result = $monuyModel::getCountYearBalance($time);
                   //日均支出
                   $day = getDaysOfYear($time);
                   $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
@@ -85,7 +84,7 @@ class MoneyLog extends Base
             try {
                   validate(MoneyValidate::class)->scene('edit')->check($data);
                   
-                  $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
+                  $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
                   if(!$rows) $this->error('数据不存在');
                   //发货数据
                   $result = $rows->save($data);
@@ -111,7 +110,7 @@ class MoneyLog extends Base
             $result = false;
             Db::startTrans();
             try {
-                  $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
+                  $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
                   if(!$rows) $this->error('数据不存在');
                   //发货数据
                   $rows->status = $monuyModel::STATUS_HIDDEN;

+ 10 - 10
app/common/model/MoneyLog.php

@@ -39,17 +39,17 @@ class MoneyLog Extends Model
     
 
     //按照类型月统计 收入- 支出
-    public static function getCountMonthBalance(int $user_id = 0, string $time = '')
+    public static function getCountMonthBalance(string $time = '')
     {
         $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
-        if(!empty($user_id) && !empty($time)){
-            $income = $income->where('user_id', $user_id)->whereMonth('create_date', $time);
+        if(!empty($time)){
+            $income = $income->whereMonth('create_date', $time);
         }
         $income = $income->sum('change');
         //支出
         $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
-        if(!empty($user_id) && !empty($time)){
-            $expenditure = $expenditure->where('user_id', $user_id)->whereMonth('create_date', $time);
+        if(!empty($time)){
+            $expenditure = $expenditure->whereMonth('create_date', $time);
         }
         $expenditure = $expenditure->sum('change');
         $balance = $income - $expenditure;
@@ -62,18 +62,18 @@ class MoneyLog Extends Model
 
 
     //按照类型年统计 收入- 支出
-    public static function getCountYearBalance(int $user_id = 0, string $time = '')
+    public static function getCountYearBalance(string $time = '')
 
     {
         $income = self::where('type', self::TYPE_INCOME)->where('status', self::STATUS_NORMAL);
-        if(!empty($user_id) && !empty($time)){
-            $income = $income->where('user_id', $user_id)->whereYear('create_date', $time);
+        if(!empty($time)){
+            $income = $income->whereYear('create_date', $time);
         }
         $income = $income->sum('change');
         //支出
         $expenditure = self::where('type', self::TYPE_EXPENDITURE)->where('status', self::STATUS_NORMAL);
-        if(!empty($user_id) && !empty($time)){
-            $expenditure = $expenditure->where('user_id', $user_id)->whereYear('create_date', $time);
+        if(!empty($time)){
+            $expenditure = $expenditure->whereYear('create_date', $time);
         }
         $expenditure = $expenditure->sum('change');
         $balance = $income - $expenditure;