Browse Source

added krs person list for found KRS object

Piotr Labudda 7 years ago
parent
commit
2ef0dbb108
1 changed files with 126 additions and 37 deletions
  1. 126 37
      tools/Bocian.php.addItemToRaport.js

+ 126 - 37
tools/Bocian.php.addItemToRaport.js

@@ -6,12 +6,105 @@ const swal = window.swal;
 const DBG = 1;
 
 
+function convertXlinkToObjects(items) {
+  var _childCache = {}; // fieldName => { pk => child }
+  items.forEach(function (item) {
+    var refFields = Object.keys(item).filter(function (fieldName) {
+      return (-1 !== fieldName.indexOf(':') && item[fieldName]);
+    });
+    refFields.forEach(function (fieldName) {
+      item[fieldName].forEach(function (child) {
+        if (1 === Object.keys(child).length && 'xlink' in child) {
+          // xlink
+        } else if (Object.keys(child).length > 1) {
+          if ('@primaryKey' in child) {
+            if (!(fieldName in _childCache)) _childCache[fieldName] = {};
+            _childCache[fieldName][ child['@primaryKey'] ] = child;
+          }
+        }
+      })
+    })
+    return item;
+  });
+  DBG && console.log('DBG convertXlinkToObjects', { _childCache, items });
+
+  return items.map(function (item) {
+    var refFields = Object.keys(item).filter(function (fieldName) {
+      return (-1 !== fieldName.indexOf(':') && item[fieldName]);
+    });
+    refFields.forEach(function (fieldName) {
+      item[fieldName] = item[fieldName].map(function (child) {
+        if (1 === Object.keys(child).length && 'xlink' in child) {
+          var pk = child['xlink'].split('.').pop();
+          return _childCache[fieldName][pk];
+        }
+        return child;
+      })
+    })
+    return item;
+  })
+}
+
+var P5UI_AddItemToReport_SelectKrsPerson = createReactClass({
+  // h(SelectKrsPerson, { krs: this.state.selected })
+  // this.props.krs: {
+  //   "@primaryKey": "161323",
+  //   "A_kod": "80-299",
+  //   "A_kraj": "POLSKA",
+  //   "A_miejscowosc": "GDAŃSK",
+  //   "A_nrDomu": "54C",
+  //   "A_nrLokalu": null,
+  //   "A_poczta": "GDAŃSK",
+  //   "A_ulica": "BARNIEWICKA",
+  //   "ID": "161323",
+  //   "S_kraj": "POLSKA",
+  //   "S_miejscowosc": "GDAŃSK",
+  //   "S_wojewodztwo": "POMORSKIE",
+  //   "krs": "0000223476",
+  //   "nazwa": "BIALL Sp. z o.o.",
+  //   "nip": "6040018535",
+  //   "regon": "193106935",
+  //   "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person": [
+  //     {
+  //       "@primaryKey": "747004",
+  //       "ID": "747004",
+  //       "imiona": "ADRIAN RYSZARD",
+  //       "nazwisko": "WIECZORKOWSKI",
+  //       "pesel": "75040301615"
+  //     },
+  //     {
+  //       "@primaryKey": "747003",
+  //       "ID": "747003",
+  //       "imiona": "EWA",
+  //       "nazwisko": "WIECZORKOWSKA",
+  //       "pesel": "75090401549"
+  //     },
+  render: function () {
+    var krsPresons = (this.props.krs && this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person'])
+    ? this.props.krs['default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person']
+    : [];
+
+    return (krsPresons.length > 0)
+    ? h('ol', { style: { margin: '22px', textAlign: 'left' } }, krsPresons.map(function (person) {
+      return h('li', {}, [
+        (person.imiona || '') + ' ' + (person.nazwisko || '') + ', ' + (person.pesel || ''),
+        h('button', {
+          className: "btn btn-primary",
+          style: { marginLeft: "8px", padding: "4px 8px", fontSize: "small" },
+          onClick: function () { this.props.selectPerson(person) }.bind(this)
+        }, "Dodaj osobę")
+      ]);
+    }))
+    : null;
+  }
+});
+
 var P5UI_AddItemToReport_BazaMenuItem = createReactClass({
     // this.props: { key: option.ID, baza: selectedBaza, data: option }
     render: function () {
         return h('div', {}, this.props.data.label);
     }
-})
+});
 
 var P5UI_AddItemToReport = createReactClass({
     getInitialState: function () {
@@ -59,7 +152,7 @@ var P5UI_AddItemToReport = createReactClass({
 				})
 				.then(function(result) {
 					if (result.type == 'success') {
-						p5UI__notifyAjaxCallback(result);
+						// p5UI__notifyAjaxCallback(result);
             if (result.body && result.body.items && result.body.items.length > 0) {
               return result.body.items;
             } else {
@@ -69,7 +162,7 @@ var P5UI_AddItemToReport = createReactClass({
 				})
         .then(function(items) {
           DBG && console.log('items fetched:', items);
-          _setState({ isSearching: false, options: items });
+          _setState({ isSearching: false, options: convertXlinkToObjects(items) });
         })
         .catch(function(error) {
           DBG && console.log('request failed', error);
@@ -78,17 +171,7 @@ var P5UI_AddItemToReport = createReactClass({
     componentWillUnmount: function () {
         DBG && console.log("DBG: conponentDidUnmount...");
     },
-    shouldComponentUpdate: function (nextProps, nextState) {
-      DBG && console.log("DBG: shouldComponentUpdate...", { nextProps, nextState });
-      return true;
-    },
     render: function () {
-        // '<div>' +
-        //   '<p style="text-align:center">' + 'Wybierz bazę' + '</p>' +
-        //   '<div>' + .join(' ') + '</div>' +
-        //   +
-        //   '</div>',
-
         var bazaBtns = [];
         bazaBtns.push({ baza: "default_db/BI_audit_KRS/BI_audit_KRS", title: "KRS - Firmy", label: "KRS" });
         bazaBtns.push({ baza: "default_db/BI_audit_KRS_person/BI_audit_KRS_person", title: "KRS - Osoby", label: "KRS/p" });
@@ -96,9 +179,7 @@ var P5UI_AddItemToReport = createReactClass({
         bazaBtns.push({ baza: "default_db/BI_audit_MSIG_person/BI_audit_MSIG_person", title: "KRS - Osoby", label: "MSIG/p" });
         bazaBtns.push({ baza: "default_db/BI_audit_taxpayer/BI_audit_taxpayer", title: "VAT - Aktywni płatnicy", label: "VATp" });
         bazaBtns.push({ baza: "default_db/BI_audit_CEIDG/BI_audit_CEIDG", title: "CEIDG", label: "CEIDG" });
-        // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY", title: "RUM Umowy", label: "RUM/u" });
-        // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "RUM Kontrahenci", label: "RUM/k" });
-        // bazaBtns.push({ baza: "default_db/BI_audit_ENERGA_FAKTURY/BI_audit_ENERGA_FAKTURY", title: "Faktury - do imoportowania", label: "Faktury" });
+
         var newItemBtns = [];
         newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY", title: "Nowa osoba", label: "Nowa osoba" });
         newItemBtns.push({ baza: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI/BI_audit_ENERGA_RUM_KONTRAHENCI", title: "Nowy podmiot", label: "Nowy podmiot" });
@@ -161,25 +242,25 @@ var P5UI_AddItemToReport = createReactClass({
                 allowNew: false,
                 multiple: false,
                 options: this.state.options,
-                // labelKey: "searchLabel",
-                labelKey: function (option) {
-                  return [
-                    option.nazwa.replace('"', ''),
-                    option.nip,
-                    option.krs,
-                    option.regon,
-                    option.S_miejscowosc,
-                  ].join(' ')
-                },
+                labelKey: "label",
+                // labelKey: function (option) {
+                //   return [
+                //     option.nazwa.replace('"', ''),
+                //     option.nip,
+                //     option.krs,
+                //     option.regon,
+                //     option.S_miejscowosc,
+                //   ].join(' ')
+                // },
                 minLength: 3,
                 onSearch: handleSearch,
                 placeholder: "Wyszukaj...",
                 onChange: handleSelected,
                 autoFocus: true,
-                filterBy: function (option, text) {
-                  console.log('TODO: filterBy...', {option, text});
-                  return true;
-                },
+                // filterBy: function (option, text) {
+                //   console.log('TODO: filterBy...', {option, text});
+                //   return true;
+                // },
                 renderMenuItemChildren: function (option, props) {
                     return h(P5UI_AddItemToReport_BazaMenuItem, { key: option.ID, baza: selectedBaza, data: option });
                 }
@@ -211,20 +292,28 @@ var P5UI_AddItemToReport = createReactClass({
                 placeholder:'REGON'
               })
             ]),
-            // '' === selectedNewItem && h
-            (selectedBaza && this.state.selected) && h('div', { style: { marginTop: '22px' } }, [
+            (selectedBaza && this.state.selected) && h('div', { style: { margin: '22px' } }, [
                 h('button', {
                     className: "btn btn-success active",
                     onClick: function () {
                         this.props.onSelect(this.state.baza, this.state.selected);
-                        ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
-                        swal.close()
+                        // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
+                        // swal.close()
                     }
-                }, "Dodaj")
-            ])
+                }, "Dodaj firmę")
+            ]),
+            ('default_db/BI_audit_KRS/BI_audit_KRS' === this.state.baza && this.state.selected) && h(P5UI_AddItemToReport_SelectKrsPerson, {
+              krs: this.state.selected,
+              selectPerson: function (person) {
+                this.props.onSelect("default_db/BI_audit_KRS_person/BI_audit_KRS_person", person);
+                // ReactDOM.unmountComponentAtNode(document.getElementById('createPracownikAjax__searchBaza'));
+                // swal.close()
+              }
+            }),
+            (DBG && this.state.selected) && h('pre', { style: { textAlign: 'left' } }, JSON.stringify(this.state.selected, null, 2)),
         ])
     }
-})
+});
 
 function getWindowWidth() {
     var w = window, d = document,