123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\admin\controller\cost;
- use app\admin\model\village\VillageHu;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use app\admin\model\cost\CostItem;
- use app\admin\model\cost\CostBill;
- /**
- * 生成费用账单记录
- *use app\admin\model\cost\CostBill;
- * @icon fa fa-circle-o
- */
- class CostAdd extends Backend
- {
-
- /**
- * CostAdd模型对象
- * @var \app\admin\model\Cost\CostAdd
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\cost\CostAdd;
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- //dump($params);exit;
- $start=strtotime($params['start_time']);
- $end=strtotime($params['end_time']);
- $get_is=$this->model->where(['property_id'=>$params['property_id'],'village_id'=>$params['village_id'],'item_id'=>$params['item_id']])
- ->whereRaw('(start_time <='.$start.' and end_time >='.$start.') or (start_time <='.$end.' and end_time >='.$end.')')
- ->find();
- if (!empty($get_is)){
- $this->error(__('这段时间的账单已经生成'));
- }
- $get_item=CostItem::where('id',$params['item_id'])->find();
- $year1 = date("Y",$start); // 时间1的年份
- $month1 = date("m",$start); // 时间1的月份
- $year2 = date("Y",$end); // 时间2的年份
- $month2 = date("m",$end); // 时间2的月份
- $mouth= ($year2 * 12 + $month2) - ($year1 * 12 + $month1) +1;
- if (is_int($mouth / $get_item['month'])) {
- $num=$mouth / $get_item['month'];
- } else {
- $this->error(__('您选择的账单生成日期不满足账单周期'));
- }
- $list_time['0']['starttime']=$start;
- $list_time['0']['endtime']= strtotime("+".($get_item['month']*1-1)." months", $start);
- for($i=1;$i<$num;$i++){
- $list_time[$i]['starttime']=strtotime("+1 months", $list_time[$i-1]['endtime']);
- $list_time[$i]['endtime']= strtotime("+".($get_item['month']*1-1)." months", $list_time[$i]['starttime']);
- }
- $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 . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- if (empty($get_item['dong_id'])){
- $get_hu_list=VillageHu::where('village_id',$params['village_id'])->field('id,big_area,village_id,dong_id,danyuan_id')->select();
- }else{
- $get_hu_list=VillageHu::where('village_id',$params['village_id'])->where(['dong_id'=>['in',explode(',',$get_item['dong_id'])]])->field('id,big_area,village_id,dong_id,danyuan_id')->select();
- }
- if ($get_item['item']=='1' || $get_item['item']=='2' || $get_item['item']=='3'){//如果是物业和生活垃圾处理费
- // <option value="1">元/每平方/月</option>
- // <option value="2">元/每户/年</option>
- // <option value="3">元/每户/月</option>
- if ($get_item['method']=='1'){//按照建筑面积
- // if ($get_item['danwei']=='1'){//按照月收费
- foreach ($get_hu_list as $k=>$v){
- $insert=[];
- $insert['order_number']=time().rand('1000','9999');
- if ($params['village_id']=='2'){
- $price=$v['big_area']*$get_item['price']*$get_item['month'];//价格*平方*周期round
- }else{
- $price=round($v['big_area']*$get_item['price']*$get_item['month']);//价格*平方*周期round
- }
- $insert['item_id']=$params['item_id'];
- $insert['village_id']=$v['village_id'];
- $insert['dong_id']=$v['dong_id'];
- $insert['danyuan_id']=$v['danyuan_id'];
- $insert['hu_id']=$v['id'];
- $insert['price']=$price;
- $insert['true_price']=$price;
- foreach ($list_time as $kn=>$vn){
- $insert['start_time']=$vn['starttime'];
- $insert['end_time']=$vn['endtime'];
- $insert['createtime']=$insert['updatetime']=time();
- CostBill::insert($insert);
- }
- }
- // }
- }elseif($get_item['method']=='2'){//按照户收费
- if ($get_item['danwei']=='3'){//按照月收费
- foreach ($get_hu_list as $k=>$v){
- $insert=[];
- $insert['order_number']=time().rand('1000','9999');
- $price=$get_item['price']*$get_item['month'];//价格*平方*周期
- $insert['item_id']=$params['item_id'];
- $insert['village_id']=$v['village_id'];
- $insert['dong_id']=$v['dong_id'];
- $insert['danyuan_id']=$v['danyuan_id'];
- $insert['hu_id']=$v['id'];
- $insert['price']=$price;
- $insert['true_price']=$price;
- foreach ($list_time as $kn=>$vn){
- $insert['start_time']=$vn['starttime'];
- $insert['end_time']=$vn['endtime'];
- $insert['createtime']=$insert['updatetime']=time();
- CostBill::insert($insert);
- }
- }
- }elseif($get_item['danwei']=='2'){
- foreach ($get_hu_list as $k=>$v){
- $insert=[];
- $insert['order_number']=time().rand('1000','9999');
- $price=$get_item['price']*$get_item['month']/12;//价格*平方*周期
- $insert['item_id']=$params['item_id'];
- $insert['village_id']=$v['village_id'];
- $insert['dong_id']=$v['dong_id'];
- $insert['danyuan_id']=$v['danyuan_id'];
- $insert['hu_id']=$v['id'];
- $insert['price']=$price;
- $insert['true_price']=$price;
- foreach ($list_time as $kn=>$vn){
- $insert['start_time']=$vn['starttime'];
- $insert['end_time']=$vn['endtime'];
- $insert['createtime']=$insert['updatetime']=time();
- CostBill::insert($insert);
- }
- }
- }
- }
- }
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- }
|