1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Ninja/Repositories/ActivityRepository.php

110 lines
4.2 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Ninja\Repositories;
2015-10-28 20:22:07 +01:00
use DB;
use Auth;
use Utils;
use Request;
use App\Models\Activity;
use App\Models\Client;
2016-07-19 08:57:17 +02:00
use App\Models\Invitation;
2015-10-28 20:22:07 +01:00
class ActivityRepository
{
public function create($entity, $activityTypeId, $balanceChange = 0, $paidToDateChange = 0, $altEntity = null)
{
if ($entity instanceof Client) {
$client = $entity;
} elseif ($entity instanceof Invitation) {
$client = $entity->invoice->client;
} else {
$client = $entity->client;
}
// init activity and copy over context
2016-07-10 12:20:22 +02:00
$activity = self::getBlank($altEntity ?: ($client ?: $entity));
2015-10-28 20:22:07 +01:00
$activity = Utils::copyContext($activity, $entity);
$activity = Utils::copyContext($activity, $altEntity);
$activity->activity_type_id = $activityTypeId;
$activity->adjustment = $balanceChange;
2016-07-10 12:20:22 +02:00
$activity->client_id = $client ? $client->id : 0;
$activity->balance = $client ? ($client->balance + $balanceChange) : 0;
2015-10-28 20:22:07 +01:00
$keyField = $entity->getKeyField();
$activity->$keyField = $entity->id;
$activity->ip = Request::getClientIp();
$activity->save();
2016-07-10 12:20:22 +02:00
if ($client) {
$client->updateBalances($balanceChange, $paidToDateChange);
}
2015-10-28 20:22:07 +01:00
return $activity;
}
private function getBlank($entity)
{
$activity = new Activity();
2015-12-02 14:26:06 +01:00
if (Auth::check() && Auth::user()->account_id == $entity->account_id) {
2015-10-28 20:22:07 +01:00
$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;
if ( ! $entity instanceof Invitation) {
$activity->is_system = true;
}
}
$activity->token_id = session('token_id');
return $activity;
}
2015-11-16 13:30:44 +01:00
public function findByClientId($clientId)
2015-10-28 20:22:07 +01:00
{
return DB::table('activities')
->join('accounts', 'accounts.id', '=', 'activities.account_id')
2015-10-28 20:22:07 +01:00
->join('users', 'users.id', '=', 'activities.user_id')
->join('clients', 'clients.id', '=', 'activities.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->leftJoin('invoices', 'invoices.id', '=', 'activities.invoice_id')
->leftJoin('payments', 'payments.id', '=', 'activities.payment_id')
->leftJoin('credits', 'credits.id', '=', 'activities.credit_id')
2015-11-16 13:30:44 +01:00
->where('clients.id', '=', $clientId)
2015-10-28 20:22:07 +01:00
->where('contacts.is_primary', '=', 1)
->whereNull('contacts.deleted_at')
->select(
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
2015-11-05 23:37:04 +01:00
'activities.id',
'activities.created_at',
'activities.contact_id',
'activities.activity_type_id',
'activities.is_system',
2015-10-28 20:22:07 +01:00
'activities.balance',
2015-11-05 23:37:04 +01:00
'activities.adjustment',
'users.first_name as user_first_name',
'users.last_name as user_last_name',
'users.email as user_email',
'invoices.invoice_number as invoice',
'invoices.public_id as invoice_public_id',
'invoices.is_recurring',
2015-10-28 20:22:07 +01:00
'clients.name as client_name',
'accounts.name as account_name',
2015-10-28 20:22:07 +01:00
'clients.public_id as client_public_id',
2015-11-05 23:37:04 +01:00
'contacts.id as contact',
'contacts.first_name as first_name',
2015-10-28 20:22:07 +01:00
'contacts.last_name as last_name',
'contacts.email as email',
'payments.transaction_reference as payment',
2016-04-23 22:40:19 +02:00
'payments.amount as payment_amount',
2015-11-05 23:37:04 +01:00
'credits.amount as credit'
);
2015-10-28 20:22:07 +01:00
}
}