close #46 Enhancement: Bill and Invoice improve total status and system

This commit is contained in:
cuneytsenturk 2017-10-11 19:52:35 +03:00
parent 40dea7fa99
commit d36bcaaf13
14 changed files with 716 additions and 148 deletions

View File

@ -11,8 +11,9 @@ use App\Models\Banking\Account;
use App\Models\Expense\BillStatus; use App\Models\Expense\BillStatus;
use App\Models\Expense\Vendor; use App\Models\Expense\Vendor;
use App\Models\Expense\Bill; use App\Models\Expense\Bill;
use App\Models\Expense\BillHistory;
use App\Models\Expense\BillItem; use App\Models\Expense\BillItem;
use App\Models\Expense\BillTotal;
use App\Models\Expense\BillHistory;
use App\Models\Expense\BillPayment; use App\Models\Expense\BillPayment;
use App\Models\Item\Item; use App\Models\Item\Item;
use App\Models\Setting\Category; use App\Models\Setting\Category;
@ -21,9 +22,8 @@ use App\Models\Setting\Tax;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\Uploads; use App\Traits\Uploads;
use Jenssegers\Date\Date;
use App\Utilities\Modules; use App\Utilities\Modules;
use Date;
class Bills extends Controller class Bills extends Controller
{ {
@ -36,7 +36,7 @@ class Bills extends Controller
*/ */
public function index() public function index()
{ {
$bills = Bill::with('status')->collect(); $bills = Bill::with(['vendor', 'status', 'items', 'payments', 'histories'])->collect();
$vendors = collect(Vendor::enabled()->pluck('name', 'id')) $vendors = collect(Vendor::enabled()->pluck('name', 'id'))
->prepend(trans('general.all_type', ['type' => trans_choice('general.vendors', 2)]), ''); ->prepend(trans('general.all_type', ['type' => trans_choice('general.vendors', 2)]), '');
@ -56,25 +56,15 @@ class Bills extends Controller
*/ */
public function show(Bill $bill) public function show(Bill $bill)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
foreach ($bill->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($bill->payments as $item) { foreach ($bill->payments as $item) {
$item->default_currency_code = $bill->currency_code; $item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid; $bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
$accounts = Account::enabled()->pluck('name', 'id'); $accounts = Account::enabled()->pluck('name', 'id');
@ -100,27 +90,17 @@ class Bills extends Controller
*/ */
public function printBill($bill_id) public function printBill($bill_id)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
$bill = Bill::where('id', $bill_id)->first(); $bill = Bill::where('id', $bill_id)->first();
foreach ($bill->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($bill->payments as $item) { foreach ($bill->payments as $item) {
$item->default_currency_code = $bill->currency_code; $item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid; $bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
return view('expenses.bills.bill', compact('bill')); return view('expenses.bills.bill', compact('bill'));
} }
@ -134,27 +114,17 @@ class Bills extends Controller
*/ */
public function pdfBill($bill_id) public function pdfBill($bill_id)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
$bill = Bill::where('id', $bill_id)->first(); $bill = Bill::where('id', $bill_id)->first();
foreach ($bill->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($bill->payments as $item) { foreach ($bill->payments as $item) {
$item->default_currency_code = $bill->currency_code; $item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid; $bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
$html = view('expenses.bills.bill', compact('bill'))->render(); $html = view('expenses.bills.bill', compact('bill'))->render();
@ -286,18 +256,24 @@ class Bills extends Controller
// Upload attachment // Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills'); $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) { if ($attachment_path) {
$request['attachment'] = $attachment_path; $request['attachment'] = $attachment_path;
} }
$bill = Bill::create($request->input()); $bill = Bill::create($request->input());
$bill_item = array(); $taxes = [];
$tax_total = 0;
$sub_total = 0;
$bill_item = [];
$bill_item['company_id'] = $request['company_id']; $bill_item['company_id'] = $request['company_id'];
$bill_item['bill_id'] = $bill->id; $bill_item['bill_id'] = $bill->id;
if ($request['item']) { if ($request['item']) {
foreach ($request['item'] as $item) { foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = ''; $item_sku = '';
if (!empty($item['item_id'])) { if (!empty($item['item_id'])) {
@ -323,16 +299,74 @@ class Bills extends Controller
$bill_item['price'] = $item['price']; $bill_item['price'] = $item['price'];
$bill_item['tax'] = $tax; $bill_item['tax'] = $tax;
$bill_item['tax_id'] = $tax_id; $bill_item['tax_id'] = $tax_id;
$bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity']; $bill_item['total'] = $item['price'] * $item['quantity'];
$request['amount'] += $bill_item['total']; 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 += $bill_item['total'];
BillItem::create($bill_item); BillItem::create($bill_item);
} }
} }
$request['amount'] += $sub_total + $tax_total;
$bill->update($request->input()); $bill->update($request->input());
// Added bill total sub total
$bill_sub_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'sub_total',
'name' => 'bills.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
BillTotal::create($bill_sub_total);
$sort_order = 2;
// Added bill total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$bill_tax_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
BillTotal::create($bill_tax_total);
$sort_order++;
}
}
// Added bill total total
$bill_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
BillTotal::create($bill_total);
$request['bill_id'] = $bill->id; $request['bill_id'] = $bill->id;
$request['status_code'] = 'new'; $request['status_code'] = 'new';
$request['notify'] = 0; $request['notify'] = 0;
@ -401,11 +435,16 @@ class Bills extends Controller
// Upload attachment // Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills'); $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) { if ($attachment_path) {
$request['attachment'] = $attachment_path; $request['attachment'] = $attachment_path;
} }
$bill_item = array(); $taxes = [];
$tax_total = 0;
$sub_total = 0;
$bill_item = [];
$bill_item['company_id'] = $request['company_id']; $bill_item['company_id'] = $request['company_id'];
$bill_item['bill_id'] = $bill->id; $bill_item['bill_id'] = $bill->id;
@ -413,6 +452,7 @@ class Bills extends Controller
BillItem::where('bill_id', $bill->id)->delete(); BillItem::where('bill_id', $bill->id)->delete();
foreach ($request['item'] as $item) { foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = ''; $item_sku = '';
if (!empty($item['item_id'])) { if (!empty($item['item_id'])) {
@ -438,16 +478,76 @@ class Bills extends Controller
$bill_item['price'] = $item['price']; $bill_item['price'] = $item['price'];
$bill_item['tax'] = $tax; $bill_item['tax'] = $tax;
$bill_item['tax_id'] = $tax_id; $bill_item['tax_id'] = $tax_id;
$bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity']; $bill_item['total'] = $item['price'] * $item['quantity'];
$request['amount'] += $bill_item['total']; 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 += $bill_item['total'];
BillItem::create($bill_item); BillItem::create($bill_item);
} }
} }
BillTotal::where('bill_id', $bill->id)->delete();
// Added bill total sub total
$bill_sub_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'sub_total',
'name' => 'bills.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
BillTotal::create($bill_sub_total);
$sort_order = 2;
// Added bill total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$bill_tax_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
BillTotal::create($bill_tax_total);
$sort_order++;
}
}
$request['amount'] += $sub_total + $tax_total;
$bill->update($request->input()); $bill->update($request->input());
// Added bill total total
$bill_total = [
'company_id' => $request['company_id'],
'bill_id' => $bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
BillTotal::create($bill_total);
// Fire the event to make it extendible // Fire the event to make it extendible
event(new BillUpdated($bill)); event(new BillUpdated($bill));
@ -476,6 +576,7 @@ class Bills extends Controller
*/ */
BillItem::where('bill_id', $bill->id)->delete(); BillItem::where('bill_id', $bill->id)->delete();
BillTotal::where('bill_id', $bill->id)->delete();
BillPayment::where('bill_id', $bill->id)->delete(); BillPayment::where('bill_id', $bill->id)->delete();
BillHistory::where('bill_id', $bill->id)->delete(); BillHistory::where('bill_id', $bill->id)->delete();

View File

@ -13,6 +13,7 @@ use App\Models\Income\Customer;
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;
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\Item\Item; use App\Models\Item\Item;
@ -22,10 +23,8 @@ use App\Models\Setting\Tax;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\Uploads; use App\Traits\Uploads;
use Jenssegers\Date\Date;
use App\Utilities\Modules; use App\Utilities\Modules;
use Date;
class Invoices extends Controller class Invoices extends Controller
{ {
@ -38,7 +37,7 @@ class Invoices extends Controller
*/ */
public function index() public function index()
{ {
$invoices = Invoice::with('status')->collect(); $invoices = Invoice::with(['customer', 'status', 'items', 'payments', 'histories'])->collect();
$customers = collect(Customer::enabled()->pluck('name', 'id')) $customers = collect(Customer::enabled()->pluck('name', 'id'))
->prepend(trans('general.all_type', ['type' => trans_choice('general.customers', 2)]), ''); ->prepend(trans('general.all_type', ['type' => trans_choice('general.customers', 2)]), '');
@ -58,25 +57,15 @@ class Invoices extends Controller
*/ */
public function show(Invoice $invoice) public function show(Invoice $invoice)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
foreach ($invoice->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($invoice->payments as $item) { foreach ($invoice->payments as $item) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid; $invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$accounts = Account::enabled()->pluck('name', 'id'); $accounts = Account::enabled()->pluck('name', 'id');
@ -102,27 +91,17 @@ class Invoices extends Controller
*/ */
public function printInvoice($invoice_id) public function printInvoice($invoice_id)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
$invoice = Invoice::where('id', $invoice_id)->first(); $invoice = Invoice::where('id', $invoice_id)->first();
foreach ($invoice->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($invoice->payments as $item) { foreach ($invoice->payments as $item) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid; $invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$invoice->template_path = 'incomes.invoices.invoice'; $invoice->template_path = 'incomes.invoices.invoice';
@ -140,27 +119,17 @@ class Invoices extends Controller
*/ */
public function pdfInvoice($invoice_id) public function pdfInvoice($invoice_id)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
$invoice = Invoice::where('id', $invoice_id)->first(); $invoice = Invoice::where('id', $invoice_id)->first();
foreach ($invoice->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($invoice->payments as $item) { foreach ($invoice->payments as $item) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid; $invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$invoice->template_path = 'incomes.invoices.invoice'; $invoice->template_path = 'incomes.invoices.invoice';
@ -296,18 +265,24 @@ class Invoices extends Controller
// Upload attachment // Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices'); $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) { if ($attachment_path) {
$request['attachment'] = $attachment_path; $request['attachment'] = $attachment_path;
} }
$invoice = Invoice::create($request->input()); $invoice = Invoice::create($request->input());
$invoice_item = array(); $taxes = [];
$tax_total = 0;
$sub_total = 0;
$invoice_item = [];
$invoice_item['company_id'] = $request['company_id']; $invoice_item['company_id'] = $request['company_id'];
$invoice_item['invoice_id'] = $invoice->id; $invoice_item['invoice_id'] = $invoice->id;
if ($request['item']) { if ($request['item']) {
foreach ($request['item'] as $item) { foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = ''; $item_sku = '';
if (!empty($item['item_id'])) { if (!empty($item['item_id'])) {
@ -333,16 +308,74 @@ class Invoices extends Controller
$invoice_item['price'] = $item['price']; $invoice_item['price'] = $item['price'];
$invoice_item['tax'] = $tax; $invoice_item['tax'] = $tax;
$invoice_item['tax_id'] = $tax_id; $invoice_item['tax_id'] = $tax_id;
$invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity']; $invoice_item['total'] = $item['price'] * $item['quantity'];
$request['amount'] += $invoice_item['total']; 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'];
InvoiceItem::create($invoice_item); InvoiceItem::create($invoice_item);
} }
} }
$request['amount'] += $sub_total + $tax_total;
$invoice->update($request->input()); $invoice->update($request->input());
// Added invoice total sub total
$invoice_sub_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'sub_total',
'name' => 'invoices.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
InvoiceTotal::create($invoice_sub_total);
$sort_order = 2;
// Added invoice total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$invoice_tax_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_tax_total);
$sort_order++;
}
}
// Added invoice total total
$invoice_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'total',
'name' => 'invoices.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_total);
$request['invoice_id'] = $invoice->id; $request['invoice_id'] = $invoice->id;
$request['status_code'] = 'draft'; $request['status_code'] = 'draft';
$request['notify'] = 0; $request['notify'] = 0;
@ -411,11 +444,16 @@ class Invoices extends Controller
// Upload attachment // Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices'); $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) { if ($attachment_path) {
$request['attachment'] = $attachment_path; $request['attachment'] = $attachment_path;
} }
$invoice_item = array(); $taxes = [];
$tax_total = 0;
$sub_total = 0;
$invoice_item = [];
$invoice_item['company_id'] = $request['company_id']; $invoice_item['company_id'] = $request['company_id'];
$invoice_item['invoice_id'] = $invoice->id; $invoice_item['invoice_id'] = $invoice->id;
@ -423,6 +461,7 @@ class Invoices extends Controller
InvoiceItem::where('invoice_id', $invoice->id)->delete(); InvoiceItem::where('invoice_id', $invoice->id)->delete();
foreach ($request['item'] as $item) { foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = ''; $item_sku = '';
if (!empty($item['item_id'])) { if (!empty($item['item_id'])) {
@ -448,16 +487,76 @@ class Invoices extends Controller
$invoice_item['price'] = $item['price']; $invoice_item['price'] = $item['price'];
$invoice_item['tax'] = $tax; $invoice_item['tax'] = $tax;
$invoice_item['tax_id'] = $tax_id; $invoice_item['tax_id'] = $tax_id;
$invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity']; $invoice_item['total'] = $item['price'] * $item['quantity'];
$request['amount'] += $invoice_item['total']; 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'];
InvoiceItem::create($invoice_item); InvoiceItem::create($invoice_item);
} }
} }
$request['amount'] += $sub_total + $tax_total;
$invoice->update($request->input()); $invoice->update($request->input());
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
// Added invoice total sub total
$invoice_sub_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'sub_total',
'name' => 'invoices.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
InvoiceTotal::create($invoice_sub_total);
$sort_order = 2;
// Added invoice total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$invoice_tax_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_tax_total);
$sort_order++;
}
}
// Added invoice total total
$invoice_total = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'total',
'name' => 'invoices.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_total);
// Fire the event to make it extendible // Fire the event to make it extendible
event(new InvoiceUpdated($invoice)); event(new InvoiceUpdated($invoice));
@ -486,6 +585,7 @@ class Invoices extends Controller
*/ */
InvoiceItem::where('invoice_id', $invoice->id)->delete(); InvoiceItem::where('invoice_id', $invoice->id)->delete();
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
InvoicePayment::where('invoice_id', $invoice->id)->delete(); InvoicePayment::where('invoice_id', $invoice->id)->delete();
InvoiceHistory::where('invoice_id', $invoice->id)->delete(); InvoiceHistory::where('invoice_id', $invoice->id)->delete();

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Transformers\Expense;
use App\Models\Expense\BillTotal as Model;
use League\Fractal\TransformerAbstract;
class BillTotals extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'company_id' => $model->company_id,
'bill_id' => $model->bill_id,
'code' => $model->code,
'name' => $model->name,
'amount' => $model->amount,
'sort_order' => $model->sort_order,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Transformers\Income;
use App\Models\Income\InvoiceTotal as Model;
use League\Fractal\TransformerAbstract;
class InvoiceTotals extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'company_id' => $model->company_id,
'invoice_id' => $model->invoice_id,
'code' => $model->code,
'name' => $model->name,
'amount' => $model->amount,
'sort_order' => $model->sort_order,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
}

View File

@ -69,6 +69,11 @@ class Bill extends Model
return $this->hasMany('App\Models\Expense\BillItem'); return $this->hasMany('App\Models\Expense\BillItem');
} }
public function totals()
{
return $this->hasMany('App\Models\Income\InvoiceTotal');
}
public function payment() public function payment()
{ {
return $this->belongsTo('App\Models\Expense\BillPayment', 'id', 'bill_id'); return $this->belongsTo('App\Models\Expense\BillPayment', 'id', 'bill_id');

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\DateTime;
class BillTotal extends Model
{
use DateTime;
protected $table = 'bill_totals';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'code', 'name', 'amount', 'sort_order'];
public function bill()
{
return $this->belongsTo('App\Models\Expense\Bill');
}
}

View File

@ -79,6 +79,11 @@ class Invoice extends Model
return $this->hasMany('App\Models\Income\InvoiceItem'); return $this->hasMany('App\Models\Income\InvoiceItem');
} }
public function totals()
{
return $this->hasMany('App\Models\Income\InvoiceTotal');
}
public function payments() public function payments()
{ {
return $this->hasMany('App\Models\Income\InvoicePayment'); return $this->hasMany('App\Models\Income\InvoicePayment');

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Income;
use App\Models\Model;
use App\Traits\DateTime;
class InvoiceTotal extends Model
{
use DateTime;
protected $table = 'invoice_totals';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'code', 'name', 'amount', 'sort_order'];
public function invoice()
{
return $this->belongsTo('App\Models\Income\Invoice');
}
}

View File

@ -0,0 +1,135 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Company\Company;
use App\Models\Expense\Bill;
use App\Models\Expense\BillItem;
use App\Models\Expense\BillTotal;
use App\Models\Setting\Tax;
class CreateBillTotalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bill_totals', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->integer('bill_id');
$table->string('code')->nullable();
$table->string('name');
$table->float('amount', 15, 4);
$table->integer('sort_order');
$table->timestamps();
$table->softDeletes();
$table->index('company_id');
});
Model::unguard();
$companies = Company::all();
foreach ($companies as $company) {
$bills = Bill::where('company_id', $company->id)->get();
foreach ($bills as $bill) {
$bill_items = BillItem::where('bill_id', $bill->id)->get();
$taxes = [];
$tax_total = 0;
$sub_total = 0;
foreach ($bill_items as $bill_item) {
unset($tax_object);
$bill_item->total = $bill_item->price * $bill_item->quantity;
$bill_item->update();
if (!empty($bill_item->tax_id)) {
$tax_object = Tax::find($bill_item->tax_id);
}
if (isset($tax_object)) {
if (array_key_exists($bill_item->tax_id, $taxes)) {
$taxes[$bill_item->tax_id]['amount'] += $bill_item->tax;
} else {
$taxes[$bill_item->tax_id] = [
'name' => $tax_object->name,
'amount' => $bill_item->tax
];
}
}
$tax_total += $bill_item->tax;
$sub_total += $bill_item->price * $bill_item->quantity;
}
$bill->amount = $sub_total + $tax_total;
$bill->update();
// Added bill total sub total
$bill_sub_total = [
'company_id' => $company->id,
'bill_id' => $bill->id,
'code' => 'sub_total',
'name' => 'bills.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
BillTotal::create($bill_sub_total);
$sort_order = 2;
// Added bill total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$bill_tax_total = [
'company_id' => $company->id,
'bill_id' => $bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
BillTotal::create($bill_tax_total);
$sort_order++;
}
}
// Added bill total total
$bill_total = [
'company_id' => $company->id,
'bill_id' => $bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
BillTotal::create($bill_total);
}
}
Model::reguard();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('bill_totals');
}
}

View File

@ -0,0 +1,135 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Company\Company;
use App\Models\Income\Invoice;
use App\Models\Income\InvoiceItem;
use App\Models\Income\InvoiceTotal;
use App\Models\Setting\Tax;
class CreateInvoiceTotalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('invoice_totals', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->integer('invoice_id');
$table->string('code')->nullable();
$table->string('name');
$table->float('amount', 15, 4);
$table->integer('sort_order');
$table->timestamps();
$table->softDeletes();
$table->index('company_id');
});
Model::unguard();
$companies = Company::all();
foreach ($companies as $company) {
$invoices = Invoice::where('company_id', $company->id)->get();
foreach ($invoices as $invoice) {
$invoice_items = InvoiceItem::where('invoice_id', $invoice->id)->get();
$taxes = [];
$tax_total = 0;
$sub_total = 0;
foreach ($invoice_items as $invoice_item) {
unset($tax_object);
$invoice_item->total = $invoice_item->price * $invoice_item->quantity;
$invoice_item->update();
if (!empty($invoice_item->tax_id)) {
$tax_object = Tax::find($invoice_item->tax_id);
}
if (isset($tax_object)) {
if (array_key_exists($invoice_item->tax_id, $taxes)) {
$taxes[$invoice_item->tax_id]['amount'] += $invoice_item->tax;
} else {
$taxes[$invoice_item->tax_id] = [
'name' => $tax_object->name,
'amount' => $invoice_item->tax
];
}
}
$tax_total += $invoice_item->tax;
$sub_total += $invoice_item->price * $invoice_item->quantity;
}
$invoice->amount = $sub_total + $tax_total;
$invoice->update();
// Added invoice total sub total
$invoice_sub_total = [
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'code' => 'sub_total',
'name' => 'invoices.sub_total',
'amount' => $sub_total,
'sort_order' => 1,
];
InvoiceTotal::create($invoice_sub_total);
$sort_order = 2;
// Added invoice total taxes
if ($taxes) {
foreach ($taxes as $tax) {
$invoice_tax_total = [
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_tax_total);
$sort_order++;
}
}
// Added invoice total total
$invoice_total = [
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'code' => 'total',
'name' => 'invoices.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
];
InvoiceTotal::create($invoice_total);
}
}
Model::reguard();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('invoice_totals');
}
}

View File

@ -112,26 +112,15 @@
</div> </div>
<div class="col-xs-5"> <div class="col-xs-5">
<div class="table-responsive"> <div class="table-responsive">
<table class="table">
<table class="table"> <table class="table">
<tbody> <tbody>
@foreach($invoice->totals as $total)
<tr> <tr>
<th style="width:50%">{{ trans('bills.sub_total') }}:</th> <th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($bill->sub_total, $bill->currency_code, true)</td> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
<tr>
<th>{{ trans('bills.tax_total') }}:</th>
<td class="text-right">@money($bill->tax_total, $bill->currency_code, true)</td>
</tr>
@if($bill->paid)
<tr>
<th>{{ trans('bills.paid') }}:</th>
<td class="text-right">@money('-' . $bill->paid, $bill->currency_code, true)</td>
</tr>
@endif
<tr>
<th>{{ trans('bills.total') }}:</th>
<td class="text-right">@money($bill->grand_total, $bill->currency_code, true)</td>
</tr> </tr>
@endforeach
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -95,7 +95,7 @@
</td> </td>
<td class="text-center">{{ $item->quantity }}</td> <td class="text-center">{{ $item->quantity }}</td>
<td class="text-right">@money($item->price, $bill->currency_code, true)</td> <td class="text-right">@money($item->price, $bill->currency_code, true)</td>
<td class="text-right">@money($item->total - $item->tax, $bill->currency_code, true)</td> <td class="text-right">@money($item->total, $bill->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
@ -117,24 +117,25 @@
<div class="table-responsive"> <div class="table-responsive">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> @foreach($invoice->totals as $total)
<th style="width:50%">{{ trans('bills.sub_total') }}:</th> @if($total->code != 'total')
<td class="text-right">@money($bill->sub_total, $bill->currency_code, true)</td> <tr>
</tr> <th>{{ trans($total['name']) }}:</th>
<tr> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
<th>{{ trans('bills.tax_total') }}:</th> </tr>
<td class="text-right">@money($bill->tax_total, $bill->currency_code, true)</td> @else
</tr> @if ($invoice->paid)
@if($bill->paid) <tr class="text-success">
<tr> <th>{{ trans('invoices.paid') }}:</th>
<th>{{ trans('bills.paid') }}:</th> <td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money('-' . $bill->paid, $bill->currency_code, true)</td> </tr>
</tr> @endif
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount - $invoice->paid, $invoice->currency_code, true)</td>
</tr>
@endif @endif
<tr> @endforeach
<th>{{ trans('bills.total') }}:</th>
<td class="text-right">@money($bill->grand_total, $bill->currency_code, true)</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -114,24 +114,12 @@
<div class="table-responsive"> <div class="table-responsive">
<table class="table"> <table class="table">
<tbody> <tbody>
@foreach($invoice->totals as $total)
<tr> <tr>
<th style="max-width: 214px">{{ trans('invoices.sub_total') }}:</th> <th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($invoice->sub_total, $invoice->currency_code, true)</td> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
</tr>
<tr>
<th>{{ trans('invoices.tax_total') }}:</th>
<td class="text-right">@money($invoice->tax_total, $invoice->currency_code, true)</td>
</tr>
@if($invoice->paid)
<tr>
<th>{{ trans('invoices.paid') }}:</th>
<td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td>
</tr>
@endif
<tr>
<th>{{ trans('invoices.total') }}:</th>
<td class="text-right">@money($invoice->grand_total, $invoice->currency_code, true)</td>
</tr> </tr>
@endforeach
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -95,7 +95,7 @@
</td> </td>
<td class="text-center">{{ $item->quantity }}</td> <td class="text-center">{{ $item->quantity }}</td>
<td class="text-right">@money($item->price, $invoice->currency_code, true)</td> <td class="text-right">@money($item->price, $invoice->currency_code, true)</td>
<td class="text-right">@money($item->total - $item->tax, $invoice->currency_code, true)</td> <td class="text-right">@money($item->total, $invoice->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
@ -117,24 +117,25 @@
<div class="table-responsive"> <div class="table-responsive">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> @foreach($invoice->totals as $total)
<th>{{ trans('invoices.sub_total') }}:</th> @if($total->code != 'total')
<td class="text-right">@money($invoice->sub_total, $invoice->currency_code, true)</td> <tr>
</tr> <th>{{ trans($total['name']) }}:</th>
<tr> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
<th>{{ trans('invoices.tax_total') }}:</th> </tr>
<td class="text-right">@money($invoice->tax_total, $invoice->currency_code, true)</td> @else
</tr> @if ($invoice->paid)
@if ($invoice->paid) <tr class="text-success">
<tr> <th>{{ trans('invoices.paid') }}:</th>
<th>{{ trans('invoices.paid') }}:</th> <td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td> </tr>
</tr> @endif
<tr>
<th>{{ trans($total['name']) }}:</th>
<td class="text-right">@money($total->amount - $invoice->paid, $invoice->currency_code, true)</td>
</tr>
@endif @endif
<tr> @endforeach
<th>{{ trans('invoices.total') }}:</th>
<td class="text-right">@money($invoice->grand_total, $invoice->currency_code, true)</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>