ProjectKosztorysCennik.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. $usrLogin = User::getLogin();
  124. $sqlObj = array();
  125. $sqlObj['CRM_LISTA_ZASOBOW_ID'] = $idType;
  126. $sqlObj['OFFER_PRICE_PER_RESOURCE_UNIT'] = $price;
  127. $sqlObj['RESOURCE_UNIT_TYPE'] = $jednostka;
  128. $sqlObj['OFFER_UNIT_TYPE'] = $jednostka;
  129. if (empty($item)) {
  130. $sqlObj['COMPANIES_ID'] = 0;
  131. $sqlObj['ID_PROJECT'] = $idProject;
  132. $sqlObj['REQUIRED_RESOURCE_UNITS'] = 0;
  133. $idInserted = DB::getDB()->ADD_NEW_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  134. if ($idInserted <= 0) throw new Exception("Nie udało się utworzyć rekordu");
  135. } else {
  136. $sqlObj['ID'] = $item['ID'];
  137. $affected = DB::getDB()->UPDATE_OBJ('CRM_LISTA_ZASOBOW_OFFERS', (object)$sqlObj);
  138. if ($affected < 0) throw new Exception("Nie udało się zaktualizować rekordu");
  139. }
  140. }
  141. public static function getPriceDefaultCennik($idType) {
  142. $item = self::getItemDefaultCennik($idType);
  143. return (!empty($item)) ? round($item['price'], 2) : 0;
  144. }
  145. public static function getItemDefaultCennik($idType) {
  146. $cennikRaw = self::_getPrices($idType);
  147. foreach ($cennikRaw as $item) {// fetch price from Oferta Admin
  148. if (!$item['id_company'] && !$item['id_project']) {
  149. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  150. return $item;
  151. }
  152. }
  153. }
  154. return null;
  155. }
  156. public static function getPriceProjectCennik($idType, $idProject, $jednostka) {
  157. $item = self::getItemProjectCennik($idType, $idProject, $jednostka);
  158. return (!empty($item)) ? round($item['price'], 2) : 0;
  159. }
  160. public static function getItemProjectCennik($idType, $idProject, $jednostka) {
  161. $cennikRaw = self::_getPrices($idType, $idProject);
  162. foreach ($cennikRaw as $item) {// fetch price from Project Oferta
  163. if ($idProject == $item['id_project'] && !$item['id_company']) {
  164. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  165. return $item;
  166. }
  167. }
  168. }
  169. return null;
  170. }
  171. public static function getPrice($idType, $idProject = 0, $idCompany = 0, $jednostka = '') {
  172. $cennikRaw = self::_getPrices($idType);
  173. DBG::_('DBG', '>1', 'prices cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
  174. $price = 0;
  175. foreach ($cennikRaw as $item) {// fetch price from Oferta Admin
  176. if (!$item['id_project'] && !$item['id_company']) {
  177. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  178. $price = round($item['price'], 2);
  179. }
  180. }
  181. }
  182. DBG::_('DBG', '>1', 'default cennik: price', $price, __CLASS__, __FUNCTION__, __LINE__);
  183. foreach ($cennikRaw as $item) {// fetch price from Project Oferta
  184. if ($idProject == $item['id_project'] && !$item['id_company']) {
  185. if (empty($jednostka) || (!empty($jednostka) && $jednostka == $item['unit'])) {
  186. $price = round($item['price'], 2);
  187. }
  188. }
  189. }
  190. DBG::_('DBG', '>1', 'project cennik: price', $price, __CLASS__, __FUNCTION__, __LINE__);
  191. return $price;
  192. }
  193. public static function _getPrices($idType, $idProject = 0, $idCompany = 0) {
  194. $schema = ProjectKosztorysSchema::getSchema();
  195. if (!array_key_exists($idType, $schema['config']['type'])) throw new Exception("Missing Type in schema ([{$idType}])");
  196. $sqlIdType = DB::getPDO()->quote($idType, PDO::PARAM_INT);
  197. $sqlFiltrProject = "";
  198. if ($idProject > 0) {
  199. $sqlIdProject = DB::getPDO()->quote($idProject, PDO::PARAM_INT);
  200. $sqlFiltrProject = "and (o.ID_PROJECT = 0 or o.ID_PROJECT = {$sqlIdProject})";
  201. }
  202. $cennikRaw = DB::getPDO()->fetchAll("
  203. select o.ID
  204. , o.CRM_LISTA_ZASOBOW_ID as id_zasob
  205. , o.COMPANIES_ID as id_company
  206. , o.ID_PROJECT as id_project
  207. , o.OFFER_PRICE_PER_RESOURCE_UNIT as price
  208. , o.RESOURCE_UNIT_TYPE as unit
  209. -- , o.OFFER_UNIT_TYPE as unit
  210. , o.REQUIRED_RESOURCE_UNITS as quantity
  211. from CRM_LISTA_ZASOBOW_OFFERS o
  212. where o.CRM_LISTA_ZASOBOW_ID = {$sqlIdType}
  213. and (o.A_STATUS is null or o.A_STATUS not in ('DELETED'))
  214. and o.OFFER_PRICE_PER_RESOURCE_UNIT >= 0
  215. {$sqlFiltrProject}
  216. ");
  217. DBG::_('DBG', '>1', 'cennikRaw', $cennikRaw, __CLASS__, __FUNCTION__, __LINE__);
  218. return $cennikRaw;
  219. }
  220. }