Bocian.php.addItemToRaport.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 });
  114. },
  115. _handleSelectNewItem: function (baza) {
  116. this.setState({ baza: null, newItemBaza: baza });
  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: "KRS - 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. 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. // labelKey: function (option) {
  241. // return [
  242. // option.nazwa.replace('"', ''),
  243. // option.nip,
  244. // option.krs,
  245. // option.regon,
  246. // option.S_miejscowosc,
  247. // ].join(' ')
  248. // },
  249. minLength: 3,
  250. onSearch: handleSearch,
  251. placeholder: "Wyszukaj...",
  252. onChange: handleSelected,
  253. autoFocus: true,
  254. // filterBy: function (option, text) {
  255. // console.log('TODO: filterBy...', {option, text});
  256. // return true;
  257. // },
  258. renderMenuItemChildren: function (option, props) {
  259. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  260. }
  261. }),
  262. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY") && h('div', { className: 'form-group' }, [
  263. h('input',{
  264. className:'form-control required',
  265. id:'personName',
  266. placeholder:'Imię'
  267. }),
  268. h('input',{
  269. className:'form-control required',
  270. id:'personSurname',
  271. placeholder:'Nazwisko'
  272. }),
  273. h('input',{
  274. className:'form-control required',
  275. id:'personPesel',
  276. placeholder:'Pesel'
  277. }),
  278. h('input',{
  279. className:'form-control',
  280. id:'personNip',
  281. placeholder:'NIP'
  282. }),
  283. h('button', { className: "btn btn-primary", onClick: function () {
  284. _onSelect(selectedNewItemBaza, {
  285. personName: document.getElementById('personName').value,
  286. personSurname: document.getElementById('personSurname').value,
  287. personPesel: document.getElementById('personPesel').value,
  288. personNip: document.getElementById('personNip').value,
  289. })
  290. }.bind(this) }, "Dodaj osobę")
  291. ]),
  292. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI") && h('div', { className: 'form-group' }, [
  293. h('input',{
  294. className:'form-control required',
  295. id:'companyName',
  296. placeholder:'Nazwa'
  297. }),
  298. h('input',{
  299. className:'form-control required',
  300. id:'companyNip',
  301. placeholder:'NIP'
  302. }),
  303. h('input',{
  304. className:'form-control',
  305. id:'comapanyRegon',
  306. placeholder:'REGON'
  307. }),
  308. h('button', { className: "btn btn-primary", onClick: function () {
  309. _onSelect(selectedNewItemBaza, {
  310. companyName: document.getElementById('companyName').value,
  311. companyNip: document.getElementById('companyNip').value,
  312. comapanyRegon: document.getElementById('comapanyRegon').value,
  313. })
  314. }.bind(this) }, "Dodaj podmiot")
  315. ]),
  316. (selectedBaza && selectedItem) && h('div', { style: { margin: '22px' } }, [
  317. h('button', {
  318. className: "btn btn-primary",
  319. onClick: function () {
  320. _onSelect(selectedBaza, selectedItem);
  321. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  322. // swal.close()
  323. }
  324. }, "Dodaj firmę") // TODO: |> KRS -> firmę, |> KRS Person -> osobę, ....
  325. ]),
  326. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectKrsPerson, {
  327. krs: selectedItem,
  328. selectPerson: function (person) {
  329. _onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
  330. // ReactDOM.unmountComponentAtNode(document.getElementById(FUNCTION_NAME + '__searchBaza'));
  331. // swal.close()
  332. }
  333. }),
  334. (DBG && selectedItem) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(selectedItem, null, 2)),
  335. ])
  336. }
  337. });
  338. function getWindowWidth() {
  339. var w = window, d = document,
  340. e = d.documentElement,
  341. g = d.getElementsByTagName('body')[0];
  342. return w.innerWidth || e.clientWidth || g.clientWidth;
  343. }
  344. function addItemToRaport(event) {
  345. // default_db/BI_audit_KRS/BI_audit_KRS
  346. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  347. // default_db/BI_audit_MSIG/BI_audit_MSIG
  348. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  349. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  350. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  351. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  352. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  353. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  354. swal({
  355. title: "Dodaj osobę lub podmiot",
  356. width: (getWindowWidth() * 0.8),
  357. html: '<div id="' + FUNCTION_NAME + '__searchBaza" style="padding:0 24px 24px 24px; min-height:400px"></div>',
  358. animation: false,
  359. showConfirmButton: false,
  360. showCancelButton: false,
  361. showCloseButton: true,
  362. allowEnterKey: false,
  363. onOpen: function () {
  364. ReactDOM.render(
  365. h(P5UI_AddItemToReport, {
  366. onSelect: function (baza, selected) {
  367. DBG && console.log('TODO: selected ', { baza, selected });
  368. global.fetch(URL_SAVE_TO_DB, {
  369. method: 'POST',
  370. credentials: 'same-origin',
  371. headers: { 'Content-Type': 'application/json' },
  372. body: JSON.stringify({
  373. baza: baza,
  374. item: selected
  375. })
  376. }).then(function(response) {
  377. return response.json();
  378. }).then(function(result) {
  379. p5UI__notifyAjaxCallback(result)
  380. })
  381. }
  382. }),
  383. document.getElementById(FUNCTION_NAME + '__searchBaza')
  384. )
  385. }
  386. }).catch(function (err) {
  387. DBG && console.log('err', err)
  388. })
  389. }
  390. // global[FUNCTION_NAME] = addItemToRaport;
  391. module.exports[FUNCTION_NAME] = addItemToRaport;
  392. // module.exports = {
  393. // P5UI_AddItemToReport_BazaMenuItem,
  394. // P5UI_AddItemToReport,
  395. // createPracownikAjax
  396. // }