tp6框架的销售统计系统

SaleOrder.php 5.4KB

    <?php namespace app\controller; use think\App; use think\facade\View; use app\model\SaleOrder as OrderModel; use app\model\SaleOrderItem as OrderItemModel; use app\model\SaleOrderDetails as OrderDetailsModel; use app\model\SaleDepartment as DepartmentModel; use think\facade\Db; class SaleOrder extends Base { protected $model; protected $itemModel; protected $detailsModel; public function __construct(App $app) { parent::__construct($app); $this->model = new OrderModel(); $this->itemModel = new OrderItemModel(); $this->detailsModel = new OrderDetailsModel(); } public function index($month=0, $key="") { $map = []; if ($key) { $map[] = ['company_name','LIKE', '%'.$key.'%']; } if ($month) { $map[] = ['month','=', $month]; } $limit = $this->request->cookie('limit') ? : 50; $list = $this->detailsModel->where($map)->paginate([ 'list_rows' => $limit, 'query' => $this->request->param() ]); // 未取消合同统计 $map[] = ['status', '=', 0]; $total_contract_money = $this->detailsModel->where($map)->sum('contract_money'); $total_payment = $this->detailsModel->where($map)->sum('payment'); $page = $list->render(); return View::fetch('',[ 'list' => $list, 'page' => $page, 'key' => $key, 'month' => $month, 'limit' => $limit, 'total_contract_money' => round($total_contract_money, 2), 'total_payment' => round($total_payment, 2) ]); } public function save() { $departmentModel = new DepartmentModel(); $departments = $departmentModel->column('name', 'id'); if ($this->request->isPost()) { $param = $this->request->param(); try { if ($param['id']) { $seller = $this->model->find(intval($param['id'])); } else { $seller = new OrderModel(); $seller->create_time = time(); } $seller->name = $param['name']; $seller->department_id = $param['department_id']; $seller->department_name = $departments[$param['department_id']]; $seller->save(); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('操作成功', 'sale_seller/index'); } else { $id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0; if ($id) { $data = $this->model->find($id); } else { $data = [ 'id' => 0, 'name' => '', 'department_id' => 1, ]; } return View::fetch('', [ 'data' => $data, 'departments' => $departments ]); } } public function delete($id) { if ($this->model->destroy($id)) { return json(['code'=>2]); } else { return json(['code'=>0]); } } public function item($order_id) { $list = $this->itemModel->where('order_id', $order_id)->select(); $orderInfo = $this->model->field('id,no,contract_no,company_name,contract_time')->find($order_id); return View::fetch('',[ 'list' => $list, 'order_id' => $order_id, 'orderInfo' => $orderInfo ]); } public function itemInfo($id) { $data = $this->itemModel->find($id); if ($this->request->isAjax()) { return json(['code'=>2, 'data'=>$data]); } } public function itemSave() { if ($this->request->isAjax()) { $param = $this->request->param(); // var_dump($param); // exit; try { if ($param['id']) { $seller = $this->itemModel->find(intval($param['id'])); } else { $seller = new OrderItemModel(); } $seller->order_id = intval($param['order_id']); $seller->goods_category = $param['goods_category']; $seller->goods_name = $param['goods_name']; $seller->goods_price = $param['goods_price']; $seller->sales_amount = $param['sales_amount']; $seller->sales_price = $param['sales_price']; $seller->actual_amount = $param['actual_amount']; $seller->actual_price = $param['actual_price']; $seller->agency = $param['agency']; $seller->fare = $param['fare']; $seller->type = intval($param['type']); $seller->remark = $param['remark']; $seller->save(); } catch (\Exception $e) { // $this->error($e->getMessage()); return json(['code'=>0, 'msg'=>$e->getMessage()]); } return json(['code'=>2]); } } public function itemDelete($id) { if ($this->request->isPost()) { if ($this->itemModel->destroy($id)) { return json(['code'=>2]); } else { return json(['code'=>0]); } } } }