瀏覽代碼

updated ZaliczkiNajemcow

Piotr Labudda 10 年之前
父節點
當前提交
a98ff4d457
共有 1 個文件被更改,包括 244 次插入71 次删除
  1. 244 71
      SE/se-lib/Route/ZaliczkiNajemcow.php

+ 244 - 71
SE/se-lib/Route/ZaliczkiNajemcow.php

@@ -154,6 +154,25 @@ jQuery(document).ready(function () {
 		}
 	}
 
+	public function zaliczkaAction() {
+		SE_Layout::gora();
+		//SE_Layout::menu();
+		try {
+			$id = V::get('nrZaliczki', 0, $_REQUEST, 'int');
+			if (!$id) throw new Exception("Wrong param id");
+			$zaliczka = $this->findZaliczkaById($id);
+			$this->validateRowZaliczka($zaliczka);
+			$lastCountCostDate = $this->getLastCountMonthCost($zaliczka);
+			if (null === $lastCountCostDate) {
+				$zaliczka = $this->recountMonthCost($zaliczka, $zaliczka['A_LAST_COST_COUNT_ID_GROUP']);
+			}
+			$this->zaliczka($zaliczka);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
 	public function zaliczkiAction() {
 		$selectedYear = V::get('year', '', $_REQUEST);
 		$selectedNajemca = V::get('najemca', '', $_REQUEST);
@@ -163,7 +182,16 @@ jQuery(document).ready(function () {
 		try {
 			$this->menu($selectedYear, $selectedNajemca, $selectedCostGroup);
 			if (!empty($selectedYear)) {
-				$this->zaliczki($selectedYear, $selectedNajemca, $selectedCostGroup);
+				if (empty($selectedCostGroup)) throw new Exception("Wybierz grupę");
+				$groups = array($selectedCostGroup);
+
+				$zaliczka = $this->findZaliczkaByNameYear($selectedNajemca, $selectedYear);
+				$this->validateRowZaliczka($zaliczka);
+				$lastCountCostDate = $this->getLastCountMonthCost($zaliczka);
+				if (null === $lastCountCostDate || $selectedCostGroup != $zaliczka['A_LAST_COST_COUNT_ID_GROUP']) {
+					$zaliczka = $this->recountMonthCost($zaliczka, $selectedCostGroup);
+				}
+				$this->zaliczka($zaliczka);
 			}
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
@@ -171,74 +199,20 @@ jQuery(document).ready(function () {
 		SE_Layout::dol();
 	}
 
-	public function zaliczki($selectedYear = null, $selectedNajemca = null, $selectedCostGroup = null) {
-		if (empty($selectedCostGroup)) throw new Exception("Wybierz grupę");
-		$groups = array($selectedCostGroup);
-
-		$zaliczka = null;
-		{
-			$pdo = DB::getPDO();
-			$sqlRok = $pdo->quote($selectedYear, PDO::PARAM_STR);
-			$sqlNajemca = $pdo->quote($selectedNajemca, PDO::PARAM_STR);
-			$zaliczki = $pdo->fetchAll("
-				select z.*
-				from ZALICZKI_NAJEMCOW z
-				where z.ROK = {$sqlRok}
-					and z.NAJEMCA = {$sqlNajemca}
-			");
-			if (empty($zaliczki)) throw new Exception("Brak danych dla wybrancyh kryteriów");
-			$zaliczka = reset($zaliczki);
-			//DBG::_(true, true, "zaliczka", $zaliczka, __CLASS__, __FUNCTION__, __LINE__);
-			// TODO: validate $zaliczka
-			if (empty($zaliczka['KOSZT_OD'])) throw new Exception("Brak KOSZT_OD");
-			if (empty($zaliczka['KOSZT_DO'])) throw new Exception("Brak KOSZT_DO");
-			if ($zaliczka['KOSZT_OD'] > $zaliczka['KOSZT_DO']) throw new Exception("KOSZT_OD nie może być później niż KOSZT_DO");
-			if (empty($zaliczka['POW_CALK'])) throw new Exception("Brak POW_CALK");
-			if (empty($zaliczka['POW_LOKALU'])) throw new Exception("Brak POW_LOKALU");
-		}
-
-		//DBG::_(true, true, "groups", $groups, __CLASS__, __FUNCTION__, __LINE__);
-		$budget = Router::getRoute('Budget');
-		//$budget->fetchDataByYear($selectedYear, $groups);
-		{
-			$budget->_fetchCostsByYear($selectedYear);
-			//$budget->_fetchPlanByYear($year);
-			$budget->_fetchProjectInfo();
-			$budget->_buildProjectTree();
-			$budget->_reacountCostsFromKoresp();
-			$budget->_filterProjectsByGroups($groups);
-		}
-		{
-			$sumCostByMonth = array(); for ($month = 1; $month <= 12; $month++) $sumCostByMonth[$month] = 0;
-			foreach ($budget->_projectInfo as $idProject => $projectInfo) {
-				if (!$projectInfo->hasAccess) continue;
-				if ($projectInfo->filteredByGroups) continue;
-				for ($month = 1; $month <= 12; $month++) {
-					$cost = $budget->getCost($idProject, $month);
-					if (!$cost) continue;
-					$sumCostByMonth[$month] += $cost->COST_SELF;
-				}
-			}
-		}
-		//DBG::_(true, true, "budget", $budget, __CLASS__, __FUNCTION__, __LINE__);
-		$kosztOd = new DateTime($zaliczka['KOSZT_OD']);
-		$kosztDo = new DateTime($zaliczka['KOSZT_DO']);
-		$daysBilled = 1 + $kosztDo->diff($kosztOd)->format("%a");
-		//DBG::_(true, true, "daysBilled", $daysBilled, __CLASS__, __FUNCTION__, __LINE__);
-
-		$localCostByMonth = array(); for ($month = 1; $month <= 12; $month++) {
-			$localCostByMonth[$month] = ($zaliczka['POW_LOKALU'] / $zaliczka['POW_CALK']) * $sumCostByMonth[$month] * $daysBilled / 365;
-		}
-		$localCostTotal = 0; for($month = 1; $month <= 12; $month++) $localCostTotal += $localCostByMonth[$month];
+	public function zaliczka($zaliczka) {//$selectedYear = null, $selectedNajemca = null, $selectedCostGroup = null) {
 		$incomeByMonth = array();
+		$localCostByMonth = array();
 		for ($month = 1; $month <= 12; $month++) {
 			$fldMonth = sprintf("%02d", $month);
 			$incomeByMonth[$month] = $zaliczka["m{$fldMonth}"];
+			$localCostByMonth[$month] = $zaliczka["c{$fldMonth}"];
 		}
-		$total = 0; for($month = 1; $month <= 12; $month++) $total += $incomeByMonth[$month];
+		$localCostTotal = 0; for($month = 1; $month <= 12; $month++) $localCostTotal += $localCostByMonth[$month];
+
+		$incomeTotal = 0; for($month = 1; $month <= 12; $month++) $incomeTotal += $incomeByMonth[$month];
 		$diff = array(); for($month = 1; $month <= 12; $month++) $diff[$month] = $localCostByMonth[$month] - $incomeByMonth[$month];
 		$sumTotalDiff = 0; for($month = 1; $month <= 12; $month++) $sumTotalDiff += $diff[$month];
-		$payTotal = -1 * ($total + $localCostTotal);
+		$payTotal = -1 * ($incomeTotal + $localCostTotal);
 		?>
 <div class="container">
 	<div class="row">
@@ -278,8 +252,8 @@ jQuery(document).ready(function () {
 			<?php for($month = 1; $month <= 12; $month++) : ?>
 				<td class="success"><?php echo number_format($incomeByMonth[$month], 2, '.', ','); ?></td>
 			<?php endfor; ?>
-			<th><?php echo number_format($total, 2, '.', ','); ?></th>
-			<th><?php echo number_format($total / 12 / $zaliczka['POW_LOKALU'], 2, '.', ','); ?></th>
+			<th><?php echo number_format($incomeTotal, 2, '.', ','); ?></th>
+			<th><?php echo number_format($incomeTotal / 12 / $zaliczka['POW_LOKALU'], 2, '.', ','); ?></th>
 		</tr>
 		<tr>
 			<th style="text-align:right">Koszty przypadające na lokal</th>
@@ -309,6 +283,143 @@ jQuery(document).ready(function () {
 <?php
 	}
 
+	public function recountMonthCostAction() {
+		SE_Layout::gora();
+		//SE_Layout::menu();
+		try {
+			$id = V::get('nrZaliczki', 0, $_REQUEST, 'int');
+			if (!$id) throw new Exception("Wrong param id");
+			$zaliczka = $this->findZaliczkaById($id);
+			if (!$zaliczka['A_LAST_COST_COUNT_ID_GROUP']) throw new Exception("Group not set");
+			$zaliczka = $this->recountMonthCost($zaliczka, $zaliczka['A_LAST_COST_COUNT_ID_GROUP']);
+			DBG::_(true, true, "zaliczka", $zaliczka, __CLASS__, __FUNCTION__, __LINE__);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function recountMonthCost($zaliczka, $idGroup) {
+		$idGroup = (int)$idGroup;
+		if (!$idGroup) throw new Exception("Podano niepoprawną grupę");
+		if ('TAK' != $zaliczka['CZY_ROZLICZENIE']) {
+			return $zaliczka;
+		}
+		$budget = Router::getRoute('Budget');
+		$groups = array($idGroup);
+		//$budget->fetchDataByYear($selectedYear, $groups);
+		{
+			$budget->_fetchCostsByYear($zaliczka['ROK']);
+			//$budget->_fetchPlanByYear($year);
+			$budget->_fetchProjectInfo();
+			$budget->_buildProjectTree();
+			$budget->_reacountCostsFromKoresp();
+			$budget->_filterProjectsByGroups($groups);
+		}
+		{
+			$sumCostByMonth = array(); for ($month = 1; $month <= 12; $month++) $sumCostByMonth[$month] = 0;
+			foreach ($budget->_projectInfo as $idProject => $projectInfo) {
+				if (!$projectInfo->hasAccess) continue;
+				if ($projectInfo->filteredByGroups) continue;
+				for ($month = 1; $month <= 12; $month++) {
+					$cost = $budget->getCost($idProject, $month);
+					if (!$cost) continue;
+					$sumCostByMonth[$month] += $cost->COST_SELF;
+				}
+			}
+		}
+		DBG::_('DBG', '>1', "sumCostByMonth", $sumCostByMonth, __CLASS__, __FUNCTION__, __LINE__);
+		{// only months between KOSZT_OD and KOSZT_DO
+			$strMonthFrom = substr($zaliczka['KOSZT_OD'], 0, 7);
+			$strMonthTo   = substr($zaliczka['KOSZT_DO'], 0, 7);
+			for ($month = 1; $month <= 12; $month++) {
+				$strMonth = "{$zaliczka['ROK']}-" . sprintf("%02d", $month);
+				if ($strMonth < $strMonthFrom) $sumCostByMonth[$month] = 0;
+				if ($strMonth > $strMonthTo)   $sumCostByMonth[$month] = 0;
+			}
+		}
+		DBG::_('DBG', '>1', "sumCostByMonth", $sumCostByMonth, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>2', "budget", $budget, __CLASS__, __FUNCTION__, __LINE__);
+		$kosztOd = new DateTime($zaliczka['KOSZT_OD']);
+		$kosztDo = new DateTime($zaliczka['KOSZT_DO']);
+		$daysBilled = 1 + $kosztDo->diff($kosztOd)->format("%a");
+		if ($daysBilled < 1) throw new Exception("Niepoprawne wartości w polach KOSZT_OD i KOSZT_DO");
+		DBG::_('DBG', '>1', "daysBilled", $daysBilled, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "zaliczka", $zaliczka, __CLASS__, __FUNCTION__, __LINE__);
+
+		$localCostByMonth = array();
+		for ($month = 1; $month <= 12; $month++) {
+			$localCostByMonth[$month] = ($zaliczka['POW_LOKALU'] / $zaliczka['POW_CALK']) * $sumCostByMonth[$month] * $daysBilled / 365;
+		}
+
+		$zaliczkaPatch = array();
+		$zaliczkaPatch['ID'] = $zaliczka['ID'];
+		$zaliczkaPatch['A_LAST_COST_COUNT_ID_GROUP'] = $idGroup;
+		$zaliczkaPatch['A_LAST_COST_COUNT_DATE'] = 'NOW()';
+		for ($month = 1; $month <= 12; $month++) {
+			$fldMonth = sprintf("%02d", $month);
+			$zaliczkaPatch["c{$fldMonth}"] = $localCostByMonth[$month];
+		}
+		DBG::_('DBG', '>1', "zaliczkaPatch", $zaliczkaPatch, __CLASS__, __FUNCTION__, __LINE__);
+
+		$tblAcl = User::getAcl()->getObjectAcl('default_db', 'ZALICZKI_NAJEMCOW');
+		//DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
+		$ds = $tblAcl->getDataSource();
+		//DBG::_(true, true, "ds", $ds, __CLASS__, __FUNCTION__, __LINE__);
+		$ds->updateItem($zaliczkaPatch);
+
+		return $this->findZaliczkaById($zaliczka['ID']);
+	}
+
+	public function getLastCountMonthCost($zaliczka) {
+		return $zaliczka['A_LAST_COST_COUNT_DATE'];
+	}
+
+	public function findZaliczkaByNameYear($najemca, $rok) {
+		$pdo = DB::getPDO();
+		if (empty($najemca)) throw new Exception("Nie podano najemcy");
+		if (empty($rok)) throw new Exception("Nie podano roku");
+		if (!is_numeric($rok) || 4 != strlen($rok)) throw new Exception("Podano nieprawidłowy rok");
+		$sth = $pdo->prepare("
+			select z.*
+			from ZALICZKI_NAJEMCOW z
+			where z.ROK = :rok
+				and z.NAJEMCA = :najemca
+		");
+		$sth->bindValue(':rok', $rok, PDO::PARAM_STR);
+		$sth->bindValue(':najemca', $najemca, PDO::PARAM_STR);
+		$sth->execute();
+		$zaliczka = $sth->fetch();
+		if (!$zaliczka) throw new Exception("Brak danych dla wybrancyh kryteriów");
+		return $zaliczka;
+	}
+
+	public function findZaliczkaById($id) {
+		$pdo = DB::getPDO();
+		$id = (int)$id;
+		if (!$id) throw new Exception("Podano nieprawidłowy nr rekordu");
+		$sth = $pdo->prepare("
+			select z.*
+			from ZALICZKI_NAJEMCOW z
+			where z.ID = :id
+		");
+		$sth->bindValue(':id', $id, PDO::PARAM_INT);
+		$sth->execute();
+		$zaliczka = $sth->fetch();
+		if (!$zaliczka) throw new Exception("Brak danych dla wybrancyh kryteriów");
+		return $zaliczka;
+	}
+
+	public function validateRowZaliczka($zaliczka) {
+		//DBG::_(true, true, "zaliczka", $zaliczka, __CLASS__, __FUNCTION__, __LINE__);
+		// TODO: validate $zaliczka
+		if (empty($zaliczka['KOSZT_OD'])) throw new Exception("Brak KOSZT_OD");
+		if (empty($zaliczka['KOSZT_DO'])) throw new Exception("Brak KOSZT_DO");
+		if ($zaliczka['KOSZT_OD'] > $zaliczka['KOSZT_DO']) throw new Exception("KOSZT_OD nie może być później niż KOSZT_DO");
+		if (empty($zaliczka['POW_CALK'])) throw new Exception("Brak POW_CALK");
+		if (empty($zaliczka['POW_LOKALU'])) throw new Exception("Brak POW_LOKALU");
+	}
+
 	public function reinstallAction() {
 		$force = (1 == V::get('_force', '', $_REQUEST));
 		try {
@@ -319,6 +430,62 @@ jQuery(document).ready(function () {
 		}
 	}
 
+	public function insertTestDataAction() {
+		try {
+			$pdo = DB::getPDO();
+			$pdo->exec("
+				insert into `ZALICZKI_NAJEMCOW` values('1',
+					'0',
+					'test1',
+					'',
+					'test1',
+					'2015',
+					'TAK',
+					'21736.91',
+					'1735.24',
+					'2015-05-12',
+					'2015-12-31',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'13962.75',
+					'21642.26',
+					'21642.26',
+					'21642.26',
+					'21642.26',
+					'21642.26',
+					'21642.26',
+					'21642.26',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					'0.00',
+					NULL,
+					'0',
+					'2016-02-22-15:34',
+					'plabudda',
+					'2016-02-23-11:21',
+					'plabudda',
+					'WAITING',
+					'',
+					''
+				)
+			");
+			echo "\n" . '.END';
+		} catch (Exception $e) {
+			echo "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage();
+		}
+	}
+
 	public function _reinstall($force = false) {
 		$sqlList = array();
 		$pdo = DB::getPDO();
@@ -360,6 +527,8 @@ CREATE TABLE IF NOT EXISTS `ZALICZKI_NAJEMCOW_HIST` (
 	`c10` varchar(20) NOT NULL DEFAULT 'N/S;',
 	`c11` varchar(20) NOT NULL DEFAULT 'N/S;',
 	`c12` varchar(20) NOT NULL DEFAULT 'N/S;',
+  `A_LAST_COST_COUNT_DATE` varchar(30) NOT NULL DEFAULT 'N/S;',
+  `A_LAST_COST_COUNT_ID_GROUP` varchar(11) NOT NULL DEFAULT 'N/S;',
   `A_RECORD_CREATE_DATE` varchar(30) NOT NULL DEFAULT 'N/S;',
   `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL DEFAULT 'N/S;',
   `A_RECORD_UPDATE_DATE` varchar(30) NOT NULL DEFAULT 'N/S;',
@@ -408,9 +577,11 @@ CREATE TABLE IF NOT EXISTS `ZALICZKI_NAJEMCOW` (
 	`c10` decimal(16,2) NOT NULL DEFAULT 0,
 	`c11` decimal(16,2) NOT NULL DEFAULT 0,
 	`c12` decimal(16,2) NOT NULL DEFAULT 0,
-  `A_RECORD_CREATE_DATE` varchar(30) NOT NULL,
+  `A_LAST_COST_COUNT_DATE` datetime,
+  `A_LAST_COST_COUNT_ID_GROUP` int(11) NOT NULL,
+  `A_RECORD_CREATE_DATE` datetime NOT NULL,
   `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL,
-  `A_RECORD_UPDATE_DATE` varchar(30) NOT NULL,
+  `A_RECORD_UPDATE_DATE` datetime,
   `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL,
   `A_STATUS` enum('WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED') NOT NULL DEFAULT 'WAITING',
   `A_CLASSIFIED` varchar(100) NOT NULL,
@@ -470,11 +641,13 @@ EOF_SQL_CREATE;
 			$expectedStruct['c10'] = array('type'=>'decimal', 'num_precision'=>16, 'num_scale'=>2);// `c10` decimal(16,2) NOT NULL DEFAULT 0,
 			$expectedStruct['c11'] = array('type'=>'decimal', 'num_precision'=>16, 'num_scale'=>2);// `c11` decimal(16,2) NOT NULL DEFAULT 0,
 			$expectedStruct['c12'] = array('type'=>'decimal', 'num_precision'=>16, 'num_scale'=>2);// `c12` decimal(16,2) NOT NULL DEFAULT 0,
-			$expectedStruct['A_RECORD_CREATE_DATE'] = array('type'=>'datetime');// `A_RECORD_CREATE_DATE` varchar(30) NOT NULL,
-			$expectedStruct['A_RECORD_CREATE_AUTHOR'] = array('type'=>'varchar');// `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL,
-			$expectedStruct['A_RECORD_UPDATE_DATE'] = array('type'=>'datetime');// `A_RECORD_UPDATE_DATE` varchar(30) NOT NULL,
-			$expectedStruct['A_RECORD_UPDATE_AUTHOR'] = array('type'=>'varchar');// `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL,
-			$expectedStruct['A_STATUS'] = array('type'=>'enum', 'values'=>array('WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED'), 'default_value'=>'WAITING');// `A_STATUS` enum('WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED') NOT NULL DEFAULT 'WAITING',
+			$expectedStruct['A_LAST_COST_COUNT_DATE'] = array('type'=>'datetime', 'is_nullable'=>true);
+			$expectedStruct['A_LAST_COST_COUNT_ID_GROUP'] = array('type'=>'int');
+			$expectedStruct['A_RECORD_CREATE_DATE'] = array('type'=>'datetime');
+			$expectedStruct['A_RECORD_CREATE_AUTHOR'] = array('type'=>'varchar', 'max_length'=>20);
+			$expectedStruct['A_RECORD_UPDATE_DATE'] = array('type'=>'datetime', 'is_nullable'=>true);
+			$expectedStruct['A_RECORD_UPDATE_AUTHOR'] = array('type'=>'varchar', 'max_length'=>20);
+			$expectedStruct['A_STATUS'] = array('type'=>'enum', 'values'=>array('WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED'), 'default_value'=>'WAITING');
 			$expectedStruct['A_CLASSIFIED'] = array('type'=>'varchar');// `A_CLASSIFIED` varchar(100) NOT NULL,
 			$expectedStruct['A_ADM_COMPANY'] = array('type'=>'varchar');// `A_ADM_COMPANY` varchar(100) NOT NULL,
 			$expectedStruct['key_id'] = array('type'=>'PRIMARY KEY', 'key_fields'=>array('ID'));// PRIMARY KEY (`ID`)