Skip to content

Commit

Permalink
fixed #239 : If you edit bills with taxes set and selected, taxes got…
Browse files Browse the repository at this point in the history
… de-selected...
  • Loading branch information
cuneytsenturk committed Mar 6, 2018
1 parent 7185fd0 commit cf15174
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 88 deletions.
179 changes: 93 additions & 86 deletions app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public function store(Request $request)
$bill_item['tax_id'] = $tax_id;
$bill_item['total'] = $item['price'] * $item['quantity'];

BillItem::create($bill_item);

// Set taxes
if (isset($tax_object)) {
if (array_key_exists($tax_object->id, $taxes)) {
$taxes[$tax_object->id]['amount'] += $tax;
Expand All @@ -191,54 +194,20 @@ public function store(Request $request)
}
}

// Calculate totals
$tax_total += $tax;
$sub_total += $bill_item['total'];

BillItem::create($bill_item);
unset($tax_object);
}
}

$request['amount'] += $sub_total + $tax_total;

$bill->update($request->input());

// Added bill total 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' => 1,
]);

$sort_order = 2;

// Added bill total taxes
if ($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 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,
]);
// Add bill totals
$this->addTotals($bill, $request, $taxes, $sub_total, $tax_total);

// Add bill history
BillHistory::create([
Expand Down Expand Up @@ -356,8 +325,6 @@ public function update(Bill $bill, Request $request)
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;

$request['amount'] = 0;

$taxes = [];
$tax_total = 0;
$sub_total = 0;
Expand Down Expand Up @@ -416,41 +383,7 @@ public function update(Bill $bill, Request $request)
}
}

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;
$request['amount'] = $sub_total + $tax_total;

$bill->update($request->input());

Expand All @@ -461,17 +394,11 @@ public function update(Bill $bill, Request $request)
$bill->attachMedia($media, 'attachment');
}

// 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,
];
// Delete previous invoice totals
BillTotal::where('bill_id', $bill->id)->delete();

BillTotal::create($bill_total);
// Add invoice totals
$this->addTotals($bill, $request, $taxes, $sub_total, $tax_total);

// Fire the event to make it extendible
event(new BillUpdated($bill));
Expand Down Expand Up @@ -669,7 +596,9 @@ public function paymentDestroy(BillPayment $payment)
{
$bill = Bill::find($payment->bill_id);

if ($bill->payments()->count() > 1) {
if ($bill->payments()->paid() == $bill->amount) {
$bill->bill_status_code = 'paid';
} elseif ($bill->payments()->count() > 1) {
$bill->bill_status_code = 'partial';
} else {
$bill->bill_status_code = 'draft';
Expand All @@ -685,4 +614,82 @@ public function paymentDestroy(BillPayment $payment)

return redirect()->back();
}

protected function addTotals($bill, $request, $taxes, $sub_total, $tax_total)
{
$sort_order = 1;

// Added bill total 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 total taxes
if ($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 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,
]);
}

protected function getLogo()
{
$logo = '';

$media_id = setting('general.company_logo');

if (setting('general.invoice_logo')) {
$media_id = setting('general.invoice_logo');
}

$media = Media::find($media_id);

if (empty($media)) {
return $logo;
}

$path = Storage::path($media->getDiskPath());

if (!is_file($path)) {
return $logo;
}

$image = Image::make($path)->encode()->getEncoded();

if (empty($image)) {
return $logo;
}

$extension = File::extension($path);

$logo = 'data:image/' . $extension . ';base64,' . base64_encode($image);

return $logo;
}
}
4 changes: 2 additions & 2 deletions resources/views/expenses/bills/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<input value="{{ $item->price }}" class="form-control text-right" required="required" name="item[{{ $item_row }}][price]" type="text" id="item-price-{{ $item_row }}">
</td>
<td>
{!! Form::select('item[' . $item_row . '][tax_id]', $taxes, $item->tax, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])]) !!}
{!! Form::select('item[' . $item_row . '][tax_id]', $taxes, $item->tax_id, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control select2', 'placeholder' => trans('general.form.enter', ['field' => trans_choice('general.taxes', 1)])]) !!}
</td>
<td class="text-right" style="vertical-align: middle;">
<span id="item-total-{{ $item_row }}">@money($item->total, $bill->currency_code, true)</span>
Expand All @@ -76,7 +76,7 @@
<input class="form-control text-right" required="required" name="item[{{ $item_row }}][price]" type="text" id="item-price-{{ $item_row }}">
</td>
<td>
{!! Form::select('item[' . $item_row . '][tax_id]', $taxes, null, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])]) !!}
{!! Form::select('item[' . $item_row . '][tax_id]', $taxes, null, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control select2', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])]) !!}
</td>
<td class="text-right" style="vertical-align: middle;">
<span id="item-total-{{ $item_row }}">0</span>
Expand Down

0 comments on commit cf15174

Please sign in to comment.