CostAdd.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\admin\controller\cost;
  3. use app\admin\model\village\VillageHu;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\cost\CostItem;
  9. use app\admin\model\cost\CostBill;
  10. /**
  11. * 生成费用账单记录
  12. *use app\admin\model\cost\CostBill;
  13. * @icon fa fa-circle-o
  14. */
  15. class CostAdd extends Backend
  16. {
  17. /**
  18. * CostAdd模型对象
  19. * @var \app\admin\model\Cost\CostAdd
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\cost\CostAdd;
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 添加
  34. */
  35. public function add()
  36. {
  37. if ($this->request->isPost()) {
  38. $params = $this->request->post("row/a");
  39. if ($params) {
  40. $params = $this->preExcludeFields($params);
  41. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  42. $params[$this->dataLimitField] = $this->auth->id;
  43. }
  44. //dump($params);exit;
  45. $start=strtotime($params['start_time']);
  46. $end=strtotime($params['end_time']);
  47. $get_is=$this->model->where(['property_id'=>$params['property_id'],'village_id'=>$params['village_id'],'item_id'=>$params['item_id']])
  48. ->whereRaw('(start_time <='.$start.' and end_time >='.$start.') or (start_time <='.$end.' and end_time >='.$end.')')
  49. ->find();
  50. if (!empty($get_is)){
  51. $this->error(__('这段时间的账单已经生成'));
  52. }
  53. $get_item=CostItem::where('id',$params['item_id'])->find();
  54. $year1 = date("Y",$start); // 时间1的年份
  55. $month1 = date("m",$start); // 时间1的月份
  56. $year2 = date("Y",$end); // 时间2的年份
  57. $month2 = date("m",$end); // 时间2的月份
  58. $mouth= ($year2 * 12 + $month2) - ($year1 * 12 + $month1) +1;
  59. if (is_int($mouth / $get_item['month'])) {
  60. $num=$mouth / $get_item['month'];
  61. } else {
  62. $this->error(__('您选择的账单生成日期不满足账单周期'));
  63. }
  64. $list_time['0']['starttime']=$start;
  65. $list_time['0']['endtime']= strtotime("+".($get_item['month']*1-1)." months", $start);
  66. for($i=1;$i<$num;$i++){
  67. $list_time[$i]['starttime']=strtotime("+1 months", $list_time[$i-1]['endtime']);
  68. $list_time[$i]['endtime']= strtotime("+".($get_item['month']*1-1)." months", $list_time[$i]['starttime']);
  69. }
  70. $result = false;
  71. Db::startTrans();
  72. try {
  73. //是否采用模型验证
  74. if ($this->modelValidate) {
  75. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  76. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  77. $this->model->validateFailException(true)->validate($validate);
  78. }
  79. $result = $this->model->allowField(true)->save($params);
  80. if (empty($get_item['dong_id'])){
  81. $get_hu_list=VillageHu::where('village_id',$params['village_id'])->field('id,big_area,village_id,dong_id,danyuan_id')->select();
  82. }else{
  83. $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();
  84. }
  85. if ($get_item['item']=='1' || $get_item['item']=='2' || $get_item['item']=='3'){//如果是物业和生活垃圾处理费
  86. // <option value="1">元/每平方/月</option>
  87. // <option value="2">元/每户/年</option>
  88. // <option value="3">元/每户/月</option>
  89. if ($get_item['method']=='1'){//按照建筑面积
  90. // if ($get_item['danwei']=='1'){//按照月收费
  91. foreach ($get_hu_list as $k=>$v){
  92. $insert=[];
  93. $insert['order_number']=time().rand('1000','9999');
  94. if ($params['village_id']=='2'){
  95. $price=$v['big_area']*$get_item['price']*$get_item['month'];//价格*平方*周期round
  96. }else{
  97. $price=round($v['big_area']*$get_item['price']*$get_item['month']);//价格*平方*周期round
  98. }
  99. $insert['item_id']=$params['item_id'];
  100. $insert['village_id']=$v['village_id'];
  101. $insert['dong_id']=$v['dong_id'];
  102. $insert['danyuan_id']=$v['danyuan_id'];
  103. $insert['hu_id']=$v['id'];
  104. $insert['price']=$price;
  105. $insert['true_price']=$price;
  106. foreach ($list_time as $kn=>$vn){
  107. $insert['start_time']=$vn['starttime'];
  108. $insert['end_time']=$vn['endtime'];
  109. $insert['createtime']=$insert['updatetime']=time();
  110. CostBill::insert($insert);
  111. }
  112. }
  113. // }
  114. }elseif($get_item['method']=='2'){//按照户收费
  115. if ($get_item['danwei']=='3'){//按照月收费
  116. foreach ($get_hu_list as $k=>$v){
  117. $insert=[];
  118. $insert['order_number']=time().rand('1000','9999');
  119. $price=$get_item['price']*$get_item['month'];//价格*平方*周期
  120. $insert['item_id']=$params['item_id'];
  121. $insert['village_id']=$v['village_id'];
  122. $insert['dong_id']=$v['dong_id'];
  123. $insert['danyuan_id']=$v['danyuan_id'];
  124. $insert['hu_id']=$v['id'];
  125. $insert['price']=$price;
  126. $insert['true_price']=$price;
  127. foreach ($list_time as $kn=>$vn){
  128. $insert['start_time']=$vn['starttime'];
  129. $insert['end_time']=$vn['endtime'];
  130. $insert['createtime']=$insert['updatetime']=time();
  131. CostBill::insert($insert);
  132. }
  133. }
  134. }elseif($get_item['danwei']=='2'){
  135. foreach ($get_hu_list as $k=>$v){
  136. $insert=[];
  137. $insert['order_number']=time().rand('1000','9999');
  138. $price=$get_item['price']*$get_item['month']/12;//价格*平方*周期
  139. $insert['item_id']=$params['item_id'];
  140. $insert['village_id']=$v['village_id'];
  141. $insert['dong_id']=$v['dong_id'];
  142. $insert['danyuan_id']=$v['danyuan_id'];
  143. $insert['hu_id']=$v['id'];
  144. $insert['price']=$price;
  145. $insert['true_price']=$price;
  146. foreach ($list_time as $kn=>$vn){
  147. $insert['start_time']=$vn['starttime'];
  148. $insert['end_time']=$vn['endtime'];
  149. $insert['createtime']=$insert['updatetime']=time();
  150. CostBill::insert($insert);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. Db::commit();
  157. } catch (ValidateException $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. } catch (PDOException $e) {
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. } catch (Exception $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. }
  167. if ($result !== false) {
  168. $this->success();
  169. } else {
  170. $this->error(__('No rows were inserted'));
  171. }
  172. }
  173. $this->error(__('Parameter %s can not be empty', ''));
  174. }
  175. return $this->view->fetch();
  176. }
  177. }