This commit is contained in:
steenrabol 2016-01-07 12:04:01 +01:00
parent 45ba7ffc3b
commit a52364a039
11 changed files with 262 additions and 37 deletions

View File

@ -46,6 +46,7 @@ class ExpenseController extends BaseController
'expense_balance',
'expense_date',
'private_notes',
'public_notes',
''
]),
));

View File

@ -190,19 +190,11 @@ Route::group(['middleware' => 'auth'], function() {
Route::post('vendors/bulk', 'VendorController@bulk');
// Expense
Route::get('expenses/{id}/edit', function() {
return View::make('header');
});
Route::resource('expenses', 'ExpenseController');
Route::get('expenses/create/{vendor_id?}', 'ExpenseController@create');
Route::get('api/expenses/{vendor_id?}', array('as'=>'api.expenses', 'uses'=>'ExpenseController@getDatatable'));
//Route::get('api/expenseactivities/{vendor_id?}', array('as'=>'api.expenseactivities', 'uses'=>'ExpenseActivityController@getDatatable'));
//Route::post('vendors/bulk', 'VendorController@bulk');
Route::post('expenses/bulk', 'ExpenseController@bulk');
Route::get('api/expense/', array('as'=>'api.expenses', 'uses'=>'ExpenseController@getDatatable'));
Route::get('api/expenseactivities/{vendor_id?}', array('as'=>'api.expenseactivities', 'uses'=>'ExpenseActivityController@getDatatable'));
});

View File

@ -1,8 +1,5 @@
<?php namespace App\Models;
use Utils;
use DB;
use Carbon;
use Laracasts\Presenter\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\ExpenseWasCreated;
@ -15,9 +12,6 @@ class Expense extends EntityModel
protected $dates = ['deleted_at'];
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
protected $fillable = [
'vendor_id',
];
public function account()
{
return $this->belongsTo('App\Models\Account');

View File

@ -0,0 +1,64 @@
<?php namespace App\Models;
use Auth;
use Eloquent;
use Utils;
use Session;
use Request;
use Carbon;
class ExpenseActivity extends Eloquent {
public $timestamps = true;
public function scopeScope($query)
{
return $query->whereAccountId(Auth::user()->account_id);
}
public function account()
{
return $this->belongsTo('App\Models\Account');
}
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
}
public function vendor()
{
return $this->belongsTo('App\Models\Vendor')->withTrashed();
}
public function expense()
{
return $this->belongsTo('App\Models\Expense')->withTrashed();
}
public function getMessage()
{
$activityTypeId = $this->activity_type_id;
$account = $this->account;
$vendor = $this->vendor;
$user = $this->user;
$contactId = $this->contact_id;
$isSystem = $this->is_system;
$expense = $this->expense;
if($expense) {
$route = link_to($expense->getRoute(), $vendor->getDisplayName());
} else {
$route ='no expense id';
}
$data = [
'expense' => $route,
'user' => $isSystem ? '<i>' . trans('texts.system') . '</i>' : $user->getDisplayName(),
];
return trans("texts.activity_{$activityTypeId}", $data);
}
}

View File

@ -0,0 +1,12 @@
<?php namespace App\Ninja\Presenters;
use Utils;
use Laracasts\Presenter\Presenter;
class VendorPresenter extends Presenter {
public function country()
{
return $this->entity->country ? $this->entity->country->name : '';
}
}

View File

@ -0,0 +1,80 @@
<?php namespace App\Ninja\Repositories;
use DB;
use Auth;
use Utils;
use Request;
use App\Models\EntityModel;
use App\Models\ExpenseActivity;
use App\Models\Expense;
class ExpenseActivityRepository
{
public function create(Expense $entity, $activityTypeId)
{
// init activity and copy over context
$activity = self::getBlank($entity);
// Fill with our information
$activity->vendor_id = $entity->vendor_id;
$activity->contact_id = $entity->contact_id;
$activity->activity_type_id = $activityTypeId;
$activity->message = $activity->getMessage();
$activity->expense_id = $entity->id;
$activity->save();
return $activity;
}
private function getBlank($entity)
{
$activity = new ExpenseActivity();
if (Auth::check() && Auth::user()->account_id == $entity->account_id) {
$activity->user_id = Auth::user()->id;
$activity->account_id = Auth::user()->account_id;
} else {
$activity->user_id = $entity->user_id;
$activity->account_id = $entity->account_id;
}
$activity->token_id = session('token_id');
$activity->ip = Request::getClientIp();
return $activity;
}
public function findByVendorId($vendorId)
{
return DB::table('expense_activities')
->join('accounts', 'accounts.id', '=', 'vendor_activities.account_id')
->join('users', 'users.id', '=', 'vendor_activities.user_id')
->join('vendors', 'vendors.id', '=', 'vendor_activities.vendor_id')
->leftJoin('vendor_contacts', 'vendor_contacts.vendor_id', '=', 'vendors.id')
->where('vendors.id', '=', $vendorId)
->where('vendor_contacts.is_primary', '=', 1)
->whereNull('vendor_contacts.deleted_at')
->select(
DB::raw('COALESCE(vendors.currency_id, accounts.currency_id) currency_id'),
DB::raw('COALESCE(vendors.country_id, accounts.country_id) country_id'),
'vendor_activities.id',
'vendor_activities.created_at',
'vendor_activities.contact_id',
'vendor_activities.activity_type_id',
'vendor_activities.is_system',
'vendor_activities.balance',
'vendor_activities.adjustment',
'users.first_name as user_first_name',
'users.last_name as user_last_name',
'users.email as user_email',
'vendors.name as vendor_name',
'vendors.public_id as vendor_public_id',
'vendor_contacts.id as contact',
'vendor_contacts.first_name as first_name',
'vendor_contacts.last_name as last_name',
'vendor_contacts.email as email'
);
}
}

View File

@ -13,8 +13,18 @@ class ExpenseRepository extends BaseRepository
return 'App\Models\Expense';
}
public function find($vendorPublicId = null, $filter = null)
public function all()
{
return Expense::scope()
->with('user')
->withTrashed()
->where('is_deleted', '=', false)
->get();
}
public function find($filter = null)
{
/*
$query = DB::table('expenses')
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
->join('vendors', 'vendors.id', '=', 'expenses.vendor_id')
@ -39,21 +49,37 @@ class ExpenseRepository extends BaseRepository
'expenses.deleted_at',
'expenses.is_deleted'
);
if ($vendorPublicId) {
$query->where('vendors.public_id', '=', $vendorPublicId);
}
*/
$accountid = \Auth::user()->account_id;
$query = DB::table('expenses')
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
//->join('vendors', 'vendors.id', '=', 'expenses.vendor_id')
->where('expenses.account_id', '=', $accountid)
->where('expenses.deleted_at', '=', null)
->select(
//DB::raw('COALESCE(vendors.currency_id, accounts.currency_id) currency_id'),
//DB::raw('COALESCE(vendors.country_id, accounts.country_id) country_id'),
'expenses.public_id',
//'vendors.name as vendor_name',
//'vendors.public_id as vendor_public_id',
'expenses.amount',
'expenses.balance',
'expenses.expense_date',
'expenses.public_notes',
'expenses.deleted_at',
'expenses.is_deleted'
);
if (!\Session::get('show_trash:expense')) {
$query->where('expenses.deleted_at', '=', null);
}
/*
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('vendors.name', 'like', '%'.$filter.'%');
});
}
*/
return $query;
}

View File

@ -5,6 +5,7 @@ use URL;
use App\Services\BaseService;
use App\Ninja\Repositories\ExpenseRepository;
class ExpenseService extends BaseService
{
protected $expenseRepo;
@ -26,33 +27,33 @@ class ExpenseService extends BaseService
return $this->expenseRepo->save($data);
}
public function getDatatable($vendorPublicId, $search)
public function getDatatable($search)
{
$query = $this->expenseRepo->find($vendorPublicId, $search);
$query = $this->expenseRepo->find($search);
return $this->createDatatable(ENTITY_CREDIT, $query, !$vendorPublicId);
return $this->createDatatable(ENTITY_EXPENSE, $query);
}
protected function getDatatableColumns($entityType, $hideClient)
{
return [
[
/*[
'vendor_name',
function ($model) {
return $model->vendor_public_id ? link_to("vendors/{$model->vendor_public_id}", Utils::getVendorDisplayName($model)) : '';
},
! $hideClient
],
],*/
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id) . '<span '.Utils::getEntityRowClass($model).'/>';
return Utils::formatMoney($model->amount, false, false) . '<span '.Utils::getEntityRowClass($model).'/>';
}
],
[
'balance',
function ($model) {
return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
return Utils::formatMoney($model->balance, false, false);
}
],
[
@ -62,14 +63,14 @@ class ExpenseService extends BaseService
}
],
[
'private_notes',
'private_public',
function ($model) {
return $model->private_notes;
return $model->public_notes;
}
]
];
}
/*
protected function getDatatableActions($entityType)
{
return [
@ -81,4 +82,5 @@ class ExpenseService extends BaseService
]
];
}
*/
}

View File

@ -32,6 +32,7 @@ class CreateExpensesTable extends Migration
$table->date('expense_date')->nullable();
$table->string('expense_number')->nullable();
$table->text('private_notes');
$table->text('public_notes');
$table->integer('currency_id',false, true)->nullable();
$table->boolean('is_invoiced')->default(false);
$table->boolean('should_be_invoiced')->default(true);

View File

@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExpenseActivitiesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('expense_activities');
Schema::create('expense_activities', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('account_id');
$table->unsignedInteger('vendor_id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('contact_id')->nullable();
$table->unsignedInteger('expense_id');
$table->unsignedInteger('invitation_id')->nullable();
$table->text('message')->nullable();
$table->text('json_backup')->nullable();
$table->integer('activity_type_id');
$table->decimal('adjustment', 13, 2)->nullable();
$table->decimal('balance', 13, 2)->nullable();
$table->unsignedInteger('token_id')->nullable();
$table->string('ip')->nullable();
$table->boolean('is_system')->default(0);
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade');
$table->foreign('expense_id')->references('id')->on('expenses')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('expense_activities');
}
}

View File

@ -896,7 +896,7 @@ return array(
'activity_31' => ':user created :vendor',
'activity_32' => ':user created :vendor',
'activity_33' => ':user created :vendor',
'activity_34' => ':user created :vendor',
'activity_34' => ':user created :expense',
'activity_35' => ':user created :vendor',
'activity_36' => ':user created :vendor',
'activity_37' => ':user created :vendor',
@ -1030,4 +1030,6 @@ return array(
'expense_amount' => 'Expense amount',
'expense_balance' => 'Expense balance',
'expense_date' => 'Expense date',
'expense_exchange_rate_100' => 'The amount for 100 in company currency',
'expense_should_be_invoiced' => 'Should this expense be invoiced?',
);