openstamanager/modules/pagamenti/src/Pagamento.php

168 lines
5.2 KiB
PHP
Raw Normal View History

2019-03-08 16:59:55 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-03-08 16:59:55 +01:00
namespace Modules\Pagamenti;
2022-01-31 11:23:32 +01:00
use Carbon\Carbon;
2023-08-04 14:54:28 +02:00
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
use Modules\Fatture\Fattura;
2024-03-20 11:13:28 +01:00
use Traits\RecordTrait;
2024-03-22 15:52:24 +01:00
2019-03-08 16:59:55 +01:00
class Pagamento extends Model
{
use SimpleModelTrait;
2024-03-20 11:13:28 +01:00
use RecordTrait;
2019-03-08 16:59:55 +01:00
protected $table = 'co_pagamenti';
2024-03-20 11:13:28 +01:00
protected static $translated_fields = [
'name',
];
2024-03-22 15:52:24 +01:00
2024-03-19 18:18:11 +01:00
public static function build($codice = null)
{
$model = new static();
$model->codice_modalita_pagamento_fe = $codice;
$model->save();
return $model;
}
2019-03-08 16:59:55 +01:00
public function fatture()
{
return $this->hasMany(Fattura::class, 'idpagamento');
}
public function rate()
{
2024-02-08 13:10:46 +01:00
return $this->hasMany(Pagamento::class, 'id');
2019-03-08 16:59:55 +01:00
}
public function calcola($importo, $data, $id_anagrafica)
2019-03-08 16:59:55 +01:00
{
2024-04-02 15:07:03 +02:00
$rate = PagamentoLang::where('name', '=', $this->getTranslation('name'))->get()->sortBy('num_giorni')->pluck('id')->toArray();
2019-03-08 16:59:55 +01:00
$number = count($rate);
$totale = 0.0;
$results = [];
2019-05-10 18:02:27 +02:00
$count = 0;
2019-03-08 16:59:55 +01:00
foreach ($rate as $key => $rata) {
2022-01-31 11:23:32 +01:00
$date = new Carbon($data);
2024-04-02 15:07:03 +02:00
$rata = Pagamento::find($rata);
2019-03-08 16:59:55 +01:00
// X giorni esatti
2024-04-02 15:07:03 +02:00
if ($rata->giorno == 0) {
2020-01-10 19:15:36 +01:00
// Offset della rata
2024-04-02 15:07:03 +02:00
if ($rata->num_giorni % 30 == 0) {
$date->addMonthsNoOverflow(round($rata->num_giorni / 30));
2022-01-31 11:23:32 +01:00
} else {
2024-04-08 15:44:33 +02:00
$date->addDay();
2022-01-31 11:23:32 +01:00
}
2019-03-08 16:59:55 +01:00
}
// Ultimo del mese
2024-04-02 15:07:03 +02:00
elseif ($rata->giorno < 0) {
2022-01-31 11:23:32 +01:00
// Offset della rata
2024-04-02 15:07:03 +02:00
if ($rata->num_giorni % 30 == 0) {
$date->addMonthsNoOverflow(round($rata->num_giorni / 30));
2022-01-31 11:23:32 +01:00
} else {
2024-04-08 15:44:33 +02:00
$date->addDay();
2022-01-31 11:23:32 +01:00
}
2021-03-30 15:22:52 +02:00
$date->modify('last day of this month');
2019-03-08 16:59:55 +01:00
2020-01-10 19:15:36 +01:00
// Opzione ultimo del mese più X giorni
2024-04-02 15:07:03 +02:00
$giorni = -$rata->giorno - 1;
2019-03-08 16:59:55 +01:00
if ($giorni > 0) {
2024-01-15 15:30:45 +01:00
$date->modify('+'.$giorni.' day');
2019-03-08 16:59:55 +01:00
} else {
$date->modify('last day of this month');
}
}
// Giorno preciso del mese
else {
2020-01-10 19:15:36 +01:00
// Offset della rata
2024-04-02 15:07:03 +02:00
if ($rata->num_giorni % 30 == 0) {
$date->addMonthsNoOverflow(round($rata->num_giorni / 30));
2022-01-31 11:23:32 +01:00
} else {
2024-04-08 15:44:33 +02:00
$date->addDay();
2022-01-31 11:23:32 +01:00
}
2023-08-04 14:54:28 +02:00
2020-01-10 19:15:36 +01:00
// Individuazione giorno effettivo (se il giorno indicato è eccessivamente grande, viene preso il massimo possibile)
$date->modify('last day of this month');
$last_day = $date->format('d');
2024-04-02 15:07:03 +02:00
$day = $rata->giorno > $last_day ? $last_day : $rata->giorno;
2020-01-10 19:15:36 +01:00
// Correzione data
$date->setDate($date->format('Y'), $date->format('m'), $day);
2019-03-08 16:59:55 +01:00
}
// Posticipo la scadenza in base alle regole pagamenti dell'anagrafica
$regola_pagamento = database()->selectOne('an_pagamenti_anagrafiche', '*', ['idanagrafica' => $id_anagrafica, 'mese' => $date->format('m')]);
if (!empty($regola_pagamento)) {
$date->modify('last day of this month');
2024-04-08 15:44:33 +02:00
$date->addDay();
}
// Conversione della data in stringa standard
2020-01-10 19:15:36 +01:00
$scadenza = $date->format('Y-m-d');
2019-03-08 16:59:55 +01:00
// All'ultimo ciclo imposto come cifra da pagare il totale della fattura meno gli importi già inseriti in scadenziario per evitare di inserire cifre arrotondate "male"
2019-05-10 18:02:27 +02:00
if ($count + 1 == $number) {
2019-03-08 16:59:55 +01:00
$da_pagare = sum($importo, -$totale, 2);
}
// Totale da pagare (totale x percentuale di pagamento nei casi pagamenti multipli)
else {
2024-04-02 15:07:03 +02:00
$da_pagare = sum($importo / 100 * $rata->prc, 0, 2);
2019-03-08 16:59:55 +01:00
}
$totale = sum($da_pagare, $totale, 2);
$results[] = [
'scadenza' => $scadenza,
'importo' => $da_pagare,
];
2019-05-10 18:02:27 +02:00
2019-05-16 04:52:16 +02:00
++$count;
2019-03-08 16:59:55 +01:00
}
return $results;
}
/**
* @return bool
*/
public function isRiBa()
{
return $this->codice_modalita_pagamento_fe == 'MP12';
}
2024-02-08 13:10:46 +01:00
2024-03-20 11:13:28 +01:00
public function getModuleAttribute()
2024-02-08 13:10:46 +01:00
{
2024-03-20 11:13:28 +01:00
return 'Pagamenti';
2024-02-08 13:10:46 +01:00
}
2024-03-22 15:52:24 +01:00
public static function getTranslatedFields()
{
2024-03-20 11:13:28 +01:00
return self::$translated_fields;
}
2023-08-04 14:54:28 +02:00
}