1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/ninja/repositories/AccountRepository.php

223 lines
6.2 KiB
PHP
Raw Normal View History

2013-12-26 01:36:34 +01:00
<?php namespace ninja\repositories;
use Client;
use Contact;
2014-04-06 11:02:04 +02:00
use Account;
use Request;
use Session;
use Language;
use User;
2014-04-13 10:19:10 +02:00
use Auth;
use Invitation;
use Invoice;
use InvoiceItem;
2014-04-13 15:34:58 +02:00
use AccountGateway;
2013-12-26 01:36:34 +01:00
class AccountRepository
{
2014-04-06 11:02:04 +02:00
public function create()
{
$account = new Account;
$account->ip = Request::getClientIp();
$account->account_key = str_random(RANDOM_KEY_LENGTH);
if (Session::has(SESSION_LOCALE))
{
$locale = Session::get(SESSION_LOCALE);
2014-04-14 13:33:43 +02:00
if ($language = Language::whereLocale($locale)->first())
2014-04-06 11:02:04 +02:00
{
$account->language_id = $language->id;
}
}
$account->save();
$random = str_random(RANDOM_KEY_LENGTH);
$user = new User;
$user->password = $random;
$user->password_confirmation = $random;
$user->username = $random;
$account->users()->save($user);
return $account;
}
2013-12-26 01:36:34 +01:00
public function getSearchData()
{
$clients = \DB::table('clients')
->where('clients.deleted_at', '=', null)
2014-01-09 22:38:18 +01:00
->where('clients.account_id', '=', \Auth::user()->account_id)
2013-12-30 21:17:45 +01:00
->whereRaw("clients.name <> ''")
2013-12-27 10:10:32 +01:00
->select(\DB::raw("'Clients' as type, clients.public_id, clients.name, '' as token"));
2013-12-26 01:36:34 +01:00
$contacts = \DB::table('clients')
->join('contacts', 'contacts.client_id', '=', 'clients.id')
->where('clients.deleted_at', '=', null)
2014-01-09 22:38:18 +01:00
->where('clients.account_id', '=', \Auth::user()->account_id)
2013-12-30 21:17:45 +01:00
->whereRaw("CONCAT(contacts.first_name, contacts.last_name, contacts.email) <> ''")
->select(\DB::raw("'Contacts' as type, clients.public_id, CONCAT(contacts.first_name, ' ', contacts.last_name, ' ', contacts.email) as name, '' as token"));
2013-12-26 01:36:34 +01:00
$invoices = \DB::table('clients')
->join('invoices', 'invoices.client_id', '=', 'clients.id')
2014-01-09 22:38:18 +01:00
->where('clients.account_id', '=', \Auth::user()->account_id)
2013-12-26 01:36:34 +01:00
->where('clients.deleted_at', '=', null)
->where('invoices.deleted_at', '=', null)
2013-12-27 10:10:32 +01:00
->select(\DB::raw("'Invoices' as type, invoices.public_id, CONCAT(invoices.invoice_number, ': ', clients.name) as name, invoices.invoice_number as token"));
2013-12-26 01:36:34 +01:00
$data = [];
foreach ($clients->union($contacts)->union($invoices)->get() as $row)
{
2013-12-27 10:10:32 +01:00
$type = $row->type;
if (!isset($data[$type]))
{
$data[$type] = [];
}
$tokens = explode(' ', $row->name);
$tokens[] = $type;
if ($type == 'Invoices')
2013-12-26 01:36:34 +01:00
{
2013-12-27 10:10:32 +01:00
$tokens[] = intVal($row->token) . '';
2013-12-26 01:36:34 +01:00
}
2013-12-27 10:10:32 +01:00
$data[$type][] = [
2013-12-26 01:36:34 +01:00
'value' => $row->name,
2013-12-27 10:10:32 +01:00
'public_id' => $row->public_id,
'tokens' => $tokens
2013-12-26 01:36:34 +01:00
];
}
2013-12-27 10:10:32 +01:00
2013-12-26 01:36:34 +01:00
return $data;
}
2014-04-13 10:19:10 +02:00
public function enableProPlan()
{
if (Auth::user()->isPro())
{
return false;
}
2014-04-22 17:10:41 +02:00
$ninjaAccount = $this->getNinjaAccount();
$lastInvoice = Invoice::withTrashed()->whereAccountId($ninjaAccount->id)->orderBy('public_id', 'DESC')->first();
2014-04-13 15:34:58 +02:00
$publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1;
2014-04-13 10:19:10 +02:00
$ninjaClient = $this->getNinjaClient($ninjaAccount);
2014-04-13 15:34:58 +02:00
$invoice = $this->createNinjaInvoice($publicId, $ninjaAccount, $ninjaClient);
2014-04-13 10:19:10 +02:00
return $invoice;
}
2014-04-13 15:34:58 +02:00
private function createNinjaInvoice($publicId, $account, $client)
2014-04-13 10:19:10 +02:00
{
$invoice = new Invoice();
$invoice->account_id = $account->id;
$invoice->user_id = $account->users()->first()->id;
$invoice->public_id = $publicId;
$invoice->client_id = $client->id;
$invoice->invoice_number = $account->getNextInvoiceNumber();
$invoice->invoice_date = date_create()->format('Y-m-d');
$invoice->amount = PRO_PLAN_PRICE;
$invoice->balance = PRO_PLAN_PRICE;
$invoice->save();
$item = new InvoiceItem();
$item->account_id = $account->id;
$item->user_id = $account->users()->first()->id;
$item->public_id = $publicId;
$item->qty = 1;
$item->cost = PRO_PLAN_PRICE;
$item->notes = trans('texts.pro_plan_description');
$item->product_key = trans('texts.pro_plan_product');
$invoice->invoice_items()->save($item);
$invitation = new Invitation();
$invitation->account_id = $account->id;
$invitation->user_id = $account->users()->first()->id;
$invitation->public_id = $publicId;
$invitation->invoice_id = $invoice->id;
$invitation->contact_id = $client->contacts()->first()->id;
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
$invitation->save();
return $invoice;
}
2014-07-15 22:36:40 +02:00
public function getNinjaAccount()
2014-04-13 10:19:10 +02:00
{
$account = Account::whereAccountKey(NINJA_ACCOUNT_KEY)->first();
if ($account)
{
return $account;
}
else
{
$account = new Account();
$account->name = 'Invoice Ninja';
$account->work_email = 'contact@invoiceninja.com';
$account->work_phone = '(800) 763-1948';
$account->account_key = NINJA_ACCOUNT_KEY;
$account->save();
$random = str_random(RANDOM_KEY_LENGTH);
$user = new User();
$user->registered = true;
$user->confirmed = true;
$user->email = 'contact@invoiceninja.com';
$user->password = $random;
$user->password_confirmation = $random;
$user->username = $random;
$user->first_name = 'Invoice';
$user->last_name = 'Ninja';
2014-04-23 09:20:01 +02:00
$user->notify_sent = true;
$user->notify_paid = true;
2014-04-13 10:19:10 +02:00
$account->users()->save($user);
2014-04-13 15:34:58 +02:00
$accountGateway = new AccountGateway();
$accountGateway->user_id = $user->id;
$accountGateway->gateway_id = NINJA_GATEWAY_ID;
2014-04-22 17:10:41 +02:00
$accountGateway->public_id = 1;
2014-04-25 15:04:57 +02:00
$accountGateway->config = NINJA_GATEWAY_CONFIG;
2014-04-13 15:34:58 +02:00
$account->account_gateways()->save($accountGateway);
2014-04-13 10:19:10 +02:00
}
return $account;
}
private function getNinjaClient($ninjaAccount)
{
$client = Client::whereAccountId($ninjaAccount->id)->wherePublicId(Auth::user()->account_id)->first();
if (!$client)
{
$client = new Client;
$client->public_id = Auth::user()->account_id;
$client->user_id = $ninjaAccount->users()->first()->id;
2014-05-09 15:28:20 +02:00
$client->currency_id = 1;
2014-04-13 10:19:10 +02:00
foreach (['name', 'address1', 'address2', 'city', 'state', 'postal_code', 'country_id', 'work_phone'] as $field)
{
$client->$field = Auth::user()->account->$field;
}
$ninjaAccount->clients()->save($client);
$contact = new Contact;
$contact->user_id = $ninjaAccount->users()->first()->id;
2014-04-27 15:40:22 +02:00
$contact->account_id = $ninjaAccount->id;
2014-04-26 21:35:14 +02:00
$contact->public_id = Auth::user()->account_id;
2014-04-13 10:19:10 +02:00
$contact->is_primary = true;
foreach (['first_name', 'last_name', 'email', 'phone'] as $field)
{
$contact->$field = Auth::user()->$field;
}
$client->contacts()->save($contact);
}
return $client;
}
2013-12-26 01:36:34 +01:00
}