dongxiaoqin 10 месяцев назад
Родитель
Сommit
1f23ea0605

+ 37 - 0
app/adminapi/controller/finance/RetentionMoneyLogController.php

@@ -0,0 +1,37 @@
+<?php
+namespace app\adminapi\controller\finance;
+
+use app\adminapi\controller\BaseAdminController;
+use app\adminapi\lists\finance\RetentionMoneyLogLists;
+use app\adminapi\logic\master_worker\MasterWorkerLogic;
+use app\adminapi\validate\master_worker\MasterWorkerValidate;
+
+/***
+ * 工程师质保金流水控制器
+ * Class RetentionMoneyLogController
+ * @package app\adminapi\controller
+ */
+class RetentionMoneyLogController extends BaseAdminController
+{
+
+    /**
+     * @notes 质保金流水明细
+     */
+    public function lists()
+    {
+        return $this->dataLists(new RetentionMoneyLogLists());
+    }
+
+    /**
+     * @notes 质保金退费审核
+     */
+    public function audit()
+    {
+        $params = (new MasterWorkerValidate())->post()->goCheck('audit',['admin_id'=>$this->adminId]);
+        $result = MasterWorkerLogic::retentionMoneyAudit($params);
+        if (true === $result) {
+            return $this->success('操作成功', [], 1, 1);
+        }
+        return $this->fail(MasterWorkerLogic::getError());
+    }
+}

+ 14 - 0
app/adminapi/controller/master_worker/MasterWorkerController.php

@@ -222,4 +222,18 @@ class MasterWorkerController extends BaseAdminController
         return $this->success('', $result, 1, 1);
         
     }
+    /**
+     * @notes 工程师质保金退费申请
+     * @return \think\response\Json
+     */
+    public function retentionMoneyRefund()
+    {
+        $params = request()->post();
+        $result = MasterWorkerLogic::retentionMoneyRefund($params);
+        if (false === $result) {
+            return $this->fail(MasterWorkerLogic::getError());
+        }
+        return $this->success('', $result, 1, 1);
+        
+    }
 }

+ 106 - 0
app/adminapi/lists/finance/RetentionMoneyLogLists.php

@@ -0,0 +1,106 @@
+<?php
+namespace app\adminapi\lists\finance;
+
+use app\adminapi\lists\BaseAdminDataLists;
+use app\common\lists\ListsSearchInterface;
+use app\common\lists\ListsExtendInterface;
+use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
+
+/**
+ * 质保金流水列表
+ * Class RetentionMoneyLogLists
+ * @package app\adminapi\lists\finance
+ */
+class RetentionMoneyLogLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
+{
+
+    /**
+     * @notes 搜索条件
+     * @return array
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['l.status','l.action'],
+        ];
+    }
+
+    /**
+     * @notes 搜索条件
+     */
+    public function queryWhere()
+    {
+        $where = [];
+        if (!empty($this->params['worker'])) {
+            $where[] = ['w.real_name|w.mobile', 'like', '%' . $this->params['worker'] . '%'];
+        }
+
+        if (!empty($this->params['start_time'])) {
+            $where[] = ['l.create_time', '>=', strtotime($this->params['start_time'])];
+        }
+
+        if (!empty($this->params['end_time'])) {
+            $where[] = ['l.create_time', '<=', strtotime($this->params['end_time'])];
+        }
+
+        return $where;
+    }
+
+
+    /**
+     * @notes 获取列表
+     * @return array
+     */
+    public function lists(): array
+    {
+        $field = 'w.real_name,w.mobile,l.*';
+        $lists = MasterWorkerRetentionMoneyLog::alias('l')
+            ->join('master_worker w', 'w.id = l.worker_id')
+            ->field($field)
+            ->where($this->searchWhere)
+            ->where($this->queryWhere())
+            ->order('l.id', 'desc')
+            ->limit($this->limitOffset, $this->limitLength)
+            ->select()
+            ->toArray();
+
+        foreach ($lists as &$item) {
+            $symbol = $item['action'] == 1 ? '+' : '-';
+            $item['amount'] = $symbol . $item['amount'];
+        }
+        return $lists;
+    }
+
+    /**
+     * @notes 获取数量
+     * @return int
+     */
+    public function count(): int
+    {
+        return MasterWorkerRetentionMoneyLog::alias('l')
+            ->join('master_worker w', 'w.id = l.worker_id')
+            ->where($this->queryWhere())
+            ->where($this->searchWhere)
+            ->count();
+    }
+
+    /**
+     * @notes 额外参数
+     * @return mixed|null
+     */
+    public function extend()
+    {
+        $count = (new MasterWorkerRetentionMoneyLog())->alias('l')
+        ->join('master_worker w', 'w.id = l.worker_id')
+            ->field([
+                'count(l.id) as total',
+                'count(if(l.action=1, true, null)) as ing',
+                'count(if(l.action=2, true, null)) as success',
+            ])
+            ->where($this->searchWhere)
+            ->where($this->queryWhere(false))
+            ->select()->toArray();
+
+        return array_shift($count);
+    }
+}

+ 64 - 0
app/adminapi/logic/master_worker/MasterWorkerLogic.php

@@ -428,4 +428,68 @@ class MasterWorkerLogic extends BaseLogic
         }
         return $order;
     }
+
+    /**
+     * 工程师质保金退费申请
+     * @param $params
+     * @return array|false|string
+     * @throws \Exception
+     */
+    public static function retentionMoneyRefund($params){
+        
+        $params['amount'] = (float)$params['amount']??0;
+        $log = MasterWorkerRetentionMoneyLog::where(['worker_id'=>$params['worker_id'], 'action' => 2, 'source' => 2])->where('status',0)->order('id','desc')->findOrEmpty();
+        if($log->isEmpty()){
+            $sn = generate_sn(MasterWorkerRetentionMoneyLog::class, 'sn');
+            $log = MasterWorkerRetentionMoneyLog::create([
+                'sn' => $sn, 
+                'status'=>0,
+                'worker_id'=>$params['worker_id'],
+                'amount'=>$params['amount']??0,
+                'action' => 2, // 退款
+                'remark' => $params['remark']??'',
+                'source' => 2, // 后台
+            ]);
+        } elseif($params['amount']>0){ // 可以改价
+                $log->amount= $params['amount']??0;
+                $log->remark= $params['remark']??0;
+                $log->save();
+        }
+        return $log->toArray();
+    }
+
+    /**
+     * 工程师质保金退费申请
+     * @param $params
+     * @return array|false|string
+     * @throws \Exception
+     */
+    public static function retentionMoneyAudit($params){
+        
+        $status = $params['status']??0;
+        Db::startTrans();
+        try {
+            $log = MasterWorkerRetentionMoneyLog::where('id',$params['id'])->findOrEmpty();
+            if($log->isEmpty()){
+                self::setError('退费记录不存在');
+                return false;
+            } else{ 
+                if ($status == 1) {
+                    //审核通过
+                    $earnest_money_usable = MasterWorker::where('id', $log->worker_id)->value('earnest_money_usable');
+                    MasterWorker::where('id', $log->worker_id)->dec('earnest_money_usable', $log->amount)->save();
+                    $log->remark .= ' 退费前余额:'.$earnest_money_usable.'元,退费后余额:'.($earnest_money_usable-$log->amount).'元';
+                } 
+                $log->status = $status;
+                $log->save();
+            }
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+    
 }

+ 9 - 0
app/adminapi/validate/master_worker/MasterWorkerValidate.php

@@ -151,4 +151,13 @@ class MasterWorkerValidate extends BaseValidate
         return $this->only(['id', 'skill']);
 
     }
+
+    /**
+     * @notes 质保金退费审核
+     * @return MasterWorkerValidate
+     */
+    public function sceneAudit()
+    {
+        return $this->only(['id','status']);
+    }
 }