2019-05-23 07:08:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-23 07:08:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Factory;
|
|
|
|
|
2020-02-26 04:26:07 +01:00
|
|
|
use App\Models\Client;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\RecurringInvoice;
|
2021-09-26 04:13:03 +02:00
|
|
|
use App\Utils\Helpers;
|
2023-01-23 13:45:45 +01:00
|
|
|
use Carbon\Carbon;
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-02-26 04:26:07 +01:00
|
|
|
class RecurringInvoiceToInvoiceFactory
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
public static function create(RecurringInvoice $recurring_invoice, Client $client): Invoice
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
$invoice = new Invoice();
|
|
|
|
$invoice->status_id = Invoice::STATUS_DRAFT;
|
|
|
|
$invoice->discount = $recurring_invoice->discount;
|
|
|
|
$invoice->is_amount_discount = $recurring_invoice->is_amount_discount;
|
|
|
|
$invoice->po_number = $recurring_invoice->po_number;
|
2022-10-12 04:24:05 +02:00
|
|
|
$invoice->footer = $recurring_invoice->footer ? self::tranformObject($recurring_invoice->footer, $client) : null;
|
2024-01-14 05:05:00 +01:00
|
|
|
$invoice->terms = $recurring_invoice->terms ? self::tranformObject($recurring_invoice->terms, $client) : null;
|
2022-10-12 04:24:05 +02:00
|
|
|
$invoice->public_notes = $recurring_invoice->public_notes ? self::tranformObject($recurring_invoice->public_notes, $client) : null;
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoice->private_notes = $recurring_invoice->private_notes;
|
|
|
|
$invoice->is_deleted = $recurring_invoice->is_deleted;
|
2021-09-26 04:13:03 +02:00
|
|
|
$invoice->line_items = self::transformItems($recurring_invoice, $client);
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoice->tax_name1 = $recurring_invoice->tax_name1;
|
|
|
|
$invoice->tax_rate1 = $recurring_invoice->tax_rate1;
|
|
|
|
$invoice->tax_name2 = $recurring_invoice->tax_name2;
|
|
|
|
$invoice->tax_rate2 = $recurring_invoice->tax_rate2;
|
2020-09-17 01:26:23 +02:00
|
|
|
$invoice->tax_name3 = $recurring_invoice->tax_name3;
|
|
|
|
$invoice->tax_rate3 = $recurring_invoice->tax_rate3;
|
2021-07-15 03:31:11 +02:00
|
|
|
$invoice->total_taxes = $recurring_invoice->total_taxes;
|
|
|
|
$invoice->subscription_id = $recurring_invoice->subscription_id;
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoice->custom_value1 = $recurring_invoice->custom_value1;
|
|
|
|
$invoice->custom_value2 = $recurring_invoice->custom_value2;
|
|
|
|
$invoice->custom_value3 = $recurring_invoice->custom_value3;
|
|
|
|
$invoice->custom_value4 = $recurring_invoice->custom_value4;
|
|
|
|
$invoice->amount = $recurring_invoice->amount;
|
2021-09-26 04:13:03 +02:00
|
|
|
$invoice->uses_inclusive_taxes = $recurring_invoice->uses_inclusive_taxes;
|
2023-03-17 02:49:08 +01:00
|
|
|
$invoice->is_proforma = $recurring_invoice->is_proforma;
|
2022-04-14 13:38:29 +02:00
|
|
|
|
|
|
|
$invoice->custom_surcharge1 = $recurring_invoice->custom_surcharge1;
|
|
|
|
$invoice->custom_surcharge2 = $recurring_invoice->custom_surcharge2;
|
|
|
|
$invoice->custom_surcharge3 = $recurring_invoice->custom_surcharge3;
|
|
|
|
$invoice->custom_surcharge4 = $recurring_invoice->custom_surcharge4;
|
|
|
|
$invoice->custom_surcharge_tax1 = $recurring_invoice->custom_surcharge_tax1;
|
|
|
|
$invoice->custom_surcharge_tax2 = $recurring_invoice->custom_surcharge_tax2;
|
|
|
|
$invoice->custom_surcharge_tax3 = $recurring_invoice->custom_surcharge_tax3;
|
|
|
|
$invoice->custom_surcharge_tax4 = $recurring_invoice->custom_surcharge_tax4;
|
|
|
|
|
2021-01-08 02:44:31 +01:00
|
|
|
// $invoice->balance = $recurring_invoice->balance;
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoice->user_id = $recurring_invoice->user_id;
|
2020-09-17 01:26:23 +02:00
|
|
|
$invoice->assigned_user_id = $recurring_invoice->assigned_user_id;
|
2019-12-30 22:59:12 +01:00
|
|
|
$invoice->company_id = $recurring_invoice->company_id;
|
|
|
|
$invoice->recurring_id = $recurring_invoice->id;
|
2020-09-16 01:29:23 +02:00
|
|
|
$invoice->client_id = $client->id;
|
2020-09-17 01:26:23 +02:00
|
|
|
$invoice->auto_bill_enabled = $recurring_invoice->auto_bill_enabled;
|
2021-01-24 07:44:14 +01:00
|
|
|
$invoice->paid_to_date = 0;
|
2021-05-02 05:14:28 +02:00
|
|
|
$invoice->design_id = $recurring_invoice->design_id;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $invoice;
|
|
|
|
}
|
2021-09-26 04:13:03 +02:00
|
|
|
|
|
|
|
private static function transformItems($recurring_invoice, $client)
|
|
|
|
{
|
2023-01-23 13:45:45 +01:00
|
|
|
$currentDateTime = null;
|
2021-09-26 04:13:03 +02:00
|
|
|
$line_items = $recurring_invoice->line_items;
|
|
|
|
|
2023-01-23 13:45:45 +01:00
|
|
|
if (isset($recurring_invoice->next_send_date)) {
|
|
|
|
$currentDateTime = Carbon::parse($recurring_invoice->next_send_date)->timezone($client->timezone()->name);
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($line_items as $key => $item) {
|
|
|
|
if (property_exists($line_items[$key], 'notes')) {
|
2023-01-23 13:45:45 +01:00
|
|
|
$line_items[$key]->notes = Helpers::processReservedKeywords($item->notes, $client, $currentDateTime);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-26 04:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $line_items;
|
|
|
|
}
|
2021-09-26 05:25:45 +02:00
|
|
|
|
|
|
|
private static function tranformObject($object, $client)
|
|
|
|
{
|
|
|
|
return Helpers::processReservedKeywords($object, $client);
|
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|