123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class BudgetDep extends Model
- {
- public function add(){ // 弃用
- $data = [
- 'items_id' => input('itemsId/d',0),
- 'money' => input('money/f',0),
- 'dep_id' => input('depId/d',0),
- 'org_id' => input('orgId/d',0),
- 'budget_id' => input('budgetId/d',0),
- 'intro' => input('intro','','trim')
- ];
- $userId = input('userId/d',0);
- $result = validate('BudgetDep')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('BudgetDep')->getError());
- }
- Db::startTrans();
- try{
- $companyId = Db::name('budget_items')
- ->where('id',$data['items_id'])
- ->value('company_id');
- $data['company_id'] = $companyId;
- $data['create_time'] = date('Y-m-d H:i:s');
- $data['update_time'] = date('Y-m-d H:i:s');
- $data['real_money'] = $data['money'];
- $data['confirm_money'] = $data['money'];
- $data['status'] = 1;
- $id = Db::name('budget_dep')->insertGetId($data);
- if(!$id){
- \exception('操作失败');
- }
- // 预算变化记录
- $d = [
- 'org_id' => $data['org_id'],
- 'user_id' => $userId,
- 'type' => 3,
- 'dep_id' => $data['dep_id'],
- 'money' => $data['money'],
- 'bus_id' => $id,
- 'content' => '添加预算'
- ];
- $ret = model('BudgetDepLog')->add($d);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败'.$e->getMessage());
- }
- return true;
- }
- // 确认实际预算
- public function confirmRealMoney(){
- $id = input('id/d',0);
- $real_money = input('realMoney/f',0);
- $orgId = input('orgId/d',0);
- $userId = input('userId/d',0);
- if($id <= 0||$real_money < 0){
- HelpHander::error('参数错误');
- }
- $info = Db::name('budget_dep')->where('id',$id)->find();
- if(!$info){
- HelpHander::error('记录不存在');
- }
- if($info['status'] != 0){
- HelpHander::error('已确认');
- }
- Db::startTrans();
- try{
- $ret = Db::name('budget_dep')->where('id',$id)->update([
- 'real_money' => $real_money,
- 'confirm_money' => $real_money,
- 'status' => 1,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 预算变化记录
- $d = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'type' => 0,
- 'dep_id' => $info['dep_id'],
- 'money' => $real_money,
- 'bus_id' => $id,
- 'content' => '预算确认'
- ];
- $ret = model('BudgetDepLog')->add($d);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function updateRealMoney(){
- $type = input('type/d',0);
- $money = input('money/d',0);
- $id = input('id/d');
- $userId = input('userId/d',0);
- if($id <= 0||!in_array($type,[0,1])||$money <= 0){
- HelpHander::error('参数错误');
- }
- $info = $this->info($id);
- if($info['status'] != 1){
- HelpHander::error('该状态无法调整金额');
- }
- if($type == 0 && $money > $info['real_money']){ // 减少
- HelpHander::error('减少的金额必须小于剩余金额');
- }
- Db::startTrans();
- try{
- $sjmoney = $money;
- if($type == 0){
- $sjmoney = -$money;
- }
- $ret = Db::name('budget_dep')->where('id',$id)->update([
- 'confirm_money' => $info['confirm_money'] + $sjmoney,
- 'real_money' => $info['real_money'] + $sjmoney,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- // 预算变化记录
- $d = [
- 'org_id' => $info['org_id'],
- 'user_id' => $userId,
- 'type' => $type == 0?2:3,
- 'dep_id' => $info['dep_id'],
- 'money' => $money,
- 'bus_id' => $id,
- 'content' => '预算修改'
- ];
- $ret = model('BudgetDepLog')->add($d);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- // 获取用户可用的部门
- public function getUserDep($userId,$orgId){
- $deps = model('Dep')->getUserDep($userId,$orgId,1);
- $depids = [];
- if($deps) {
- foreach ($deps as $k => $v) {
- $depids[] = $v['id'];
- }
- $map[] = ['bd.dep_id','in',$depids];
- }else{
- $map[] = ['bd.dep_id','=',0];
- }
- $map[] = ['bd.del','=',0];
- $map[] = ['bd.org_id','=',$orgId];
- $map[] = ['b.year','=',date('Y')];
- $map[] = ['bd.status','=',1];
- $lists = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->where($map)
- ->field('d.id,d.name')
- ->group('d.id')
- ->distinct(true)
- ->order('d.sorts asc')
- ->select();
- return $lists?$lists:[];
- }
- public function lists($page,$size,$type,$year,$status,$title,$budgetId,$companyId,$userId,$orgId){
- $map[] = ['bd.del','=',0];
- $map[] = ['bd.org_id','=',$orgId];
- if($status >= 0){
- $map[] = ['bd.status','=',$status];
- }
- if($year){
- $map[] = ['b.year','=',$year];
- }
- if($title){
- $map[] = ['d.name','like','%'.$title.'%'];
- }
- if($budgetId > 0){
- $map[] = ['bd.budget_id','=',$budgetId];
- }
- if($companyId > 0){
- $map[] = ['bd.company_id','=',$companyId];
- }
- if($type == 1){ // 本部门
- $deps = model('Dep')->getUserDep($userId,$orgId,1);
- $depids = [];
- if($deps) {
- foreach ($deps as $k => $v) {
- $depids[] = $v['id'];
- }
- $map[] = ['bd.dep_id','in',$depids];
- }else{
- $map[] = ['bd.dep_id','=',0];
- }
- }
- $lists = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->join('budget_items bi','bi.id = bd.items_id')
- ->where($map)
- ->field('bd.*,d.name as depName,b.title as budgetTitle,bi.title as itemsName')
- ->page($page,$size)
- ->order('b.year desc,bi.sorts asc,bd.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- // $lists[$k]['itemsName'] = Db::name('budget_items')->where('id',$v['items_id'])->value('title');
- $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title');
- }
- $total = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function lists2($page,$size,$year,$title,$userId,$orgId){
- $map[] = ['bd.del','=',0];
- $map[] = ['bd.org_id','=',$orgId];
- if($year){
- $map[] = ['b.year','=',$year];
- }
- if($title){
- $map[] = ['d.name','like','%'.$title.'%'];
- }
- $lists = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->where($map)
- ->field('bd.dep_id,bd.budget_id,d.name as depName,b.year')
- ->page($page,$size)
- ->group('bd.dep_id,bd.budget_id')
- ->order('b.year desc,bd.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['money1'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',1)
- ->sum('money');
- $lists[$k]['confirm_money1'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',1)
- ->sum('confirm_money');
- $lists[$k]['real_money1'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',1)
- ->sum('real_money');
- $lists[$k]['money2'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',2)
- ->sum('money');
- $lists[$k]['confirm_money2'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',2)
- ->sum('confirm_money');
- $lists[$k]['real_money2'] = Db::name('budget_dep')
- ->where('del',0)
- ->where('dep_id',$v['dep_id'])
- ->where('budget_id',$v['budget_id'])
- ->where('company_id',2)
- ->sum('real_money');
- }
- $total = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->group('bd.dep_id,bd.budget_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- public function alllists($userId,$orgId){
- $map[] = ['bd.del','=',0];
- $map[] = ['bd.status','=',1];
- $map[] = ['bd.org_id','=',$orgId];
- $deps = model('Dep')->getUserDep($userId,$orgId);
- $depids = [];
- if($deps) {
- foreach ($deps as $k => $v) {
- $depids[] = $v['id'];
- }
- $map[] = ['bd.dep_id','in',$depids];
- }else{
- $map[] = ['dep_id','=',0];
- }
- $lists = Db::name('budget_dep')
- ->alias('bd')
- ->join('dep d','d.id = bd.dep_id')
- ->join('budget b','b.id = bd.budget_id')
- ->join('budget_items bi','bi.id = bd.budget_id')
- ->where($map)
- ->field('bd.id,bi.title,d.title as depName')
- ->order('bd.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['title'] = $v['title'].'['.$v['depName'].']';
- }
- return $lists;
- }
- public function del($id){
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- }
|