Bocian.php.addItemToRaport.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 STORE_NAME = STORE_NAME || '_bocianRaportStore';
  7. var DBG = DBG || false;
  8. var DBG1 = true;
  9. function bocianFeatureKey(ns, pk) {
  10. switch (ns) {
  11. case 'default_db/BI_audit_KRS/BI_audit_KRS': return 'KRS.' + pk;
  12. case 'default_db/BI_audit_KRS_person/BI_audit_KRS_person': return 'KRS/p.' + pk;
  13. case 'default_db/BI_audit_MSIG/BI_audit_MSIG': return 'MSIG.' + pk;
  14. case 'default_db/BI_audit_MSIG_person/BI_audit_MSIG_person': return 'MSIG/p.' + pk;
  15. case 'default_db/BI_audit_CEIDG/BI_audit_CEIDG': return 'CEIDG.' + pk;
  16. default: throw "Not implemented feature key for ns '" + ns + "'";
  17. }
  18. }
  19. function getSelectedListLocalStorageKey(from) { // from: ( OD | DO )
  20. switch (from) {
  21. case 'OD': return 'Bocian.biAuditForm.odFeatureKeys';
  22. case 'DO': return 'Bocian.biAuditForm.doFeatureKeys';
  23. default: throw "Not implemented from selected list '" + from + "' - expected 'OD' or 'DO'";
  24. }
  25. }
  26. function getSelectedListFromLocalStorage(from) {
  27. return JSON.parse( localStorage.getItem(getSelectedListLocalStorageKey(from)) || '[]' );
  28. }
  29. function saveToSelectedListInLocalStorage(from, featureKey) {
  30. DBG && console.log("DBG::saveToSelectedListInLocalStorage(from, featureKey)", {from, featureKey}, {added: getSelectedListFromLocalStorage(from).concat(featureKey)});
  31. localStorage.setItem(getSelectedListLocalStorageKey(from), JSON.stringify(
  32. getSelectedListFromLocalStorage(from).concat(featureKey)
  33. ))
  34. }
  35. function removeFromSelectedListInLocalStorage(from, featureKey) {
  36. DBG && console.log("DBG::removeFromSelectedListInLocalStorage(from, featureKey)", {from, featureKey});
  37. localStorage.setItem(getSelectedListLocalStorageKey(from), JSON.stringify(
  38. getSelectedListFromLocalStorage(from).filter(function (item) {
  39. return ( item !== featureKey );
  40. })
  41. ))
  42. }
  43. function convertXlinkToObjects(items) {
  44. var _childCache = {}; // fieldName => { pk => child }
  45. items.forEach(function (item) {
  46. var refFields = Object.keys(item).filter(function (fieldName) {
  47. return (-1 !== fieldName.indexOf(':') && item[fieldName]);
  48. });
  49. refFields.forEach(function (fieldName) {
  50. item[fieldName].forEach(function (child) {
  51. if (1 === Object.keys(child).length && 'xlink' in child) {
  52. // xlink
  53. } else if (Object.keys(child).length > 1) {
  54. if ('@primaryKey' in child) {
  55. if (!(fieldName in _childCache)) _childCache[fieldName] = {};
  56. _childCache[fieldName][ child['@primaryKey'] ] = child;
  57. }
  58. }
  59. })
  60. })
  61. return item;
  62. });
  63. DBG && console.log('DBG convertXlinkToObjects', { _childCache, items });
  64. return items.map(function (item) {
  65. var refFields = Object.keys(item).filter(function (fieldName) {
  66. return (-1 !== fieldName.indexOf(':') && item[fieldName]);
  67. });
  68. refFields.forEach(function (fieldName) {
  69. item[fieldName] = item[fieldName].map(function (child) {
  70. if (1 === Object.keys(child).length && 'xlink' in child) {
  71. var pk = child['xlink'].split('.').pop();
  72. return _childCache[fieldName][pk];
  73. } else if ('p5:links' in child && 'p5:next' in child['p5:links']) {
  74. return Object.assign({}, child['p5:links']['p5:next'], {
  75. type: 'p5:next'
  76. });
  77. // child.value = "https://.../SE/wfs-data.php/?SERVICE=WFS&VERSION=1.0.0&TYPENAME=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&REQUEST=GetFeature&backRefNS=default_db/BI_audit_KRS/BI_audit_KRS&backRefPK=1332604&backRefField=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&maxFeatures=10&startIndex=10"
  78. }
  79. return child;
  80. })
  81. })
  82. return item;
  83. })
  84. }
  85. function filterByHelper(option, text) {
  86. var label = option.label.toLowerCase();
  87. var words = ( (text.indexOf(' ') > 0)
  88. ? text.split(' ')
  89. : [ text ]
  90. ).map(function (word) { return word.trim().replace(/^[0]+/g, '').toLowerCase(); })
  91. .filter(function (word) { return word.length > 2; });
  92. var foundWords = words.filter(function (word) {
  93. return (label.indexOf(word) > -1);
  94. })
  95. return (foundWords.length === words.length);
  96. }
  97. // function (option, text) {
  98. // if (option.ID == 161323) return false;
  99. // console.log('TODO: filterBy...', {option, text});
  100. // return true;
  101. // },
  102. var P5UI_AddItemToReport_SelectKrsPerson = createReactClass({
  103. // h(SelectKrsPerson, { krs: this.state.selected })
  104. // this.props.krs: {
  105. // "@primaryKey": "161323",
  106. // "A_kod": "80-299",
  107. // "A_kraj": "POLSKA",
  108. // "A_miejscowosc": "GDAŃSK",
  109. // "A_nrDomu": "54C",
  110. // "A_nrLokalu": null,
  111. // "A_poczta": "GDAŃSK",
  112. // "A_ulica": "BARNIEWICKA",
  113. // "ID": "161323",
  114. // "S_kraj": "POLSKA",
  115. // "S_miejscowosc": "GDAŃSK",
  116. // "S_wojewodztwo": "POMORSKIE",
  117. // "krs": "0000223476",
  118. // "nazwa": "BIALL Sp. z o.o.",
  119. // "nip": "6040018535",
  120. // "regon": "193106935",
  121. // "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person": [
  122. // {
  123. // "@primaryKey": "747004",
  124. // "ID": "747004",
  125. // "imiona": "ADRIAN RYSZARD",
  126. // "nazwisko": "WIECZORKOWSKI",
  127. // "pesel": "75040301615"
  128. // },
  129. // {
  130. // "@primaryKey": "747003",
  131. // "ID": "747003",
  132. // "imiona": "EWA",
  133. // "nazwisko": "WIECZORKOWSKA",
  134. // "pesel": "75090401549"
  135. // },
  136. // ...
  137. // {
  138. // "@backRefNS: "default_db/BI_audit_KRS/BI_audit_KRS"
  139. // @backRefPK: "1332604"
  140. // @maxFeatures: 10
  141. // @startIndex: 10
  142. // @typeName: "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person"
  143. // type: "p5:next"
  144. // value: "https://biuro.biall-net.pl/dev-pl/se-projects/bocian/SE/wfs-data.php/?SERVICE=WFS&VERSION=1.0.0&TYPENAME=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&REQUEST=GetFeature&backRefNS=default_db/BI_audit_KRS/BI_audit_KRS&backRefPK=1332604&backRefField=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&maxFeatures=10&startIndex=10"
  145. // }
  146. getKrsPersonsFromProps: function () {
  147. return (this.props.krs && this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'])
  148. ? this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person']
  149. : [];
  150. },
  151. handleFetchMore: function () {
  152. this.props.fetchMoreKrsPerson(this.props.krs);
  153. },
  154. render: function () {
  155. var krsPresons = this.getKrsPersonsFromProps();
  156. // var _selectPerson = this.props.selectPerson.bind(this)
  157. var selectedBaza = 'default_db/BI_audit_KRS_person/BI_audit_KRS_person';
  158. var handleFetchMore = this.handleFetchMore.bind(this);
  159. return (krsPresons.length > 0)
  160. ? h('ol', { style: { margin: '22px', textAlign: 'left' } }, krsPresons.map(function (person) {
  161. var label = (person.imiona || '') + ' ' + (person.nazwisko || '') + ', ' + (person.pesel || '');
  162. if ('type' in person && "p5:next" == person['type']) {
  163. return h('li', {}, [
  164. h('a', { className: "btn btn-link", onClick: handleFetchMore }, "pobierz więcej ...")
  165. ]);
  166. }
  167. return h('li', {}, [
  168. h(P5UI_AddItemToReport_Checkbox, { from: 'OD', selectedBaza: selectedBaza, selectedItem: person }),
  169. h(P5UI_AddItemToReport_Checkbox, { from: 'DO', selectedBaza: selectedBaza, selectedItem: person }),
  170. h('span', { style: { 'margin-left': "6px" } }, label),
  171. // h('button', {
  172. // className: "btn btn-primary",
  173. // style: { marginLeft: "8px", padding: "4px 8px", fontSize: "small" },
  174. // onClick: function () { _selectPerson(person) }
  175. // }, "Dodaj osobę")
  176. ]);
  177. }))
  178. : null;
  179. }
  180. });
  181. var P5UI_AddItemToReport_SelectMSIGPerson = createReactClass({
  182. render: function () { // BI_audit_MSIG_person
  183. var msigPresons = (this.props.msig && this.props.msig['default_db__x3A__BI_audit_MSIG_person:BI_audit_MSIG_person'])
  184. ? this.props.msig['default_db__x3A__BI_audit_MSIG_person:BI_audit_MSIG_person']
  185. : [];
  186. var _selectPerson = this.props.selectPerson.bind(this)
  187. var selectedBaza = 'default_db/BI_audit_MSIG_person/BI_audit_MSIG_person';
  188. return (msigPresons.length > 0)
  189. ? h('ol', { style: { margin: '22px', textAlign: 'left' } }, msigPresons.map(function (person) {
  190. var label = (person.imiona || '') + ' ' + (person.nazwisko || '') + ', ' + (person.pesel || '');
  191. return h('li', {}, [
  192. h(P5UI_AddItemToReport_Checkbox, { from: 'OD', selectedBaza: selectedBaza, selectedItem: person }),
  193. h(P5UI_AddItemToReport_Checkbox, { from: 'DO', selectedBaza: selectedBaza, selectedItem: person }),
  194. h('span', { style: { 'margin-left': "6px" } }, label),
  195. // h('button', {
  196. // className: "btn btn-primary",
  197. // style: { marginLeft: "8px", padding: "4px 8px", fontSize: "small" },
  198. // onClick: function () { _selectPerson(person) }
  199. // }, "Dodaj osobę")
  200. ]);
  201. }))
  202. : null;
  203. }
  204. });
  205. var P5UI_AddItemToReport_BazaMenuItem = createReactClass({
  206. // this.props: { key: option.ID, baza: selectedBaza, data: option }
  207. render: function () {
  208. return h('div', {}, this.props.data.label);
  209. }
  210. });
  211. var P5UI_AddItemToReport_Checkbox = createReactClass({
  212. _getStore: function () {
  213. DBG && console.warn('DBG:: window[STORE_NAME]', { store: window[STORE_NAME], STORE_NAME: STORE_NAME})
  214. return window[STORE_NAME] || null;
  215. },
  216. getStateFromStore: function () {
  217. var featureKey = bocianFeatureKey(this.props.selectedBaza, this.props.selectedItem['@primaryKey']);
  218. var storeState = this._getStore().getState();
  219. return {
  220. featureKey: featureKey,
  221. isSelected: (-1 !== this.getStoreList(this.props.from).indexOf(featureKey)),
  222. // totalOdPracownicy: storeState.totalOdPracownicy,
  223. // totalOdPozostale: storeState.totalOdPozostale,
  224. // totalDoKontrahenci: storeState.totalDoKontrahenci,
  225. // totalDoPozostale: storeState.totalDoPozostale,
  226. };
  227. },
  228. getStoreList: function (from) {
  229. var storeState = this._getStore().getState();
  230. switch (from) {
  231. case 'OD': return storeState.pozostaleOd;
  232. case 'DO': return storeState.pozostaleDo;
  233. default: throw "Not imeplemented store list from = '" + from + "'";
  234. }
  235. },
  236. getInitialState: function () {
  237. return this.getStateFromStore();
  238. },
  239. componentDidMount: function () {
  240. var store = this._getStore()
  241. this._unsubscribe = store ? store.subscribe(this.storeUpdated) : function () {};
  242. },
  243. componentWillUnmount: function () {
  244. this._unsubscribe();
  245. },
  246. storeUpdated: function () {
  247. DBG && console.log('DBG::P5UI__BocianHeader::storeUpdated ', this.getStateFromStore());
  248. this.setState(this.getStateFromStore())
  249. },
  250. _handleClick: function () {
  251. this.state.isSelected ? this.removeItem() : this.saveItem();
  252. // _onSelect(from, selectedBaza, selectedItem);
  253. },
  254. removeItem: function () {
  255. this._getStore().dispatch({ type: 'RM_' + this.props.from, featureKey: this.state.featureKey })
  256. // removeFromSelectedListInLocalStorage(this.props.from, this.state.featureKey);
  257. // this.setState({ isSelected: false });
  258. },
  259. saveItem: function () {
  260. this._getStore().dispatch({ type: 'ADD_' + this.props.from, featureKey: this.state.featureKey })
  261. // saveToSelectedListInLocalStorage(this.props.from, this.state.featureKey);
  262. // this.setState({ isSelected: true });
  263. },
  264. render: function () {
  265. var handleClick = this._handleClick.bind(this);
  266. // var _onSelect = this.props.onSelect;
  267. var isSelected = this.state.isSelected;
  268. return h('div', { className: "checkbox", style: { display: "inline-block", 'margin-left': "6px" } }, [
  269. h('label', { style: { border: "1px solid " + ( isSelected ? "red" : "#ccc" ), 'border-radius': "4px", padding: "2px 6px 0 6px", 'font-size': "12px" } }, [
  270. h('input', {
  271. style: { margin: "0 6px 0 0", position: "relative" },
  272. type: "checkbox",
  273. checked: isSelected,
  274. onClick: handleClick,
  275. }),
  276. h('span', { style: { 'line-height': "20px" } }, this.props.from),
  277. ])
  278. ]);
  279. }
  280. });
  281. var P5UI_AddItemToReport = createReactClass({
  282. getInitialState: function () {
  283. // var listOd = getSelectedListFromLocalStorage('OD');
  284. // var listDo = getSelectedListFromLocalStorage('DO');
  285. return {
  286. baza: null,
  287. newItemBaza: null,
  288. isSearching: false,
  289. selected: null,
  290. };
  291. },
  292. _handleSelectBaza: function (baza) {
  293. this.setState({ baza: baza, newItemBaza: null, selected: null, isSearching: false });
  294. },
  295. _handleSelectNewItem: function (baza) {
  296. this.setState({ baza: null, newItemBaza: baza, selected: null, isSearching: false });
  297. },
  298. _handleSelected: function (selected) {
  299. this.setState({ selected: (selected.length > 0) ? selected[0] : null });
  300. },
  301. _fetchMoreKrsPerson: function (krs) {
  302. var _addMoreKrsPersons = this._addMoreKrsPersons.bind(this);
  303. var pk = krs['@primaryKey']
  304. var krsPersons = (krs && krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'])
  305. ? krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person']
  306. : [];
  307. var lastPerson = krsPersons[ krsPersons.length - 1 ];
  308. if (lastPerson && 'type' in lastPerson && "p5:next" == lastPerson['type']) {
  309. var fetchNextLink = lastPerson['value'];
  310. DBG && console.log("DBG: fetchNextLink", { fetchNextLink });
  311. var cols = [ 'ID', 'imiona', 'nazwisko', 'pesel' ];
  312. p5WFS_GetFeature(lastPerson['@typeName'], {
  313. 'p5:next': fetchNextLink.replace('&maxFeatures=10', '&maxFeatures=100') + '&' + 'propertyName=' + cols.join(',')
  314. }).then(function (results) {
  315. DBG && console.log('DBG: _fetchMoreKrsPerson result', { results });
  316. _addMoreKrsPersons(pk, results.map(function (item) { return Object.assign({}, item, { '@primaryKey': item['ID'] }) }));
  317. })
  318. } else {
  319. DBG && console.log("DBG: missing fetch more link");
  320. return;
  321. }
  322. },
  323. _addMoreKrsPersons: function (pk, moreKrsPersons) {
  324. var newOptions = this.state.options.map(function (item) {
  325. if (item['@primaryKey'] === pk) {
  326. item['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'].pop() // rm last link node - p5:link/p5:next
  327. return Object.assign(item, {
  328. 'default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person': item['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'].concat(moreKrsPersons)
  329. })
  330. }
  331. return item;
  332. });
  333. this.setState({
  334. options: newOptions
  335. })
  336. },
  337. _handleSearch: function (query) {
  338. this.setState({ isSearching: true });
  339. var _setState = this.setState.bind(this);
  340. var baza = this.state.baza;
  341. window.fetch(URL_FETCH_FROM_BAZA, {
  342. method: 'POST',
  343. header: {
  344. 'contentType': 'applications/json'
  345. },
  346. credentials: 'same-origin',
  347. body: JSON.stringify({
  348. baza: baza,
  349. query: query,
  350. })
  351. })
  352. .then(function(response) {
  353. return response.text();
  354. })
  355. .then(function(responseText) {
  356. try {
  357. return JSON.parse(responseText);
  358. } catch (e) {
  359. throw responseText;
  360. }
  361. })
  362. .then(function(result) {
  363. if (result.type == 'success') {
  364. // p5UI__notifyAjaxCallback(result);
  365. if (result.body && result.body.items && result.body.items.length > 0) {
  366. return result.body.items;
  367. } else {
  368. p5UI__notifyAjaxCallback({ type: 'warning', msg: "Brak danych pasujących do kryteriów wyszukiwania" });
  369. _setState({ isSearching: false, options: [] });
  370. }
  371. }
  372. })
  373. .then(function(items) {
  374. DBG && console.log('items fetched:', items);
  375. _setState({ isSearching: false, options: convertXlinkToObjects(items) });
  376. })
  377. .catch(function(error) {
  378. DBG && console.log('request failed', error);
  379. })
  380. },
  381. componentWillUnmount: function () {
  382. DBG && console.log("DBG: conponentDidUnmount...");
  383. },
  384. renderItemSave__OLD(selectedBaza, selectedItem) {
  385. var _onSelect = this.props.onSelect;
  386. return h('div', { style: { margin: '22px' } }, [
  387. h('button', {
  388. className: "btn btn-primary",
  389. onClick: function () {
  390. _onSelect(selectedBaza, selectedItem);
  391. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  392. // swal.close()
  393. }
  394. }, "XXX Dodaj " + (
  395. ( ('default_db/BI_audit_KRS_person/BI_audit_KRS_person' === selectedBaza)
  396. || ('default_db/BI_audit_MSIG_person/BI_audit_MSIG_person' === selectedBaza)
  397. )
  398. ? "osobę"
  399. : "firmę"
  400. )) // TODO: |> KRS -> firmę, |> KRS Person -> osobę, ....
  401. ]);
  402. },
  403. renderItemSave(selectedBaza, selectedItem) {
  404. return h('div', { style: { margin: '22px', 'font-size': "16px" } }, [
  405. "Dodaj jako: ",
  406. h(P5UI_AddItemToReport_Checkbox, { from: 'OD', selectedBaza: selectedBaza, selectedItem: selectedItem }),
  407. h(P5UI_AddItemToReport_Checkbox, { from: 'DO', selectedBaza: selectedBaza, selectedItem: selectedItem }),
  408. ]);
  409. },
  410. render: function () {
  411. var bazaBtns = [];
  412. bazaBtns.push({ baza: "default_db/BI_audit_KRS/BI_audit_KRS", title: "KRS - Firmy", label: "KRS" });
  413. bazaBtns.push({ baza: "default_db/BI_audit_KRS_person/BI_audit_KRS_person", title: "KRS - Osoby", label: "KRS/p" });
  414. bazaBtns.push({ baza: "default_db/BI_audit_MSIG/BI_audit_MSIG", title: "MSIG - Firmy", label: "MSIG" });
  415. bazaBtns.push({ baza: "default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", title: "MSIG - Osoby", label: "MSIG/p" });
  416. //bazaBtns.push({ baza: "default_db/BI_audit_taxpayer/BI_audit_taxpayer", title: "VAT - Aktywni płatnicy", label: "VATp" });
  417. bazaBtns.push({ baza: "default_db/BI_audit_CEIDG/BI_audit_CEIDG", title: "CEIDG", label: "CEIDG" });
  418. var newItemBtns = [];
  419. // newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY", title: "Nowa osoba", label: "Nowa osoba" });
  420. // newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "Nowy podmiot", label: "Nowy podmiot" });
  421. function generateBazaBtn(btn, opts) {
  422. return h('button', {
  423. title: btn.title,
  424. className: "btn btn-success" + (opts.selected ? " active" : ""),
  425. style: { margin: "3px" },
  426. onClick: function () {
  427. opts.onClick(btn.baza)
  428. },
  429. }, btn.label)
  430. }
  431. function generateNewItemBtn(btn, opts) {
  432. return h('button', {
  433. title: btn.title,
  434. className: "btn btn-success" + (opts.selected ? " active" : ""),
  435. style: { margin: "3px" },
  436. onClick: function () {
  437. opts.onClick(btn.baza)
  438. },
  439. }, btn.label)
  440. }
  441. var handleSelectBaza = this._handleSelectBaza.bind(this);
  442. var handleSelectNewItem = this._handleSelectNewItem.bind(this);
  443. var selectedBaza = this.state.baza;
  444. var selectedNewItemBaza = this.state.newItemBaza;
  445. var selectedItem = this.state.selected;
  446. var selectedLabel = '';
  447. if (selectedBaza) {
  448. var selBazaBtn = bazaBtns.filter(function (btn) { return btn.baza === selectedBaza; })
  449. if (selBazaBtn) {
  450. selBazaBtn[0].title
  451. }
  452. }
  453. var handleSearch = this._handleSearch.bind(this);
  454. var handleSelected = this._handleSelected.bind(this);
  455. var _onSelect = this.props.onSelect;
  456. var _fetchMoreKrsPerson = this._fetchMoreKrsPerson.bind(this);
  457. DBG && console.log('DBG: render this.state.options', this.state.options);
  458. return h('div', {}, [
  459. h('p', { style: { textAlign: "center" } }, "Wybierz bazę"),
  460. h('div', { style: { marginBottom: '22px' } },
  461. bazaBtns.map(function (btn) {
  462. return generateBazaBtn(btn, {
  463. onClick: handleSelectBaza,
  464. selected: (selectedBaza === btn.baza),
  465. });
  466. }).concat(
  467. newItemBtns.map(function (btn) {
  468. return generateNewItemBtn(btn, {
  469. onClick: handleSelectNewItem,
  470. selected: (selectedNewItemBaza === btn.baza),
  471. });
  472. })
  473. )
  474. ),
  475. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza) && h(AsyncTypeahead, {
  476. isLoading: this.state.isSearching,
  477. allowNew: false,
  478. multiple: false,
  479. options: this.state.options,
  480. labelKey: "label",
  481. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  482. searchText: "Wyszukiwanie...",
  483. // labelKey: function (option) {
  484. // return [
  485. // option.nazwa.replace('"', ''),
  486. // option.nip,
  487. // option.krs,
  488. // option.regon,
  489. // option.S_miejscowosc,
  490. // ].join(' ')
  491. // },
  492. minLength: 3,
  493. onSearch: handleSearch,
  494. placeholder: "Wyszukaj...",
  495. onChange: handleSelected,
  496. autoFocus: true,
  497. filterBy: filterByHelper,
  498. renderMenuItemChildren: function (option, props) {
  499. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  500. }
  501. }),
  502. ('default_db/BI_audit_KRS_person/BI_audit_KRS_person' === selectedBaza) && h(AsyncTypeahead, {
  503. isLoading: this.state.isSearching,
  504. allowNew: false,
  505. multiple: false,
  506. options: this.state.options,
  507. labelKey: "label",
  508. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  509. searchText: "Wyszukiwanie...",
  510. // labelKey: function (option) {
  511. // return [
  512. // option.nazwa.replace('"', ''),
  513. // option.nip,
  514. // option.krs,
  515. // option.regon,
  516. // option.S_miejscowosc,
  517. // ].join(' ')
  518. // },
  519. minLength: 3,
  520. onSearch: handleSearch,
  521. placeholder: "Wyszukaj...",
  522. onChange: handleSelected,
  523. autoFocus: true,
  524. filterBy: filterByHelper,
  525. renderMenuItemChildren: function (option, props) {
  526. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  527. }
  528. }),
  529. ('default_db/BI_audit_MSIG/BI_audit_MSIG' === selectedBaza) && h(AsyncTypeahead, {
  530. isLoading: this.state.isSearching,
  531. allowNew: false,
  532. multiple: false,
  533. options: this.state.options,
  534. labelKey: "label",
  535. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  536. searchText: "Wyszukiwanie...",
  537. // labelKey: function (option) {
  538. // return [
  539. // option.nazwa.replace('"', ''),
  540. // option.nip,
  541. // option.krs,
  542. // option.regon,
  543. // option.S_miejscowosc,
  544. // ].join(' ')
  545. // },
  546. minLength: 3,
  547. onSearch: handleSearch,
  548. placeholder: "Wyszukaj...",
  549. onChange: handleSelected,
  550. autoFocus: true,
  551. filterBy: filterByHelper,
  552. renderMenuItemChildren: function (option, props) {
  553. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  554. }
  555. }),
  556. ('default_db/BI_audit_MSIG_person/BI_audit_MSIG_person' === selectedBaza) && h(AsyncTypeahead, {
  557. isLoading: this.state.isSearching,
  558. allowNew: false,
  559. multiple: false,
  560. options: this.state.options,
  561. labelKey: "label",
  562. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  563. searchText: "Wyszukiwanie...",
  564. // labelKey: function (option) {
  565. // return [
  566. // option.nazwa.replace('"', ''),
  567. // option.nip,
  568. // option.krs,
  569. // option.regon,
  570. // option.S_miejscowosc,
  571. // ].join(' ')
  572. // },
  573. minLength: 3,
  574. onSearch: handleSearch,
  575. placeholder: "Wyszukaj...",
  576. onChange: handleSelected,
  577. autoFocus: true,
  578. filterBy: filterByHelper,
  579. renderMenuItemChildren: function (option, props) {
  580. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  581. }
  582. }),
  583. ('default_db/BI_audit_CEIDG/BI_audit_CEIDG' === selectedBaza) && h(AsyncTypeahead, {
  584. isLoading: this.state.isSearching,
  585. allowNew: false,
  586. multiple: false,
  587. options: this.state.options,
  588. labelKey: "label",
  589. emptyLabel: "Brak danych pasujących do kryteriów wyszukiwania",
  590. searchText: "Wyszukiwanie...",
  591. // labelKey: function (option) {
  592. // return [
  593. // option.nazwa.replace('"', ''),
  594. // option.nip,
  595. // option.krs,
  596. // option.regon,
  597. // option.S_miejscowosc,
  598. // ].join(' ')
  599. // },
  600. minLength: 3,
  601. onSearch: handleSearch,
  602. placeholder: "Wyszukaj...",
  603. onChange: handleSelected,
  604. autoFocus: true,
  605. filterBy: filterByHelper,
  606. renderMenuItemChildren: function (option, props) {
  607. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  608. }
  609. }),
  610. ("default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY" === selectedNewItemBaza) && h('div', { className: 'form-group' }, [
  611. h('input',{
  612. className:'form-control required',
  613. id:'personName',
  614. placeholder:'Imię'
  615. }),
  616. h('input',{
  617. className:'form-control required',
  618. id:'personSurname',
  619. placeholder:'Nazwisko'
  620. }),
  621. h('input',{
  622. className:'form-control required',
  623. id:'personPesel',
  624. placeholder:'Pesel'
  625. }),
  626. h('input',{
  627. className:'form-control',
  628. id:'personNip',
  629. placeholder:'NIP'
  630. }),
  631. h('button', { className: "btn btn-primary", onClick: function () {
  632. _onSelect(selectedNewItemBaza, {
  633. personName: document.getElementById('personName').value,
  634. personSurname: document.getElementById('personSurname').value,
  635. personPesel: document.getElementById('personPesel').value,
  636. personNip: document.getElementById('personNip').value,
  637. })
  638. swal.close();
  639. }.bind(this) }, "Dodaj osobę")
  640. ]),
  641. ("default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI" === selectedNewItemBaza) && h('div', { className: 'form-group' }, [
  642. h('input',{
  643. className:'form-control required',
  644. id:'companyName',
  645. placeholder:'Nazwa'
  646. }),
  647. h('input',{
  648. className:'form-control required',
  649. id:'companyPesel',
  650. placeholder:'Pesel'
  651. }),
  652. h('input',{
  653. className:'form-control required',
  654. id:'companyStreet',
  655. placeholder:'Ulica'
  656. }),
  657. h('input',{
  658. className:'form-control required',
  659. id:'companyNumber',
  660. placeholder:'Numer budynku'
  661. }),
  662. h('input',{
  663. className:'form-control required',
  664. id:'companyNumberLocal',
  665. placeholder:'Numer lokalu'
  666. }),
  667. h('input',{
  668. className:'form-control required',
  669. id:'companyPostCode',
  670. placeholder:'Kod pocztowy'
  671. }),
  672. h('input',{
  673. className:'form-control required',
  674. id:'companyCity',
  675. placeholder:'Miejscowosc'
  676. }),
  677. h('input',{
  678. className:'form-control required',
  679. id:'companyNip',
  680. placeholder:'NIP'
  681. }),
  682. h('input',{
  683. className:'form-control',
  684. id:'companyRegon',
  685. placeholder:'REGON'
  686. }),
  687. h('input',{
  688. className:'form-control',
  689. id:'companyKrs',
  690. placeholder:'KRS'
  691. }),
  692. h('button', { className: "btn btn-primary", onClick: function () {
  693. _onSelect(selectedNewItemBaza, {
  694. companyName: document.getElementById('companyName').value,
  695. companyNip: document.getElementById('companyNip').value,
  696. companyRegon: document.getElementById('companyRegon').value,
  697. companyPesel: document.getElementById('companyPesel').value,
  698. companyStreet: document.getElementById('companyStreet').value,
  699. companyNumber: document.getElementById('companyNumber').value,
  700. companyNumberLocal: document.getElementById('companyNumberLocal').value,
  701. companyPostCode: document.getElementById('companyPostCode').value,
  702. companyCity: document.getElementById('companyCity').value,
  703. companyKrs: document.getElementById('companyKrs').value,
  704. })
  705. swal.close();
  706. }.bind(this) }, "Dodaj podmiot")
  707. ]),
  708. (selectedBaza && selectedItem) && this.renderItemSave(selectedBaza, selectedItem),
  709. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectKrsPerson, {
  710. krs: selectedItem,
  711. selectPerson: function (person) {
  712. _onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
  713. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  714. // swal.close()
  715. },
  716. fetchMoreKrsPerson: _fetchMoreKrsPerson
  717. }),
  718. ('default_db/BI_audit_MSIG/BI_audit_MSIG' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectMSIGPerson, {
  719. msig: selectedItem,
  720. selectPerson: function (person) {
  721. _onSelect("default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", person);
  722. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  723. // swal.close()
  724. }
  725. }),
  726. (DBG && selectedItem) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(selectedItem, null, 2)),
  727. ])
  728. }
  729. });
  730. function getWindowWidth() {
  731. var w = window, d = document,
  732. e = d.documentElement,
  733. g = d.getElementsByTagName('body')[0];
  734. return w.innerWidth || e.clientWidth || g.clientWidth;
  735. }
  736. function addItemToRaport(event) {
  737. // default_db/BI_audit_KRS/BI_audit_KRS
  738. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  739. // default_db/BI_audit_MSIG/BI_audit_MSIG
  740. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  741. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  742. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  743. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  744. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  745. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  746. var valueUrl = global.location.hash;
  747. swal({
  748. title: "Dodaj osobę lub podmiot",
  749. width: (getWindowWidth() * 0.8),
  750. html: '<div id="' + FUNCTION_NAME + '__searchBaza" style="padding:0 24px 24px 24px; min-height:400px"></div>',
  751. animation: false,
  752. showConfirmButton: false,
  753. showCancelButton: false,
  754. showCloseButton: true,
  755. allowEnterKey: false,
  756. onOpen: function () {
  757. ReactDOM.render(
  758. h(P5UI_AddItemToReport, {
  759. onSelect: function (from, baza, selected) {
  760. DBG && console.log('TODO: selected ', { from, baza, selected });
  761. global.fetch(URL_SAVE_TO_DB, {
  762. method: 'POST',
  763. credentials: 'same-origin',
  764. headers: { 'Content-Type': 'application/json' },
  765. body: JSON.stringify({
  766. from: from,
  767. baza: baza,
  768. item: selected
  769. })
  770. }).then(function(response) {
  771. return response.json();
  772. }).then(function(result) {
  773. if(result.type === "success"){
  774. if(valueUrl === '#PRACOWNICY'){
  775. urlFetchPracownicy(1);
  776. }else{
  777. urlFetchKontrahenci(1);
  778. }
  779. }
  780. p5UI__notifyAjaxCallback(result);
  781. })
  782. }
  783. }),
  784. document.getElementById(FUNCTION_NAME + '__searchBaza')
  785. )
  786. }
  787. }).catch(function (err) {
  788. DBG && console.log('err', err)
  789. })
  790. }
  791. // global[FUNCTION_NAME] = addItemToRaport;
  792. module.exports[FUNCTION_NAME] = addItemToRaport;
  793. // module.exports = {
  794. // P5UI_AddItemToReport_BazaMenuItem,
  795. // P5UI_AddItemToReport,
  796. // createPracownikAjax
  797. // }