123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- use think\response\Json;
- class BudgetApply extends Model
- {
- public function advancedStartBudgetApply($id,$orgId,$userId,$formJson,$extra){
- $formJson = json_decode($formJson,true);
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'apply_id' => $id,
- 'dep_id' => $extra['depId'],
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ];
- $values = [];
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddbudgetfield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $values = json_decode($val['values'],true);
- break;
- }
- }
- break;
- }
- }
- $items = $values['items'];
- $data['budget_id'] = $items[0]['budgetId'];
- $data['company_id'] = $items[0]['companyId'];
- $ret = Db::name('budget_apply')->insert($data);
- return $ret?true:false;
- }
- public function advancedEndBudgetApply($id,$orgId,$userId,$formJson){
- // 生成部门预算记录
- $values = [];
- $formJson = json_decode($formJson,true);
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddbudgetfield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $values = json_decode($val['values'],true);
- break;
- }
- }
- break;
- }
- }
- $items = $values['items'];
- $apply = Db::name('budget_apply')->where('apply_id',$id)->find();
- $curTime = date('Y-m-d H:i:s');
- $depTotal = Db::name('budget_dep_total')
- ->where('dep_id',$apply['dep_id'])
- ->where('org_id',$orgId)
- ->where('budget_id',$apply['budget_id'])
- ->where('company_id',$apply['company_id'])
- ->find();
- if(!$depTotal){
- $ret = Db::name('budget_dep_total')->insertGetId([
- 'org_id' => $orgId,
- 'dep_id' => $apply['dep_id'],
- 'budget_id' => $apply['budget_id'],
- 'company_id' => $apply['company_id'],
- 'create_time' => $curTime
- ]);
- if(!$ret){
- return false;
- }
- }
- foreach ($items as $k=>$v){
- $sdata = [
- 'org_id' => $orgId,
- 'dep_id' => $apply['dep_id'],
- 'budget_id' => $apply['budget_id'],
- 'company_id' => $apply['company_id'],
- 'items_id' => $v['id'],
- 'money' => floatval($v['money']),
- 'money2' => !empty($v['money2'])?floatval($v['money2']):0,
- 'money3' => !empty($v['money3'])?floatval($v['money3']):0,
- 'intro' => !empty($v['intro'])?$v['intro']:'',
- 'create_time' => $curTime
- ];
- $info = Db::name('budget_dep')
- ->where('dep_id',$apply['dep_id'])
- ->where('org_id',$orgId)
- ->where('del',0)
- ->where('budget_id',$apply['budget_id'])
- ->where('company_id',$apply['company_id'])
- ->where('items_id',$v['id'])
- ->find();
- if($info){ // 存在金额累加
- $res = Db::name('budget_dep')->where('id',$info['id'])->update([
- 'money' => $sdata['money'] + $info['money'],
- 'money2' => $sdata['money2'] + $info['money2'],
- 'money3' => $sdata['money3'] + $info['money3'],
- 'update_time' => $curTime
- ]);
- if(!$res){
- return false;
- }
- }else{ // 不存在新增
- $res = Db::name('budget_dep')->insert($sdata);
- if(!$res){
- return false;
- }
- }
- }
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1
- ];
- $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
- return $ret?true:false;
- }
- public function advancedDisagreeBudgetApply($id){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
- return $ret?true:false;
- }
- public function advancedDelBudgetApply($id){
- $info = Db::name('budget_apply')->where('apply_id',$id)->find();
- $curTime = date('Y-m-d H:i:s');
- $data = [
- 'update_time' => $curTime,
- 'status' => 2
- ];
- $ret = Db::name('budget_apply')->where('apply_id',$id)->update($data);
- if($ret && $info['status'] == 1){
- $formJson = Db::name('apply')->where('id',$info['apply_id'])->value('form_json');
- // 删除生成的部门预算记录
- $values = [];
- $formJson = json_decode($formJson,true);
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddbudgetfield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $values = json_decode($val['values'],true);
- break;
- }
- }
- break;
- }
- }
- $items = $values['items'];
- foreach ($items as $k=>$v){
- $money = floatval($v['money']);
- $money2 = !empty($v['money2'])?floatval($v['money2']):0;
- $money3 = !empty($v['money3'])?floatval($v['money3']):0;
- //检查是否存在以前人的预算,与确认的无法删除
- $info = Db::name('budget_dep')
- ->where('dep_id',$info['dep_id'])
- ->where('org_id',$info['org_id'])
- ->where('del',0)
- ->where('budget_id',$info['budget_id'])
- ->where('company_id',$info['company_id'])
- ->where('items_id',$v['id'])
- ->find();
- if($info){ // 存在金额累加
- if($info['status'] == 1){
- $this->error = "预算项已确认,无法删除";
- return false;
- }
- $res = Db::name('budget_dep')->where('id',$info['id'])->update([
- 'money' => $info['money'] - $money,
- 'money2' => $info['money2'] - $money2,
- 'money3' => $info['money3'] - $money3,
- 'update_time' => $curTime
- ]);
- if(!$res){
- $this->error = "操作失败";
- return false;
- }
- }
- }
- }
- if(!$ret){
- $this->error = "操作失败";
- }
- return $ret?true:false;
- }
- public function lists($page,$size,$budgetId,$companyId,$orgId){
- $map[] = ['status','=',1];
- $map[] = ['org_id','=',$orgId];
- if($budgetId > 0){
- $map[] = ['budget_id','=',$budgetId];
- }
- if($companyId > 0){
- $map[] = ['company_id','=',$companyId];
- }
- $lists = Db::name('budget_apply')
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
- $lists[$k]['budgetName'] = Db::name('budget')->where('id',$v['budget_id'])->value('title');
- $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- $lists[$k]['companyName'] = Db::name('company')->where('id',$v['company_id'])->value('title');
- }
- $total = Db::name('budget_apply')->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- }
|