ProjectKosztorysCennik.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. Lib::loadClass('ProjectKosztorysSchema');
  3. class ProjectKosztorysCennik {
  4. /**
  5. * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
  6. */
  7. public static function getCennik($idProject, $idCompany = 0) {
  8. $schema = ProjectKosztorysSchema::getSchema();
  9. $typeIdList = array_keys($schema['config']['type']);
  10. $sqlTypeIdList = implode(',', $typeIdList);
  11. $pdo = DB::getPDO();
  12. $sth = $pdo->prepare("
  13. select o.ID
  14. , o.CRM_LISTA_ZASOBOW_ID as id_zasob
  15. , o.COMPANIES_ID as id_company
  16. , o.ID_PROJECT as id_project
  17. , o.OFFER_PRICE_PER_RESOURCE_UNIT as price
  18. , o.RESOURCE_UNIT_TYPE as unit
  19. -- , o.OFFER_UNIT_TYPE as unit
  20. , o.REQUIRED_RESOURCE_UNITS as quantity
  21. from CRM_LISTA_ZASOBOW_OFFERS o
  22. where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
  23. and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
  24. and o.ID_PROJECT = :id_project
  25. and o.COMPANIES_ID = :id_company
  26. and o.RESOURCE_UNIT_TYPE != 'ROBOCIZNA'
  27. and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
  28. ");
  29. $sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  30. $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  31. $sth->execute();
  32. $cennikRaw = $sth->fetchAll();
  33. $cennik = array();
  34. foreach ($cennikRaw as $itemRaw) {
  35. $item = $itemRaw;
  36. $item['price'] = round($item['price'], 2);
  37. $cennik[$itemRaw['id_zasob']] = $item;
  38. }
  39. return $cennik;
  40. }
  41. /**
  42. * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
  43. */
  44. public static function getWorkCennik($idProject, $idCompany = 0) {
  45. $schema = ProjectKosztorysSchema::getSchema();
  46. $typeIdList = array_keys($schema['config']['type']);
  47. $sqlTypeIdList = implode(',', $typeIdList);
  48. $pdo = DB::getPDO();
  49. $sth = $pdo->prepare("
  50. select o.ID
  51. , o.CRM_LISTA_ZASOBOW_ID as id_zasob
  52. , o.COMPANIES_ID as id_company
  53. , o.ID_PROJECT as id_project
  54. , o.OFFER_PRICE_PER_RESOURCE_UNIT as price
  55. , o.RESOURCE_UNIT_TYPE as unit
  56. -- , o.OFFER_UNIT_TYPE as unit
  57. , o.REQUIRED_RESOURCE_UNITS as quantity
  58. from CRM_LISTA_ZASOBOW_OFFERS o
  59. where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
  60. and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
  61. and o.ID_PROJECT = :id_project
  62. and o.COMPANIES_ID = :id_company
  63. and o.RESOURCE_UNIT_TYPE = 'ROBOCIZNA'
  64. and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
  65. ");
  66. $sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  67. $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  68. $sth->execute();
  69. $robociznaCennikRaw = $sth->fetchAll();
  70. $robociznaCennik = array();
  71. foreach ($robociznaCennikRaw as $itemRaw) {
  72. $item = $itemRaw;
  73. $item['price'] = round($item['price'], 2);
  74. $robociznaCennik[$itemRaw['id_zasob']] = $item;
  75. }
  76. return $robociznaCennik;
  77. }
  78. /**
  79. * @returns [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
  80. */
  81. public static function getDefaultCennik($idCompany = 0) {
  82. $schema = ProjectKosztorysSchema::getSchema();
  83. $typeIdList = array_keys($schema['config']['type']);
  84. $sqlTypeIdList = implode(',', $typeIdList);
  85. $pdo = DB::getPDO();
  86. $sth = $pdo->prepare("
  87. select o.ID
  88. , o.CRM_LISTA_ZASOBOW_ID as id_zasob
  89. , o.COMPANIES_ID as id_company
  90. , o.ID_PROJECT as id_project
  91. , o.OFFER_PRICE_PER_RESOURCE_UNIT as price
  92. , o.RESOURCE_UNIT_TYPE as unit
  93. -- , o.OFFER_UNIT_TYPE as unit
  94. , o.REQUIRED_RESOURCE_UNITS as quantity
  95. from CRM_LISTA_ZASOBOW_OFFERS o
  96. where o.CRM_LISTA_ZASOBOW_ID in({$sqlTypeIdList})
  97. and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
  98. and o.ID_PROJECT = 0
  99. and o.COMPANIES_ID = :id_company
  100. ");
  101. $sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  102. $sth->execute();
  103. $cennikRaw = $sth->fetchAll();
  104. $cennik = array();
  105. foreach ($cennikRaw as $itemRaw) {
  106. $item = $itemRaw;
  107. $item['price'] = round($item['price'], 2);
  108. if (!empty($cennik[$itemRaw['id_zasob']])) {
  109. if ($itemRaw['ID'] < $cennik[$itemRaw['id_zasob']]['ID']) continue;
  110. }
  111. $cennik[$item['id_zasob']] = $item;
  112. }
  113. return $cennik;
  114. }
  115. public static function getTypeLabel($idType) {
  116. $schema = ProjectKosztorysSchema::getSchema();
  117. if (!array_key_exists($idType, $schema['config']['type'])) throw new Exception("Missing Type in schema ([{$idType}])");
  118. DBG::_('DBG', '>1', 'type', $schema['config']['type'][$idType], __CLASS__, __FUNCTION__, __LINE__);
  119. return V::get($idType, "[$idType]", $schema['config']['type']);
  120. }
  121. public static function updatePriceProjectCennik($idType, $idProject, $price, $jednostka) {
  122. $item = self::getItemProjectCennik($idType, $idProject, $jednostka);
  123. DBG::log($item, 'array', "item to update price '{$price}' for (\$idType: {$idType}, \$idProject: {$idProject}, \$jednostka: {$jednostka})");
  124. $usrLogin = User::getLogin();
  125. $sqlObj = array();
  126. $sqlObj['CRM_LISTA_ZASOBOW_ID'] = $idType;
  127. $sqlObj['OFFER_PRICE_PER_RESOURCE_UNIT'] = $price;
  128. $sqlObj['RESOURCE_UNIT_TYPE'] = $jednostka;
  129. $sqlObj['OFFER_UNIT_TYPE'] = $jednostka;
  130. if (empty($item)) {
  131. $sqlObj['COMPANIES_ID'] = 0;
  132. $sqlObj['ID_PROJECT'] = $idProject;
  133. $sqlObj['REQUIRED_RESOURCE_UNITS'] = 0;
  134. $idInserted = DB::getDB()->ADD_NEW_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  135. if ($idInserted <= 0) throw new Exception("Nie udało się utworzyć rekordu");
  136. } else {
  137. $sqlObj['ID'] = $item['ID'];
  138. $affected = DB::getDB()->UPDATE_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  139. if ($affected < 0) throw new Exception("Nie udało się zaktualizować rekordu");
  140. }
  141. }
  142. public static function updatePriceDefaultCennik($idType, $idCompany, $price, $jednostka) {
  143. $item = self::getItemDefaultCennik($idType, $idCompany, $jednostka);
  144. $usrLogin = User::getLogin();
  145. $sqlObj = array();
  146. $sqlObj['CRM_LISTA_ZASOBOW_ID'] = $idType;
  147. $sqlObj['OFFER_PRICE_PER_RESOURCE_UNIT'] = $price;
  148. $sqlObj['RESOURCE_UNIT_TYPE'] = $jednostka;
  149. $sqlObj['OFFER_UNIT_TYPE'] = $jednostka;
  150. if (empty($item)) {
  151. $sqlObj['COMPANIES_ID'] = $idCompany;
  152. $sqlObj['ID_PROJECT'] = 0;
  153. $sqlObj['REQUIRED_RESOURCE_UNITS'] = 0;
  154. $idInserted = DB::getDB()->ADD_NEW_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  155. if ($idInserted <= 0) throw new Exception("Nie udało się utworzyć rekordu");
  156. } else {
  157. $sqlObj['ID'] = $item['ID'];
  158. $affected = DB::getDB()->UPDATE_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  159. if ($affected < 0) throw new Exception("Nie udało się zaktualizować rekordu");
  160. }
  161. }
  162. public static function getPriceDefaultCennik($idType, $idCompany = 0, $jednostka = 'zasob') {
  163. $item = self::getItemDefaultCennik($idType, $idCompany, $jednostka);
  164. return (!empty($item)) ? round($item['price'], 2) : 0;
  165. }
  166. public static function getItemDefaultCennik($idType, $idCompany = 0, $jednostka = 'zasob') {
  167. $cennikRaw = self::_getPrices($idType);
  168. foreach ($cennikRaw as $item) {// fetch price from Oferta Admin
  169. if ($item['id_project']) continue;
  170. if (!empty($idCompany) && !empty($item['id_company']) && $idCompany != $item['id_company']) continue;
  171. if (!empty($idCompany) && empty($item['id_company'])) continue;
  172. if (empty($idCompany) && !empty($item['id_company'])) continue;
  173. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  174. return $item;
  175. }
  176. }
  177. return null;
  178. }
  179. public static function getPriceProjectCennik($idType, $idProject, $jednostka) {
  180. $item = self::getItemProjectCennik($idType, $idProject, $jednostka);
  181. return (!empty($item)) ? round($item['price'], 2) : 0;
  182. }
  183. public static function getItemProjectCennik($idType, $idProject, $jednostka) {
  184. $cennikRaw = self::_getPrices($idType, $idProject);
  185. foreach ($cennikRaw as $item) {// fetch price from Project Oferta
  186. if ($idProject == $item['id_project'] && !$item['id_company']) {
  187. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  188. return $item;
  189. }
  190. }
  191. }
  192. return null;
  193. }
  194. public static function getPrice($idType, $idProject = 0, $idCompany = 0, $jednostka = '') {
  195. $cennikRaw = self::_getPrices($idType);
  196. if (empty($cennikRaw)) return 0;
  197. DBG::_('DBG', '>1', 'prices cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
  198. $price = 0;
  199. $filteredCennik = $cennikRaw;
  200. DBG::log($filteredCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) all prices rows");
  201. $filteredCennik = array_filter($filteredCennik, function ($item) use ($jednostka) {
  202. return (empty($jednostka) || (!empty($jednostka) && $jednostka === $item['unit']));
  203. });
  204. DBG::log($filteredCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) prices filtered by unit");
  205. if (empty($filteredCennik)) return 0;
  206. $adminCennik = array_filter($filteredCennik, function ($item) {
  207. return (!$item['id_project'] && !$item['id_company']);
  208. });
  209. DBG::log($adminCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) filtered admin prices");
  210. if (!empty($adminCennik)) {
  211. foreach ($adminCennik as $item) {
  212. $price = round($item['price'], 2);
  213. }
  214. }
  215. if ($idProject === 0 && $idCompany === 0) {
  216. return $price; // shold be in $adminCennik
  217. }
  218. else if ($idProject > 0 && $idCompany === 0) {
  219. $filteredCennik = array_filter($filteredCennik, function ($item) {
  220. return ($item['id_project'] > 0 && !$item['id_company']);
  221. });
  222. DBG::log($filteredCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) prices filtered by proj > 0 && com == 0");
  223. }
  224. else if ($idProject === 0 && $idCompany > 0) {
  225. $filteredCennik = array_filter($filteredCennik, function ($item) {
  226. return (!$item['id_project'] && $item['id_company'] > 0);
  227. });
  228. DBG::log($filteredCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) prices filtered by proj == 0 && com > 0");
  229. }
  230. else if ($idProject > 0 && $idCompany > 0) {
  231. $filteredCennik = array_filter($filteredCennik, function ($item) {
  232. return ($item['id_project'] > 0 && $item['id_company'] > 0);
  233. });
  234. DBG::log($filteredCennik, 'array', "getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) prices filtered by proj > 0 && com > 0");
  235. }
  236. if (empty($filteredCennik)) return $price;
  237. foreach ($filteredCennik as $item) {
  238. $price = round($item['price'], 2);
  239. }
  240. DBG::log("getPrice({$idType}, {$idProject}, {$idCompany}, {$jednostka}) returns '{$price}'");
  241. return $price;
  242. }
  243. public static function _getPrices($idType, $idProject = 0, $idCompany = 0) {
  244. $schema = ProjectKosztorysSchema::getSchema();
  245. if (!array_key_exists($idType, $schema['config']['type'])) throw new Exception("Missing Type in schema ([{$idType}])");
  246. $sqlIdType = DB::getPDO()->quote($idType, PDO::PARAM_INT);
  247. $sqlFiltrProject = "";
  248. if ($idProject > 0) {
  249. $sqlIdProject = DB::getPDO()->quote($idProject, PDO::PARAM_INT);
  250. $sqlFiltrProject = "and (o.ID_PROJECT = 0 or o.ID_PROJECT = {$sqlIdProject})";
  251. }
  252. $cennikRaw = DB::getPDO()->fetchAll("
  253. select o.ID
  254. , o.CRM_LISTA_ZASOBOW_ID as id_zasob
  255. , o.COMPANIES_ID as id_company
  256. , o.ID_PROJECT as id_project
  257. , o.OFFER_PRICE_PER_RESOURCE_UNIT as price
  258. , o.RESOURCE_UNIT_TYPE as unit
  259. -- , o.OFFER_UNIT_TYPE as unit
  260. , o.REQUIRED_RESOURCE_UNITS as quantity
  261. from CRM_LISTA_ZASOBOW_OFFERS o
  262. where o.CRM_LISTA_ZASOBOW_ID = {$sqlIdType}
  263. and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
  264. and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
  265. {$sqlFiltrProject}
  266. ");
  267. DBG::_('DBG', '>1', 'cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
  268. return $cennikRaw;
  269. }
  270. }