瀏覽代碼

added zaliczki najemcow route

Piotr Labudda 10 年之前
父節點
當前提交
2d7f35c31b
共有 3 個文件被更改,包括 544 次插入20 次删除
  1. 23 20
      SE/se-lib/Route/Budget.php
  2. 493 0
      SE/se-lib/Route/ZaliczkiNajemcow.php
  3. 28 0
      SE/se-lib/TypespecialVariable.php

+ 23 - 20
SE/se-lib/Route/Budget.php

@@ -7,10 +7,10 @@ Lib::loadClass('UserStorageFactory');
 
 class Route_Budget extends RouteBase {
 
-	private $_costs = array();
-	private $_plan = array();
-	private $_projectInfo = array();
-	private $_projectPathsOrder = array();
+	public $_costs = array();
+	public $_plan = array();
+	public $_projectInfo = array();
+	public $_projectPathsOrder = array();
 
 	public function handleAuth() {
 		if (!User::logged()) {
@@ -40,7 +40,7 @@ class Route_Budget extends RouteBase {
 		$hasData = false;
 		$groups = null;
 		if ($args['year'] > 0) {
-			$hasData = $this->fetchDataByYear($args['year']);
+			$hasData = $this->fetchDataByYear($args['year'], $args['groups']);
 			$groups = $this->getUsedUserGroups();
 		}
 
@@ -75,7 +75,7 @@ class Route_Budget extends RouteBase {
 		SE_Layout::dol();
 	}
 
-	private function menu($selectedYear, $groups = array(), $selectedGroups = array()) {
+	public function menu($selectedYear, $groups = array(), $selectedGroups = array()) {
 		//SE_Layout::menu();
 		$year = ($selectedYear)? $selectedYear : date("Y");
 		?>
@@ -198,14 +198,6 @@ jQuery(document).ready(function () {
 		$this->css();
 		//DBG::_('DBG', '>1', "costs", $this->_costs, __CLASS__, __FUNCTION__, __LINE__);
 
-		if (!empty($groups)) {
-			foreach ($this->_projectInfo as $idProject => $projInfo) {
-				if (!$this->hasGroupsAccessToProjects($idProject, $groups)) {
-					$this->_projectInfo[$idProject]->filteredByGroups = true;
-				}
-			}
-		}
-
 //echo'<pre>' . json_encode(array_keys($this->_projectPathsOrder)) . '</pre>';
 //echo'<pre>' . json_encode($this->_projectPathsOrder) . '</pre>';
 //echo'<pre>' . json_encode($this->_projectInfo) . '</pre>';
@@ -254,15 +246,26 @@ jQuery("#widget-budget").Budget({
 		return $this->_plan[$idProject][$month];
 	}
 
-	public function fetchDataByYear($year) {
+	public function fetchDataByYear($year, $groups) {
 		$this->_fetchCostsByYear($year);
 		$this->_fetchPlanByYear($year);
 		$this->_fetchProjectInfo();
 		$this->_buildProjectTree();
 		$this->_reacountCostsFromKoresp();
+		$this->_filterProjectsByGroups($groups);
 		return count($this->_projectInfo) > 1;// $this->_projectInfo[0] - Wszystkie projekty
 	}
 
+	public function _filterProjectsByGroups($groups) {
+		if (!empty($groups)) {
+			foreach ($this->_projectInfo as $idProject => $projInfo) {
+				if (!$this->hasGroupsAccessToProjects($idProject, $groups)) {
+					$this->_projectInfo[$idProject]->filteredByGroups = true;
+				}
+			}
+		}
+	}
+
 	public function _fetchPlanByYear($year) {
 		$db = DB::getDB();
 		$this->_plan = array();
@@ -402,7 +405,7 @@ jQuery("#widget-budget").Budget({
 		return $this->_costs;
 	}
 
-	private function _fetchProjectInfo() {
+	public function _fetchProjectInfo() {
 		$db = DB::getDB();
 		$hasAccessForAllProjects = true;
 		$projectIds = array();
@@ -469,7 +472,7 @@ jQuery("#widget-budget").Budget({
 		return false;
 	}
 
-	private function _userHasAccessToProject($project) {
+	public function _userHasAccessToProject($project) {
 		$groups = $this->_getLdapGroupsNames();
 		$userLogin = User::getLogin();
 		if ($project->aclOwner == $userLogin) {
@@ -515,8 +518,8 @@ jQuery("#widget-budget").Budget({
 		return $userGroupsByZasobId;
 	}
 
-	private function _reacountCostsFromKoresp() {
-		$projMonthHasCostSelfIds = array();
+	public function _reacountCostsFromKoresp() {
+		$projHasCostSelfIds = array();
 		foreach ($this->_costs as $kProjId => $vProjInfo) {
 			$projectPath = $this->_projectInfo[$kProjId]->path;
 			foreach ($vProjInfo->korespByMonth as $kMonthNum => $vKorespList) {
@@ -554,7 +557,7 @@ jQuery("#widget-budget").Budget({
 		}
 	}
 
-	private function _createCostIfNotDefined($projId, $monthNum) {
+	public function _createCostIfNotDefined($projId, $monthNum) {
 		if (empty($this->_costs[$projId]->costsByMonth[$monthNum])) {
 			$vEmptyCost = new stdClass();
 			$vEmptyCost->COST_SELF = 0;

+ 493 - 0
SE/se-lib/Route/ZaliczkiNajemcow.php

@@ -0,0 +1,493 @@
+<?php
+
+Lib::loadClass('RouteBase');
+Lib::loadClass('TypespecialVariable');
+Lib::loadClass('Router');
+
+class Route_ZaliczkiNajemcow extends RouteBase {
+
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
+	public function defaultAction() {
+		SE_Layout::gora();
+		//SE_Layout::menu();
+		$this->menu();
+		SE_Layout::dol();
+	}
+
+	public function menu($selectedYear = null, $selectedNajemca = null, $selectedCostGroup = null) {
+		$typeSpecialNajemca  = TypespecialVariable::getInstance(-1, '__ZALICZKI_NAJEMCOW__NAJEMCA');
+		$typeSpecialCostGroup  = TypespecialVariable::getInstance(-1, '__USER_GROUPS');
+		$year = ($selectedYear)? $selectedYear : date("Y");
+		?>
+<div class="jumbotron">
+  <div class="container">
+		<h3>Rozliczenia najemców</h3>
+		<form class="form-inline" method="POST">
+			<input type="hidden" name="_task" value="zaliczki" />
+			<label for="year">Wybierz rok:</label>
+			<div class="input-group date" id="fldYear">
+				<input type="text" name="year" class="form-control" value="" />
+				<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
+			</div>
+			<br>
+			<br>
+
+			<label for="telbox">Wybierz najemcę:</label>
+			<?php if ($typeSpecialNajemca) : ?>
+				<?php
+					$fName = 'najemca';
+					$fldParams = array();
+					$fldParams['allowCreate'] = false;
+					$fldParams['ajaxDataUrlBase'] = "index.php?_route=ZaliczkiNajemcow&_task=typespecial&fld={$fName}";
+					//$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
+					echo $typeSpecialNajemca->showFormItem($tblID = -1, $fName, $selValue = $selectedNajemca, $fldParams);
+				?>
+			<?php endif; ?>
+			<br>
+
+			<label for="group">Wybierz grupę kosztów:</label>
+			<?php if ($typeSpecialCostGroup) : ?>
+				<?php
+					$fName = 'cost_group';
+					// TODO: default group [6388] DZIAL ADMINISTRACJI
+					$fldParams = array();
+					$fldParams['allowCreate'] = false;
+					$fldParams['ajaxDataUrlBase'] = "index.php?_route=ZaliczkiNajemcow&_task=typespecial&fld={$fName}";
+					//$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
+					if (!empty($selectedCostGroup)) {
+						$tsValue = '';
+						$tsValues = $typeSpecialCostGroup->getValuesWithExports($selectedCostGroup);
+						if (!empty($tsValues)) {
+							foreach ($tsValues as $tsItem) {
+								if ($selectedCostGroup == $tsItem->id) {
+									$tsValue = $tsItem->param_out;
+								}
+							}
+						}
+						$fldParams['typespecialValue'] = $tsValue;
+					}
+					echo $typeSpecialCostGroup->showFormItem($tblID = -1, $fName, $selValue = $selectedCostGroup, $fldParams);
+				?>
+			<?php endif; ?>
+			<br>
+			<button type="submit" id="fldGenerateBtn" class="btn btn-primary" autocomplete="off">
+				Zaliczki
+			</button>
+		</form>
+<!--
+		<div style="text-align:right">
+			Edytuj
+			<a href="index.php?_route=Budget&_task=plan&year=<?php echo $year; ?>"
+				 class="btn btn-xs btn-default"
+				 title="Plan budżetu (projects_budget_year_month)">plan budżetu</a>
+			na rok <?php echo $year; ?>
+		</div>
+-->
+	</div>
+</div>
+<script type="text/javascript">
+jQuery(document).ready(function () {
+  jQuery('#fldGenerateBtn').on('click', function () {
+    jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
+		jQuery(this).parent().submit();
+  })
+
+	jQuery("#fldYear").datetimepicker({
+		format: "YYYY",
+		defaultDate: new Date(<?php echo $year; ?>, <?php echo intval(date("m")); ?>, 1),
+		// minDate: new Date(2014, 11, 1),
+		// maxDate: "<?php echo date("Y"); ?>"
+	});
+});
+</script>
+<?php
+	}
+
+	public function typespecialAction() {
+		header("Content-type: application/json");
+
+		$fld = V::get('fld', '', $_GET);
+		switch ($fld) {
+			case 'najemca': {
+					$typeSpecialNajemca = TypespecialVariable::getInstance(-1, '__ZALICZKI_NAJEMCOW__NAJEMCA');
+
+					$query = V::get('q', '', $_REQUEST);
+					$rawRows = null;
+					$rows = $typeSpecialNajemca->getValuesWithExports($query);
+					DBG::_('DBG', '>0', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
+					foreach ($rows as $kID => $vItem) {
+						$itemJson = new stdClass();
+						$itemJson->id = $vItem->id;
+						$itemJson->name = $vItem->param_out;
+						if (!empty($vItem->exports)) {
+							$itemJson->exports = $vItem->exports;
+						}
+						$jsonData[] = $itemJson;
+					}
+					echo json_encode($jsonData);
+				}
+				break;
+			case 'cost_group': {
+					$typeSpecialCostGroup = TypespecialVariable::getInstance(-1, '__USER_GROUPS');
+
+					$query = V::get('q', '', $_REQUEST);
+					$rawRows = null;
+					$rows = $typeSpecialCostGroup->getValuesWithExports($query);
+					DBG::_('DBG', '>0', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
+					foreach ($rows as $kID => $vItem) {
+						$itemJson = new stdClass();
+						$itemJson->id = $vItem->id;
+						$itemJson->name = $vItem->param_out;
+						if (!empty($vItem->exports)) {
+							$itemJson->exports = $vItem->exports;
+						}
+						$jsonData[] = $itemJson;
+					}
+					echo json_encode($jsonData);
+				}
+				break;
+		}
+	}
+
+	public function zaliczkiAction() {
+		$selectedYear = V::get('year', '', $_REQUEST);
+		$selectedNajemca = V::get('najemca', '', $_REQUEST);
+		$selectedCostGroup = V::get('cost_group', '', $_REQUEST);
+		SE_Layout::gora();
+		//SE_Layout::menu();
+		try {
+			$this->menu($selectedYear, $selectedNajemca, $selectedCostGroup);
+			if (!empty($selectedYear)) {
+				$this->zaliczki($selectedYear, $selectedNajemca, $selectedCostGroup);
+			}
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		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];
+		$incomeByMonth = array();
+		for ($month = 1; $month <= 12; $month++) {
+			$fldMonth = sprintf("%02d", $month);
+			$incomeByMonth[$month] = $zaliczka["m{$fldMonth}"];
+		}
+		$total = 0; for($month = 1; $month <= 12; $month++) $total += $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);
+		?>
+<div class="container">
+	<div class="row">
+		<div class="col-md-3" style="text-align:right">Najemca</div>
+		<div class="col-md-3"><?php echo $zaliczka['NAJEMCA']; ?></div>
+		<div class="col-md-3" style="text-align:right">Powierzchnia całkowita do wynajęcia [m<sup>2</sup>]</div>
+		<div class="col-md-3"><?php echo $zaliczka['POW_CALK']; ?></div>
+	</div>
+	<div class="row">
+		<div class="col-md-3" style="text-align:right">Nr lokalu</div>
+		<div class="col-md-3"><?php echo $zaliczka['NR_LOKALU']; ?></div>
+		<div class="col-md-3" style="text-align:right">Powierzchnia lokalu przyjęta do obliczeń [m<sup>2</sup>]</div>
+		<div class="col-md-3"><?php echo $zaliczka['POW_LOKALU']; ?></div>
+	</div>
+	<div class="row">
+		<div class="col-md-3" style="text-align:right">Czy bierze udział w opdziale kosztów opłat administracyjnych</div>
+		<div class="col-md-3"><?php echo $zaliczka['CZY_ROZLICZENIE']; ?></div>
+		<div class="col-md-3" style="text-align:right">Rozliczenie za okres</div>
+		<div class="col-md-3"><?php echo $zaliczka['KOSZT_OD']; ?> - <?php echo $zaliczka['KOSZT_DO']; ?></div>
+	</div>
+	<div class="row">
+		<div class="col-md-3" style="text-align:right">Rok</div>
+		<div class="col-md-3"><?php echo $zaliczka['ROK']; ?></div>
+	</div>
+	<h4 style="margin:12px;text-align:center">Załącznik do faktury za rozliczenie opłat serwisowych</h4>
+	<table class="table table-bordered">
+		<tr>
+			<th></th>
+			<?php for($month = 1; $month <= 12; $month++) : ?>
+				<th><?php echo sprintf("%02d", $month); ?></th>
+			<?php endfor; ?>
+			<th>suma</th>
+			<th>stawka średnia za m<sup>2</sup></th>
+		</tr>
+		<tr>
+			<th style="text-align:right">Wpłacone zaliczki</th>
+			<?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>
+		</tr>
+		<tr>
+			<th style="text-align:right">Koszty przypadające na lokal</th>
+			<?php for($month = 1; $month <= 12; $month++) : ?>
+				<td class="success"><?php echo number_format($localCostByMonth[$month], 2, '.', ','); ?></td>
+			<?php endfor; ?>
+			<th><?php echo number_format($localCostTotal, 2, '.', ','); ?></th>
+			<th><?php echo number_format($localCostTotal / 12 / $zaliczka['POW_LOKALU'], 2, '.', ','); ?></th>
+		</tr>
+		<tr>
+			<th style="text-align:right">Różnica</th>
+			<?php for($month = 1; $month <= 12; $month++) : ?>
+				<td class="info"><?php echo number_format($diff[$month], 2, '.', ','); ?></td>
+			<?php endfor; ?>
+			<th><?php echo number_format($sumTotalDiff, 2, '.', ','); ?></th>
+			<th><?php echo number_format($sumTotalDiff / 12 / $zaliczka['POW_LOKALU'], 2, '.', ','); ?></th>
+		</tr>
+	</table>
+	<div class="row" style="margin-top:60px">
+		<div class="col-md-12">
+			Kwota netto w PLN do zapłaty za rozliczenie kosztów eksploatacyjnych za rok <?php echo $zaliczka['ROK']; ?> wynosi:
+			<br>Razem do zapłaty <?php echo number_format($payTotal, 2, '.', ','); ?> zł
+			<br><br>Przygotowane przez: <?php echo User::getFullName(); ?>
+		</div>
+	</div>
+</div>
+<?php
+	}
+
+	public function reinstallAction() {
+		$force = (1 == V::get('_force', '', $_REQUEST));
+		try {
+			$this->_reinstall($force);
+			echo "\n" . '.END';
+		} catch (Exception $e) {
+			echo "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage();
+		}
+	}
+
+	public function _reinstall($force = false) {
+		$sqlList = array();
+		$pdo = DB::getPDO();
+		$sqlHistCreate = <<<EOF_SQL_HIST_CREATE
+CREATE TABLE IF NOT EXISTS `ZALICZKI_NAJEMCOW_HIST` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
+  `ID_USERS2` int(11) NOT NULL,
+  `ID_BILLING_USERS` varchar(11) NOT NULL DEFAULT 'N/S;',
+  `NAJEMCA` varchar(255) NOT NULL DEFAULT 'N/S;',
+  `T_TELBOX_NEIGHBOUR_IN` varchar(32) NOT NULL DEFAULT 'N/S;',
+  `NR_LOKALU` varchar(255) NOT NULL DEFAULT 'N/S;',
+  `ROK` varchar(5) NOT NULL DEFAULT 'N/S;',
+  `CZY_ROZLICZENIE` varchar(10) NOT NULL DEFAULT 'N/S;',
+  `POW_CALK` varchar(11) NOT NULL DEFAULT 'N/S;',
+  `POW_LOKALU` varchar(11) NOT NULL DEFAULT 'N/S;',
+  `KOSZT_OD` varchar(10) NOT NULL DEFAULT 'N/S;',
+  `KOSZT_DO` varchar(10) NOT NULL DEFAULT 'N/S;',
+	`m01` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m02` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m03` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m04` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m05` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m06` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m07` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m08` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m09` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m10` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m11` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`m12` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c01` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c02` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c03` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c04` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c05` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c06` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c07` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c08` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`c09` varchar(20) NOT NULL DEFAULT 'N/S;',
+	`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_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;',
+  `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL DEFAULT 'N/S;',
+  `A_STATUS` varchar(100) NOT NULL DEFAULT 'N/S;',
+  `A_ADM_COMPANY` varchar(255) DEFAULT 'N/S;',
+  `A_CLASSIFIED` varchar(255) DEFAULT 'N/S;',
+  PRIMARY KEY (`ID`),
+  KEY `ID_USERS2` (`ID_USERS2`)
+) ENGINE=MyISAM  DEFAULT CHARSET=latin2;
+EOF_SQL_HIST_CREATE;
+		$sqlCreate = <<<EOF_SQL_CREATE
+CREATE TABLE IF NOT EXISTS `ZALICZKI_NAJEMCOW` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
+  `ID_BILLING_USERS` int(11) NOT NULL,
+  `NAJEMCA` varchar(255) NOT NULL DEFAULT '',
+  `T_TELBOX_NEIGHBOUR_IN` varchar(32) NOT NULL DEFAULT '',
+  `NR_LOKALU` varchar(255) NOT NULL DEFAULT '',
+  `ROK` int(4) NOT NULL,
+  `CZY_ROZLICZENIE` enum('TAK','NIE') NOT NULL DEFAULT 'NIE',
+  `POW_CALK` double NOT NULL,
+  `POW_LOKALU` double NOT NULL,
+  `KOSZT_OD` date NOT NULL,
+  `KOSZT_DO` date NOT NULL,
+	`m01` decimal(16,2) NOT NULL DEFAULT 0,
+	`m02` decimal(16,2) NOT NULL DEFAULT 0,
+	`m03` decimal(16,2) NOT NULL DEFAULT 0,
+	`m04` decimal(16,2) NOT NULL DEFAULT 0,
+	`m05` decimal(16,2) NOT NULL DEFAULT 0,
+	`m06` decimal(16,2) NOT NULL DEFAULT 0,
+	`m07` decimal(16,2) NOT NULL DEFAULT 0,
+	`m08` decimal(16,2) NOT NULL DEFAULT 0,
+	`m09` decimal(16,2) NOT NULL DEFAULT 0,
+	`m10` decimal(16,2) NOT NULL DEFAULT 0,
+	`m11` decimal(16,2) NOT NULL DEFAULT 0,
+	`m12` decimal(16,2) NOT NULL DEFAULT 0,
+	`c01` decimal(16,2) NOT NULL DEFAULT 0,
+	`c02` decimal(16,2) NOT NULL DEFAULT 0,
+	`c03` decimal(16,2) NOT NULL DEFAULT 0,
+	`c04` decimal(16,2) NOT NULL DEFAULT 0,
+	`c05` decimal(16,2) NOT NULL DEFAULT 0,
+	`c06` decimal(16,2) NOT NULL DEFAULT 0,
+	`c07` decimal(16,2) NOT NULL DEFAULT 0,
+	`c08` decimal(16,2) NOT NULL DEFAULT 0,
+	`c09` decimal(16,2) NOT NULL DEFAULT 0,
+	`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_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL,
+  `A_RECORD_UPDATE_DATE` varchar(30) NOT NULL,
+  `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,
+  `A_ADM_COMPANY` varchar(100) NOT NULL,
+  PRIMARY KEY (`ID`)
+) ENGINE=MyISAM  DEFAULT CHARSET=latin2;
+EOF_SQL_CREATE;
+		/*
+			`ZALICZKI_NAJEMCOW`:
+				- who - user login
+				- when - shedule (once a day, once on houd)
+				- what - action type
+				- last_exec_time
+		*/
+		$pdo->exec($sqlHistCreate);
+		$pdo->exec($sqlCreate);
+		if($force){
+			$pdo->exec("DROP TABLE IF EXISTS ZALICZKI_NAJEMCOW_HIST");
+			$pdo->exec("DROP TABLE IF EXISTS ZALICZKI_NAJEMCOW");
+			$pdo->exec($sqlHistCreate);
+			$pdo->exec($sqlCreate);
+		}
+		if(0){// TODO: assertTableStruct ZALICZKI_NAJEMCOW
+			$expectedStruct = array();
+			$expectedStruct['ID'] = array('type'=>'int', 'extra'=>'AUTO_INCREMENT');// `ID` int(11) NOT NULL AUTO_INCREMENT,
+			$expectedStruct['ID_BILLING_USERS'] = array('type'=>'int');// `ID_BILLING_USERS` int(11) NOT NULL,
+			$expectedStruct['NAJEMCA'] = array('type'=>'varchar', 'max_length'=>'255', 'default'=>'');// `NAJEMCA` varchar(255) NOT NULL DEFAULT '',
+			$expectedStruct['T_TELBOX_NEIGHBOUR_IN'] = array('type'=>'varchar');// `T_TELBOX_NEIGHBOUR_IN` varchar(32) NOT NULL DEFAULT '',
+			$expectedStruct['NR_LOKALU'] = array('type'=>'varchar');// `NR_LOKALU` varchar(255) NOT NULL DEFAULT '',
+			$expectedStruct['ROK'] = array('type'=>'varchar');// `ROK` int(4) NOT NULL,
+			$expectedStruct['CZY_ROZLICZENIE'] = array('type'=>'varchar');// `CZY_ROZLICZENIE` enum('TAK','NIE') NOT NULL DEFAULT 'NIE',
+			$expectedStruct['POW_CALK'] = array('type'=>'varchar');// `POW_CALK` double NOT NULL,
+			$expectedStruct['POW_LOKALU'] = array('type'=>'varchar');// `POW_LOKALU` double NOT NULL,
+			$expectedStruct['KOSZT_OD'] = array('type'=>'varchar');// `KOSZT_OD` datetime NOT NULL,
+			$expectedStruct['KOSZT_DO'] = array('type'=>'varchar');// `KOSZT_DO` datetime NOT NULL,
+			$expectedStruct['m01'] = array('type'=>'varchar');// `m01` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m02'] = array('type'=>'varchar');// `m02` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m03'] = array('type'=>'varchar');// `m03` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m04'] = array('type'=>'varchar');// `m04` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m05'] = array('type'=>'varchar');// `m05` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m06'] = array('type'=>'varchar');// `m06` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m07'] = array('type'=>'varchar');// `m07` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m08'] = array('type'=>'varchar');// `m08` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m09'] = array('type'=>'varchar');// `m09` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m10'] = array('type'=>'varchar');// `m10` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m11'] = array('type'=>'varchar');// `m11` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['m12'] = array('type'=>'varchar');// `m12` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c01'] = array('type'=>'varchar');// `c01` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c02'] = array('type'=>'varchar');// `c02` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c03'] = array('type'=>'varchar');// `c03` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c04'] = array('type'=>'varchar');// `c04` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c05'] = array('type'=>'varchar');// `c05` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c06'] = array('type'=>'varchar');// `c06` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c07'] = array('type'=>'varchar');// `c07` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c08'] = array('type'=>'varchar');// `c08` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c09'] = array('type'=>'varchar');// `c09` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c10'] = array('type'=>'varchar');// `c10` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c11'] = array('type'=>'varchar');// `c11` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['c12'] = array('type'=>'varchar');// `c12` decimal(16,2) NOT NULL DEFAULT 0,
+			$expectedStruct['A_RECORD_CREATE_DATE'] = array('type'=>'varchar');// `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'=>'varchar');// `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'=>'varchar');// `A_STATUS` enum('WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED') NOT NULL DEFAULT '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');// PRIMARY KEY (`ID`)
+
+			DBG::_(true, true, "sqlCreate - raw", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
+			$sqlCreate = $pdo->showCreateStruct('ZALICZKI_NAJEMCOW', $expectedStruct, array('char_encoding'=>'latin2'));
+			DBG::_(true, true, "sqlCreate - generated", $sqlCreate, __CLASS__, __FUNCTION__, __LINE__);
+			$pdo->assertTableStruct('ZALICZKI_NAJEMCOW', $expectedStruct, array('char_encoding'=>'latin2'));
+			if(0){// force - drop/create
+				$pdo->exec("DROP TABLE IF EXISTS ZALICZKI_NAJEMCOW");
+				$pdo->exec($sqlCreate);
+			}
+		}
+	}
+
+}

+ 28 - 0
SE/se-lib/TypespecialVariable.php

@@ -26,6 +26,7 @@ class TypespecialVariable extends TypespecialBase {
 			case '__TELBOXES':
 			case '__TELBOXES_NAME':
 			case '__DEALS_SALES_TELBOXES_NAME':
+			case '__ZALICZKI_NAJEMCOW__NAJEMCA':
 			case '__ZASOB':
 			case '__PROCES':
 			case '__COMPANIES':
@@ -509,6 +510,33 @@ jQuery(document).ready(function(){
 				if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
 				break;
 			}
+			case '__ZALICZKI_NAJEMCOW__NAJEMCA': {
+				$pdo = DB::getPDO();
+				$query = trim($query, ' %');
+				$sqlLimit = 20;
+				$sql = "select x.`id`, x.`label`
+					from (
+						select d.`NAJEMCA` as `id`
+							, d.`NAJEMCA` as `label`
+						from `ZALICZKI_NAJEMCOW` d
+						where d.`A_STATUS`!='DELETED'
+							and d.`NAJEMCA` like :query_like
+						group by d.`NAJEMCA`
+					) as x
+					order by x.`label`
+					limit {$sqlLimit}
+				";
+				DBG::_('DBG_TS', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
+				$sth = $pdo->prepare($sql);
+				$sth->bindValue(':query_like', "%{$query}%", PDO::PARAM_STR);
+				$sth->execute();
+				$list = $sth->fetchAll();
+				foreach ($list as $item) {
+					$values[] = (object)array('id'=>$item['id'], 'param_out'=>'');
+				}
+				DBG::_('DBG_TS', '>2', "values", $values, __CLASS__, __FUNCTION__, __LINE__);
+				break;
+			}
 			case '__ZASOB': {
 				$db = DB::getDB();
 				$sqlQuery = new stdClass();