Merge Invoice and Bill into Document
This commit is contained in:
@@ -4,15 +4,14 @@ namespace App\Jobs\Banking;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Jobs\Banking\CreateTransaction;
|
||||
use App\Jobs\Purchase\CreateBillHistory;
|
||||
use App\Jobs\Sale\CreateInvoiceHistory;
|
||||
use App\Jobs\Document\CreateDocumentHistory;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Models\Document\Document;
|
||||
use App\Traits\Currencies;
|
||||
use Date;
|
||||
|
||||
class CreateDocumentTransaction extends Job
|
||||
class CreateBankingDocumentTransaction extends Job
|
||||
{
|
||||
use Currencies;
|
||||
|
||||
@@ -74,7 +73,7 @@ class CreateDocumentTransaction extends Job
|
||||
|
||||
$this->request['company_id'] = session('company_id');
|
||||
$this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;
|
||||
$this->request['type'] = ($this->model instanceof Invoice) ? 'income' : 'expense';
|
||||
$this->request['type'] = ($this->model->type === Document::INVOICE_TYPE) ? 'income' : 'expense';
|
||||
$this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d');
|
||||
$this->request['currency_rate'] = config('money.' . $this->request['currency_code'] . '.rate');
|
||||
$this->request['account_id'] = isset($this->request['account_id']) ? $this->request['account_id'] : setting('default.account');
|
||||
@@ -132,10 +131,6 @@ class CreateDocumentTransaction extends Job
|
||||
{
|
||||
$history_desc = money((double) $this->transaction->amount, (string) $this->transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1);
|
||||
|
||||
if ($this->model instanceof Invoice) {
|
||||
$this->dispatch(new CreateInvoiceHistory($this->model, 0, $history_desc));
|
||||
} else {
|
||||
$this->dispatch(new CreateBillHistory($this->model, 0, $history_desc));
|
||||
}
|
||||
$this->dispatch(new CreateDocumentHistory($this->model, 0, $history_desc));
|
||||
}
|
||||
}
|
||||
40
app/Jobs/Document/CancelDocument.php
Normal file
40
app/Jobs/Document/CancelDocument.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Document\Document;
|
||||
|
||||
class CancelDocument extends Job
|
||||
{
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->deleteRelationships($this->document, [
|
||||
'transactions', 'recurring'
|
||||
]);
|
||||
|
||||
$this->document->status = 'cancelled';
|
||||
$this->document->save();
|
||||
});
|
||||
|
||||
return $this->document;
|
||||
}
|
||||
}
|
||||
62
app/Jobs/Document/CreateDocument.php
Normal file
62
app/Jobs/Document/CreateDocument.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\DocumentCreated;
|
||||
use App\Events\Document\DocumentCreating;
|
||||
use App\Jobs\Document\CreateDocumentItemsAndTotals;
|
||||
use App\Models\Document\Document;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateDocument extends Job
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new DocumentCreating($this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->document = Document::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), Str::plural($this->document->type));
|
||||
|
||||
$this->document->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request));
|
||||
|
||||
$this->document->update($this->request->all());
|
||||
|
||||
$this->document->createRecurring();
|
||||
});
|
||||
|
||||
event(new DocumentCreated($this->document));
|
||||
|
||||
return $this->document;
|
||||
}
|
||||
}
|
||||
50
app/Jobs/Document/CreateDocumentHistory.php
Normal file
50
app/Jobs/Document/CreateDocumentHistory.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Document\DocumentHistory;
|
||||
|
||||
class CreateDocumentHistory extends Job
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $notify;
|
||||
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
* @param $notify
|
||||
* @param $description
|
||||
*/
|
||||
public function __construct($document, $notify = 0, $description = null)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->notify = $notify;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return DocumentHistory
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$description = $this->description ?: trans_choice('general.payments', 1);
|
||||
|
||||
$document_history = DocumentHistory::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'status' => $this->document->status,
|
||||
'notify' => $this->notify,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
return $document_history;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\BillItem;
|
||||
use App\Models\Purchase\BillItemTax;
|
||||
use App\Models\Document\DocumentItem;
|
||||
use App\Models\Document\DocumentItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateBillItem extends Job
|
||||
class CreateDocumentItem extends Job
|
||||
{
|
||||
protected $bill;
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
* @param $document
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($bill, $request)
|
||||
public function __construct($document, $request)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->document = $document;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return BillItem
|
||||
* @return DocumentItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
||||
$precision = config('money.' . $this->bill->currency_code . '.precision');
|
||||
$precision = config('money.' . $this->document->currency_code . '.precision');
|
||||
|
||||
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
|
||||
|
||||
@@ -59,10 +59,10 @@ class CreateBillItem extends Job
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
|
||||
if (!empty($this->request['tax_id'])) {
|
||||
if (!empty($this->request['tax_ids'])) {
|
||||
$inclusives = $compounds = [];
|
||||
|
||||
foreach ((array) $this->request['tax_id'] as $tax_id) {
|
||||
foreach ((array) $this->request['tax_ids'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
@@ -78,8 +78,9 @@ class CreateBillItem extends Job
|
||||
$tax_amount = $tax->rate * (double) $this->request['quantity'];
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
@@ -92,8 +93,9 @@ class CreateBillItem extends Job
|
||||
$tax_amount = 0 - $item_discounted_amount * ($tax->rate / 100);
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
@@ -106,8 +108,9 @@ class CreateBillItem extends Job
|
||||
$tax_amount = $item_discounted_amount * ($tax->rate / 100);
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'tax_id' => $tax_id,
|
||||
'name' => $tax->name,
|
||||
'amount' => $tax_amount,
|
||||
@@ -128,8 +131,9 @@ class CreateBillItem extends Job
|
||||
$tax_amount = $item_base_rate * ($inclusive->rate / 100);
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'tax_id' => $inclusive->id,
|
||||
'name' => $inclusive->name,
|
||||
'amount' => $tax_amount,
|
||||
@@ -146,8 +150,9 @@ class CreateBillItem extends Job
|
||||
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'tax_id' => $compound->id,
|
||||
'name' => $compound->name,
|
||||
'amount' => $tax_amount,
|
||||
@@ -158,11 +163,13 @@ class CreateBillItem extends Job
|
||||
}
|
||||
}
|
||||
|
||||
$bill_item = BillItem::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
$document_item = DocumentItem::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => Str::limit($this->request['name'], 180, ''),
|
||||
'description' => $this->request['description'],
|
||||
'quantity' => (double) $this->request['quantity'],
|
||||
'price' => round($this->request['price'], $precision),
|
||||
'tax' => round($item_tax_total, $precision),
|
||||
@@ -170,23 +177,23 @@ class CreateBillItem extends Job
|
||||
'total' => round($item_amount, $precision),
|
||||
]);
|
||||
|
||||
$bill_item->item_taxes = false;
|
||||
$bill_item->inclusives = false;
|
||||
$bill_item->compounds = false;
|
||||
$document_item->item_taxes = false;
|
||||
$document_item->inclusives = false;
|
||||
$document_item->compounds = false;
|
||||
|
||||
if (!empty($item_taxes)) {
|
||||
$bill_item->item_taxes = $item_taxes;
|
||||
$bill_item->inclusives = $inclusives;
|
||||
$bill_item->compounds = $compounds;
|
||||
$document_item->item_taxes = $item_taxes;
|
||||
$document_item->inclusives = $inclusives;
|
||||
$document_item->compounds = $compounds;
|
||||
|
||||
foreach ($item_taxes as $item_tax) {
|
||||
$item_tax['bill_item_id'] = $bill_item->id;
|
||||
$item_tax['document_item_id'] = $document_item->id;
|
||||
$item_tax['amount'] = round(abs($item_tax['amount']), $precision);
|
||||
|
||||
BillItemTax::create($item_tax);
|
||||
DocumentItemTax::create($item_tax);
|
||||
}
|
||||
}
|
||||
|
||||
return $bill_item;
|
||||
return $document_item;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Models\Sale\InvoiceTotal;
|
||||
use App\Models\Document\DocumentTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
|
||||
class CreateInvoiceItemsAndTotals extends Job
|
||||
class CreateDocumentItemsAndTotals extends Job
|
||||
{
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $invoice;
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
@@ -21,10 +20,10 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($invoice, $request)
|
||||
public function __construct($document, $request)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
$this->document = $document;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,20 +33,21 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$precision = config('money.' . $this->invoice->currency_code . '.precision');
|
||||
$precision = config('money.' . $this->document->currency_code . '.precision');
|
||||
|
||||
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
|
||||
|
||||
$sort_order = 1;
|
||||
|
||||
// Add sub total
|
||||
InvoiceTotal::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => round($sub_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
DocumentTotal::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => round($sub_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $sub_total;
|
||||
@@ -56,13 +56,14 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
|
||||
// Add discount
|
||||
if ($discount_amount_total > 0) {
|
||||
InvoiceTotal::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'code' => 'item_discount',
|
||||
'name' => 'invoices.item_discount',
|
||||
'amount' => round($discount_amount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
DocumentTotal::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'code' => 'item_discount',
|
||||
'name' => 'invoices.item_discount',
|
||||
'amount' => round($discount_amount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_amount_total;
|
||||
@@ -73,13 +74,14 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
if (!empty($this->request['discount'])) {
|
||||
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
|
||||
|
||||
InvoiceTotal::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'code' => 'discount',
|
||||
'name' => 'invoices.discount',
|
||||
'amount' => round($discount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
DocumentTotal::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'code' => 'discount',
|
||||
'name' => 'invoices.discount',
|
||||
'amount' => round($discount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_total;
|
||||
@@ -90,13 +92,14 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
// Add taxes
|
||||
if (!empty($taxes)) {
|
||||
foreach ($taxes as $tax) {
|
||||
InvoiceTotal::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => round(abs($tax['amount']), $precision),
|
||||
'sort_order' => $sort_order,
|
||||
DocumentTotal::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => round(abs($tax['amount']), $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $tax['amount'];
|
||||
@@ -108,8 +111,9 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
// Add extra totals, i.e. shipping fee
|
||||
if (!empty($this->request['totals'])) {
|
||||
foreach ($this->request['totals'] as $total) {
|
||||
$total['company_id'] = $this->invoice->company_id;
|
||||
$total['invoice_id'] = $this->invoice->id;
|
||||
$total['company_id'] = $this->document->company_id;
|
||||
$total['type'] = $this->document->type;
|
||||
$total['document_id'] = $this->document->id;
|
||||
$total['sort_order'] = $sort_order;
|
||||
|
||||
if (empty($total['code'])) {
|
||||
@@ -118,7 +122,7 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
|
||||
$total['amount'] = round(abs($total['amount']), $precision);
|
||||
|
||||
InvoiceTotal::create($total);
|
||||
DocumentTotal::create($total);
|
||||
|
||||
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
|
||||
$this->request['amount'] += $total['amount'];
|
||||
@@ -134,13 +138,14 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
$this->request['amount'] = round($this->request['amount'], $precision);
|
||||
|
||||
// Add total
|
||||
InvoiceTotal::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $this->request['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
DocumentTotal::create([
|
||||
'company_id' => $this->document->company_id,
|
||||
'type' => $this->document->type,
|
||||
'document_id' => $this->document->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $this->request['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -161,7 +166,7 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
$item['global_discount'] = $this->request['discount'];
|
||||
}
|
||||
|
||||
$invoice_item = $this->dispatch(new CreateInvoiceItem($this->invoice, $item));
|
||||
$document_item = $this->dispatch(new CreateDocumentItem($this->document, $item));
|
||||
|
||||
$item_amount = (double) $item['price'] * (double) $item['quantity'];
|
||||
|
||||
@@ -172,16 +177,16 @@ class CreateInvoiceItemsAndTotals extends Job
|
||||
}
|
||||
|
||||
// Calculate totals
|
||||
$sub_total += $invoice_item->total + $discount_amount;
|
||||
$sub_total += $document_item->total + $discount_amount;
|
||||
|
||||
$discount_amount_total += $discount_amount;
|
||||
|
||||
if (!$invoice_item->item_taxes) {
|
||||
if (!$document_item->item_taxes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set taxes
|
||||
foreach ((array) $invoice_item->item_taxes as $item_tax) {
|
||||
foreach ((array) $document_item->item_taxes as $item_tax) {
|
||||
if (array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||
} else {
|
||||
@@ -1,22 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Observers\Transaction;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DeleteBill extends Job
|
||||
class DeleteDocument extends Job
|
||||
{
|
||||
protected $bill;
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($bill)
|
||||
public function __construct($document)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,11 +32,11 @@ class DeleteBill extends Job
|
||||
\DB::transaction(function () {
|
||||
Transaction::mute();
|
||||
|
||||
$this->deleteRelationships($this->bill, [
|
||||
$this->deleteRelationships($this->document, [
|
||||
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
|
||||
]);
|
||||
|
||||
$this->bill->delete();
|
||||
$this->document->delete();
|
||||
|
||||
Transaction::unmute();
|
||||
});
|
||||
@@ -50,8 +51,9 @@ class DeleteBill extends Job
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
if ($this->bill->transactions()->isReconciled()->count()) {
|
||||
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice('general.bills', 1)]);
|
||||
if ($this->document->transactions()->isReconciled()->count()) {
|
||||
$type = Str::plural($this->document->type);
|
||||
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]);
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
40
app/Jobs/Document/DuplicateDocument.php
Normal file
40
app/Jobs/Document/DuplicateDocument.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\DocumentCreated;
|
||||
use App\Models\Document\Document;
|
||||
|
||||
class DuplicateDocument extends Job
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $clone;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->clone = $this->document->duplicate();
|
||||
});
|
||||
|
||||
event(new DocumentCreated($this->clone));
|
||||
|
||||
return $this->clone;
|
||||
}
|
||||
}
|
||||
77
app/Jobs/Document/UpdateDocument.php
Normal file
77
app/Jobs/Document/UpdateDocument.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Document\DocumentUpdated;
|
||||
use App\Events\Document\DocumentUpdating;
|
||||
use App\Jobs\Document\CreateDocumentItemsAndTotals;
|
||||
use App\Models\Document\Document;
|
||||
use App\Traits\Relationships;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UpdateDocument extends Job
|
||||
{
|
||||
use Relationships;
|
||||
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($document, $request)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new DocumentUpdating($this->document, $this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), Str::plural($this->document->type));
|
||||
|
||||
$this->document->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->document, ['items', 'item_taxes', 'totals']);
|
||||
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request));
|
||||
|
||||
$this->document->paid_amount = $this->document->paid;
|
||||
event(new PaidAmountCalculated($this->document));
|
||||
|
||||
if ($this->request['amount'] > $this->document->paid_amount && $this->document->paid_amount > 0) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
|
||||
unset($this->document->reconciled);
|
||||
unset($this->document->paid_amount);
|
||||
|
||||
$this->document->update($this->request->all());
|
||||
|
||||
$this->document->updateRecurring();
|
||||
});
|
||||
|
||||
event(new DocumentUpdated($this->document, $this->request));
|
||||
|
||||
return $this->document;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\Bill;
|
||||
|
||||
class CancelBill extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
*/
|
||||
public function __construct($bill)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->deleteRelationships($this->bill, [
|
||||
'transactions', 'recurring'
|
||||
]);
|
||||
|
||||
$this->bill->status = 'cancelled';
|
||||
$this->bill->save();
|
||||
});
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Purchase\BillCreated;
|
||||
use App\Events\Purchase\BillCreating;
|
||||
use App\Jobs\Purchase\CreateBillItemsAndTotals;
|
||||
use App\Models\Purchase\Bill;
|
||||
|
||||
class CreateBill extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new BillCreating($this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->bill = Bill::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||
|
||||
$this->bill->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
|
||||
|
||||
$this->bill->update($this->request->all());
|
||||
|
||||
$this->bill->createRecurring();
|
||||
});
|
||||
|
||||
event(new BillCreated($this->bill));
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\BillHistory;
|
||||
|
||||
class CreateBillHistory extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
protected $notify;
|
||||
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
* @param $notify
|
||||
* @param $description
|
||||
*/
|
||||
public function __construct($bill, $notify = 0, $description = null)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->notify = $notify;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return BillHistory
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$description = $this->description ?: trans_choice('general.payments', 1);
|
||||
|
||||
$bill_history = BillHistory::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'status' => $this->bill->status,
|
||||
'notify' => $this->notify,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
return $bill_history;
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Purchase\BillTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
|
||||
class CreateBillItemsAndTotals extends Job
|
||||
{
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $bill;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($bill, $request)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$precision = config('money.' . $this->bill->currency_code . '.precision');
|
||||
|
||||
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
|
||||
|
||||
$sort_order = 1;
|
||||
|
||||
// Add sub total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'bills.sub_total',
|
||||
'amount' => round($sub_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $sub_total;
|
||||
|
||||
$sort_order++;
|
||||
|
||||
// Add discount
|
||||
if ($discount_amount_total > 0) {
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'item_discount',
|
||||
'name' => 'bills.item_discount',
|
||||
'amount' => round($discount_amount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_amount_total;
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
|
||||
if (!empty($this->request['discount'])) {
|
||||
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
|
||||
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'discount',
|
||||
'name' => 'bills.discount',
|
||||
'amount' => round($discount_total, $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] -= $discount_total;
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
|
||||
// Add taxes
|
||||
if (!empty($taxes)) {
|
||||
foreach ($taxes as $tax) {
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => round(abs($tax['amount']), $precision),
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
|
||||
$this->request['amount'] += $tax['amount'];
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Add extra totals, i.e. shipping fee
|
||||
if (!empty($this->request['totals'])) {
|
||||
foreach ($this->request['totals'] as $total) {
|
||||
$total['company_id'] = $this->bill->company_id;
|
||||
$total['bill_id'] = $this->bill->id;
|
||||
$total['sort_order'] = $sort_order;
|
||||
|
||||
if (empty($total['code'])) {
|
||||
$total['code'] = 'extra';
|
||||
}
|
||||
|
||||
$total['amount'] = round(abs($total['amount']), $precision);
|
||||
|
||||
BillTotal::create($total);
|
||||
|
||||
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
|
||||
$this->request['amount'] += $total['amount'];
|
||||
} else {
|
||||
// subtraction
|
||||
$this->request['amount'] -= $total['amount'];
|
||||
}
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->request['amount'] = round($this->request['amount'], $precision);
|
||||
|
||||
// Add total
|
||||
BillTotal::create([
|
||||
'company_id' => $this->bill->company_id,
|
||||
'bill_id' => $this->bill->id,
|
||||
'code' => 'total',
|
||||
'name' => 'bills.total',
|
||||
'amount' => $this->request['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createItems()
|
||||
{
|
||||
$sub_total = $discount_amount = $discount_amount_total = 0;
|
||||
|
||||
$taxes = [];
|
||||
|
||||
if (empty($this->request['items'])) {
|
||||
return [$sub_total, $discount_amount_total, $taxes];
|
||||
}
|
||||
|
||||
foreach ((array) $this->request['items'] as $item) {
|
||||
$item['global_discount'] = 0;
|
||||
|
||||
if (!empty($this->request['discount'])) {
|
||||
$item['global_discount'] = $this->request['discount'];
|
||||
}
|
||||
|
||||
$bill_item = $this->dispatch(new CreateBillItem($this->bill, $item));
|
||||
|
||||
$item_amount = (double) $item['price'] * (double) $item['quantity'];
|
||||
|
||||
$discount_amount = 0;
|
||||
|
||||
if (!empty($item['discount'])) {
|
||||
$discount_amount = ($item_amount * ($item['discount'] / 100));
|
||||
}
|
||||
|
||||
// Calculate totals
|
||||
$sub_total += $bill_item->total + $discount_amount;
|
||||
|
||||
$discount_amount_total += $discount_amount;
|
||||
|
||||
if (!$bill_item->item_taxes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set taxes
|
||||
foreach ((array) $bill_item->item_taxes as $item_tax) {
|
||||
if (array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||
} else {
|
||||
$taxes[$item_tax['tax_id']] = [
|
||||
'name' => $item_tax['name'],
|
||||
'amount' => $item_tax['amount']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$sub_total, $discount_amount_total, $taxes];
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Purchase\BillCreated;
|
||||
use App\Models\Purchase\Bill;
|
||||
|
||||
class DuplicateBill extends Job
|
||||
{
|
||||
protected $bill;
|
||||
|
||||
protected $clone;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $bill
|
||||
*/
|
||||
public function __construct($bill)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->clone = $this->bill->duplicate();
|
||||
});
|
||||
|
||||
event(new BillCreated($this->clone));
|
||||
|
||||
return $this->clone;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Purchase\BillUpdated;
|
||||
use App\Events\Purchase\BillUpdating;
|
||||
use App\Jobs\Purchase\CreateBillItemsAndTotals;
|
||||
use App\Models\Purchase\Bill;
|
||||
use App\Traits\Relationships;
|
||||
|
||||
class UpdateBill extends Job
|
||||
{
|
||||
use Relationships;
|
||||
|
||||
protected $bill;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($bill, $request)
|
||||
{
|
||||
$this->bill = $bill;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Bill
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new BillUpdating($this->bill, $this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||
|
||||
$this->bill->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->bill, ['items', 'item_taxes', 'totals']);
|
||||
|
||||
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
|
||||
|
||||
$this->bill->paid_amount = $this->bill->paid;
|
||||
event(new PaidAmountCalculated($this->bill));
|
||||
|
||||
if ($this->request['amount'] > $this->bill->paid_amount && $this->bill->paid_amount > 0) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
|
||||
unset($this->bill->reconciled);
|
||||
unset($this->bill->paid_amount);
|
||||
|
||||
$this->bill->update($this->request->input());
|
||||
|
||||
$this->bill->updateRecurring();
|
||||
});
|
||||
|
||||
event(new BillUpdated($this->bill));
|
||||
|
||||
return $this->bill;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Sale\Invoice;
|
||||
|
||||
class CancelInvoice 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()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->deleteRelationships($this->invoice, [
|
||||
'transactions', 'recurring'
|
||||
]);
|
||||
|
||||
$this->invoice->status = 'cancelled';
|
||||
$this->invoice->save();
|
||||
});
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Sale\InvoiceCreated;
|
||||
use App\Events\Sale\InvoiceCreating;
|
||||
use App\Jobs\Sale\CreateInvoiceItemsAndTotals;
|
||||
use App\Models\Sale\Invoice;
|
||||
|
||||
class CreateInvoice extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Invoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new InvoiceCreating($this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->invoice = Invoice::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
||||
|
||||
$this->invoice->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->dispatch(new CreateInvoiceItemsAndTotals($this->invoice, $this->request));
|
||||
|
||||
$this->invoice->update($this->request->all());
|
||||
|
||||
$this->invoice->createRecurring();
|
||||
});
|
||||
|
||||
event(new InvoiceCreated($this->invoice));
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Sale\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' => $this->invoice->status,
|
||||
'notify' => $this->notify,
|
||||
'description' => $description,
|
||||
]);
|
||||
|
||||
return $invoice_history;
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Sale\InvoiceItem;
|
||||
use App\Models\Sale\InvoiceItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateInvoiceItem extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($invoice, $request)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return InvoiceItem
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
||||
$precision = config('money.' . $this->invoice->currency_code . '.precision');
|
||||
|
||||
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
|
||||
|
||||
$discount = 0;
|
||||
$item_discounted_amount = $item_amount;
|
||||
|
||||
// Apply line discount to amount
|
||||
if (!empty($this->request['discount'])) {
|
||||
$discount += $this->request['discount'];
|
||||
|
||||
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
|
||||
}
|
||||
|
||||
// Apply global discount to amount
|
||||
if (!empty($this->request['global_discount'])) {
|
||||
$discount += $this->request['global_discount'];
|
||||
|
||||
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['global_discount'] / 100));
|
||||
}
|
||||
|
||||
$tax_amount = 0;
|
||||
$item_taxes = [];
|
||||
$item_tax_total = 0;
|
||||
|
||||
if (!empty($this->request['tax_id'])) {
|
||||
$inclusives = $compounds = [];
|
||||
|
||||
foreach ((array) $this->request['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
|
||||
switch ($tax->type) {
|
||||
case 'inclusive':
|
||||
$inclusives[] = $tax;
|
||||
|
||||
break;
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
|
||||
break;
|
||||
case 'fixed':
|
||||
$tax_amount = $tax->rate * (double) $this->request['quantity'];
|
||||
|
||||
$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 'withholding':
|
||||
$tax_amount = 0 - $item_discounted_amount * ($tax->rate / 100);
|
||||
|
||||
$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;
|
||||
default:
|
||||
$tax_amount = $item_discounted_amount * ($tax->rate / 100);
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
if ($inclusives) {
|
||||
$item_amount = $item_discounted_amount + $item_tax_total;
|
||||
|
||||
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
|
||||
|
||||
foreach ($inclusives as $inclusive) {
|
||||
$tax_amount = $item_base_rate * ($inclusive->rate / 100);
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'tax_id' => $inclusive->id,
|
||||
'name' => $inclusive->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
}
|
||||
|
||||
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
|
||||
}
|
||||
|
||||
if ($compounds) {
|
||||
foreach ($compounds as $compound) {
|
||||
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
|
||||
|
||||
$item_taxes[] = [
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'tax_id' => $compound->id,
|
||||
'name' => $compound->name,
|
||||
'amount' => $tax_amount,
|
||||
];
|
||||
|
||||
$item_tax_total += $tax_amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$invoice_item = InvoiceItem::create([
|
||||
'company_id' => $this->invoice->company_id,
|
||||
'invoice_id' => $this->invoice->id,
|
||||
'item_id' => $item_id,
|
||||
'name' => Str::limit($this->request['name'], 180, ''),
|
||||
'quantity' => (double) $this->request['quantity'],
|
||||
'price' => round($this->request['price'], $precision),
|
||||
'tax' => round($item_tax_total, $precision),
|
||||
'discount_rate' => !empty($this->request['discount']) ? $this->request['discount'] : 0,
|
||||
'total' => round($item_amount, $precision),
|
||||
]);
|
||||
|
||||
$invoice_item->item_taxes = false;
|
||||
$invoice_item->inclusives = false;
|
||||
$invoice_item->compounds = false;
|
||||
|
||||
if (!empty($item_taxes)) {
|
||||
$invoice_item->item_taxes = $item_taxes;
|
||||
$invoice_item->inclusives = $inclusives;
|
||||
$invoice_item->compounds = $compounds;
|
||||
|
||||
foreach ($item_taxes as $item_tax) {
|
||||
$item_tax['invoice_item_id'] = $invoice_item->id;
|
||||
$item_tax['amount'] = round(abs($item_tax['amount']), $precision);
|
||||
|
||||
InvoiceItemTax::create($item_tax);
|
||||
}
|
||||
}
|
||||
|
||||
return $invoice_item;
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Observers\Transaction;
|
||||
|
||||
class DeleteInvoice extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
*/
|
||||
public function __construct($invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
\DB::transaction(function () {
|
||||
Transaction::mute();
|
||||
|
||||
$this->deleteRelationships($this->invoice, [
|
||||
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
|
||||
]);
|
||||
|
||||
$this->invoice->delete();
|
||||
|
||||
Transaction::unmute();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
if ($this->invoice->transactions()->isReconciled()->count()) {
|
||||
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice('general.invoices', 1)]);
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Sale\InvoiceCreated;
|
||||
use App\Models\Sale\Invoice;
|
||||
|
||||
class DuplicateInvoice extends Job
|
||||
{
|
||||
protected $invoice;
|
||||
|
||||
protected $clone;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $invoice
|
||||
*/
|
||||
public function __construct($invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Invoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->clone = $this->invoice->duplicate();
|
||||
});
|
||||
|
||||
event(new InvoiceCreated($this->clone));
|
||||
|
||||
return $this->clone;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Sale\InvoiceUpdated;
|
||||
use App\Events\Sale\InvoiceUpdating;
|
||||
use App\Jobs\Sale\CreateInvoiceItemsAndTotals;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Traits\Relationships;
|
||||
|
||||
class UpdateInvoice extends Job
|
||||
{
|
||||
use Relationships;
|
||||
|
||||
protected $invoice;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($invoice, $request)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Invoice
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new InvoiceUpdating($this->invoice, $this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
||||
|
||||
$this->invoice->attachMedia($media, 'attachment');
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->invoice, ['items', 'item_taxes', 'totals']);
|
||||
|
||||
$this->dispatch(new CreateInvoiceItemsAndTotals($this->invoice, $this->request));
|
||||
|
||||
$this->invoice->paid_amount = $this->invoice->paid;
|
||||
event(new PaidAmountCalculated($this->invoice));
|
||||
|
||||
if ($this->request['amount'] > $this->invoice->paid_amount && $this->invoice->paid_amount > 0) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
|
||||
unset($this->invoice->reconciled);
|
||||
unset($this->invoice->paid_amount);
|
||||
|
||||
$this->invoice->update($this->request->all());
|
||||
|
||||
$this->invoice->updateRecurring();
|
||||
});
|
||||
|
||||
event(new InvoiceUpdated($this->invoice, $this->request));
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user