Bocian.php.addItemToRaport.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. }
  155. }
  156. })
  157. .then(function(items) {
  158. DBG && console.log('items fetched:', items);
  159. _setState({ isSearching: false, options: convertXlinkToObjects(items) });
  160. })
  161. .catch(function(error) {
  162. DBG && console.log('request failed', error);
  163. })
  164. },
  165. componentWillUnmount: function () {
  166. DBG && console.log("DBG: conponentDidUnmount...");
  167. },
  168. render: function () {
  169. var bazaBtns = [];
  170. bazaBtns.push({ baza: "default_db/BI_audit_KRS/BI_audit_KRS", title: "KRS - Firmy", label: "KRS" });
  171. bazaBtns.push({ baza: "default_db/BI_audit_KRS_person/BI_audit_KRS_person", title: "KRS - Osoby", label: "KRS/p" });
  172. bazaBtns.push({ baza: "default_db/BI_audit_MSIG/BI_audit_MSIG", title: "MSIG - Firmy", label: "MSIG" });
  173. bazaBtns.push({ baza: "default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", title: "KRS - Osoby", label: "MSIG/p" });
  174. bazaBtns.push({ baza: "default_db/BI_audit_taxpayer/BI_audit_taxpayer", title: "VAT - Aktywni płatnicy", label: "VATp" });
  175. bazaBtns.push({ baza: "default_db/BI_audit_CEIDG/BI_audit_CEIDG", title: "CEIDG", label: "CEIDG" });
  176. var newItemBtns = [];
  177. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY", title: "Nowa osoba", label: "Nowa osoba" });
  178. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "Nowy podmiot", label: "Nowy podmiot" });
  179. function generateBazaBtn(btn, opts) {
  180. return h('button', {
  181. title: btn.title,
  182. className: "btn btn-success" + (opts.selected ? " active" : ""),
  183. style: { margin: "3px" },
  184. onClick: function () {
  185. opts.onClick(btn.baza)
  186. },
  187. }, btn.label)
  188. }
  189. function generateNewItemBtn(btn, opts) {
  190. return h('button', {
  191. title: btn.title,
  192. className: "btn btn-success" + (opts.selected ? " active" : ""),
  193. style: { margin: "3px" },
  194. onClick: function () {
  195. opts.onClick(btn.baza)
  196. },
  197. }, btn.label)
  198. }
  199. var handleSelectBaza = this._handleSelectBaza.bind(this);
  200. var handleSelectNewItem = this._handleSelectNewItem.bind(this);
  201. var selectedBaza = this.state.baza;
  202. var selectedNewItemBaza = this.state.newItemBaza;
  203. var selectedItem = this.state.selected;
  204. var selectedLabel = '';
  205. if (selectedBaza) {
  206. var selBazaBtn = bazaBtns.filter(function (btn) { return btn.baza === selectedBaza; })
  207. if (selBazaBtn) {
  208. selBazaBtn[0].title
  209. }
  210. }
  211. var handleSearch = this._handleSearch.bind(this);
  212. var handleSelected = this._handleSelected.bind(this);
  213. var _onSelect = this.props.onSelect;
  214. DBG && console.log('DBG: render this.state.options', this.state.options);
  215. return h('div', {}, [
  216. h('p', { style: { textAlign: "center" } }, "Wybierz bazę"),
  217. h('div', { style: { marginBottom: '22px' } },
  218. bazaBtns.map(function (btn) {
  219. return generateBazaBtn(btn, {
  220. onClick: handleSelectBaza,
  221. selected: (selectedBaza === btn.baza),
  222. });
  223. }).concat(
  224. newItemBtns.map(function (btn) {
  225. return generateNewItemBtn(btn, {
  226. onClick: handleSelectNewItem,
  227. selected: (selectedNewItemBaza === btn.baza),
  228. });
  229. })
  230. )
  231. ),
  232. selectedBaza && h(AsyncTypeahead, {
  233. isLoading: this.state.isSearching,
  234. allowNew: false,
  235. multiple: false,
  236. options: this.state.options,
  237. labelKey: "label",
  238. // labelKey: function (option) {
  239. // return [
  240. // option.nazwa.replace('"', ''),
  241. // option.nip,
  242. // option.krs,
  243. // option.regon,
  244. // option.S_miejscowosc,
  245. // ].join(' ')
  246. // },
  247. minLength: 3,
  248. onSearch: handleSearch,
  249. placeholder: "Wyszukaj...",
  250. onChange: handleSelected,
  251. autoFocus: true,
  252. // filterBy: function (option, text) {
  253. // console.log('TODO: filterBy...', {option, text});
  254. // return true;
  255. // },
  256. renderMenuItemChildren: function (option, props) {
  257. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  258. }
  259. }),
  260. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY") && h('div', { className: 'form-group' }, [
  261. h('input',{
  262. className:'form-control required',
  263. id:'personName',
  264. placeholder:'Imię'
  265. }),
  266. h('input',{
  267. className:'form-control required',
  268. id:'personSurname',
  269. placeholder:'Nazwisko'
  270. }),
  271. h('input',{
  272. className:'form-control required',
  273. id:'personPesel',
  274. placeholder:'Pesel'
  275. }),
  276. h('input',{
  277. className:'form-control',
  278. id:'personNip',
  279. placeholder:'NIP'
  280. }),
  281. h('button', { className: "btn btn-primary", onClick: function () {
  282. _onSelect(selectedNewItemBaza, {
  283. personName: document.getElementById('personName').value,
  284. personSurname: document.getElementById('personSurname').value,
  285. personPesel: document.getElementById('personPesel').value,
  286. personNip: document.getElementById('personNip').value,
  287. })
  288. }.bind(this) }, "Dodaj osobę")
  289. ]),
  290. (selectedNewItemBaza === "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI") && h('div', { className: 'form-group' }, [
  291. h('input',{
  292. className:'form-control required',
  293. id:'companyName',
  294. placeholder:'Nazwa'
  295. }),
  296. h('input',{
  297. className:'form-control required',
  298. id:'companyNip',
  299. placeholder:'NIP'
  300. }),
  301. h('input',{
  302. className:'form-control',
  303. id:'comapanyRegon',
  304. placeholder:'REGON'
  305. }),
  306. h('button', { className: "btn btn-primary", onClick: function () {
  307. _onSelect(selectedNewItemBaza, {
  308. companyName: document.getElementById('companyName').value,
  309. companyNip: document.getElementById('companyNip').value,
  310. comapanyRegon: document.getElementById('comapanyRegon').value,
  311. })
  312. }.bind(this) }, "Dodaj podmiot")
  313. ]),
  314. (selectedBaza && selectedItem) && h('div', { style: { margin: '22px' } }, [
  315. h('button', {
  316. className: "btn btn-primary",
  317. onClick: function () {
  318. _onSelect(selectedBaza, selectedItem);
  319. // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
  320. // swal.close()
  321. }
  322. }, "Dodaj firmę") // TODO: |> KRS -> firmę, |> KRS Person -> osobę, ....
  323. ]),
  324. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectKrsPerson, {
  325. krs: selectedItem,
  326. selectPerson: function (person) {
  327. _onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
  328. // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
  329. // swal.close()
  330. }
  331. }),
  332. (DBG && selectedItem) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(selectedItem, null, 2)),
  333. ])
  334. }
  335. });
  336. function getWindowWidth() {
  337. var w = window, d = document,
  338. e = d.documentElement,
  339. g = d.getElementsByTagName('body')[0];
  340. return w.innerWidth || e.clientWidth || g.clientWidth;
  341. }
  342. function createPracownikAjax(event) {
  343. // default_db/BI_audit_KRS/BI_audit_KRS
  344. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  345. // default_db/BI_audit_MSIG/BI_audit_MSIG
  346. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  347. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  348. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  349. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  350. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  351. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  352. swal({
  353. title: "Dodaj osobę lub podmiot",
  354. width: (getWindowWidth() * 0.8),
  355. html: '<div id="createPracownikAjax__searchBaza" style="padding:0 24px 24px 24px; min-height:400px"></div>',
  356. animation: false,
  357. showConfirmButton: false,
  358. showCancelButton: false,
  359. showCloseButton: true,
  360. allowEnterKey: false,
  361. onOpen: function () {
  362. ReactDOM.render(
  363. h(P5UI_AddItemToReport, {
  364. onSelect: function (baza, selected) {
  365. //insert do tabeli BI_audit_ENERGA_PRACOWNICY
  366. console.log('TODO: selected ', { baza, selected });
  367. global.fetch(URL_SAVE_TO_PRACOWNICY, {
  368. method: 'POST',
  369. credentials: 'same-origin',
  370. headers: { 'Content-Type': 'application/json' },
  371. body: JSON.stringify({
  372. baza: baza,
  373. item: selected
  374. })
  375. }).then(function(response) {
  376. return response.json();
  377. }).then(function(result) {
  378. p5UI__notifyAjaxCallback(result)
  379. })
  380. }
  381. }),
  382. document.getElementById('createPracownikAjax__searchBaza')
  383. )
  384. }
  385. }).catch(function (err) {
  386. DBG && console.log('err', err)
  387. })
  388. }
  389. global.createPracownikAjax = createPracownikAjax;
  390. // module.exports = {
  391. // P5UI_AddItemToReport_BazaMenuItem,
  392. // P5UI_AddItemToReport,
  393. // createPracownikAjax
  394. // }