Bill create and update tax changes finished

This commit is contained in:
cuneytsenturk
2018-11-07 11:06:38 +03:00
parent 71bcdb915a
commit fe9ce1c733
6 changed files with 749 additions and 395 deletions

View File

@ -8,6 +8,9 @@ use App\Events\BillUpdated;
use App\Http\Controllers\Controller;
use App\Http\Requests\Expense\Bill as Request;
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\Common\Media;
use App\Models\Expense\BillStatus;
@ -111,137 +114,7 @@ class Bills extends Controller
*/
public function store(Request $request)
{
$bill = Bill::create($request->input());
// 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));
$bill = dispatch(new CreateBill($request));
$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)
{
$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));
$bill = dispatch(new UpdateBill($bill, $request));
$message = trans('messages.success.updated', ['type' => trans_choice('general.bills', 1)]);
@ -655,20 +408,7 @@ class Bills extends Controller
$bill->save();
$bill_payment_request = [
'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);
$bill_payment = dispatch(new CreateBillPayment($request, $bill));
// Upload attachment
if ($request->file('attachment')) {
@ -677,15 +417,6 @@ class Bills extends Controller
$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)]);
return response()->json([
@ -794,64 +525,4 @@ class Bills extends Controller
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,
]);
}
}

View File

@ -654,64 +654,4 @@ class Invoices extends Controller
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,
]);
}
}