2017-01-30 21:40:43 +02:00
< ? php
namespace App\Ninja\Repositories ;
2016-08-15 06:46:47 +03:00
use App\Models\Activity ;
2016-09-11 17:30:23 +03:00
use App\Models\Invoice ;
use App\Models\Task ;
use DateInterval ;
use DatePeriod ;
2017-01-30 21:40:43 +02:00
use DB ;
use stdClass ;
2016-08-15 06:46:47 +03:00
class DashboardRepository
{
2016-09-11 17:30:23 +03:00
/**
* @ param $groupBy
* @ param $startDate
* @ param $endDate
2017-01-30 21:49:42 +02:00
* @ param mixed $account
* @ param mixed $currencyId
* @ param mixed $includeExpenses
2017-01-30 21:40:43 +02:00
*
2016-09-11 17:30:23 +03:00
* @ return array
*/
public function chartData ( $account , $groupBy , $startDate , $endDate , $currencyId , $includeExpenses )
{
$accountId = $account -> id ;
$startDate = date_create ( $startDate );
$endDate = date_create ( $endDate );
$groupBy = strtoupper ( $groupBy );
if ( $groupBy == 'DAY' ) {
$groupBy = 'DAYOFYEAR' ;
}
$datasets = [];
$labels = [];
2017-01-30 21:40:43 +02:00
$totals = new stdClass ();
2016-09-11 17:30:23 +03:00
$entitTypes = [ ENTITY_INVOICE , ENTITY_PAYMENT ];
if ( $includeExpenses ) {
$entitTypes [] = ENTITY_EXPENSE ;
}
foreach ( $entitTypes as $entityType ) {
$data = [];
$count = 0 ;
2016-11-02 09:43:57 +02:00
$balance = 0 ;
2019-01-30 22:00:26 +11:00
if ( $currencyId == 'totals' ) {
$records = $this -> rawChartDataTotals ( $entityType , $account , $groupBy , $startDate , $endDate , $account -> currency -> id );
} else {
$records = $this -> rawChartData ( $entityType , $account , $groupBy , $startDate , $endDate , $currencyId );
}
2016-09-11 17:30:23 +03:00
2016-11-02 09:43:57 +02:00
array_map ( function ( $item ) use ( & $data , & $count , & $balance , $groupBy ) {
2016-09-11 17:30:23 +03:00
$data [ $item -> $groupBy ] = $item -> total ;
$count += $item -> count ;
2016-11-02 09:43:57 +02:00
$balance += isset ( $item -> balance ) ? $item -> balance : 0 ;
}, $records );
2016-11-16 12:41:32 +02:00
2016-09-11 17:30:23 +03:00
$padding = $groupBy == 'DAYOFYEAR' ? 'day' : ( $groupBy == 'WEEK' ? 'week' : 'month' );
$endDate -> modify ( '+1 ' . $padding );
$interval = new DateInterval ( 'P1' . substr ( $groupBy , 0 , 1 ));
$period = new DatePeriod ( $startDate , $interval , $endDate );
$endDate -> modify ( '-1 ' . $padding );
$records = [];
foreach ( $period as $d ) {
$dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ( $groupBy == 'WEEK' ? 'W' : 'n' );
2017-02-18 23:19:53 +02:00
if ( $groupBy == 'DAYOFYEAR' ) {
// MySQL returns 1-366 for DAYOFYEAR, whereas PHP returns 0-365
$date = $d -> format ( 'Y' ) . ( $d -> format ( $dateFormat ) + 1 );
} elseif ( $groupBy == 'WEEK' && ( $d -> format ( $dateFormat ) < 10 )) {
// PHP zero pads the week
$date = $d -> format ( 'Y' ) . round ( $d -> format ( $dateFormat ));
} else {
$date = $d -> format ( 'Y' . $dateFormat );
}
2016-09-11 17:30:23 +03:00
$records [] = isset ( $data [ $date ]) ? $data [ $date ] : 0 ;
if ( $entityType == ENTITY_INVOICE ) {
2017-04-02 11:08:12 +03:00
$labels [] = $d -> format ( 'm/d/Y' );
2016-09-11 17:30:23 +03:00
}
}
if ( $entityType == ENTITY_INVOICE ) {
$color = '51,122,183' ;
} elseif ( $entityType == ENTITY_PAYMENT ) {
$color = '54,193,87' ;
} elseif ( $entityType == ENTITY_EXPENSE ) {
$color = '128,128,128' ;
}
2017-01-30 21:40:43 +02:00
$record = new stdClass ();
2016-09-11 17:30:23 +03:00
$record -> data = $records ;
$record -> label = trans ( " texts. { $entityType } s " );
$record -> lineTension = 0 ;
$record -> borderWidth = 4 ;
$record -> borderColor = " rgba( { $color } , 1) " ;
2017-12-24 23:54:19 +02:00
$record -> backgroundColor = " rgba( { $color } , 0.1) " ;
2016-09-11 17:30:23 +03:00
$datasets [] = $record ;
if ( $entityType == ENTITY_INVOICE ) {
$totals -> invoices = array_sum ( $data );
$totals -> average = $count ? round ( $totals -> invoices / $count , 2 ) : 0 ;
2016-11-02 09:43:57 +02:00
$totals -> balance = $balance ;
2016-09-11 17:30:23 +03:00
} elseif ( $entityType == ENTITY_PAYMENT ) {
$totals -> revenue = array_sum ( $data );
} elseif ( $entityType == ENTITY_EXPENSE ) {
//$totals->profit = $totals->revenue - array_sum($data);
$totals -> expenses = array_sum ( $data );
}
}
2017-01-30 21:40:43 +02:00
$data = new stdClass ();
2016-09-11 17:30:23 +03:00
$data -> labels = $labels ;
$data -> datasets = $datasets ;
2017-01-30 21:40:43 +02:00
$response = new stdClass ();
2016-09-11 17:30:23 +03:00
$response -> data = $data ;
$response -> totals = $totals ;
return $response ;
}
private function rawChartData ( $entityType , $account , $groupBy , $startDate , $endDate , $currencyId )
{
2017-01-30 18:05:31 +02:00
if ( ! in_array ( $groupBy , [ 'DAYOFYEAR' , 'WEEK' , 'MONTH' ])) {
2016-11-02 09:43:57 +02:00
return [];
}
2019-01-30 22:00:26 +11:00
list ( $timeframe , $records ) = $this -> rawChartDataPrepare ( $entityType , $account , $groupBy , $startDate , $endDate );
2016-09-11 17:30:23 +03:00
if ( $entityType == ENTITY_EXPENSE ) {
$records -> where ( 'expenses.expense_currency_id' , '=' , $currencyId );
} elseif ( $currencyId == $account -> getCurrencyId ()) {
$records -> whereRaw ( " (clients.currency_id = { $currencyId } or coalesce(clients.currency_id, 0) = 0) " );
} else {
$records -> where ( 'clients.currency_id' , '=' , $currencyId );
}
if ( $entityType == ENTITY_INVOICE ) {
2016-11-02 09:43:57 +02:00
$records -> select ( DB :: raw ( 'sum(invoices.amount) as total, sum(invoices.balance) as balance, count(invoices.id) as count, ' . $timeframe . ' as ' . $groupBy ))
2016-09-11 17:30:23 +03:00
-> where ( 'invoice_type_id' , '=' , INVOICE_TYPE_STANDARD )
2017-02-05 14:45:14 +02:00
-> where ( 'invoices.is_public' , '=' , true )
2016-09-11 17:30:23 +03:00
-> where ( 'is_recurring' , '=' , false );
} elseif ( $entityType == ENTITY_PAYMENT ) {
$records -> select ( DB :: raw ( 'sum(payments.amount - payments.refunded) as total, count(payments.id) as count, ' . $timeframe . ' as ' . $groupBy ))
-> join ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> where ( 'invoices.is_deleted' , '=' , false )
-> whereNotIn ( 'payment_status_id' , [ PAYMENT_STATUS_VOIDED , PAYMENT_STATUS_FAILED ]);
} elseif ( $entityType == ENTITY_EXPENSE ) {
2016-10-27 17:26:42 +03:00
$records -> select ( DB :: raw ( 'sum(expenses.amount + (expenses.amount * expenses.tax_rate1 / 100) + (expenses.amount * expenses.tax_rate2 / 100)) as total, count(expenses.id) as count, ' . $timeframe . ' as ' . $groupBy ));
2016-09-11 17:30:23 +03:00
}
2017-11-14 10:58:08 +02:00
return $records -> get () -> all ();
2016-09-11 17:30:23 +03:00
}
2019-01-30 22:00:26 +11:00
private function rawChartDataTotals ( $entityType , $account , $groupBy , $startDate , $endDate , $currencyId )
{
if ( ! in_array ( $groupBy , [ 'DAYOFYEAR' , 'WEEK' , 'MONTH' ])) {
return [];
}
list ( $timeframe , $records ) = $this -> rawChartDataPrepare ( $entityType , $account , $groupBy , $startDate , $endDate );
if ( $entityType == ENTITY_INVOICE ) {
// as default invoice exchange rate column we use just 1 value
$invoiceExchangeRateColumn = 1 ;
if ( $exchageRateCustomFieldIndex = $account -> getInvoiceExchangeRateCustomFieldIndex ()) {
$invoiceExchangeRateColumn = 'invoices.custom_text_value' . $exchageRateCustomFieldIndex ;
}
$records -> select ( DB :: raw ( 'sum(if(clients.currency_id = ' . $currencyId . ' OR clients.currency_id is null, invoices.amount, invoices.amount / ' . $invoiceExchangeRateColumn . ')) as total, sum(if(clients.currency_id = ' . $currencyId . ' OR clients.currency_id is null, invoices.balance, invoices.balance / ' . $invoiceExchangeRateColumn . ')) as balance, count(invoices.id) as count, ' . $timeframe . ' as ' . $groupBy ))
-> where ( 'invoice_type_id' , '=' , INVOICE_TYPE_STANDARD )
-> where ( 'invoices.is_public' , '=' , true )
-> where ( 'is_recurring' , '=' , false );
} elseif ( $entityType == ENTITY_PAYMENT ) {
$records -> select ( DB :: raw ( 'sum(if(clients.currency_id = ' . $currencyId . ' OR clients.currency_id is null, payments.amount - payments.refunded, (payments.amount - payments.refunded) * payments.exchange_rate)) as total, count(payments.id) as count, ' . $timeframe . ' as ' . $groupBy ))
-> join ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> where ( 'invoices.is_deleted' , '=' , false )
-> whereNotIn ( 'payment_status_id' , [ PAYMENT_STATUS_VOIDED , PAYMENT_STATUS_FAILED ]);
} elseif ( $entityType == ENTITY_EXPENSE ) {
$records -> select ( DB :: raw ( 'if(expenses.expense_currency_id = ' . $currencyId . ' OR clients.currency_id is null, sum(expenses.amount + (expenses.amount * expenses.tax_rate1 / 100) + (expenses.amount * expenses.tax_rate2 / 100)), sum(expenses.amount * expenses.exchange_rate + (expenses.amount * expenses.exchange_rate * expenses.tax_rate1 / 100) + (expenses.amount * expenses.exchange_rate * expenses.tax_rate2 / 100))) as total, count(expenses.id) as count, ' . $timeframe . ' as ' . $groupBy ));
}
return $records -> get () -> all ();
}
/**
* @ param $entityType
* @ param $account
* @ param $groupBy
* @ param $startDate
* @ param $endDate
* @ return array $timeframe , $records
*/
private function rawChartDataPrepare ( $entityType , $account , $groupBy , $startDate , $endDate )
{
$accountId = $account -> id ;
$timeframe = 'concat(YEAR(' . $entityType . '_date), ' . $groupBy . '(' . $entityType . '_date))' ;
$records = DB :: table ( $entityType . 's' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , $entityType . 's.client_id' )
-> whereRaw ( '(clients.id IS NULL OR clients.is_deleted = 0)' )
-> where ( $entityType . 's.account_id' , '=' , $accountId )
-> where ( $entityType . 's.is_deleted' , '=' , false )
-> where ( $entityType . 's.' . $entityType . '_date' , '>=' , $startDate -> format ( 'Y-m-d' ))
-> where ( $entityType . 's.' . $entityType . '_date' , '<=' , $endDate -> format ( 'Y-m-d' ))
-> groupBy ( $groupBy );
return [ $timeframe , $records ];
}
2016-08-15 06:46:47 +03:00
public function totals ( $accountId , $userId , $viewAll )
{
// total_income, billed_clients, invoice_sent and active_clients
$select = DB :: raw (
'COUNT(DISTINCT CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'invoices.id' , true ) . ' IS NOT NULL THEN ' . DB :: getQueryGrammar () -> wrap ( 'clients.id' , true ) . ' ELSE null END ) billed_clients ,
SUM ( CASE WHEN '.DB::getQueryGrammar()->wrap(' invoices . invoice_status_id ', true).' >= '.INVOICE_STATUS_SENT.' THEN 1 ELSE 0 END ) invoices_sent ,
COUNT ( DISTINCT '.DB::getQueryGrammar()->wrap(' clients . id ', true).' ) active_clients '
);
$metrics = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> leftJoin ( 'invoices' , 'clients.id' , '=' , 'invoices.client_id' )
-> where ( 'accounts.id' , '=' , $accountId )
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'invoices.is_recurring' , '=' , false )
2017-02-05 14:45:14 +02:00
-> where ( 'invoices.is_public' , '=' , true )
2016-08-15 06:46:47 +03:00
-> where ( 'invoices.invoice_type_id' , '=' , INVOICE_TYPE_STANDARD );
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2017-01-30 18:05:31 +02:00
$metrics = $metrics -> where ( function ( $query ) use ( $userId ) {
2016-08-15 06:46:47 +03:00
$query -> where ( 'invoices.user_id' , '=' , $userId );
2017-01-30 18:05:31 +02:00
$query -> orwhere ( function ( $query ) use ( $userId ) {
2016-08-15 06:46:47 +03:00
$query -> where ( 'invoices.user_id' , '=' , null );
$query -> where ( 'clients.user_id' , '=' , $userId );
});
});
}
return $metrics -> groupBy ( 'accounts.id' ) -> first ();
}
2016-11-16 12:41:32 +02:00
public function paidToDate ( $account , $userId , $viewAll , $startDate = false )
2016-08-15 06:46:47 +03:00
{
$select = DB :: raw (
2016-10-31 11:11:30 +02:00
'SUM(' . DB :: getQueryGrammar () -> wrap ( 'payments.amount' , true ) . ' - ' . DB :: getQueryGrammar () -> wrap ( 'payments.refunded' , true ) . ') as value,'
2019-01-30 22:00:26 +11:00
. 'IFNULL(' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ', ' . $account -> currency -> id . ') as currency_id,'
. DB :: getQueryGrammar () -> wrap ( 'payments.exchange_rate' , true ) . ' as exchange_rate'
2016-08-15 06:46:47 +03:00
);
2016-10-31 11:11:30 +02:00
$paidToDate = DB :: table ( 'payments' )
2016-08-15 06:46:47 +03:00
-> select ( $select )
2016-10-31 11:11:30 +02:00
-> leftJoin ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'invoices.client_id' )
2019-01-30 22:00:26 +11:00
-> where ( 'payments.account_id' , '=' , $account -> id )
2016-10-31 11:11:30 +02:00
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
2016-11-16 12:41:32 +02:00
-> where ( 'payments.is_deleted' , '=' , false )
2016-10-31 11:11:30 +02:00
-> whereNotIn ( 'payments.payment_status_id' , [ PAYMENT_STATUS_VOIDED , PAYMENT_STATUS_FAILED ]);
2016-08-15 06:46:47 +03:00
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-10-31 11:11:30 +02:00
$paidToDate -> where ( 'invoices.user_id' , '=' , $userId );
2016-08-15 06:46:47 +03:00
}
2016-11-16 12:41:32 +02:00
if ( $startDate ) {
$paidToDate -> where ( 'payments.payment_date' , '>=' , $startDate );
2017-02-07 14:04:13 +02:00
} elseif ( $startDate = $account -> financialYearStart ()) {
2017-02-26 20:33:35 +02:00
//$paidToDate->where('payments.payment_date', '>=', $startDate);
2016-10-31 11:11:30 +02:00
}
return $paidToDate -> groupBy ( 'payments.account_id' )
-> groupBy ( DB :: raw ( 'CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' IS NULL THEN ' . ( $account -> currency_id ? : DEFAULT_CURRENCY ) . ' ELSE ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' END' ))
2016-08-15 06:46:47 +03:00
-> get ();
}
2016-10-31 11:11:30 +02:00
public function averages ( $account , $userId , $viewAll )
2016-08-15 06:46:47 +03:00
{
2019-01-30 22:00:26 +11:00
// as default invoice exchange rate column we use just 1 value
$invoiceExchangeRateColumn = 1 ;
if ( $exchageRateCustomFieldIndex = $account -> getInvoiceExchangeRateCustomFieldIndex ()) {
$invoiceExchangeRateColumn = DB :: getQueryGrammar () -> wrap ( 'invoices.custom_text_value' . $exchageRateCustomFieldIndex , true );
}
2016-08-15 06:46:47 +03:00
$select = DB :: raw (
'AVG(' . DB :: getQueryGrammar () -> wrap ( 'invoices.amount' , true ) . ') as invoice_avg, '
2019-01-30 22:00:26 +11:00
. 'IFNULL(' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ', ' . $account -> currency -> id . ') as currency_id,'
. $invoiceExchangeRateColumn . ' as exchange_rate,'
. 'COUNT(*) as invoice_count'
2016-08-15 06:46:47 +03:00
);
$averageInvoice = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> leftJoin ( 'invoices' , 'clients.id' , '=' , 'invoices.client_id' )
2019-01-30 22:00:26 +11:00
-> where ( 'accounts.id' , '=' , $account -> id )
2016-08-15 06:46:47 +03:00
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
2017-02-05 14:45:14 +02:00
-> where ( 'invoices.is_public' , '=' , true )
2016-08-15 06:46:47 +03:00
-> where ( 'invoices.invoice_type_id' , '=' , INVOICE_TYPE_STANDARD )
-> where ( 'invoices.is_recurring' , '=' , false );
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-10-31 11:11:30 +02:00
$averageInvoice -> where ( 'invoices.user_id' , '=' , $userId );
}
2017-02-07 14:04:13 +02:00
if ( $startDate = $account -> financialYearStart ()) {
2017-02-26 20:33:35 +02:00
//$averageInvoice->where('invoices.invoice_date', '>=', $startDate);
2016-08-15 06:46:47 +03:00
}
return $averageInvoice -> groupBy ( 'accounts.id' )
-> groupBy ( DB :: raw ( 'CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' IS NULL THEN CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'accounts.currency_id' , true ) . ' IS NULL THEN 1 ELSE ' . DB :: getQueryGrammar () -> wrap ( 'accounts.currency_id' , true ) . ' END ELSE ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' END' ))
-> get ();
}
2019-01-30 22:00:26 +11:00
public function balances ( $account , $userId , $viewAll )
2016-08-15 06:46:47 +03:00
{
$select = DB :: raw (
'SUM(' . DB :: getQueryGrammar () -> wrap ( 'clients.balance' , true ) . ') as value, '
2019-01-30 22:00:26 +11:00
. 'IFNULL(' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ', ' . $account -> currency -> id . ') as currency_id'
2016-08-15 06:46:47 +03:00
);
$balances = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
2019-01-30 22:00:26 +11:00
-> where ( 'accounts.id' , '=' , $account -> id )
2016-08-15 06:46:47 +03:00
-> where ( 'clients.is_deleted' , '=' , false )
-> groupBy ( 'accounts.id' )
-> groupBy ( DB :: raw ( 'CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' IS NULL THEN CASE WHEN ' . DB :: getQueryGrammar () -> wrap ( 'accounts.currency_id' , true ) . ' IS NULL THEN 1 ELSE ' . DB :: getQueryGrammar () -> wrap ( 'accounts.currency_id' , true ) . ' END ELSE ' . DB :: getQueryGrammar () -> wrap ( 'clients.currency_id' , true ) . ' END' ));
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-08-15 06:46:47 +03:00
$balances -> where ( 'clients.user_id' , '=' , $userId );
}
return $balances -> get ();
}
public function activities ( $accountId , $userId , $viewAll )
{
$activities = Activity :: where ( 'activities.account_id' , '=' , $accountId )
-> where ( 'activities.activity_type_id' , '>' , 0 );
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-08-15 06:46:47 +03:00
$activities = $activities -> where ( 'activities.user_id' , '=' , $userId );
}
2019-05-17 08:25:40 -04:00
return $activities -> orderBy ( 'activities.created_at' , 'desc' ) -> orderBy ( 'activities.id' , 'desc' )
2016-10-30 08:50:58 +02:00
-> with ( 'client.contacts' , 'user' , 'invoice' , 'payment' , 'credit' , 'account' , 'task' , 'expense' , 'contact' )
2016-08-15 06:46:47 +03:00
-> take ( 50 )
-> get ();
}
public function pastDue ( $accountId , $userId , $viewAll )
{
$pastDue = DB :: table ( 'invoices' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'invoices.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'invoices.account_id' , '=' , $accountId )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'contacts.deleted_at' , '=' , null )
-> where ( 'invoices.is_recurring' , '=' , false )
-> where ( 'invoices.quote_invoice_id' , '=' , null )
-> where ( 'invoices.balance' , '>' , 0 )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'invoices.deleted_at' , '=' , null )
2017-02-05 14:45:14 +02:00
-> where ( 'invoices.is_public' , '=' , true )
2016-08-15 06:46:47 +03:00
-> where ( 'contacts.is_primary' , '=' , true )
2017-10-26 10:56:59 +03:00
-> where ( DB :: raw ( " coalesce(invoices.partial_due_date, invoices.due_date) " ), '<' , date ( 'Y-m-d' ));
2016-08-15 06:46:47 +03:00
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-08-15 06:46:47 +03:00
$pastDue = $pastDue -> where ( 'invoices.user_id' , '=' , $userId );
}
2017-10-26 10:56:59 +03:00
return $pastDue -> select ([ DB :: raw ( " coalesce(invoices.partial_due_date, invoices.due_date) due_date " ), 'invoices.balance' , 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' , 'invoice_type_id' ])
2016-08-15 06:46:47 +03:00
-> orderBy ( 'invoices.due_date' , 'asc' )
-> take ( 50 )
-> get ();
}
public function upcoming ( $accountId , $userId , $viewAll )
{
$upcoming = DB :: table ( 'invoices' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'invoices.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'invoices.account_id' , '=' , $accountId )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'contacts.deleted_at' , '=' , null )
-> where ( 'invoices.deleted_at' , '=' , null )
-> where ( 'invoices.is_recurring' , '=' , false )
-> where ( 'invoices.quote_invoice_id' , '=' , null )
-> where ( 'invoices.balance' , '>' , 0 )
-> where ( 'invoices.is_deleted' , '=' , false )
2017-02-05 14:45:14 +02:00
-> where ( 'invoices.is_public' , '=' , true )
2016-08-15 06:46:47 +03:00
-> where ( 'contacts.is_primary' , '=' , true )
2017-02-27 11:13:32 +02:00
-> where ( function ( $query ) {
2017-12-23 21:25:58 +02:00
$query -> where ( DB :: raw ( " coalesce(invoices.partial_due_date, invoices.due_date) " ), '>=' , date ( 'Y-m-d' ))
-> orWhereNull ( 'invoices.due_date' );
2017-02-27 11:13:32 +02:00
})
2016-08-15 06:46:47 +03:00
-> orderBy ( 'invoices.due_date' , 'asc' );
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-08-15 06:46:47 +03:00
$upcoming = $upcoming -> where ( 'invoices.user_id' , '=' , $userId );
}
return $upcoming -> take ( 50 )
2017-10-26 10:56:59 +03:00
-> select ([ DB :: raw ( " coalesce(invoices.partial_due_date, invoices.due_date) due_date " ), 'invoices.balance' , 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' , 'invoice_type_id' ])
2016-08-15 06:46:47 +03:00
-> get ();
}
public function payments ( $accountId , $userId , $viewAll )
{
$payments = DB :: table ( 'payments' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'payments.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> leftJoin ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> where ( 'payments.account_id' , '=' , $accountId )
-> where ( 'payments.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'contacts.deleted_at' , '=' , null )
2016-09-11 20:05:59 +03:00
-> where ( 'contacts.is_primary' , '=' , true )
-> whereNotIn ( 'payments.payment_status_id' , [ PAYMENT_STATUS_VOIDED , PAYMENT_STATUS_FAILED ]);
2016-08-15 06:46:47 +03:00
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2016-08-15 06:46:47 +03:00
$payments = $payments -> where ( 'payments.user_id' , '=' , $userId );
}
2016-09-11 20:05:59 +03:00
return $payments -> select ([ 'payments.payment_date' , DB :: raw ( '(payments.amount - payments.refunded) as amount' ), 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' ])
2016-08-15 06:46:47 +03:00
-> orderBy ( 'payments.payment_date' , 'desc' )
-> take ( 50 )
-> get ();
}
2016-09-11 17:30:23 +03:00
2017-02-07 14:04:13 +02:00
public function expenses ( $account , $userId , $viewAll )
2016-09-11 17:30:23 +03:00
{
2016-10-27 17:26:42 +03:00
$amountField = DB :: getQueryGrammar () -> wrap ( 'expenses.amount' , true );
$taxRate1Field = DB :: getQueryGrammar () -> wrap ( 'expenses.tax_rate1' , true );
$taxRate2Field = DB :: getQueryGrammar () -> wrap ( 'expenses.tax_rate2' , true );
2016-09-11 17:30:23 +03:00
$select = DB :: raw (
2016-10-27 17:26:42 +03:00
" SUM( { $amountField } + ( { $amountField } * { $taxRate1Field } / 100) + ( { $amountField } * { $taxRate2Field } / 100)) as value, "
2019-01-30 22:00:26 +11:00
. DB :: getQueryGrammar () -> wrap ( 'expenses.expense_currency_id' , true ) . ' as currency_id,'
. DB :: getQueryGrammar () -> wrap ( 'expenses.exchange_rate' , true ) . ' as exchange_rate'
2016-09-11 17:30:23 +03:00
);
2017-02-07 14:04:13 +02:00
$expenses = DB :: table ( 'accounts' )
2016-09-11 17:30:23 +03:00
-> select ( $select )
-> leftJoin ( 'expenses' , 'accounts.id' , '=' , 'expenses.account_id' )
2017-02-07 14:04:13 +02:00
-> where ( 'accounts.id' , '=' , $account -> id )
2016-09-11 17:30:23 +03:00
-> where ( 'expenses.is_deleted' , '=' , false );
2017-01-30 21:40:43 +02:00
if ( ! $viewAll ) {
2017-02-07 14:04:13 +02:00
$expenses = $expenses -> where ( 'expenses.user_id' , '=' , $userId );
}
if ( $startDate = $account -> financialYearStart ()) {
2017-02-26 20:33:35 +02:00
//$expenses->where('expenses.expense_date', '>=', $startDate);
2016-09-11 17:30:23 +03:00
}
2017-02-07 14:04:13 +02:00
return $expenses -> groupBy ( 'accounts.id' )
2016-09-11 17:30:23 +03:00
-> groupBy ( 'expenses.expense_currency_id' )
-> get ();
}
public function tasks ( $accountId , $userId , $viewAll )
{
return Task :: scope ()
-> withArchived ()
-> whereIsRunning ( true )
-> get ();
}
2016-08-15 06:46:47 +03:00
}