Commit iniziale
This commit is contained in:
398
app/Models/Movimenti.php
Normal file
398
app/Models/Movimenti.php
Normal file
@@ -0,0 +1,398 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Rap2hpoutre\FastExcel\FastExcel;
|
||||
use App\Models\Categorie;
|
||||
use App\Models\Conto;
|
||||
use App\Models\User;
|
||||
|
||||
class Movimenti extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $dates = ['mov_data'];
|
||||
protected $casts = [ 'mov_data'=>'datetime'];
|
||||
|
||||
public static $query= 'SELECT
|
||||
a.id,
|
||||
a.mov_data,
|
||||
a.mov_importo,
|
||||
a.mov_descrizione,
|
||||
c.cat_name,
|
||||
t.tag_name,
|
||||
(SELECT Count(entity_id) as quanti FROM gen_docs WHERE entity=0 AND entity_id = a.id) as quanti
|
||||
FROM movimentis as a
|
||||
JOIN tags as t ON a.mov_fk_tags=t.id
|
||||
JOIN categories as c ON a.mov_fk_categoria=c.id';
|
||||
|
||||
public function Conto()
|
||||
{
|
||||
return $this->belongsTo(Conto::class);
|
||||
}
|
||||
|
||||
public function User()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function Categorie()
|
||||
{
|
||||
return $this->belongsTo(Categorie::class);
|
||||
}
|
||||
|
||||
public function Tags()
|
||||
{
|
||||
return $this->belongsTo(Tags::class);
|
||||
}
|
||||
|
||||
public static function getList() {
|
||||
|
||||
|
||||
$expression=DB::raw(
|
||||
'SELECT
|
||||
a.id,
|
||||
a.mov_data,
|
||||
a.mov_importo,
|
||||
a.mov_descrizione,
|
||||
c.cat_name,
|
||||
t.tag_name,
|
||||
co.nomeConto,
|
||||
(SELECT Count(entity_id) as quanti FROM gen_docs WHERE entity=0 AND entity_id = a.id) as quanti
|
||||
FROM movimentis as a
|
||||
JOIN tags as t ON a.mov_fk_tags=t.id
|
||||
JOIN categories as c ON a.mov_fk_categoria=c.id
|
||||
JOIN contos as co ON a.conto_id=co.id'
|
||||
);
|
||||
|
||||
$query = $expression->getValue(DB::connection()->getQueryGrammar());
|
||||
return DB::select($query);
|
||||
}
|
||||
|
||||
public static function getSaldo($date) {
|
||||
return DB::table('movimentis')->whereYear('mov_data','=',$date)->sum('mov_importo');
|
||||
}
|
||||
|
||||
public static function getSaldoTot() {
|
||||
return DB::table('movimentis')->sum('mov_importo');
|
||||
}
|
||||
|
||||
public static function insSpesa( $request) {
|
||||
DB::table('movimentis')->insert(
|
||||
[
|
||||
'mov_data'=>$request->mov_data,
|
||||
'mov_fk_categoria'=>$request->mov_fk_categoria,
|
||||
'mov_descrizione'=>$request->mov_descrizione,
|
||||
'mov_importo'=>'-'.$request->mov_importo,
|
||||
'mov_fk_tags'=>$request->mov_fk_tags,
|
||||
'mov_inserito_da'=>$request->userid,
|
||||
'conto_id'=>$request->conto_id,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function giroconto($request){
|
||||
|
||||
$requestUscita=$request;
|
||||
$requestEntrata=$request;
|
||||
|
||||
$requestUscita['conto_id']=$requestUscita->daConto;
|
||||
$requestUscita['mov_fk_tags']=1;
|
||||
$rqUscita = $requestUscita;
|
||||
self::insSpesa($rqUscita);
|
||||
|
||||
$requestEntrata['conto_id']=$requestEntrata->aConto;
|
||||
$requestEntrata['mov_fk_tags']=1;
|
||||
$rqEntrata = $requestEntrata;
|
||||
self::insEntrata($rqEntrata);
|
||||
unset($requestUscita['daConto']);
|
||||
unset($requestEntrata['aConto']);
|
||||
|
||||
}
|
||||
|
||||
public static function insEntrata( $request) {
|
||||
DB::table('movimentis')->insert(
|
||||
[
|
||||
'mov_data'=>$request['mov_data'],
|
||||
'mov_fk_categoria'=>$request['mov_fk_categoria'],
|
||||
'mov_descrizione'=>$request['mov_descrizione'],
|
||||
'mov_importo'=>$request['mov_importo'],
|
||||
'mov_fk_tags'=>$request['mov_fk_tags'],
|
||||
'mov_inserito_da'=>$request['userid'],
|
||||
'conto_id'=>$request->conto_id,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function export() {
|
||||
return DB::table('movimentis')
|
||||
->join('categories','movimentis.mov_fk_categoria','=','categories.id')
|
||||
->join('tags','movimentis.mov_fk_tags','=','tags.id')
|
||||
->join('contos','movimentis.conto_id','=','contos.id')
|
||||
->selectRaw('mov_data AS Data,cat_name AS Categoria,tag_name AS Tag,NomeConto AS Conto, mov_descrizione AS Descrizione,mov_importo AS Importo')
|
||||
->orderBy('Data','asc')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function reportSpesa($year,$month) {
|
||||
return DB::table('movimentis')
|
||||
->selectRaw('ABS(Sum(movimentis.mov_importo)) as resoconto, categories.cat_name,categories.id')
|
||||
->join('categories','movimentis.mov_fk_categoria','=','categories.id')
|
||||
->where('mov_importo','<',0)
|
||||
->whereYear('mov_data',$year)
|
||||
->whereMonth('mov_data',$month)
|
||||
->groupBy('cat_name','categories.id')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function reportEntrate($year,$month) {
|
||||
return DB::table('movimentis')
|
||||
->selectRaw('ABS(Sum(movimentis.mov_importo)) as resoconto, categories.cat_name,categories.id')
|
||||
->join('categories','movimentis.mov_fk_categoria','=','categories.id')
|
||||
->where('mov_importo','>',0)
|
||||
->whereYear('mov_data',$year)
|
||||
->whereMonth('mov_data',$month)
|
||||
->groupBy('cat_name','categories.id')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getMovimentoById($id) {
|
||||
return DB::table('movimentis')
|
||||
->join('categories','movimentis.mov_fk_categoria','=','categories.id')
|
||||
->join('tags','movimentis.mov_fk_tags','=','tags.id')
|
||||
->where('movimentis.id','=',$id)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function updateMovimenti($request) {
|
||||
DB::table('movimentis')
|
||||
->where('id','=', $request['id'])
|
||||
->update([
|
||||
'mov_data' => $request['mov_data'],
|
||||
'mov_fk_categoria'=>$request['mov_fk_categoria'],
|
||||
'mov_descrizione'=>$request['mov_descrizione'],
|
||||
'mov_importo'=>$request['mov_importo'],
|
||||
'mov_fk_tags'=>$request['mov_fk_tags'],
|
||||
'mov_inserito_da'=>$request['userid'],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function deleteMovimento($id) {
|
||||
DB::table('movimentis')
|
||||
->where('id','=', $id)
|
||||
->delete();
|
||||
}
|
||||
|
||||
public static function listByCatMonth($month,$cat,$year) {
|
||||
|
||||
$expression= DB::raw('SELECT
|
||||
a.id,
|
||||
a.mov_data,
|
||||
a.mov_importo,
|
||||
a.mov_descrizione,
|
||||
c.cat_name,
|
||||
t.tag_name,
|
||||
(SELECT Count(entity_id) as quanti FROM gen_docs WHERE entity=0 AND entity_id = a.id) as quanti
|
||||
FROM movimentis as a
|
||||
JOIN tags as t ON a.mov_fk_tags=t.id
|
||||
JOIN categories as c ON a.mov_fk_categoria=c.id
|
||||
WHERE Month(a.mov_data)='.$month.' AND Year(a.mov_data)='.$year.' AND a.mov_fk_categoria='.$cat
|
||||
);
|
||||
$query = $expression->getValue(DB::connection()->getQueryGrammar());
|
||||
return DB::select($query);
|
||||
|
||||
}
|
||||
|
||||
public static function listByCategory($cat) {
|
||||
|
||||
$expression=DB::raw(' SELECT a.id,
|
||||
a.mov_data,
|
||||
a.mov_importo,
|
||||
a.mov_descrizione,
|
||||
c.cat_name,
|
||||
t.tag_name,
|
||||
(SELECT Count(entity_id) as quanti FROM gen_docs WHERE entity=0 AND entity_id = a.id) as quanti
|
||||
FROM movimentis as a
|
||||
JOIN tags as t ON a.mov_fk_tags=t.id
|
||||
JOIN categories as c ON a.mov_fk_categoria=c.id
|
||||
WHERE a.mov_fk_categoria = '.$cat );
|
||||
|
||||
$query = $expression->getValue(DB::connection()->getQueryGrammar());
|
||||
return DB::select($query);
|
||||
|
||||
}
|
||||
|
||||
public static function getByTag($tag) {
|
||||
$expression=DB::raw('SELECT
|
||||
a.id,
|
||||
a.mov_data,
|
||||
a.mov_importo,
|
||||
a.mov_descrizione,
|
||||
c.cat_name,
|
||||
t.tag_name,
|
||||
(SELECT Count(entity_id) as quanti FROM gen_docs WHERE entity=0 AND entity_id = a.id) as quanti
|
||||
FROM movimentis as a
|
||||
JOIN tags as t ON a.mov_fk_tags=t.id
|
||||
JOIN categories as c ON a.mov_fk_categoria=c.id
|
||||
WHERE a.mov_fk_tags = '.$tag );
|
||||
$query = $expression->getValue(DB::connection()->getQueryGrammar());
|
||||
return DB::select($query);
|
||||
}
|
||||
|
||||
public static function importEstrattoIng($filename)
|
||||
{
|
||||
|
||||
$inputPath='/var/www/html/gestionale_mt/public/storage/tenant'.tenant('id').'/'.$filename;
|
||||
// $outputPath='/var/www/html/gestionale_mt/public/tenant'.tenant('id').'/import/'.$filename.'.csv';
|
||||
$outputPath = $inputPath.'.csv';
|
||||
rename($inputPath,$outputPath);
|
||||
$collection = (new FastExcel)->configureCsv(";")->import($outputPath, function ($line){
|
||||
if($line['DATA VALUTA'])
|
||||
{
|
||||
if($line['ENTRATE']!=null)
|
||||
{
|
||||
$importo = $line['ENTRATE'];
|
||||
} elseif($line['USCITE']!=null)
|
||||
{
|
||||
$importo = $line['USCITE'];
|
||||
}
|
||||
$euro=str_replace(',','.',str_replace('+','',str_replace('.','', $importo)));
|
||||
list($giorno,$mese,$anno) = explode('/',$line['DATA VALUTA']);
|
||||
$data="$anno-$mese-$giorno";
|
||||
self::insEntrata([
|
||||
'mov_data'=>self::dateFormat(0,$line['DATA VALUTA']),
|
||||
'mov_fk_categoria'=>1,
|
||||
'mov_descrizione'=>$line['DESCRIZIONE OPERAZIONE'],
|
||||
'mov_importo'=>$euro,
|
||||
'mov_fk_tags'=>1,
|
||||
'userid'=>1,
|
||||
]);
|
||||
}
|
||||
});
|
||||
unlink($outputPath);
|
||||
|
||||
}
|
||||
|
||||
public static function importEstrattoCR($filename)
|
||||
{
|
||||
$inputPath='/var/www/html/gestionale_mt/public/storage/tenant'.tenant('id').'/'.$filename;
|
||||
// $outputPath='/var/www/html/gestionale_mt/public/'.tenant('id')."/app/".$filename;
|
||||
$outputPath = $inputPath.'.csv';
|
||||
rename($inputPath,$outputPath);
|
||||
|
||||
//$outputPath="/var/www/html/gestionale_mt/storage/tenant".tenant('id')."/app/".$filename;
|
||||
|
||||
$collection = (new FastExcel)->configureCsv(';')->import($outputPath, function ($line){
|
||||
if($line['VALUTA'])
|
||||
{
|
||||
if($line['DARE']<>'')
|
||||
{
|
||||
$dati=[
|
||||
'mov_data'=>self::dateFormat(0,$line['VALUTA']),
|
||||
// date_format(date_create($movimento->mov_data),'d/m/Y'
|
||||
// 'mov_data'=>date_format(date_create($line['VALUTA']),'Y-m-d'),
|
||||
'mov_fk_categoria'=>1,
|
||||
'mov_descrizione'=>$line['DESCRIZIONE OPERAZIONE'],
|
||||
'mov_importo'=>'-'.trim(str_replace(',','.',(str_replace('.','',$line['DARE'])))),
|
||||
'mov_fk_tags'=>1,
|
||||
'userid'=>1,
|
||||
];
|
||||
}
|
||||
if($line['AVERE']<>'')
|
||||
{
|
||||
$dati=[
|
||||
'mov_data'=>self::dateFormat(0,$line['VALUTA']),
|
||||
//'mov_data'=>date_format(date_create($line['VALUTA']),'Y-m-d'),
|
||||
'mov_fk_categoria'=>1,
|
||||
'mov_descrizione'=>$line['DESCRIZIONE OPERAZIONE'],
|
||||
'mov_importo'=>trim(str_replace(',','.',(str_replace('.','',$line['AVERE'])))),
|
||||
'mov_fk_tags'=>1,
|
||||
'userid'=>1,
|
||||
];
|
||||
}
|
||||
self::insEntrata($dati);
|
||||
}
|
||||
});
|
||||
unlink($outputPath);
|
||||
}
|
||||
|
||||
// Test filtri categoria
|
||||
public function mapFieldByName($name)
|
||||
{
|
||||
// definisce lo schema per rigenerare il file CSV secondo standard di importazione
|
||||
$fields = Illuminate\Support\Facade\Schema::getColumnListing('movimentis')->mapFieldByName;
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
echo $column . "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
private static function dateFormat($type,$string)
|
||||
{
|
||||
// $string=(string)$string;
|
||||
if($type)
|
||||
{
|
||||
// $string=$string->format('Y-m-d');
|
||||
list($year,$month,$day) = explode('-',$string);
|
||||
return $day.'/'.$month.'/'.$year;
|
||||
} else {
|
||||
// $string=$string->format('d/m/Y');
|
||||
list($day,$month,$year) =explode('/',$string);
|
||||
return $year.'-'.$month.'-'.$day;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user