1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00

- Use user from $billing_subscription to create client

- Disable generating the cache for temporary state
- Generate the blank invoice based on the product
This commit is contained in:
Benjamin Beganović 2021-03-17 12:06:58 +01:00
parent eb2cfde303
commit 81f5808bf6
4 changed files with 34 additions and 50 deletions

View File

@ -4,12 +4,9 @@ namespace App\Http\Livewire;
use App\Factory\ClientFactory;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Models\User;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class BillingPortalPurchase extends Component
@ -75,7 +72,7 @@ class BillingPortalPurchase extends Component
protected function createBlankClient()
{
$company = $this->billing_subscription->company;
$user = User::first(); // TODO: What should be a value of $user?
$user = $this->billing_subscription->user;
$client_repo = new ClientRepository(new ClientContactRepository());
@ -91,7 +88,7 @@ class BillingPortalPurchase extends Component
protected function getPaymentMethods(ClientContact $contact): self
{
Cache::put($this->hash, ['email' => $this->email ?? $this->contact->email, 'url' => url()->current()]);
// Cache::put($this->hash, ['email' => $this->email ?? $this->contact->email, 'url' => url()->current()]);
$this->steps['fetched_payment_methods'] = true;
@ -116,38 +113,23 @@ class BillingPortalPurchase extends Component
public function handleBeforePaymentEvents()
{
$company = $this->billing_subscription->company;
$user = User::first(); // TODO: What should be a value of $user?
$invoice = [
$data = [
'client_id' => $this->contact->client->id,
'line_items' => [[
'quantity' => 1,
'cost' => 10,
'product_key' => 'example',
'notes' => 'example',
'discount' => 0,
'is_amount_discount' => true,
'tax_rate1' => 0,
'tax_rate2' => 0,
'tax_rate3' => 0,
'tax_name1' => '',
'tax_name2' => '',
'tax_name3' => '',
'sort_id' => 0,
'line_total' => 1,
'custom_value1' => 'example',
'custom_value2' => 'example',
'custom_value3' => 'example',
'custom_value4' => 'example',
'type_id' => 1,
'date' => '',
'date' => now()->format('Y-m-d'),
'invitations' => [[
'key' => '',
'client_contact_id' => $this->contact->hashed_id,
]],
'user_input_promo_code' => '', // Field to input the promo code,
'quantity' => 1, // Option to increase quantity
];
// TODO: Only for testing.
$this->invoice = Invoice::where('status_id', Invoice::STATUS_SENT)->first();
// $this->invoice = (new \App\Repositories\InvoiceRepository)->save($invoice, InvoiceFactory::create($company->id, $user->id));
$this->invoice = $this->billing_subscription
->service()
->createInvoice($data)
->service()
->markSent()
->save();
$this->emit('beforePaymentEventsCompleted');
}

View File

@ -73,5 +73,4 @@ class BillingSubscription extends BaseModel
{
return $this->belongsTo(Product::class);
}
}

View File

@ -13,13 +13,14 @@ namespace App\Services\BillingSubscription;
use App\DataMapper\InvoiceItem;
use App\Factory\InvoiceFactory;
use App\Models\BillingSubscription;
use App\Models\ClientSubscription;
use App\Models\Product;
use App\Repositories\InvoiceRepository;
class BillingSubscriptionService
{
/** @var BillingSubscription */
private $billing_subscription;
public function __construct(BillingSubscription $billing_subscription)
@ -27,9 +28,8 @@ class BillingSubscriptionService
$this->billing_subscription = $billing_subscription;
}
public function createInvoice($data)
public function createInvoice($data): ?\App\Models\Invoice
{
$invoice_repo = new InvoiceRepository();
// $data = [
@ -38,31 +38,31 @@ class BillingSubscriptionService
// 'invitations' => [
// 'client_contact_id' => hashed_id
// ],
// 'line_items' => [],
// 'line_items' => [],
// ];
$data['line_items'] = $this->createLineItems($data['quantity']);
$invoice = $invoice_repo->save($data, InvoiceFactory::create($billing_subscription->company_id, $billing_subscription->user_id));
/*
If trial_enabled -> return early
-- what we need to know that we don't already
-- Has a promo code been entered, and does it match
-- Is this a recurring subscription
--
--
1. Is this a recurring product?
2. What is the quantity? ie is this a multi seat product ( does this mean we need this value stored in the client sub?)
*/
return $invoice;
return $invoice_repo->save($data, InvoiceFactory::create($this->billing_subscription->company_id, $this->billing_subscription->user_id));
}
private function createLineItems($quantity)
private function createLineItems($quantity): array
{
$line_items = [];
$product = $this->billing_subscription->product;
$item = new InvoiceItem;
@ -88,7 +88,7 @@ class BillingSubscriptionService
public function createClientSubscription($payment_hash, $recurring_invoice_id = null)
{
//create the client sub record
//?trial enabled?
$cs = new ClientSubscription();
$cs->subscription_id = $this->billing_subscription->id;

View File

@ -31,10 +31,13 @@
<div>
@yield('gateway_content')
</div>
<span class="block mx-4 mb-4 text-xs inline-flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-green-600"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
<span class="ml-1">Secure 256-bit encryption</span>
</span>
@if(Request::isSecure())
<span class="block mx-4 mb-4 text-xs inline-flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-green-600"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
<span class="ml-1">Secure 256-bit encryption</span>
</span>
@endif
</div>
</div>
@endsection