mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 13:54:34 -04:00
Set exchange rates on invoices
This commit is contained in:
parent
646130a104
commit
d7bf927549
@ -92,6 +92,7 @@ class BaseController extends Controller
|
|||||||
'company.quotes.invitations.company',
|
'company.quotes.invitations.company',
|
||||||
'company.quotes.documents',
|
'company.quotes.documents',
|
||||||
'company.tasks.documents',
|
'company.tasks.documents',
|
||||||
|
'company.subcsriptions',
|
||||||
'company.tax_rates',
|
'company.tax_rates',
|
||||||
'company.tokens_hashed',
|
'company.tokens_hashed',
|
||||||
'company.vendors.contacts.company',
|
'company.vendors.contacts.company',
|
||||||
@ -340,6 +341,13 @@ class BaseController extends Controller
|
|||||||
if(!$user->isAdmin())
|
if(!$user->isAdmin())
|
||||||
$query->where('activities.user_id', $user->id);
|
$query->where('activities.user_id', $user->id);
|
||||||
|
|
||||||
|
},
|
||||||
|
'company.subscriptions'=> function ($query) use($user) {
|
||||||
|
$query->where('updated_at', '>=', $updated_at);
|
||||||
|
|
||||||
|
if(!$user->isAdmin())
|
||||||
|
$query->where('subscriptions.user_id', $user->id);
|
||||||
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
@ -182,7 +182,6 @@ class PaymentRepository extends BaseRepository {
|
|||||||
$company_currency = $client->company->settings->currency_id;
|
$company_currency = $client->company->settings->currency_id;
|
||||||
|
|
||||||
if ($company_currency != $client_currency) {
|
if ($company_currency != $client_currency) {
|
||||||
$currency = $client->currency();
|
|
||||||
|
|
||||||
$exchange_rate = new CurrencyApi();
|
$exchange_rate = new CurrencyApi();
|
||||||
|
|
||||||
|
@ -24,18 +24,6 @@ class RecurringInvoiceRepository extends BaseRepository
|
|||||||
{
|
{
|
||||||
|
|
||||||
$invoice = $this->alternativeSave($data, $invoice);
|
$invoice = $this->alternativeSave($data, $invoice);
|
||||||
// $invoice->fill($data);
|
|
||||||
|
|
||||||
// $invoice->save();
|
|
||||||
|
|
||||||
// $invoice_calc = new InvoiceSum($invoice);
|
|
||||||
|
|
||||||
// $invoice->service()
|
|
||||||
// ->applyNumber()
|
|
||||||
// ->createInvitations()
|
|
||||||
// ->save();
|
|
||||||
|
|
||||||
// $invoice = $invoice_calc->build()->getRecurringInvoice();
|
|
||||||
|
|
||||||
return $invoice;
|
return $invoice;
|
||||||
}
|
}
|
||||||
|
@ -93,17 +93,20 @@ class SubscriptionRepository extends BaseRepository
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateLineItems($subscription)
|
public function generateLineItems($subscription, $is_recurring = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
$line_items = [];
|
$line_items = [];
|
||||||
|
|
||||||
foreach($subscription->service()->products() as $product)
|
if(!$is_recurring)
|
||||||
{
|
{
|
||||||
$line_items[] = (array)$this->makeLineItem($product);
|
foreach($subscription->service()->products() as $product)
|
||||||
|
{
|
||||||
|
$line_items[] = (array)$this->makeLineItem($product);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($subscription->service()->recurring_products() as $product)
|
foreach($subscription->service()->recurringProducts() as $product)
|
||||||
{
|
{
|
||||||
$line_items[] = (array)$this->makeLineItem($product);
|
$line_items[] = (array)$this->makeLineItem($product);
|
||||||
}
|
}
|
||||||
|
@ -63,11 +63,22 @@ class InvoiceService
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the exchange rate on the invoice if the client currency
|
||||||
|
* is different to the company currency.
|
||||||
|
*/
|
||||||
public function setExchangeRate()
|
public function setExchangeRate()
|
||||||
{
|
{
|
||||||
$exchange_rate = new CurrencyApi();
|
|
||||||
|
|
||||||
// $payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
|
$client_currency = $this->invoice->client->getSetting('currency_id');
|
||||||
|
$company_currency = $this->invoice->company->settings->currency_id;
|
||||||
|
|
||||||
|
if ($company_currency != $client_currency) {
|
||||||
|
|
||||||
|
$exchange_rate = new CurrencyApi();
|
||||||
|
|
||||||
|
$this->invoice->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, now());
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -140,6 +151,8 @@ class InvoiceService
|
|||||||
{
|
{
|
||||||
$this->invoice = (new MarkSent($this->invoice->client, $this->invoice))->run();
|
$this->invoice = (new MarkSent($this->invoice->client, $this->invoice))->run();
|
||||||
|
|
||||||
|
$this->setExchangeRate();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ namespace App\Services\Subscription;
|
|||||||
use App\DataMapper\InvoiceItem;
|
use App\DataMapper\InvoiceItem;
|
||||||
use App\Factory\InvoiceFactory;
|
use App\Factory\InvoiceFactory;
|
||||||
use App\Factory\InvoiceToRecurringInvoiceFactory;
|
use App\Factory\InvoiceToRecurringInvoiceFactory;
|
||||||
|
use App\Factory\RecurringInvoiceFactory;
|
||||||
use App\Jobs\Util\SystemLogger;
|
use App\Jobs\Util\SystemLogger;
|
||||||
use App\Models\ClientContact;
|
use App\Models\ClientContact;
|
||||||
use App\Models\ClientSubscription;
|
use App\Models\ClientSubscription;
|
||||||
@ -23,6 +24,7 @@ use App\Models\Product;
|
|||||||
use App\Models\Subscription;
|
use App\Models\Subscription;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
use App\Repositories\InvoiceRepository;
|
use App\Repositories\InvoiceRepository;
|
||||||
|
use App\Repositories\RecurringInvoiceRepository;
|
||||||
use App\Repositories\SubscriptionRepository;
|
use App\Repositories\SubscriptionRepository;
|
||||||
use App\Utils\Traits\CleanLineItems;
|
use App\Utils\Traits\CleanLineItems;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
@ -69,18 +71,23 @@ class SubscriptionService
|
|||||||
if(!$this->subscription->trial_enabled)
|
if(!$this->subscription->trial_enabled)
|
||||||
return new \Exception("Trials are disabled for this product");
|
return new \Exception("Trials are disabled for this product");
|
||||||
|
|
||||||
// $contact = ClientContact::with('client')->find($data['contact_id']);
|
//create recurring invoice with start date = trial_duration + 1 day
|
||||||
|
$recurring_invoice_repo = new RecurringInvoiceRepository();
|
||||||
|
$subscription_repo = new SubscriptionRepository();
|
||||||
|
|
||||||
// $cs = new ClientSubscription();
|
$invoice = RecurringInvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
|
||||||
// $cs->subscription_id = $this->subscription->id;
|
$invoice->line_items = $subscription_repo->generateLineItems($this->subscription, true);
|
||||||
// $cs->company_id = $this->subscription->company_id;
|
$invoice->subscription_id = $this->subscription->id;
|
||||||
// $cs->trial_started = time();
|
$invoice->frequency_id = $this->subscription->frequency_id;
|
||||||
// $cs->trial_ends = time() + $this->subscription->trial_duration;
|
$invoice->date = now()->addSeconds($this->subscription->trial_duration)->addDays(1);
|
||||||
// $cs->quantity = $data['quantity'];
|
|
||||||
// $cs->client_id = $contact->client->id;
|
|
||||||
// $cs->save();
|
|
||||||
|
|
||||||
// $this->client_subscription = $cs;
|
if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0)
|
||||||
|
{
|
||||||
|
$invoice->discount = $this->subscription->promo_discount;
|
||||||
|
$invoice->is_amount_discount = $this->subscription->is_amount_discount;
|
||||||
|
}
|
||||||
|
|
||||||
|
$recurring_invoice = $recurring_invoice_repo->save($data, $invoice);
|
||||||
|
|
||||||
//execute any webhooks
|
//execute any webhooks
|
||||||
$this->triggerWebhook();
|
$this->triggerWebhook();
|
||||||
@ -198,7 +205,7 @@ class SubscriptionService
|
|||||||
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->product_ids)))->get();
|
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->product_ids)))->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function recurring_products()
|
public function recurringProducts()
|
||||||
{
|
{
|
||||||
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->recurring_product_ids)))->get();
|
return Product::whereIn('id', $this->transformKeys(explode(",", $this->subscription->recurring_product_ids)))->get();
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ class CompanyTransformer extends EntityTransformer
|
|||||||
'system_logs',
|
'system_logs',
|
||||||
'expense_categories',
|
'expense_categories',
|
||||||
'task_statuses',
|
'task_statuses',
|
||||||
'billing_subscriptions',
|
'subscriptions',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -366,6 +366,6 @@ class CompanyTransformer extends EntityTransformer
|
|||||||
{
|
{
|
||||||
$transformer = new SubscriptionTransformer($this->serializer);
|
$transformer = new SubscriptionTransformer($this->serializer);
|
||||||
|
|
||||||
return $this->includeCollection($company->billing_subscriptions, $transformer, Subscription::class);
|
return $this->includeCollection($company->subscriptions, $transformer, Subscription::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,8 @@ class SubscriptionTransformer extends EntityTransformer
|
|||||||
'id' => $this->encodePrimaryKey($subscription->id),
|
'id' => $this->encodePrimaryKey($subscription->id),
|
||||||
'user_id' => $this->encodePrimaryKey($subscription->user_id),
|
'user_id' => $this->encodePrimaryKey($subscription->user_id),
|
||||||
'group_id' => $this->encodePrimaryKey($subscription->group_id),
|
'group_id' => $this->encodePrimaryKey($subscription->group_id),
|
||||||
'product_ids' => $subscription->product_ids,
|
'product_ids' => (string)$subscription->product_ids,
|
||||||
'recurring_product_ids' => $subscription->recurring_product_ids,
|
'recurring_product_ids' => (string)$subscription->recurring_product_ids,
|
||||||
'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id),
|
'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id),
|
||||||
'company_id' => $this->encodePrimaryKey($subscription->company_id),
|
'company_id' => $this->encodePrimaryKey($subscription->company_id),
|
||||||
'price' => (float) $subscription->price,
|
'price' => (float) $subscription->price,
|
||||||
|
44
tests/Unit/RecurringDateTest.php
Normal file
44
tests/Unit/RecurringDateTest.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://opensource.org/licenses/AAL
|
||||||
|
*/
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
class RecurringDateTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions;
|
||||||
|
use MockAccountData;
|
||||||
|
|
||||||
|
public function setUp() :void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
//$this->makeTestData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNextDay()
|
||||||
|
{
|
||||||
|
$trial = 60*60*24;
|
||||||
|
|
||||||
|
$now = Carbon::parse('2021-12-01');
|
||||||
|
|
||||||
|
$trial_ends = $now->addSeconds($trial)->addDays(1);
|
||||||
|
|
||||||
|
$this->assertequals($trial_ends->format('Y-m-d'), '2021-12-03');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user