ProjektyKosztyWstepnychRobot.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('ProjectKosztorysSchema');
  4. Lib::loadClass('ProjectKosztorysModel');
  5. Lib::loadClass('ProjectKosztorysCennik');
  6. Lib::loadClass('UI');
  7. Lib::loadClass('Response');
  8. class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {// TODO: UrlActionBase @see Route_UrlAction
  9. public $_model = array();
  10. public function handleAuth() {
  11. if (!User::logged()) {
  12. User::authByRequest();
  13. }
  14. }
  15. public function defaultAction() {
  16. // TODO: check if user is allowed to run this action
  17. UI::gora();
  18. if (1 != V::get('_print', '', $_GET)) UI::menu();
  19. try {
  20. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  21. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  22. if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
  23. $this->panel($idProject, $idCompany);
  24. $this->kosztorys($idProject, $idCompany);
  25. } catch (Exception $e) {
  26. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  27. }
  28. UI::dol();
  29. }
  30. public function przedmiarAction() {
  31. // TODO: check if user is allowed to run this action
  32. UI::gora();
  33. if (1 != V::get('_print', '', $_GET)) UI::menu();
  34. try {
  35. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  36. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  37. if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
  38. $this->panel($idProject, $idCompany);
  39. $this->przedmiar($idProject, $idCompany);
  40. } catch (Exception $e) {
  41. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  42. }
  43. UI::dol();
  44. }
  45. public function zestawienieSwMikRurAction() {
  46. UI::gora();
  47. if (1 != V::get('_print', '', $_GET)) UI::menu();
  48. try {
  49. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  50. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  51. if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");
  52. $this->panel($idProject, $idCompany);
  53. $this->zestawienieSwMikRur($idProject, $idCompany);
  54. } catch (Exception $e) {
  55. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  56. }
  57. UI::dol();
  58. }
  59. public function ofertaAdminAction() {
  60. // TODO: check if user is allowed to run this action
  61. UI::gora();
  62. UI::menu();
  63. try {
  64. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  65. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  66. $admin = true;
  67. $ofertaArgs = compact('idProject', 'idCompany', 'admin');
  68. $this->panel($idProject, $idCompany);
  69. if ($idProject > 0) {
  70. $this->oferta($ofertaArgs);
  71. } else {
  72. $this->defaultOferta();
  73. }
  74. } catch (Exception $e) {
  75. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  76. }
  77. UI::dol();
  78. }
  79. public function ofertaCompanyAction() {
  80. // TODO: check if user is allowed to run this action
  81. UI::gora();
  82. UI::menu();
  83. try {
  84. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  85. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');// TODO: $idCompany from ADMIN_USERS
  86. $admin = false;
  87. $companyAdmin = true;
  88. $ofertaArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin');
  89. $this->panel($idProject, $idCompany);
  90. if ($idProject > 0) {
  91. $this->oferta($ofertaArgs);
  92. } else {
  93. $this->defaultOferta();
  94. }
  95. } catch (Exception $e) {
  96. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  97. }
  98. UI::dol();
  99. }
  100. public function ofertaDefaultAdminAction() {
  101. // TODO: check if user is allowed to run this action
  102. UI::gora();
  103. UI::menu();
  104. try {
  105. $idProject = 0;
  106. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  107. $this->panel($idProject, $idCompany);
  108. $this->defaultOferta();
  109. } catch (Exception $e) {
  110. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  111. }
  112. UI::dol();
  113. }
  114. public function ofertaAction() {
  115. // TODO: check if user is allowed to run this action
  116. UI::gora();
  117. UI::menu();
  118. try {
  119. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  120. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  121. $admin = false;
  122. if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");// TODO: show select box if not defined
  123. $ofertaArgs = compact('idProject', 'idCompany', 'admin');
  124. $this->panel($idProject, $idCompany);
  125. $this->oferta($ofertaArgs);
  126. } catch (Exception $e) {
  127. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  128. }
  129. UI::dol();
  130. }
  131. public function getArgsList() {// TODO: override UrlActionBase::getArgsList action
  132. $args = array();
  133. $args[] = 'ID_PROJECT';
  134. return $args;
  135. }
  136. public function reinstallAction() {// TODO: mv to UrlActionBase
  137. $jsonData = new stdClass();
  138. $jsonData->type = 'success';
  139. $jsonData->msg = 'Gotowe';
  140. try {
  141. $this->reinstall();
  142. } catch (Exception $e) {
  143. $jsonData->type = 'danger';
  144. $jsonData->msg = $e->getMessage();
  145. }
  146. echo json_encode($jsonData);
  147. }
  148. public function reinstall() {// TODO: mv struct check to UrlActionBase
  149. $pdo = DB::getPDO();
  150. {
  151. $dropGDistanceFuncion = "DROP FUNCTION IF EXISTS `GDistance`";
  152. $pdo->exec($dropGDistanceFuncion);
  153. // Test for GDistance:
  154. // select '' as test, GDistance('') as GDistance, IF(GDistance('') = 0, 1, 0) as test_ok;
  155. // select 'NULL' as test, GDistance(NULL) as GDistance, IF(GDistance(NULL) = 0, 1, 0) as test_ok;
  156. // select 'GeomFromText(POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951)))' as test, GDistance(GeomFromText('POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951))')) as GDistance, IF(1 = GDistance(GeomFromText('POLYGON((18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799,18.5617610722586 54.346524689865,18.5619728898979 54.3488528136092,18.5609743210268 54.3496376437764,18.5600057772984 54.3494578097971,18.559603951991 54.350742572617,18.5590637025025 54.3507194967965,18.5578575757622 54.349434823939,18.5573431614953 54.3472831082951))')), 1, 0) as test_ok;
  157. // select 'GeomFromText(LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799))' as test, GDistance(GeomFromText('LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799)')) as GDistance, IF(GDistance(GeomFromText('LINESTRING(18.5573431614953 54.3472831082951,18.5612466579916 54.3460308284799)')) is not null, 1, 0) as test_ok;
  158. // select 'GeomFromText(POINT(18.5573431614953 54.3472831082951))' as test, GDistance(GeomFromText('POINT(18.5573431614953 54.3472831082951)')) as GDistance, IF(GDistance(GeomFromText('POINT(18.5573431614953 54.3472831082951)')) is not null, 1, 0) as test_ok;
  159. $gDistanceFuncion = <<<SQL_FUN
  160. CREATE DEFINER=`root`@`localhost` FUNCTION `GDistance`(`LS` LINESTRING) RETURNS double NO SQL
  161. BEGIN
  162. DECLARE LON_A, LAT_A, LON_B, LAT_B, R, RESULT DOUBLE;
  163. DECLARE A, B POINT;
  164. DECLARE I, N INT;
  165. -- if LS is not geometry type then return 0
  166. -- if GeometryType is not 'LINESTRING' then return 1
  167. -- if GeometryType is 'LINESTRING' then return length
  168. IF LS is null or GeometryType(LS) is null THEN
  169. RETURN 0;
  170. END IF;
  171. IF 'LINESTRING' != GeometryType(LS) THEN
  172. RETURN 1;
  173. END IF;
  174. SET R = 6372795.477598;
  175. SET N = NUMPOINTS(LS);
  176. IF N is null or N = 1 THEN
  177. RETURN 1;
  178. END IF;
  179. SET RESULT = 0;
  180. SET I = 1;
  181. WHILE I < N DO
  182. SET A = POINTN(LS, I);
  183. SET B = POINTN(LS, I + 1);
  184. SET LON_A = RADIANS(X(A));
  185. SET LAT_A = RADIANS(Y(A));
  186. SET LON_B = RADIANS(X(B));
  187. SET LAT_B = RADIANS(Y(B));
  188. SET RESULT = RESULT + ACOS(SIN(LAT_A) * SIN(LAT_B) + COS(LAT_A) * COS(LAT_B) * COS(LON_A - LON_B));
  189. SET I = I + 1;
  190. END WHILE;
  191. SET RESULT = RESULT * R;
  192. RETURN RESULT;
  193. END
  194. SQL_FUN;
  195. $pdo->exec($gDistanceFuncion);
  196. }
  197. /* required Zasoby tree structure - XML (parent rel in PARENT_ID field):
  198. <zasob:tree>
  199. <zasob:TYPESPECIALS>
  200. <zasob:URL_ACTION zasob:key=":action_id" zasob:desc=":url_action_name">
  201. <zasob:PARAM_IN zasob:key=":action_param_1_id" zasob:desc=":url_action_param_1_name"/>
  202. </zasob:URL_ACTION>
  203. </zasob:TYPESPECIALS>
  204. <zasob:BAZA_DANYCH zasob:key=":main_db_id" altername_types="DATABASE_MYSQL,DATABASE_POSTGRESQL">
  205. <zasob:TABELA zasob:key=":tbl_id" zasob:desc="IN7_MK_BAZA_DYSTRYBUCJI">
  206. <zasob:KOMORKA zasob:key=":cell_id" zasob:desc="ID"/>
  207. <zasob:URL_ACTION zasob:key=":link_action_id" zasob:desc=":url_action_name" zasob:alias_id=":action_id">
  208. <zasob:PARAM_IN zasob:key=":cell_id" zasob:desc="ID_PROJECT" zasob:alias_id=":cell_id"/>
  209. </zasob:URL_ACTION>
  210. </zasob:TABELA>
  211. </zasob:BAZA_DANYCH>
  212. </zasob:tree>
  213. */
  214. /* required Zasoby tree structure:
  215. - required base structure
  216. [:ts_main_id] TYPESPECIALS
  217. [:action_id] URL_ACTION :url_action_name
  218. [:action_param_1_id] PARAM_IN :url_action_param_1_name
  219. - current function from schema
  220. [:main_db_id] (DATABASE_MYSQL, BAZA_DANYCH, DATABASE_POSTGRESQL)
  221. [:tbl_id] TABELA 'IN7_MK_BAZA_DYSTRYBUCJI'
  222. [:cell_id] KOMORKA 'ID'
  223. [:link_action_id] (ALIAS DO :action_id) URL_ACTION :url_action_name
  224. [:link_param_id] (ALIAS DO :cell_id) PARAM_IN :url_action_param_1_name
  225. :url_action_name = 'ProjektyKosztyWstepnychRobot'
  226. :url_action_param_1_name = 'ID_PROJECT'
  227. */
  228. $flatConfig_main = array();
  229. {
  230. $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS)
  231. 'query' => "
  232. select z.ID from CRM_LISTA_ZASOBOW z
  233. where z.TYPE='TYPESPECIALS'
  234. and z.A_STATUS not in('DELETED')
  235. ",
  236. 'createTable' => 'CRM_LISTA_ZASOBOW',
  237. 'createArgs' => array('TYPE' => 'TYPESPECIALS', 'DESC' => 'Typespecials'),
  238. 'out' => array(':ts_main_id' => 'ID')
  239. );
  240. $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS/:url_action_name)
  241. 'query' => "
  242. select z.ID from CRM_LISTA_ZASOBOW z
  243. where z.PARENT_ID = :ts_main_id
  244. and z.TYPE = 'URL_ACTION'
  245. and z.DESC = :url_action_name
  246. and z.A_STATUS not in('DELETED')
  247. ",
  248. 'createTable' => 'CRM_LISTA_ZASOBOW',
  249. 'createArgs' => array('TYPE' => 'URL_ACTION', 'PARENT_ID' => ':ts_main_id', 'DESC' => ':url_action_name'),
  250. 'in' => array(':ts_main_id', ':url_action_name'),
  251. 'out' => array(':action_id' => 'ID')
  252. );
  253. $flatConfig_main[] = (object)array(// Zasob::assert(TYPESPECIALS/:url_action_name/:url_action_param_1_name)
  254. 'query' => "
  255. select z.ID from CRM_LISTA_ZASOBOW z
  256. where z.PARENT_ID = :action_id
  257. and z.TYPE = 'PARAM_IN'
  258. and z.DESC = :url_action_param_1_name
  259. and z.A_STATUS not in('DELETED')
  260. ",
  261. 'createTable' => 'CRM_LISTA_ZASOBOW',
  262. 'createArgs' => array('TYPE' => 'PARAM_IN', 'PARENT_ID' => ':action_id', 'DESC' => ':url_action_param_1_name'),
  263. 'in' => array(':action_id', ':url_action_param_1_name'),
  264. 'out' => array(':action_param_1_id' => 'ID')
  265. );
  266. }
  267. //DBG::_('DBG', '>1', "flatConfig_main", $flatConfig_main, __CLASS__, __FUNCTION__, __LINE__);
  268. $flatConfig_link_in_projekt = array();
  269. {
  270. $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db)
  271. 'query' => "
  272. select z.ID from CRM_LISTA_ZASOBOW z
  273. where z.ID = :main_db_id
  274. and z.TYPE in('DATABASE_MYSQL', 'BAZA_DANYCH', 'DATABASE_POSTGRESQL')
  275. and z.A_STATUS not in('DELETED')
  276. ",
  277. 'createTable' => 'CRM_LISTA_ZASOBOW',
  278. 'createArgs' => array('TYPE' => 'BAZA_DANYCH', 'DESC' => 'Baza danych'),
  279. 'in' => array(':main_db_id'),
  280. 'out' => array(':main_db_id' => 'ID')
  281. );
  282. $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI)
  283. 'query' => "
  284. select z.ID from CRM_LISTA_ZASOBOW z
  285. where z.PARENT_ID = :main_db_id
  286. and z.TYPE = 'TABELA'
  287. and z.DESC = 'IN7_MK_BAZA_DYSTRYBUCJI'
  288. and z.A_STATUS not in('DELETED')
  289. ",
  290. 'createTable' => 'CRM_LISTA_ZASOBOW',
  291. 'createArgs' => array('TYPE' => 'TABELA', 'PARENT_ID' => ':main_db_id', 'DESC' => 'IN7_MK_BAZA_DYSTRYBUCJI'),
  292. 'in' => array(':main_db_id'),
  293. 'out' => array(':tbl_id' => 'ID')
  294. );
  295. $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/ID)
  296. 'query' => "
  297. select z.ID from CRM_LISTA_ZASOBOW z
  298. where z.PARENT_ID = :tbl_id
  299. and z.TYPE = 'KOMORKA'
  300. and z.DESC = 'ID'
  301. and z.A_STATUS not in('DELETED')
  302. ",
  303. 'createTable' => 'CRM_LISTA_ZASOBOW',
  304. 'createArgs' => array('TYPE' => 'KOMORKA', 'PARENT_ID' => ':tbl_id', 'DESC' => 'ID'),
  305. 'in' => array(':tbl_id'),
  306. 'out' => array(':cell_id' => 'ID')
  307. );
  308. $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/:url_action_name)
  309. 'query' => "
  310. select z.ID from CRM_LISTA_ZASOBOW z
  311. where z.PARENT_ID = :tbl_id
  312. and z.ALIAS_ID = ':action_id'
  313. and z.TYPE = 'URL_ACTION'
  314. and z.DESC = ':url_action_name'
  315. and z.A_STATUS not in('DELETED')
  316. ",
  317. 'createTable' => 'CRM_LISTA_ZASOBOW',
  318. 'createArgs' => array('TYPE' => 'URL_ACTION', 'PARENT_ID' => ':tbl_id', 'ALIAS_ID' => ':action_id', 'DESC' => ':url_action_name'),
  319. 'in' => array(':tbl_id', ':action_id', ':url_action_name'),
  320. 'out' => array(':link_action_id' => 'ID')
  321. );
  322. // [:link_param_id] (ALIAS DO :cell_id) PARAM_IN :url_action_param_1_name
  323. $flatConfig_link_in_projekt[] = (object)array(// Zasob::assert(default_db/IN7_MK_BAZA_DYSTRYBUCJI/:url_action_name/:url_action_param_1_name)
  324. 'query' => "
  325. select z.ID from CRM_LISTA_ZASOBOW z
  326. where z.PARENT_ID = :link_action_id
  327. and z.ALIAS_ID = ':cell_id'
  328. and z.TYPE = 'PARAM_IN'
  329. and z.DESC = ':url_action_param_1_name'
  330. and z.A_STATUS not in('DELETED')
  331. ",
  332. 'createTable' => 'CRM_LISTA_ZASOBOW',
  333. 'createArgs' => array('TYPE' => 'PARAM_IN', 'PARENT_ID' => ':link_action_id', 'ALIAS_ID' => ':cell_id', 'DESC' => ':url_action_param_1_name'),
  334. 'in' => array(':cell_id', ':link_action_id', ':url_action_param_1_name'),
  335. 'out' => array(':link_action_id' => 'ID')
  336. );
  337. }
  338. //DBG::_('DBG', '>1', "flatConfig_link_in_projekt", $flatConfig_link_in_projekt, __CLASS__, __FUNCTION__, __LINE__);
  339. $flatConf = array_merge($flatConfig_main, $flatConfig_link_in_projekt);
  340. $knownArgs = array();
  341. $knownArgs[':url_action_name'] = 'Test';
  342. $knownArgs[':url_action_param_1_name'] = 'ID_PROJECT';
  343. $knownArgs[':main_db_id'] = '36';// from DB::getDB()->getZasobId(); or DB::getPDO()->getZasobId();
  344. $this->_debugFlatConfig($flatConf, $knownArgs);
  345. $args = $this->getArgsList();
  346. $clsName = __CLASS__;
  347. $urlActionName = str_replace('Route_UrlAction_', '', $clsName);
  348. DBG::_('DBG', '>1', "reinstall class", __CLASS__, __CLASS__, __FUNCTION__, __LINE__);
  349. DBG::_('DBG', '>1', "reinstall funName", $urlActionName, __CLASS__, __FUNCTION__, __LINE__);
  350. DBG::_('DBG', '>1', "reinstall test", $args, __CLASS__, __FUNCTION__, __LINE__);
  351. $dbFunction = $this->fetchZasobInfo();
  352. $usrLogin = User::getLogin();
  353. {// check if URL_ACTION already exists
  354. if (!$dbFunction) throw new Exception("Brak id zasobu typu TYPESPECIALS - TODO: CREATE");// $this->createZasobTypespecials();
  355. DBG::_('DBG', '>1', "dbFunction", $dbFunction, __CLASS__, __FUNCTION__, __LINE__);
  356. if ($dbFunction['action_id_is_null']) {
  357. $sth = $pdo->prepare("
  358. insert into CRM_LISTA_ZASOBOW (
  359. TYPE,
  360. PARENT_ID,
  361. `DESC`,
  362. A_RECORD_CREATE_AUTHOR,
  363. A_RECORD_CREATE_DATE
  364. )
  365. values (
  366. 'URL_ACTION',
  367. :ts_id,
  368. :url_action_name,
  369. 'system-by-{$usrLogin}',
  370. NOW()
  371. )
  372. ");
  373. //$sth->bindValue('ts_id', $dbFunction['ts_id'], PDO::PARAM_INT);
  374. //$sth->bindValue('url_action_name', $urlActionName, PDO::PARAM_STR);
  375. $bindValues = array();
  376. $bindValues['ts_id'] = array($dbFunction['ts_id'], PDO::PARAM_INT);
  377. $bindValues['url_action_name'] = $urlActionName;
  378. $pdo->bindValues($sth, $bindValues);
  379. DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
  380. $sth->execute();
  381. $dbFunction = $this->fetchZasobInfo();
  382. if (!$dbFunction || $dbFunction['action_id_is_null']) throw new Exception("Brak zasobu typu URL_ACTION, nie udało się go utworzyć!");
  383. }
  384. }
  385. {// check params
  386. if (!empty($args)) {
  387. $todoArgs = array();
  388. foreach ($args as $argName) {
  389. if (empty($dbFunction['url_params'])) {
  390. $todoArgs[$argName] = true;
  391. } else if (!array_key_exists($argName, $dbFunction['url_params'])) {
  392. $todoArgs[$argName] = true;
  393. }
  394. // OK PARAM_IN exists
  395. }
  396. $todoArgs = array_keys($todoArgs);
  397. DBG::_('DBG', '>1', "todoArgs", $todoArgs, __CLASS__, __FUNCTION__, __LINE__);
  398. if (!empty($todoArgs)) {
  399. $sqlValues = array();
  400. foreach ($todoArgs as $argName) {
  401. $sqlValues[] = "(
  402. 'PARAM_IN',
  403. {$dbFunction['action_id']},
  404. '{$argName}',
  405. 'system-by-{$usrLogin}',
  406. NOW()
  407. )
  408. ";
  409. }
  410. $sqlValues = implode(", ", $sqlValues);
  411. $sth = $pdo->prepare("
  412. insert into CRM_LISTA_ZASOBOW (
  413. TYPE,
  414. PARENT_ID,
  415. `DESC`,
  416. A_RECORD_CREATE_AUTHOR,
  417. A_RECORD_CREATE_DATE
  418. )
  419. values {$sqlValues}
  420. ");
  421. DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
  422. //$sth->execute();
  423. $dbFunction = $this->fetchZasobInfo();
  424. }
  425. if (empty($dbFunction['url_params'])) {
  426. throw new Exception("Brak zdefiniowanych parametrów, nie udało się ich utworzyć!");
  427. }
  428. }
  429. }
  430. {// check links in tables
  431. // $info['links']['IN7_MK_BAZA_DYSTRYBUCJI'] = $linkInfo;
  432. if (empty($dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'])) throw new Exception("Brak poprawne wprowadzonej tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' w drzewie zasobów!");
  433. $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'];
  434. /*
  435. [link_action_id] =>
  436. [db_id] => 36
  437. [tbl_id] => 636
  438. [cell_id] => 763
  439. [param1_id] =>
  440. */
  441. if (!$projInfo['link_action_id']) {
  442. $sth = $pdo->prepare("
  443. insert into CRM_LISTA_ZASOBOW (
  444. TYPE,
  445. PARENT_ID,
  446. ALIAS_ID,
  447. `DESC`,
  448. A_RECORD_CREATE_AUTHOR,
  449. A_RECORD_CREATE_DATE
  450. )
  451. values (
  452. 'URL_ACTION',
  453. {$projInfo['tbl_id']},
  454. {$dbFunction['action_id']},
  455. '{$urlActionName}',
  456. 'system-by-{$usrLogin}',
  457. NOW()
  458. )
  459. ");
  460. //DBG::_('DBG', '>1', "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
  461. $sth->execute();
  462. $dbFunction = $this->fetchZasobInfo();
  463. $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'];
  464. if (!$projInfo['link_action_id']) throw new Exception("Brak zainstalowanej akcji dla tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' - nie udało się zainstalować");
  465. }
  466. if (!$projInfo['param1_id']) {// TODO: get from params
  467. $sth = $pdo->prepare("
  468. insert into CRM_LISTA_ZASOBOW (
  469. TYPE,
  470. PARENT_ID,
  471. ALIAS_ID,
  472. `DESC`,
  473. A_RECORD_CREATE_AUTHOR,
  474. A_RECORD_CREATE_DATE
  475. )
  476. values (
  477. 'PARAM_IN',
  478. {$projInfo['link_action_id']},
  479. {$projInfo['cell_id']},
  480. 'ID_PROJECT',
  481. 'system-by-{$usrLogin}',
  482. NOW()
  483. )
  484. ");
  485. $sth->execute();
  486. $dbFunction = $this->fetchZasobInfo();
  487. $projInfo = $dbFunction['links']['IN7_MK_BAZA_DYSTRYBUCJI'];
  488. if (!$projInfo['param1_id']) throw new Exception("Brak zainstalowanego parametru 1 w akcji dla tabeli 'IN7_MK_BAZA_DYSTRYBUCJI' - nie udało się zainstalować");
  489. }
  490. }
  491. }
  492. public function _debugFlatConfig($flatConfig, $args = array()) {
  493. //DBG::_('DBG', '>1', "flatConfig", $flatConfig, __CLASS__, __FUNCTION__, __LINE__);
  494. echo '<pre style="border:1px solid green">';
  495. foreach ($flatConfig as $conf) {
  496. /*
  497. [query] => string
  498. [createTable] => srting - eg. CRM_LISTA_ZASOBOW
  499. [createArgs] => array - eg. [ TYPE: 'TYPESPECIALS', DESC: 'Typespecials' ]
  500. [in] => array - eg. [ ':ts_main_id', ':url_action_name' ]
  501. [out] => array - eg. [ ':ts_main_id': 'ID' ]
  502. */
  503. echo "Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
  504. }
  505. echo '</pre>';
  506. echo '<pre style="border:1px solid green">';
  507. echo "DBG: Simulate iter loop:\n";
  508. $simulateGeneratedId = 1000;
  509. $i = 0; $limit = 10; $todoIdx = array(); $knownArgsMap = array();
  510. foreach ($flatConfig as $idx => $conf) $todoIdx[] = $idx;
  511. foreach ($args as $arg => $val) $knownArgsMap[$arg] = $val;
  512. for ($i = 0; $i < $limit; $i++) {
  513. echo ">> DBG loop({$i}):\n";
  514. $isExecuted = false;
  515. foreach ($todoIdx as $idx) {
  516. $conf = $flatConfig[$idx];
  517. /*
  518. [query] => string
  519. [createTable] => srting - eg. CRM_LISTA_ZASOBOW
  520. [createArgs] => array - eg. [ TYPE: 'TYPESPECIALS', DESC: 'Typespecials' ]
  521. [in] => array - eg. [ ':ts_main_id', ':url_action_name' ]
  522. [out] => array - eg. [ ':ts_main_id': 'ID' ]
  523. */
  524. echo ">>>> Loop({$i}): idx({$idx}) Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
  525. $canExecute = true;
  526. if (!empty($conf->in)) foreach ($conf->in as $argName) {
  527. if (!array_key_exists($argName, $knownArgsMap)) {
  528. $canExecute = false;
  529. }
  530. }
  531. if ($canExecute) {
  532. // TODO: execute - $r = $this->_assertZasobConf($conf, $knownArgsMap);
  533. // fetch params out to $knownArgsMap
  534. foreach ($conf->out as $argName => $outFieldName) {
  535. $knownArgsMap[$argName] = $simulateGeneratedId++;// TODO: fetch from returned object ($r->{$outFieldName})
  536. }
  537. $todoIdx = array_diff($todoIdx, array($idx));// remove $idx from $todoIdx
  538. echo ">>>> Loop({$i}): knownArgsMap: " . json_encode($knownArgsMap) . " \n";
  539. $isExecuted = true;
  540. } else {
  541. echo ">>>> Loop({$i}): cannot execute conf [{$idx}] missing args: " . json_encode($conf->in) . " \n";
  542. }
  543. }
  544. if (!$isExecuted) {
  545. echo "DBG: Stop";
  546. if (empty($todoIdx)) {
  547. echo " - OK";
  548. } else {
  549. echo " - Error - TODO:\n";
  550. foreach ($todoIdx as $idx) {
  551. $conf = $flatConfig[$idx];
  552. echo "Zasob::assert({$conf->createTable}): " . json_encode($conf->createArgs) . " returns: " . json_encode($conf->out) . "\n";
  553. }
  554. }
  555. break;
  556. }
  557. }
  558. echo '</pre>';
  559. }
  560. public function fetchZasobInfo() {
  561. $pdo = DB::getPDO();
  562. $args = $this->getArgsList();
  563. $clsName = __CLASS__;
  564. $urlActionName = str_replace('Route_UrlAction_', '', $clsName);
  565. $sth = $pdo->prepare("
  566. select zp.ID as ts_id
  567. , IF(z.ID is null, 1, 0) as action_id_is_null
  568. , z.ID as action_id
  569. , z.DESC as action_desc
  570. from CRM_LISTA_ZASOBOW zp
  571. left join CRM_LISTA_ZASOBOW z on(z.PARENT_ID = zp.ID
  572. and z.TYPE = 'URL_ACTION'
  573. and z.DESC = :url_action_name
  574. and z.A_STATUS not in('DELETED')
  575. )
  576. where zp.TYPE = 'TYPESPECIALS'
  577. and zp.A_STATUS not in('DELETED')
  578. ");
  579. $sth->bindValue('url_action_name', $urlActionName);
  580. $sth->execute();
  581. $info = $sth->fetch();
  582. if (!$info) return $info;
  583. $info['url_params'] = array();
  584. $info['links'] = array();// where action is installed - expected `IN7_MK_BAZA_DYSTRYBUCJI`
  585. if ($info['action_id'] > 0) {
  586. $sthParams = $pdo->prepare("
  587. select z.ID as param_id
  588. , z.DESC as param_desc
  589. from CRM_LISTA_ZASOBOW z
  590. where z.TYPE = 'PARAM_IN'
  591. and z.A_STATUS not in('DELETED')
  592. and z.PARENT_ID = :url_action_id
  593. ");
  594. $sthParams->bindValue('url_action_id', $info['action_id']);
  595. $sthParams->execute();
  596. $urlParams = array();
  597. $rawUrlParams = $sthParams->fetchAll();
  598. foreach ($rawUrlParams as $urlParam) {
  599. $urlParams[$urlParam['param_desc']] = $urlParam;
  600. }
  601. $info['url_params'] = $urlParams;
  602. }
  603. {
  604. $main_db_id = $pdo->getZasobId();
  605. $sth_dbInfo = $pdo->prepare("
  606. select za.id as link_action_id
  607. , zd.id as db_id
  608. , zt.id as tbl_id
  609. , zi.id as cell_id
  610. , zp1.id as param1_id -- for every param
  611. from CRM_LISTA_ZASOBOW zt
  612. join CRM_LISTA_ZASOBOW zd on(zd.ID = zt.PARENT_ID
  613. and zd.TYPE in('DATABASE_MYSQL', 'BAZA_DANYCH', 'DATABASE_POSTGRESQL')
  614. and zd.ID = {$main_db_id}
  615. )
  616. join CRM_LISTA_ZASOBOW zi on(zi.PARENT_ID = zt.ID
  617. and zi.TYPE = 'KOMORKA'
  618. and zi.DESC = 'ID'
  619. )
  620. left join CRM_LISTA_ZASOBOW za on(za.PARENT_ID = zt.ID
  621. and za.TYPE = 'URL_ACTION'
  622. and za.ALIAS_ID = {$info['action_id']}
  623. )
  624. -- TODO: for every param:
  625. left join CRM_LISTA_ZASOBOW zp1 on(zp1.PARENT_ID = za.ID
  626. and zp1.TYPE = 'PARAM_IN'
  627. and zp1.ALIAS_ID = zi.id
  628. and zp1.DESC = 'ID_PROJECT'
  629. )
  630. where zt.TYPE = 'TABELA'
  631. and zt.A_STATUS not in('DELETED')
  632. and zt.DESC = 'IN7_MK_BAZA_DYSTRYBUCJI'
  633. ");
  634. DBG::_('DBG', '>1', "dbInfo query", $pdo->getRawSql($sth_dbInfo), __CLASS__, __FUNCTION__, __LINE__);
  635. $sth_dbInfo->execute();
  636. $linkInfo = $sth_dbInfo->fetch();
  637. if ($linkInfo) {
  638. $info['links']['IN7_MK_BAZA_DYSTRYBUCJI'] = $linkInfo;
  639. }
  640. }
  641. return $info;
  642. }
  643. public function kosztorysXmlAction() {// TODO: using old schema fields Agr_*
  644. /*
  645. $idProject = 1921;
  646. $schema = ProjectKosztorysSchema::getSchema();
  647. $data = $this->_fetchKosztorysData($idProject);
  648. //DBG::_('DBG', '>1', "XMLWriter", class_exists('XMLWriter'), __CLASS__, __FUNCTION__, __LINE__);
  649. //header('Content-type: application/xml; charset=utf-8');
  650. header('Content-type: text/plain; charset=utf-8');
  651. $xmlWriter = new XMLWriter();
  652. $xmlWriter->openUri('php://output');
  653. $xmlWriter->setIndent(true);
  654. if ($xmlWriter) {
  655. $xmlWriter->startDocument('1.0','UTF-8');
  656. //$xmlWriter->startElementNS(null, 'kosztorysy', 'https://biuro.biall-net.pl/wfs');// adds @xmlns=...
  657. $xmlWriter->startElement('kosztorysy');
  658. $xmlWriter->writeAttribute('targetNamespace', 'https://biuro.biall-net.pl/wfs');
  659. $xmlWriter->writeAttributeNS('xmlns', 'p5', 'http://www.w3.org/2000/xmlns/', 'https://biuro.biall-net.pl/wfs');
  660. for ($i = 1; $i <= 10; $i++) {
  661. $idProject += 1;
  662. $schema = ProjectKosztorysSchema::getSchema();
  663. $data = $this->_fetchKosztorysData($idProject);
  664. $xmlWriter->startElement('kosztorys');
  665. $xmlWriter->startElement('projekt');
  666. $xmlWriter->writeAttribute('id', $idProject);
  667. $xmlWriter->startElement('projekt');
  668. $xmlWriter->text("TODO L." . __LINE__);
  669. $xmlWriter->endElement();
  670. $xmlWriter->endElement();
  671. $xmlWriter->endElement();
  672. }
  673. // $memXmlWriter = new XMLWriter();
  674. // $memXmlWriter->openMemory();
  675. // $memXmlWriter->setIndent(true);
  676. //
  677. // for ($i = 1; $i <= 10; $i++) {
  678. // $idProject += 1;
  679. // $schema = ProjectKosztorysSchema::getSchema();
  680. // $data = $this->_fetchKosztorysData($idProject);
  681. //
  682. // $memXmlWriter->startElement('kosztorys');
  683. // $memXmlWriter->writeAttribute('id', $idProject);
  684. // $memXmlWriter->writeAttributeNS('p5', 'typeName', 'https://biuro.biall-net.pl/wfs', 'Kosztorys');
  685. // $memXmlWriter->text('book_'.$i);
  686. // $memXmlWriter->endElement();
  687. //
  688. // if ($i % 5 == 0) {
  689. // $batchXmlString = $memXmlWriter->outputMemory(true);
  690. // $xmlWriter->writeRaw($batchXmlString);
  691. // }
  692. // }
  693. // $memXmlWriter->flush();
  694. // unset($memXmlWriter);
  695. $xmlWriter->endElement();
  696. $xmlWriter->endDocument();
  697. }
  698. */
  699. }
  700. public function panel($idProject, $idCompany = 0) {
  701. if (User::get('ADM_ADMIN_LEVEL') > 6 || 'Pracownik' != User::getType()) return;// Only for workers with admin level < 6
  702. if (1 == V::get('_print', '', $_GET)) return;// print mode
  703. $task = V::get('_task', '', $_GET);
  704. ?>
  705. <div class="jumbotron">
  706. <div class="container">
  707. <div class="row">
  708. <div class="col-md-12">
  709. <?php if ($idProject > 0) : ?>
  710. <a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>">Kosztorys</a>
  711. <a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=przedmiar&_print=1" target="_blank">Przedmiar</a>
  712. <a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=zestawienieSwMikRur&_print=1" target="_blank">zestawienie (światłowód i mikrorurki)</a>
  713. <a class="btn btn-warning" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=oferta">oferta</a>
  714. <a class="btn btn-warning" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=ofertaAdmin">oferta (Admin)</a>
  715. <a class="btn btn-warning" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_task=ofertaCompany">oferta company</a>
  716. <?php endif; ?>
  717. <a class="btn btn-default" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&_task=ofertaDefaultAdmin" target="_blank">oferta domyślna (Admin)</a>
  718. </div>
  719. </div>
  720. <div class="row">
  721. <div class="col-md-12">
  722. <?php
  723. switch ($task) {
  724. case 'oferta': break;
  725. case '': ?><a class="btn btn-link" href="index.php?_route=UrlAction_ProjektyKosztyWstepnychRobot&ID_PROJECT=<?php echo $idProject; ?>&_print=1" target="_blank"><i class="glyphicon glyphicon-print"></i> Wydruk Kosztorysu</a><?php break;
  726. }
  727. ?>
  728. </div>
  729. </div>
  730. <br>
  731. <?php if ('ofertaAdmin' == $task) : ?>
  732. <form action="" method="get" class="form-inline">
  733. <input type="hidden" name="_route" value="UrlAction_ProjektyKosztyWstepnychRobot">
  734. <input type="hidden" name="_task" value="ofertaAdmin">
  735. <label for="ID_PROJECT">Nr projektu:</label>
  736. <input type="text" name="ID_PROJECT" value="<?php echo $idProject; ?>" class="form-control">
  737. <label for="ID_COMPANY">Nr kontrahenta (0 = oferta wewnętrzna/kosztorys)</label>
  738. <input type="text" name="ID_COMPANY" value="<?php echo $idCompany; ?>" class="form-control">
  739. <input type="submit" value="Wybierz" class="btn btn-primary">
  740. </form>
  741. <?php elseif ('oferta' == $task || 'ofertaDefaultAdmin' == $task) : ?>
  742. <form action="" method="get" class="form-inline">
  743. <input type="hidden" name="_route" value="UrlAction_ProjektyKosztyWstepnychRobot">
  744. <input type="hidden" name="_task" value="oferta">
  745. <input type="hidden" name="ID_PROJECT" value="<?php echo $idProject; ?>">
  746. <label for="ID_COMPANY">Nr kontrahenta</label>
  747. <input type="text" name="ID_COMPANY" value="<?php echo $idCompany; ?>" class="form-control">
  748. <input type="submit" value="Wybierz" class="btn btn-primary">
  749. </form>
  750. <?php endif; ?>
  751. </div>
  752. </div>
  753. <?php
  754. }
  755. public function saveOffer($idProject, $idCompany, $args, $admin = false) {
  756. DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
  757. if (!$admin) return;
  758. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  759. DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  760. $pdo = DB::getPDO();
  761. {
  762. $add_id_zasob = 0;
  763. $add_unit = '';
  764. $add_price = 0;
  765. $add_quantity = 0;
  766. $add__sth = $pdo->prepare("
  767. insert into CRM_LISTA_ZASOBOW_OFFERS (
  768. CRM_LISTA_ZASOBOW_ID
  769. , COMPANIES_ID
  770. , ID_PROJECT
  771. , OFFER_PRICE_PER_RESOURCE_UNIT
  772. , RESOURCE_UNIT_TYPE
  773. , OFFER_UNIT_TYPE
  774. , REQUIRED_RESOURCE_UNITS
  775. , A_RECORD_CREATE_AUTHOR
  776. , A_RECORD_CREATE_DATE
  777. ) values (
  778. :id_zasob
  779. , :id_company
  780. , :id_project
  781. , :price
  782. , :unit
  783. , :unit
  784. , :quantity
  785. , :author
  786. , NOW()
  787. )
  788. ");
  789. $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  790. $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  791. $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  792. $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
  793. $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
  794. $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
  795. $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
  796. }
  797. {
  798. $edit_id_company = $idCompany;
  799. $edit_id_zasob = 0;
  800. $edit_unit = '';
  801. $edit_price = 0;
  802. $edit_author = 0;
  803. $edit_quantity = 0;
  804. $edit__sth = $pdo->prepare("
  805. update CRM_LISTA_ZASOBOW_OFFERS
  806. set CRM_LISTA_ZASOBOW_ID = :id_zasob
  807. , COMPANIES_ID = :id_company
  808. , ID_PROJECT = :id_project
  809. , OFFER_PRICE_PER_RESOURCE_UNIT = :price
  810. , RESOURCE_UNIT_TYPE = :unit
  811. , OFFER_UNIT_TYPE = :unit
  812. , REQUIRED_RESOURCE_UNITS = :quantity
  813. , A_RECORD_UPDATE_AUTHOR = :author
  814. , A_RECORD_UPDATE_DATE = NOW()
  815. where ID = :id
  816. ");
  817. $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
  818. $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  819. $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  820. $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  821. $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
  822. $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
  823. $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
  824. $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
  825. }
  826. $schema = ProjectKosztorysSchema::getSchema();
  827. foreach ($schema['config']['layer'] as $idLayer => $layData) {
  828. DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
  829. foreach ($layData['type'] as $idType => $typeLabel) {
  830. $edit_id = V::get("edit_price_id_{$idType}", '', $args);
  831. $add_price = V::get("price_{$idType}", '', $args);
  832. DBG::_('DBG', '>1', "typeLabel (edit={$edit_id}, price={$add_price})", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  833. if ($edit_id > 0) {
  834. $edit_price = V::get("price_{$idType}", '', $args);
  835. $edit_price = str_replace(',', '.', $edit_price);
  836. if ($edit_id > 0 && $edit_price > 0) {
  837. $edit_id_zasob = $idType;
  838. $edit_unit = $layData['jednostka'];
  839. // TODO: check if anything change
  840. DBG::_('DBG', '>1', "EDIT price for idZasob({$idType}) {price:{$edit_price}, id_offer:{$edit_id}}", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  841. $edit__sth->execute();
  842. // TODO: add to hist
  843. }
  844. } else {
  845. $add_price = V::get("price_{$idType}", '', $args);
  846. $add_price = str_replace(',', '.', $add_price);
  847. if ($add_price > 0) {
  848. $add_id_zasob = $idType;
  849. $add_unit = $layData['jednostka'];
  850. DBG::_('DBG', '>1', "ADD price for idZasob({$idType}) {price:{$add_price}}", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  851. $add__sth->execute();
  852. }
  853. }
  854. }
  855. }
  856. }
  857. public function saveDefaultOffer($args) {
  858. DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
  859. $cennik = ProjectKosztorysCennik::getDefaultCennik();
  860. DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  861. $idCompany = 0;
  862. $idProject = 0;
  863. $pdo = DB::getPDO();
  864. {
  865. $add_id_zasob = 0;
  866. $add_unit = '';
  867. $add_price = 0;
  868. $add_quantity = 0;
  869. $add__sth = $pdo->prepare("
  870. insert into CRM_LISTA_ZASOBOW_OFFERS (
  871. CRM_LISTA_ZASOBOW_ID
  872. , COMPANIES_ID
  873. , ID_PROJECT
  874. , OFFER_PRICE_PER_RESOURCE_UNIT
  875. , RESOURCE_UNIT_TYPE
  876. , OFFER_UNIT_TYPE
  877. , REQUIRED_RESOURCE_UNITS
  878. , A_RECORD_CREATE_AUTHOR
  879. , A_RECORD_CREATE_DATE
  880. ) values (
  881. :id_zasob
  882. , :id_company
  883. , :id_project
  884. , :price
  885. , :unit
  886. , :unit
  887. , :quantity
  888. , :author
  889. , NOW()
  890. )
  891. ");
  892. $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  893. $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  894. $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  895. $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
  896. $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
  897. $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
  898. $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
  899. }
  900. {
  901. $edit_id_zasob = 0;
  902. $edit_unit = '';
  903. $edit_price = 0;
  904. $edit_author = 0;
  905. $edit_quantity = 0;
  906. $edit__sth = $pdo->prepare("
  907. update CRM_LISTA_ZASOBOW_OFFERS
  908. set CRM_LISTA_ZASOBOW_ID = :id_zasob
  909. , COMPANIES_ID = :id_company
  910. , ID_PROJECT = :id_project
  911. , OFFER_PRICE_PER_RESOURCE_UNIT = :price
  912. , RESOURCE_UNIT_TYPE = :unit
  913. , OFFER_UNIT_TYPE = :unit
  914. , REQUIRED_RESOURCE_UNITS = :quantity
  915. , A_RECORD_UPDATE_AUTHOR = :author
  916. , A_RECORD_UPDATE_DATE = NOW()
  917. where ID = :id
  918. ");
  919. $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
  920. $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  921. $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  922. $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  923. $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
  924. $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
  925. $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
  926. $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
  927. }
  928. $schema = ProjectKosztorysSchema::getSchema();
  929. foreach ($schema['config']['layer'] as $idLayer => $layData) {
  930. DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
  931. foreach ($layData['type'] as $idType => $typeLabel) {
  932. DBG::_('DBG', '>1', "typeLabel", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  933. $edit_id = V::get("edit_price_id_{$idType}", '', $args);
  934. if ($edit_id > 0) {
  935. $edit_price = V::get("price_{$idType}", '', $args);
  936. $edit_price = str_replace(',', '.', $edit_price);
  937. if ($edit_id > 0 && $edit_price > 0) {
  938. $edit_id_zasob = $idType;
  939. $edit_unit = $layData['jednostka'];
  940. // TODO: check if anything change
  941. $edit__sth->execute();
  942. // TODO: add to hist
  943. }
  944. } else {
  945. $add_price = V::get("price_{$idType}", '', $args);
  946. $add_price = str_replace(',', '.', $add_price);
  947. if ($add_price > 0) {
  948. $add_id_zasob = $idType;
  949. $add_unit = $layData['jednostka'];
  950. $add__sth->execute();
  951. }
  952. }
  953. }
  954. }
  955. }
  956. public function defaultOferta($idCompany = 0) {
  957. if ('1' == V::get('save_offer', '', $_POST)) {
  958. $this->saveDefaultOffer($_POST);
  959. }
  960. $schema = ProjectKosztorysSchema::getSchema();
  961. if (empty($schema['config']['type'])) throw new Exception("Schema error - brak zdefiniowanych typów");
  962. $cennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  963. //DBG::_(true, true, "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  964. UI::setTitleJsTag("Cennik domyślny dla Kosztorysów");
  965. ?>
  966. <div class="container">
  967. <form action="" method="post">
  968. <?php foreach ($schema['config']['layer'] as $idLayer => $layData) : ?>
  969. <h4 style="padding:0 6px"><?php echo $layData['label']; ?></h4>
  970. <table class="tabel table-bordered" style="width:100%">
  971. <tbody>
  972. <tr>
  973. <th style="padding:0 6px">id zasobu</th>
  974. <th style="padding:0 6px">typ</th>
  975. <th style="padding:0 6px">jednostka miary</th>
  976. <th style="padding:0 6px">cena jednostkowa</th>
  977. </tr>
  978. <?php foreach ($layData['type'] as $idType => $typeLabel) : ?>
  979. <tr>
  980. <td style="width:100px; padding:0 6px"><?php echo $idType; ?></td>
  981. <td style="padding:0 6px" title="[<?php echo $idType; ?>] <?php echo $typeLabel; ?>"><?php echo $typeLabel; ?></td>
  982. <td style="padding:0 6px"><?php echo $layData['jednostka']; ?></td>
  983. <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>
  984. </tr>
  985. <?php endforeach; ?>
  986. </tbody>
  987. </table>
  988. <?php endforeach; ?>
  989. <br>
  990. <?php foreach ($cennik as $item) : ?>
  991. <input type="hidden" name="edit_price_id_<?php echo $item['id_zasob']; ?>" value="<?php echo $item['ID']; ?>">
  992. <?php endforeach; ?>
  993. <input type="hidden" name="save_offer" value="1">
  994. <input type="submit" value="Zapisz" class="btn btn-primary">
  995. </form>
  996. </div>
  997. <?php
  998. // Fix Zasoby [22444] INNE Kosztorys - zasoby
  999. // SELECT * FROM `CRM_LISTA_ZASOBOW_OFFERS`
  1000. // WHERE `CRM_LISTA_ZASOBOW_ID` IN (22801,22445,22446,22447,22448,22449,22450,22451,22452,22453,22454,22455,22456,22457,22458,22459,22552,22517,22514,22481,22484,22515,22516,22518,22519,22520,22521,22550,22551,22617,22618,22638,22639,22640,22641,22642,22643,22644,22645,22646,22724,22725,22797,22798)
  1001. // ORDER BY `CRM_LISTA_ZASOBOW_OFFERS`.`CRM_LISTA_ZASOBOW_ID` ASC
  1002. // limit 100
  1003. // <edittype widgetv2type="ValueMap" name="Typ_kosztu">
  1004. // <widgetv2config fieldEditable="1" labelOnTop="0">
  1005. // <value zasobID="22643" key="Montaż zapasu w studni" value="Montaż zapasu w studni"/>
  1006. // <value zasobID="22638" key="Montaż złączek mikrokanalizacji" value="Montaż złączek mikrokanalizacji"/>
  1007. // <value zasobID="22645" key="Mufa z montażem" value="Mufa z montażem"/>
  1008. // <value zasobID="22642" key="Pigtail od puszki do terminala" value="Pigtail od puszki do terminala"/>
  1009. // <value zasobID="22640" key="Przewiert w budynku z uszczelnieniem" value="Przewiert w budynku z uszczelnieniem"/>
  1010. // <value zasobID="22639" key="Puszka abonencka z montażem" value="Puszka abonencka z montażem"/>
  1011. // <value zasobID="22641" key="Spaw poza mufą" value="Spaw poza mufą"/>
  1012. // <value zasobID="22724" key="Spaw w mufie" value="Spaw w mufie"/>
  1013. // <value zasobID="22617" key="Spliter 1/16" value="Spliter 1/16"/>
  1014. // <value zasobID="22618" key="Spliter 1/4" value="Spliter 1/4"/>
  1015. // <value zasobID="22725" key="Spliter 1/8" value="Spliter 1/8"/>
  1016. // <value zasobID="22644" key="Wprowadzenie kabli do mufy" value="Wprowadzenie kabli do mufy"/>
  1017. // <value zasobID="22646" key="Zamek PIOCH do studni" value="Zamek PIOCH do studni"/>
  1018. // <value zasobID="22515" key="Zapas kabla" value="Zapas kabla"/>
  1019. // <value zasobID="22514" key="Złącze" value="Złącze "/>
  1020. // </widgetv2config>
  1021. // </edittype>
  1022. // 22574 ->22515 INNE TYPE Zapas kabla
  1023. // 22620 ->22618 INNE TYPE Spliter 1/4
  1024. // 22621 ->22617 INNE TYPE Spliter 1/16
  1025. // 22647 ->22638 INNE TYPE Montaż złączek mikrokanalizacji
  1026. // 22648 ->22639 INNE TYPE Puszka abonencka z montażem
  1027. // 22649 ->22640 INNE TYPE Przewiert w budynku z uszczelnieniem
  1028. // 22650 ->22641 INNE TYPE Spaw poza mufą
  1029. // 22651 ->22642 INNE TYPE Pigtail od puszki do terminala
  1030. // 22652 ->22643 INNE TYPE Montaż zapasu w studni
  1031. // 22653 ->22644 INNE TYPE Wprowadzenie kabli do mufy
  1032. // 22654 ->22645 INNE TYPE Mufa z montażem
  1033. // 22655 ->22646 INNE TYPE Zamek PIOCH do studni
  1034. // 22881 ->22514 INNE TYPE Złącze
  1035. // TODO: ? 22484
  1036. }
  1037. /**
  1038. * @returns [ 'cost_total' => :numeric, 'sub_costs' => [ $id_zasob => 'price' ] ]
  1039. */
  1040. public function getProjectCostByCennik($idProject, $idCompany = 0) {
  1041. $projCost = array();
  1042. $projCost['cost_total'] = 0;
  1043. $projCost['sub_costs'] = array();
  1044. $defCennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  1045. // $defCennik = [ $id_zasob => [ 'price' => $price, 'ID', 'id_zasob', 'id_company', 'id_project', 'unit', 'quantity' ] ]
  1046. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  1047. $workCennik = ProjectKosztorysCennik::getWorkCennik($idProject, $idCompany);
  1048. $data = $this->getKosztorysData($idProject, $idOrder = 0, $admin = false);
  1049. foreach ($data['summary'] as $idLayer => $layData) {
  1050. foreach ($layData['data'] as $typeData) {
  1051. if (!$typeData['idType']) continue;
  1052. $price = (!empty($cennik[$typeData['idType']]['price']))? $cennik[$typeData['idType']]['price'] : 0;
  1053. $defPrice = (!empty($defCennik[$typeData['idType']]['price']))? $defCennik[$typeData['idType']]['price'] : 0;
  1054. $workPrice = (!empty($workCennik[$typeData['idType']]['price']))? $workCennik[$typeData['idType']]['price'] : 0;
  1055. $cost = ($price > 0)? $price : $defPrice;
  1056. if ($workPrice > 0) $cost += $workPrice;
  1057. $cost = $typeData['ilosc'] * $cost;
  1058. $projCost['sub_costs'][$typeData['idType']] = $cost;
  1059. $projCost['cost_total'] += $cost;
  1060. }
  1061. }
  1062. // fetch sub costs for sub projects
  1063. // $data['by_project'] = array_reduce($data['rawDataByType'], function($result, $row) {
  1064. // $idProj = $row['ID_PROJECT'];
  1065. // $idLayer = $row['idLayer'];
  1066. // $idType = $row['idType'];
  1067. // if (!array_key_exists($idProj, $result)) $result[$idProj] = array();
  1068. // if (!array_key_exists($idLayer, $result)) $result[$idProj][$idLayer] = array();
  1069. // if (!array_key_exists($idType, $result[$idProj][$idLayer])) {
  1070. // $result[$idProj][$idLayer][$idType] = $row;
  1071. // } else {
  1072. // $result[$idProj][$idLayer][$idType]['ilosc'] += $row['ilosc'];
  1073. $projCost['sub_proj'] = array();
  1074. foreach ($data['by_project'] as $idSubProj => $subProjData) {
  1075. foreach ($subProjData as $idLayer => $layData) {
  1076. foreach ($layData as $idType => $typeData) {
  1077. $price = (!empty($cennik[$typeData['idType']]['price']))? $cennik[$typeData['idType']]['price'] : 0;
  1078. $defPrice = (!empty($defCennik[$typeData['idType']]['price']))? $defCennik[$typeData['idType']]['price'] : 0;
  1079. $cost = ($price > 0)? $price : $defPrice;
  1080. $cost = $typeData['ilosc'] * $cost;
  1081. $projCost['sub_proj'][$idSubProj][$idType] = $cost;
  1082. }
  1083. }
  1084. }
  1085. return $projCost;
  1086. }
  1087. public function getModel($idProject) {
  1088. if (array_key_exists($idProject, $this->_model)) return $this->_model[$idProject];
  1089. $this->_model[$idProject] = new ProjectKosztorysModel($idProject);
  1090. return $this->_model[$idProject];
  1091. }
  1092. public function fetchBudynki($idProject) {
  1093. return DB::getPDO()->fetchAll("
  1094. select b.*
  1095. from BUILDINGS b
  1096. where b.ID_PROJECT = '{$idProject}'
  1097. ");
  1098. }
  1099. public function fetchProject($idProject) {
  1100. $rows = DB::getPDO()->fetchAll("
  1101. select p.*
  1102. from IN7_MK_BAZA_DYSTRYBUCJI p
  1103. where p.ID = '{$idProject}'
  1104. ");
  1105. return reset($rows);
  1106. }
  1107. public function zestawienieSwMikRur($idProject) {
  1108. $idCompany = 0;
  1109. $admin = false;
  1110. $companyAdmin = false;
  1111. $model = $this->getModel($idProject);
  1112. //DBG::table("subProjectList", $model->subProjectList, __CLASS__, __FUNCTION__, __LINE__);
  1113. $schema = ProjectKosztorysSchema::getSchema();
  1114. $projCost = $this->getProjectCostByCennik($idProject, $idCompany);
  1115. //DBG::_(true, true, "projCost", $projCost, __CLASS__, __FUNCTION__, __LINE__);
  1116. $viewLayerDataArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin', 'projCost');
  1117. ?>
  1118. <div class="container">
  1119. <h1></h1>
  1120. <table class="table">
  1121. <tr>
  1122. <th><?php echo $schema['nr']; ?></th>
  1123. <th><?php echo $schema['title']; ?></th>
  1124. <th><?php echo $schema['ownerName']; ?></th>
  1125. <th style="text-align:right"><?php echo $schema['cost_total']; ?></th>
  1126. </tr>
  1127. <tr>
  1128. <td><?php echo $model->idProject; ?></td>
  1129. <td><?php echo $model->title; ?></td>
  1130. <td><?php echo $model->ownerName; ?></td>
  1131. <td style="text-align:right"><?php echo number_format($projCost['cost_total'], 2, ',', ' '); ?></td>
  1132. </tr>
  1133. </table>
  1134. <?php $args = $viewLayerDataArgs;
  1135. $idProject = $args['idProject'];
  1136. $idCompany = V::get('idCompany', 0, $args);
  1137. $admin = V::get('admin', false, $args);
  1138. $companyAdmin = V::get('companyAdmin', false, $args);
  1139. $projCosts = V::get('projCosts', array(), $args);
  1140. $schema = ProjectKosztorysSchema::getSchema();
  1141. $conf = $schema['config'];
  1142. DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__);
  1143. $summaryTypeCost = V::get('sub_costs', array(), $projCosts);
  1144. $subProjCost = V::get('sub_proj', array(), $projCosts);
  1145. $data = $this->getKosztorysData($idProject, $idOrder = 0, $admin);
  1146. $dataSummary = $data['summary'];
  1147. $dataSubProj = $data['by_project'];
  1148. // $dataSubProj = array();
  1149. // foreach ($data['by_project'] as $idSubProj => $subProjData) {
  1150. // if ($idProject != $idSubProj) $dataSubProj[$idSubProj] = $subProjData;
  1151. // }
  1152. DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
  1153. $defCennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  1154. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  1155. $workCennik = ProjectKosztorysCennik::getWorkCennik($idProject, $idCompany);
  1156. $additionalCosts = array();
  1157. $additionalCosts[] = array();
  1158. $additionalCosts[] = array();
  1159. $additionalCosts[] = array();
  1160. $additionalSummaryTypeCost = array();
  1161. ?>
  1162. <style type="text/css">
  1163. /* Print Styles */
  1164. @media print {
  1165. body { font-size:10px; }
  1166. th, td { font-size:10px; }
  1167. h1 { font-size:2em; }
  1168. h2 { font-size:1.6em; }
  1169. h3 { font-size:1.4em; }
  1170. h4 { font-size:1.2em; }
  1171. }
  1172. </style>
  1173. <?php if (!empty($dataSubProj)) : ?>
  1174. <table class="table table-bordered">
  1175. <thead>
  1176. <tr>
  1177. <th>ID PROJ.</th>
  1178. <th>ID BUDYNKU</th>
  1179. <th>WŁAŚCICIEL</th>
  1180. <th>ADRES</th>
  1181. <th>KONTAKT</th>
  1182. <th>NR DZIAŁKI</th>
  1183. <th>DŁ. ŚWIATŁOWODU</th>
  1184. <th>DŁ. MIKRORURKI DO KLIENTA</th>
  1185. </tr>
  1186. </thead>
  1187. <tbody>
  1188. <?php foreach ($dataSubProj as $idSubProj => $subProjData) : ?>
  1189. <?php
  1190. // 22460 Światłowód
  1191. // 22467 Mikrokanalizacja do klienta
  1192. $sumSwiatlo = 0;
  1193. if (!empty($subProjData[22460])) foreach ($subProjData[22460] as $swiatlo) {
  1194. $sumSwiatlo += $swiatlo['ilosc'];
  1195. }
  1196. $sumMikroRur = 0;
  1197. if (!empty($subProjData[22467])) foreach ($subProjData[22467] as $mikroRur) {
  1198. $sumMikroRur += $mikroRur['ilosc'];
  1199. }
  1200. ?>
  1201. <?php $budynki = $this->fetchBudynki($idSubProj); ?>
  1202. <?php $proj = $this->fetchProject($idSubProj); ?>
  1203. <?php if (empty($budynki)) : ?>
  1204. <tr>
  1205. <td><?php echo $idSubProj; ?></td>
  1206. <td>brak</td>
  1207. <td><?php echo $proj['IN_NAME']; ?></td>
  1208. <td><?php echo $proj['M_DISTRIBUTOR_ADDRESS']; ?></td>
  1209. <td><?php echo $proj['M_DISTRIBUTOR_CONTACT']; ?></td>
  1210. <td><?php echo $proj['INWDZ__obreby_i_nr_dzialek']; ?></td>
  1211. <td><?php echo $sumSwiatlo; ?></td>
  1212. <td><?php echo $sumMikroRur; ?></td>
  1213. </tr>
  1214. <?php else : ?>
  1215. <?php foreach ($budynki as $bud) : ?>
  1216. <tr>
  1217. <td><?php echo $idSubProj; ?></td>
  1218. <td><?php echo $bud['ID']; ?></td>
  1219. <td><?php echo $proj['IN_NAME']; ?></td>
  1220. <td><?php echo $proj['M_DISTRIBUTOR_ADDRESS']; ?></td>
  1221. <td><?php echo $proj['M_DISTRIBUTOR_CONTACT']; ?></td>
  1222. <td><?php echo $proj['INWDZ__obreby_i_nr_dzialek']; ?></td>
  1223. <td><?php echo $sumSwiatlo; ?></td>
  1224. <td><?php echo $sumMikroRur; ?></td>
  1225. </tr>
  1226. <?php endforeach; ?>
  1227. <?php endif; ?>
  1228. <?php endforeach; ?>
  1229. </tbody>
  1230. </table>
  1231. <?php endif; ?>
  1232. <?php
  1233. DBG::_('DBG', '>0', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__);
  1234. DBG::_('DBG', '>0', "projCosts", $projCosts, __CLASS__, __FUNCTION__, __LINE__);
  1235. }
  1236. public function kosztorys($idProject) {
  1237. $idCompany = 0;
  1238. $admin = false;
  1239. $companyAdmin = false;
  1240. $priceEditJs = (1 != V::get('_print', '', $_GET));
  1241. $model = $this->getModel($idProject);
  1242. //DBG::table("subProjectList", $model->subProjectList, __CLASS__, __FUNCTION__, __LINE__);
  1243. $schema = ProjectKosztorysSchema::getSchema();
  1244. $projCosts = $this->getProjectCostByCennik($idProject, $idCompany);
  1245. //DBG::_(true, true, "projCosts", $projCosts, __CLASS__, __FUNCTION__, __LINE__);
  1246. $viewLayerDataArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin', 'projCosts', 'priceEditJs');
  1247. UI::setTitleJsTag("Kosztorys wstępny robót telekomunikacyjnych [{$idProject}]");
  1248. ?>
  1249. <div class="container">
  1250. <h1>Kosztorys wstępny robót telekomunikacyjnych</h1>
  1251. <table class="table">
  1252. <tr>
  1253. <th><?php echo $schema['nr']; ?></th>
  1254. <th><?php echo $schema['title']; ?></th>
  1255. <th><?php echo $schema['ownerName']; ?></th>
  1256. <th style="text-align:right"><?php echo $schema['cost_total']; ?></th>
  1257. </tr>
  1258. <tr>
  1259. <td><?php echo $model->idProject; ?></td>
  1260. <td><?php echo $model->title; ?></td>
  1261. <td><?php echo $model->ownerName; ?></td>
  1262. <td style="text-align:right"><?php echo number_format($projCosts['cost_total'], 2, ',', ' '); ?></td>
  1263. </tr>
  1264. </table>
  1265. <?php $this->viewLayersData($viewLayerDataArgs); ?>
  1266. </div>
  1267. <?php
  1268. DBG::_('DBG', '>0', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__);
  1269. DBG::_('DBG', '>0', "projCosts", $projCosts, __CLASS__, __FUNCTION__, __LINE__);
  1270. }
  1271. public function przedmiar($idProject) {
  1272. $idCompany = 0;
  1273. $admin = false;
  1274. $companyAdmin = false;
  1275. $model = $this->getModel($idProject);
  1276. //DBG::table("subProjectList", $model->subProjectList, __CLASS__, __FUNCTION__, __LINE__);
  1277. $schema = ProjectKosztorysSchema::getSchema();
  1278. $projCosts = $this->getProjectCostByCennik($idProject, $idCompany);
  1279. //DBG::_(true, true, "projCosts", $projCosts, __CLASS__, __FUNCTION__, __LINE__);
  1280. $hidePrices = true;
  1281. $viewLayerDataArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin', 'hidePrices');
  1282. UI::setTitleJsTag("Przedmiar robót telekomunikacyjnych [{$idProject}]");
  1283. ?>
  1284. <div class="container">
  1285. <h1>Przedmiar robót telekomunikacyjnych</h1>
  1286. <table class="table">
  1287. <tr>
  1288. <th><?php echo $schema['nr']; ?></th>
  1289. <th><?php echo $schema['title']; ?></th>
  1290. </tr>
  1291. <tr>
  1292. <td><?php echo $model->idProject; ?></td>
  1293. <td><?php echo $model->title; ?></td>
  1294. </tr>
  1295. </table>
  1296. <?php $this->viewLayersData($viewLayerDataArgs); ?>
  1297. </div>
  1298. <?php
  1299. DBG::_('DBG', '>0', "schema", $schema, __CLASS__, __FUNCTION__, __LINE__);
  1300. DBG::_('DBG', '>0', "projCosts", $projCosts, __CLASS__, __FUNCTION__, __LINE__);
  1301. }
  1302. public function oferta($args) {
  1303. $idProject = $args['idProject'];
  1304. $idCompany = V::get('idCompany', 0, $args);
  1305. $admin = V::get('admin', false, $args);
  1306. $companyAdmin = V::get('companyAdmin', false, $args);
  1307. if ($admin && '1' == V::get('save_offer', '', $_POST)) {
  1308. $this->saveOffer($idProject, $idCompany, $_POST, $admin);
  1309. }
  1310. $defCennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  1311. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  1312. $viewLayerDataArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin');
  1313. UI::setTitleJsTag("Oferta " . (($idProject)? " [{$idProject}] " : '') . (($idCompany)? " Kontrahent({$idCompany})" : '') . " - Kosztorysy");
  1314. ?>
  1315. <div class="container">
  1316. <?php if ($admin) : ?>
  1317. <form action="" method="post">
  1318. <?php endif; ?>
  1319. <?php $this->viewLayersData($viewLayerDataArgs); ?>
  1320. <?php if ($admin || $companyAdmin) : ?>
  1321. <?php foreach ($cennik as $item) : ?>
  1322. <input type="hidden" name="edit_price_id_<?php echo $item['id_zasob']; ?>" value="<?php echo $item['ID']; ?>">
  1323. <?php endforeach; ?>
  1324. <input type="hidden" name="save_offer" value="1">
  1325. <hr><input class="btn btn-primary" type="submit" value="Zapisz ofertę">
  1326. </form>
  1327. <?php endif; ?>
  1328. </div>
  1329. <?php
  1330. }
  1331. public function viewLayersData($args) {
  1332. $idProject = $args['idProject'];
  1333. $idCompany = V::get('idCompany', 0, $args);
  1334. $admin = V::get('admin', false, $args);
  1335. $priceEditJs = V::get('priceEditJs', false, $args);
  1336. $companyAdmin = V::get('companyAdmin', false, $args);
  1337. $projCosts = V::get('projCosts', array(), $args);
  1338. $showPrices = ! V::get('hidePrices', 0, $args);
  1339. $schema = ProjectKosztorysSchema::getSchema();
  1340. $conf = $schema['config'];
  1341. DBG::_('DBG', '>1', "conf", $conf, __CLASS__, __FUNCTION__, __LINE__);
  1342. $summaryTypeCost = V::get('sub_costs', array(), $projCosts);
  1343. $subProjCost = V::get('sub_proj', array(), $projCosts);
  1344. $data = $this->getKosztorysData($idProject, $idOrder = 0, $admin);
  1345. $dataSummary = $data['summary'];
  1346. $dataSubProj = $data['by_project'];
  1347. // $dataSubProj = array();
  1348. // foreach ($data['by_project'] as $idSubProj => $subProjData) {
  1349. // if ($idProject != $idSubProj) $dataSubProj[$idSubProj] = $subProjData;
  1350. // }
  1351. DBG::_('DBG', '>1', "data", $data, __CLASS__, __FUNCTION__, __LINE__);
  1352. $defCennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  1353. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  1354. $workCennik = ProjectKosztorysCennik::getWorkCennik($idProject, $idCompany);
  1355. if (!empty($summaryTypeCost)) {
  1356. $additionalCosts = array();
  1357. $additionalCosts[] = array();
  1358. $additionalCosts[] = array();
  1359. $additionalCosts[] = array();
  1360. $additionalSummaryTypeCost = array();
  1361. $additionalSummaryTypeCost[] = 0;
  1362. $additionalSummaryTypeCost[] = 0;
  1363. $additionalSummaryTypeCost[] = 0;
  1364. }
  1365. ?>
  1366. <style type="text/css">
  1367. .type_price-UNDEFINED { color:silver }
  1368. .type_price-UNDEFINED input { color:silver }
  1369. .type_price-GLOBAL { color:red }
  1370. .type_price-GLOBAL input { color:red }
  1371. .type_price-PROJECT { color:green }
  1372. .type_price-PROJECT input { color:green }
  1373. /* Print Styles */
  1374. @media print {
  1375. body { font-size:10px; }
  1376. th, td { font-size:10px; }
  1377. h1 { font-size:2em; }
  1378. h2 { font-size:1.6em; }
  1379. h3 { font-size:1.4em; }
  1380. h4 { font-size:1.2em; }
  1381. }
  1382. </style>
  1383. <?php $tblCols = 6 + ($admin ? 1 : 0) + ((!empty($summaryTypeCost))? 1 : 0); ?>
  1384. <table class="tabel table-bordered table-hover" style="width:100%; margin-bottom:6px; page-break-inside:avoid">
  1385. <tbody>
  1386. <?php foreach ($dataSummary as $idLayer => $layData) : ?>
  1387. <tr>
  1388. <td colspan="<?php echo $tblCols + 1; ?>" style="padding:0 6px; font-size:1.2em; line-height:2em"> &mdash; <?php echo $layData['label']; ?></td>
  1389. </tr>
  1390. <tr>
  1391. <td style="width:24px"></td>
  1392. <td style="color:#888; padding:0 6px">typ</td>
  1393. <td style="color:#888; width:60px; padding:0 6px; text-align:right">ilość</td>
  1394. <td style="color:#888; width:80px; padding:0 6px; text-align:right">jednostka</td>
  1395. <?php if ($showPrices) : ?>
  1396. <td style="color:#888; padding:0 6px; text-align:right">cena jednostkowa</td>
  1397. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (robocizna)</td>
  1398. <?php endif; ?>
  1399. <?php if ($admin) : ?>
  1400. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (domyślna)</td>
  1401. <?php endif; ?>
  1402. <?php if (!empty($summaryTypeCost)) : ?>
  1403. <td style="color:#888; padding:0 6px; text-align:right">szacowany koszt [zł]</td>
  1404. <?php endif; ?>
  1405. <td style="color:#888; padding:0 6px; text-align:right">uwagi</td>
  1406. </tr>
  1407. <?php foreach ($layData['data'] as $typeData) : ?>
  1408. <?php if (!$typeData['idType']) : ?>
  1409. <tr>
  1410. <td colspan="4">
  1411. <?php if ($admin) {
  1412. $fixLink = 'index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=' . $conf['layer'][$idLayer]['tabela_id'];
  1413. $fixLink .= '&f_' . $conf['layer'][$idLayer]['zasob_field'] . '=%3D' . $typeData['type'];
  1414. $fixLink .= '&_hash=' . uniqid();
  1415. $fixProjectLink = '<a target="_blank" href="' . $fixLink . "&f_ID_PROJECT={$idProject}" . '">' . "Popraw" . '</a>' . " (dane dla projektu [{$idProject}])";
  1416. $fixAllLink = '<a target="_blank" href="' . $fixLink . '">' . "Popraw wszystkie" . '</a>';
  1417. UI::alert('danger', "Niezdefiniowany typ: <code>{$typeData['type']}</code> - {$fixProjectLink}, {$fixAllLink}");
  1418. } ?>
  1419. <?php DBG::_('DBG', '>1', "Error conf", $conf, __CLASS__, __FUNCTION__, __LINE__); ?>
  1420. <?php DBG::_('DBG', '>1', "Error", $typeData, __CLASS__, __FUNCTION__, __LINE__); ?>
  1421. </td>
  1422. </tr>
  1423. <?php else : ?>
  1424. <?php $idType = $typeData['idType']; ?>
  1425. <?php $defPrice = (!empty($defCennik[$idType]['price']))? $defCennik[$idType]['price'] : 0; ?>
  1426. <?php $price = (!empty($cennik[$idType]['price']))? $cennik[$idType]['price'] : $defPrice; ?>
  1427. <?php
  1428. $typePrice = 'UNDEFINED';
  1429. if (!empty($defCennik[$idType]['price'])) $typePrice = 'GLOBAL';
  1430. if (!empty($cennik[$idType]['price'])) $typePrice = 'PROJECT';
  1431. ?>
  1432. <?php $workPrice = (!empty($workCennik[$idType]['price']))? $workCennik[$idType]['price'] : 0; ?>
  1433. <?php $uwagi = (!empty($cennik[$idType]['notes']))? $cennik[$idType]['notes'] : '';// TODO: uwagi ?>
  1434. <tr>
  1435. <td style="width:24px"></td>
  1436. <td style="padding:0 6px" title="[<?php echo $idType; ?>] <?php echo $typeData['type']; ?>"> &ndash; <?php echo $typeData['type']; ?></td>
  1437. <td style="padding:0 6px; text-align:right"><?php echo $typeData['ilosc']; ?></td>
  1438. <td style="padding:0 6px; text-align:right"><?php echo ProjectKosztorysSchema::getLayerJednostka($idLayer); ?></td>
  1439. <?php if ($showPrices) : ?>
  1440. <td
  1441. <?php if ($priceEditJs) : ?>
  1442. <?php
  1443. $updateProjektyOferta = Request::getPathUri() . "index.php?_route=UrlAction_ProjektyKosztorys&_task=updateProjektyOfertaAjax&idProject={$idProject}&idType={$idType}&unitType=zasob";
  1444. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Kosztorys:updateProjektyOferta', { href: '{$updateProjektyOferta}' })";
  1445. ?>
  1446. onClick="<?= $onClick; ?>"
  1447. class="type_price-<?= $typePrice; ?>"
  1448. <?php endif; ?>
  1449. style="padding:3px 6px; text-align:right">
  1450. <?php if ($admin || $companyAdmin) : ?>
  1451. <input type="text" style="text-align:right" class="form-control input-sm" name="price_<?= $idType; ?>" value="<?= $price; ?>"/>
  1452. <?php else : ?>
  1453. <?php echo number_format($price, 2, ',', ' '); ?>
  1454. <?php endif; ?>
  1455. </td>
  1456. <td
  1457. <?php if ($priceEditJs) : ?>
  1458. <?php
  1459. $updateProjektyOferta = Request::getPathUri() . "index.php?_route=UrlAction_ProjektyKosztorys&_task=updateProjektyOfertaAjax&idProject={$idProject}&idType={$idType}&unitType=robocizna";
  1460. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Kosztorys:updateProjektyOferta', { href: '{$updateProjektyOferta}' })";
  1461. ?>
  1462. onClick="<?= $onClick; ?>"
  1463. <?php endif; ?>
  1464. style="padding:3px 6px; text-align:right">
  1465. <?php if ($admin || $companyAdmin) : ?>
  1466. <input type="text" style="text-align:right" class="form-control input-sm" name="work_price_<?php echo $idType; ?>" value="<?php echo $workPrice; ?>"/>
  1467. <?php else : ?>
  1468. <?php echo number_format($workPrice, 2, ',', ' '); ?>
  1469. <?php endif; ?>
  1470. </td>
  1471. <?php if ($admin) : ?>
  1472. <td style="padding:3px 6px; text-align:right"><?php echo $defPrice; ?></td>
  1473. <?php endif; ?>
  1474. <?php if (!empty($summaryTypeCost)) : ?>
  1475. <td style="padding:3px 6px; text-align:right"><?php echo number_format(V::get($idType, 0, $summaryTypeCost), 2, ',', ' '); ?></td>
  1476. <?php endif; ?>
  1477. <?php endif; ?>
  1478. <td style="padding:3px 6px; text-align:right">
  1479. <?php if ($admin || $companyAdmin) : ?>
  1480. <input type="text" style="text-align:left" class="form-control input-sm" name="uwagi_<?php echo $idType; ?>" value="<?php echo $uwagi; ?>"/>
  1481. <?php else : ?>
  1482. <?php echo $uwagi; ?>
  1483. <?php endif; ?>
  1484. </td>
  1485. </tr>
  1486. <?php endif; ?>
  1487. <?php endforeach; ?>
  1488. <?php endforeach; ?>
  1489. <?php if (!empty($additionalCosts)) : ?>
  1490. <tr>
  1491. <td colspan="<?php echo $tblCols + 1; ?>" style="padding:0 6px; font-size:1.2em; line-height:2em">Koszty dodatkowe</td>
  1492. </tr>
  1493. <tr>
  1494. <td style="width:24px"></td>
  1495. <td style="color:#888; padding:0 6px">typ</td>
  1496. <td style="color:#888; width:60px; padding:0 6px; text-align:right">ilość</td>
  1497. <td style="color:#888; width:80px; padding:0 6px; text-align:right">jednostka</td>
  1498. <td style="color:#888; padding:0 6px; text-align:right">cena jednostkowa</td>
  1499. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (robocizna)</td>
  1500. <?php if ($admin) : ?>
  1501. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (domyślna)</td>
  1502. <?php endif; ?>
  1503. <?php if (!empty($summaryTypeCost)) : ?>
  1504. <td style="color:#888; padding:0 6px; text-align:right">szacowany koszt [zł]</td>
  1505. <?php endif; ?>
  1506. <td style="color:#888; padding:0 6px; text-align:right">uwagi</td>
  1507. </tr>
  1508. <?php foreach ($additionalCosts as $idType => $additionalCost) : ?>
  1509. <?php $additionalPrice = 0;//TODO: get from $additionalCennik or $additionalCost ?>
  1510. <?php $additionalWorkPrice = 0;//TODO: get from $additionalWorkCennik or $additionalCost ?>
  1511. <?php $additionalCount = 0;//TODO: fetch ?>
  1512. <?php $additionalUnit = '';//TODO: fetch ?>
  1513. <?php $jednostka = '';//TODO: fetch ?>
  1514. <?php $ilosc = '';//TODO: fetch ?>
  1515. <?php $typeLabel = '';//TODO: fetch ?>
  1516. <?php $price = 0;//TODO: fetch ?>
  1517. <?php $defPrice = 0;//TODO: fetch ?>
  1518. <?php $workPrice = 0;//TODO: fetch ?>
  1519. <?php $uwagi = '';//TODO: fetch ?>
  1520. <tr>
  1521. <td style="width:24px"></td>
  1522. <td style="padding:0 6px">
  1523. <?php if ($admin || $companyAdmin) : ?>
  1524. <input type="text" style="text-align:right" class="form-control input-sm" name="additional_label_<?php echo $idType; ?>" value="<?php echo $typeLabel; ?>"/>
  1525. <?php else : ?>
  1526. - <?php echo $typeLabel; ?>
  1527. <?php endif; ?>
  1528. </td>
  1529. <td style="padding:0 6px; text-align:right">
  1530. <?php if ($admin || $companyAdmin) : ?>
  1531. <input type="text" style="text-align:right" class="form-control input-sm" name="additional_count_<?php echo $idType; ?>" value="<?php echo $additionalCount; ?>"/>
  1532. <?php else : ?>
  1533. <?php echo $ilosc; ?>
  1534. <?php endif; ?>
  1535. </td>
  1536. <td style="padding:0 6px; text-align:right">
  1537. <?php if ($admin || $companyAdmin) : ?>
  1538. <input type="text" style="text-align:right" class="form-control input-sm" name="additional_unit_<?php echo $idType; ?>" value="<?php echo $additionalUnit; ?>"/>
  1539. <?php else : ?>
  1540. <?php echo $jednostka; ?>
  1541. <?php endif; ?>
  1542. </td>
  1543. <td style="padding:3px 6px; text-align:right">
  1544. <?php if ($admin || $companyAdmin) : ?>
  1545. <input type="text" style="text-align:right" class="form-control input-sm" name="additional_price_<?php echo $idType; ?>" value="<?php echo $additionalPrice; ?>"/>
  1546. <?php else : ?>
  1547. <?php echo number_format($price, 2, ',', ' '); ?>
  1548. <?php endif; ?>
  1549. </td>
  1550. <td style="padding:3px 6px; text-align:right">
  1551. <?php if ($admin || $companyAdmin) : ?>
  1552. <input type="text" style="text-align:right" class="form-control input-sm" name="additional_work_price_<?php echo $idType; ?>" value="<?php echo $additionalWorkPrice; ?>"/>
  1553. <?php else : ?>
  1554. <?php echo number_format($workPrice, 2, ',', ' '); ?>
  1555. <?php endif; ?>
  1556. </td>
  1557. <?php if ($admin) : ?>
  1558. <td style="padding:3px 6px; text-align:right"></td>
  1559. <?php endif; ?>
  1560. <?php if (!empty($additionalSummaryTypeCost)) : ?>
  1561. <td style="padding:3px 6px; text-align:right"><?php echo number_format(V::get($idType, 0, $additionalSummaryTypeCost), 2, ',', ' '); ?></td>
  1562. <?php endif; ?>
  1563. <td style="padding:3px 6px; text-align:right">
  1564. <?php if ($admin || $companyAdmin) : ?>
  1565. <input type="text" style="text-align:left" class="form-control input-sm" name="additional_uwagi_<?php echo $idType; ?>" value="<?php echo $uwagi; ?>"/>
  1566. <?php else : ?>
  1567. <?php echo $uwagi; ?>
  1568. <?php endif; ?>
  1569. </td>
  1570. </tr>
  1571. <?php endforeach; ?>
  1572. <?php endif; ?>
  1573. </tbody>
  1574. </table>
  1575. <?php $tblCols = 4 + ($admin ? 1 : 0) + ((!empty($summaryTypeCost))? 1 : 0); ?>
  1576. <?php if (!empty($dataSubProj)) : ?>
  1577. <p style="page-break-before:always; margin-top:60px"></p>
  1578. <h3>Składowe wg projektów podrzędnych:</h3>
  1579. <?php foreach ($dataSubProj as $idSubProj => $subProjData) : ?>
  1580. <table class="tabel table-bordered table-hover" style="width:100%; margin-bottom:6px; page-break-inside:avoid">
  1581. <tbody>
  1582. <tr>
  1583. <th colspan="<?php echo $tblCols + 1; ?>"
  1584. style="padding:0 6px">
  1585. <h4>Projekt <?php echo $idSubProj; ?>: <i><?php echo $this->getModel($idProject)->getProjectName($idSubProj); ?></i>:</h4>
  1586. </th>
  1587. </tr>
  1588. <?php foreach ($subProjData as $idLayer => $layData) : ?>
  1589. <tr>
  1590. <td colspan="<?php echo $tblCols + 1; ?>"
  1591. style="padding:0 6px; font-size:1.2em; line-height:2em"> &mdash; <?php echo $conf['layer'][$idLayer]['label']; ?></td>
  1592. </tr>
  1593. <tr>
  1594. <td style="width:24px"></td>
  1595. <td style="color:#888; padding:0 6px">typ</td>
  1596. <td style="color:#888; padding:0 6px; text-align:right">ilość</td>
  1597. <td style="color:#888; padding:0 6px; text-align:right">jednostka</td>
  1598. <?php if ($admin) : ?>
  1599. <td style="color:#888; padding:0 6px; text-align:right">cena jednostkowa</td>
  1600. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (domyślna)</td>
  1601. <?php endif; ?>
  1602. <?php if (!empty($subProjCost)) : ?>
  1603. <td style="color:#888; padding:0 6px; text-align:right">szacowany koszt [zł]</td>
  1604. <?php endif; ?>
  1605. </tr>
  1606. <?php foreach ($layData as $idType => $typeData) : ?>
  1607. <?php if (!$typeData['idType']) : ?>
  1608. <tr>
  1609. <td colspan="4">
  1610. <?php if ($admin) {
  1611. $fixLink = 'index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=' . $conf['layer'][$idLayer]['tabela_id'];
  1612. $fixLink .= '&f_' . $conf['layer'][$idLayer]['zasob_field'] . '=%3D' . $typeData['type'];
  1613. $fixLink .= '&_hash=' . uniqid();
  1614. $fixProjectLink = '<a target="_blank" href="' . $fixLink . "&f_ID_PROJECT={$idProject}" . '">' . "Popraw" . '</a>' . " (dane dla projektu [{$idProject}])";
  1615. $fixAllLink = '<a target="_blank" href="' . $fixLink . '">' . "Popraw wszystkie" . '</a>';
  1616. UI::alert('danger', "Niezdefiniowany typ: <code>{$typeData['type']}</code> - {$fixProjectLink}, {$fixAllLink}");
  1617. } ?>
  1618. <?php DBG::_('DBG', '>1', "Error conf", $conf, __CLASS__, __FUNCTION__, __LINE__); ?>
  1619. <?php DBG::_('DBG', '>1', "Error", $typeData, __CLASS__, __FUNCTION__, __LINE__); ?>
  1620. </td>
  1621. </tr>
  1622. <?php else : ?>
  1623. <?php $defPrice = (!empty($defCennik[$typeData['idType']]['price']))? $defCennik[$typeData['idType']]['price'] : 0; ?>
  1624. <?php $price = (!empty($cennik[$typeData['idType']]['price']))? $cennik[$typeData['idType']]['price'] : $defPrice; ?>
  1625. <tr>
  1626. <td style="width:24px"></td>
  1627. <td style="padding:0 6px" title="[<?php echo $typeData['idType']; ?>] <?php echo $typeData['type']; ?>"> &ndash; <?php echo $typeData['type']; ?></td>
  1628. <td style="padding:0 6px; text-align:right"><?php echo $typeData['ilosc']; ?></td>
  1629. <td style="padding:0 6px; text-align:right"><?php echo ProjectKosztorysSchema::getLayerJednostka($idLayer); ?></td>
  1630. <?php if ($admin) : ?>
  1631. <td style="padding:3px 6px; text-align:right">
  1632. <input type="text" style="text-align:right" class="form-control input-sm" name="price_<?php echo $typeData['idType']; ?>" value="<?php echo $price; ?>"/>
  1633. </td>
  1634. <?php endif; ?>
  1635. <?php if ($admin) : ?>
  1636. <td style="padding:3px 6px; text-align:right"><?php echo $defPrice; ?></td>
  1637. <?php endif; ?>
  1638. <?php if (!empty($subProjCost)) : ?>
  1639. <td style="padding:3px 6px; text-align:right"><?php echo number_format(V::get($typeData['idType'], 0, $subProjCost[$idSubProj]), 2, ',', ' '); ?></td>
  1640. <?php endif; ?>
  1641. </tr>
  1642. <?php endif; ?>
  1643. <?php endforeach; ?>
  1644. <?php endforeach; ?>
  1645. <?php endforeach; ?>
  1646. </tbody>
  1647. </table>
  1648. <?php endif; ?>
  1649. <link rel="stylesheet" type="text/css" href="static/sweetalert2.min.css">
  1650. <script src="static/sweetalert2.min.js"></script>
  1651. <script>
  1652. (function(){
  1653. var _updateProjektyOfertaSaveLink = '<?= Request::getPathUri(); ?>index.php?_route=UrlAction_ProjektyKosztorys&_task=updateProjektyOfertaAjax&idProject=<?= $idProject; ?>';
  1654. jQuery(document).on('p5UIBtnAjax:Kosztorys:updateProjektyOferta:click', function(e, n, payload) {
  1655. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:updateProjektyOferta:click', n, payload);<?php endif; ?>
  1656. });
  1657. jQuery(document).on('p5UIBtnAjax:Kosztorys:updateProjektyOferta:ajaxLoaded', function(e, n, payload) {
  1658. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:updateProjektyOferta:ajaxLoaded', n, payload);<?php endif; ?>
  1659. if ('success' != payload.type) {
  1660. jQuery.notify(payload.msg, payload.type);
  1661. return;
  1662. }
  1663. if (payload.body && payload.body.id > 0) {
  1664. jQuery(n).text(p5Utils__pricePrintPL(payload.body.price));
  1665. }
  1666. var id = payload.body.id;
  1667. var price = payload.body.price;
  1668. var unitType = payload.body.unitType;
  1669. var label = payload.body.label || '['+id+']';
  1670. if ('robocizna' != unitType) label += '<br><i style="color:#999">(cena domyślna: ' + p5Utils__pricePrintPL(payload.body.defaultPrice) + ')</i>';
  1671. swal({
  1672. title: 'Cena ' + (('robocizna' == unitType) ? 'robocizny' : 'zasobu') + ' [' + id + ']:',
  1673. html: label,
  1674. animation: false,
  1675. input: 'text',
  1676. inputPlaceholder: '0,00',
  1677. inputValue: p5Utils__pricePrintPL(price),
  1678. // inputAttributes: {'step': '0.01'},
  1679. showCancelButton: true,
  1680. confirmButtonText: 'Zapisz',
  1681. showLoaderOnConfirm: true,
  1682. preConfirm: Kosztorys__saveFormUpdateProjectOferta(id, unitType),
  1683. allowOutsideClick: false
  1684. }).then(function(responseBody) {
  1685. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:savedFormUpdateProjectOferta:ajaxLoaded', responseBody);<?php endif; ?>
  1686. if ('success' != responseBody.type) {
  1687. jQuery.notify(responseBody.msg || 'Wystąpiły błędy podczas aktualizacji ceny dla ['+id+']', 'error')
  1688. return;
  1689. }
  1690. jQuery.notify(responseBody.msg || 'Zaktualizowano cenę za ['+id+']', 'success')
  1691. jQuery.notify('Odśwież stronę żeby zobaczyć zmiany', 'info')
  1692. jQuery(n).removeClass('type_price-UNDEFINED')
  1693. jQuery(n).removeClass('type_price-GLOBAL')
  1694. jQuery(n).addClass('type_price-PROJECT')
  1695. jQuery(n).text(p5Utils__pricePrintPL(responseBody.price))
  1696. // TODO: update dom price
  1697. // if (responseBody.update_data) {
  1698. // budget__renderCosts(responseBody.update_data['costs']);
  1699. // }
  1700. }).catch(function(e) {// eg. hit Cancel
  1701. })
  1702. });
  1703. function Kosztorys__saveFormUpdateProjectOferta(idType, type) {
  1704. var idType = idType, type = type;// TODO: zasob / robocizna
  1705. return function(price) {
  1706. return new Promise(function(resolve, reject) {
  1707. price = p5Utils__parseFloatOrZero(price)
  1708. if (price < 0) {
  1709. reject('Kwota musi być nie mniejsza od zera.')
  1710. } else {
  1711. superagent
  1712. .post(_updateProjektyOfertaSaveLink + '&idType=' + idType + '&unitType=' + type)
  1713. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  1714. .send({
  1715. price: price
  1716. })
  1717. .set('Accept', 'application/json')
  1718. .end(function(err, res) {
  1719. <?php if (DBG::isActive()) : ?>console.log('#widget::Kosztorys/saveFormUpdateProjectOferta: res:', res, 'res.body:', res.body);<?php endif; ?>
  1720. if (err || !res.ok || 'application/json' !== res.type) reject("Request error")
  1721. if (!res.body.id || res.body.id <= 0) reject(res.body.msg || "Wystąpiły błędy podczas dodawaniu kosztu")
  1722. resolve(res.body)
  1723. })
  1724. }
  1725. })
  1726. }
  1727. }
  1728. })();
  1729. </script>
  1730. <?php
  1731. }
  1732. public function checkGeomDuplicate() {
  1733. // TODO: for every schema.layer
  1734. $sqlTblName = 'Rozdzielcza_Kabel_Swiatlowodowy_wsg84';
  1735. $sql = "select a.ID, b.ID, aswkt(a.the_geom)
  1736. from `{$sqlTblName}` a
  1737. join `$sqlTblName` b on(b.the_geom = a.the_geom and a.ID != b.ID)
  1738. ";
  1739. }
  1740. public function getKosztorysData($idProject, $idOrder = 0, $admin = 0) {// TODO: before was: ($idProject, $admin = 0)
  1741. static $_data = null;
  1742. if (null === $_data) {
  1743. $idSubProject = $this->getModel($idProject)->getSubProjectIds();
  1744. $idSubProject[] = $idProject;
  1745. $_data = $this->fetchData($idSubProject, $idOrder, $admin);
  1746. }
  1747. return $_data;
  1748. }
  1749. public function fetchData($idProject, $idOrder = 0, $admin = 0) {// TODO: before was: ($idProject, $admin = 0)
  1750. $data = array();
  1751. $data['summary'] = array();// [ $idType ] = ['type', 'jednostka', 'ilosc']
  1752. $data['rawDataByType'] = array();
  1753. if (empty($idProject)) return $data;
  1754. $schema = ProjectKosztorysSchema::getSchema();// OK only 'config'
  1755. $conf = $schema['config'];
  1756. $pdo = DB::getPDO();
  1757. foreach ($conf['layer'] as $idLayer => $layer) {
  1758. if (!$layer['tabela_name']) continue;// TODO: throw error in validate
  1759. if ('ZASOB' == $layer['zasob_type']) {
  1760. } else if ('ZASOB_ID' == $layer['zasob_type']) {
  1761. } else {
  1762. continue;// TODO: validate config error
  1763. }
  1764. $tblName = $layer['tabela_name'];
  1765. $sqlIloscField = (!empty($layer['ilosc_field']))? $layer['ilosc_field'] : 'ID';
  1766. $sqlIlosc = ('SZTUKA' == $layer['jednostka'])? "count(1)" : "sum(t.{$sqlIloscField})";
  1767. $sqlZasobField = (!empty($layer['zasob_field']))? $layer['zasob_field'] : 'ID';
  1768. $sqlGroupBy = "group by t.ID_PROJECT" . ((!empty($layer['zasob_field']))? ", t.{$sqlZasobField}" : '');
  1769. $sqlIdProject = (is_array($idProject))? implode(",", $idProject) : (int)$idProject;
  1770. $sql = "
  1771. select t.{$sqlZasobField}
  1772. , t.ID_PROJECT
  1773. , {$sqlIlosc} as ilosc
  1774. from {$tblName} t
  1775. where t.ID_PROJECT in({$sqlIdProject})
  1776. and t.the_geom is not null
  1777. {$sqlGroupBy}
  1778. ";
  1779. $data['_DBG_sql'][$idLayer] = $sql;
  1780. $rawLayData = $pdo->fetchAll($sql);
  1781. $rawLayData = array_filter($rawLayData, function($row) {
  1782. return ($row['ilosc'] > 0);
  1783. });
  1784. foreach ($rawLayData as $idx => &$row) {
  1785. if ('ZASOB' == $layer['zasob_type']) {
  1786. $row['type'] = trim($row[$sqlZasobField]);
  1787. $row['idType'] = array_search($row['type'], $conf['type']);
  1788. } else if ('ZASOB_ID' == $layer['zasob_type']) {
  1789. $row['type'] = $layer['zasob_label'];
  1790. $row['idType'] = (array_key_exists($layer['zasob_id'], $conf['type']))? $layer['zasob_id'] : null;
  1791. }
  1792. $row['idLayer'] = $idLayer;
  1793. unset($row[$sqlZasobField]);
  1794. $data['rawDataByType'][] = $row;
  1795. }
  1796. }
  1797. //DBG::_(true, true, "data", $data, __CLASS__, __FUNCTION__, __LINE__);
  1798. //DBG::table("data['rawDataByType']", $data['rawDataByType'], __CLASS__, __FUNCTION__, __LINE__);
  1799. if ($admin) {
  1800. foreach ($data['rawDataByType'] as $row) {
  1801. $layer = $conf['layer'][$row['idLayer']];
  1802. if ($row['idType'] <= 0) {
  1803. UI::alert('warning', "Pomijanie nieznanego oznaczenia '{$row['type']}' w ilości {$row['ilosc']} - warstwa '{$layer['label']}' ({$layer['tabela_name']}), projekt nr {$row['ID_PROJECT']}");
  1804. }
  1805. }
  1806. }
  1807. $data['rawDataByType'] = array_filter($data['rawDataByType'], function($row) {
  1808. return ($row['idType'] > 0);
  1809. });
  1810. //DBG::table("data['rawDataByType'] - clean", $data['rawDataByType'], __CLASS__, __FUNCTION__, __LINE__);
  1811. $data['by_layer'] = array_reduce($data['rawDataByType'], function($result, $row) {
  1812. $idLayer = $row['idLayer'];
  1813. $idType = $row['idType'];
  1814. if (!array_key_exists($idLayer, $result)) $result[$idLayer] = array();
  1815. if (!array_key_exists($idType, $result[$idLayer])) {
  1816. $result[$idLayer][$idType] = $row;
  1817. } else {
  1818. $result[$idLayer][$idType]['ilosc'] += $row['ilosc'];
  1819. }
  1820. return $result;
  1821. }, array());
  1822. //DBG::_(true, true, "data['by_layer']", $data['by_layer'], __CLASS__, __FUNCTION__, __LINE__);
  1823. $data['summary'] = array();
  1824. foreach ($data['by_layer'] as $idLayer => $iloscByType) {
  1825. $layData = array();
  1826. $layData['label'] = $conf['layer'][$idLayer]['label'];
  1827. $layData['data'] = $iloscByType;
  1828. foreach ($layData['data'] as &$iloscData) {
  1829. $iloscData['jednostka'] = $conf['layer'][$idLayer]['jednostka'];
  1830. }
  1831. $data['summary'][$idLayer] = $layData;
  1832. }
  1833. //DBG::_(true, true, "data['summary']", $data['summary'], __CLASS__, __FUNCTION__, __LINE__);
  1834. $data['by_project'] = array_reduce($data['rawDataByType'], function($result, $row) {
  1835. $idProj = $row['ID_PROJECT'];
  1836. $idLayer = $row['idLayer'];
  1837. $idType = $row['idType'];
  1838. if (!array_key_exists($idProj, $result)) $result[$idProj] = array();
  1839. if (!array_key_exists($idLayer, $result[$idProj])) $result[$idProj][$idLayer] = array();
  1840. if (!array_key_exists($idType, $result[$idProj][$idLayer])) {
  1841. $result[$idProj][$idLayer][$idType] = $row;
  1842. } else {
  1843. $result[$idProj][$idLayer][$idType]['ilosc'] += $row['ilosc'];
  1844. }
  1845. return $result;
  1846. }, array());
  1847. //DBG::_(true, true, "data['by_project']", $data['by_project'], __CLASS__, __FUNCTION__, __LINE__);
  1848. return $data;
  1849. }
  1850. }