Merge pull request #602 from cuneytsenturk/tax-rate
Add calculate and compound feature on Tax
This commit is contained in:
commit
4131237c47
@ -323,20 +323,78 @@ class Items extends Controller
|
|||||||
$quantity = (double) $item['quantity'];
|
$quantity = (double) $item['quantity'];
|
||||||
|
|
||||||
$item_tax_total = 0;
|
$item_tax_total = 0;
|
||||||
|
$item_tax_amount = 0;
|
||||||
|
|
||||||
$item_sub_total = ($price * $quantity);
|
$item_sub_total = ($price * $quantity);
|
||||||
|
$item_discount_total = $item_sub_total;
|
||||||
|
|
||||||
|
// Apply discount to item
|
||||||
|
if ($discount) {
|
||||||
|
$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($item['tax_id'])) {
|
if (!empty($item['tax_id'])) {
|
||||||
|
$calculates = $compounds = $taxes = [];
|
||||||
|
|
||||||
foreach ($item['tax_id'] as $tax_id) {
|
foreach ($item['tax_id'] as $tax_id) {
|
||||||
$tax = Tax::find($tax_id);
|
$tax = Tax::find($tax_id);
|
||||||
|
|
||||||
$item_tax = (($price * $quantity) / 100) * $tax->rate;
|
switch ($tax->type) {
|
||||||
|
case 'calculate':
|
||||||
|
$calculates[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'compound':
|
||||||
|
$compounds[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'normal':
|
||||||
|
default:
|
||||||
|
$taxes[] = $tax;
|
||||||
|
|
||||||
// Apply discount to tax
|
$item_tax_amount = ($item_discount_total / 100) * $tax->rate;
|
||||||
if ($discount) {
|
|
||||||
$item_tax = $item_tax - ($item_tax * ($discount / 100));
|
$item_tax_total += $item_tax_amount;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$item_tax_total += $item_tax;
|
if ($calculates) {
|
||||||
|
if ($discount) {
|
||||||
|
$item_tax_total = 0;
|
||||||
|
|
||||||
|
if ($taxes) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
$item_tax_amount = ($item_sub_total / 100) * $tax->rate;
|
||||||
|
|
||||||
|
$item_tax_total += $item_tax_amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_sub_total + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);
|
||||||
|
|
||||||
|
$item_sub_total = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
|
||||||
|
$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_discount_total + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);
|
||||||
|
|
||||||
|
$item_sub_total = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
|
||||||
|
$item_discount_total = $item_sub_total - ($item_sub_total * ($discount / 100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($compounds) {
|
||||||
|
foreach ($compounds as $compound) {
|
||||||
|
$item_tax_total += (($item_discount_total + $item_tax_total) / 100) * $compound->rate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ use App\Events\BillUpdated;
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\Expense\Bill as Request;
|
use App\Http\Requests\Expense\Bill as Request;
|
||||||
use App\Http\Requests\Expense\BillPayment as PaymentRequest;
|
use App\Http\Requests\Expense\BillPayment as PaymentRequest;
|
||||||
|
use App\Jobs\Expense\CreateBill;
|
||||||
|
use App\Jobs\Expense\UpdateBill;
|
||||||
|
use App\Jobs\Expense\CreateBillPayment;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
use App\Models\Common\Media;
|
use App\Models\Common\Media;
|
||||||
use App\Models\Expense\BillStatus;
|
use App\Models\Expense\BillStatus;
|
||||||
@ -111,137 +114,7 @@ class Bills extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$bill = Bill::create($request->input());
|
$bill = dispatch(new CreateBill($request));
|
||||||
|
|
||||||
// Upload attachment
|
|
||||||
if ($request->file('attachment')) {
|
|
||||||
$media = $this->getMedia($request->file('attachment'), 'bills');
|
|
||||||
|
|
||||||
$bill->attachMedia($media, 'attachment');
|
|
||||||
}
|
|
||||||
|
|
||||||
$taxes = [];
|
|
||||||
|
|
||||||
$tax_total = 0;
|
|
||||||
$sub_total = 0;
|
|
||||||
$discount_total = 0;
|
|
||||||
$discount = $request['discount'];
|
|
||||||
|
|
||||||
$bill_item = [];
|
|
||||||
$bill_item['company_id'] = $request['company_id'];
|
|
||||||
$bill_item['bill_id'] = $bill->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;
|
|
||||||
|
|
||||||
// Increase stock (item bought)
|
|
||||||
$item_object->quantity += $item['quantity'];
|
|
||||||
$item_object->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
$item_tax = 0;
|
|
||||||
$item_taxes = [];
|
|
||||||
$bill_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));
|
|
||||||
}
|
|
||||||
|
|
||||||
$bill_item_taxes[] = [
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'tax_id' => $tax_id,
|
|
||||||
'name' => $tax_object->name,
|
|
||||||
'amount' => $tax,
|
|
||||||
];
|
|
||||||
|
|
||||||
$item_tax += $tax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$bill_item['item_id'] = $item['item_id'];
|
|
||||||
$bill_item['name'] = str_limit($item['name'], 180, '');
|
|
||||||
$bill_item['sku'] = $item_sku;
|
|
||||||
$bill_item['quantity'] = (double) $item['quantity'];
|
|
||||||
$bill_item['price'] = (double) $item['price'];
|
|
||||||
$bill_item['tax'] = $item_tax;
|
|
||||||
$bill_item['tax_id'] = 0;//$tax_id;
|
|
||||||
$bill_item['total'] = (double) $item['price'] * (double) $item['quantity'];
|
|
||||||
|
|
||||||
$bill_item_created = BillItem::create($bill_item);
|
|
||||||
|
|
||||||
if ($bill_item_taxes) {
|
|
||||||
foreach ($bill_item_taxes as $bill_item_tax) {
|
|
||||||
$bill_item_tax['bill_item_id'] = $bill_item_created->id;
|
|
||||||
|
|
||||||
BillItemTax::create($bill_item_tax);
|
|
||||||
|
|
||||||
// Set taxes
|
|
||||||
if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) {
|
|
||||||
$taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount'];
|
|
||||||
} else {
|
|
||||||
$taxes[$bill_item_tax['tax_id']] = [
|
|
||||||
'name' => $bill_item_tax['name'],
|
|
||||||
'amount' => $bill_item_tax['amount']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate totals
|
|
||||||
$tax_total += $item_tax;
|
|
||||||
$sub_total += $bill_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();
|
|
||||||
|
|
||||||
$bill->update($request->input());
|
|
||||||
|
|
||||||
// Add bill totals
|
|
||||||
$this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total);
|
|
||||||
|
|
||||||
// Add bill history
|
|
||||||
BillHistory::create([
|
|
||||||
'company_id' => session('company_id'),
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'status_code' => 'draft',
|
|
||||||
'notify' => 0,
|
|
||||||
'description' => trans('messages.success.added', ['type' => $bill->bill_number]),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Recurring
|
|
||||||
$bill->createRecurring();
|
|
||||||
|
|
||||||
// Fire the event to make it extendible
|
|
||||||
event(new BillCreated($bill));
|
|
||||||
|
|
||||||
$message = trans('messages.success.added', ['type' => trans_choice('general.bills', 1)]);
|
$message = trans('messages.success.added', ['type' => trans_choice('general.bills', 1)]);
|
||||||
|
|
||||||
@ -354,127 +227,7 @@ class Bills extends Controller
|
|||||||
*/
|
*/
|
||||||
public function update(Bill $bill, Request $request)
|
public function update(Bill $bill, Request $request)
|
||||||
{
|
{
|
||||||
$taxes = [];
|
$bill = dispatch(new UpdateBill($bill, $request));
|
||||||
|
|
||||||
$tax_total = 0;
|
|
||||||
$sub_total = 0;
|
|
||||||
$discount_total = 0;
|
|
||||||
$discount = $request['discount'];
|
|
||||||
|
|
||||||
$bill_item = [];
|
|
||||||
$bill_item['company_id'] = $request['company_id'];
|
|
||||||
$bill_item['bill_id'] = $bill->id;
|
|
||||||
|
|
||||||
if ($request['item']) {
|
|
||||||
$this->deleteRelationships($bill, '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 = [];
|
|
||||||
$bill_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));
|
|
||||||
}
|
|
||||||
|
|
||||||
$bill_item_taxes[] = [
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'tax_id' => $tax_id,
|
|
||||||
'name' => $tax_object->name,
|
|
||||||
'amount' => $tax,
|
|
||||||
];
|
|
||||||
|
|
||||||
$item_tax += $tax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$bill_item['item_id'] = $item['item_id'];
|
|
||||||
$bill_item['name'] = str_limit($item['name'], 180, '');
|
|
||||||
$bill_item['sku'] = $item_sku;
|
|
||||||
$bill_item['quantity'] = (double) $item['quantity'];
|
|
||||||
$bill_item['price'] = (double) $item['price'];
|
|
||||||
$bill_item['tax'] = $item_tax;
|
|
||||||
$bill_item['tax_id'] = 0;//$tax_id;
|
|
||||||
$bill_item['total'] = (double) $item['price'] * (double) $item['quantity'];
|
|
||||||
|
|
||||||
$tax_total += $item_tax;
|
|
||||||
$sub_total += $bill_item['total'];
|
|
||||||
|
|
||||||
$bill_item_created = BillItem::create($bill_item);
|
|
||||||
|
|
||||||
if ($bill_item_taxes) {
|
|
||||||
foreach ($bill_item_taxes as $bill_item_tax) {
|
|
||||||
$bill_item_tax['bill_item_id'] = $bill_item_created->id;
|
|
||||||
|
|
||||||
BillItemTax::create($bill_item_tax);
|
|
||||||
|
|
||||||
// Set taxes
|
|
||||||
if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) {
|
|
||||||
$taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount'];
|
|
||||||
} else {
|
|
||||||
$taxes[$bill_item_tax['tax_id']] = [
|
|
||||||
'name' => $bill_item_tax['name'],
|
|
||||||
'amount' => $bill_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;
|
|
||||||
|
|
||||||
$request['amount'] = money($amount, $request['currency_code'])->getAmount();
|
|
||||||
|
|
||||||
$bill->update($request->input());
|
|
||||||
|
|
||||||
// Upload attachment
|
|
||||||
if ($request->file('attachment')) {
|
|
||||||
$media = $this->getMedia($request->file('attachment'), 'bills');
|
|
||||||
|
|
||||||
$bill->attachMedia($media, 'attachment');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete previous bill totals
|
|
||||||
$this->deleteRelationships($bill, 'totals');
|
|
||||||
|
|
||||||
// Add bill totals
|
|
||||||
$this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total);
|
|
||||||
|
|
||||||
// Recurring
|
|
||||||
$bill->updateRecurring();
|
|
||||||
|
|
||||||
// Fire the event to make it extendible
|
|
||||||
event(new BillUpdated($bill));
|
|
||||||
|
|
||||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.bills', 1)]);
|
$message = trans('messages.success.updated', ['type' => trans_choice('general.bills', 1)]);
|
||||||
|
|
||||||
@ -655,20 +408,7 @@ class Bills extends Controller
|
|||||||
|
|
||||||
$bill->save();
|
$bill->save();
|
||||||
|
|
||||||
$bill_payment_request = [
|
$bill_payment = dispatch(new CreateBillPayment($request, $bill));
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $request['bill_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']
|
|
||||||
];
|
|
||||||
|
|
||||||
$bill_payment = BillPayment::create($bill_payment_request);
|
|
||||||
|
|
||||||
// Upload attachment
|
// Upload attachment
|
||||||
if ($request->file('attachment')) {
|
if ($request->file('attachment')) {
|
||||||
@ -677,15 +417,6 @@ class Bills extends Controller
|
|||||||
$bill_payment->attachMedia($media, 'attachment');
|
$bill_payment->attachMedia($media, 'attachment');
|
||||||
}
|
}
|
||||||
|
|
||||||
$request['status_code'] = $bill->bill_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);
|
|
||||||
|
|
||||||
BillHistory::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([
|
||||||
@ -794,64 +525,4 @@ class Bills extends Controller
|
|||||||
|
|
||||||
return $bill;
|
return $bill;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total)
|
|
||||||
{
|
|
||||||
$sort_order = 1;
|
|
||||||
|
|
||||||
// Added bill sub total
|
|
||||||
BillTotal::create([
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'code' => 'sub_total',
|
|
||||||
'name' => 'bills.sub_total',
|
|
||||||
'amount' => $sub_total,
|
|
||||||
'sort_order' => $sort_order,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$sort_order++;
|
|
||||||
|
|
||||||
// Added bill discount
|
|
||||||
if ($discount_total) {
|
|
||||||
BillTotal::create([
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'code' => 'discount',
|
|
||||||
'name' => 'bills.discount',
|
|
||||||
'amount' => $discount_total,
|
|
||||||
'sort_order' => $sort_order,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// This is for total
|
|
||||||
$sub_total = $sub_total - $discount_total;
|
|
||||||
|
|
||||||
$sort_order++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Added bill taxes
|
|
||||||
if (isset($taxes)) {
|
|
||||||
foreach ($taxes as $tax) {
|
|
||||||
BillTotal::create([
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'code' => 'tax',
|
|
||||||
'name' => $tax['name'],
|
|
||||||
'amount' => $tax['amount'],
|
|
||||||
'sort_order' => $sort_order,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$sort_order++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Added bill total
|
|
||||||
BillTotal::create([
|
|
||||||
'company_id' => $request['company_id'],
|
|
||||||
'bill_id' => $bill->id,
|
|
||||||
'code' => 'total',
|
|
||||||
'name' => 'bills.total',
|
|
||||||
'amount' => $sub_total + $tax_total,
|
|
||||||
'sort_order' => $sort_order,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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,128 +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 = 0;
|
|
||||||
$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)]);
|
||||||
|
|
||||||
@ -774,64 +654,4 @@ class Invoices extends Controller
|
|||||||
|
|
||||||
return $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,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,13 @@ class Taxes extends Controller
|
|||||||
*/
|
*/
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
return view('settings.taxes.create');
|
$types = [
|
||||||
|
'normal' => trans('taxes.normal'),
|
||||||
|
'calculate' => trans('taxes.calculate'),
|
||||||
|
'compound' => trans('taxes.compound'),
|
||||||
|
];
|
||||||
|
|
||||||
|
return view('settings.taxes.create', compact('types'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,7 +74,13 @@ class Taxes extends Controller
|
|||||||
*/
|
*/
|
||||||
public function edit(Tax $tax)
|
public function edit(Tax $tax)
|
||||||
{
|
{
|
||||||
return view('settings.taxes.edit', compact('tax'));
|
$types = [
|
||||||
|
'normal' => trans('taxes.normal'),
|
||||||
|
'calculate' => trans('taxes.calculate'),
|
||||||
|
'compound' => trans('taxes.compound'),
|
||||||
|
];
|
||||||
|
|
||||||
|
return view('settings.taxes.edit', compact('tax', 'types'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
170
app/Jobs/Expense/CreateBill.php
Normal file
170
app/Jobs/Expense/CreateBill.php
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Expense;
|
||||||
|
|
||||||
|
use App\Events\BillCreated;
|
||||||
|
use App\Models\Expense\Bill;
|
||||||
|
use App\Models\Expense\BillHistory;
|
||||||
|
use App\Models\Expense\BillTotal;
|
||||||
|
use App\Traits\Currencies;
|
||||||
|
use App\Traits\DateTime;
|
||||||
|
use App\Traits\Uploads;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
|
||||||
|
class CreateBill
|
||||||
|
{
|
||||||
|
use Currencies, DateTime, Dispatchable, 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()
|
||||||
|
{
|
||||||
|
$bill = Bill::create($this->request->input());
|
||||||
|
|
||||||
|
// Upload attachment
|
||||||
|
if ($this->request->file('attachment')) {
|
||||||
|
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||||
|
|
||||||
|
$bill->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) {
|
||||||
|
$bill_item = dispatch(new CreateBillItem($item, $bill, $discount));
|
||||||
|
|
||||||
|
// Calculate totals
|
||||||
|
$tax_total += $bill_item->tax;
|
||||||
|
$sub_total += $bill_item->total;
|
||||||
|
|
||||||
|
// Set taxes
|
||||||
|
foreach ($bill_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;
|
||||||
|
|
||||||
|
$request['amount'] = money($amount, $this->request['currency_code'])->getAmount();
|
||||||
|
|
||||||
|
$bill->update($this->request->input());
|
||||||
|
|
||||||
|
// Add bill totals
|
||||||
|
$this->addTotals($bill, $this->request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||||
|
|
||||||
|
// Add bill history
|
||||||
|
BillHistory::create([
|
||||||
|
'company_id' => session('company_id'),
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'status_code' => 'draft',
|
||||||
|
'notify' => 0,
|
||||||
|
'description' => trans('messages.success.added', ['type' => $bill->bill_number]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Recurring
|
||||||
|
$bill->createRecurring();
|
||||||
|
|
||||||
|
// Fire the event to make it extendible
|
||||||
|
event(new BillCreated($bill));
|
||||||
|
|
||||||
|
return $bill;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total)
|
||||||
|
{
|
||||||
|
$sort_order = 1;
|
||||||
|
|
||||||
|
// Added bill sub total
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'sub_total',
|
||||||
|
'name' => 'bills.sub_total',
|
||||||
|
'amount' => $sub_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
|
||||||
|
// Added bill discount
|
||||||
|
if ($discount_total) {
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'discount',
|
||||||
|
'name' => 'bills.discount',
|
||||||
|
'amount' => $discount_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// This is for total
|
||||||
|
$sub_total = $sub_total - $discount_total;
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added bill taxes
|
||||||
|
if (isset($taxes)) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'tax',
|
||||||
|
'name' => $tax['name'],
|
||||||
|
'amount' => $tax['amount'],
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added bill total
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'total',
|
||||||
|
'name' => 'bills.total',
|
||||||
|
'amount' => $sub_total + $tax_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
202
app/Jobs/Expense/CreateBillItem.php
Normal file
202
app/Jobs/Expense/CreateBillItem.php
Normal file
@ -0,0 +1,202 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Expense;
|
||||||
|
|
||||||
|
use App\Models\Common\Item;
|
||||||
|
use App\Models\Expense\BillItem;
|
||||||
|
use App\Models\Expense\BillItemTax;
|
||||||
|
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 CreateBillItem
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
|
||||||
|
protected $data;
|
||||||
|
|
||||||
|
protected $bill;
|
||||||
|
|
||||||
|
protected $discount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $data
|
||||||
|
* @param $bill
|
||||||
|
* @param $discount
|
||||||
|
*/
|
||||||
|
public function __construct($data, $bill, $discount = null)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
$this->bill = $bill;
|
||||||
|
$this->discount = $discount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return BillItem
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$item_sku = '';
|
||||||
|
|
||||||
|
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||||
|
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||||
|
|
||||||
|
$item_discount_amount = $item_amount;
|
||||||
|
|
||||||
|
// Apply discount to tax
|
||||||
|
if ($this->discount) {
|
||||||
|
$item_discount_amount = $item_amount * ($this->discount / 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($item_id)) {
|
||||||
|
$item_object = Item::find($item_id);
|
||||||
|
|
||||||
|
$this->data['name'] = $item_object->name;
|
||||||
|
$item_sku = $item_object->sku;
|
||||||
|
|
||||||
|
// Increase stock (item bought)
|
||||||
|
$item_object->quantity += $this->data['quantity'];
|
||||||
|
$item_object->save();
|
||||||
|
} elseif (!empty($this->data['sku'])) {
|
||||||
|
$item_sku = $this->data['sku'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$tax_amount = 0;
|
||||||
|
$item_taxes = [];
|
||||||
|
$item_tax_total = 0;
|
||||||
|
|
||||||
|
if (!empty($this->data['tax_id'])) {
|
||||||
|
$calculates = $compounds = $taxes = [];
|
||||||
|
|
||||||
|
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
||||||
|
$tax = Tax::find($tax_id);
|
||||||
|
|
||||||
|
switch ($tax->type) {
|
||||||
|
case 'calculate':
|
||||||
|
$calculates[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'compound':
|
||||||
|
$compounds[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'normal':
|
||||||
|
default:
|
||||||
|
$taxes[] = $tax;
|
||||||
|
|
||||||
|
$tax_amount = ($item_discount_amount / 100) * $tax->rate;
|
||||||
|
|
||||||
|
$item_taxes[] = [
|
||||||
|
'company_id' => $this->bill->company_id,
|
||||||
|
'bill_id' => $this->bill->id,
|
||||||
|
'tax_id' => $tax_id,
|
||||||
|
'name' => $tax->name,
|
||||||
|
'amount' => $tax_amount,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_tax_total += $tax_amount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($calculates) {
|
||||||
|
if ($this->discount) {
|
||||||
|
$item_tax_total = 0;
|
||||||
|
|
||||||
|
if ($taxes) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
$item_tax_amount = ($item_amount / 100) * $tax->rate;
|
||||||
|
|
||||||
|
$item_tax_total += $item_tax_amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_amount + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);
|
||||||
|
|
||||||
|
$item_sub_total = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
|
||||||
|
$item_discount_amount = $item_sub_total - ($item_sub_total * ($this->discount / 100));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_discount_amount + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $tax_amount = $item_sub_and_tax_total - ($item_sub_and_tax_total / (1 + ($calculate->rate / 100)));
|
||||||
|
|
||||||
|
$item_taxes[] = [
|
||||||
|
'company_id' => $this->bill->company_id,
|
||||||
|
'bill_id' => $this->bill->id,
|
||||||
|
'tax_id' => $calculate->id,
|
||||||
|
'name' => $calculate->name,
|
||||||
|
'amount' => $tax_amount,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_amount = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($compounds) {
|
||||||
|
foreach ($compounds as $compound) {
|
||||||
|
$tax_amount = (($item_discount_amount + $item_tax_total) / 100) * $compound->rate;
|
||||||
|
|
||||||
|
$item_tax_total += $tax_amount;
|
||||||
|
|
||||||
|
$item_taxes[] = [
|
||||||
|
'company_id' => $this->bill->company_id,
|
||||||
|
'bill_id' => $this->bill->id,
|
||||||
|
'tax_id' => $compound->id,
|
||||||
|
'name' => $compound->name,
|
||||||
|
'amount' => $tax_amount,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bill_item = BillItem::create([
|
||||||
|
'company_id' => $this->bill->company_id,
|
||||||
|
'bill_id' => $this->bill->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_total,
|
||||||
|
'tax_id' => 0,
|
||||||
|
'total' => $item_amount,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!empty($this->data['tax_id'])) {
|
||||||
|
// set item_taxes for
|
||||||
|
$bill_item->item_taxes = $item_taxes;
|
||||||
|
$bill_item->calculates = $calculates;
|
||||||
|
$bill_item->compounds = $compounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($item_taxes) {
|
||||||
|
foreach ($item_taxes as $item_tax) {
|
||||||
|
$item_tax['bill_item_id'] = $bill_item->id;
|
||||||
|
|
||||||
|
BillItemTax::create($item_tax);
|
||||||
|
|
||||||
|
// Set taxes
|
||||||
|
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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $bill_item;
|
||||||
|
}
|
||||||
|
}
|
52
app/Jobs/Expense/CreateBillPayment.php
Normal file
52
app/Jobs/Expense/CreateBillPayment.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Expense;
|
||||||
|
|
||||||
|
use App\Models\Expense\BillHistory;
|
||||||
|
use App\Models\Expense\BillPayment;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
|
||||||
|
class CreateBillPayment
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
protected $bill;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $request
|
||||||
|
* @param $bill
|
||||||
|
*/
|
||||||
|
public function __construct($request, $bill)
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
$this->bill = $bill;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return BillPayment
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$bill_payment = BillPayment::create($this->request->input());
|
||||||
|
|
||||||
|
$desc_amount = money((float) $bill_payment->amount, (string) $bill_payment->currency_code, true)->format();
|
||||||
|
|
||||||
|
$history_data = [
|
||||||
|
'company_id' => $bill_payment->company_id,
|
||||||
|
'bill_id' => $bill_payment->bill_id,
|
||||||
|
'status_code' => $this->bill->bill_status_code,
|
||||||
|
'notify' => '0',
|
||||||
|
'description' => $desc_amount . ' ' . trans_choice('general.payments', 1),
|
||||||
|
];
|
||||||
|
|
||||||
|
BillHistory::create($history_data);
|
||||||
|
|
||||||
|
return $bill_payment;
|
||||||
|
}
|
||||||
|
}
|
319
app/Jobs/Expense/UpdateBill.php
Normal file
319
app/Jobs/Expense/UpdateBill.php
Normal file
@ -0,0 +1,319 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs\Expense;
|
||||||
|
|
||||||
|
use App\Events\BillUpdated;
|
||||||
|
use App\Models\Expense\Bill;
|
||||||
|
use App\Models\Expense\BillTotal;
|
||||||
|
use App\Traits\Currencies;
|
||||||
|
use App\Traits\DateTime;
|
||||||
|
use App\Traits\Uploads;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
|
||||||
|
class UpdateBill
|
||||||
|
{
|
||||||
|
use Currencies, DateTime, Dispatchable, Uploads;
|
||||||
|
|
||||||
|
protected $bill;
|
||||||
|
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param $request
|
||||||
|
*/
|
||||||
|
public function __construct($bill, $request)
|
||||||
|
{
|
||||||
|
$this->bill = $bill;
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return Bill
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
// Upload attachment
|
||||||
|
if ($this->request->file('attachment')) {
|
||||||
|
$media = $this->getMedia($this->request->file('attachment'), 'bills');
|
||||||
|
|
||||||
|
$this->bill->attachMedia($media, 'attachment');
|
||||||
|
}
|
||||||
|
|
||||||
|
$taxes = [];
|
||||||
|
|
||||||
|
$tax_total = 0;
|
||||||
|
$sub_total = 0;
|
||||||
|
$discount_total = 0;
|
||||||
|
$discount = $this->request['discount'];
|
||||||
|
|
||||||
|
if ($this->request['item']) {
|
||||||
|
$this->deleteRelationships($this->bill, 'items');
|
||||||
|
|
||||||
|
foreach ($this->request['item'] as $item) {
|
||||||
|
$bill_item = dispatch(new CreateBillItem($item, $this->bill, $discount));
|
||||||
|
|
||||||
|
// Calculate totals
|
||||||
|
$tax_total += $bill_item->tax;
|
||||||
|
$sub_total += $bill_item->total;
|
||||||
|
|
||||||
|
// Set taxes
|
||||||
|
foreach ($bill_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->bill->update($this->request->input());
|
||||||
|
|
||||||
|
// Delete previous bill totals
|
||||||
|
$this->deleteRelationships($this->bill, 'totals');
|
||||||
|
|
||||||
|
// Add bill totals
|
||||||
|
$this->addTotals($this->bill, $this->request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||||
|
|
||||||
|
// Recurring
|
||||||
|
$this->bill->updateRecurring();
|
||||||
|
|
||||||
|
// Fire the event to make it extensible
|
||||||
|
event(new BillUpdated($this->bill));
|
||||||
|
|
||||||
|
return $this->bill;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total)
|
||||||
|
{
|
||||||
|
$sort_order = 1;
|
||||||
|
|
||||||
|
// Added bill sub total
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'sub_total',
|
||||||
|
'name' => 'bills.sub_total',
|
||||||
|
'amount' => $sub_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
|
||||||
|
// Added bill discount
|
||||||
|
if ($discount_total) {
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'discount',
|
||||||
|
'name' => 'bills.discount',
|
||||||
|
'amount' => $discount_total,
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// This is for total
|
||||||
|
$sub_total = $sub_total - $discount_total;
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added bill taxes
|
||||||
|
if (isset($taxes)) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'tax',
|
||||||
|
'name' => $tax['name'],
|
||||||
|
'amount' => $tax['amount'],
|
||||||
|
'sort_order' => $sort_order,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sort_order++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added bill total
|
||||||
|
BillTotal::create([
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'code' => 'total',
|
||||||
|
'name' => 'bills.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function test() {
|
||||||
|
|
||||||
|
$taxes = [];
|
||||||
|
|
||||||
|
$tax_total = 0;
|
||||||
|
$sub_total = 0;
|
||||||
|
$discount_total = 0;
|
||||||
|
$discount = $request['discount'];
|
||||||
|
|
||||||
|
$bill_item = [];
|
||||||
|
$bill_item['company_id'] = $request['company_id'];
|
||||||
|
$bill_item['bill_id'] = $bill->id;
|
||||||
|
|
||||||
|
if ($request['item']) {
|
||||||
|
$this->deleteRelationships($bill, '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 = [];
|
||||||
|
$bill_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));
|
||||||
|
}
|
||||||
|
|
||||||
|
$bill_item_taxes[] = [
|
||||||
|
'company_id' => $request['company_id'],
|
||||||
|
'bill_id' => $bill->id,
|
||||||
|
'tax_id' => $tax_id,
|
||||||
|
'name' => $tax_object->name,
|
||||||
|
'amount' => $tax,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_tax += $tax;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bill_item['item_id'] = $item['item_id'];
|
||||||
|
$bill_item['name'] = str_limit($item['name'], 180, '');
|
||||||
|
$bill_item['sku'] = $item_sku;
|
||||||
|
$bill_item['quantity'] = (double) $item['quantity'];
|
||||||
|
$bill_item['price'] = (double) $item['price'];
|
||||||
|
$bill_item['tax'] = $item_tax;
|
||||||
|
$bill_item['tax_id'] = 0;//$tax_id;
|
||||||
|
$bill_item['total'] = (double) $item['price'] * (double) $item['quantity'];
|
||||||
|
|
||||||
|
$tax_total += $item_tax;
|
||||||
|
$sub_total += $bill_item['total'];
|
||||||
|
|
||||||
|
$bill_item_created = BillItem::create($bill_item);
|
||||||
|
|
||||||
|
if ($bill_item_taxes) {
|
||||||
|
foreach ($bill_item_taxes as $bill_item_tax) {
|
||||||
|
$bill_item_tax['bill_item_id'] = $bill_item_created->id;
|
||||||
|
|
||||||
|
BillItemTax::create($bill_item_tax);
|
||||||
|
|
||||||
|
// Set taxes
|
||||||
|
if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) {
|
||||||
|
$taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount'];
|
||||||
|
} else {
|
||||||
|
$taxes[$bill_item_tax['tax_id']] = [
|
||||||
|
'name' => $bill_item_tax['name'],
|
||||||
|
'amount' => $bill_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;
|
||||||
|
|
||||||
|
$request['amount'] = money($amount, $request['currency_code'])->getAmount();
|
||||||
|
|
||||||
|
$bill->update($request->input());
|
||||||
|
|
||||||
|
// Upload attachment
|
||||||
|
if ($request->file('attachment')) {
|
||||||
|
$media = $this->getMedia($request->file('attachment'), 'bills');
|
||||||
|
|
||||||
|
$bill->attachMedia($media, 'attachment');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete previous bill totals
|
||||||
|
$this->deleteRelationships($bill, 'totals');
|
||||||
|
|
||||||
|
// Add bill totals
|
||||||
|
$this->addTotals($bill, $request, $taxes, $sub_total, $discount_total, $tax_total);
|
||||||
|
|
||||||
|
// Recurring
|
||||||
|
$bill->updateRecurring();
|
||||||
|
|
||||||
|
// Fire the event to make it extendible
|
||||||
|
event(new BillUpdated($bill));
|
||||||
|
}
|
||||||
|
}
|
@ -58,6 +58,18 @@ class CreateInvoice
|
|||||||
// Calculate totals
|
// Calculate totals
|
||||||
$tax_total += $invoice_item->tax;
|
$tax_total += $invoice_item->tax;
|
||||||
$sub_total += $invoice_item->total;
|
$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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,4 +171,4 @@ class CreateInvoice
|
|||||||
'sort_order' => $sort_order,
|
'sort_order' => $sort_order,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,14 @@ class CreateInvoiceItem
|
|||||||
$item_sku = '';
|
$item_sku = '';
|
||||||
|
|
||||||
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
$item_id = !empty($this->data['item_id']) ? $this->data['item_id'] : 0;
|
||||||
|
$item_amount = (double) $this->data['price'] * (double) $this->data['quantity'];
|
||||||
|
|
||||||
|
$item_discount_amount = $item_amount;
|
||||||
|
|
||||||
|
// Apply discount to tax
|
||||||
|
if ($this->discount) {
|
||||||
|
$item_discount_amount = $item_amount * ($this->discount / 100);
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($item_id)) {
|
if (!empty($item_id)) {
|
||||||
$item_object = Item::find($item_id);
|
$item_object = Item::find($item_id);
|
||||||
@ -85,32 +93,96 @@ class CreateInvoiceItem
|
|||||||
$item_sku = $this->data['sku'];
|
$item_sku = $this->data['sku'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$item_tax = 0;
|
$tax_amount = 0;
|
||||||
$item_taxes = [];
|
$item_taxes = [];
|
||||||
$invoice_item_taxes = [];
|
$item_tax_total = 0;
|
||||||
|
|
||||||
if (!empty($this->data['tax_id'])) {
|
if (!empty($this->data['tax_id'])) {
|
||||||
|
$calculates = $compounds = $taxes = [];
|
||||||
|
|
||||||
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
foreach ((array) $this->data['tax_id'] as $tax_id) {
|
||||||
$tax_object = Tax::find($tax_id);
|
$tax = Tax::find($tax_id);
|
||||||
|
|
||||||
$item_taxes[] = $tax_id;
|
switch ($tax->type) {
|
||||||
|
case 'calculate':
|
||||||
|
$calculates[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'compound':
|
||||||
|
$compounds[] = $tax;
|
||||||
|
break;
|
||||||
|
case 'normal':
|
||||||
|
default:
|
||||||
|
$taxes[] = $tax;
|
||||||
|
|
||||||
$tax = (((double) $this->data['price'] * (double) $this->data['quantity']) / 100) * $tax_object->rate;
|
$tax_amount = ($item_discount_amount / 100) * $tax->rate;
|
||||||
|
|
||||||
// Apply discount to tax
|
$item_taxes[] = [
|
||||||
if ($this->discount) {
|
'company_id' => $this->invoice->company_id,
|
||||||
$tax = $tax - ($tax * ($this->discount / 100));
|
'invoice_id' => $this->invoice->id,
|
||||||
|
'tax_id' => $tax_id,
|
||||||
|
'name' => $tax->name,
|
||||||
|
'amount' => $tax_amount,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_tax_total += $tax_amount;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$invoice_item_taxes[] = [
|
if ($calculates) {
|
||||||
'company_id' => $this->invoice->company_id,
|
if ($this->discount) {
|
||||||
'invoice_id' => $this->invoice->id,
|
$item_tax_total = 0;
|
||||||
'tax_id' => $tax_id,
|
|
||||||
'name' => $tax_object->name,
|
|
||||||
'amount' => $tax,
|
|
||||||
];
|
|
||||||
|
|
||||||
$item_tax += $tax;
|
if ($taxes) {
|
||||||
|
foreach ($taxes as $tax) {
|
||||||
|
$item_tax_amount = ($item_amount / 100) * $tax->rate;
|
||||||
|
|
||||||
|
$item_tax_total += $item_tax_amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_amount + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);
|
||||||
|
|
||||||
|
$item_sub_total = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
|
||||||
|
$item_discount_amount = $item_sub_total - ($item_sub_total * ($this->discount / 100));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($calculates as $calculate) {
|
||||||
|
$item_sub_and_tax_total = $item_discount_amount + $item_tax_total;
|
||||||
|
|
||||||
|
$item_tax_total = $tax_amount = $item_sub_and_tax_total - ($item_sub_and_tax_total / (1 + ($calculate->rate / 100)));
|
||||||
|
|
||||||
|
$item_taxes[] = [
|
||||||
|
'company_id' => $this->invoice->company_id,
|
||||||
|
'invoice_id' => $this->invoice->id,
|
||||||
|
'tax_id' => $calculate->id,
|
||||||
|
'name' => $calculate->name,
|
||||||
|
'amount' => $tax_amount,
|
||||||
|
];
|
||||||
|
|
||||||
|
$item_amount = $item_sub_and_tax_total - $item_tax_total;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($compounds) {
|
||||||
|
foreach ($compounds as $compound) {
|
||||||
|
$tax_amount = (($item_discount_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,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,24 +194,31 @@ class CreateInvoiceItem
|
|||||||
'sku' => $item_sku,
|
'sku' => $item_sku,
|
||||||
'quantity' => (double) $this->data['quantity'],
|
'quantity' => (double) $this->data['quantity'],
|
||||||
'price' => (double) $this->data['price'],
|
'price' => (double) $this->data['price'],
|
||||||
'tax' => $item_tax,
|
'tax' => $item_tax_total,
|
||||||
'tax_id' => 0, // (int) $item_taxes;
|
'tax_id' => 0,
|
||||||
'total' => (double) $this->data['price'] * (double) $this->data['quantity'],
|
'total' => $item_amount,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($invoice_item_taxes) {
|
if (!empty($this->data['tax_id'])) {
|
||||||
foreach ($invoice_item_taxes as $invoice_item_tax) {
|
// set item_taxes for
|
||||||
$invoice_item_tax['invoice_item_id'] = $invoice_item->id;
|
$invoice_item->item_taxes = $item_taxes;
|
||||||
|
$invoice_item->calculates = $calculates;
|
||||||
|
$invoice_item->compounds = $compounds;
|
||||||
|
}
|
||||||
|
|
||||||
InvoiceItemTax::create($invoice_item_tax);
|
if ($item_taxes) {
|
||||||
|
foreach ($item_taxes as $item_tax) {
|
||||||
|
$item_tax['invoice_item_id'] = $invoice_item->id;
|
||||||
|
|
||||||
|
InvoiceItemTax::create($item_tax);
|
||||||
|
|
||||||
// Set taxes
|
// Set taxes
|
||||||
if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) {
|
if (isset($taxes) && array_key_exists($item_tax['tax_id'], $taxes)) {
|
||||||
$taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount'];
|
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
|
||||||
} else {
|
} else {
|
||||||
$taxes[$invoice_item_tax['tax_id']] = [
|
$taxes[$item_tax['tax_id']] = [
|
||||||
'name' => $invoice_item_tax['name'],
|
'name' => $item_tax['name'],
|
||||||
'amount' => $invoice_item_tax['amount']
|
'amount' => $item_tax['amount']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,4 +226,4 @@ class CreateInvoiceItem
|
|||||||
|
|
||||||
return $invoice_item;
|
return $invoice_item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -212,6 +212,10 @@ class Invoice extends Model
|
|||||||
*/
|
*/
|
||||||
public function getPaidAttribute()
|
public function getPaidAttribute()
|
||||||
{
|
{
|
||||||
|
if (empty($this->amount)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$paid = 0;
|
$paid = 0;
|
||||||
$reconciled = $reconciled_amount = 0;
|
$reconciled = $reconciled_amount = 0;
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class Tax extends Model
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fillable = ['company_id', 'name', 'rate', 'enabled'];
|
protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sortable columns.
|
* Sortable columns.
|
||||||
|
32
database/migrations/2018_11_05_000000_add_tax_columns.php
Normal file
32
database/migrations/2018_11_05_000000_add_tax_columns.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class AddTaxColumns extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('taxes', function ($table) {
|
||||||
|
$table->string('type')->default('normal');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('taxes', function ($table) {
|
||||||
|
$table->dropColumn([
|
||||||
|
'type',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -4,5 +4,8 @@ return [
|
|||||||
|
|
||||||
'rate' => 'Rate',
|
'rate' => 'Rate',
|
||||||
'rate_percent' => 'Rate (%)',
|
'rate_percent' => 'Rate (%)',
|
||||||
|
'normal' => 'Normal',
|
||||||
|
'calculate' => 'Calculate as VAT/GST',
|
||||||
|
'compound' => 'Compound',
|
||||||
|
|
||||||
];
|
];
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}
|
{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}
|
||||||
|
|
||||||
|
{{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', $types, null, []) }}
|
||||||
|
|
||||||
{{ Form::radioGroup('enabled', trans('general.enabled')) }}
|
{{ Form::radioGroup('enabled', trans('general.enabled')) }}
|
||||||
</div>
|
</div>
|
||||||
<!-- /.box-body -->
|
<!-- /.box-body -->
|
||||||
@ -34,6 +36,10 @@
|
|||||||
$('#enabled_1').trigger('click');
|
$('#enabled_1').trigger('click');
|
||||||
|
|
||||||
$('#name').focus();
|
$('#name').focus();
|
||||||
|
|
||||||
|
$("#type").select2({
|
||||||
|
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.types', 1)]) }}"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@endpush
|
@endpush
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}
|
{{ Form::textGroup('rate', trans('taxes.rate'), 'percent') }}
|
||||||
|
|
||||||
|
{{ Form::selectGroup('type', trans('general.type'), 'bars', $types, null, []) }}
|
||||||
|
|
||||||
{{ Form::radioGroup('enabled', trans('general.enabled')) }}
|
{{ Form::radioGroup('enabled', trans('general.enabled')) }}
|
||||||
</div>
|
</div>
|
||||||
<!-- /.box-body -->
|
<!-- /.box-body -->
|
||||||
@ -36,5 +38,13 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var text_yes = '{{ trans('general.yes') }}';
|
var text_yes = '{{ trans('general.yes') }}';
|
||||||
var text_no = '{{ trans('general.no') }}';
|
var text_no = '{{ trans('general.no') }}';
|
||||||
|
|
||||||
|
$(document).ready(function() {
|
||||||
|
$('#name').focus();
|
||||||
|
|
||||||
|
$("#type").select2({
|
||||||
|
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.types', 1)]) }}"
|
||||||
|
});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
@endpush
|
@endpush
|
||||||
|
Loading…
x
Reference in New Issue
Block a user