ProjektyOfertaAdminKosztorys.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. Lib::loadClass('Route_ProjektyKosztorysBase');
  9. class Route_UrlAction_ProjektyOfertaAdminKosztorys extends Route_ProjektyKosztorysBase {
  10. public $_model = array();
  11. public function defaultAction() {
  12. // TODO: check if user is allowed to run this action
  13. UI::gora();
  14. if (1 != V::get('_print', '', $_GET)) UI::menu();
  15. try {
  16. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  17. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  18. $this->panel($idProject, $idCompany);
  19. if (1 != V::get('_print', '', $_GET)) {
  20. UI::startContainer(['style'=>'text-align:right']);
  21. UI::link('link', "<i class=\"glyphicon glyphicon-print\"></i> Drukuj", Request::getPathUri() . "index.php?_route=UrlAction_ProjektyOfertaAdminKosztorys&ID_COMPANY={$idCompany}&_print=1");
  22. UI::endContainer();
  23. }
  24. $this->defaultOferta($idCompany);// TODO: $idCompany
  25. } catch (Exception $e) {
  26. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  27. }
  28. UI::dol();
  29. }
  30. public function additionalPanel($idProject, $idCompany = 0) {
  31. UI::startTag('div', ['class'=>'container', 'style'=>'margin-top:1em']);
  32. UI::startTag('form', ['action'=>"", 'method'=>'get', 'class'=>'form-inline']);
  33. UI::startTag('div', ['class'=>"form-group"]);
  34. UI::emptyTag('input', ['type'=>"hidden", 'name'=>"_route", 'value'=>"UrlAction_ProjektyOfertaAdminKosztorys"]);
  35. UI::tag('label', ['for'=>'ID_COMPANY', 'style'=>'margin-right:1em'], "Nr kontrahenta (0 = cennik główny)");
  36. UI::emptyTag('input', ['type'=>"text", 'name'=>"ID_COMPANY", 'value'=>$idCompany, 'class'=>'form-control', 'style'=>'margin-right:1em']);// TODO: Typespecial for Companies
  37. UI::emptyTag('input', ['type'=>"submit", 'value'=>"Wybierz", 'class'=>'btn btn-primary']);
  38. UI::endTag('div');// .form-group
  39. UI::endTag('form');
  40. UI::endTag('div');// .container
  41. }
  42. public function ofertaAdminAction() {
  43. // TODO: check if user is allowed to run this action
  44. UI::gora();
  45. UI::menu();
  46. try {
  47. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  48. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  49. $admin = true;
  50. $ofertaArgs = compact('idProject', 'idCompany', 'admin');
  51. $this->panel($idProject, $idCompany);
  52. if ($idProject > 0) {
  53. $this->oferta($ofertaArgs);
  54. } else {
  55. $this->defaultOferta();
  56. }
  57. } catch (Exception $e) {
  58. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  59. }
  60. UI::dol();
  61. }
  62. public function ofertaCompanyAction() {
  63. // TODO: check if user is allowed to run this action
  64. UI::gora();
  65. UI::menu();
  66. try {
  67. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  68. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');// TODO: $idCompany from ADMIN_USERS
  69. $admin = false;
  70. $companyAdmin = true;
  71. $ofertaArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin');
  72. $this->panel($idProject, $idCompany);
  73. if ($idProject > 0) {
  74. $this->oferta($ofertaArgs);
  75. } else {
  76. $this->defaultOferta();
  77. }
  78. } catch (Exception $e) {
  79. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  80. }
  81. UI::dol();
  82. }
  83. public function ofertaDefaultAdminAction() {
  84. // TODO: check if user is allowed to run this action
  85. UI::gora();
  86. UI::menu();
  87. try {
  88. $idProject = 0;
  89. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  90. $this->panel($idProject, $idCompany);
  91. $this->defaultOferta();
  92. } catch (Exception $e) {
  93. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  94. }
  95. UI::dol();
  96. }
  97. public function ofertaAction() {
  98. // TODO: check if user is allowed to run this action
  99. UI::gora();
  100. UI::menu();
  101. try {
  102. $idProject = V::get('ID_PROJECT', 0, $_REQUEST, 'int');
  103. $idCompany = V::get('ID_COMPANY', 0, $_REQUEST, 'int');
  104. $admin = false;
  105. if (!$idProject) throw new Exception("Wrong param in 'ID_PROJECT' - expected integer!");// TODO: show select box if not defined
  106. $ofertaArgs = compact('idProject', 'idCompany', 'admin');
  107. $this->panel($idProject, $idCompany);
  108. $this->oferta($ofertaArgs);
  109. } catch (Exception $e) {
  110. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  111. }
  112. UI::dol();
  113. }
  114. public function saveOffer($idProject, $idCompany, $args, $admin = false) {
  115. DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
  116. if (!$admin) return;
  117. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  118. DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  119. $pdo = DB::getPDO();
  120. {
  121. $add_id_zasob = 0;
  122. $add_unit = '';
  123. $add_price = 0;
  124. $add_quantity = 0;
  125. $add__sth = $pdo->prepare("
  126. insert into CRM_LISTA_ZASOBOW_OFFERS (
  127. CRM_LISTA_ZASOBOW_ID
  128. , COMPANIES_ID
  129. , ID_PROJECT
  130. , OFFER_PRICE_PER_RESOURCE_UNIT
  131. , RESOURCE_UNIT_TYPE
  132. , OFFER_UNIT_TYPE
  133. , REQUIRED_RESOURCE_UNITS
  134. , A_RECORD_CREATE_AUTHOR
  135. , A_RECORD_CREATE_DATE
  136. ) values (
  137. :id_zasob
  138. , :id_company
  139. , :id_project
  140. , :price
  141. , :unit
  142. , :unit
  143. , :quantity
  144. , :author
  145. , NOW()
  146. )
  147. ");
  148. $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  149. $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  150. $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  151. $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
  152. $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
  153. $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
  154. $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
  155. }
  156. {
  157. $edit_id_company = $idCompany;
  158. $edit_id_zasob = 0;
  159. $edit_unit = '';
  160. $edit_price = 0;
  161. $edit_author = 0;
  162. $edit_quantity = 0;
  163. $edit__sth = $pdo->prepare("
  164. update CRM_LISTA_ZASOBOW_OFFERS
  165. set CRM_LISTA_ZASOBOW_ID = :id_zasob
  166. , COMPANIES_ID = :id_company
  167. , ID_PROJECT = :id_project
  168. , OFFER_PRICE_PER_RESOURCE_UNIT = :price
  169. , RESOURCE_UNIT_TYPE = :unit
  170. , OFFER_UNIT_TYPE = :unit
  171. , REQUIRED_RESOURCE_UNITS = :quantity
  172. , A_RECORD_UPDATE_AUTHOR = :author
  173. , A_RECORD_UPDATE_DATE = NOW()
  174. where ID = :id
  175. ");
  176. $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
  177. $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  178. $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  179. $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  180. $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
  181. $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
  182. $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
  183. $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
  184. }
  185. $schema = ProjectKosztorysSchema::getSchema();
  186. foreach ($schema['config']['layer'] as $idLayer => $layData) {
  187. DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
  188. foreach ($layData['type'] as $idType => $typeLabel) {
  189. $edit_id = V::get("edit_price_id_{$idType}", '', $args);
  190. $add_price = V::get("price_{$idType}", '', $args);
  191. DBG::_('DBG', '>1', "typeLabel (edit={$edit_id}, price={$add_price})", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  192. if ($edit_id > 0) {
  193. $edit_price = V::get("price_{$idType}", '', $args);
  194. $edit_price = str_replace(',', '.', $edit_price);
  195. if ($edit_id > 0 && $edit_price > 0) {
  196. $edit_id_zasob = $idType;
  197. $edit_unit = $layData['jednostka'];
  198. // TODO: check if anything change
  199. DBG::_('DBG', '>1', "EDIT price for idZasob({$idType}) {price:{$edit_price}, id_offer:{$edit_id}}", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  200. $edit__sth->execute();
  201. // TODO: add to hist
  202. }
  203. } else {
  204. $add_price = V::get("price_{$idType}", '', $args);
  205. $add_price = str_replace(',', '.', $add_price);
  206. if ($add_price > 0) {
  207. $add_id_zasob = $idType;
  208. $add_unit = $layData['jednostka'];
  209. DBG::_('DBG', '>1', "ADD price for idZasob({$idType}) {price:{$add_price}}", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  210. $add__sth->execute();
  211. }
  212. }
  213. }
  214. }
  215. }
  216. public function saveDefaultOffer($args) {
  217. DBG::_('DBG', '>1', "args", $args, __CLASS__, __FUNCTION__, __LINE__);
  218. $cennik = ProjectKosztorysCennik::getDefaultCennik();
  219. DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  220. $idCompany = 0;
  221. $idProject = 0;
  222. $pdo = DB::getPDO();
  223. {
  224. $add_id_zasob = 0;
  225. $add_unit = '';
  226. $add_price = 0;
  227. $add_quantity = 0;
  228. $add__sth = $pdo->prepare("
  229. insert into CRM_LISTA_ZASOBOW_OFFERS (
  230. CRM_LISTA_ZASOBOW_ID
  231. , COMPANIES_ID
  232. , ID_PROJECT
  233. , OFFER_PRICE_PER_RESOURCE_UNIT
  234. , RESOURCE_UNIT_TYPE
  235. , OFFER_UNIT_TYPE
  236. , REQUIRED_RESOURCE_UNITS
  237. , A_RECORD_CREATE_AUTHOR
  238. , A_RECORD_CREATE_DATE
  239. ) values (
  240. :id_zasob
  241. , :id_company
  242. , :id_project
  243. , :price
  244. , :unit
  245. , :unit
  246. , :quantity
  247. , :author
  248. , NOW()
  249. )
  250. ");
  251. $add__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  252. $add__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  253. $add__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  254. $add__sth->bindValue(':quantity', $add_quantity, PDO::PARAM_STR);
  255. $add__sth->bindParam(':id_zasob', $add_id_zasob, PDO::PARAM_INT);
  256. $add__sth->bindParam(':price', $add_price, PDO::PARAM_STR);
  257. $add__sth->bindParam(':unit', $add_unit, PDO::PARAM_STR);
  258. }
  259. {
  260. $edit_id_zasob = 0;
  261. $edit_unit = '';
  262. $edit_price = 0;
  263. $edit_author = 0;
  264. $edit_quantity = 0;
  265. $edit__sth = $pdo->prepare("
  266. update CRM_LISTA_ZASOBOW_OFFERS
  267. set CRM_LISTA_ZASOBOW_ID = :id_zasob
  268. , COMPANIES_ID = :id_company
  269. , ID_PROJECT = :id_project
  270. , OFFER_PRICE_PER_RESOURCE_UNIT = :price
  271. , RESOURCE_UNIT_TYPE = :unit
  272. , OFFER_UNIT_TYPE = :unit
  273. , REQUIRED_RESOURCE_UNITS = :quantity
  274. , A_RECORD_UPDATE_AUTHOR = :author
  275. , A_RECORD_UPDATE_DATE = NOW()
  276. where ID = :id
  277. ");
  278. $edit__sth->bindParam(':id', $edit_id, PDO::PARAM_INT);
  279. $edit__sth->bindValue(':author', User::getLogin(), PDO::PARAM_STR);
  280. $edit__sth->bindValue(':id_project', $idProject, PDO::PARAM_INT);
  281. $edit__sth->bindValue(':id_company', $idCompany, PDO::PARAM_INT);
  282. $edit__sth->bindValue(':quantity', $edit_quantity, PDO::PARAM_STR);
  283. $edit__sth->bindParam(':id_zasob', $edit_id_zasob, PDO::PARAM_INT);
  284. $edit__sth->bindParam(':price', $edit_price, PDO::PARAM_STR);
  285. $edit__sth->bindParam(':unit', $edit_unit, PDO::PARAM_STR);
  286. }
  287. $schema = ProjectKosztorysSchema::getSchema();
  288. foreach ($schema['config']['layer'] as $idLayer => $layData) {
  289. DBG::_('DBG', '>1', "layData", $layData, __CLASS__, __FUNCTION__, __LINE__);
  290. foreach ($layData['type'] as $idType => $typeLabel) {
  291. DBG::_('DBG', '>1', "typeLabel", $typeLabel, __CLASS__, __FUNCTION__, __LINE__);
  292. $edit_id = V::get("edit_price_id_{$idType}", '', $args);
  293. if ($edit_id > 0) {
  294. $edit_price = V::get("price_{$idType}", '', $args);
  295. $edit_price = str_replace(',', '.', $edit_price);
  296. if ($edit_id > 0 && $edit_price > 0) {
  297. $edit_id_zasob = $idType;
  298. $edit_unit = $layData['jednostka'];
  299. // TODO: check if anything change
  300. $edit__sth->execute();
  301. // TODO: add to hist
  302. }
  303. } else {
  304. $add_price = V::get("price_{$idType}", '', $args);
  305. $add_price = str_replace(',', '.', $add_price);
  306. if ($add_price > 0) {
  307. $add_id_zasob = $idType;
  308. $add_unit = $layData['jednostka'];
  309. $add__sth->execute();
  310. }
  311. }
  312. }
  313. }
  314. }
  315. public function defaultOferta($idCompany = 0) {
  316. $priceEditJs = (1 != V::get('_print', '', $_GET)) ? Request::getPathUri() . "index.php?_route=UrlAction_ProjektyOfertaAdminKosztorys&_task=updateAdminOfertaAjax&idCompany={$idCompany}" : false;
  317. $schema = ProjectKosztorysSchema::getSchema();
  318. if (empty($schema['config']['type'])) throw new Exception("Schema error - brak zdefiniowanych typów");
  319. $defCennik = ProjectKosztorysCennik::getDefaultCennik();
  320. $cennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  321. $workCennik = ProjectKosztorysCennik::getWorkCennik($idProject = 0, $idCompany);
  322. DBG::_('DBG', '>1', "defCennik", $defCennik, __CLASS__, __FUNCTION__, __LINE__);
  323. DBG::_('DBG', '>1', "cennik", $cennik, __CLASS__, __FUNCTION__, __LINE__);
  324. DBG::_('DBG', '>1', "workCennik", $workCennik, __CLASS__, __FUNCTION__, __LINE__);
  325. $title = "Cennik domyślny " . ($idCompany ? "firmy [{$idCompany}]" : '') . " dla Kosztorysów";
  326. UI::setTitleJsTag($title);
  327. ?>
  328. <div class="container">
  329. <h1><?= $title; ?></h1>
  330. <table class="tabel table-bordered table-hover" style="width:100%; margin-bottom:6px; page-break-inside:avoid">
  331. <tbody>
  332. <?php foreach ($schema['config']['layer'] as $idLayer => $layData) : ?>
  333. <tr>
  334. <td colspan="5" style="padding:0 6px; font-size:1.2em; line-height:2em"> &mdash; <?php echo $layData['label']; ?></td>
  335. </tr>
  336. <tr>
  337. <td style="color:#888; padding:0 6px">id zasobu</td>
  338. <td style="color:#888; padding:0 6px">typ</td>
  339. <td style="color:#888; padding:0 6px">jednostka miary</td>
  340. <td style="color:#888; padding:0 6px; text-align:right">cena jednostkowa
  341. <?php if ($priceEditJs && $idCompany > 0) : ?>
  342. <button type="button"
  343. class="btn btn-xs btn-link"
  344. data-toggle="popover"
  345. data-trigger="hover"
  346. title="Cena jednostkowa"
  347. data-content="Cena na czerwono oznacza cenę z cennika głównego, cena na zielono to cena wg oferty danej firmy"><i class="glyphicon glyphicon-question-sign"></i></button>
  348. <?php endif; ?>
  349. </td>
  350. <td style="color:#888; padding:0 6px; text-align:right">cena jedn. (robocizna)</td>
  351. </tr>
  352. <?php foreach ($layData['type'] as $idType => $typeLabel) : ?>
  353. <?php $defPrice = (!empty($defCennik[$idType]['price']))? $defCennik[$idType]['price'] : 0; ?>
  354. <?php $price = (!empty($cennik[$idType]) && array_key_exists('price', $cennik[$idType]))? $cennik[$idType]['price'] : $defPrice; ?>
  355. <?php
  356. $typePrice = 'UNDEFINED';
  357. if (!empty($defCennik[$idType]['price'])) $typePrice = 'GLOBAL';
  358. if (!empty($cennik[$idType]) && array_key_exists('price', $cennik[$idType])) $typePrice = 'PROJECT';
  359. ?>
  360. <?php $workPrice = (!empty($workCennik[$idType]['price']))? $workCennik[$idType]['price'] : 0; ?>
  361. <tr>
  362. <td style="width:100px; padding:0 6px"><?php echo $idType; ?></td>
  363. <td style="padding:0 6px" title="[<?php echo $idType; ?>] <?php echo $typeLabel; ?>"><?php echo $typeLabel; ?></td>
  364. <td style="padding:0 6px"><?php echo $layData['jednostka']; ?></td>
  365. <!--
  366. <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>
  367. -->
  368. <td
  369. <?php if ($priceEditJs) : ?>
  370. onClick="return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Kosztorys:updateAdminOferta', { href: '<?= "{$priceEditJs}&idType={$idType}&unitType=zasob"; ?>' })"
  371. <?php endif; ?>
  372. class="type_price-<?= $typePrice; ?>"
  373. style="padding:3px 6px; text-align:right">
  374. <?= number_format($price, 2, ',', ' '); ?>
  375. </td>
  376. <td
  377. <?php if ($priceEditJs) : ?>
  378. onClick="return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Kosztorys:updateAdminOferta', { href: '<?= "{$priceEditJs}&idType={$idType}&unitType=robocizna"; ?>' })"
  379. <?php endif; ?>
  380. style="padding:3px 6px; text-align:right">
  381. <?= number_format($workPrice, 2, ',', ' '); ?>
  382. </td>
  383. </tr>
  384. <?php endforeach; ?>
  385. <?php endforeach; ?>
  386. </tbody>
  387. </table>
  388. </div>
  389. <style type="text/css">
  390. .type_price-UNDEFINED { color:silver }
  391. .type_price-UNDEFINED input { color:silver }
  392. .type_price-GLOBAL { color:red }
  393. .type_price-GLOBAL input { color:red }
  394. .type_price-PROJECT { color:green }
  395. .type_price-PROJECT input { color:green }
  396. /* Print Styles */
  397. @media print {
  398. body { font-size:10px; }
  399. th, td { font-size:10px; }
  400. h1 { font-size:2em; }
  401. h2 { font-size:1.6em; }
  402. h3 { font-size:1.4em; }
  403. h4 { font-size:1.2em; }
  404. }
  405. </style>
  406. <link rel="stylesheet" type="text/css" href="static/sweetalert2.min.css">
  407. <script src="static/sweetalert2.min.js"></script>
  408. <?php if ($priceEditJs) : ?>
  409. <script>
  410. (function(){
  411. var _updateAdminOfertaSaveLink = '<?= $priceEditJs ?>';
  412. jQuery(document).on('p5UIBtnAjax:Kosztorys:updateAdminOferta:click', function(e, n, payload) {
  413. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:updateAdminOferta:click', n, payload);<?php endif; ?>
  414. });
  415. jQuery(document).on('p5UIBtnAjax:Kosztorys:updateAdminOferta:ajaxLoaded', function(e, n, payload) {
  416. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:updateAdminOferta:ajaxLoaded', n, payload);<?php endif; ?>
  417. if ('success' != payload.type) {
  418. jQuery.notify(payload.msg, payload.type);
  419. return;
  420. }
  421. if (payload.body && payload.body.id > 0) {
  422. jQuery(n).text(p5Utils__pricePrintPL(payload.body.price));
  423. }
  424. var id = payload.body.id;
  425. var price = payload.body.price;
  426. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:updateAdminOferta:ajaxLoaded price', price, 'PL', p5Utils__pricePrintPL(price));<?php endif; ?>
  427. var unitType = payload.body.unitType;
  428. var label = payload.body.label || '['+id+']';
  429. label += '<br><i style="color:#999">(cena domyślna: ' + p5Utils__pricePrintPL(payload.body.defaultPrice) + ')</i>';
  430. swal({
  431. title: 'Cena ' + (('robocizna' == unitType) ? 'robocizny' : 'zasobu') + ' [' + id + ']:',
  432. html: label,
  433. animation: false,
  434. input: 'text',
  435. inputPlaceholder: '0,00',
  436. inputValue: (0 === price) ? price : p5Utils__pricePrintPL(price),
  437. showCancelButton: true,
  438. confirmButtonText: 'Zapisz',
  439. showLoaderOnConfirm: true,
  440. preConfirm: Kosztorys__saveFormUpdateAdminOferta(id, unitType),
  441. allowOutsideClick: false
  442. }).then(function(responseBody) {
  443. <?php if (DBG::isActive()) : ?>console.log('event p5UIBtnAjax:Kosztorys:savedFormUpdateProjectOferta:ajaxLoaded', responseBody);<?php endif; ?>
  444. if ('success' != responseBody.type) {
  445. jQuery.notify(responseBody.msg || 'Wystąpiły błędy podczas aktualizacji ceny dla ['+id+']', 'error')
  446. return;
  447. }
  448. jQuery.notify(responseBody.msg || 'Zaktualizowano cenę za ['+id+']', 'success')
  449. jQuery.notify('Odśwież stronę żeby zobaczyć zmiany', 'info')
  450. jQuery(n).removeClass('type_price-UNDEFINED')
  451. jQuery(n).removeClass('type_price-GLOBAL')
  452. jQuery(n).addClass('type_price-PROJECT')
  453. jQuery(n).text(p5Utils__pricePrintPL(responseBody.price))
  454. // TODO: update dom price
  455. // if (responseBody.update_data) {
  456. // budget__renderCosts(responseBody.update_data['costs']);
  457. // }
  458. }).catch(function(e) {// eg. hit Cancel
  459. })
  460. });
  461. function Kosztorys__saveFormUpdateAdminOferta(idType, type) {
  462. var idType = idType, type = type;// TODO: zasob / robocizna
  463. return function(price) {
  464. return new Promise(function(resolve, reject) {
  465. price = p5Utils__parseFloatOrZero(price)
  466. if (price < 0) {
  467. reject('Kwota musi być nie mniejsza od zera.')
  468. } else {
  469. superagent
  470. .post(_updateAdminOfertaSaveLink + '&idType=' + idType + '&unitType=' + type)
  471. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  472. .send({
  473. price: price
  474. })
  475. .set('Accept', 'application/json')
  476. .end(function(err, res) {
  477. <?php if (DBG::isActive()) : ?>console.log('#widget::Kosztorys/saveFormUpdateProjectOferta: res:', res, 'res.body:', res.body);<?php endif; ?>
  478. if (err || !res.ok || 'application/json' !== res.type) reject("Request error")
  479. if (!res.body.id || res.body.id <= 0) reject(res.body.msg || "Wystąpiły błędy podczas dodawaniu kosztu")
  480. resolve(res.body)
  481. })
  482. }
  483. })
  484. }
  485. }
  486. $(function () {
  487. $('[data-toggle="popover"]').popover()
  488. })
  489. })();
  490. </script>
  491. <?php endif; ?>
  492. <?php
  493. }
  494. public function updateAdminOfertaAjaxAction() {
  495. $args = array();
  496. $args['idCompany'] = V::get('idCompany', 0, $_GET, 'int');
  497. $args['idType'] = V::get('idType', 0, $_GET, 'int');
  498. $args['unitType'] = V::get('unitType', '', $_GET, 'word');
  499. Response::sendTryCatchJson(array($this, 'updateAdminOfertaAjax'), $args);
  500. }
  501. public function updateAdminOfertaAjax($args) {
  502. $idCompany = V::get('idCompany', 0, $args, 'int');
  503. $idType = V::get('idType', 0, $args, 'int');
  504. $unitType = V::get('unitType', '', $args, 'word');
  505. if ($idCompany < 0) throw new Exception("Wrong param idCompany");// $idCompany may be equal 0
  506. if (empty($idType) || $idType <= 0) throw new Exception("Wrong param idType");
  507. if (empty($unitType) || !in_array($unitType, array('zasob', 'robocizna'))) throw new Exception("Wrong param unitType");
  508. $response = array();
  509. if (DBG::isActive()) $response['_idProject'] = $idCompany;
  510. if (DBG::isActive()) $response['_idType'] = $idType;
  511. $jednostka = '';
  512. if ('robocizna' == $unitType) $jednostka = 'ROBOCIZNA';
  513. else if ('zasob' == $unitType) {
  514. $schema = ProjectKosztorysSchema::getSchema();
  515. foreach ($schema['config']['layer'] as $idLayer => $layData) {
  516. if (array_key_exists($idType, $layData['type'])) $jednostka = $layData['jednostka'];
  517. }
  518. }
  519. if (DBG::isActive()) $response['_unit'] = $jednostka;
  520. $reqJson = Request::getRequestJson();
  521. if (!empty($reqJson)) {
  522. if (!array_key_exists('price', $reqJson)) throw new Exception("Missing param price");
  523. $price = V::get('price', 0, $reqJson, 'float');
  524. ProjectKosztorysCennik::updatePriceDefaultCennik($idType, $idCompany, $price, $jednostka);
  525. }
  526. $response['id'] = $idType;
  527. $response['unitType'] = $unitType;
  528. $response['label'] = ProjectKosztorysCennik::getTypeLabel($idType);
  529. $response['defaultPrice'] = ProjectKosztorysCennik::getPriceDefaultCennik($idType, 0, $jednostka);
  530. $response['price'] = ProjectKosztorysCennik::getPrice($idType, $idProject = 0, $idCompany, $jednostka);
  531. $response['msg'] = "";
  532. $response['type'] = "success";
  533. return $response;
  534. }
  535. public function oferta($args) {
  536. $idProject = $args['idProject'];
  537. $idCompany = V::get('idCompany', 0, $args);
  538. $admin = V::get('admin', false, $args);
  539. $companyAdmin = V::get('companyAdmin', false, $args);
  540. if ($admin && '1' == V::get('save_offer', '', $_POST)) {
  541. $this->saveOffer($idProject, $idCompany, $_POST, $admin);
  542. }
  543. $defCennik = ProjectKosztorysCennik::getDefaultCennik($idCompany);
  544. $cennik = ProjectKosztorysCennik::getCennik($idProject, $idCompany);
  545. $viewLayerDataArgs = compact('idProject', 'idCompany', 'admin', 'companyAdmin');
  546. UI::setTitleJsTag("Oferta " . (($idProject)? " [{$idProject}] " : '') . (($idCompany)? " Kontrahent({$idCompany})" : '') . " - Kosztorysy");
  547. ?>
  548. <div class="container">
  549. <?php if ($admin) : ?>
  550. <form action="" method="post">
  551. <?php endif; ?>
  552. <?php $this->viewLayersData($viewLayerDataArgs); ?>
  553. <?php if ($admin || $companyAdmin) : ?>
  554. <?php foreach ($cennik as $item) : ?>
  555. <input type="hidden" name="edit_price_id_<?php echo $item['id_zasob']; ?>" value="<?php echo $item['ID']; ?>">
  556. <?php endforeach; ?>
  557. <input type="hidden" name="save_offer" value="1">
  558. <hr><input class="btn btn-primary" type="submit" value="Zapisz ofertę">
  559. </form>
  560. <?php endif; ?>
  561. </div>
  562. <?php
  563. }
  564. }