From 9320ce76a1564b5db68d79d8cca591b6ea9bfe33 Mon Sep 17 00:00:00 2001 From: Flavio Barachino Date: Thu, 7 Aug 2025 16:10:49 +0200 Subject: [PATCH] sistemato alcuni riferimenti ad importo e modifica per sole spese. Vedere anche per entrate --- app/Http/Controllers/MovimentiController.php | 86 ++++++++++++------- app/Models/Movimenti.php | 74 ++++++++-------- config/logging.php | 8 ++ ...2_02_04_134245_create_movimentis_table.php | 4 +- public/js/app/movimenti.js | 5 +- .../views/conti/movimenti/list.blade.php | 19 ++-- 6 files changed, 112 insertions(+), 84 deletions(-) diff --git a/app/Http/Controllers/MovimentiController.php b/app/Http/Controllers/MovimentiController.php index d00a44e..c8583a7 100644 --- a/app/Http/Controllers/MovimentiController.php +++ b/app/Http/Controllers/MovimentiController.php @@ -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); } + } diff --git a/app/Models/Movimenti.php b/app/Models/Movimenti.php index 4e691c2..d4c691e 100644 --- a/app/Models/Movimenti.php +++ b/app/Models/Movimenti.php @@ -10,7 +10,8 @@ use Rap2hpoutre\FastExcel\FastExcel; use App\Models\Categorie; use App\Models\Conto; use App\Models\User; -//use Illuminate\Support\Facades\Schema; +use App\Models\ImportRule; + class Movimenti extends Model { @@ -33,7 +34,8 @@ class Movimenti extends Model public static $query= 'SELECT a.id, a.mov_data, - a.mov_importo, + a.mov_importo_dare, + a.mov_importo_avere, a.mov_descrizione, c.cat_name, t.tag_name, @@ -186,7 +188,8 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; 'mov_data' => $request['mov_data'], 'mov_fk_categoria'=>$request['mov_fk_categoria'], 'mov_descrizione'=>$request['mov_descrizione'], - 'mov_importo'=>$request['mov_importo'], + 'mov_importo_dare' => $request['mov_importo_dare'], + 'mov_importo_avere' => $request['mov_importo_avere'], 'mov_fk_tags'=>$request['mov_fk_tags'], 'mov_inserito_da'=>$request['userid'], ]); @@ -203,7 +206,8 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; $expression= DB::raw('SELECT a.id, a.mov_data, - a.mov_importo, + a.mov_importo_dare, + a.mov_importo_avere, a.mov_descrizione, c.cat_name, t.tag_name, @@ -222,7 +226,8 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; $expression=DB::raw(' SELECT a.id, a.mov_data, - a.mov_importo, + a.mov_importo_dare, + a.mov_importo_avere, a.mov_descrizione, c.cat_name, t.tag_name, @@ -241,7 +246,8 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; $expression=DB::raw('SELECT a.id, a.mov_data, - a.mov_importo, + a.mov_importo_dare, + a.mov_importo_avere, a.mov_descrizione, c.cat_name, t.tag_name, @@ -357,25 +363,9 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; ]; } - // Test applicazione regole - public function setCategoriaMovimento($movimento) - { - $rules= \App\Models\ImportRule::all(); - foreach ($rules as $role) - { - if (preg_match("/$role->parola/i","$movimento->descrizione")) - { - $movimento->catemov_fk_categoria=$role->categoria_id; - } - else - { - $movimento->catemov_fk_categoria=1; - } - } - - } + // Ritorna la categoria suggerita in base alla descrizione del movimento da utilizzare sul db public static function getSuggestedCategory($descrizione) { $rule = ImportRule::whereRaw('? LIKE CONCAT("%", pattern, "%")', [$descrizione])->first(); @@ -384,7 +374,7 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; public static function setCategoriesFromCsv($descrizione) { - $rules = \App\Models\ImportRule::all(); + $rules = ImportRule::all(); $categoria = 1; // Default category foreach ($rules as $rule) { @@ -400,12 +390,13 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; - + // Ritorna gli anni presenti nei movimenti public static function getYearsFromMovimenti() { - $anni=DB::table('movimentis')->select(DB::raw('DISTINCT YEAR(mov_data) as anno'))->get(); - // dd($anni); // for test purposes - return $anni; + return self::selectRaw('YEAR(mov_data) as anno') + ->distinct() + ->orderBy('anno', 'desc') + ->get(); } @@ -424,24 +415,27 @@ JOIN categories as c ON a.mov_fk_categoria=c.id'; } } + // Ritorna la somma delle entrate per un anno specifico public static function getEntrate($year) { - $entrate_anno=DB::table('movimentis') - ->where('mov_importo','>',0) - ->whereYear('mov_data', '=' , $year) - ->sum('mov_importo'); - //->get(); - return $entrate_anno; + return self::whereYear('mov_data', $year) + ->sum('mov_importo_avere'); } + // Ritorna la somma delle uscite per un anno specifico public static function getUscite($year) { - $uscite_anno=DB::table('movimentis') - ->where('mov_importo','<',0) - ->whereYear('mov_data', '=' , $year) - ->sum('mov_importo'); - //->get(); - return ($uscite_anno); + return self::whereYear('mov_data', $year) + ->sum('mov_importo_dare'); + } + + public static function cleanImporto($importo){ + // Rimuove i punti e sostituisce la virgola con un punto + $importo = str_replace('.', '', $importo); + $importo = str_replace(',', '.', $importo); + $importo = str_replace('-', '', $importo); + $importo = str_replace('+', '', $importo); + return $importo; } } diff --git a/config/logging.php b/config/logging.php index d526b64..f078a43 100644 --- a/config/logging.php +++ b/config/logging.php @@ -65,6 +65,14 @@ return [ 'replace_placeholders' => true, ], + 'app' => [ + 'driver' => 'single', + 'path' => storage_path('logs/gestionale2025.log'), + 'level' => env('LOG_LEVEL', 'debug'), + 'days' => env('LOG_DAILY_DAYS', 14), + 'replace_placeholders' => true, + ], + 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), diff --git a/database/migrations/2022_02_04_134245_create_movimentis_table.php b/database/migrations/2022_02_04_134245_create_movimentis_table.php index b6f4a0f..ec86736 100644 --- a/database/migrations/2022_02_04_134245_create_movimentis_table.php +++ b/database/migrations/2022_02_04_134245_create_movimentis_table.php @@ -20,8 +20,8 @@ class CreateMovimentisTable extends Migration $table->unsignedBigInteger('mov_fk_categoria'); $table->foreign('mov_fk_categoria')->references('id')->on('categories'); $table->longText('mov_descrizione'); - $table->decimal('mov_importo_dare',8,2); - $table->decimal('mov_importo_avere',8,2); + $table->decimal('mov_importo_dare',8,2)->nullable(); + $table->decimal('mov_importo_avere',8,2)->nullable(); $table->unsignedBigInteger('mov_inserito_da'); $table->foreign('mov_inserito_da')->references('id')->on('users'); $table->unsignedBigInteger('mov_fk_tags'); diff --git a/public/js/app/movimenti.js b/public/js/app/movimenti.js index 6ee803a..628254d 100644 --- a/public/js/app/movimenti.js +++ b/public/js/app/movimenti.js @@ -91,6 +91,7 @@ $.getJSON("/admin/service/contolist", {}, function(contis) { }); }); +// Modifica movimento $(document).on('click', '.open_modal_modifica', function() { var url = "/admin/movimenti/modify"; var riga_id = $(this).val(); @@ -135,9 +136,9 @@ $(document).on('click', '.open_modal_modifica', function() { ); }); $('.modal-title').text('Modifica movimento'); - $('#data').val(data[0].mov_data); + $('#data').val(data[0].mov_data.substring(0,10)); $('#descrizione').val(data[0].mov_descrizione); - $('#importo').val(data[0].mov_importo); + $('#importo').val(data[0].mov_importo_dare); $('#myModal').modal('show'); // $('.panel-heading').text('Modifica movimento'); diff --git a/resources/views/conti/movimenti/list.blade.php b/resources/views/conti/movimenti/list.blade.php index 59380b1..caf7904 100644 --- a/resources/views/conti/movimenti/list.blade.php +++ b/resources/views/conti/movimenti/list.blade.php @@ -32,7 +32,8 @@ Data Categoria - Conto + Conto Dare + Conto Avere Descrizione Importo Dare Importo Avere @@ -42,13 +43,13 @@ @foreach( $movimenti as $movimento ) - {{ $movimento->mov_data}} {{ $movimento->cat_name }} - {{ $movimento->nomeConto }} + {{ $movimento->nomeContoDare ?? '' }} + {{ $movimento->nomeContoAvere ?? '' }} {{ $movimento->mov_descrizione }} - € {{ $movimento->mov_importo_dare }} - € {{ $movimento->mov_importo_avere }} + {{ $movimento->mov_importo_dare ." €"}} + {{ $movimento->mov_importo_avere." €" }}     @@ -71,6 +72,8 @@ + +