diff --git a/application/admin/controller/Dashboard.php b/application/admin/controller/Dashboard.php index f68c923..564b2ba 100644 --- a/application/admin/controller/Dashboard.php +++ b/application/admin/controller/Dashboard.php @@ -3,11 +3,13 @@ namespace app\admin\controller; use app\admin\model\Admin; +use app\admin\model\car\Sales; use app\admin\model\User; use app\common\controller\Backend; use app\common\model\Attachment; use fast\Date; use think\Db; +use function Symfony\Component\Clock\now; /** * 控制台 @@ -24,18 +26,58 @@ class Dashboard extends Backend public function index() { + $start = now()->modify('-30 days')->format('Y-m-d'); + $end_at = now()->format('Y-m-d'); + $default_daterange = $start . ' - ' . $end_at; + + $this->view->assign('default_daterange',$default_daterange); + return $this->view->fetch(); + } + + public function getData() + { $car_num = Db::query('SELECT COUNT(IF(car_type = 1,1,null)) new_car, COUNT(IF(car_type = 3,1,null)) rent_car, COUNT(IF(car_type = 2,1,null)) old_car FROM cars;')[0]; - $this->view->assign([ - ...$car_num + + $start = now()->modify('-30 days')->format('Y-m-d'); + $end_at = now()->format('Y-m-d 23:29:59'); + + $filter = request()->get('range',''); + if (!empty($filter)){ + $arr = explode(' - ', $filter); + if (trim($arr[0])) { + $start = trim($arr[0]). ' 00:00:00'; + } + if (trim($arr[1])) { + $end_at = trim($arr[1]) . ' 23:29:59'; + } + } + + $build = new Sales(); + $build->whereBetween('finish_at', [$start, $end_at]); + + $group = \model('auth_group_access')->where('uid', $this->auth->id)->find()->group_id ?? 0; + + if (!in_array($group,[1,2])){ + $build->where('saler_id',$this->auth->id); + } + $sale = $build->field([ + "count(if(type=1,1,null)) new_count", + "count(if(type=2,1,null)) old_count", + "count(if(type=3,1,null)) rent_count", + "sum(if(type=1,total_price,0)) new_sum", + "sum(if(type=2,total_price,0)) old_sum", + "sum(if(type=3,total_price,0)) rent_sum", + "sum(total_price) total_price", + ])->select()[0]->toArray(); + + $this->success(data:[ + ...$car_num,...$sale ]); - - - return $this->view->fetch(); } } diff --git a/application/admin/controller/car/Sales.php b/application/admin/controller/car/Sales.php index b9f70d7..109c6c8 100644 --- a/application/admin/controller/car/Sales.php +++ b/application/admin/controller/car/Sales.php @@ -4,6 +4,7 @@ namespace app\admin\controller\car; use app\admin\model\Cars; use app\common\controller\Backend; +use Carbon\Carbon; use think\Db; use think\exception\DbException; use think\exception\PDOException; @@ -27,7 +28,11 @@ class Sales extends Backend { parent::_initialize(); $this->model = new \app\admin\model\car\Sales; + $users = Db::name('admin') + ->field(['id', 'nickname']) + ->select(); + $this->view->assign('users',$users); } @@ -77,6 +82,17 @@ class Sales extends Backend $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } + + $params['saler_id'] = ($params['saler_id'] ?? -1) == -1 ? $this->auth->id : $params['saler_id']; + if (empty($params['saler_id'])) { + $params['saler_id'] = $this->auth->id; + } + + if ($params['type'] != 3){ + $params['finish_at'] = (new Carbon())->format('Y-m-d H:i:s'); + } + + $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { @@ -108,6 +124,18 @@ class Sales extends Backend $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { + + $car = Cars::where('id',$row->cart_id)->with([ + 'brand' =>function ($q) { + $q->field('id,name'); + }, + 'series' =>function ($q) { + $q->field('id,name'); + }, + ] + )->select()[0] ?? null; + $this->view->assign('car',$car); + $this->view->assign('row', $row); return $this->view->fetch(); } @@ -125,6 +153,12 @@ class Sales extends Backend $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } + + $params['saler_id'] = ($params['saler_id'] ?? -1) == -1 ? $this->auth->id : $params['saler_id']; + if (empty($params['saler_id'])) { + $params['saler_id'] = $this->auth->id; + } + $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { @@ -137,5 +171,79 @@ class Sales extends Backend $this->success(); } + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'trim']); + if (false === $this->request->isAjax()) { + return $this->view->fetch(); + } + //如果发送的来源是 Selectpage,则转发到 Selectpage + if ($this->request->request('keyField')) { + return $this->selectpage(); + } + [$where, $sort, $order, $offset, $limit] = $this->buildparams(); + $list = $this->model + ->where($where) + ->with('saler') + ->order($sort, $order) + ->paginate($limit); + $result = ['total' => $list->total(), 'rows' => $list->items()]; + return json($result); + } + + public function end($ids = null) + { + $row = $this->model->get($ids); + if (!$row) { + $this->error(__('No Results were found')); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { + $this->error(__('You have no permission')); + } + if (false === $this->request->isPost()) { + + $car = Cars::where('id',$row->cart_id)->with([ + 'brand' =>function ($q) { + $q->field('id,name'); + }, + 'series' =>function ($q) { + $q->field('id,name'); + }, + ] + )->select()[0] ?? null; + $this->view->assign('car',$car); + + $this->view->assign('row', $row); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException()->validate($validate); + } + + $params['finish_at'] = (new Carbon())->format('Y-m-d H:i:s'); + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); + } } diff --git a/application/admin/model/car/Sales.php b/application/admin/model/car/Sales.php index 12dcf02..739ec9d 100644 --- a/application/admin/model/car/Sales.php +++ b/application/admin/model/car/Sales.php @@ -2,6 +2,8 @@ namespace app\admin\model\car; +use app\admin\model\Admin; +use app\admin\model\Cars; use think\Model; @@ -27,9 +29,19 @@ class Sales extends Model protected $append = [ ]; - - + public function saler() + { + return $this->belongsTo(Admin::class,'saler_id'); + } + + public function car() + { + return $this->belongsTo(Cars::class,'cart_id'); + } + + + diff --git a/application/admin/view/car/sales/add.html b/application/admin/view/car/sales/add.html index a976ea8..dc36c23 100644 --- a/application/admin/view/car/sales/add.html +++ b/application/admin/view/car/sales/add.html @@ -8,19 +8,25 @@ +
- +
-
@@ -28,33 +34,41 @@
- +
+ + {if condition="$car.car_type != 3"} +
+ +
+ +
+
+ {else /}
- +
- +
- +
- -
-
-
- -
- +
+ {/if} + + + +
@@ -73,12 +87,6 @@
-
- -
- -
-