sistemato alcuni riferimenti ad importo e modifica per sole spese. Vedere anche per entrate
This commit is contained in:
parent
f82f65a0c5
commit
9320ce76a1
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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'),
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -32,7 +32,8 @@
|
||||
<tr>
|
||||
<th>Data</th>
|
||||
<th>Categoria</th>
|
||||
<th>Conto</th>
|
||||
<th>Conto Dare</th>
|
||||
<th>Conto Avere</th>
|
||||
<th>Descrizione</th>
|
||||
<th>Importo Dare</th>
|
||||
<th>Importo Avere</th>
|
||||
@ -42,13 +43,13 @@
|
||||
<tbody>
|
||||
@foreach( $movimenti as $movimento )
|
||||
<tr>
|
||||
|
||||
<td>{{ $movimento->mov_data}}</td>
|
||||
<td>{{ $movimento->cat_name }}</td>
|
||||
<td>{{ $movimento->nomeConto }}</td>
|
||||
<td>{{ $movimento->nomeContoDare ?? '' }}</td>
|
||||
<td>{{ $movimento->nomeContoAvere ?? '' }}</td>
|
||||
<td>{{ $movimento->mov_descrizione }}</td>
|
||||
<td>€ {{ $movimento->mov_importo_dare }}</td>
|
||||
<td>€ {{ $movimento->mov_importo_avere }}</td>
|
||||
<td>{{ $movimento->mov_importo_dare ." €"}}</td>
|
||||
<td>{{ $movimento->mov_importo_avere." €" }}</td>
|
||||
<td>
|
||||
<button class="btn btn-warning btn-detail open_modal_modifica" value="{{ $movimento->id }}"><i class="fa-solid fa-pencil"></i></button>
|
||||
<a class="btn btn-danger" href="/admin/movimenti/delete?id={{ $movimento->id }}"><i class="fa-solid fa-trash-can"></i></a>
|
||||
@ -71,6 +72,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- MODAL NEW -->
|
||||
<div class="modal fade " id="myModal" tabindex="-1" role="dialog"
|
||||
aria-labelledby="myModalLabel" aria-hidden="true">
|
||||
@ -110,10 +113,10 @@
|
||||
<div class="col-xs-5">
|
||||
<label for="importo" class="form-label">Importo</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-addon"> <i class="fa fa-eur"></i>
|
||||
</span> <input type="number" step="0.01" min="-999999"
|
||||
<span class="input-group-addon"> <i class="fa fa-eur"></i></span>
|
||||
<input type="number" step="0.01" min="-999999"
|
||||
max="999999" class="form-control" id="importo" size="50"
|
||||
name="mov_importo" aria-describedby="importo">
|
||||
name="mov_importo_dare" aria-describedby="importo">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-7">
|
||||
|
Loading…
x
Reference in New Issue
Block a user