v2 first commit
This commit is contained in:
@@ -2,19 +2,18 @@
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Events\InvoiceCreated;
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Income\InvoiceCreated;
|
||||
use App\Events\Income\InvoiceCreating;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoiceTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Incomes;
|
||||
use App\Traits\Uploads;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class CreateInvoice
|
||||
class CreateInvoice extends Job
|
||||
{
|
||||
use Currencies, DateTime, Dispatchable, Incomes, Uploads;
|
||||
use Currencies, DateTime, Incomes;
|
||||
|
||||
protected $request;
|
||||
|
||||
@@ -25,7 +24,7 @@ class CreateInvoice
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +34,13 @@ class CreateInvoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$invoice = Invoice::create($this->request->input());
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new InvoiceCreating($this->request));
|
||||
|
||||
$invoice = Invoice::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
@@ -51,9 +56,9 @@ class CreateInvoice
|
||||
$discount_total = 0;
|
||||
$discount = $this->request['discount'];
|
||||
|
||||
if ($this->request['item']) {
|
||||
foreach ($this->request['item'] as $item) {
|
||||
$invoice_item = dispatch(new CreateInvoiceItem($item, $invoice, $discount));
|
||||
if ($this->request['items']) {
|
||||
foreach ($this->request['items'] as $item) {
|
||||
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $invoice, $discount));
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $invoice_item->tax;
|
||||
@@ -88,27 +93,14 @@ class CreateInvoice
|
||||
|
||||
$this->request['amount'] = money($amount, $this->request['currency_code'])->getAmount();
|
||||
|
||||
$invoice->update($this->request->input());
|
||||
$invoice->update($this->request->all());
|
||||
|
||||
// Add invoice totals
|
||||
$this->addTotals($invoice, $this->request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||
|
||||
// Add invoice history
|
||||
InvoiceHistory::create([
|
||||
'company_id' => session('company_id'),
|
||||
'invoice_id' => $invoice->id,
|
||||
'status_code' => 'draft',
|
||||
'notify' => 0,
|
||||
'description' => trans('messages.success.added', ['type' => $invoice->invoice_number]),
|
||||
]);
|
||||
|
||||
// Update next invoice number
|
||||
$this->increaseNextInvoiceNumber();
|
||||
|
||||
// Recurring
|
||||
$invoice->createRecurring();
|
||||
|
||||
// Fire the event to make it extensible
|
||||
event(new InvoiceCreated($invoice));
|
||||
|
||||
return $invoice;
|
||||
|
||||
49
app/Jobs/Income/CreateInvoiceHistory.php
Normal file
49
app/Jobs/Income/CreateInvoiceHistory.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
|
||||
class CreateInvoiceHistory extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
protected $notify;
|
||||
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
* @param $notify
|
||||
* @param $description
|
||||
*/
|
||||
public function __construct($invoice, $notify = 0, $description = null)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->notify = $notify;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return InvoiceHistory
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$description = $this->description ?: trans_choice('general.payments', 1);
|
||||
|
||||
$invoice_history = InvoiceHistory::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'status_code' => $this->invoice->invoice_status_code,
|
||||
'notify' => $this->notify,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
return $invoice_history;
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,14 @@
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Models\Common\Item;
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Income\InvoiceItem;
|
||||
use App\Models\Income\InvoiceItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Notifications\Common\Item as ItemNotification;
|
||||
use App\Notifications\Common\ItemReminder as ItemReminderNotification;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateInvoiceItem
|
||||
class CreateInvoiceItem extends Job
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
protected $data;
|
||||
|
||||
protected $invoice;
|
||||
@@ -41,8 +37,6 @@ class CreateInvoiceItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_sku = '';
|
||||
|
||||
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||
|
||||
@@ -53,54 +47,14 @@ class CreateInvoiceItem
|
||||
$item_discount_amount = $item_amount - ($item_amount * ($this->discount / 100));
|
||||
}
|
||||
|
||||
if (!empty($item_id)) {
|
||||
$item_object = Item::find($item_id);
|
||||
|
||||
$this->data['name'] = $item_object->name;
|
||||
$item_sku = $item_object->sku;
|
||||
|
||||
// Decrease stock (item sold)
|
||||
$item_object->quantity -= (double) $this->data['quantity'];
|
||||
$item_object->save();
|
||||
|
||||
if (setting('general.send_item_reminder')) {
|
||||
$item_stocks = explode(',', setting('general.schedule_item_stocks'));
|
||||
|
||||
foreach ($item_stocks as $item_stock) {
|
||||
if ($item_object->quantity == $item_stock) {
|
||||
foreach ($item_object->company->users as $user) {
|
||||
if (!$user->can('read-notifications')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$user->notify(new ItemReminderNotification($item_object));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Notify users if out of stock
|
||||
if ($item_object->quantity == 0) {
|
||||
foreach ($item_object->company->users as $user) {
|
||||
if (!$user->can('read-notifications')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$user->notify(new ItemNotification($item_object));
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->data['sku'])) {
|
||||
$item_sku = $this->data['sku'];
|
||||
}
|
||||
|
||||
$tax_amount = 0;
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
|
||||
if (!empty($this->data['tax_id'])) {
|
||||
$inclusives = $compounds = $taxes = [];
|
||||
$inclusives = $compounds = $taxes = $fixed_taxes = [];
|
||||
|
||||
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
||||
foreach ((array)$this->data['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
@@ -110,6 +64,20 @@ class CreateInvoiceItem
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
break;
|
||||
case 'fixed':
|
||||
$fixed_taxes[] = $tax;
|
||||
$tax_amount = $tax->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
break;
|
||||
case 'normal':
|
||||
default:
|
||||
$taxes[] = $tax;
|
||||
@@ -132,7 +100,7 @@ class CreateInvoiceItem
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discount_amount + $item_tax_total;
|
||||
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate')/100);
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
|
||||
|
||||
foreach ($inclusives as $inclusive) {
|
||||
$item_tax_total += $tax_amount = $item_base_rate * ($inclusive->rate / 100);
|
||||
@@ -170,8 +138,7 @@ class CreateInvoiceItem
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => str_limit($this->data['name'], 180, ''),
|
||||
'sku' => $item_sku,
|
||||
'name' => Str::limit($this->data['name'], 180, ''),
|
||||
'quantity' => (double) $this->data['quantity'],
|
||||
'price' => (double) $this->data['price'],
|
||||
'tax' => $item_tax_total,
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class CreateInvoicePayment
|
||||
{
|
||||
use Dispatchable;
|
||||
|
||||
protected $request;
|
||||
|
||||
protected $invoice;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
* @param $invoice
|
||||
*/
|
||||
public function __construct($request, $invoice)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return InvoicePayment
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$invoice_payment = InvoicePayment::create($this->request->input());
|
||||
|
||||
$desc_amount = money((float) $invoice_payment->amount, (string) $invoice_payment->currency_code, true)->format();
|
||||
|
||||
$history_data = [
|
||||
'company_id' => $invoice_payment->company_id,
|
||||
'invoice_id' => $invoice_payment->invoice_id,
|
||||
'status_code' => $this->invoice->invoice_status_code,
|
||||
'notify' => '0',
|
||||
'description' => $desc_amount . ' ' . trans_choice('general.payments', 1),
|
||||
];
|
||||
|
||||
InvoiceHistory::create($history_data);
|
||||
|
||||
return $invoice_payment;
|
||||
}
|
||||
}
|
||||
37
app/Jobs/Income/DeleteInvoice.php
Normal file
37
app/Jobs/Income/DeleteInvoice.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Income\Invoice;
|
||||
|
||||
class DeleteInvoice extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
*/
|
||||
public function __construct($invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Invoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->deleteRelationships($this->invoice, [
|
||||
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
|
||||
]);
|
||||
|
||||
$this->invoice->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
36
app/Jobs/Income/DuplicateInvoice.php
Normal file
36
app/Jobs/Income/DuplicateInvoice.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Income\InvoiceCreated;
|
||||
use App\Models\Income\Invoice;
|
||||
|
||||
class DuplicateInvoice extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
*/
|
||||
public function __construct($invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Invoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$clone = $this->invoice->duplicate();
|
||||
|
||||
event(new InvoiceCreated($clone));
|
||||
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,18 @@
|
||||
|
||||
namespace App\Jobs\Income;
|
||||
|
||||
use App\Events\InvoiceUpdated;
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Incomes;
|
||||
use App\Traits\Uploads;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class UpdateInvoice
|
||||
class UpdateInvoice extends Job
|
||||
{
|
||||
use Currencies, DateTime, Dispatchable, Incomes, Uploads;
|
||||
use Currencies, DateTime, Incomes;
|
||||
|
||||
protected $invoice;
|
||||
|
||||
@@ -29,7 +27,7 @@ class UpdateInvoice
|
||||
public function __construct($invoice, $request)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->request = $request;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,6 +37,8 @@ class UpdateInvoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
event(new \App\Events\Income\InvoiceUpdating($this->invoice, $this->request));
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
||||
@@ -53,27 +53,11 @@ class UpdateInvoice
|
||||
$discount_total = 0;
|
||||
$discount = $this->request['discount'];
|
||||
|
||||
if ($this->request['item']) {
|
||||
$items = $this->invoice->items;
|
||||
if ($this->request['items']) {
|
||||
$this->deleteRelationships($this->invoice, ['items', 'item_taxes']);
|
||||
|
||||
if ($items) {
|
||||
foreach ($items as $item) {
|
||||
if (empty($item->item_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item_object = Item::find($item->item_id);
|
||||
|
||||
// Increase stock
|
||||
$item_object->quantity += (double) $item->quantity;
|
||||
$item_object->save();
|
||||
}
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->invoice, 'items');
|
||||
|
||||
foreach ($this->request['item'] as $item) {
|
||||
$invoice_item = dispatch(new CreateInvoiceItem($item, $this->invoice, $discount));
|
||||
foreach ($this->request['items'] as $item) {
|
||||
$invoice_item = dispatch_now(new CreateInvoiceItem($item, $this->invoice, $discount));
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $invoice_item->tax;
|
||||
@@ -127,8 +111,7 @@ class UpdateInvoice
|
||||
// Recurring
|
||||
$this->invoice->updateRecurring();
|
||||
|
||||
// Fire the event to make it extensible
|
||||
event(new InvoiceUpdated($this->invoice));
|
||||
event(new \App\Events\Income\InvoiceUpdated($this->invoice, $this->request));
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user