From 25338e33f5a06b59741f35af56063d22085cd526 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Mon, 28 Mar 2022 07:23:53 +0000 Subject: [PATCH 1/2] * 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 * From 6bc82e81c11bbbfa3945b5e1f35039e746021e19 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Mon, 28 Mar 2022 09:19:14 +0000 Subject: [PATCH 2/2] * Fix bug #14512. --- module/program/control.php | 6 +-- module/program/model.php | 58 ++++++++--------------------- module/program/view/create.html.php | 2 +- 3 files changed, 19 insertions(+), 47 deletions(-) diff --git a/module/program/control.php b/module/program/control.php index 64bab2777e..72f7c663dc 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->getProgramBudgetLeft($parentProgram); + $this->view->budgetLeft = $this->program->getBudgetLeft($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->getProgramBudgetLeft($parentProgram) + (float)$program->budget; + $this->view->availableBudget = $this->program->getBudgetLeft($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->getProgramBudgetLeft($program); + $budgetLeft = $this->program->getBudgetLeft($program); echo number_format($budgetLeft, 2); } diff --git a/module/program/model.php b/module/program/model.php index 28b35d5ff2..49c9e07f15 100644 --- a/module/program/model.php +++ b/module/program/model.php @@ -623,7 +623,7 @@ class programModel extends model $program->budgetUnit = $parentProgram->budgetUnit; if(isset($program->budget)) { - $availableBudget = $this->getProgramBudgetLeft($parentProgram); + $availableBudget = $this->getBudgetLeft($parentProgram); if($program->budget > $availableBudget) dao::$errors['budget'] = $this->lang->program->beyondParentBudget; } @@ -724,7 +724,7 @@ class programModel extends model $program->budgetUnit = $parentProgram->budgetUnit; if($program->budget != 0) { - $availableBudget = $this->getProgramBudgetLeft($parentProgram); + $availableBudget = $this->getBudgetLeft($parentProgram); if($program->budget > $availableBudget + $oldProgram->budget) dao::$errors['budget'] = $this->lang->program->beyondParentBudget; } } @@ -1090,63 +1090,35 @@ class programModel extends model /** * Get budget left. * - * @param object $parentProgram + * @param int $parentProgram + * @param int $leftBudget * @access public * @return int */ - public function getBudgetLeft($parentProgram) + public function getBudgetLeft($parentProgram, $leftBudget = 0) { - if(empty($parentProgram)) return; + if(empty($parentProgram->id)) return; $childGrade = $parentProgram->grade + 1; $childSumBudget = $this->dao->select("sum(budget) as sumBudget")->from(TABLE_PROGRAM) ->where('path')->like("%{$parentProgram->id}%") ->andWhere('grade')->eq($childGrade) - ->andWhere('deleted')->eq(0) + ->andWhere('deleted')->eq('0') ->fetch('sumBudget'); - return (float)$parentProgram->budget - (float)$childSumBudget; - } + $leftBudget += (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) + if($parentProgram->budget == 0 and $parentProgram->parent) { - $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; - } + $parentParent = $this->getById($parentProgram->parent); + return $this->getBudgetLeft($parentParent, $leftBudget); + } + else + { + return $leftBudget; } - - return $budgetLeft > 0 ? $budgetLeft : 0; } - /** * Get program parent pairs * diff --git a/module/program/view/create.html.php b/module/program/view/create.html.php index 279a2349fd..2c5ea86d51 100644 --- a/module/program/view/create.html.php +++ b/module/program/view/create.html.php @@ -66,7 +66,7 @@ program->budget;?>
- budget != 0) ? 'placeholder=' . $lang->program->parentBudget . zget($lang->project->currencySymbol, $parentProgram->budgetUnit) . $budgetLeft : '';?> + program->parentBudget . zget($lang->project->currencySymbol, $parentProgram->budgetUnit) . $budgetLeft : '';?> budgetUnit);?>