Invoice create and update function finished.
This commit is contained in:
parent
475a39c818
commit
71bcdb915a
@ -9,6 +9,7 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Http\Requests\Income\Invoice as Request;
|
use App\Http\Requests\Income\Invoice as Request;
|
||||||
use App\Http\Requests\Income\InvoicePayment as PaymentRequest;
|
use App\Http\Requests\Income\InvoicePayment as PaymentRequest;
|
||||||
use App\Jobs\Income\CreateInvoice;
|
use App\Jobs\Income\CreateInvoice;
|
||||||
|
use App\Jobs\Income\UpdateInvoice;
|
||||||
use App\Jobs\Income\CreateInvoicePayment;
|
use App\Jobs\Income\CreateInvoicePayment;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
use App\Models\Common\Item;
|
use App\Models\Common\Item;
|
||||||
@ -238,127 +239,7 @@ class Invoices extends Controller
|
|||||||
*/
|
*/
|
||||||
public function update(Invoice $invoice, Request $request)
|
public function update(Invoice $invoice, Request $request)
|
||||||
{
|
{
|
||||||
$taxes = [];
|
$invoice = dispatch(new UpdateInvoice($invoice, $request));
|
||||||
|
|
||||||
$tax_total = 0;
|
|
||||||
$sub_total = 0;
|
|
||||||
$discount_total = 0;
|
|
||||||
$discount = $request['discount'];
|
|
||||||
|
|
||||||
$invoice_item = [];
|
|
||||||
$invoice_item['company_id'] = $request['company_id'];
|
|
||||||
$invoice_item['invoice_id'] = $invoice->id;
|
|
||||||
|
|
||||||
if ($request['item']) {
|
|
||||||
$this->deleteRelationships($invoice, 'items');
|
|
||||||
|
|
||||||
foreach ($request['item'] as $item) {
|
|
||||||
unset($tax_object);
|
|
||||||
$item_sku = '';
|
|
||||||
|
|
||||||
if (!empty($item['item_id'])) {
|
|
||||||
$item_object = Item::find($item['item_id']);
|
|
||||||
|
|
||||||
$item['name'] = $item_object->name;
|
|
||||||
$item_sku = $item_object->sku;
|
|
||||||
}
|
|
||||||
|
|
||||||
$item_tax = 0;
|
|
||||||
$item_taxes = [];
|
|
||||||
$invoice_item_taxes = [];
|
|
||||||
|
|
||||||
if (!empty($item['tax_id'])) {
|
|
||||||
foreach ($item['tax_id'] as $tax_id) {
|
|
||||||
$tax_object = Tax::find($tax_id);
|
|
||||||
|
|
||||||
$item_taxes[] = $tax_id;
|
|
||||||
|
|
||||||
$tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate;
|
|
||||||
|
|
||||||
// Apply discount to tax
|
|
||||||
if ($discount) {
|
|
||||||
$tax = $tax - ($tax * ($discount / 100));
|
|
||||||
}
|
|
||||||
|
|
||||||
$invoice_item_taxes[] = [
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'invoice_id' => $invoice->id,
|
|
||||||
'tax_id' => $tax_id,
|
|
||||||
'name' => $tax_object->name,
|
|
||||||
'amount' => $tax,
|
|
||||||
];
|
|
||||||
|
|
||||||
$item_tax += $tax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$invoice_item['item_id'] = $item['item_id'];
|
|
||||||
$invoice_item['name'] = str_limit($item['name'], 180, '');
|
|
||||||
$invoice_item['sku'] = $item_sku;
|
|
||||||
$invoice_item['quantity'] = (double) $item['quantity'];
|
|
||||||
$invoice_item['price'] = (double) $item['price'];
|
|
||||||
$invoice_item['tax'] = $tax;
|
|
||||||
$invoice_item['tax_id'] = 0;//$tax_id;
|
|
||||||
$invoice_item['total'] = (double) $item['price'] * (double) $item['quantity'];
|
|
||||||
|
|
||||||
$invoice_item_created = InvoiceItem::create($invoice_item);
|
|
||||||
|
|
||||||
if ($invoice_item_taxes) {
|
|
||||||
foreach ($invoice_item_taxes as $invoice_item_tax) {
|
|
||||||
$invoice_item_tax['invoice_item_id'] = $invoice_item_created->id;
|
|
||||||
|
|
||||||
InvoiceItemTax::create($invoice_item_tax);
|
|
||||||
|
|
||||||
// Set taxes
|
|
||||||
if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) {
|
|
||||||
$taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount'];
|
|
||||||
} else {
|
|
||||||
$taxes[$invoice_item_tax['tax_id']] = [
|
|
||||||
'name' => $invoice_item_tax['name'],
|
|
||||||
'amount' => $invoice_item_tax['amount']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$tax_total += $item_tax;
|
|
||||||
$sub_total += $invoice_item['total'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$s_total = $sub_total;
|
|
||||||
|
|
||||||
// Apply discount to total
|
|
||||||
if ($discount) {
|
|
||||||
$s_discount = $s_total * ($discount / 100);
|
|
||||||
$discount_total += $s_discount;
|
|
||||||
$s_total = $s_total - $s_discount;
|
|
||||||
}
|
|
||||||
|
|
||||||
$amount = $s_total + $tax_total;
|
|
||||||
|
|
||||||
$request['amount'] = money($amount, $request['currency_code'])->getAmount();
|
|
||||||
|
|
||||||
$invoice->update($request->input());
|
|
||||||
|
|
||||||
// Upload attachment
|
|
||||||
if ($request->file('attachment')) {
|
|
||||||
$media = $this->getMedia($request->file('attachment'), 'invoices');
|
|
||||||
|
|
||||||
$invoice->attachMedia($media, 'attachment');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete previous invoice totals
|
|
||||||
$this->deleteRelationships($invoice, 'totals');
|
|
||||||
|
|
||||||
// Add invoice totals
|
|
||||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total);
|
|
||||||
|
|
||||||
// Recurring
|
|
||||||
$invoice->updateRecurring();
|
|
||||||
|
|
||||||
// Fire the event to make it extendible
|
|
||||||
event(new InvoiceUpdated($invoice));
|
|
||||||
|
|
||||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.invoices', 1)]);
|
$message = trans('messages.success.updated', ['type' => trans_choice('general.invoices', 1)]);
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ class CreateInvoice
|
|||||||
|
|
||||||
// Set taxes
|
// Set taxes
|
||||||
foreach ($invoice_item->item_taxes as $item_tax) {
|
foreach ($invoice_item->item_taxes as $item_tax) {
|
||||||
if (isset($taxes) && in_array($item['tax_id'], $taxes)) {
|
if (isset($taxes) && array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||||
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||||
} else {
|
} else {
|
||||||
$taxes[$item_tax['tax_id']] = [
|
$taxes[$item_tax['tax_id']] = [
|
||||||
|
195
app/Jobs/Income/UpdateInvoice.php
Normal file
195
app/Jobs/Income/UpdateInvoice.php
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Income;
|
||||||
|
|
||||||
|
use App\Events\InvoiceUpdated;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
use Currencies, DateTime, Dispatchable, Incomes, Uploads;
|
||||||
|
|
||||||
|
protected $invoice;
|
||||||
|
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $request
|
||||||
|
*/
|
||||||
|
public function __construct($invoice, $request)
|
||||||
|
{
|
||||||
|
$this->invoice = $invoice;
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return Invoice
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
// Upload attachment
|
||||||
|
if ($this->request->file('attachment')) {
|
||||||
|
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
||||||
|
|
||||||
|
$this->invoice->attachMedia($media, 'attachment');
|
||||||
|
}
|
||||||
|
|
||||||
|
$taxes = [];
|
||||||
|
|
||||||
|
$tax_total = 0;
|
||||||
|
$sub_total = 0;
|
||||||
|
$discount_total = 0;
|
||||||
|
$discount = $this->request['discount'];
|
||||||
|
|
||||||
|
if ($this->request['item']) {
|
||||||
|
$this->deleteRelationships($this->invoice, 'items');
|
||||||
|
|
||||||
|
foreach ($this->request['item'] as $item) {
|
||||||
|
$invoice_item = dispatch(new CreateInvoiceItem($item, $this->invoice, $discount));
|
||||||
|
|
||||||
|
// Calculate totals
|
||||||
|
$tax_total += $invoice_item->tax;
|
||||||
|
$sub_total += $invoice_item->total;
|
||||||
|
|
||||||
|
// Set taxes
|
||||||
|
foreach ($invoice_item->item_taxes as $item_tax) {
|
||||||
|
if (isset($taxes) && 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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$s_total = $sub_total;
|
||||||
|
|
||||||
|
// Apply discount to total
|
||||||
|
if ($discount) {
|
||||||
|
$s_discount = $s_total * ($discount / 100);
|
||||||
|
$discount_total += $s_discount;
|
||||||
|
$s_total = $s_total - $s_discount;
|
||||||
|
}
|
||||||
|
|
||||||
|
$amount = $s_total + $tax_total;
|
||||||
|
|
||||||
|
$this->request['amount'] = money($amount, $this->request['currency_code'])->getAmount();
|
||||||
|
|
||||||
|
$this->invoice->update($this->request->input());
|
||||||
|
|
||||||
|
// Delete previous invoice totals
|
||||||
|
$this->deleteRelationships($this->invoice, 'totals');
|
||||||
|
|
||||||
|
// Add invoice totals
|
||||||
|
$this->addTotals($this->invoice, $this->request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||||
|
|
||||||
|
// Recurring
|
||||||
|
$this->invoice->updateRecurring();
|
||||||
|
|
||||||
|
// Fire the event to make it extensible
|
||||||
|
event(new InvoiceUpdated($this->invoice));
|
||||||
|
|
||||||
|
return $this->invoice;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total)
|
||||||
|
{
|
||||||
|
$sort_order = 1;
|
||||||
|
|
||||||
|
// Added invoice sub total
|
||||||
|
InvoiceTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'invoice_id' => $invoice->id,
|
||||||
|
'code' => 'sub_total',
|
||||||
|
'name' => 'invoices.sub_total',
|
||||||
|
'amount' => $sub_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
|
||||||
|
// Added invoice discount
|
||||||
|
if ($discount_total) {
|
||||||
|
InvoiceTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'invoice_id' => $invoice->id,
|
||||||
|
'code' => 'discount',
|
||||||
|
'name' => 'invoices.discount',
|
||||||
|
'amount' => $discount_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// This is for total
|
||||||
|
$sub_total = $sub_total - $discount_total;
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added invoice taxes
|
||||||
|
if (isset($taxes)) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
InvoiceTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'invoice_id' => $invoice->id,
|
||||||
|
'code' => 'tax',
|
||||||
|
'name' => $tax['name'],
|
||||||
|
'amount' => $tax['amount'],
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added invoice total
|
||||||
|
InvoiceTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'invoice_id' => $invoice->id,
|
||||||
|
'code' => 'total',
|
||||||
|
'name' => 'invoices.total',
|
||||||
|
'amount' => $sub_total + $tax_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mass delete relationships with events being fired.
|
||||||
|
*
|
||||||
|
* @param $model
|
||||||
|
* @param $relationships
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deleteRelationships($model, $relationships)
|
||||||
|
{
|
||||||
|
foreach ((array) $relationships as $relationship) {
|
||||||
|
if (empty($model->$relationship)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$items = $model->$relationship->all();
|
||||||
|
|
||||||
|
if ($items instanceof Collection) {
|
||||||
|
$items = $items->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $items as $item) {
|
||||||
|
$item->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user