提交 a70cbfe9 authored 作者: chengye's avatar chengye 提交者: wangkr

完成update 方法

上级 cc440ac8
......@@ -8,6 +8,7 @@ use app\em\model\Staff;
use app\em\model\Station;
use app\em\model\UseageDetail;
use app\em\validate\UseageGeneralInfoValidate;
use think\Db;
use think\facade\Request;
use think\facade\Session;
......@@ -118,9 +119,11 @@ class Useage extends Base
}
//保存抄表单
public function store(){
public function store($useage_id = ''){
//获取表单数据
$data = Request::post();
$action = '新增';
//数据验证
$validate = new UseageGeneralInfoValidate();
//根据计量模式 验证去表单数据
......@@ -129,25 +132,46 @@ class Useage extends Base
}
//获取电表基本信息
$em = (new \app\em\model\Em)->getEmInfoById(['id'=>$data['em_id']],'rule');
//保存抄表单
if(empty($useage_id)) {
$uModel = new \app\em\model\Useage();
//生成抄表单ID
$data['useage_id'] = $this->getUId();
$data['last_sum_numb'] = $data['last_numb'];
$data['current_sum_numb'] = $data['current_numb'];
//保存用户ID
$uModel->useage_id = $this->getUId();
$user = Session::get('user');
$data['uid'] = $user['user_id'];
$uModel->uid = $user['user_id'];
$uModel->last_sum_numb = $data['last_numb'];
$uModel->last_date = $data['last_date'];
$uModel->em_id = $data['em_id'];
//峰谷类型
$data['type'] = $data['pricing_type'];
$uModel->type = $data['pricing_type'];
}
else {
$this->checkEnd($useage_id);
$action = '修改';
$uModel = (new \app\em\model\Useage())->get(['useage_id' => $useage_id]);
}
//更新
$uModel->staff_id = $data['staff_id'];
$uModel->current_date = $data['current_date'];
$uModel->current_sum_numb = $data['current_numb'];
$uModel->photo_id = $data['photo_id'];
//单价
$data['price'] = $data['pricing_type'] == 1 ? $em->rule->pay_price : $data['price'];
$uModel->save($data); //保存抄表记录
$uModel->price = $data['pricing_type'] == 1 ? $em->rule->pay_price : $data['price'];
$uModel->save(); //保存抄表记录
$data['type'] = $data['type'] == 1 ? 5 : 6;
if(empty($useage_id)){
$udModel = new UseageDetail(); //抄表详情模型
$udModel->save($data);
return json(['status'=>1,'message'=>'添加抄表记录成功']);
$udModel->type = $data['type'] == 1 ? 5 : 6;
$uModel->current_numb = $data['current_numb'];
$uModel->last_numb = $data['last_numb'];
} else {
$udModel = (new UseageDetail())->get(['useage_id' => $useage_id]);; //抄表详情模型
}
$udModel->current_numb = $data['current_numb'];
$udModel->price = $uModel->price;
$udModel->save();
return json(['status'=>1,'message'=>$action.'抄表记录成功']);
}
/**
......@@ -210,30 +234,48 @@ class Useage extends Base
$useage->repo_numb = round(round($useage->current_sum_numb - $useage->last_sum_numb,2) * $useage->rate,2);
$this->assign('em',$em);
$this->assign('useage',$useage);
return $this->fetch('detail');
}
public function del(){
$uid = Request::param('uid');
$useage_id = Request::param('uid');
$this->checkEnd($useage_id);
$useage = (new \app\em\model\Useage())->get(['useage_id'=>$useage_id]);
$useage_details = (new UseageDetail())->select(['useage_id'=>$useage_id]);
$useage = (new \app\em\model\Useage())->get(['useage_id'=>$uid]);
$useage_details = (new UseageDetail())->select(['useage_id'=>$uid]);
if(!$useage){
return json(['status'=>0,'message'=>"删除异常,未找到单据编号,请联系管理员"]);
}
if($useage->delete()){
if(!$useage_details->isEmpty()){
foreach ($useage_details as $k => $v){
unset($useage_details[$k]);
}
}
Db::startTrans();
try{
$useage->delete();
$useage_details->delete();
Db::commit();
return json(['status'=>1,'message'=>"删除单据成功"]);
} {
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return json(['status'=>0,'message'=>"删除异常,单据删除失败,请联系管理员"]);
}
}
/**
* 判断useage_id是否是em_id的最后一条记录
* @param $useage_id
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function checkEnd($useage_id){
$uModel = (new \app\em\model\Useage())->get(['useage_id'=>$useage_id]);
if(!$uModel) return json(['status'=>0,'message'=>"不存在的抄表单号,请联系管理员"]);
if($uModel->status == 2) return json(['status'=>0,'message'=>"已审核过的单据无法编辑,请联系管理员"]);
//找出电表最后一条数据
$lastModel = (new \app\em\model\Useage())->where('em_id','=',$uModel->em_id)
->order('id desc')->find();
if($lastModel->status == 2) return json(['status'=>0,'message'=>"单号不是最新的抄表记录且最新的抄表记录已经审核无法修改,请联系管理员"]);
if($uModel->id != $lastModel->id){
return json(['status'=>0,'message'=>"抄表单号不是最新的抄表记录,无法修改,请联系管理员"]);
}
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论