sistemato alcuni riferimenti ad importo e modifica per sole spese. Vedere anche per entrate
This commit is contained in:
@@ -349,7 +349,7 @@ class MovimentiController extends Controller
|
||||
$db_fields=Movimenti::getDbFields();
|
||||
|
||||
return view('conti.importGeneric',['csv'=>$csv_headers,'db'=>$db_fields,'filename'=>$filename]);
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
return 'Nessun File trovato';
|
||||
@@ -358,42 +358,63 @@ class MovimentiController extends Controller
|
||||
|
||||
public function importmappedCsv(Request $request)
|
||||
{
|
||||
//$request->mov_data
|
||||
$mapped=$request['mapping'];
|
||||
foreach( $mapped as $key => $value)
|
||||
{
|
||||
if($value!=null)
|
||||
{
|
||||
$this->map[$key]=$value;
|
||||
$collection = collect($this->map);
|
||||
$this->prepareMapping($request['mapping']);
|
||||
$filename = Storage::path($request->filename);
|
||||
|
||||
(new FastExcel)->configureCsv(';')->import($filename, function($line) {
|
||||
$data = $this->mapCsvLineToDb($line);
|
||||
// dd($data);
|
||||
if (isset($data['mov_data'])) {
|
||||
Movimenti::create($data);
|
||||
}
|
||||
});
|
||||
|
||||
return redirect(Route('movimenti'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepara la mappatura tra i campi CSV e i campi DB
|
||||
*/
|
||||
private function prepareMapping($mapping)
|
||||
{
|
||||
foreach ($mapping as $key => $value) {
|
||||
if ($value != null) {
|
||||
$this->map[$key] = $value;
|
||||
}
|
||||
}
|
||||
//dd ($this->map,$collection);
|
||||
}
|
||||
|
||||
$mappdCvs=(new FastExcel)->configureCsv(';')->import(
|
||||
Storage::path($request->filename), function($line){
|
||||
if(isset($line[$this->map['mov_data']]))
|
||||
{
|
||||
/**
|
||||
* Mappa una riga del CSV ai campi del database
|
||||
*/
|
||||
private function mapCsvLineToDb($line)
|
||||
{
|
||||
if (isset($line[$this->map['mov_data']])) {
|
||||
$data = [
|
||||
'mov_data' => $this->parseDate($line[$this->map['mov_data']]),
|
||||
'mov_descrizione' => $line[$this->map['mov_descrizione']] ?? null,
|
||||
'mov_importo_dare' => Movimenti::cleanImporto($line[$this->map['mov_importo_dare']]?? null) ,
|
||||
'mov_importo_avere' => Movimenti::cleanImporto($line[$this->map['mov_importo_avere']] ?? null),
|
||||
'mov_fk_categoria' => Movimenti::setCategoriesFromCsv($line[$this->map['mov_descrizione']] ?? ''),
|
||||
'mov_fk_tags' => 1,
|
||||
'mov_inserito_da' => Auth::id(),
|
||||
'conto_id_da' => 1,
|
||||
'conto_id_a' => 1,
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
return null; // Se non c'è la data, non importa la riga
|
||||
|
||||
Movimenti::create (
|
||||
['mov_data' => DateTime::createFromFormat(
|
||||
'd/m/Y',$line[$this->map['mov_data']]),
|
||||
'mov_descrizione' => $line[$this->map['mov_descrizione']],
|
||||
'mov_importo_dare' => $line[$this->map['mov_importo_dare']],
|
||||
'mov_importo_avere' => $line[$this->map['mov_importo_avere']],
|
||||
'mov_fk_categoria' => Movimenti::setCategoriesFromCsv($line[$this->map['mov_descrizione']]),
|
||||
'mov_fk_tags' => 1,
|
||||
'mov_inserito_da' => Auth::id(),
|
||||
'conto_id_da' => 1
|
||||
,'conto_id_a' => 1,
|
||||
]
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
// dd($map,$mapped,$request,$collection);
|
||||
// dd($request);
|
||||
return redirect(Route('movimenti'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converte la data dal formato d/m/Y a Y-m-d
|
||||
*/
|
||||
private function parseDate($dateString)
|
||||
{
|
||||
$date = DateTime::createFromFormat('d/m/Y', $dateString);
|
||||
return $date ? $date->format('Y-m-d') : null;
|
||||
}
|
||||
|
||||
public function importFile()
|
||||
@@ -428,4 +449,5 @@ class MovimentiController extends Controller
|
||||
$mov=Movimenti::getMovimentoById($id);
|
||||
return json_encode($mov);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user