Przeglądaj źródła

Import osób z KRS do pracowników

Mariusz Muszyński 8 lat temu
rodzic
commit
18e34dc316
1 zmienionych plików z 218 dodań i 19 usunięć
  1. 218 19
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 218 - 19
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -446,7 +446,6 @@ where `REMOTE_PRIMARY_KEY` = '{$this->SOURCE['ID']}'";
     <div class="form-group">
       <div class="col-sm-offset-1 col-sm-11">
         <button type="submit" class="btn btn-primary" name="action" value="search">Szukaj</button>
-        <a href="<?=$this->REFERER?>" class="btn btn-default">Powrót</a>
       </div>
     </div>
     <input type="hidden" name="REFERER" value="<?=$this->REFERER?>"/>
@@ -456,34 +455,234 @@ where `REMOTE_PRIMARY_KEY` = '{$this->SOURCE['ID']}'";
 	}
 
 	private function importKrsToPracownicySearch() {
-		$items = [
-			"nazwa" => 'like',
-			"krs" => '=',
-			"nip" => '=',
-			"regon" => '=',
-		];
-		$form = [];
-		foreach ($items as $item => $type) {
-			if ($param = V::get($item, '', $_POST)) {
-				if ($type == 'like') $param = "%{$param}%";
-				$form[$item] = DB::getPDO()->quote($param);
+		try {
+			$subaction = V::get('subaction', '', $_POST);
+			switch ($subaction) {
+				case "listKrsPerson":
+					$krsId = V::get('krsId', 0, $_POST, int);
+					break;
+				case "addKrsPersonToPracownicy":
+					$personId = V::get('personId', [], $_POST);
+					if (!$personId) throw new Exception("Błąd formularza");
+					$query = "insert into BI_audit_ENERGA_PRACOWNICY (source, imiona, nazwisko, pesel) select 'KRS', imiona, nazwisko, pesel from BI_audit_KRS_person where ID in (" . implode(", ", $personId) . ")";
+					DB::getPDO()->query($query);
+					SE_Layout::alert('success', "Pomyślnie zaimportowano " . count($personId) . " pracownik" . ((count($personId) == 1) ? "a" : "ów"));
+					$this->importKrsToPracownicyForm();
+					return;
+				default:
+					$krsId = null;
+			}
+
+			if ($krsId) {
+				$where = ["ID = '{$krsId}'"];
+			} else {
+				$items = [
+					"nazwa" => 'like',
+					"krs" => '=',
+					"nip" => '=',
+					"regon" => '=',
+				];
+				$form = [];
+				foreach ($items as $item => $type) {
+					if ($param = V::get($item, '', $_POST)) {
+						if ($type == 'like') $param = "%{$param}%";
+						$form[$item] = DB::getPDO()->quote($param);
+					}
+				}
+				if (!$form) throw new Exception("Nie wypełniono żadnego pola wyszukiwania!");
+
+				$where = [];
+				foreach ($form as $name => $value) $where[] = "`{$name}` {$items[$name]} {$value}";
 			}
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', $e->getMessage());
+			$this->importKrsToPracownicyForm();
+			return;
 		}
 
-		$where = [];
-		foreach ($form as $name => $value) $where[] = "`{$name}` {$items[$name]} {$value}";
 		$query = "select * from `BI_audit_KRS` where " . implode(" and ", $where) . "order by ID limit 1001";
 		$result = DB::getPDO()->fetchAll($query);
 		if (count($result) == 1001) {
 			SE_Layout::alert('danger', 'Znaleziono zbyt wiele wyników. Doprecyzuj parametry wyszukiwania.');
 			$this->importKrsToPracownicyForm();
 			return;
+		} elseif (count($result) == 1) {
+			$krsId = $result[0]['ID'];
 		}
-echo "test";
-//echo $query . "<br>";
-echo "<pre>";
-//print_r($result);
-echo "</pre>";
+?>
+<div class="container" style="margin-top:20px">
+  <form method="post">
+    <legend>
+      Importowanie osób z KRS do tabeli pracowników
+    </legend>
+    <div class="form-group">
+      <div class="col-sm-12">
+        <h4>Znalezione podmioty:</h4>
+      </div>
+    </div>
+    <div class="form-group">
+      <div class="col-sm-12">
+        <table class="table table-bordered table-hover table-striped" height=5>
+          <thead>
+            <tr style="text-align:center; background-color:lightgray">
+              <td width=1>Lp.</td>
+              <td>Nazwa</td>
+              <td>Adres</td>
+              <td>KRS</td>
+              <td>NIP</td>
+              <td>Regon</td>
+<?php
+		if (count($result) > 1) {
+?>
+              <td width=1>Wybierz</td>
+<?php
+		}
+?>
+            </tr>
+          </thead>
+          <tbody>
+
+<?php
+		if (!$result) echo '<tr><td align="center" colspan="7">Nie znaleziono podmiotu</td></tr>';
+		else {
+			$lp = 1;
+			foreach ($result as $row) {
+				$adres = $row['A_miejscowosc'];
+				if ($row['A_ulica']) $adres .= ", {$row['A_ulica']}";
+				if ($row['A_nrDomu']) {
+					$adres .= " {$row['A_nrDomu']}";
+					if ($row['A_nrLokalu']) $adres .= "/{$row['A_nrLokalu']}";
+				}
+?>
+            <tr>
+              <td align="right"><?=$lp++?></td>
+              <td><?=$row['nazwa']?></td>
+              <td><?=$adres?></td>
+              <td><?=$row['krs']?></td>
+              <td><?=$row['nip']?></td>
+              <td><?=$row['regon']?></td>
+<?php
+		if (count($result) > 1) {
+?>
+              <td align="center">
+               <input type="radio" name="krsId" value="<?=$row['ID']?>"/>
+             </td>
+<?php
+		}
+?>
+            </tr>
+<?php
+			}
+		}
+?>
+          </tbody>
+        </table>
+      </div>
+    </div>
+<?php
+		if (count($result) > 1) {
+?>
+    <div class="form-group">
+      <div class="col-sm-12">
+<?php
+			foreach (array_keys($items) as $item) {
+?>
+        <input type="hidden" name="<?=$item?>" value="<?=V::get($item, '', $_POST)?>"/>
+<?php
+			}
+?>
+        <div class="containter" style="text-align:center">
+          <button type="submit" class="btn btn-primary" name="subaction" value="listKrsPerson">Znajdź osoby powiązane z podmiotem</button>
+          <a href="" class="btn btn-default">Powrót</a>
+        </div>
+      </div>
+    </div>
+<?php
+		} elseif ($krsId) {
+?>
+    <div class="form-group">
+      <div class="col-sm-12">
+        <h4>Znalezione osoby:</h4>
+      </div>
+    </div>
+    <div class="form-group">
+      <div class="col-sm-12">
+        <table class="table table-bordered table-hover table-striped" height=5>
+          <thead>
+            <tr style="text-align:center; background-color:lightgray">
+              <td width=1>Lp.</td>
+              <td>Nazwisko</td>
+              <td>Imiona</td>
+              <td>Pesel</td>
+              <td width=1><input type="checkbox" name="checkAll" onClick="toggleAll(this)"/></td>
+            </tr>
+          </thead>
+          <tbody>
+
+<?php
+			$refKrsToKrsPerson = ACL::getRefTable('default_db/BI_audit_KRS/BI_audit_KRS', "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person");
+			$query = "select person.*, pracownicy.ID as pracownicyId from `{$refKrsToKrsPerson}` ref join `BI_audit_KRS_person` person on ref.REMOTE_PRIMARY_KEY = person.ID left join BI_audit_ENERGA_PRACOWNICY pracownicy on person.pesel = pracownicy.pesel where ref.PRIMARY_KEY = '{$krsId}'";
+			$result = DB::getPDO()->fetchAll($query);
+			if (!$result) echo '<tr><td align="center" colspan="5">Nie znaleziono osób powiązanych z podmiotem</td></tr>';
+			else {
+				$lp = 1;
+				foreach ($result as $row) {
+?>
+            <tr<?=($row['pracownicyId'] ? ' style="color:darkgrey"' : '')?>>
+              <td align="right"><?=$lp++?></td>
+              <td><?=$row['nazwisko']?></td>
+              <td><?=$row['imiona']?></td>
+              <td><?=$row['pesel']?></td>
+              <td><input type="checkbox" name="personId[]" value="<?=$row['ID']?>" onClick="toggle(this)" <?=($row['pracownicyId'] ? "disabled" : "")?>/></td>
+            </tr>
+<?php
+				}
+			}
+?>
+          </tbody>
+        </table>
+        <div class="containter" style="text-align:center">
+          <button type="submit" class="btn btn-primary" name="subaction" value="addKrsPersonToPracownicy" onClick="return validate(this)">Dodaj zaznaczone osoby do tabeli pracowników</button>
+          <a href="" class="btn btn-default">Powrót</a>
+        </div>
+      </div>
+    </div>
+<?php
+		}
+?>
+    <input type="hidden" name="action" value="search">
+  </form>
+</div>
+<script language="JavaScript">
+<!--
+function toggleAll(source) {
+  checkboxes = document.getElementsByName('personId[]');
+  checked = false;
+  for(var i=0, n=checkboxes.length;i<n;i++) {
+    if (!checkboxes[i].disabled) {
+      checkboxes[i].checked = source.checked;
+      checked = true;
+    }
+  }
+  if (source.checked && (!checked)) source.checked = false;
+}
+function toggle(source) {
+  checkboxes = document.getElementsByName('personId[]');
+  all = true;
+  for(var i=0, n=checkboxes.length;i<n;i++) if(!checkboxes[i].disabled && checkboxes[i].checked == false) all = false;
+  source.form['checkAll'].checked = all;
+}
+function validate(source) {
+  checkboxes = document.getElementsByName('personId[]');
+  checked = false;
+  for(var i=0, n=checkboxes.length;i<n;i++) if(checkboxes[i].checked) checked = true;
+  if (!checked) alert('Nie wybrano żadnej osoby!');
+  return checked;
+}
+-->
+</script>
+<?php
+
 	}
 
 	public function defaultAction() {