Bocian.php.addItemToRaport.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. var createReactClass = window.p5VendorJs.createReactClass;
  2. var h = window.p5VendorJs.React.createElement;
  3. var ReactDOM = window.p5VendorJs.ReactDOM;
  4. var AsyncTypeahead = window.p5VendorJs.AsyncTypeahead;
  5. var swal = window.swal;
  6. var DBG = DBG || false;
  7. function convertXlinkToObjects(items) {
  8. var _childCache = {}; // fieldName => { pk => child }
  9. items.forEach(function (item) {
  10. var refFields = Object.keys(item).filter(function (fieldName) {
  11. return (-1 !== fieldName.indexOf(':') && item[fieldName]);
  12. });
  13. refFields.forEach(function (fieldName) {
  14. item[fieldName].forEach(function (child) {
  15. if (1 === Object.keys(child).length && 'xlink' in child) {
  16. // xlink
  17. } else if (Object.keys(child).length > 1) {
  18. if ('@primaryKey' in child) {
  19. if (!(fieldName in _childCache)) _childCache[fieldName] = {};
  20. _childCache[fieldName][ child['@primaryKey'] ] = child;
  21. }
  22. }
  23. })
  24. })
  25. return item;
  26. });
  27. DBG && console.log('DBG convertXlinkToObjects', { _childCache, items });
  28. return items.map(function (item) {
  29. var refFields = Object.keys(item).filter(function (fieldName) {
  30. return (-1 !== fieldName.indexOf(':') && item[fieldName]);
  31. });
  32. refFields.forEach(function (fieldName) {
  33. item[fieldName] = item[fieldName].map(function (child) {
  34. if (1 === Object.keys(child).length && 'xlink' in child) {
  35. var pk = child['xlink'].split('.').pop();
  36. return _childCache[fieldName][pk];
  37. }
  38. return child;
  39. })
  40. })
  41. return item;
  42. })
  43. }
  44. var P5UI_AddItemToReport_SelectKrsPerson = createReactClass({
  45. // h(SelectKrsPerson, { krs: this.state.selected })
  46. // this.props.krs: {
  47. // "@primaryKey": "161323",
  48. // "A_kod": "80-299",
  49. // "A_kraj": "POLSKA",
  50. // "A_miejscowosc": "GDAŃSK",
  51. // "A_nrDomu": "54C",
  52. // "A_nrLokalu": null,
  53. // "A_poczta": "GDAŃSK",
  54. // "A_ulica": "BARNIEWICKA",
  55. // "ID": "161323",
  56. // "S_kraj": "POLSKA",
  57. // "S_miejscowosc": "GDAŃSK",
  58. // "S_wojewodztwo": "POMORSKIE",
  59. // "krs": "0000223476",
  60. // "nazwa": "BIALL Sp. z o.o.",
  61. // "nip": "6040018535",
  62. // "regon": "193106935",
  63. // "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person": [
  64. // {
  65. // "@primaryKey": "747004",
  66. // "ID": "747004",
  67. // "imiona": "ADRIAN RYSZARD",
  68. // "nazwisko": "WIECZORKOWSKI",
  69. // "pesel": "75040301615"
  70. // },
  71. // {
  72. // "@primaryKey": "747003",
  73. // "ID": "747003",
  74. // "imiona": "EWA",
  75. // "nazwisko": "WIECZORKOWSKA",
  76. // "pesel": "75090401549"
  77. // },
  78. render: function () {
  79. var krsPresons = (this.props.krs && this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'])
  80. ? this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person']
  81. : [];
  82. var _selectPerson = this.props.selectPerson.bind(this)
  83. return (krsPresons.length > 0)
  84. ? h('ol', { style: { margin: '22px', textAlign: 'left' } }, krsPresons.map(function (person) {
  85. return h('li', {}, [
  86. (person.imiona || '') + ' ' + (person.nazwisko || '') + ', ' + (person.pesel || ''),
  87. h('button', {
  88. className: "btn btn-primary",
  89. style: { marginLeft: "8px", padding: "4px 8px", fontSize: "small" },
  90. onClick: function () { _selectPerson(person) }
  91. }, "Dodaj osobę")
  92. ]);
  93. }))
  94. : null;
  95. }
  96. });
  97. var P5UI_AddItemToReport_BazaMenuItem = createReactClass({
  98. // this.props: { key: option.ID, baza: selectedBaza, data: option }
  99. render: function () {
  100. return h('div', {}, this.props.data.label);
  101. }
  102. });
  103. var P5UI_AddItemToReport = createReactClass({
  104. getInitialState: function () {
  105. return {
  106. baza: null,
  107. newItemBaza: null,
  108. isSearching: false,
  109. selected: null,
  110. };
  111. },
  112. _handleSelectBaza: function (baza) {
  113. this.setState({ baza: baza, newItemBaza: null, selected: null, isSearching: false });
  114. },
  115. _handleSelectNewItem: function (baza) {
  116. this.setState({ baza: null, newItemBaza: baza, selected: null, isSearching: false });
  117. },
  118. _handleSelected: function (selected) {
  119. this.setState({ selected: (selected.length > 0) ? selected[0] : null });
  120. },
  121. _handleSearch: function (query) {
  122. this.setState({ isSearching: true });
  123. var _setState = this.setState.bind(this);
  124. var baza = this.state.baza;
  125. var options= [];
  126. window.fetch(URL_FETCH_FROM_BAZA, {
  127. method: 'POST',
  128. header: {
  129. 'contentType': 'applications/json'
  130. },
  131. credentials: 'same-origin',
  132. body: JSON.stringify({
  133. baza: baza,
  134. query: query,
  135. })
  136. })
  137. .then(function(response) {
  138. return response.text();
  139. })
  140. .then(function(responseText) {
  141. try {
  142. return JSON.parse(responseText);
  143. } catch (e) {
  144. throw responseText;
  145. }
  146. })
  147. .then(function(result) {
  148. if (result.type == 'success') {
  149. // p5UI__notifyAjaxCallback(result);
  150. if (result.body && result.body.items && result.body.items.length > 0) {
  151. return result.body.items;
  152. } else {
  153. p5UI__notifyAjaxCallback({ type: 'warning', msg: "Brak danych pasujących do kryteriów wyszukiwania" });
  154. _setState({ isSearching: false, options: [] });
  155. }
  156. }
  157. })
  158. .then(function(items) {
  159. DBG && console.log('items fetched:', items);
  160. _setState({ isSearching: false, options: convertXlinkToObjects(items) });
  161. })
  162. .catch(function(error) {
  163. DBG && console.log('request failed', error);
  164. })
  165. },
  166. componentWillUnmount: function () {
  167. DBG && console.log("DBG: conponentDidUnmount...");
  168. },
  169. render: function () {
  170. var bazaBtns = [];
  171. bazaBtns.push({ baza: "default_db/BI_audit_KRS/BI_audit_KRS", title: "KRS - Firmy", label: "KRS" });
  172. bazaBtns.push({ baza: "default_db/BI_audit_KRS_person/BI_audit_KRS_person", title: "KRS - Osoby", label: "KRS/p" });
  173. bazaBtns.push({ baza: "default_db/BI_audit_MSIG/BI_audit_MSIG", title: "MSIG - Firmy", label: "MSIG" });
  174. bazaBtns.push({ baza: "default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", title: "MSIG - Osoby", label: "MSIG/p" });
  175. bazaBtns.push({ baza: "default_db/BI_audit_taxpayer/BI_audit_taxpayer", title: "VAT - Aktywni płatnicy", label: "VATp" });
  176. bazaBtns.push({ baza: "default_db/BI_audit_CEIDG/BI_audit_CEIDG", title: "CEIDG", label: "CEIDG" });
  177. var newItemBtns = [];
  178. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY", title: "Nowa osoba", label: "Nowa osoba" });
  179. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "Nowy podmiot", label: "Nowy podmiot" });
  180. function generateBazaBtn(btn, opts) {
  181. return h('button', {
  182. title: btn.title,
  183. className: "btn btn-success" + (opts.selected ? " active" : ""),
  184. style: { margin: "3px" },
  185. onClick: function () {
  186. opts.onClick(btn.baza)
  187. },
  188. }, btn.label)
  189. }
  190. function generateNewItemBtn(btn, opts) {
  191. return h('button', {
  192. title: btn.title,
  193. className: "btn btn-success" + (opts.selected ? " active" : ""),
  194. style: { margin: "3px" },
  195. onClick: function () {
  196. opts.onClick(btn.baza)
  197. },
  198. }, btn.label)
  199. }
  200. var handleSelectBaza = this._handleSelectBaza.bind(this);
  201. var handleSelectNewItem = this._handleSelectNewItem.bind(this);
  202. var selectedBaza = this.state.baza;
  203. var selectedNewItemBaza = this.state.newItemBaza;
  204. var selectedItem = this.state.selected;
  205. var selectedLabel = '';
  206. if (selectedBaza) {
  207. var selBazaBtn = bazaBtns.filter(function (btn) { return btn.baza === selectedBaza; })
  208. if (selBazaBtn) {
  209. selBazaBtn[0].title
  210. }
  211. }
  212. var handleSearch = this._handleSearch.bind(this);
  213. var handleSelected = this._handleSelected.bind(this);
  214. var _onSelect = this.props.onSelect;
  215. DBG && console.log('DBG: render this.state.options', this.state.options);
  216. return h('div', {}, [
  217. h('p', { style: { textAlign: "center" } }, "Wybierz bazę"),
  218. h('div', { style: { marginBottom: '22px' } },
  219. bazaBtns.map(function (btn) {
  220. return generateBazaBtn(btn, {
  221. onClick: handleSelectBaza,
  222. selected: (selectedBaza === btn.baza),
  223. });
  224. }).concat(
  225. newItemBtns.map(function (btn) {
  226. return generateNewItemBtn(btn, {
  227. onClick: handleSelectNewItem,
  228. selected: (selectedNewItemBaza === btn.baza),
  229. });
  230. })
  231. )
  232. ),
  233. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza) && h(AsyncTypeahead, {
  234. isLoading: this.state.isSearching,
  235. allowNew: false,
  236. multiple: false,
  237. options: this.state.options,
  238. labelKey: "label",
  239. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  240. searchText: "Wyszukiwanie...",
  241. // labelKey: function (option) {
  242. // return [
  243. // option.nazwa.replace('"', ''),
  244. // option.nip,
  245. // option.krs,
  246. // option.regon,
  247. // option.S_miejscowosc,
  248. // ].join(' ')
  249. // },
  250. minLength: 3,
  251. onSearch: handleSearch,
  252. placeholder: "Wyszukaj...",
  253. onChange: handleSelected,
  254. autoFocus: true,
  255. // filterBy: function (option, text) {
  256. // console.log('TODO: filterBy...', {option, text});
  257. // return true;
  258. // },
  259. renderMenuItemChildren: function (option, props) {
  260. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  261. }
  262. }),
  263. ('default_db/BI_audit_KRS_person/BI_audit_KRS_person' === selectedBaza) && h(AsyncTypeahead, {
  264. isLoading: this.state.isSearching,
  265. allowNew: false,
  266. multiple: false,
  267. options: this.state.options,
  268. labelKey: "label",
  269. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  270. searchText: "Wyszukiwanie...",
  271. // labelKey: function (option) {
  272. // return [
  273. // option.nazwa.replace('"', ''),
  274. // option.nip,
  275. // option.krs,
  276. // option.regon,
  277. // option.S_miejscowosc,
  278. // ].join(' ')
  279. // },
  280. minLength: 3,
  281. onSearch: handleSearch,
  282. placeholder: "Wyszukaj...",
  283. onChange: handleSelected,
  284. autoFocus: true,
  285. // filterBy: function (option, text) {
  286. // console.log('TODO: filterBy...', {option, text});
  287. // return true;
  288. // },
  289. renderMenuItemChildren: function (option, props) {
  290. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  291. }
  292. }),
  293. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY") && h('div', { className: 'form-group' }, [
  294. h('input',{
  295. className:'form-control required',
  296. id:'personName',
  297. placeholder:'Imię'
  298. }),
  299. h('input',{
  300. className:'form-control required',
  301. id:'personSurname',
  302. placeholder:'Nazwisko'
  303. }),
  304. h('input',{
  305. className:'form-control required',
  306. id:'personPesel',
  307. placeholder:'Pesel'
  308. }),
  309. h('input',{
  310. className:'form-control',
  311. id:'personNip',
  312. placeholder:'NIP'
  313. }),
  314. h('button', { className: "btn btn-primary", onClick: function () {
  315. _onSelect(selectedNewItemBaza, {
  316. personName: document.getElementById('personName').value,
  317. personSurname: document.getElementById('personSurname').value,
  318. personPesel: document.getElementById('personPesel').value,
  319. personNip: document.getElementById('personNip').value,
  320. })
  321. }.bind(this) }, "Dodaj osobę")
  322. ]),
  323. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI") && h('div', { className: 'form-group' }, [
  324. h('input',{
  325. className:'form-control required',
  326. id:'companyName',
  327. placeholder:'Nazwa'
  328. }),
  329. h('input',{
  330. className:'form-control required',
  331. id:'companyPesel',
  332. placeholder:'Pesel'
  333. }),
  334. h('input',{
  335. className:'form-control required',
  336. id:'companyStreet',
  337. placeholder:'Ulica'
  338. }),
  339. h('input',{
  340. className:'form-control required',
  341. id:'companyNumber',
  342. placeholder:'Numer budynku'
  343. }),
  344. h('input',{
  345. className:'form-control required',
  346. id:'companyNumberLocal',
  347. placeholder:'Numer lokalu'
  348. }),
  349. h('input',{
  350. className:'form-control required',
  351. id:'companyPostCode',
  352. placeholder:'Kod pocztowy'
  353. }),
  354. h('input',{
  355. className:'form-control required',
  356. id:'companyCity',
  357. placeholder:'Miejscowosc'
  358. }),
  359. h('input',{
  360. className:'form-control required',
  361. id:'companyNip',
  362. placeholder:'NIP'
  363. }),
  364. h('input',{
  365. className:'form-control',
  366. id:'comapnyRegon',
  367. placeholder:'REGON'
  368. }),
  369. h('input',{
  370. className:'form-control',
  371. id:'companyKrs',
  372. placeholder:'KRS'
  373. }),
  374. h('button', { className: "btn btn-primary", onClick: function () {
  375. _onSelect(selectedNewItemBaza, {
  376. companyName: document.getElementById('companyName').value,
  377. companyNip: document.getElementById('companyNip').value,
  378. companyRegon: document.getElementById('companyRegon').value,
  379. companyPesel: document.getElementById('companyPesel').value,
  380. companyStreet: document.getElementById('companyStreet').value,
  381. companyNumber: document.getElementById('companyNumber').value,
  382. companyNumberLocal: document.getElementById('companyNumberLocal').value,
  383. companyPostCode: document.getElementById('companyPostCode').value,
  384. companyCity: document.getElementById('companyCity').value,
  385. companyKrs: document.getElementById('companyKrs').value,
  386. })
  387. }.bind(this) }, "Dodaj podmiot")
  388. ]),
  389. (selectedBaza && selectedItem) && h('div', { style: { margin: '22px' } }, [
  390. h('button', {
  391. className: "btn btn-primary",
  392. onClick: function () {
  393. _onSelect(selectedBaza, selectedItem);
  394. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  395. // swal.close()
  396. }
  397. }, "Dodaj firmę") // TODO: |> KRS -> firmę, |> KRS Person -> osobę, ....
  398. ]),
  399. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectKrsPerson, {
  400. krs: selectedItem,
  401. selectPerson: function (person) {
  402. _onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
  403. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  404. // swal.close()
  405. }
  406. }),
  407. (DBG && selectedItem) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(selectedItem, null, 2)),
  408. ])
  409. }
  410. });
  411. function getWindowWidth() {
  412. var w = window, d = document,
  413. e = d.documentElement,
  414. g = d.getElementsByTagName('body')[0];
  415. return w.innerWidth || e.clientWidth || g.clientWidth;
  416. }
  417. function addItemToRaport(event) {
  418. // default_db/BI_audit_KRS/BI_audit_KRS
  419. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  420. // default_db/BI_audit_MSIG/BI_audit_MSIG
  421. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  422. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  423. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  424. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  425. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  426. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  427. swal({
  428. title: "Dodaj osobę lub podmiot",
  429. width: (getWindowWidth() * 0.8),
  430. html: '<div id="' + FUNCTION_NAME + '__searchBaza" style="padding:0 24px 24px 24px; min-height:400px"></div>',
  431. animation: false,
  432. showConfirmButton: false,
  433. showCancelButton: false,
  434. showCloseButton: true,
  435. allowEnterKey: false,
  436. onOpen: function () {
  437. ReactDOM.render(
  438. h(P5UI_AddItemToReport, {
  439. onSelect: function (baza, selected) {
  440. DBG && console.log('TODO: selected ', { baza, selected });
  441. global.fetch(URL_SAVE_TO_DB, {
  442. method: 'POST',
  443. credentials: 'same-origin',
  444. headers: { 'Content-Type': 'application/json' },
  445. body: JSON.stringify({
  446. baza: baza,
  447. item: selected
  448. })
  449. }).then(function(response) {
  450. return response.json();
  451. }).then(function(result) {
  452. p5UI__notifyAjaxCallback(result)
  453. })
  454. }
  455. }),
  456. document.getElementById(FUNCTION_NAME + '__searchBaza')
  457. )
  458. }
  459. }).catch(function (err) {
  460. DBG && console.log('err', err)
  461. })
  462. }
  463. // global[FUNCTION_NAME] = addItemToRaport;
  464. module.exports[FUNCTION_NAME] = addItemToRaport;
  465. // module.exports = {
  466. // P5UI_AddItemToReport_BazaMenuItem,
  467. // P5UI_AddItemToReport,
  468. // createPracownikAjax
  469. // }