2015-03-24 09:21:12 +01:00
< ? php namespace App\Ninja\Repositories ;
2015-03-16 22:45:25 +01:00
use Auth ;
2015-03-24 08:35:20 +01:00
use Request ;
use Session ;
2015-03-16 22:45:25 +01:00
use Utils ;
2015-06-16 21:35:35 +02:00
use DB ;
use stdClass ;
2015-03-16 22:45:25 +01:00
2015-03-31 11:38:24 +02:00
use App\Models\AccountGateway ;
use App\Models\Invitation ;
use App\Models\Invoice ;
use App\Models\InvoiceItem ;
2015-03-24 08:35:20 +01:00
use App\Models\Client ;
2015-04-05 21:15:37 +02:00
use App\Models\Language ;
2015-03-24 08:35:20 +01:00
use App\Models\Contact ;
use App\Models\Account ;
use App\Models\User ;
2015-06-16 21:35:35 +02:00
use App\Models\UserAccount ;
2015-03-24 08:35:20 +01:00
2015-03-16 22:45:25 +01:00
class AccountRepository
{
2015-03-29 14:37:42 +02:00
public function create ( $firstName = '' , $lastName = '' , $email = '' , $password = '' )
2015-03-16 22:45:25 +01:00
{
$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 );
if ( $language = Language :: whereLocale ( $locale ) -> first ()) {
$account -> language_id = $language -> id ;
}
}
$account -> save ();
$user = new User ();
2015-03-29 14:37:42 +02:00
if ( ! $firstName && ! $lastName && ! $email && ! $password ) {
$user -> password = str_random ( RANDOM_KEY_LENGTH );
2015-05-09 20:25:16 +02:00
$user -> username = str_random ( RANDOM_KEY_LENGTH );
2015-03-29 14:37:42 +02:00
} else {
$user -> first_name = $firstName ;
$user -> last_name = $lastName ;
$user -> email = $user -> username = $email ;
$user -> password = bcrypt ( $password );
}
2015-03-16 22:45:25 +01:00
$user -> confirmed = ! Utils :: isNinja ();
2015-05-05 11:48:23 +02:00
$user -> registered = ! Utils :: isNinja () && $user -> email ;
2015-03-29 14:37:42 +02:00
2015-04-05 21:15:37 +02:00
if ( ! $user -> confirmed ) {
$user -> confirmation_code = str_random ( RANDOM_KEY_LENGTH );
}
2015-03-29 14:37:42 +02:00
$account -> users () -> save ( $user );
2015-03-16 22:45:25 +01:00
return $account ;
}
public function getSearchData ()
{
$clients = \DB :: table ( 'clients' )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
-> whereRaw ( " clients.name <> '' " )
-> select ( \DB :: raw ( " 'Clients' as type, clients.public_id, clients.name, '' as token " ));
$contacts = \DB :: table ( 'clients' )
-> join ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
-> 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 " ));
$invoices = \DB :: table ( 'clients' )
-> join ( 'invoices' , 'invoices.client_id' , '=' , 'clients.id' )
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'invoices.deleted_at' , '=' , null )
-> select ( \DB :: raw ( " 'Invoices' as type, invoices.public_id, CONCAT(invoices.invoice_number, ': ', clients.name) as name, invoices.invoice_number as token " ));
$data = [];
foreach ( $clients -> union ( $contacts ) -> union ( $invoices ) -> get () as $row ) {
$type = $row -> type ;
if ( ! isset ( $data [ $type ])) {
$data [ $type ] = [];
}
$tokens = explode ( ' ' , $row -> name );
$tokens [] = $type ;
if ( $type == 'Invoices' ) {
$tokens [] = intVal ( $row -> token ) . '' ;
}
$data [ $type ][] = [
'value' => $row -> name ,
'public_id' => $row -> public_id ,
'tokens' => $tokens ,
];
}
return $data ;
}
public function enableProPlan ()
{
if ( Auth :: user () -> isPro ()) {
return false ;
}
2015-05-08 10:21:29 +02:00
$client = $this -> getNinjaClient ( Auth :: user () -> account );
$invitation = $this -> createNinjaInvoice ( $client );
2015-03-16 22:45:25 +01:00
return $invitation ;
}
2015-05-08 10:21:29 +02:00
public function createNinjaInvoice ( $client )
2015-03-16 22:45:25 +01:00
{
2015-05-08 10:21:29 +02:00
$account = $this -> getNinjaAccount ();
$lastInvoice = Invoice :: withTrashed () -> whereAccountId ( $account -> id ) -> orderBy ( 'public_id' , 'DESC' ) -> first ();
$publicId = $lastInvoice ? ( $lastInvoice -> public_id + 1 ) : 1 ;
2015-03-16 22:45:25 +01: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 $invitation ;
}
public function getNinjaAccount ()
{
$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 -> username = $random ;
$user -> first_name = 'Invoice' ;
$user -> last_name = 'Ninja' ;
$user -> notify_sent = true ;
$user -> notify_paid = true ;
$account -> users () -> save ( $user );
$accountGateway = new AccountGateway ();
$accountGateway -> user_id = $user -> id ;
$accountGateway -> gateway_id = NINJA_GATEWAY_ID ;
$accountGateway -> public_id = 1 ;
$accountGateway -> config = NINJA_GATEWAY_CONFIG ;
$account -> account_gateways () -> save ( $accountGateway );
}
return $account ;
}
2015-05-08 10:21:29 +02:00
public function getNinjaClient ( $account )
2015-03-16 22:45:25 +01:00
{
2015-05-08 10:21:29 +02:00
$account -> load ( 'users' );
$ninjaAccount = $this -> getNinjaAccount ();
$client = Client :: whereAccountId ( $ninjaAccount -> id ) -> wherePublicId ( $account -> id ) -> first ();
2015-03-16 22:45:25 +01:00
if ( ! $client ) {
$client = new Client ();
2015-05-08 10:21:29 +02:00
$client -> public_id = $account -> id ;
2015-03-16 22:45:25 +01:00
$client -> user_id = $ninjaAccount -> users () -> first () -> id ;
$client -> currency_id = 1 ;
foreach ([ 'name' , 'address1' , 'address2' , 'city' , 'state' , 'postal_code' , 'country_id' , 'work_phone' ] as $field ) {
2015-05-08 10:21:29 +02:00
$client -> $field = $account -> $field ;
2015-03-16 22:45:25 +01:00
}
$ninjaAccount -> clients () -> save ( $client );
$contact = new Contact ();
$contact -> user_id = $ninjaAccount -> users () -> first () -> id ;
$contact -> account_id = $ninjaAccount -> id ;
2015-05-08 10:21:29 +02:00
$contact -> public_id = $account -> id ;
2015-03-16 22:45:25 +01:00
$contact -> is_primary = true ;
foreach ([ 'first_name' , 'last_name' , 'email' , 'phone' ] as $field ) {
2015-05-08 10:21:29 +02:00
$contact -> $field = $account -> users () -> first () -> $field ;
2015-03-16 22:45:25 +01:00
}
$client -> contacts () -> save ( $contact );
}
return $client ;
}
public function registerUser ( $user )
{
2015-05-08 10:21:29 +02:00
$url = ( Utils :: isNinjaDev () ? '' : NINJA_APP_URL ) . '/signup/register' ;
2015-03-16 22:45:25 +01:00
$data = '' ;
$fields = [
2015-05-08 10:21:29 +02:00
'first_name' => urlencode ( $user -> first_name ),
'last_name' => urlencode ( $user -> last_name ),
'email' => urlencode ( $user -> email ),
];
2015-03-16 22:45:25 +01:00
foreach ( $fields as $key => $value ) {
$data .= $key . '=' . $value . '&' ;
}
rtrim ( $data , '&' );
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $url );
curl_setopt ( $ch , CURLOPT_POST , count ( $fields ));
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data );
curl_exec ( $ch );
curl_close ( $ch );
}
2015-06-16 21:35:35 +02:00
public function findUserAccounts ( $userId1 , $userId2 = false )
{
$query = UserAccount :: where ( 'user_id1' , '=' , $userId1 )
-> orWhere ( 'user_id2' , '=' , $userId1 )
-> orWhere ( 'user_id3' , '=' , $userId1 )
-> orWhere ( 'user_id4' , '=' , $userId1 )
-> orWhere ( 'user_id5' , '=' , $userId1 );
if ( $userId2 ) {
$query -> orWhere ( 'user_id1' , '=' , $userId2 )
-> orWhere ( 'user_id2' , '=' , $userId2 )
-> orWhere ( 'user_id3' , '=' , $userId2 )
-> orWhere ( 'user_id4' , '=' , $userId2 )
-> orWhere ( 'user_id5' , '=' , $userId2 );
}
return $query -> first ([ 'id' , 'user_id1' , 'user_id2' , 'user_id3' , 'user_id4' , 'user_id5' ]);
}
public function prepareUsersData ( $record ) {
if ( ! $record ) {
return false ;
}
$userIds = [];
for ( $i = 1 ; $i <= 5 ; $i ++ ) {
$field = " user_id $i " ;
if ( $record -> $field ) {
$userIds [] = $record -> $field ;
}
}
$users = User :: with ( 'account' )
-> whereIn ( 'id' , $userIds )
-> get ();
$data = [];
foreach ( $users as $user ) {
$item = new stdClass ();
$item -> id = $record -> id ;
$item -> user_id = $user -> id ;
$item -> user_name = $user -> getDisplayName ();
$item -> account_id = $user -> account -> id ;
$item -> account_name = $user -> account -> getDisplayName ();
$item -> pro_plan_paid = $user -> account -> pro_plan_paid ;
$data [] = $item ;
}
return $data ;
}
public function loadAccounts ( $userId ) {
$record = self :: findUserAccounts ( $userId );
return self :: prepareUsersData ( $record );
}
public function syncAccounts ( $userId , $proPlanPaid ) {
$users = self :: loadAccounts ( $userId );
self :: syncUserAccounts ( $users , $proPlanPaid );
}
public function syncUserAccounts ( $users , $proPlanPaid = false ) {
if ( ! $proPlanPaid ) {
foreach ( $users as $user ) {
if ( $user -> pro_plan_paid && $user -> pro_plan_paid != '0000-00-00' ) {
$proPlanPaid = $user -> pro_plan_paid ;
break ;
}
}
}
if ( ! $proPlanPaid ) {
return ;
}
$accountIds = [];
foreach ( $users as $user ) {
if ( $user -> pro_plan_paid != $proPlanPaid ) {
$accountIds [] = $user -> account_id ;
}
}
if ( count ( $accountIds )) {
DB :: table ( 'accounts' )
-> whereIn ( 'id' , $accountIds )
-> update ([ 'pro_plan_paid' => $proPlanPaid ]);
}
}
public function associateAccounts ( $userId1 , $userId2 ) {
$record = self :: findUserAccounts ( $userId1 , $userId2 );
if ( $record ) {
foreach ([ $userId1 , $userId2 ] as $userId ) {
if ( ! $record -> hasUserId ( $userId )) {
$record -> setUserId ( $userId );
}
}
} else {
$record = new UserAccount ();
$record -> user_id1 = $userId1 ;
$record -> user_id2 = $userId2 ;
}
$record -> save ();
$users = self :: prepareUsersData ( $record );
self :: syncUserAccounts ( $users );
return $users ;
}
public function unlinkAccount ( $userAccountId , $userId ) {
$userAccount = UserAccount :: whereId ( $userAccountId ) -> first ();
if ( $userAccount -> hasUserId ( Auth :: user () -> id )) {
$userAccount -> removeUserId ( $userId );
$userAccount -> save ();
}
}
2015-03-16 22:45:25 +01:00
}