123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class FinanceLog extends Model
- {
- public function add(){ // 弃用
- $data = [
- 'id' => input('id/d',0),
- 'income' => input('income/f',0),
- 'expenditure' => input('expenditure/f',0),
- 'day' => input('day','','trim'),
- 'org_id' => input('orgId/d',0),
- ];
- $result = validate('FinanceLog')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('FinanceLog')->getError());
- }
- $data['ym'] = date('Ym',strtotime($data['day']));
- $data['y'] = date('Y',strtotime($data['day']));
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- public function lists($page,$size,$day,$orgId){
- $map[] = ['org_id','=',$orgId];
- if($day){
- $map[] = ['day','=',$day];
- }
- $lists = Db::name('finance_log')
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $total = Db::name('finance_log')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- public function del($id){
- $ret = $this->where('id',$id)->delete();
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- /**
- * 根据性别获取在职人数
- * @param $type 1=当日 2=月 3=年
- * @param $cate 1=收入 2=支出
- * @return float|string
- */
- public function financeStructure($orgId,$type,$cate){
- $map[] = ['org_id','=',$orgId];
- if($type == 1){
- $map[] = ['day','=',date('Y-m-d')];
- }else if($type == 2){
- $map[] = ['ym','=',date('Ym')];
- }else if($type == 3){
- $map[] = ['y','=',date('Y')];
- }
- if($cate == 1){
- $field = 'income';
- }else{
- $field = 'expenditure';
- }
- $total = Db::name('finance_log')
- ->where($map)
- ->sum($field);
- return $total;
- }
- }
|