1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Helpers/Invoice/ProRata.php

132 lines
4.3 KiB
PHP
Raw Normal View History

2021-10-21 03:40:59 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-21 03:40:59 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Invoice;
2021-10-21 04:03:41 +02:00
use App\Models\Invoice;
2021-10-21 03:40:59 +02:00
use App\Models\RecurringInvoice;
2021-10-21 04:03:41 +02:00
use Exception;
2021-10-21 03:40:59 +02:00
use Illuminate\Support\Carbon;
2021-10-21 04:03:41 +02:00
class ProRata
2021-10-21 03:40:59 +02:00
{
2021-10-21 04:03:41 +02:00
/**
2021-10-21 06:08:46 +02:00
* Returns the amount to refund based on
* the time interval and the frequency duration
*
* @param float $amount
* @param Carbon $from_date
* @param Carbon $to_date
* @param int $frequency
* @return float
2021-10-21 04:03:41 +02:00
*/
public function refund(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
2021-10-21 03:40:59 +02:00
{
2021-10-21 06:51:36 +02:00
$days = $from_date->copy()->diffInDays($to_date);
2021-10-21 03:40:59 +02:00
$days_in_frequency = $this->getDaysInFrequency($frequency);
return round((($days / $days_in_frequency) * $amount), 2);
2021-10-21 03:40:59 +02:00
}
2021-10-21 06:08:46 +02:00
/**
* Returns the amount to charge based on
* the time interval and the frequency duration
*
* @param float $amount
* @param Carbon $from_date
* @param Carbon $to_date
* @param int $frequency
* @return float
2021-10-21 06:08:46 +02:00
*/
public function charge(float $amount, Carbon $from_date, Carbon $to_date, int $frequency) :float
{
2021-10-21 06:51:36 +02:00
$days = $from_date->copy()->diffInDays($to_date);
2021-10-21 06:08:46 +02:00
$days_in_frequency = $this->getDaysInFrequency($frequency);
2022-03-29 11:06:35 +02:00
return round((($days / $days_in_frequency) * $amount), 2);
2021-10-21 06:08:46 +02:00
}
2021-10-21 04:03:41 +02:00
/**
* Prepares the line items of an invoice
* to be pro rata refunded.
*
* @param Invoice $invoice
* @param bool $is_credit
* @return array
* @throws Exception
2021-10-21 04:03:41 +02:00
*/
public function refundItems(Invoice $invoice, $is_credit = false) :array
{
if (! $invoice) {
2021-10-21 04:03:41 +02:00
return [];
}
2021-10-21 04:03:41 +02:00
2023-08-02 11:14:28 +02:00
/** @var \App\Models\RecurringInvoice $recurring_invoice **/
2021-10-21 04:03:41 +02:00
$recurring_invoice = RecurringInvoice::find($invoice->recurring_id)->first();
if (! $recurring_invoice) {
2021-10-21 04:03:41 +02:00
throw new \Exception("Invoice isn't attached to a recurring invoice");
}
2021-10-21 04:03:41 +02:00
/* depending on whether we are creating an invoice or a credit*/
$multiplier = $is_credit ? 1 : -1;
$start_date = Carbon::parse($invoice->date);
$line_items = [];
foreach ($invoice->line_items as $item) {
if ($item->product_key != ctrans('texts.refund')) {
2021-10-21 04:03:41 +02:00
$item->quantity = 1;
$item->cost = $this->refund($item->cost * $multiplier, $start_date, now(), $recurring_invoice->frequency_id);
2021-10-21 04:03:41 +02:00
$item->product_key = ctrans('texts.refund');
$item->notes = ctrans('texts.refund').': '.$item->notes;
2021-10-21 04:03:41 +02:00
$line_items[] = $item;
}
}
return $line_items;
}
2021-10-21 03:40:59 +02:00
private function getDaysInFrequency($frequency)
{
switch ($frequency) {
case RecurringInvoice::FREQUENCY_DAILY:
return 1;
case RecurringInvoice::FREQUENCY_WEEKLY:
return 7;
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return 14;
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return now()->diffInDays(now()->addWeeks(4));
case RecurringInvoice::FREQUENCY_MONTHLY:
return now()->diffInDays(now()->addMonthNoOverflow());
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
2023-08-03 06:30:14 +02:00
return now()->diffInDays(now()->addMonthsNoOverflow(2));
2021-10-21 03:40:59 +02:00
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
2023-08-03 06:30:14 +02:00
return now()->diffInDays(now()->addMonthsNoOverflow(3));
2021-10-21 03:40:59 +02:00
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
2023-08-03 06:30:14 +02:00
return now()->diffInDays(now()->addMonthsNoOverflow(4));
2021-10-21 03:40:59 +02:00
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
2023-08-03 06:30:14 +02:00
return now()->diffInDays(now()->addMonthsNoOverflow(6));
2021-10-21 03:40:59 +02:00
case RecurringInvoice::FREQUENCY_ANNUALLY:
return now()->diffInDays(now()->addYear());
case RecurringInvoice::FREQUENCY_TWO_YEARS:
return now()->diffInDays(now()->addYears(2));
case RecurringInvoice::FREQUENCY_THREE_YEARS:
return now()->diffInDays(now()->addYears(3));
default:
return 0;
}
}
}