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

433 lines
12 KiB
PHP
Raw Normal View History

2013-11-26 22:26:08 +01:00
<?php
2013-12-01 21:58:25 +01:00
2013-11-26 22:45:10 +01:00
define("ACTIVITY_TYPE_CREATE_CLIENT", 1);
define("ACTIVITY_TYPE_ARCHIVE_CLIENT", 2);
2013-12-01 21:58:25 +01:00
define("ACTIVITY_TYPE_DELETE_CLIENT", 3);
2014-01-14 12:52:56 +01:00
2013-12-01 21:58:25 +01:00
define("ACTIVITY_TYPE_CREATE_INVOICE", 4);
2013-12-30 21:17:45 +01:00
define("ACTIVITY_TYPE_UPDATE_INVOICE", 5);
define("ACTIVITY_TYPE_EMAIL_INVOICE", 6);
define("ACTIVITY_TYPE_VIEW_INVOICE", 7);
define("ACTIVITY_TYPE_ARCHIVE_INVOICE", 8);
define("ACTIVITY_TYPE_DELETE_INVOICE", 9);
2014-01-14 12:52:56 +01:00
2013-12-30 21:17:45 +01:00
define("ACTIVITY_TYPE_CREATE_PAYMENT", 10);
2014-01-14 12:52:56 +01:00
define("ACTIVITY_TYPE_UPDATE_PAYMENT", 11);
define("ACTIVITY_TYPE_ARCHIVE_PAYMENT", 12);
define("ACTIVITY_TYPE_DELETE_PAYMENT", 13);
define("ACTIVITY_TYPE_CREATE_CREDIT", 14);
define("ACTIVITY_TYPE_UPDATE_CREDIT", 15);
define("ACTIVITY_TYPE_ARCHIVE_CREDIT", 16);
define("ACTIVITY_TYPE_DELETE_CREDIT", 17);
2013-12-10 23:10:43 +01:00
2013-11-26 22:45:10 +01:00
2013-11-26 22:26:08 +01:00
class Activity extends Eloquent
{
2014-01-08 21:09:47 +01:00
public $timestamps = true;
protected $softDelete = false;
2013-12-03 18:32:33 +01:00
public function scopeScope($query)
{
return $query->whereAccountId(Auth::user()->account_id);
}
2013-12-29 18:40:11 +01:00
public function account()
{
return $this->belongsTo('Account');
}
2013-12-10 18:18:35 +01:00
private static function getBlank($entity = false)
2013-11-26 22:26:08 +01:00
{
$activity = new Activity;
2013-12-10 18:18:35 +01:00
2014-01-14 12:52:56 +01:00
if ($entity)
{
2013-12-10 18:18:35 +01:00
$activity->user_id = $entity->user_id;
$activity->account_id = $entity->account_id;
2014-01-14 12:52:56 +01:00
}
2014-02-03 11:34:58 +01:00
else if (Auth::check())
2014-01-14 12:52:56 +01:00
{
2014-01-09 12:24:03 +01:00
$activity->user_id = Auth::user()->id;
$activity->account_id = Auth::user()->account_id;
2014-01-14 12:52:56 +01:00
}
else
{
2014-01-02 00:12:33 +01:00
Utils::fatalError();
2013-12-10 18:18:35 +01:00
}
2013-11-26 22:26:08 +01:00
return $activity;
2013-11-26 22:26:08 +01:00
}
public static function createClient($client)
2013-12-01 08:33:17 +01:00
{
2013-11-26 22:26:08 +01:00
$activity = Activity::getBlank();
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_CREATE_CLIENT;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'created', $client);
2013-12-01 08:33:17 +01:00
$activity->save();
2013-11-26 22:45:10 +01:00
}
2013-11-26 22:26:08 +01:00
2014-01-08 00:59:06 +01:00
public static function updateClient($client)
{
if ($client->is_deleted && !$client->getOriginal('is_deleted'))
{
$activity = Activity::getBlank();
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_CLIENT;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted', $client);
2014-01-08 00:59:06 +01:00
$activity->save();
}
}
2013-11-26 22:26:08 +01:00
public static function archiveClient($client)
{
2014-01-08 00:59:06 +01:00
if (!$client->is_deleted)
{
$activity = Activity::getBlank();
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_CLIENT;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'archived', $client);
2014-01-08 00:59:06 +01:00
$activity->balance = $client->balance;
$activity->save();
}
2013-11-26 22:26:08 +01:00
}
public static function createInvoice($invoice)
{
2014-03-23 10:30:48 +01:00
if (Auth::check())
2013-12-30 21:17:45 +01:00
{
2014-03-23 10:30:48 +01:00
$message = Utils::encodeActivity(Auth::user(), 'created', $invoice);
2013-12-30 21:17:45 +01:00
}
else
{
2014-03-23 10:30:48 +01:00
$message = Utils::encodeActivity(null, 'created', $invoice);
2013-12-11 12:11:59 +01:00
}
2014-02-01 21:01:32 +01:00
$client = $invoice->client;
$adjustment = $invoice->amount;
$client->balance = $client->balance + $adjustment;
$client->save();
2013-12-10 18:18:35 +01:00
$activity = Activity::getBlank($invoice);
2013-11-26 22:26:08 +01:00
$activity->invoice_id = $invoice->id;
$activity->client_id = $invoice->client_id;
$activity->activity_type_id = ACTIVITY_TYPE_CREATE_INVOICE;
2013-12-11 12:11:59 +01:00
$activity->message = $message;
2014-02-01 21:01:32 +01:00
$activity->balance = $client->balance;
$activity->adjustment = $adjustment;
2013-11-26 22:26:08 +01:00
$activity->save();
}
public static function archiveInvoice($invoice)
{
2014-01-08 00:59:06 +01:00
if ($invoice->invoice_status_id < INVOICE_STATUS_SENT)
{
return;
}
if (!$invoice->is_deleted)
{
if ($invoice->balance > 0)
{
$client = $invoice->client;
$client->balance = $client->balance - $invoice->balance;
$client->save();
}
$activity = Activity::getBlank();
$activity->invoice_id = $invoice->id;
$activity->client_id = $invoice->client_id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_INVOICE;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'archived', $invoice);
2014-01-09 11:39:20 +01:00
$activity->balance = $invoice->client->balance;
2014-01-08 00:59:06 +01:00
$activity->adjustment = $invoice->balance;
$activity->save();
}
2013-11-26 22:26:08 +01:00
}
2013-11-28 17:40:13 +01:00
public static function emailInvoice($invitation)
2013-11-26 22:26:08 +01:00
{
2013-12-30 21:17:45 +01:00
$adjustment = 0;
2014-01-02 00:12:33 +01:00
$client = $invitation->invoice->client;
2013-12-30 21:17:45 +01:00
2013-12-10 18:18:35 +01:00
$activity = Activity::getBlank($invitation);
2013-12-02 13:22:29 +01:00
$activity->client_id = $invitation->invoice->client_id;
2013-11-28 17:40:13 +01:00
$activity->invoice_id = $invitation->invoice_id;
$activity->contact_id = $invitation->contact_id;
2013-11-26 22:26:08 +01:00
$activity->activity_type_id = ACTIVITY_TYPE_EMAIL_INVOICE;
2014-01-09 00:22:56 +01:00
$activity->message = Utils::encodeActivity(Auth::check() ? Auth::user() : null, 'emailed', $invitation->invoice, $invitation->contact);
2014-01-02 00:12:33 +01:00
$activity->balance = $client->balance;
2013-12-30 21:17:45 +01:00
$activity->save();
}
public static function updateInvoice($invoice)
{
2014-01-08 00:59:06 +01:00
if ($invoice->is_deleted && !$invoice->getOriginal('is_deleted'))
2013-12-30 21:17:45 +01:00
{
2014-01-08 00:59:06 +01:00
if ($invoice->balance > 0)
{
$client = $invoice->client;
$client->balance = $client->balance - $invoice->balance;
$client->save();
}
$activity = Activity::getBlank();
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_INVOICE;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted', $invoice);
2014-01-09 11:39:20 +01:00
$activity->balance = $invoice->client->balance;
2014-01-08 00:59:06 +01:00
$activity->adjustment = $invoice->balance * -1;
$activity->save();
2013-12-30 21:17:45 +01:00
}
2014-01-08 00:59:06 +01:00
else
{
$diff = floatval($invoice->amount) - floatval($invoice->getOriginal('amount'));
if ($diff == 0)
{
return;
}
2013-12-30 21:17:45 +01:00
2014-01-08 00:59:06 +01:00
$backupInvoice = Invoice::with('invoice_items', 'client.account', 'client.contacts')->find($invoice->id);
2013-12-31 17:38:49 +01:00
2014-01-08 00:59:06 +01:00
$client = $invoice->client;
$client->balance = $client->balance + $diff;
$client->save();
2013-12-30 21:17:45 +01:00
2014-01-08 00:59:06 +01:00
$activity = Activity::getBlank($invoice);
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_UPDATE_INVOICE;
2014-01-08 21:09:47 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'updated', $invoice);
2014-01-08 00:59:06 +01:00
$activity->balance = $client->balance;
$activity->adjustment = $diff;
$activity->json_backup = $backupInvoice->hidePrivateFields()->toJSON();
$activity->save();
}
2013-11-26 22:26:08 +01:00
}
2013-12-30 21:17:45 +01:00
2014-01-14 12:52:56 +01:00
public static function viewInvoice($invitation)
{
if (Session::get($invitation->invitation_key))
{
return;
}
Session::put($invitation->invitation_key, true);
$invoice = $invitation->invoice;
if (!$invoice->isViewed())
{
$invoice->invoice_status_id = INVOICE_STATUS_VIEWED;
$invoice->save();
}
$now = Carbon::now()->toDateTimeString();
$invitation->viewed_date = $now;
$invitation->save();
$client = $invoice->client;
$client->last_login = $now;
$client->save();
$activity = Activity::getBlank($invitation);
2014-01-14 12:52:56 +01:00
$activity->client_id = $invitation->invoice->client_id;
$activity->invitation_id = $invitation->id;
$activity->contact_id = $invitation->contact_id;
$activity->invoice_id = $invitation->invoice_id;
$activity->activity_type_id = ACTIVITY_TYPE_VIEW_INVOICE;
$activity->message = Utils::encodeActivity($invitation->contact, 'viewed', $invitation->invoice);
$activity->balance = $invitation->invoice->client->balance;
$activity->save();
}
2013-11-26 22:45:10 +01:00
public static function createPayment($payment)
2013-11-26 22:26:08 +01:00
{
2013-12-30 21:17:45 +01:00
$client = $payment->client;
$client->balance = $client->balance - $payment->amount;
2014-01-08 00:59:06 +01:00
$client->paid_to_date = $client->paid_to_date + $payment->amount;
2013-12-30 21:17:45 +01:00
$client->save();
2014-02-03 11:34:58 +01:00
if ($payment->contact_id)
2013-11-26 22:26:08 +01:00
{
$activity = Activity::getBlank($client);
2013-11-26 22:45:10 +01:00
$activity->contact_id = $payment->contact_id;
2014-01-16 22:12:46 +01:00
$activity->message = Utils::encodeActivity($payment->invitation->contact, 'entered payment');
2013-11-26 22:26:08 +01:00
}
2014-02-03 11:34:58 +01:00
else
{
$activity = Activity::getBlank();
$message = $payment->payment_type_id == PAYMENT_TYPE_CREDIT ? 'applied credit' : 'entered payment';
$activity->message = Utils::encodeActivity(Auth::user(), $message);
}
2013-11-26 22:26:08 +01:00
$activity->payment_id = $payment->id;
2014-01-02 00:12:33 +01:00
if ($payment->invoice_id)
{
2013-12-05 16:23:24 +01:00
$activity->invoice_id = $payment->invoice_id;
2014-01-02 00:12:33 +01:00
$invoice = $payment->invoice;
$invoice->balance = $invoice->balance - $payment->amount;
$invoice->save();
2013-12-05 16:23:24 +01:00
}
2014-01-02 00:12:33 +01:00
2014-01-16 22:12:46 +01:00
$activity->payment_id = $payment->id;
2013-12-05 16:23:24 +01:00
$activity->client_id = $payment->client_id;
2013-11-26 22:26:08 +01:00
$activity->activity_type_id = ACTIVITY_TYPE_CREATE_PAYMENT;
2013-12-30 21:17:45 +01:00
$activity->balance = $client->balance;
$activity->adjustment = $payment->amount * -1;
2013-11-26 22:26:08 +01:00
$activity->save();
}
2014-01-14 12:52:56 +01:00
public static function updatePayment($payment)
{
if ($payment->is_deleted && !$payment->getOriginal('is_deleted'))
{
$client = $payment->client;
$client->balance = $client->balance + $payment->amount;
$client->paid_to_date = $client->paid_to_date - $payment->amount;
$client->save();
$invoice = $payment->invoice;
$invoice->balance = $invoice->balance + $payment->amount;
$invoice->save();
$activity = Activity::getBlank();
2014-01-16 22:12:46 +01:00
$activity->payment_id = $payment->id;
2014-01-14 12:52:56 +01:00
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_PAYMENT;
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted payment');
$activity->balance = $client->balance;
$activity->adjustment = $payment->amount;
$activity->save();
}
else
{
/*
$diff = floatval($invoice->amount) - floatval($invoice->getOriginal('amount'));
if ($diff == 0)
{
return;
}
$client = $invoice->client;
$client->balance = $client->balance + $diff;
$client->save();
$activity = Activity::getBlank($invoice);
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_UPDATE_INVOICE;
$activity->message = Utils::encodeActivity(Auth::user(), 'updated', $invoice);
$activity->balance = $client->balance;
$activity->adjustment = $diff;
$activity->json_backup = $backupInvoice->hidePrivateFields()->toJSON();
$activity->save();
*/
}
}
public static function archivePayment($payment)
{
if ($payment->is_deleted)
{
return;
}
$client = $payment->client;
$client->balance = $client->balance + $payment->amount;
$client->paid_to_date = $client->paid_to_date - $payment->amount;
$client->save();
$invoice = $payment->invoice;
$invoice->balance = $invoice->balance + $payment->amount;
$invoice->save();
$activity = Activity::getBlank();
2014-01-16 22:12:46 +01:00
$activity->payment_id = $payment->id;
2014-01-14 12:52:56 +01:00
$activity->invoice_id = $invoice->id;
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_PAYMENT;
$activity->message = Utils::encodeActivity(Auth::user(), 'archived payment');
$activity->balance = $client->balance;
$activity->adjustment = $payment->amount;
$activity->save();
}
2013-12-05 16:23:24 +01:00
public static function createCredit($credit)
{
$activity = Activity::getBlank();
2014-01-16 22:12:46 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'entered ' . Utils::formatMoney($credit->amount, $credit->client->currency_id) . ' credit');
2013-12-05 16:23:24 +01:00
$activity->credit_id = $credit->id;
$activity->client_id = $credit->client_id;
$activity->activity_type_id = ACTIVITY_TYPE_CREATE_CREDIT;
2014-01-16 22:12:46 +01:00
$activity->balance = $credit->client->balance;
2013-12-05 16:23:24 +01:00
$activity->save();
}
2014-01-14 12:52:56 +01:00
public static function updateCredit($credit)
2013-11-26 22:26:08 +01:00
{
2014-01-14 12:52:56 +01:00
if ($credit->is_deleted && !$credit->getOriginal('is_deleted'))
{
$activity = Activity::getBlank();
2014-01-16 22:12:46 +01:00
$activity->credit_id = $credit->id;
$activity->client_id = $credit->client_id;
2014-01-14 12:52:56 +01:00
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_CREDIT;
2014-01-16 22:12:46 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted ' . Utils::formatMoney($credit->balance, $credit->client->currency_id) . ' credit');
$activity->balance = $credit->client->balance;
2014-01-14 12:52:56 +01:00
$activity->save();
}
else
{
/*
$diff = floatval($invoice->amount) - floatval($invoice->getOriginal('amount'));
if ($diff == 0)
{
return;
}
$client = $invoice->client;
$client->balance = $client->balance + $diff;
$client->save();
$activity = Activity::getBlank($invoice);
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_UPDATE_INVOICE;
$activity->message = Utils::encodeActivity(Auth::user(), 'updated', $invoice);
$activity->balance = $client->balance;
$activity->adjustment = $diff;
$activity->json_backup = $backupInvoice->hidePrivateFields()->toJSON();
$activity->save();
*/
}
}
public static function archiveCredit($credit)
2013-11-26 22:26:08 +01:00
{
2014-01-14 12:52:56 +01:00
if ($credit->is_deleted)
2014-01-13 20:22:43 +01:00
{
return;
}
2014-01-14 12:52:56 +01:00
$activity = Activity::getBlank();
$activity->client_id = $client->id;
2014-01-16 22:12:46 +01:00
$activity->credit_id = $credit->id;
2014-01-14 12:52:56 +01:00
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_CREDIT;
2014-01-16 22:12:46 +01:00
$activity->message = Utils::encodeActivity(Auth::user(), 'archived ' . Utils::formatMoney($credit->balance, $credit->client->currency_id) . ' credit');
$activity->balance = $credit->client->balance;
2013-11-26 22:26:08 +01:00
$activity->save();
2013-11-26 22:45:10 +01:00
}
2013-11-26 22:26:08 +01:00
}