Browse Source

added save offer and save default offer in Kosztorysy

Piotr Labudda 10 years ago
parent
commit
ea2a75c972
1 changed files with 411 additions and 88 deletions
  1. 411 88
      SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

+ 411 - 88
SE/se-lib/Route/UrlAction/ProjektyKosztyWstepnychRobot.php

@@ -14,10 +14,11 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		// TODO: check if user is allowed to run this action
 		SE_Layout::gora();
 		try {
-			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
+			$idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
+			$idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
 			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
-			$this->panel($idProject);
-			$this->kosztorys($idProject);
+			$this->panel($idProject, $idCompany);
+			$this->kosztorys($idProject, $idCompany);
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
@@ -28,10 +29,28 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		// TODO: check if user is allowed to run this action
 		SE_Layout::gora();
 		try {
-			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
-			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
-			$this->panel($idProject);
-			$this->oferta($idProject, $admin = true);
+			$idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
+			$idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
+			$this->panel($idProject, $idCompany);
+			if ($idProject > 0) {
+				$this->oferta($idProject, $idCompany, $admin = true);
+			} else {
+				$this->defaultOferta();
+			}
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function ofertaDefaultAdminAction() {
+		// TODO: check if user is allowed to run this action
+		SE_Layout::gora();
+		try {
+			$idProject = 0;
+			$idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
+			$this->panel($idProject, $idCompany);
+			$this->defaultOferta();
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
@@ -42,10 +61,11 @@ class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: U
 		// TODO: check if user is allowed to run this action
 		SE_Layout::gora();
 		try {
-			$idProject = V::get('ID_PROJECT', '', $_REQUEST, 'int');
-			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
-			$this->panel($idProject);
-			$this->oferta($idProject, $admin = false);
+			$idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
+			$idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
+			if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");// TODO: show select box if not defined
+			$this->panel($idProject, $idCompany);
+			$this->oferta($idProject, $idCompany, $admin = false);
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
@@ -640,9 +660,10 @@ SQL_FUN;
 		}
 	}
 
-	public function panel($idProject) {
+	public function panel($idProject, $idCompany = 0) {
 		if (User::get('ADM_ADMIN_LEVEL') > 6 || 'Pracownik' != User::getType()) return;// Only for workers with admin level < 6
 		if (1 == V::get('_print', '', $_GET)) return;// print mode
+		$task = V::get('_task', '', $_GET);
 		?>
 <div class="jumbotron">
   <div class="container">
@@ -651,81 +672,355 @@ SQL_FUN;
 ?>
 		<div class="row">
 			<div class="col-md-12">
-				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>">Kosztorys</a>
-				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_print=1" target="_blank">Wydruk Kosztorysu</a>
-				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=ofertaAdmin">oferta (Admin)</a>
-				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=oferta">oferta</a>
+				<?php if ($idProject > 0) : ?>
+					<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>">Kosztorys</a>
+					<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_print=1" target="_blank">Wydruk Kosztorysu</a>
+					<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=oferta">oferta</a>
+					<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=ofertaAdmin">oferta (Admin)</a>
+				<?php endif; ?>
+				<a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&_task=ofertaDefaultAdmin" target="_blank">oferta domyślna (Admin)</a>
 			</div>
 		</div>
+		<br>
+		<?php if ('ofertaAdmin' == $task) : ?>
+			<form action="" method="get" class="form-inline">
+				<input type="hidden" name="_route" value="UrlAction_ProjektyKosztyWstepnychRobot">
+				<input type="hidden" name="_task" value="ofertaAdmin">
+				<label for="ID_PROJECT">Nr projektu:</label>
+				<input type="text" name="ID_PROJECT" value="<?php echo $idProject; ?>" class="form-control">
+				<label for="ID_COMPANY">Nr kontrahenta (0 = oferta wewnętrzna/kosztorys)</label>
+				<input type="text" name="ID_COMPANY" value="<?php echo $idCompany; ?>" class="form-control">
+				<input type="submit" value="Wybierz" class="btn btn-primary">
+			</form>
+		<?php elseif ('oferta' == $task || 'ofertaDefaultAdmin' == $task) : ?>
+			<form action="" method="get" class="form-inline">
+				<input type="hidden" name="_route" value="UrlAction_ProjektyKosztyWstepnychRobot">
+				<input type="hidden" name="_task" value="oferta">
+				<input type="hidden" name="ID_PROJECT" value="<?php echo $idProject; ?>">
+				<label for="ID_COMPANY">Nr kontrahenta</label>
+				<input type="text" name="ID_COMPANY" value="<?php echo $idCompany; ?>" class="form-control">
+				<input type="submit" value="Wybierz" class="btn btn-primary">
+			</form>
+		<?php endif; ?>
   </div>
 </div>
 <?php
 	}
 
-	public function saveOffer($idProject, $args, $admin = false) {
-		DBG::_(true, true, "TODO...", null, __CLASS__, __FUNCTION__, __LINE__);return;
+	public function saveOffer($idProject, $idCompany, $args, $admin = false) {
+		DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
 		if (!$admin) return;
-		DBG::_(true, true, "args", $args, __CLASS__, __FUNCTION__, __LINE__);
+		$cennik = $this->getCennik($idProject, $idCompany);
+		DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
 		$pdo = DB::getPDO();
-		$add_id_zasob = 0;
-		$add_id_companies = 0;// TODO: oferty dostawców
-		$add_id_project = $idProject;
-		$add_unit = '';
-		$add_price = 0;
-		$add_author = 0;
-		$add_quantity = 0;
-		$add__sth = $pdo->prepare("
-			insert into CRM_LISTA_ZASOBOW_OFFERS__TEST (
-				CRM_LISTA_ZASOBOW_ID
-				, COMPANIES_ID
-				, ID_PROJECT
-				, OFFER_PRICE_PER_RESOURCE_UNIT
-				, RESOURCE_UNIT_TYPE
-				, OFFER_UNIT_TYPE
-				, REQUIRED_RESOURCE_UNITS
-				, A_RECORD_CREATE_AUTHOR
-				, A_RECORD_CREATE_DATE
-			) values (
-				:id_zasob
-				, :id_companies
-				, :id_project
-				, :price
-				, :unit
-				, :unit
-				, :quantity
-				, :author
-				, NOW()
-			)
-		");
-		$add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_STR);
-		$add__sth->bindParam(':id_companies', $add_id_companies, PDO::PARAM_STR);
-		$add__sth->bindParam(':id_project', $add_id_project, PDO::PARAM_STR);
-		$add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
-		$add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
-		$add__sth->bindParam(':quantity', $add_quantity, PDO::PARAM_STR);
-		$add__sth->bindParam(':author',   $usrLogin, PDO::PARAM_STR);
+		{
+			$add_id_zasob = 0;
+			$add_unit = '';
+			$add_price = 0;
+			$add_quantity = 0;
+			$add__sth = $pdo->prepare("
+				insert into CRM_LISTA_ZASOBOW_OFFERS__TEST (
+					CRM_LISTA_ZASOBOW_ID
+					, COMPANIES_ID
+					, ID_PROJECT
+					, OFFER_PRICE_PER_RESOURCE_UNIT
+					, RESOURCE_UNIT_TYPE
+					, OFFER_UNIT_TYPE
+					, REQUIRED_RESOURCE_UNITS
+					, A_RECORD_CREATE_AUTHOR
+					, A_RECORD_CREATE_DATE
+				) values (
+					:id_zasob
+					, :id_company
+					, :id_project
+					, :price
+					, :unit
+					, :unit
+					, :quantity
+					, :author
+					, NOW()
+				)
+			");
+			$add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
+			$add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+			$add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+			$add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
+			$add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
+			$add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
+			$add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
+		}
+		{
+			$edit_id_company = $idCompany;
+			$edit_id_zasob = 0;
+			$edit_unit = '';
+			$edit_price = 0;
+			$edit_author = 0;
+			$edit_quantity = 0;
+			$edit__sth = $pdo->prepare("
+				update CRM_LISTA_ZASOBOW_OFFERS__TEST
+				set CRM_LISTA_ZASOBOW_ID = :id_zasob
+					, COMPANIES_ID = :id_company
+					, ID_PROJECT = :id_project
+					, OFFER_PRICE_PER_RESOURCE_UNIT = :price
+					, RESOURCE_UNIT_TYPE = :unit
+					, OFFER_UNIT_TYPE = :unit
+					, REQUIRED_RESOURCE_UNITS = :quantity
+					, A_RECORD_UPDATE_AUTHOR = :author
+					, A_RECORD_UPDATE_DATE = NOW()
+				where ID = :id
+			");
+			$edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
+			$edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
+			$edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+			$edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+			$edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
+			$edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
+			$edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
+			$edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
+		}
 		$schema = $this->getSchema();
-		$data = $this->fetchData($idProject, $admin);
-		foreach ($data as $idLayer => $layData) {
-			DBG::_(true, true, "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
-			foreach ($layData['data'] as $typeData) {
-				DBG::_(true, true, "typeData", $typeData, __CLASS__, __FUNCTION__, __LINE__);
-				if ($typeData['idType']) {
-					$add_id_zasob = $typeData['idType'];
-					$add_price = V::get("price_{$idLayer}_{$typeData['idType']}", '', $args);
-					$add_unit = $typeData['jednostka'];
-					$add_quantity = $typeData['ilosc'];
+		foreach ($schema['config']['layer'] as $idLayer => $layData) {
+			DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
+			foreach ($layData['type'] as $idType => $typeLabel) {
+				$edit_id = V::get("edit_price_id_{$idType}", '', $args);
+				$add_price = V::get("price_{$idType}", '', $args);
+				DBG::_('DBG', '>1', "typeLabel (edit={$edit_id}, price={$add_price})", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
+				if ($edit_id > 0) {
+					$edit_price = V::get("price_{$idType}", '', $args);
+					$edit_price = str_replace(',', '.', $edit_price);
+					if ($edit_id > 0 && $edit_price > 0) {
+						$edit_id_zasob = $idType;
+						$edit_unit = $layData['jednostka'];
+						// TODO: check if anything change
+						$edit__sth->execute();
+						// TODO: add to hist
+					}
+				} else {
+					$add_price = V::get("price_{$idType}", '', $args);
+					$add_price = str_replace(',', '.', $add_price);
 					if ($add_price > 0) {
+						$add_id_zasob = $idType;
+						$add_unit = $layData['jednostka'];
 						$add__sth->execute();
 					}
 				}
 			}
 		}
 	}
+	public function saveDefaultOffer($args) {
+		DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
+		$cennik = $this->getDefaultCennik();
+		DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
+		$idCompany = 0;
+		$idProject = 0;
+		$pdo = DB::getPDO();
+		{
+			$add_id_zasob = 0;
+			$add_unit = '';
+			$add_price = 0;
+			$add_quantity = 0;
+			$add__sth = $pdo->prepare("
+				insert into CRM_LISTA_ZASOBOW_OFFERS__TEST (
+					CRM_LISTA_ZASOBOW_ID
+					, COMPANIES_ID
+					, ID_PROJECT
+					, OFFER_PRICE_PER_RESOURCE_UNIT
+					, RESOURCE_UNIT_TYPE
+					, OFFER_UNIT_TYPE
+					, REQUIRED_RESOURCE_UNITS
+					, A_RECORD_CREATE_AUTHOR
+					, A_RECORD_CREATE_DATE
+				) values (
+					:id_zasob
+					, :id_company
+					, :id_project
+					, :price
+					, :unit
+					, :unit
+					, :quantity
+					, :author
+					, NOW()
+				)
+			");
+			$add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
+			$add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+			$add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+			$add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
+			$add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
+			$add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
+			$add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
+		}
+		{
+			$edit_id_zasob = 0;
+			$edit_unit = '';
+			$edit_price = 0;
+			$edit_author = 0;
+			$edit_quantity = 0;
+			$edit__sth = $pdo->prepare("
+				update CRM_LISTA_ZASOBOW_OFFERS__TEST
+				set CRM_LISTA_ZASOBOW_ID = :id_zasob
+					, COMPANIES_ID = :id_company
+					, ID_PROJECT = :id_project
+					, OFFER_PRICE_PER_RESOURCE_UNIT = :price
+					, RESOURCE_UNIT_TYPE = :unit
+					, OFFER_UNIT_TYPE = :unit
+					, REQUIRED_RESOURCE_UNITS = :quantity
+					, A_RECORD_UPDATE_AUTHOR = :author
+					, A_RECORD_UPDATE_DATE = NOW()
+				where ID = :id
+			");
+			$edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
+			$edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
+			$edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+			$edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+			$edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
+			$edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
+			$edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
+			$edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
+		}
+		$schema = $this->getSchema();
+		foreach ($schema['config']['layer'] as $idLayer => $layData) {
+			DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
+			foreach ($layData['type'] as $idType => $typeLabel) {
+				DBG::_('DBG', '>1', "typeLabel", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
+				$edit_id = V::get("edit_price_id_{$idType}", '', $args);
+				if ($edit_id > 0) {
+					$edit_price = V::get("price_{$idType}", '', $args);
+					$edit_price = str_replace(',', '.', $edit_price);
+					if ($edit_id > 0 && $edit_price > 0) {
+						$edit_id_zasob = $idType;
+						$edit_unit = $layData['jednostka'];
+						// TODO: check if anything change
+						$edit__sth->execute();
+						// TODO: add to hist
+					}
+				} else {
+					$add_price = V::get("price_{$idType}", '', $args);
+					$add_price = str_replace(',', '.', $add_price);
+					if ($add_price > 0) {
+						$add_id_zasob = $idType;
+						$add_unit = $layData['jednostka'];
+						$add__sth->execute();
+					}
+				}
+			}
+		}
+	}
+
+	public function getDefaultCennik($idCompany = 0) {
+		$schema = $this->getSchema();
+		$typeIdList = array_keys($schema['config']['type']);
+		$sqlTypeIdList = implode(',', $typeIdList);
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			select o.ID
+				, o.CRM_LISTA_ZASOBOW_ID as id_zasob
+				, o.COMPANIES_ID as id_company
+				, o.ID_PROJECT as id_project
+				, o.OFFER_PRICE_PER_RESOURCE_UNIT as price
+				, o.RESOURCE_UNIT_TYPE as unit
+--				, o.OFFER_UNIT_TYPE as unit
+				, o.REQUIRED_RESOURCE_UNITS as quantity
+			from CRM_LISTA_ZASOBOW_OFFERS__TEST o
+			where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
+--				and o.A_STATUS not in ('DELETED')
+				and o.ID_PROJECT = 0
+				and o.COMPANIES_ID = :id_company
+		");
+		$sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+		$sth->execute();
+		$cennikRaw = $sth->fetchAll();
+		$cennik = array();
+		foreach ($cennikRaw as $itemRaw) {
+			$item = $itemRaw;
+			$item['price'] = round($item['price'], 2);
+			if (!empty($cennik[$itemRaw['id_zasob']])) {
+				if ($itemRaw['ID'] < $cennik[$itemRaw['id_zasob']]['ID']) continue;
+			}
+			$cennik[$item['id_zasob']] = $item;
+		}
+		return $cennik;
+	}
+
+	public function getCennik($idProject, $idCompany = 0) {
+		$schema = $this->getSchema();
+		$typeIdList = array_keys($schema['config']['type']);
+		$sqlTypeIdList = implode(',', $typeIdList);
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			select o.ID
+				, o.CRM_LISTA_ZASOBOW_ID as id_zasob
+				, o.COMPANIES_ID as id_company
+				, o.ID_PROJECT as id_project
+				, o.OFFER_PRICE_PER_RESOURCE_UNIT as price
+				, o.RESOURCE_UNIT_TYPE as unit
+--				, o.OFFER_UNIT_TYPE as unit
+				, o.REQUIRED_RESOURCE_UNITS as quantity
+			from CRM_LISTA_ZASOBOW_OFFERS__TEST o
+			where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
+--				and o.A_STATUS not in ('DELETED')
+				and o.ID_PROJECT = :id_project
+				and o.COMPANIES_ID = :id_company
+		");
+		$sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
+		$sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
+		$sth->execute();
+		$cennikRaw = $sth->fetchAll();
+		$cennik = array();
+		foreach ($cennikRaw as $itemRaw) {
+			$item = $itemRaw;
+			$item['price'] = round($item['price'], 2);
+			$cennik[$itemRaw['id_zasob']] = $item;
+		}
+		return $cennik;
+	}
 
-	public function oferta($idProject, $admin = false) {
+	public function defaultOferta($idCompany = 0) {
+		if ('1' == V::get('save_offer', '', $_POST)) {
+			$this->saveDefaultOffer($_POST);
+		}
+		$schema = $this->getSchema();
+		if (empty($schema['config']['type'])) throw new Exception("Schema error - brak zdefiniowanych typów");
+		$cennik = $this->getDefaultCennik($idCompany);
+		//DBG::_(true, true, "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
+		?>
+<div class="container">
+<form action="" method="post">
+	<?php foreach ($schema['config']['layer'] as $idLayer => $layData) : ?>
+		<h4 style="padding:0 6px"><?php echo $layData['label']; ?></h4>
+		<table class="tabel table-bordered" style="width:100%">
+		<tbody>
+			<tr>
+				<th style="padding:0 6px">id zasobu</th>
+				<th style="padding:0 6px">typ</th>
+				<th style="padding:0 6px">jednotka miary</th>
+				<th style="padding:0 6px">cena jednostkowa</th>
+			</tr>
+			<?php foreach ($layData['type'] as $idType => $typeLabel) : ?>
+				<tr>
+					<td style="width:100px; padding:0 6px"><?php echo $idType; ?></td>
+					<td style="padding:0 6px" title="[<?php echo $idType; ?>] <?php echo $typeLabel; ?>"><?php echo $typeLabel; ?></td>
+					<td style="padding:0 6px"><?php echo $layData['jednostka']; ?></td>
+					<td style="padding:3px 6px"><input type="text" class="form-control input-sm" name="price_<?php echo $idType; ?>" value="<?php echo $cennik[$idType]['price']; ?>"/></td>
+				</tr>
+			<?php endforeach; ?>
+		</tbody>
+		</table>
+	<?php endforeach; ?>
+	<br>
+	<?php foreach ($cennik as $item) : ?>
+		<input type="hidden" name="edit_price_id_<?php echo $item['id_zasob']; ?>" value="<?php echo $item['ID']; ?>">
+	<?php endforeach; ?>
+	<input type="hidden" name="save_offer" value="1">
+	<input type="submit" value="Zapisz" class="btn btn-primary">
+</form>
+</div>
+<?php
+	}
+
+	public function oferta($idProject, $idCompany = 0, $admin = false) {
 		if ($admin && '1' == V::get('save_offer', '', $_POST)) {
-			$this->saveOffer($idProject, $_POST, $admin);
+			$this->saveOffer($idProject, $idCompany, $_POST, $admin);
 		}
 		$schema = $this->getSchema();
 		$conf = $schema['config'];
@@ -733,7 +1028,10 @@ SQL_FUN;
 
 		$data = $this->fetchData($idProject, $admin);
 		DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
-		?>
+
+		$defCennik = $this->getDefaultCennik($idCompany);
+		$cennik = $this->getCennik($idProject, $idCompany);
+?>
 <div class="container">
 	<?php if ($admin) : ?>
 		<form action="" method="post">
@@ -747,6 +1045,7 @@ SQL_FUN;
 				<th style="padding:0 6px">ilość</th>
 				<th style="padding:0 6px">jednotka miary</th>
 				<th style="padding:0 6px">cena jednostkowa</th>
+				<th style="padding:0 6px">cena jednostkowa (domyślna)</th>
 			</tr>
 			<?php foreach ($layData['data'] as $typeData) : ?>
 				<?php if (!$typeData['idType']) : ?>
@@ -765,11 +1064,14 @@ SQL_FUN;
 						</td>
 					</tr>
 				<?php else : ?>
+					<?php $price = (!empty($cennik[$typeData['idType']]['price']))? $cennik[$typeData['idType']]['price'] : 0; ?>
+					<?php $defPrice = (!empty($defCennik[$typeData['idType']]['price']))? $defCennik[$typeData['idType']]['price'] : 0; ?>
 					<tr>
 						<td style="padding:0 6px" title="[<?php echo $typeData['idType']; ?>] <?php echo $typeData['type']; ?>"><?php echo $typeData['type']; ?></td>
 						<td style="padding:0 6px"><?php echo $typeData['ilosc']; ?></td>
 						<td style="padding:0 6px"><?php echo $typeData['jednostka']; ?></td>
-						<td style="padding:3px 6px"><input type="text" class="form-control input-sm" name="price_<?php echo $idLayer; ?>_<?php echo $typeData['idType']; ?>" value="<?php echo V::get('cena_jednostkowa', '', $typeData); ?>"/></td>
+						<td style="padding:3px 6px"><input type="text" class="form-control input-sm" name="price_<?php echo $typeData['idType']; ?>" value="<?php echo $price; ?>"/></td>
+						<td style="padding:3px 6px"><?php echo $defPrice; ?></td>
 					</tr>
 				<?php endif; ?>
 			<?php endforeach; ?>
@@ -777,6 +1079,9 @@ SQL_FUN;
 		</table>
 	<?php endforeach; ?>
 	<?php if ($admin) : ?>
+		<?php foreach ($cennik as $item) : ?>
+			<input type="hidden" name="edit_price_id_<?php echo $item['id_zasob']; ?>" value="<?php echo $item['ID']; ?>">
+		<?php endforeach; ?>
 			<input type="hidden" name="save_offer" value="1">
 			<hr><input class="btn btn-primary" type="submit" value="Zapisz ofertę">
 		</form>
@@ -888,7 +1193,10 @@ SQL_FUN;
 					if (!array_key_exists($z['ID'], $conf['type'])) {
 						$conf['type'][$z['ID']] = $z['DESC'];
 					}
-				} else if ('TABELA' == $z['TYPE']) {
+				}
+			}
+			foreach ($rawConf as $z) {
+				if ('TABELA' == $z['TYPE']) {
 					if (!array_key_exists($z['ID'], $conf['layer'])) {
 						$layer = array();
 						$layer['label'] = $z['DESC'];
@@ -899,6 +1207,7 @@ SQL_FUN;
 						$layer['zasob_field'] = '';
 						$layer['zasob_id'] = '';
 						$layer['ilosc_field'] = '';
+						$layer['type'] = array();
 						$conf['layer'][$z['ID']] = $layer;
 					}
 					if ($z['c_ID'] > 0) {
@@ -915,25 +1224,39 @@ SQL_FUN;
 							[za_DESC] => OznKabla
 							[za_ALIAS_ID] => 0
 						*/
-						if ('ZASOB' == $z['c_DESC']) {
-							if ($z['za_ID'] > 0) {
-								$layer = $conf['layer'][$z['ID']];
-								$layer['zasob_type'] = $z['c_DESC'];
-								$layer['zasob_field'] = $z['za_DESC'];
-								$layer['zasob_id'] = $z['za_ID'];
-								$conf['layer'][$z['ID']] = $layer;
-							}
-						}
-						if ('ZASOB_ID' == $z['c_DESC']) {
-							if ($z['za_ID'] > 0) {
-								$layer = $conf['layer'][$z['ID']];
-								$layer['zasob_type'] = $z['c_DESC'];
-								$layer['zasob_id'] = $z['za_ID'];
-								$layer['zasob_label'] = $z['za_DESC'];
-								$conf['layer'][$z['ID']] = $layer;
-							}
+						switch ($z['c_DESC']) {
+							case 'ZASOB': {
+								if ($z['za_ID'] > 0) {
+									$layer = $conf['layer'][$z['ID']];
+									$layer['zasob_type'] = $z['c_DESC'];
+									$layer['zasob_field'] = $z['za_DESC'];
+									$layer['zasob_id'] = $z['za_ID'];
+									$conf['layer'][$z['ID']] = $layer;
+								}
+							} break;
+							case 'ZASOB_ID': {
+								if ($z['za_ID'] > 0) {
+									$layer = $conf['layer'][$z['ID']];
+									$layer['zasob_type'] = $z['c_DESC'];
+									$layer['zasob_id'] = $z['za_ID'];
+									$layer['zasob_label'] = $z['za_DESC'];
+									$conf['layer'][$z['ID']] = $layer;
+									$conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC'];
+								}
+							} break;
+							case 'TYPE': {
+								//DBG::_(true, true, "z", $z, __CLASS__, __FUNCTION__, __LINE__);
+								if (empty($z['c_ALIAS_ID'])) throw new Exception("Schema error - brak ALIAS_ID dla typu");
+								if (empty($conf['type'][$z['za_ID']])) ;// TODO: throw exception
+								$conf['layer'][$z['ID']]['type'][$z['za_ID']] = $z['za_DESC'];
+							} break;
 						}
 					}
+				}
+			}
+			foreach ($rawConf as $z) {
+				if ('INNE' == $z['TYPE']) {
+				} else if ('TABELA' == $z['TYPE']) {
 				} else {
 					SE_Layout::alert('warnig', "BUG: unimplemented type '{$z['TYPE']}' for zasob nr: {$z['ID']}");
 				}