From 25338e33f5a06b59741f35af56063d22085cd526 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Mon, 28 Mar 2022 07:23:53 +0000 Subject: [PATCH] * Fix bug #14512. --- module/program/control.php | 6 ++--- module/program/model.php | 48 ++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/module/program/control.php b/module/program/control.php index 72f7c663dc..64bab2777e 100644 --- a/module/program/control.php +++ b/module/program/control.php @@ -176,7 +176,7 @@ class program extends control $this->view->parents = $this->program->getParentPairs(); $this->view->programList = $this->program->getList(); $this->view->budgetUnitList = $this->project->getBudgetUnitList(); - $this->view->budgetLeft = $this->program->getBudgetLeft($parentProgram); + $this->view->budgetLeft = $this->program->getProgramBudgetLeft($parentProgram); $this->display(); } @@ -222,7 +222,7 @@ class program extends control $this->view->programList = $this->program->getList(); $this->view->budgetUnitList = $this->program->getBudgetUnitList(); $this->view->parentProgram = $parentProgram; - $this->view->availableBudget = $this->program->getBudgetLeft($parentProgram) + (float)$program->budget; + $this->view->availableBudget = $this->program->getProgramBudgetLeft($parentProgram) + (float)$program->budget; $this->display(); } @@ -675,7 +675,7 @@ class program extends control public function ajaxGetBudgetLeft($programID) { $program = $this->program->getByID($programID); - $budgetLeft = $this->program->getBudgetLeft($program); + $budgetLeft = $this->program->getProgramBudgetLeft($program); echo number_format($budgetLeft, 2); } diff --git a/module/program/model.php b/module/program/model.php index 7720a4b932..28b35d5ff2 100644 --- a/module/program/model.php +++ b/module/program/model.php @@ -621,9 +621,9 @@ class programModel extends model /* The budget of a child program cannot beyond the remaining budget of the parent program. */ $program->budgetUnit = $parentProgram->budgetUnit; - if(isset($program->budget) and $parentProgram->budget != 0) + if(isset($program->budget)) { - $availableBudget = $this->getBudgetLeft($parentProgram); + $availableBudget = $this->getProgramBudgetLeft($parentProgram); if($program->budget > $availableBudget) dao::$errors['budget'] = $this->lang->program->beyondParentBudget; } @@ -722,9 +722,9 @@ class programModel extends model /* The budget of a child program cannot beyond the remaining budget of the parent program. */ $program->budgetUnit = $parentProgram->budgetUnit; - if($program->budget != 0 and $parentProgram->budget != 0) + if($program->budget != 0) { - $availableBudget = $this->getBudgetLeft($parentProgram); + $availableBudget = $this->getProgramBudgetLeft($parentProgram); if($program->budget > $availableBudget + $oldProgram->budget) dao::$errors['budget'] = $this->lang->program->beyondParentBudget; } } @@ -1102,11 +1102,51 @@ class programModel extends model $childSumBudget = $this->dao->select("sum(budget) as sumBudget")->from(TABLE_PROGRAM) ->where('path')->like("%{$parentProgram->id}%") ->andWhere('grade')->eq($childGrade) + ->andWhere('deleted')->eq(0) ->fetch('sumBudget'); return (float)$parentProgram->budget - (float)$childSumBudget; } + /** + * Get program budget left. + * + * @param int $program + * @access public + * @return int + */ + public function getProgramBudgetLeft($program) + { + if(empty($program->id)) return; + + if($program->budget > 0) return $this->getBudgetLeft($program); + + $programList = array(); + $programs = $this->dao->select('*')->from(TABLE_PROGRAM) + ->where('id')->in($program->path) + ->andWhere('deleted')->eq(0) + ->andWhere('type')->eq('program') + ->orderBy('id_asc') + ->fetchAll(); + + foreach($programs as $id => $program) + { + $left = $this->getBudgetLeft($program); + if(isset($programs[$id + 1]) and $programs[$id + 1]->budget <= 0) + { + $programs[$id + 1]->budget = $left; + } + elseif(!isset($programs[$id + 1])) + { + $budgetLeft = $left; + break; + } + } + + return $budgetLeft > 0 ? $budgetLeft : 0; + } + + /** * Get program parent pairs *