Skip to content

Commit

Permalink
Merge branch 'expense-refactor' into 'master'
Browse files Browse the repository at this point in the history
add customer in expense

See merge request mohit.panjvani/crater-web!172
  • Loading branch information
mohitpanjwani committed Mar 20, 2020
2 parents 386f96d + 82d85af commit 742e1e4
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 13 deletions.
16 changes: 16 additions & 0 deletions app/Expense.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Crater\ExpenseCategory;
use Crater\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

Expand All @@ -16,6 +17,7 @@ class Expense extends Model implements HasMedia
'expense_category_id',
'amount',
'company_id',
'user_id',
'expense_date',
'notes',
'attachment_receipt'
Expand All @@ -32,6 +34,11 @@ public function category()
return $this->belongsTo(ExpenseCategory::class, 'expense_category_id');
}

public function user()
{
return $this->belongsTo(User::class);
}

public function getFormattedExpenseDateAttribute($value)
{
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
Expand Down Expand Up @@ -81,6 +88,11 @@ public function scopeWhereCategory($query, $categoryId)
return $query->where('expenses.expense_category_id', $categoryId);
}

public function scopeWhereUser($query, $user_id)
{
return $query->where('expenses.user_id', $user_id);
}

public function scopeApplyFilters($query, array $filters)
{
$filters = collect($filters);
Expand All @@ -89,6 +101,10 @@ public function scopeApplyFilters($query, array $filters)
$query->whereCategory($filters->get('expense_category_id'));
}

if ($filters->get('user_id')) {
$query->whereUser($filters->get('user_id'));
}

if ($filters->get('from_date') && $filters->get('to_date')) {
$start = Carbon::createFromFormat('d/m/Y', $filters->get('from_date'));
$end = Carbon::createFromFormat('d/m/Y', $filters->get('to_date'));
Expand Down
25 changes: 20 additions & 5 deletions app/Http/Controllers/ExpensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@ public function index(Request $request)
$limit = $request->has('limit') ? $request->limit : 10;

$expenses = Expense::with('category')
->leftJoin('users', 'users.id', '=', 'expenses.user_id')
->join('expense_categories', 'expense_categories.id', '=', 'expenses.expense_category_id')
->applyFilters($request->only([
'expense_category_id',
'user_id',
'search',
'from_date',
'to_date',
'orderByField',
'orderBy'
]))
->whereCompany($request->header('company'))
->select('expenses.*', 'expense_categories.name')
->select('expenses.*', 'expense_categories.name', 'users.name as user_name')
->paginate($limit);

$customers = User::customer()
->whereCompany($request->header('company'))
->get();

return response()->json([
'expenses' => $expenses,
'customers' => $customers,
'currency' => Currency::findOrFail(
CompanySetting::getSetting('currency', $request->header('company'))
)
Expand All @@ -53,9 +60,13 @@ public function index(Request $request)
public function create(Request $request)
{
$categories = ExpenseCategory::whereCompany($request->header('company'))->get();
$customers = User::customer()
->whereCompany($request->header('company'))
->get();

return response()->json([
'categories' => $categories
'categories' => $categories,
'customers' => $customers
]);
}

Expand All @@ -72,6 +83,7 @@ public function store(ExpenseRequest $request)
$expense = new Expense();
$expense->notes = $request->notes;
$expense->expense_category_id = $request->expense_category_id;
$expense->user_id = $request->user_id;
$expense->amount = $request->amount;
$expense->company_id = $request->header('company');
$expense->expense_date = $expense_date;
Expand Down Expand Up @@ -107,7 +119,9 @@ public function show(Expense $expense)
public function edit(Request $request,$id)
{
$categories = ExpenseCategory::whereCompany($request->header('company'))->get();
$customers = User::where('role', 'customer')->whereCompany($request->header('company'))->get();
$customers = User::customer()
->whereCompany($request->header('company'))
->get();
$expense = Expense::with('category')->where('id', $id)->first();

return response()->json([
Expand All @@ -132,6 +146,7 @@ public function update(ExpenseRequest $request, Expense $expense)
$expense->notes = $request->notes;
$expense->expense_category_id = $request->expense_category_id;
$expense->amount = $request->amount;
$expense->user_id = $request->user_id;
$expense->expense_date = $expense_date;
$expense->save();

Expand Down Expand Up @@ -205,7 +220,7 @@ public function uploadReceipts(Request $request, $id)
* Retrive details of an expense receipt from storage.
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
*/
public function showReceipt($id)
{
$expense = Expense::find($id);
Expand Down Expand Up @@ -239,7 +254,7 @@ public function showReceipt($id)
* @param int $id
* @param strig $hash
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse | \Illuminate\Http\JsonResponse
*/
*/
public function downloadReceipt($id, $hash)
{
$company = Company::where('unique_hash', $hash)->first();
Expand Down
6 changes: 6 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Crater\MemberLoan;
use Crater\Address;
use Crater\Payment;
use Crater\Expense;
use Crater\Company;
use Crater\Notifications\MailResetPasswordNotification;
use Spatie\MediaLibrary\HasMedia\HasMedia;
Expand Down Expand Up @@ -105,6 +106,11 @@ public function addresses()
return $this->hasMany(Address::class);
}

public function expenses()
{
return $this->hasMany(Expense::class);
}

public function billingAddress()
{
return $this->hasOne(Address::class)->where('type', Address::BILLING_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function up()
$table->foreign('expense_category_id')->references('id')->on('expense_categories')->onDelete('cascade');
$table->integer('company_id')->unsigned()->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/plugins/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@
"title": "النفقات",
"expenses_list": "قائمة النفقات",
"expense_title": "Title",
"select_a_customer": "حدد عميلاً",
"customer": "العميل",
"contact": "تواصل",
"category": "الفئة",
"from_date": "من تاريخ",
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/plugins/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
"action": "Aktion",
"add_expense": "Aufwendung hinzufügen",
"add_new_expense": "Neue Aufwendung hinzufügen",
"select_a_customer": "Wählen Sie einen Kunden aus",
"amount": "Summe",
"categories": {
"actions": "Aktionen",
Expand All @@ -215,6 +216,7 @@
"title": "Titel"
},
"category": "Kategorie",
"customer": "Kunden",
"category_id": "Kategorie-Id",
"confirm_delete": "Sie können diese Ausgabe nicht wiederherstellen. | Sie können diese Ausgaben nicht wiederherstellen.",
"contact": "Kontakt",
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/plugins/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@
"expenses": {
"title": "Expenses",
"expenses_list": "Expenses List",
"select_a_customer": "Select a customer",
"expense_title": "Title",
"customer": "Customer",
"contact": "Contact",
"category": "Category",
"from_date": "From Date",
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/plugins/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@
"expenses_list": "Lista de gastos",
"expense_title": "Título",
"contact": "Contacto",
"customer": "Cliente",
"select_a_customer": "Selecciona un cliente",
"category": "Categoría",
"from_date": "Desde la fecha",
"to_date": "Hasta la fecha",
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/plugins/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@
"expenses_list": "Liste des dépenses",
"expense_title": "Titre",
"contact": "Contact",
"customer": "Client Client",
"select_a_customer": "Sélectionnez un client",
"category": "Catégorie",
"from_date": "A partir de la date",
"to_date": "À ce jour",
Expand Down
1 change: 1 addition & 0 deletions resources/assets/js/plugins/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@
"expense_title": "Título",
"contact": "Contato",
"category": "Categoria",
"customer": "Cliente",
"from_date": "A partir da Data",
"to_date": "Até a Data",
"expense_date": "Data",
Expand Down
28 changes: 25 additions & 3 deletions resources/assets/js/views/expenses/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
<span v-if="!$v.formData.amount.minValue" class="text-danger">{{ $t('validation.price_minvalue') }}</span>
</div>
</div>
<div class="form-group col-sm-6">
<label class="form-label">{{ $t('expenses.customer') }}</label>
<base-select
ref="baseSelect"
v-model="customer"
:options="customerList"
:searchable="true"
:show-labels="false"
:placeholder="$t('customers.select_a_customer')"
label="name"
track-by="id"
/>
</div>
<div class="form-group col-sm-6">
<label for="description">{{ $t('expenses.note') }}</label>
<base-text-area
Expand Down Expand Up @@ -169,7 +182,8 @@ export default {
expense_category_id: null,
expense_date: new Date(),
amount: null,
notes: ''
notes: '',
user_id: null
},
money: {
decimal: '.',
Expand All @@ -185,7 +199,9 @@ export default {
passData: [],
contacts: [],
previewReceipt: null,
fileSendUrl: '/api/expenses'
fileSendUrl: '/api/expenses',
customer: null,
customerList: []
}
},
validations: {
Expand Down Expand Up @@ -297,13 +313,18 @@ export default {
},
async fetchInitialData () {
this.fetchCategories()
let fetchData = await this.fetchCreateExpense()
this.customerList = fetchData.data.customers
if (this.isEdit) {
let response = await this.fetchExpense(this.$route.params.id)
this.category = response.data.expense.category
this.formData = { ...response.data.expense }
this.formData.expense_date = moment(this.formData.expense_date).toString()
this.formData.amount = (response.data.expense.amount)
this.fileSendUrl = `/api/expenses/${this.$route.params.id}`
if (response.data.expense.user_id) {
this.customer = this.customerList.find(customer => customer.id === response.data.expense.user_id)
}
}
},
async sendData () {
Expand All @@ -319,9 +340,10 @@ export default {
data.append('attachment_receipt', this.file)
}
data.append('expense_category_id', this.formData.expense_category_id)
data.append('expense_date', moment(this.formData.expense_date).format('DD/MM/YYYY'))
data.append('expense_date', moment(this.formData.expense_date).format('DD/MM/YYYY'))
data.append('amount', (this.formData.amount))
data.append('notes', this.formData.notes ? this.formData.notes : '')
data.append('user_id', this.customer ? this.customer.id : '')
if (this.isEdit) {
this.isLoading = true
Expand Down
32 changes: 27 additions & 5 deletions resources/assets/js/views/expenses/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@
<transition name="fade">
<div v-show="showFilters" class="filter-section">
<div class="row">
<div class="col-md-4">
<div class="col-md-3">
<label>{{ $t('expenses.customer') }}</label>
<base-select
v-model="filters.user"
:options="customers"
:searchable="true"
:show-labels="false"
:placeholder="$t('expenses.select_a_customer')"
label="name"
@click="filter = ! filter"
/>
</div>
<div class="col-md-3">
<label>{{ $t('expenses.category') }}</label>
<base-select
v-model="filters.category"
Expand All @@ -55,15 +67,15 @@
@click="filter = ! filter"
/>
</div>
<div class="col-md-4">
<div class="col-md-3">
<label>{{ $t('expenses.from_date') }}</label>
<base-date-picker
v-model="filters.from_date"
:calendar-button="true"
calendar-button-icon="calendar"
/>
</div>
<div class="col-md-4">
<div class="col-md-3">
<label>{{ $t('expenses.to_date') }}</label>
<base-date-picker
v-model="filters.to_date"
Expand Down Expand Up @@ -161,6 +173,11 @@
sort-as="name"
show="category.name"
/>
<table-column
:label="$t('expenses.customer')"
sort-as="user_name"
show="user_name"
/>
<table-column
:label="$t('expenses.date')"
sort-as="expense_date"
Expand Down Expand Up @@ -237,10 +254,12 @@ export default {
showFilters: false,
filtersApplied: false,
isRequestOngoing: true,
customers: [],
filters: {
category: null,
from_date: '',
to_date: ''
to_date: '',
user: ''
}
}
},
Expand Down Expand Up @@ -308,6 +327,7 @@ export default {
]),
async fetchData ({ page, filter, sort }) {
let data = {
user_id: this.filters.user ? this.filters.user.id : null,
expense_category_id: this.filters.category !== null ? this.filters.category.id : '',
from_date: this.filters.from_date === '' ? this.filters.from_date : moment(this.filters.from_date).format('DD/MM/YYYY'),
to_date: this.filters.to_date === '' ? this.filters.to_date : moment(this.filters.to_date).format('DD/MM/YYYY'),
Expand All @@ -318,6 +338,7 @@ export default {
this.isRequestOngoing = true
let response = await this.fetchExpenses(data)
this.customers = response.data.customers
this.isRequestOngoing = false
return {
Expand All @@ -340,7 +361,8 @@ export default {
this.filters = {
category: null,
from_date: '',
to_date: ''
to_date: '',
user: null
}
this.$nextTick(() => {
Expand Down

0 comments on commit 742e1e4

Please sign in to comment.