close #46 Enhancement: Bill and Invoice improve total status and system
This commit is contained in:
@ -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();
|
||||
|
||||
|
Reference in New Issue
Block a user