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

@ -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');
}
}