Added expense category

This commit is contained in:
Hillel Coren 2016-07-05 21:49:47 +03:00
parent f2cbfec926
commit 21a91ff0e1
11 changed files with 117 additions and 23 deletions

View File

@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added ### Added
- Configuration for first day of the week #950 - Configuration for first day of the week #950
- StyleCI configuration #929 - StyleCI configuration #929
- Added expense category
### Changed ### Changed
- Removed `invoiceninja.komodoproject` from Git #932 - Removed `invoiceninja.komodoproject` from Git #932

View File

@ -10,6 +10,7 @@ use Redirect;
use Cache; use Cache;
use App\Models\Vendor; use App\Models\Vendor;
use App\Models\Expense; use App\Models\Expense;
use App\Models\ExpenseCategory;
use App\Models\Client; use App\Models\Client;
use App\Services\ExpenseService; use App\Services\ExpenseService;
use App\Ninja\Repositories\ExpenseRepository; use App\Ninja\Repositories\ExpenseRepository;
@ -232,6 +233,7 @@ class ExpenseController extends BaseController
'countries' => Cache::get('countries'), 'countries' => Cache::get('countries'),
'customLabel1' => Auth::user()->account->custom_vendor_label1, 'customLabel1' => Auth::user()->account->custom_vendor_label1,
'customLabel2' => Auth::user()->account->custom_vendor_label2, 'customLabel2' => Auth::user()->account->custom_vendor_label2,
'categories' => ExpenseCategory::scope()->get()
]; ];
} }

View File

@ -39,8 +39,17 @@ class Expense extends EntityModel
'public_notes', 'public_notes',
'bank_id', 'bank_id',
'transaction_id', 'transaction_id',
'expense_category_id',
]; ];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function expense_category()
{
return $this->belongsTo('App\Models\ExpenseCategory');
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */

View File

@ -5,6 +5,7 @@ use Utils;
use App\Ninja\Repositories\ExpenseRepository; use App\Ninja\Repositories\ExpenseRepository;
use App\Models\Client; use App\Models\Client;
use App\Models\Vendor; use App\Models\Vendor;
use App\Models\ExpenseCategory;
use App\Ninja\Datatables\ExpenseDatatable; use App\Ninja\Datatables\ExpenseDatatable;
/** /**
@ -57,6 +58,19 @@ class ExpenseService extends BaseService
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']); $data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
} }
if ( ! empty($data['category'])) {
$name = trim($data['category']);
$category = ExpenseCategory::scope()->whereName($name)->first();
if ( ! $category) {
$category = ExpenseCategory::createNew();
$category->name = $name;
$category->save();
}
$data['expense_category_id'] = $category->id;
} elseif (isset($data['category'])) {
$data['expense_category_id'] = null;
}
return $this->expenseRepo->save($data, $expense); return $this->expenseRepo->save($data, $expense);
} }

View File

@ -30319,6 +30319,34 @@ if (window.ko) {
trigger: "hover" trigger: "hover"
} }
}; };
ko.bindingHandlers.typeahead = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var $element = $(element);
var allBindings = allBindingsAccessor();
$element.typeahead({
highlight: true,
minLength: 0,
},
{
name: 'data',
display: allBindings.key,
limit: 50,
source: searchData(allBindings.items, allBindings.key)
}).on('typeahead:change', function(element, datum, name) {
var value = valueAccessor();
value(datum);
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) {
$(element).typeahead('val', value);
}
}
};
} }
function getContactDisplayName(contact) function getContactDisplayName(contact)
@ -31005,6 +31033,7 @@ function searchData(data, key, fuzzy) {
function escapeRegExp(str) { function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
} }
var NINJA = NINJA || {}; var NINJA = NINJA || {};
NINJA.TEMPLATES = { NINJA.TEMPLATES = {

View File

@ -425,6 +425,34 @@ if (window.ko) {
trigger: "hover" trigger: "hover"
} }
}; };
ko.bindingHandlers.typeahead = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var $element = $(element);
var allBindings = allBindingsAccessor();
$element.typeahead({
highlight: true,
minLength: 0,
},
{
name: 'data',
display: allBindings.key,
limit: 50,
source: searchData(allBindings.items, allBindings.key)
}).on('typeahead:change', function(element, datum, name) {
var value = valueAccessor();
value(datum);
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (value) {
$(element).typeahead('val', value);
}
}
};
} }
function getContactDisplayName(contact) function getContactDisplayName(contact)

View File

@ -2009,6 +2009,7 @@ $LANG = array(
'vendor_contacts' => 'Vendor Contacts', 'vendor_contacts' => 'Vendor Contacts',
'all' => 'All', 'all' => 'All',
'selected' => 'Selected', 'selected' => 'Selected',
'category' => 'Category',
); );

View File

@ -26,6 +26,7 @@
@if ($expense) @if ($expense)
{!! Former::populate($expense) !!} {!! Former::populate($expense) !!}
{!! Former::populateField('should_be_invoiced', intval($expense->should_be_invoiced)) !!} {!! Former::populateField('should_be_invoiced', intval($expense->should_be_invoiced)) !!}
{!! Former::populateField('category', $expense->expense_category ? $expense->expense_category->name : '') !!}
{!! Former::hidden('public_id') !!} {!! Former::hidden('public_id') !!}
@endif @endif
@ -33,11 +34,16 @@
<div class="panel-body"> <div class="panel-body">
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-6">
{!! Former::select('vendor_id')->addOption('', '') {!! Former::select('vendor_id')->addOption('', '')
->data_bind('combobox: vendor_id') ->data_bind('combobox: vendor_id')
->label(trans('texts.vendor')) ->label(trans('texts.vendor'))
->addGroupClass('vendor-select') !!} ->addGroupClass('vendor-select') !!}
{!! Former::text('category')
->data_bind("typeahead: category, items: categories, key: 'name', valueUpdate: 'afterkeydown'")
->label(trans('texts.category')) !!}
{!! Former::text('expense_date') {!! Former::text('expense_date')
->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT)) ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))
->addGroupClass('expense_date') ->addGroupClass('expense_date')
@ -279,6 +285,10 @@
var ViewModel = function(data) { var ViewModel = function(data) {
var self = this; var self = this;
self.categories = {!! $categories !!};
console.log(self.categories[0].name);
self.category = ko.observable();
self.expense_currency_id = ko.observable(); self.expense_currency_id = ko.observable();
self.invoice_currency_id = ko.observable(); self.invoice_currency_id = ko.observable();
self.documents = ko.observableArray(); self.documents = ko.observableArray();

View File

@ -256,7 +256,7 @@
</td> </td>
<td> <td>
<div id="scrollable-dropdown-menu"> <div id="scrollable-dropdown-menu">
<input id="product_key" type="text" data-bind="typeahead: product_key, items: $root.products, key: 'product_key', valueUpdate: 'afterkeydown', attr: {name: 'invoice_items[' + $index() + '][product_key]'}" class="form-control invoice-item handled"/> <input id="product_key" type="text" data-bind="productTypeahead: product_key, items: $root.products, key: 'product_key', valueUpdate: 'afterkeydown', attr: {name: 'invoice_items[' + $index() + '][product_key]'}" class="form-control invoice-item handled"/>
</div> </div>
</td> </td>
<td> <td>

View File

@ -822,7 +822,7 @@ var ExpenseModel = function(data) {
}; };
/* Custom binding for product key typeahead */ /* Custom binding for product key typeahead */
ko.bindingHandlers.typeahead = { ko.bindingHandlers.productTypeahead = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var $element = $(element); var $element = $(element);
var allBindings = allBindingsAccessor(); var allBindings = allBindingsAccessor();