|
<?php
namespace app\controller;
use think\App;
use think\facade\View;
use app\model\Order as OrderModel;
use app\model\OrderItem as OrderItemModel;
use app\model\OrderDetails as OrderDetailsModel;
use app\model\Department as DepartmentModel;
use think\facade\Db;
class Order 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()
{
return View::fetch();
}
public function lists($year=0, $month=0, $key="")
{
$map = [];
if ($year) {
$map[] = ['year','=', $year];
}
if ($key) {
$map[] = ['company_name','LIKE', '%'.$key.'%'];
}
if ($month) {
$map[] = ['month','=', $month];
}
$limit = $this->request->cookie('limit') ? : 50;
$list = $this->detailsModel->where($map)->order('id')->paginate([
'list_rows' => $limit,
'query' => $this->request->param()
]);
// 统计去除已取消合同
$map[] = ['status', '<>', -1];
$total_contract_money = $this->detailsModel->where($map)->sum('contract_money');
$total_payment = $this->detailsModel->where($map)->sum('payment');
$page = $list->render();
return View::fetch('',[
'year' => $year,
'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($year=0)
{
if ($this->request->isPost()) {
$param = $this->request->param();
// var_dump($param);
// exit;
try {
if ($param['id']) {
$order = $this->model->find(intval($param['id']));
} else {
$order = new OrderModel();
$order->create_time = time();
}
$order->no = $param['no'];
$order->contract_no = $param['contract_no'];
$order->contract_time = $param['contract_time'];
$order->company_name = $param['company_name'];
$order->department_name = $param['department_name'];
$order->seller_name = $param['seller_name'];
$order->consignor = $param['consignor'];
$order->fare = $param['fare'];
$order->agency = $param['agency'];
$order->remark = $param['remark'];
$order->save();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功', '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',
'no' => '',
'contract_no' => '',
'contract_time' => '',
'company_name' => '',
'department_name' => '',
'seller_name' => '',
'consignor' => '',
'fare' => '0.00',
'agency' => '0.00',
'remark' => '',
];
}
return View::fetch('',[
'data' => $data,
'year' => $year
]);
}
}
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);
$year = date('Y', strtotime($orderInfo->contract_time));
return View::fetch('',[
'list' => $list,
'order_id' => $order_id,
'orderInfo' => $orderInfo,
'year' => $year,
]);
}
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']) {
$order = $this->itemModel->find(intval($param['id']));
} else {
$order = new OrderItemModel();
}
$order->order_id = intval($param['order_id']);
$order->goods_category = $param['goods_category'];
$order->goods_name = $param['goods_name'];
$order->goods_price = $param['goods_price'];
$order->sales_amount = $param['sales_amount'];
$order->sales_price = $param['sales_price'];
$order->actual_amount = $param['actual_amount'];
$order->actual_price = $param['actual_price'];
$order->agency = $param['agency'];
$order->fare = $param['fare'];
$order->type = intval($param['type']);
$order->remark = $param['remark'];
$order->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]);
}
}
}
}
|