Bocian.php.addItemToRaport.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const createReactClass = window.p5VendorJs.createReactClass;
  2. const h = window.p5VendorJs.React.createElement;
  3. const ReactDOM = window.p5VendorJs.ReactDOM;
  4. const AsyncTypeahead = window.p5VendorJs.AsyncTypeahead;
  5. const swal = window.swal;
  6. const DBG = 1;
  7. var P5UI_AddItemToReport_BazaMenuItem = createReactClass({
  8. // this.props: { key: option.ID, baza: selectedBaza, data: option }
  9. render: function () {
  10. return h('div', {}, this.props.data.label);
  11. }
  12. })
  13. var P5UI_AddItemToReport = createReactClass({
  14. getInitialState: function () {
  15. return {
  16. baza: null,
  17. newItemBaza: null,
  18. isSearching: false,
  19. selected: null,
  20. };
  21. },
  22. _handleSelectBaza: function (baza) {
  23. this.setState({ baza: baza, newItemBaza: null });
  24. },
  25. _handleSelectNewItem: function (baza) {
  26. this.setState({ baza: null, newItemBaza: baza });
  27. },
  28. _handleSelected: function (selected) {
  29. this.setState({ selected: (selected.length > 0) ? selected[0] : null });
  30. },
  31. _handleSearch: function (query) {
  32. this.setState({ isSearching: true });
  33. var baza = this.state.baza;
  34. var options= [];
  35. window.fetch(URL_FETCH_FROM_BAZA, {
  36. method: 'POST',
  37. header: {
  38. 'contentType': 'applications/json'
  39. },
  40. credentials: 'same-origin',
  41. body: JSON.stringify({
  42. baza: baza,
  43. query: query,
  44. })
  45. })
  46. .then(function(responseText) {
  47. try {
  48. return JSON.parse(responseText);
  49. } catch (e) {
  50. throw responseText;
  51. }
  52. })
  53. .then(function(result) {
  54. if (result.type == 'success') {
  55. p5UI__notifyAjaxCallback(result);
  56. resolve(result.body);
  57. }
  58. })
  59. .then(function(items) {
  60. options = items;
  61. this.setState({ isSearching: false, options: options });
  62. })
  63. .catch(function(error) {
  64. if(DBG) console.log('request failed', error);
  65. })
  66. // setTimeout(function () {
  67. // var options = [];
  68. // options.push({
  69. // ID: 601900,
  70. // nazwa: "ADEGROUP SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ SPÓŁKA KOMANDYTOWA",
  71. // krs: "0000721712", nip: "5862328735", regon: "369609850",
  72. // S_kraj: "POLSKA", S_miejscowosc: "GDYNIA", A_kod: "81-310", A_ulica: "ŚLĄSKA", A_nrDomu: "35/37",
  73. // searchLabel: "ADEGROUP SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ SPÓŁKA KOMANDYTOWA, ŚLĄSKA 35/37, 81-310 GDYNIA, krs: 0000721712, nip: 5862328735, regon: 369609850",
  74. // label: "ADEGROUP SPÓŁKA ..., ŚLĄSKA 35/37, 81-310 GDYNIA"
  75. // })
  76. // options.push({
  77. // ID: 601900,
  78. // nazwa: "BIZNESLIMIT.PL SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ",
  79. // krs: "0000721708", nip: "9452218050", regon: "369608402",
  80. // S_kraj: "POLSKA", S_miejscowosc: "KRAKÓW", A_kod: "31-315", A_ulica: "ELIASZA RADZIKOWSKIEGO", A_nrLokalu: "16", A_nrDomu: "74",
  81. // searchLabel: "BIZNESLIMIT.PL SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ ELIASZA RADZIKOWSKIEGO 74/16, 31-315 KRAKÓW 0000721708 9452218050 369608402",
  82. // label: "BIZNESLIMIT.PL ..., ELIASZA RADZIKOWSKIEGO 74/16, 31-315 KRAKÓW"
  83. // })
  84. // this.setState({ isSearching: false, options: options });
  85. // }.bind(this), 1000);
  86. // makeAndHandleRequest(query)
  87. // .then(({ options }) => {
  88. // this.setState({
  89. // isLoading: false,
  90. // options,
  91. // });
  92. // });
  93. },
  94. componentWillUnmount: function () {
  95. DBG && console.log("DBG: conponentDidUnmount...");
  96. },
  97. render: function () {
  98. // '<div>' +
  99. // '<p style="text-align:center">' + 'Wybierz bazę' + '</p>' +
  100. // '<div>' + .join(' ') + '</div>' +
  101. // +
  102. // '</div>',
  103. var bazaBtns = [];
  104. bazaBtns.push({ baza: "default_db/BI_audit_KRS/BI_audit_KRS", title: "KRS - Firmy", label: "KRS" });
  105. bazaBtns.push({ baza: "default_db/BI_audit_KRS_person/BI_audit_KRS_person", title: "KRS - Osoby", label: "KRS/p" });
  106. bazaBtns.push({ baza: "default_db/BI_audit_MSIG/BI_audit_MSIG", title: "MSIG - Firmy", label: "MSIG" });
  107. bazaBtns.push({ baza: "default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", title: "KRS - Osoby", label: "MSIG/p" });
  108. bazaBtns.push({ baza: "default_db/BI_audit_taxpayer/BI_audit_taxpayer", title: "VAT - Aktywni płatnicy", label: "VATp" });
  109. bazaBtns.push({ baza: "default_db/BI_audit_CEIDG/BI_audit_CEIDG", title: "CEIDG", label: "CEIDG" });
  110. // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY", title: "RUM Umowy", label: "RUM/u" });
  111. // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "RUM Kontrahenci", label: "RUM/k" });
  112. // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY", title: "Faktury - do imoportowania", label: "Faktury" });
  113. var newItemBtns = [];
  114. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY", title: "Nowa osoba", label: "Nowa osoba" });
  115. newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "Nowy podmiot", label: "Nowy podmiot" });
  116. function generateBazaBtn(btn, opts) {
  117. return h('button', {
  118. title: btn.title,
  119. className: "btn btn-success" + (opts.selected ? " active" : ""),
  120. style: { margin: "3px" },
  121. onClick: function () {
  122. opts.onClick(btn.baza)
  123. },
  124. }, btn.label)
  125. }
  126. function generateNewItemBtn(btn, opts) {
  127. return h('button', {
  128. title: btn.title,
  129. className: "btn btn-success" + (opts.selected ? " active" : ""),
  130. style: { margin: "3px" },
  131. onClick: function () {
  132. opts.onClick(btn.baza)
  133. },
  134. }, btn.label)
  135. }
  136. var handleSelectBaza = this._handleSelectBaza.bind(this);
  137. var handleSelectNewItem = this._handleSelectNewItem.bind(this);
  138. var selectedBaza = this.state.baza;
  139. var selectedNewItem = this.state.newItemBaza;
  140. var selectedLabel = '';
  141. if (selectedBaza) {
  142. var selBazaBtn = bazaBtns.filter(function (btn) { return btn.baza === selectedBaza; })
  143. if (selBazaBtn) {
  144. selBazaBtn[0].title
  145. }
  146. }
  147. var handleSearch = this._handleSearch.bind(this);
  148. var handleSelected = this._handleSelected.bind(this);
  149. return h('div', {}, [
  150. h('p', { style: { textAlign: "center" } }, "Wybierz bazę"),
  151. h('div', { style: { marginBottom: '22px' } },
  152. bazaBtns.map(function (btn) {
  153. return generateBazaBtn(btn, {
  154. onClick: handleSelectBaza,
  155. selected: (selectedBaza === btn.baza),
  156. });
  157. }).concat(
  158. newItemBtns.map(function (btn) {
  159. return generateNewItemBtn(btn, {
  160. onClick: handleSelectNewItem,
  161. selected: (selectedNewItem === btn.baza),
  162. });
  163. })
  164. )
  165. ),
  166. selectedBaza && h(AsyncTypeahead, {
  167. isLoading: this.state.isSearching,
  168. allowNew: false,
  169. multiple: false,
  170. options: this.state.options,
  171. labelKey: "searchLabel",
  172. minLength: 2,
  173. onSearch: handleSearch,
  174. placeholder: "Wyszukaj...",
  175. onChange: handleSelected,
  176. autoFocus: true,
  177. // filterBy: function (option, text) {
  178. // console.log('TODO: filterBy...', {option, text});
  179. // return true;
  180. // },
  181. renderMenuItemChildren: function (option, props) {
  182. return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
  183. }
  184. }),
  185. selectedNewItem && h('p', {}, "Dodaj nowy ... " + selectedNewItem),
  186. //TODO dlaczego to nie działa
  187. // selectedNewItem && h('div', {className='form-group'},[
  188. // h('input',{
  189. // className:'form-control required',
  190. // id:'personName',
  191. // placeholder:'Imię'
  192. // }),
  193. // h('input',{
  194. // className:'form-control required',
  195. // id:'personSurname',
  196. // placeholder:'Nazwisko'
  197. // },[]),
  198. // h('input',{
  199. // className:'form-control required',
  200. // id:'personNip',
  201. // placeholder:'NIP'
  202. // },[]),
  203. // h('input',{
  204. // className:'form-control required',
  205. // id:'personPesel',
  206. // placeholder:'personPesel'
  207. // },[]),
  208. // h('input',{
  209. // className:'form-control required',
  210. // id:'personRegon',
  211. // placeholder:'REGON'
  212. // },[])
  213. // ]
  214. // ),
  215. // '' === selectedNewItem && h
  216. (selectedBaza && this.state.selected) && h('div', { style: { marginTop: '22px' } }, [
  217. h('button', {
  218. className: "btn btn-success active",
  219. onClick: function () {
  220. this.props.onSelect(this.state.baza, this.state.selected);
  221. ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
  222. swal.close()
  223. }
  224. }, "Dodaj")
  225. ])
  226. ])
  227. }
  228. })
  229. function getWindowWidth() {
  230. var w = window, d = document,
  231. e = d.documentElement,
  232. g = d.getElementsByTagName('body')[0];
  233. return w.innerWidth || e.clientWidth || g.clientWidth;
  234. }
  235. function createPracownikAjax(event) {
  236. // default_db/BI_audit_KRS/BI_audit_KRS
  237. // default_db/BI_audit_KRS_person/BI_audit_KRS_person
  238. // default_db/BI_audit_MSIG/BI_audit_MSIG
  239. // default_db/BI_audit_MSIG_person/BI_audit_MSIG_person
  240. // default_db/BI_audit_taxpayer/BI_audit_taxpayer
  241. // default_db/BI_audit_CEIDG/BI_audit_CEIDG
  242. // default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY
  243. // default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI
  244. // default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY
  245. swal({
  246. title: "Dodaj osobę lub podmiot",
  247. width: (getWindowWidth() * 0.8),
  248. html: '<div id="createPracownikAjax__searchBaza" style="padding:0 24px 24px 24px"></div>',
  249. animation: false,
  250. showConfirmButton: false,
  251. showCancelButton: false,
  252. showCloseButton: true,
  253. onOpen: function () {
  254. ReactDOM.render(
  255. h(P5UI_AddItemToReport, {
  256. onSelect: function (baza, selected) {
  257. //insert do tabeli BI_audit_ENERGA_PRACOWNICY
  258. console.log('TODO: selected ', { baza, selected });
  259. }
  260. }),
  261. document.getElementById('createPracownikAjax__searchBaza')
  262. )
  263. }
  264. }).catch(function (err) {
  265. DBG && console.log('err', err)
  266. })
  267. }
  268. global.createPracownikAjax = createPracownikAjax;
  269. // module.exports = {
  270. // P5UI_AddItemToReport_BazaMenuItem,
  271. // P5UI_AddItemToReport,
  272. // createPracownikAjax
  273. // }