Bocian.php.addItemToRaport.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 selectedNewItem = 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: (selectedNewItem === 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. (selectedNewItem==="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 required',
  278. id:'personNip',
  279. placeholder:'NIP'
  280. })
  281. ]),
  282. (selectedNewItem==="default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI") && h('div', { className: 'form-group' }, [
  283. h('input',{
  284. className:'form-control required',
  285. id:'companyName',
  286. placeholder:'Nazwa'
  287. }),
  288. h('input',{
  289. className:'form-control required',
  290. id:'comapanyRegon',
  291. placeholder:'REGON'
  292. }),
  293. h('input',{
  294. className:'form-control required',
  295. id:'companyNip',
  296. placeholder:'NIP'
  297. })
  298. ]),
  299. (selectedBaza && selectedItem) && h('div', { style: { margin: '22px' } }, [
  300. h('button', {
  301. className: "btn btn-primary",
  302. onClick: function () {
  303. _onSelect(selectedBaza, selectedItem);
  304. // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
  305. // swal.close()
  306. }
  307. }, "Dodaj firmę") // TODO: |> KRS -> firmę, |> KRS Person -> osobę, ....
  308. ]),
  309. ('default_db/BI_audit_KRS/BI_audit_KRS' === selectedBaza && selectedItem) && h(P5UI_AddItemToReport_SelectKrsPerson, {
  310. krs: selectedItem,
  311. selectPerson: function (person) {
  312. _onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
  313. // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
  314. // swal.close()
  315. }
  316. }),
  317. (DBG && selectedItem) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(selectedItem, null, 2)),
  318. ])
  319. }
  320. });
  321. function getWindowWidth() {
  322. var w = window, d = document,
  323. e = d.documentElement,
  324. g = d.getElementsByTagName('body')[0];
  325. return w.innerWidth || e.clientWidth || g.clientWidth;
  326. }
  327. function createPracownikAjax(event) {
  328. // default_db/BI_audit_KRS/BI_audit_KRS
  329. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  330. // default_db/BI_audit_MSIG/BI_audit_MSIG
  331. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  332. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  333. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  334. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  335. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  336. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  337. swal({
  338. title: "Dodaj osobę lub podmiot",
  339. width: (getWindowWidth() * 0.8),
  340. html: '<div id="createPracownikAjax__searchBaza" style="padding:0 24px 24px 24px; min-height:400px"></div>',
  341. animation: false,
  342. showConfirmButton: false,
  343. showCancelButton: false,
  344. showCloseButton: true,
  345. onOpen: function () {
  346. ReactDOM.render(
  347. h(P5UI_AddItemToReport, {
  348. onSelect: function (baza, selected) {
  349. //insert do tabeli BI_audit_ENERGA_PRACOWNICY
  350. console.log('TODO: selected ', { baza, selected });
  351. }
  352. }),
  353. document.getElementById('createPracownikAjax__searchBaza')
  354. )
  355. }
  356. }).catch(function (err) {
  357. DBG && console.log('err', err)
  358. })
  359. }
  360. global.createPracownikAjax = createPracownikAjax;
  361. // module.exports = {
  362. // P5UI_AddItemToReport_BazaMenuItem,
  363. // P5UI_AddItemToReport,
  364. // createPracownikAjax
  365. // }