Merge pull request #304 from denisdulici/category-invoice-bill

Invoice/Bill: Categories
This commit is contained in:
Denis Duliçi 2018-04-23 22:21:31 +03:00 committed by GitHub
commit 89f163ef8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 215 additions and 182 deletions

View File

@ -243,52 +243,63 @@ class Dashboard extends Controller
private function calculateAmounts()
{
$incomes_amount = $expenses_amount = 0;
$incomes_amount = $open_invoice = $overdue_invoice = 0;
$expenses_amount = $open_bill = $overdue_bill = 0;
// Invoices
$invoices = Invoice::with('payments')->accrued()->get();
list($invoice_paid_amount, $open_invoice, $overdue_invoice) = $this->calculateTotals($invoices, 'invoice');
$incomes_amount += $invoice_paid_amount;
// Add to Incomes By Category
$this->addToIncomeDonut('#00c0ef', $invoice_paid_amount, trans_choice('general.invoices', 2));
// Bills
$bills = Bill::with('payments')->accrued()->get();
list($bill_paid_amount, $open_bill, $overdue_bill) = $this->calculateTotals($bills, 'bill');
$expenses_amount += $bill_paid_amount;
// Add to Expenses By Category
$this->addToExpenseDonut('#dd4b39', $bill_paid_amount, trans_choice('general.bills', 2));
// Revenues & Payments
$categories = Category::orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
// Get categories
$categories = Category::with(['bills', 'invoices', 'payments', 'revenues'])->orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
// Revenues
foreach ($category->revenues as $revenue) {
$amount += $revenue->getConvertedAmount();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$incomes_amount += $paid;
$open_invoice += $open;
$overdue_invoice += $overdue;
$amount += $paid;
}
$this->addToIncomeDonut($category->color, $amount, $category->name);
$incomes_amount += $amount;
break;
case 'expense':
$amount = 0;
// Payments
foreach ($category->payments as $payment) {
$amount += $payment->getConvertedAmount();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$expenses_amount += $paid;
$open_bill += $open;
$overdue_bill += $overdue;
$amount += $paid;
}
$this->addToExpenseDonut($category->color, $amount, $category->name);
$expenses_amount += $amount;
break;
}
}
@ -382,22 +393,19 @@ class Dashboard extends Controller
return $profit;
}
private function calculateTotals($items, $type)
private function calculateInvoiceBillTotals($item, $type)
{
$paid = $open = $overdue = 0;
$today = $this->today->toDateString();
foreach ($items as $item) {
$paid += $item->getConvertedAmount();
$paid += $item->getConvertedAmount();
$code_field = $type . '_status_code';
if ($item->$code_field == 'paid') {
continue;
}
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->payments as $payment) {
$payments += $payment->getConvertedAmount();

View File

@ -102,7 +102,9 @@ class Bills extends Controller
$taxes = Tax::enabled()->get()->pluck('title', 'id');
return view('expenses.bills.create', compact('vendors', 'currencies', 'items', 'taxes'));
$categories = Category::enabled()->type('expense')->pluck('name', 'id');
return view('expenses.bills.create', compact('vendors', 'currencies', 'items', 'taxes', 'categories'));
}
/**
@ -319,7 +321,9 @@ class Bills extends Controller
$taxes = Tax::enabled()->get()->pluck('title', 'id');
return view('expenses.bills.edit', compact('bill', 'vendors', 'currencies', 'items', 'taxes'));
$categories = Category::enabled()->type('expense')->pluck('name', 'id');
return view('expenses.bills.edit', compact('bill', 'vendors', 'currencies', 'items', 'taxes', 'categories'));
}
/**

View File

@ -105,9 +105,11 @@ class Invoices extends Controller
$taxes = Tax::enabled()->get()->pluck('title', 'id');
$categories = Category::enabled()->type('income')->pluck('name', 'id');
$number = $this->getNextInvoiceNumber();
return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'number'));
return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'number'));
}
/**
@ -340,7 +342,9 @@ class Invoices extends Controller
$taxes = Tax::enabled()->get()->pluck('title', 'id');
return view('incomes.invoices.edit', compact('invoice', 'customers', 'currencies', 'items', 'taxes'));
$categories = Category::enabled()->type('income')->pluck('name', 'id');
return view('incomes.invoices.edit', compact('invoice', 'customers', 'currencies', 'items', 'taxes', 'categories'));
}
/**

View File

@ -23,12 +23,7 @@ class ExpenseSummary extends Controller
$status = request('status');
//if ($filter != 'upcoming') {
$categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
//}
// Add Bill in Categories
$categories[0] = trans_choice('general.bills', 2);
$categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
// Get year
$year = request('year');
@ -49,15 +44,6 @@ class ExpenseSummary extends Controller
'currency_rate' => 1
);
// Bill
$expenses[0][$dates[$j]] = array(
'category_id' => 0,
'name' => trans_choice('general.bills', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
foreach ($categories as $category_id => $category_name) {
$expenses[$category_id][$dates[$j]] = array(
'category_id' => $category_id,
@ -109,9 +95,6 @@ class ExpenseSummary extends Controller
->credits(false)
->view($chart_template);
// Expenses Graph
$expenses_graph = json_encode($expenses_graph);
return view($view_template, compact('chart', 'dates', 'categories', 'expenses', 'totals'));
}
@ -120,13 +103,7 @@ class ExpenseSummary extends Controller
foreach ($items as $item) {
$date = Date::parse($item->$date_field)->format('F');
if ($type == 'bill') {
$category_id = 0;
} else {
$category_id = $item->category_id;
}
if (!isset($expenses[$category_id])) {
if (!isset($expenses[$item->category_id])) {
continue;
}
@ -139,9 +116,9 @@ class ExpenseSummary extends Controller
}
}
$expenses[$category_id][$date]['amount'] += $amount;
$expenses[$category_id][$date]['currency_code'] = $item->currency_code;
$expenses[$category_id][$date]['currency_rate'] = $item->currency_rate;
$expenses[$item->category_id][$date]['amount'] += $amount;
$expenses[$item->category_id][$date]['currency_code'] = $item->currency_code;
$expenses[$item->category_id][$date]['currency_rate'] = $item->currency_rate;
$graph[Date::parse($item->$date_field)->format('F-Y')] += $amount;

View File

@ -26,19 +26,9 @@ class IncomeExpenseSummary extends Controller
$status = request('status');
//if ($filter != 'upcoming') {
$income_categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
//}
$income_categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
// Add Invoice in Categories
$income_categories[0] = trans_choice('general.invoices', 2);
//if ($filter != 'upcoming') {
$expense_categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
//}
// Add Bill in Categories
$expense_categories[0] = trans_choice('general.bills', 2);
$expense_categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
// Get year
$year = request('year');
@ -59,15 +49,6 @@ class IncomeExpenseSummary extends Controller
'currency_rate' => 1
);
// Compares
$compares['income'][0][$dates[$j]] = array(
'category_id' => 0,
'name' => trans_choice('general.invoices', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
foreach ($income_categories as $category_id => $category_name) {
$compares['income'][$category_id][$dates[$j]] = array(
'category_id' => $category_id,
@ -78,14 +59,6 @@ class IncomeExpenseSummary extends Controller
);
}
$compares['expense'][0][$dates[$j]] = array(
'category_id' => 0,
'name' => trans_choice('general.bills', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
foreach ($expense_categories as $category_id => $category_name) {
$compares['expense'][$category_id][$dates[$j]] = array(
'category_id' => $category_id,
@ -167,15 +140,9 @@ class IncomeExpenseSummary extends Controller
foreach ($items as $item) {
$date = Date::parse($item->$date_field)->format('F');
if (($type == 'invoice') || ($type == 'bill')) {
$category_id = 0;
} else {
$category_id = $item->category_id;
}
$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
if (!isset($compares[$group][$category_id])) {
if (!isset($compares[$group][$item->category_id])) {
continue;
}
@ -188,9 +155,9 @@ class IncomeExpenseSummary extends Controller
}
}
$compares[$group][$category_id][$date]['amount'] += $amount;
$compares[$group][$category_id][$date]['currency_code'] = $item->currency_code;
$compares[$group][$category_id][$date]['currency_rate'] = $item->currency_rate;
$compares[$group][$item->category_id][$date]['amount'] += $amount;
$compares[$group][$item->category_id][$date]['currency_code'] = $item->currency_code;
$compares[$group][$item->category_id][$date]['currency_rate'] = $item->currency_rate;
if ($group == 'income') {
$graph[Date::parse($item->$date_field)->format('F-Y')] += $amount;

View File

@ -23,12 +23,7 @@ class IncomeSummary extends Controller
$status = request('status');
//if ($filter != 'upcoming') {
$categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
//}
// Add Invoice in Categories
$categories[0] = trans_choice('general.invoices', 2);
$categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
// Get year
$year = request('year');
@ -49,15 +44,6 @@ class IncomeSummary extends Controller
'currency_rate' => 1
);
// Invoice
$incomes[0][$dates[$j]] = array(
'category_id' => 0,
'name' => trans_choice('general.invoices', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
);
foreach ($categories as $category_id => $category_name) {
$incomes[$category_id][$dates[$j]] = array(
'category_id' => $category_id,
@ -117,13 +103,7 @@ class IncomeSummary extends Controller
foreach ($items as $item) {
$date = Date::parse($item->$date_field)->format('F');
if ($type == 'invoice') {
$category_id = 0;
} else {
$category_id = $item->category_id;
}
if (!isset($incomes[$category_id])) {
if (!isset($incomes[$item->category_id])) {
continue;
}
@ -136,9 +116,9 @@ class IncomeSummary extends Controller
}
}
$incomes[$category_id][$date]['amount'] += $amount;
$incomes[$category_id][$date]['currency_code'] = $item->currency_code;
$incomes[$category_id][$date]['currency_rate'] = $item->currency_rate;
$incomes[$item->category_id][$date]['amount'] += $amount;
$incomes[$item->category_id][$date]['currency_code'] = $item->currency_code;
$incomes[$item->category_id][$date]['currency_rate'] = $item->currency_rate;
$graph[Date::parse($item->$date_field)->format('F-Y')] += $amount;

View File

@ -28,14 +28,8 @@ class ProfitLoss extends Controller
$income_categories = Category::enabled()->type('income')->pluck('name', 'id')->toArray();
// Add invoice to income categories
$income_categories[0] = trans_choice('general.invoices', 2);
$expense_categories = Category::enabled()->type('expense')->pluck('name', 'id')->toArray();
// Add bill to expense categories
$expense_categories[0] = trans_choice('general.bills', 2);
// Get year
$year = request('year');
if (empty($year)) {
@ -53,15 +47,6 @@ class ProfitLoss extends Controller
'currency_rate' => 1
);
// Compares
$compares['income'][0][$dates[$j]] = [
'category_id' => 0,
'name' => trans_choice('general.invoices', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($income_categories as $category_id => $category_name) {
$compares['income'][$category_id][$dates[$j]] = [
'category_id' => $category_id,
@ -72,14 +57,6 @@ class ProfitLoss extends Controller
];
}
$compares['expense'][0][$dates[$j]] = [
'category_id' => 0,
'name' => trans_choice('general.bills', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($expense_categories as $category_id => $category_name) {
$compares['expense'][$category_id][$dates[$j]] = [
'category_id' => $category_id,
@ -99,14 +76,6 @@ class ProfitLoss extends Controller
'currency_rate' => 1
];
$compares['income'][0]['total'] = [
'category_id' => 0,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
$gross['income'] = $gross['expense'] = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 'total' => 0];
foreach ($income_categories as $category_id => $category_name) {
@ -119,14 +88,6 @@ class ProfitLoss extends Controller
];
}
$compares['expense'][0]['total'] = [
'category_id' => 0,
'name' => trans_choice('general.totals', 1),
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1
];
foreach ($expense_categories as $category_id => $category_name) {
$compares['expense'][$category_id]['total'] = [
'category_id' => $category_id,
@ -196,15 +157,9 @@ class ProfitLoss extends Controller
foreach ($items as $item) {
$date = Date::parse($item->$date_field)->quarter;
if (($type == 'invoice') || ($type == 'bill')) {
$category_id = 0;
} else {
$category_id = $item->category_id;
}
$group = (($type == 'invoice') || ($type == 'revenue')) ? 'income' : 'expense';
if (!isset($compares[$group][$category_id])) {
if (!isset($compares[$group][$item->category_id])) {
continue;
}
@ -217,10 +172,10 @@ class ProfitLoss extends Controller
}
}
$compares[$group][$category_id][$date]['amount'] += $amount;
$compares[$group][$category_id][$date]['currency_code'] = $item->currency_code;
$compares[$group][$category_id][$date]['currency_rate'] = $item->currency_rate;
$compares[$group][$category_id]['total']['amount'] += $amount;
$compares[$group][$item->category_id][$date]['amount'] += $amount;
$compares[$group][$item->category_id][$date]['currency_code'] = $item->currency_code;
$compares[$group][$item->category_id][$date]['currency_rate'] = $item->currency_rate;
$compares[$group][$item->category_id]['total']['amount'] += $amount;
if ($group == 'income') {
$totals[$date]['amount'] += $amount;

View File

@ -39,6 +39,7 @@ class Bill extends Request
'billed_at' => 'required|date',
'due_at' => 'required|date',
'currency_code' => 'required|string',
'category_id' => 'required|integer',
'attachment' => 'mimes:' . setting('general.file_types') . '|between:0,' . setting('general.file_size') * 1024,
];
}

View File

@ -39,6 +39,7 @@ class Invoice extends Request
'invoiced_at' => 'required|date',
'due_at' => 'required|date',
'currency_code' => 'required|string',
'category_id' => 'required|integer',
'attachment' => 'mimes:' . setting('general.file_types') . '|between:0,' . setting('general.file_size') * 1024,
];
}

View File

@ -5,6 +5,11 @@ namespace App\Listeners\Updates;
use App\Events\UpdateFinished;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
use App\Models\Company\Company;
use App\Models\Expense\Bill;
use App\Models\Income\Invoice;
use App\Models\Setting\Category;
use Artisan;
class Version120 extends Listener
{
@ -25,6 +30,16 @@ class Version120 extends Listener
return;
}
$this->updatePermissions();
// Update database
Artisan::call('migrate', ['--force' => true]);
$this->updateInvoicesAndBills();
}
protected function updatePermissions()
{
$permissions = [];
// Create tax summary permission
@ -56,4 +71,39 @@ class Version120 extends Listener
}
}
}
protected function updateInvoicesAndBills()
{
$companies = Company::all();
foreach ($companies as $company) {
// Invoices
$invoice_category = Category::create([
'company_id' => $company->id,
'name' => trans_choice('general.invoices', 2),
'type' => 'income',
'color' => '#00c0ef',
'enabled' => '1'
]);
foreach ($company->invoices as $invoice) {
$invoice->category_id = $invoice_category->id;
$invoice->save();
}
// Bills
$bill_category = Category::create([
'company_id' => $company->id,
'name' => trans_choice('general.bills', 2),
'type' => 'expense',
'color' => '#dd4b39',
'enabled' => '1'
]);
foreach ($company->bills as $bill) {
$bill->category_id = $bill_category->id;
$bill->save();
}
}
}
}

View File

@ -60,6 +60,11 @@ class Bill extends Model
*/
protected $cloneable_relations = ['items', 'totals'];
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function vendor()
{
return $this->belongsTo('App\Models\Expense\Vendor');

View File

@ -61,6 +61,11 @@ class Invoice extends Model
*/
protected $cloneable_relations = ['items', 'totals'];
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function customer()
{
return $this->belongsTo('App\Models\Income\Customer');

View File

@ -22,9 +22,19 @@ class Category extends Model
*/
public $sortable = ['name', 'type', 'enabled'];
public function revenues()
public function bills()
{
return $this->hasMany('App\Models\Income\Revenue');
return $this->hasMany('App\Models\Expense\Bill');
}
public function invoices()
{
return $this->hasMany('App\Models\Income\Invoice');
}
public function items()
{
return $this->hasMany('App\Models\Item\Item');
}
public function payments()
@ -32,9 +42,9 @@ class Category extends Model
return $this->hasMany('App\Models\Expense\Payment');
}
public function items()
public function revenues()
{
return $this->hasMany('App\Models\Item\Item');
return $this->hasMany('App\Models\Income\Revenue');
}
/**

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
class AddCategoryColumnInvoicesBills extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('invoices', function ($table) {
$table->integer('category_id');
});
Schema::table('bills', function ($table) {
$table->integer('category_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('invoices', function ($table) {
$table->dropColumn('category_id');
});
Schema::table('bills', function ($table) {
$table->dropColumn('category_id');
});
}
}

View File

@ -97,6 +97,9 @@
</table>
</div>
</div>
{{ Form::selectGroup('category_id', trans_choice('general.categories', 1), 'folder-open-o', $categories) }}
{{ Form::textareaGroup('notes', trans_choice('general.notes', 2)) }}
{{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}
@ -190,6 +193,10 @@
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.currencies', 1)]) }}"
});
$("#category_id").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}"
});
$('#attachment').fancyfile({
text : '{{ trans('general.form.select.file') }}',
style : 'btn-default',

View File

@ -113,6 +113,9 @@
</table>
</div>
</div>
{{ Form::selectGroup('category_id', trans_choice('general.categories', 1), 'folder-open-o', $categories) }}
{{ Form::textareaGroup('notes', trans_choice('general.notes', 2)) }}
{{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}
@ -210,6 +213,10 @@
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.currencies', 1)]) }}"
});
$("#category_id").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}"
});
$('#attachment').fancyfile({
text : '{{ trans('general.form.select.file') }}',
style : 'btn-default',

View File

@ -97,6 +97,9 @@
</table>
</div>
</div>
{{ Form::selectGroup('category_id', trans_choice('general.categories', 1), 'folder-open-o', $categories) }}
{{ Form::textareaGroup('notes', trans_choice('general.notes', 2)) }}
{{ Form::fileGroup('attachment', trans('general.attachment')) }}
@ -191,6 +194,10 @@
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.currencies', 1)]) }}"
});
$("#category_id").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}"
});
$('#attachment').fancyfile({
text : '{{ trans('general.form.select.file') }}',
style : 'btn-default',

View File

@ -112,6 +112,9 @@
</table>
</div>
</div>
{{ Form::selectGroup('category_id', trans_choice('general.categories', 1), 'folder-open-o', $categories) }}
{{ Form::textareaGroup('notes', trans_choice('general.notes', 2)) }}
{{ Form::fileGroup('attachment', trans('general.attachment')) }}
@ -209,6 +212,10 @@
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.currencies', 1)]) }}"
});
$("#category_id").select2({
placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}"
});
$('#attachment').fancyfile({
text : '{{ trans('general.form.select.file') }}',
style : 'btn-default',