added jobs
This commit is contained in:
parent
7ca8d03844
commit
b80cb67487
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api\Incomes;
|
namespace App\Http\Controllers\Api\Incomes;
|
||||||
|
|
||||||
use App\Events\InvoiceCreated;
|
|
||||||
use App\Events\InvoiceUpdated;
|
use App\Events\InvoiceUpdated;
|
||||||
use App\Http\Controllers\ApiController;
|
use App\Http\Controllers\ApiController;
|
||||||
use App\Http\Requests\Income\Invoice as Request;
|
use App\Http\Requests\Income\Invoice as Request;
|
||||||
|
use App\Jobs\Income\CreateInvoice;
|
||||||
use App\Models\Income\Invoice;
|
use App\Models\Income\Invoice;
|
||||||
use App\Models\Income\InvoiceHistory;
|
use App\Models\Income\InvoiceHistory;
|
||||||
use App\Models\Income\InvoiceItem;
|
use App\Models\Income\InvoiceItem;
|
||||||
@ -13,7 +13,6 @@ use App\Models\Income\InvoicePayment;
|
|||||||
use App\Models\Income\InvoiceTotal;
|
use App\Models\Income\InvoiceTotal;
|
||||||
use App\Models\Common\Item;
|
use App\Models\Common\Item;
|
||||||
use App\Models\Setting\Tax;
|
use App\Models\Setting\Tax;
|
||||||
use App\Notifications\Common\Item as ItemNotification;
|
|
||||||
use App\Traits\Incomes;
|
use App\Traits\Incomes;
|
||||||
use App\Transformers\Income\Invoice as Transformer;
|
use App\Transformers\Income\Invoice as Transformer;
|
||||||
use Dingo\Api\Routing\Helpers;
|
use Dingo\Api\Routing\Helpers;
|
||||||
@ -64,112 +63,9 @@ class Invoices extends ApiController
|
|||||||
$request['amount'] = 0;
|
$request['amount'] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$invoice = Invoice::create($request->all());
|
$invoice = dispatch(new CreateInvoice($request));
|
||||||
|
|
||||||
$taxes = [];
|
return $this->response->created(url('api/invoices/' . $invoice->id));
|
||||||
$tax_total = 0;
|
|
||||||
$sub_total = 0;
|
|
||||||
|
|
||||||
$invoice_item = array();
|
|
||||||
$invoice_item['company_id'] = $request['company_id'];
|
|
||||||
$invoice_item['invoice_id'] = $invoice->id;
|
|
||||||
|
|
||||||
if ($request['item']) {
|
|
||||||
foreach ($request['item'] as $item) {
|
|
||||||
$item_id = 0;
|
|
||||||
$item_sku = '';
|
|
||||||
|
|
||||||
if (!empty($item['item_id'])) {
|
|
||||||
$item_object = Item::find($item['item_id']);
|
|
||||||
|
|
||||||
$item_id = $item['item_id'];
|
|
||||||
|
|
||||||
$item['name'] = $item_object->name;
|
|
||||||
$item_sku = $item_object->sku;
|
|
||||||
|
|
||||||
// Decrease stock (item sold)
|
|
||||||
$item_object->quantity -= $item['quantity'];
|
|
||||||
$item_object->save();
|
|
||||||
|
|
||||||
// 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($item['sku'])) {
|
|
||||||
$item_sku = $item['sku'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$tax = $tax_id = 0;
|
|
||||||
|
|
||||||
if (!empty($item['tax_id'])) {
|
|
||||||
$tax_object = Tax::find($item['tax_id']);
|
|
||||||
|
|
||||||
$tax_id = $item['tax_id'];
|
|
||||||
|
|
||||||
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
|
|
||||||
} elseif (!empty($item['tax'])) {
|
|
||||||
$tax = $item['tax'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$invoice_item['item_id'] = $item_id;
|
|
||||||
$invoice_item['name'] = str_limit($item['name'], 180, '');
|
|
||||||
$invoice_item['sku'] = $item_sku;
|
|
||||||
$invoice_item['quantity'] = $item['quantity'];
|
|
||||||
$invoice_item['price'] = $item['price'];
|
|
||||||
$invoice_item['tax'] = $tax;
|
|
||||||
$invoice_item['tax_id'] = $tax_id;
|
|
||||||
$invoice_item['total'] = $item['price'] * $item['quantity'];
|
|
||||||
|
|
||||||
InvoiceItem::create($invoice_item);
|
|
||||||
|
|
||||||
if (isset($tax_object)) {
|
|
||||||
if (array_key_exists($tax_object->id, $taxes)) {
|
|
||||||
$taxes[$tax_object->id]['amount'] += $tax;
|
|
||||||
} else {
|
|
||||||
$taxes[$tax_object->id] = [
|
|
||||||
'name' => $tax_object->name,
|
|
||||||
'amount' => $tax
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$tax_total += $tax;
|
|
||||||
$sub_total += $invoice_item['total'];
|
|
||||||
|
|
||||||
unset($item_object);
|
|
||||||
unset($tax_object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($request['amount'])) {
|
|
||||||
$request['amount'] = $sub_total + $tax_total;
|
|
||||||
}
|
|
||||||
|
|
||||||
$invoice->update($request->input());
|
|
||||||
|
|
||||||
// Add invoice totals
|
|
||||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
|
||||||
|
|
||||||
$request['invoice_id'] = $invoice->id;
|
|
||||||
$request['status_code'] = $request['invoice_status_code'];
|
|
||||||
$request['notify'] = 0;
|
|
||||||
$request['description'] = trans('messages.success.added', ['type' => $request['invoice_number']]);
|
|
||||||
|
|
||||||
InvoiceHistory::create($request->input());
|
|
||||||
|
|
||||||
// Update next invoice number
|
|
||||||
$this->increaseNextInvoiceNumber();
|
|
||||||
|
|
||||||
// Fire the event to make it extendible
|
|
||||||
event(new InvoiceCreated($invoice));
|
|
||||||
|
|
||||||
return $this->response->created(url('api/invoices/'.$invoice->id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -276,12 +172,9 @@ class Invoices extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function destroy(Invoice $invoice)
|
public function destroy(Invoice $invoice)
|
||||||
{
|
{
|
||||||
|
$this->deleteRelationships($invoice, ['items', 'histories', 'payments', 'recurring', 'totals']);
|
||||||
$invoice->delete();
|
$invoice->delete();
|
||||||
|
|
||||||
InvoiceItem::where('invoice_id', $invoice->id)->delete();
|
|
||||||
InvoicePayment::where('invoice_id', $invoice->id)->delete();
|
|
||||||
InvoiceHistory::where('invoice_id', $invoice->id)->delete();
|
|
||||||
|
|
||||||
return $this->response->noContent();
|
return $this->response->noContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,10 @@ use App\Events\InvoiceUpdated;
|
|||||||
use App\Http\Controllers\Controller;
|
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\CreateInvoicePayment;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
|
use App\Models\Common\Item;
|
||||||
use App\Models\Common\Media;
|
use App\Models\Common\Media;
|
||||||
use App\Models\Income\Customer;
|
use App\Models\Income\Customer;
|
||||||
use App\Models\Income\Invoice;
|
use App\Models\Income\Invoice;
|
||||||
@ -18,7 +21,6 @@ use App\Models\Income\InvoiceItemTax;
|
|||||||
use App\Models\Income\InvoiceTotal;
|
use App\Models\Income\InvoiceTotal;
|
||||||
use App\Models\Income\InvoicePayment;
|
use App\Models\Income\InvoicePayment;
|
||||||
use App\Models\Income\InvoiceStatus;
|
use App\Models\Income\InvoiceStatus;
|
||||||
use App\Models\Common\Item;
|
|
||||||
use App\Models\Setting\Category;
|
use App\Models\Setting\Category;
|
||||||
use App\Models\Setting\Currency;
|
use App\Models\Setting\Currency;
|
||||||
use App\Models\Setting\Tax;
|
use App\Models\Setting\Tax;
|
||||||
@ -120,167 +122,7 @@ class Invoices extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$invoice = Invoice::create($request->input());
|
$invoice = dispatch(new CreateInvoice($request));
|
||||||
|
|
||||||
// Upload attachment
|
|
||||||
if ($request->file('attachment')) {
|
|
||||||
$media = $this->getMedia($request->file('attachment'), 'invoices');
|
|
||||||
|
|
||||||
$invoice->attachMedia($media, 'attachment');
|
|
||||||
}
|
|
||||||
|
|
||||||
$taxes = [];
|
|
||||||
|
|
||||||
$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']) {
|
|
||||||
foreach ($request['item'] as $item) {
|
|
||||||
$item_sku = '';
|
|
||||||
|
|
||||||
if (!empty($item['item_id'])) {
|
|
||||||
$item_object = Item::find($item['item_id']);
|
|
||||||
|
|
||||||
$item['name'] = $item_object->name;
|
|
||||||
$item_sku = $item_object->sku;
|
|
||||||
|
|
||||||
// Decrease stock (item sold)
|
|
||||||
$item_object->quantity -= $item['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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$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'] = $item_tax;
|
|
||||||
$invoice_item['tax_id'] = 0;//(int) $item_taxes;
|
|
||||||
$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']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate totals
|
|
||||||
$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());
|
|
||||||
|
|
||||||
// Add invoice totals
|
|
||||||
$this->addTotals($invoice, $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 extendible
|
|
||||||
event(new InvoiceCreated($invoice));
|
|
||||||
|
|
||||||
$message = trans('messages.success.added', ['type' => trans_choice('general.invoices', 1)]);
|
$message = trans('messages.success.added', ['type' => trans_choice('general.invoices', 1)]);
|
||||||
|
|
||||||
@ -813,20 +655,7 @@ class Invoices extends Controller
|
|||||||
|
|
||||||
$invoice->save();
|
$invoice->save();
|
||||||
|
|
||||||
$invoice_payment_request = [
|
$invoice_payment = dispatch(new CreateInvoicePayment($request, $invoice));
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'invoice_id' => $request['invoice_id'],
|
|
||||||
'account_id' => $request['account_id'],
|
|
||||||
'paid_at' => $request['paid_at'],
|
|
||||||
'amount' => $request['amount'],
|
|
||||||
'currency_code' => $request['currency_code'],
|
|
||||||
'currency_rate' => $request['currency_rate'],
|
|
||||||
'description' => $request['description'],
|
|
||||||
'payment_method' => $request['payment_method'],
|
|
||||||
'reference' => $request['reference']
|
|
||||||
];
|
|
||||||
|
|
||||||
$invoice_payment = InvoicePayment::create($invoice_payment_request);
|
|
||||||
|
|
||||||
// Upload attachment
|
// Upload attachment
|
||||||
if ($request->file('attachment')) {
|
if ($request->file('attachment')) {
|
||||||
@ -835,15 +664,6 @@ class Invoices extends Controller
|
|||||||
$invoice_payment->attachMedia($media, 'attachment');
|
$invoice_payment->attachMedia($media, 'attachment');
|
||||||
}
|
}
|
||||||
|
|
||||||
$request['status_code'] = $invoice->invoice_status_code;
|
|
||||||
$request['notify'] = 0;
|
|
||||||
|
|
||||||
$desc_amount = money((float) $request['amount'], (string) $request['currency_code'], true)->format();
|
|
||||||
|
|
||||||
$request['description'] = $desc_amount . ' ' . trans_choice('general.payments', 1);
|
|
||||||
|
|
||||||
InvoiceHistory::create($request->input());
|
|
||||||
|
|
||||||
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
|
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
|
162
app/Jobs/Income/CreateInvoice.php
Normal file
162
app/Jobs/Income/CreateInvoice.php
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Income;
|
||||||
|
|
||||||
|
use App\Events\InvoiceCreated;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
use Currencies, DateTime, Dispatchable, Incomes, Uploads;
|
||||||
|
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $request
|
||||||
|
*/
|
||||||
|
public function __construct($request)
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return Invoice
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$invoice = Invoice::create($this->request->input());
|
||||||
|
|
||||||
|
// Upload attachment
|
||||||
|
if ($this->request->file('attachment')) {
|
||||||
|
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
||||||
|
|
||||||
|
$invoice->attachMedia($media, 'attachment');
|
||||||
|
}
|
||||||
|
|
||||||
|
$taxes = [];
|
||||||
|
|
||||||
|
$tax_total = 0;
|
||||||
|
$sub_total = 0;
|
||||||
|
$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));
|
||||||
|
|
||||||
|
// Calculate totals
|
||||||
|
$tax_total += $invoice_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;
|
||||||
|
|
||||||
|
$this->request['amount'] = money($amount, $this->request['currency_code'])->getAmount();
|
||||||
|
|
||||||
|
$invoice->update($this->request->input());
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
150
app/Jobs/Income/CreateInvoiceItem.php
Normal file
150
app/Jobs/Income/CreateInvoiceItem.php
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Income;
|
||||||
|
|
||||||
|
use App\Models\Common\Item;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class CreateInvoiceItem
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
|
||||||
|
protected $data;
|
||||||
|
|
||||||
|
protected $invoice;
|
||||||
|
|
||||||
|
protected $discount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $data
|
||||||
|
* @param $invoice
|
||||||
|
* @param $discount
|
||||||
|
*/
|
||||||
|
public function __construct($data, $invoice, $discount = null)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
$this->invoice = $invoice;
|
||||||
|
$this->discount = $discount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return InvoiceItem
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$item_sku = '';
|
||||||
|
|
||||||
|
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||||
|
|
||||||
|
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 -= $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'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$item_tax = 0;
|
||||||
|
$item_taxes = [];
|
||||||
|
$invoice_item_taxes = [];
|
||||||
|
|
||||||
|
if (!empty($this->data['tax_id'])) {
|
||||||
|
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
||||||
|
$tax_object = Tax::find($tax_id);
|
||||||
|
|
||||||
|
$item_taxes[] = $tax_id;
|
||||||
|
|
||||||
|
$tax = (((double) $this->data['price'] * (double) $this->data['quantity']) / 100) * $tax_object->rate;
|
||||||
|
|
||||||
|
// Apply discount to tax
|
||||||
|
if ($this->discount) {
|
||||||
|
$tax = $tax - ($tax * ($this->discount / 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
$invoice_item_taxes[] = [
|
||||||
|
'company_id' => $this->invoice->company_id,
|
||||||
|
'invoice_id' => $this->invoice->id,
|
||||||
|
'tax_id' => $tax_id,
|
||||||
|
'name' => $tax_object->name,
|
||||||
|
'amount' => $tax,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_tax += $tax;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$invoice_item = InvoiceItem::create([
|
||||||
|
'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,
|
||||||
|
'quantity' => (double) $this->data['quantity'],
|
||||||
|
'price' => (double) $this->data['price'],
|
||||||
|
'tax' => $item_tax,
|
||||||
|
'tax_id' => 0, // (int) $item_taxes;
|
||||||
|
'total' => (double) $this->data['price'] * (double) $this->data['quantity'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($invoice_item_taxes) {
|
||||||
|
foreach ($invoice_item_taxes as $invoice_item_tax) {
|
||||||
|
$invoice_item_tax['invoice_item_id'] = $invoice_item->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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invoice_item;
|
||||||
|
}
|
||||||
|
}
|
52
app/Jobs/Income/CreateInvoicePayment.php
Normal file
52
app/Jobs/Income/CreateInvoicePayment.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user