renamed income/expense

This commit is contained in:
denisdulici
2019-12-31 15:49:09 +03:00
parent e2189158b9
commit 2428feb73b
235 changed files with 815 additions and 2147 deletions

View File

@@ -0,0 +1,195 @@
<?php
namespace App\Jobs\Sale;
use App\Abstracts\Job;
use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceCreating;
use App\Models\Sale\Invoice;
use App\Models\Sale\InvoiceTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
class CreateInvoice extends Job
{
use Currencies, DateTime;
protected $request;
protected $invoice;
/**
* 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));
$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->createItemsAndTotals();
$this->invoice->update($this->request->all());
$this->invoice->createRecurring();
event(new InvoiceCreated($this->invoice));
return $this->invoice;
}
protected function createItemsAndTotals()
{
// Create items
list($sub_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' => $sub_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] += $sub_total;
$sort_order++;
// Add discount
if (!empty($this->request['discount'])) {
$discount_total = $sub_total * ($this->request['discount'] / 100);
InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'discount',
'name' => 'invoices.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_total;
$sort_order++;
}
// 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' => $tax['amount'],
'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->invoice->company_id;
$total['invoice_id'] = $this->invoice->id;
$total['sort_order'] = $sort_order;
if (empty($total['code'])) {
$total['code'] = 'extra';
}
InvoiceTotal::create($total);
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
$this->request['amount'] += $total['amount'];
} else {
// subtraction
$this->request['amount'] -= $total['amount'];
}
$sort_order++;
}
}
// 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,
]);
}
protected function createItems()
{
$sub_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $taxes];
}
foreach ((array) $this->request['items'] as $item) {
if (empty($item['discount'])) {
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
}
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));
// Calculate totals
$sub_total += $invoice_item->total;
if (!$invoice_item->item_taxes) {
continue;
}
// Set taxes
foreach ((array) $invoice_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, $taxes];
}
}

View File

@@ -0,0 +1,49 @@
<?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_code' => $this->invoice->invoice_status_code,
'notify' => $this->notify,
'description' => $description,
]);
return $invoice_history;
}
}

View File

@@ -0,0 +1,162 @@
<?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 $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 InvoiceItem
*/
public function handle()
{
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
$item_discounted_amount = $item_amount;
// Apply discount to amount
if (!empty($this->request['discount'])) {
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['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;
default:
$tax_amount = ($item_discounted_amount / 100) * $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;
}
}
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) {
$item_tax_total += $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_amount = ($item_amount - $item_tax_total) / (1 - $this->request['discount'] / 100);
}
if ($compounds) {
foreach ($compounds as $compound) {
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
$item_tax_total += $tax_amount;
$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $compound->id,
'name' => $compound->name,
'amount' => $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' => (double) $this->request['price'],
'tax' => $item_tax_total,
'total' => $item_amount,
]);
$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;
InvoiceItemTax::create($item_tax);
}
}
return $invoice_item;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Jobs\Sale;
use App\Abstracts\Job;
use App\Models\Sale\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;
}
}

View File

@@ -0,0 +1,36 @@
<?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;
/**
* 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;
}
}

View File

@@ -0,0 +1,209 @@
<?php
namespace App\Jobs\Sale;
use App\Abstracts\Job;
use App\Events\Sale\InvoiceUpdated;
use App\Events\Sale\InvoiceUpdating;
use App\Models\Sale\Invoice;
use App\Models\Sale\InvoiceTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Relationships;
class UpdateInvoice extends Job
{
use Currencies, DateTime, 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));
// Upload attachment
if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
$this->invoice->attachMedia($media, 'attachment');
}
$this->createItemsAndTotals();
$invoice_paid = $this->invoice->paid;
unset($this->invoice->reconciled);
if (($invoice_paid) && $this->request['amount'] > $invoice_paid) {
$this->request['invoice_status_code'] = 'partial';
}
$this->invoice->update($this->request->all());
$this->invoice->updateRecurring();
event(new InvoiceUpdated($this->invoice, $this->request));
return $this->invoice;
}
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $taxes) = $this->createItems();
// Delete current totals
$this->deleteRelationships($this->invoice, 'totals');
$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' => $sub_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] += $sub_total;
$sort_order++;
// Add discount
if (!empty($this->request['discount'])) {
$discount_total = $sub_total * ($this->request['discount'] / 100);
InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'discount',
'name' => 'invoices.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_total;
$sort_order++;
}
// 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' => $tax['amount'],
'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->invoice->company_id;
$total['invoice_id'] = $this->invoice->id;
$total['sort_order'] = $sort_order;
if (empty($total['code'])) {
$total['code'] = 'extra';
}
InvoiceTotal::create($total);
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
$this->request['amount'] += $total['amount'];
} else {
// subtraction
$this->request['amount'] -= $total['amount'];
}
$sort_order++;
}
}
// 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,
]);
}
protected function createItems()
{
$sub_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $taxes];
}
// Delete current items
$this->deleteRelationships($this->invoice, ['items', 'item_taxes']);
foreach ((array) $this->request['items'] as $item) {
if (empty($item['discount'])) {
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
}
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));
// Calculate totals
$sub_total += $invoice_item->total;
if (!$invoice_item->item_taxes) {
continue;
}
// Set taxes
foreach ((array) $invoice_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, $taxes];
}
}