Explorar o código

- dodanie funkcji zapisujacej nowe rekordy w db oraz dodajace aktualizacje istniejacych

dariusz.andryskowski %!s(int64=7) %!d(string=hai) anos
pai
achega
214ce39637
Modificáronse 2 ficheiros con 117 adicións e 53 borrados
  1. 105 44
      tools/Bocian.php
  2. 12 9
      tools/Bocian.php.view.js

+ 105 - 44
tools/Bocian.php

@@ -66,15 +66,19 @@ public function formDataCsvFileAjax() {
 		foreach ( $arrayDataCsv as $line ) {
 			//$viewFormCol .= '<div style="margin: 10px 0"><span>'.($i + 1).'. </span>';
 
+			$lenArrayLine = count($line); // sprawdzenie długości tablicy
 			$viewFormCol .= '<tr>';
+			$j = 0;
 			foreach ( $line as $keyData => $rowData ) {
 								// utworzenie nagłówków tabeli
 								if( $i == 0)  { $header .= '<td>' . $keyData . '</td>'; }
 								// utworzenie inputa do edycji
-								$viewFormCol .= '<td><input type="text" name="item[$i]['.$keyData.']" value="'.$rowData .'" /></td>';
-
-								if (end($rowData )) { $viewFormCol .= '<td><input type="checkbox" name="item['.$i.']['.$keyData.']" value="'.$rowData.'" /></td>'; }
+								$viewFormCol .= '<td><input type="text" name="item['.$i.']['.$keyData.']" value="'.$rowData .'" class="form-control input-lg" /></td>';
 
+								if ($j == $lenArrayLine - 1) {
+									$viewFormCol .= '<td><input type="checkbox" name="item['.$i.'][update]" value="1" /></td>';
+								}
+								$j++;
 			 }
 			 $viewFormCol .= '</tr>';
 
@@ -83,12 +87,7 @@ public function formDataCsvFileAjax() {
 
 		}
 
-		$viewFormHtml = '<div id="smad-window-modal" style="overflow-x: scroll;"><form method="POST" id="formDataFromCsv" >';
-		$viewFormHtml = ' <thead><tr>'. $header .'
-										 <td>Zaktualizuj</td>
-									 </tr>
-									 </thead>';
-		$viewFormHtml .= '<tbody id="smad-csv-data">'. $viewFormCol .'</tbody></table></form></div>';
+		$viewFormHtml = '<div id="smad-window-modal" style="overflow-x: scroll;"><form method="POST" id="formDataFromCsv" class="form-horizontal"><table class="table table-bordered table-hover table-striped" height="5"><thead><tr>'. $header .'<td>Zaktualizuj</td></tr></thead><tbody id="smad-csv-data">'. $viewFormCol .'</tbody></table></form></div>';
 
 		//return $viewFormHtml;
 		return [
@@ -117,54 +116,67 @@ public function formDataCsvFileAjax() {
  * Funkcja ajax do zapisania danych z przesłanego formularza z parsowanego CSV
  */
 	public function saveFormCsvFileAjax($args) {
+		//dane z formularza
 		$formData = V::get('formData', '', $args);
+		$enumType = V::get('enumType', '', $args);
+
+		$arrayInfo = array();
+		$arrayInfo['insert'] = 0;
+		$arrayInfo['update'] = 0;
+		$arrayInfo['error'] = 0;
+
+		// pobranie nazwy tabeli z db
+		$table = $this->getNameTableByEnum( strtoupper($enumType) );
 
+		if ( $table == null ) throw new Exception('Błąd zapisu. Tabela w db nie istnieje');
 		// unserialize data from serialize in javascript
 		parse_str($formData, $resultArrayFormdata);
-		//$formData = V::get('formData', '', $_REQUEST, '');
-		//print_r($resultArrayFormdata);
 
 		if ( count($resultArrayFormdata['item']) == 0) {
 			throw new Exception("Wystapił problem podczas przesyłania danych - brak przesłanych danych.");
 		}
 
-
+		// dodanie/aktualizacjia wpisu
 		foreach ( $resultArrayFormdata['item'] as $line ) {
-
-			$insertData = array();
-			$insertTemp = array();
-				print_r($line);
-			// foreach ( $line as $keyData => $rowData) {
-			//
-			// 	$insertTemp[$keyData] = $rowData;
-			// //	print $keyData ."--". $rowData . "<br />";
-			// 	$insertData = array_merge($insertTemp, $insertData);
-			//
-			// }
-
-	//		$this->saveDataToDb($table, $arrayData);
+			// aktualizuj
+			if ( array_key_exists('update', $line) ) {
+					$response = $this->updateDataToDb($table, $line);
+			} else {
+				$response = $this->saveDataToDb($table, $line);
+			}
+			// tworzymy listę informacji o liczbie nowych/zaktualizowanych/z błedami rekordów
+			 $arrayInfo = $this->responseCountListInfoAboutSaveData($response, $arrayInfo);
 		}
-		//print_r($insertData);
-					exit;
 
-exit;
-		try {
+		return [
+			'msg' => "Zapisano dane. Liczba nowych rekordów: (" . $arrayInfo['insert'] . "); Liczba zaktualizowanych rekordów: (" . $arrayInfo['update'] . ")'; Liczba rekordów nie zapisanych: (" . $arrayInfo['error'] . ")" ,
+			'type' => "success",
+		];
 
-			$responseInsert = DB::getPDO()->insert('BI_audit_ENERGA_RUM_KONTRAHENCI', [
-				'Pelna_nazwa_kontrahenta' => 'Teste NAZWY FIRMY ',
-				'Skrocona_Nazwa_Kontrahenta' => 'TEST NAZWY SKR.'
-			]);
+	}
 
-		} catch (Exception $e) {
+	/**
+	 * Count list
+	 */
+	public function responseCountListInfoAboutSaveData($response, $arrayInfo) {
 
-			print_r($e);
-		}
+		if (!is_array($response)) throw new Exception('Wystapił bład podczas zapisu danych');
 
 
-		//print_r($formData);
-		//todo: dane do zapisania z formularza
-	}
+		switch(key($response)) {
+			case 'insert':
+				$arrayInfo['insert'] = $arrayInfo['insert'] + 1;
+				break;
+			case 'update':
+				$arrayInfo['update'] = $arrayInfo['insert'] + 1;
+				break;
+			case 'error':
+				$arrayInfo['error'] = $arrayInfo['insert'] + 1;
+				break;
+		}
 
+		return $countData;
+	}
 
 	/**
 	 * Function parse csv to array
@@ -188,17 +200,66 @@ exit;
 	  return $data;
 	}
 
-	public function saveDataToDb($table, $arrayData ) {
+	/**
+	 *  Save data in database
+	 */
+	public function saveDataToDb( $table, $arrayData ) {
+		$response = array();
 
 		try {
-			$responseInsert = DB::getPDO()->insert($table, $arrayData);
+				// dodanie nowego rekordu
+				$responseInsert = DB::getPDO()->insert($table, $arrayData);
+				$response['insert'] = 1;
+
 		} catch (Exception $e) {
-			print_r($e);
+				// aktualizacja
+				$response = $this->updateDataToDb( $table, $arrayData );
 		}
+
+		return $response;
+	}
+
+
+	/**
+	 *  Update data in database
+	 */
+	public function updateDataToDb( $table, $arrayData ) {
+		$response = array();
+
+			// aktualizacja
+			try {
+				$responseInsert = DB::getPDO()->update($table, $arrayData);
+				$response['update'] = 1;
+
+			} catch (Exception $e) {
+				print_r($e);
+					$response['error'] = 1;
+			}
+
+		return $response;
 	}
 
 
 
+	/**
+	 * Get name table by enum
+	 */
+	public function getNameTableByEnum($type) {
+
+		$nameTable = null;
+			switch ($type) {
+					case 'PRACOWNICY':
+						$nameTable = 'BI_audit_ENERGA_RUM_KONTRAHENCI';
+					break;
+					case 'KONTRAHENCI':
+						$nameTable = 'BI_audit_ENERGA_PRACOWNICY';
+					break;
+			}
+
+			return $nameTable;
+	}
+
+
 
 	public function fetchEnergaRumKontrahenciPowiazaniaAjaxAction() {
 		Response::sendTryCatchJson(array($this, 'fetchEnergaRumKontrahenciPowiazaniaAjax')); // , $args = 'JSON_FROM_REQUEST_BODY');
@@ -1053,7 +1114,7 @@ public function showPowiazaniaEnergaRumKontrahenciPowiazania($items) {
 					</div>';
 
 					$formAddNewData ='
-					<li><a  title="IMPORT KONTRAHENCI" onClick="showViewUploadFile(event, \'Import kontrahentów\' )" class="btn btn-info">IMPORT KONTRAHENCI</a></li>
+					<li><a  title="IMPORT KONTRAHENCI" onClick="showViewUploadFile(event, \'Import kontrahentów\', \'kontrahenci\' )" class="btn btn-info">IMPORT KONTRAHENCI</a></li>
 					<!--<li><a href="#" title="DODAJ KONTRAHENCI" class="btn btn-info">+ DODAJ KONTRAHENCI</a></li>-->'; //todo: dodać obsługe doddawania kontrahentow
 
 					$showButtonNextStep = '<button type="button" class="btn btn-primary" onClick="generateBiAuditRaport(event)" id="button-generate-reaport">SZUKAJ POWIĄZAŃ</button>';
@@ -1075,7 +1136,7 @@ public function showPowiazaniaEnergaRumKontrahenciPowiazania($items) {
 					</div>';
 
 					$formAddNewData ='
-					<li><a href="#" title="IMPORT PRACOWNIKÓW" class="btn btn-info">IMPORT PRACOWNIKÓW</a></li>
+					<li><a  title="IMPORT PRACOWNIKÓW" onClick="showViewUploadFile(event, \'Import pracowników\', \'pracownicy\' )" class="btn btn-info">IMPORT PRACOWNIKÓW</a></li>
 					<!--<li><a href="#" title="DODAJ PRACOWNIKÓW" class="btn btn-info" >+ DODAJ PRACOWNIKÓW</a></li>-->'; //todo: dodać obsługe doddawania pracownikow
 
 					$showButtonNextStep = '<a href="index.php?_route=UrlAction_Bocian#KONTRAHENCI"  title="DODAJ DO ANALIZY" class="btn btn-primary">DODAJ DO ANALIZY</a>';

+ 12 - 9
tools/Bocian.php.view.js

@@ -109,9 +109,11 @@ function initLocalStorage() {
 /**
  * Funkcja pozwala na wyświetlenie okienka importu
  */
-function showViewUploadFile( event, headerTitle ) {
+function showViewUploadFile( event, headerTitle, enumType ) {
 	event.preventDefault();
 
+	var enumType = enumType;
+
 	swal({
 		title: 'Importuj plik csv',
 		input: 'file',
@@ -129,7 +131,7 @@ function showViewUploadFile( event, headerTitle ) {
 				var formData = new FormData();
 				formData.append('file', file);
 				console.log('uploadowany plik: ',formData);
-				var responseParseFile = parseCsvFile( formData );
+				var responseParseFile = parseCsvFile( formData, enumType );
 
 				console.log('TODO: upload file', file)
 				resolve('file imported')
@@ -148,7 +150,7 @@ function showViewUploadFile( event, headerTitle ) {
 /**
  * Funkcja przekazuje dane z pliku csv do przeparsowania
  */
-function parseCsvFile( fileData ) {
+function parseCsvFile( fileData, enumType ) {
 	event.preventDefault();
 
 	fetch(URL_FORM_DATA_CSV_FILE_AJAX, {
@@ -177,7 +179,7 @@ function parseCsvFile( fileData ) {
 
 
 										// zapisanie danych
-						 				var responseSaveData = saveFormCsvFileAjaxAction();
+						 				var responseSaveData = saveFormCsvFileAjaxAction(enumType);
 
 						 				console.log('TODO: data save from form', dataForm)
 						 				resolve('data save from form')
@@ -199,7 +201,7 @@ function parseCsvFile( fileData ) {
 /**
  * Funkcja zapisuje dane z formularza do wgrania pliku csv
  */
-function saveFormCsvFileAjaxAction() {
+function saveFormCsvFileAjaxAction(enumType) {
 	event.preventDefault();
 
 	var formDataFrom =	$("#formDataFromCsv").serialize();
@@ -217,7 +219,8 @@ console.log('dane z formularza formDataFrom ', formDataFrom );
 			'Content-Type': 'application/json'
 		},
 		body: JSON.stringify({
-			 formData: formDataFrom
+			 formData: formDataFrom,
+			 enumType: enumType
 		})
 	})
 	.then(function(response) {
@@ -287,7 +290,7 @@ function generateBiAuditRaport(event) {
 
 						p5UI__notifyAjaxCallback(result);
 						defaultBIAuditLocalStorage();
-						window.setTimeout(window.location.href = "https://bravecom.yellowgroup.pl/SE/index.php?_route=ViewTableAjax&namespace=default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA", 4000);
+						window.setTimeout(window.location.href = "/SE/index.php?_route=ViewTableAjax&namespace=default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA", 4000);
 						resolve(result.msg);
 
 					} else {
@@ -706,7 +709,7 @@ console.log('kkontrahenci data', data);
 
 				if (data.body.pagination !== undefined) {
 					Pagination.Init(document.getElementById('pagination-kontrahenci'), {
-							url: 'https://bravecom.yellowgroup.pl/SE/index.php?_route=UrlAction_Bocian#KONTRAHENCI',
+							url: '/SE/index.php?_route=UrlAction_Bocian#KONTRAHENCI',
 							id_pagination: 'pagination-kontrahenci',
 							type: 'KONTRAHENCI',
 							total_items: data.body.pagination.total_items, // pages size
@@ -778,7 +781,7 @@ function urlFetchPracownicy(page) {
 
 			if (data.body.pagination.size) {
 						Pagination.Init(document.getElementById('pagination-pracownicy'), {
-							  url: 'https://bravecom.yellowgroup.pl/SE/index.php?_route=UrlAction_Bocian#PRACOWNICY',
+							  url: '/SE/index.php?_route=UrlAction_Bocian#PRACOWNICY',
 								id_pagination: 'pagination-pracownicy',
 								type: 'PRACOWNICY',
 								total_items: data.body.pagination.total_items, // pages size