| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- Lib::loadClass('ProjectKosztorysSchema');
- class ProjectKosztorysCennik {
- /**
- * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
- */
- public static function getCennik($idProject, $idCompany = 0) {
- $schema = ProjectKosztorysSchema::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 o
- where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
- and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
- and o.ID_PROJECT = :id_project
- and o.COMPANIES_ID = :id_company
- and o.RESOURCE_UNIT_TYPE != 'ROBOCIZNA'
- and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
- ");
- $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;
- }
- /**
- * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
- */
- public static function getWorkCennik($idProject, $idCompany = 0) {
- $schema = ProjectKosztorysSchema::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 o
- where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
- and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
- and o.ID_PROJECT = :id_project
- and o.COMPANIES_ID = :id_company
- and o.RESOURCE_UNIT_TYPE = 'ROBOCIZNA'
- and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
- ");
- $sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
- $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
- $sth->execute();
- $robociznaCennikRaw = $sth->fetchAll();
- $robociznaCennik = array();
- foreach ($robociznaCennikRaw as $itemRaw) {
- $item = $itemRaw;
- $item['price'] = round($item['price'], 2);
- $robociznaCennik[$itemRaw['id_zasob']] = $item;
- }
- return $robociznaCennik;
- }
- /**
- * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
- */
- public static function getDefaultCennik($idCompany = 0) {
- $schema = ProjectKosztorysSchema::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 o
- where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
- and (o.A_STATUS is null or 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 static function getTypeLabel($idType) {
- $schema = ProjectKosztorysSchema::getSchema();
- if (!array_key_exists($idType, $schema['config']['type'])) throw new Exception("Missing Type in schema ([{$idType}])");
- DBG::_('DBG', '>1', 'type', $schema['config']['type'][$idType], __CLASS__, __FUNCTION__, __LINE__);
- return V::get($idType, "[$idType]", $schema['config']['type']);
- }
- public static function updatePriceProjectCennik($idType, $idProject, $price, $jednostka) {
- $item = self::getItemProjectCennik($idType, $idProject, $jednostka);
- $usrLogin = User::getLogin();
- $sqlObj = array();
- $sqlObj['CRM_LISTA_ZASOBOW_ID'] = $idType;
- $sqlObj['OFFER_PRICE_PER_RESOURCE_UNIT'] = $price;
- $sqlObj['RESOURCE_UNIT_TYPE'] = $jednostka;
- $sqlObj['OFFER_UNIT_TYPE'] = $jednostka;
- if (empty($item)) {
- $sqlObj['COMPANIES_ID'] = 0;
- $sqlObj['ID_PROJECT'] = $idProject;
- $sqlObj['REQUIRED_RESOURCE_UNITS'] = 0;
- $idInserted = DB::getDB()->ADD_NEW_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
- if ($idInserted <= 0) throw new Exception("Nie udało się utworzyć rekordu");
- } else {
- $sqlObj['ID'] = $item['ID'];
- $affected = DB::getDB()->UPDATE_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
- if ($affected < 0) throw new Exception("Nie udało się zaktualizować rekordu");
- }
- }
- public static function getPriceDefaultCennik($idType) {
- $item = self::getItemDefaultCennik($idType);
- return (!empty($item)) ? round($item['price'], 2) : 0;
- }
- public static function getItemDefaultCennik($idType) {
- $cennikRaw = self::_getPrices($idType);
- foreach ($cennikRaw as $item) {// fetch price from Oferta Admin
- if (!$item['id_company'] && !$item['id_project']) {
- if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
- return $item;
- }
- }
- }
- return null;
- }
- public static function getPriceProjectCennik($idType, $idProject, $jednostka) {
- $item = self::getItemProjectCennik($idType, $idProject, $jednostka);
- return (!empty($item)) ? round($item['price'], 2) : 0;
- }
- public static function getItemProjectCennik($idType, $idProject, $jednostka) {
- $cennikRaw = self::_getPrices($idType, $idProject);
- foreach ($cennikRaw as $item) {// fetch price from Project Oferta
- if ($idProject == $item['id_project'] && !$item['id_company']) {
- if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
- return $item;
- }
- }
- }
- return null;
- }
- public static function getPrice($idType, $idProject = 0, $idCompany = 0, $jednostka = '') {
- $cennikRaw = self::_getPrices($idType);
- DBG::_('DBG', '>1', 'prices cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
- $price = 0;
- foreach ($cennikRaw as $item) {// fetch price from Oferta Admin
- if (!$item['id_project'] && !$item['id_company']) {
- if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
- $price = round($item['price'], 2);
- }
- }
- }
- DBG::_('DBG', '>1', 'default cennik: price', $price, __CLASS__, __FUNCTION__, __LINE__);
- foreach ($cennikRaw as $item) {// fetch price from Project Oferta
- if ($idProject == $item['id_project'] && !$item['id_company']) {
- if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
- $price = round($item['price'], 2);
- }
- }
- }
- DBG::_('DBG', '>1', 'project cennik: price', $price, __CLASS__, __FUNCTION__, __LINE__);
- return $price;
- }
- public static function _getPrices($idType, $idProject = 0, $idCompany = 0) {
- $schema = ProjectKosztorysSchema::getSchema();
- if (!array_key_exists($idType, $schema['config']['type'])) throw new Exception("Missing Type in schema ([{$idType}])");
- $sqlIdType = DB::getPDO()->quote($idType, PDO::PARAM_INT);
- $sqlFiltrProject = "";
- if ($idProject > 0) {
- $sqlIdProject = DB::getPDO()->quote($idProject, PDO::PARAM_INT);
- $sqlFiltrProject = "and (o.ID_PROJECT = 0 or o.ID_PROJECT = {$sqlIdProject})";
- }
- $cennikRaw = DB::getPDO()->fetchAll("
- 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 o
- where o.CRM_LISTA_ZASOBOW_ID = {$sqlIdType}
- and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
- and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
- {$sqlFiltrProject}
- ");
- DBG::_('DBG', '>1', 'cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
- return $cennikRaw;
- }
- }
|