ProjektyOfertaAdminKosztorys.php 23 KB

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