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\Vendor;
use App\Models\Expense\Bill;
use App\Models\Expense\BillHistory;
use App\Models\Expense\BillItem;
use App\Models\Expense\BillTotal;
use App\Models\Expense\BillHistory;
use App\Models\Expense\BillPayment;
use App\Models\Item\Item;
use App\Models\Setting\Category;
@ -21,9 +22,8 @@ use App\Models\Setting\Tax;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Uploads;
use Jenssegers\Date\Date;
use App\Utilities\Modules;
use Date;
class Bills extends Controller
{
@ -36,7 +36,7 @@ class Bills extends Controller
*/
public function index()
{
$bills = Bill::with('status')->collect();
$bills = Bill::with(['vendor', 'status', 'items', 'payments', 'histories'])->collect();
$vendors = collect(Vendor::enabled()->pluck('name', 'id'))
->prepend(trans('general.all_type', ['type' => trans_choice('general.vendors', 2)]), '');
@ -56,25 +56,15 @@ class Bills extends Controller
*/
public function show(Bill $bill)
{
$sub_total = 0;
$tax_total = 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) {
$item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
$accounts = Account::enabled()->pluck('name', 'id');
@ -100,27 +90,17 @@ class Bills extends Controller
*/
public function printBill($bill_id)
{
$sub_total = 0;
$tax_total = 0;
$paid = 0;
$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) {
$item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
return view('expenses.bills.bill', compact('bill'));
}
@ -134,27 +114,17 @@ class Bills extends Controller
*/
public function pdfBill($bill_id)
{
$sub_total = 0;
$tax_total = 0;
$paid = 0;
$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) {
$item->default_currency_code = $bill->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$bill->sub_total = $sub_total;
$bill->tax_total = $tax_total;
$bill->paid = $paid;
$bill->grand_total = (($sub_total + $tax_total) - $paid);
$html = view('expenses.bills.bill', compact('bill'))->render();
@ -286,18 +256,24 @@ class Bills extends Controller
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$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['bill_id'] = $bill->id;
if ($request['item']) {
foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = '';
if (!empty($item['item_id'])) {
@ -323,16 +299,74 @@ class Bills extends Controller
$bill_item['price'] = $item['price'];
$bill_item['tax'] = $tax;
$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);
}
}
$request['amount'] += $sub_total + $tax_total;
$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['status_code'] = 'new';
$request['notify'] = 0;
@ -401,11 +435,16 @@ class Bills extends Controller
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($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['bill_id'] = $bill->id;
@ -413,6 +452,7 @@ class Bills extends Controller
BillItem::where('bill_id', $bill->id)->delete();
foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = '';
if (!empty($item['item_id'])) {
@ -438,16 +478,76 @@ class Bills extends Controller
$bill_item['price'] = $item['price'];
$bill_item['tax'] = $tax;
$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);
}
}
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());
// 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
event(new BillUpdated($bill));
@ -476,6 +576,7 @@ class Bills extends Controller
*/
BillItem::where('bill_id', $bill->id)->delete();
BillTotal::where('bill_id', $bill->id)->delete();
BillPayment::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\InvoiceHistory;
use App\Models\Income\InvoiceItem;
use App\Models\Income\InvoiceTotal;
use App\Models\Income\InvoicePayment;
use App\Models\Income\InvoiceStatus;
use App\Models\Item\Item;
@ -22,10 +23,8 @@ use App\Models\Setting\Tax;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Uploads;
use Jenssegers\Date\Date;
use App\Utilities\Modules;
use Date;
class Invoices extends Controller
{
@ -38,7 +37,7 @@ class Invoices extends Controller
*/
public function index()
{
$invoices = Invoice::with('status')->collect();
$invoices = Invoice::with(['customer', 'status', 'items', 'payments', 'histories'])->collect();
$customers = collect(Customer::enabled()->pluck('name', 'id'))
->prepend(trans('general.all_type', ['type' => trans_choice('general.customers', 2)]), '');
@ -58,25 +57,15 @@ class Invoices extends Controller
*/
public function show(Invoice $invoice)
{
$sub_total = 0;
$tax_total = 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) {
$item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$accounts = Account::enabled()->pluck('name', 'id');
@ -102,27 +91,17 @@ class Invoices extends Controller
*/
public function printInvoice($invoice_id)
{
$sub_total = 0;
$tax_total = 0;
$paid = 0;
$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) {
$item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$invoice->template_path = 'incomes.invoices.invoice';
@ -140,27 +119,17 @@ class Invoices extends Controller
*/
public function pdfInvoice($invoice_id)
{
$sub_total = 0;
$tax_total = 0;
$paid = 0;
$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) {
$item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$invoice->template_path = 'incomes.invoices.invoice';
@ -296,18 +265,24 @@ class Invoices extends Controller
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$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['invoice_id'] = $invoice->id;
if ($request['item']) {
foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = '';
if (!empty($item['item_id'])) {
@ -333,16 +308,74 @@ class Invoices extends Controller
$invoice_item['price'] = $item['price'];
$invoice_item['tax'] = $tax;
$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);
}
}
$request['amount'] += $sub_total + $tax_total;
$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['status_code'] = 'draft';
$request['notify'] = 0;
@ -411,11 +444,16 @@ class Invoices extends Controller
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($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['invoice_id'] = $invoice->id;
@ -423,6 +461,7 @@ class Invoices extends Controller
InvoiceItem::where('invoice_id', $invoice->id)->delete();
foreach ($request['item'] as $item) {
unset($tax_object);
$item_sku = '';
if (!empty($item['item_id'])) {
@ -448,16 +487,76 @@ class Invoices extends Controller
$invoice_item['price'] = $item['price'];
$invoice_item['tax'] = $tax;
$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);
}
}
$request['amount'] += $sub_total + $tax_total;
$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
event(new InvoiceUpdated($invoice));
@ -486,6 +585,7 @@ class Invoices extends Controller
*/
InvoiceItem::where('invoice_id', $invoice->id)->delete();
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
InvoicePayment::where('invoice_id', $invoice->id)->delete();
InvoiceHistory::where('invoice_id', $invoice->id)->delete();