From eca4d7ce6a0063bae40c5cc13baf581884f18044 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 31 Oct 2016 11:11:30 +0200 Subject: [PATCH] Add financial year start setting for dashboard --- app/Http/Controllers/AccountController.php | 8 ++-- .../Controllers/DashboardApiController.php | 4 +- app/Http/Controllers/DashboardController.php | 6 +-- .../Controllers/OnlinePaymentController.php | 4 +- app/Libraries/Utils.php | 22 +++++++++-- app/Models/Account.php | 1 + .../Repositories/DashboardRepository.php | 37 +++++++++++++------ resources/lang/en/texts.php | 3 +- .../views/accounts/localization.blade.php | 11 +++++- 9 files changed, 68 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index eaa3de6529..dc08c1af9d 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -429,6 +429,7 @@ class AccountController extends BaseController 'currencies' => Cache::get('currencies'), 'title' => trans('texts.localization'), 'weekdays' => Utils::getTranslatedWeekdayNames(), + 'months' => Utils::getMonthOptions(), ]; return View::make('accounts.localization', $data); @@ -674,11 +675,9 @@ class AccountController extends BaseController * @param $section * @return \Illuminate\Http\RedirectResponse */ - public function doSection($section = ACCOUNT_COMPANY_DETAILS) + public function doSection($section) { - if ($section === ACCOUNT_COMPANY_DETAILS) { - return AccountController::saveDetails(); - } elseif ($section === ACCOUNT_LOCALIZATION) { + if ($section === ACCOUNT_LOCALIZATION) { return AccountController::saveLocalization(); } elseif ($section == ACCOUNT_PAYMENTS) { return self::saveOnlinePayments(); @@ -1225,6 +1224,7 @@ class AccountController extends BaseController $account->military_time = Input::get('military_time') ? true : false; $account->show_currency_code = Input::get('show_currency_code') ? true : false; $account->start_of_week = Input::get('start_of_week') ? Input::get('start_of_week') : 0; + $account->financial_year_start = Input::get('financial_year_start') ? Input::get('financial_year_start') : null; $account->save(); event(new UserSettingsChanged()); diff --git a/app/Http/Controllers/DashboardApiController.php b/app/Http/Controllers/DashboardApiController.php index d5ff416e7e..c6cf454666 100644 --- a/app/Http/Controllers/DashboardApiController.php +++ b/app/Http/Controllers/DashboardApiController.php @@ -23,8 +23,8 @@ class DashboardApiController extends BaseAPIController $dashboardRepo = $this->dashboardRepo; $metrics = $dashboardRepo->totals($accountId, $userId, $viewAll); - $paidToDate = $dashboardRepo->paidToDate($accountId, $userId, $viewAll); - $averageInvoice = $dashboardRepo->averages($accountId, $userId, $viewAll); + $paidToDate = $dashboardRepo->paidToDate($account, $userId, $viewAll); + $averageInvoice = $dashboardRepo->averages($account, $userId, $viewAll); $balances = $dashboardRepo->balances($accountId, $userId, $viewAll); $activities = $dashboardRepo->activities($accountId, $userId, $viewAll); $pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll); diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 50c5d3d394..14cb4c638b 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -33,9 +33,9 @@ class DashboardController extends BaseController $dashboardRepo = $this->dashboardRepo; $metrics = $dashboardRepo->totals($accountId, $userId, $viewAll); - $paidToDate = $dashboardRepo->paidToDate($accountId, $userId, $viewAll); - $averageInvoice = $dashboardRepo->averages($accountId, $userId, $viewAll); - $balances = $dashboardRepo->balances($accountId, $userId, $viewAll); + $paidToDate = $dashboardRepo->paidToDate($account, $userId, $viewAll); + $averageInvoice = $dashboardRepo->averages($account, $userId, $viewAll); + $balances = $dashboardRepo->balances($accountId, $userId, $viewAll); $activities = $dashboardRepo->activities($accountId, $userId, $viewAll); $pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll); $upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll); diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index c49a4d6669..e7d709096e 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -75,6 +75,8 @@ class OnlinePaymentController extends BaseController } $invitation = $invitation->load('invoice.client.account.account_gateways.gateway'); + $account = $invitation->account; + $account->loadLocalizationSettings($invitation->invoice->client); if ( ! $gatewayTypeAlias) { $gatewayTypeId = Session::get($invitation->id . 'gateway_type'); @@ -84,7 +86,7 @@ class OnlinePaymentController extends BaseController $gatewayTypeId = $gatewayTypeAlias; } - $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId); + $paymentDriver = $account->paymentDriver($invitation, $gatewayTypeId); try { return $paymentDriver->startPurchase(Input::all(), $sourceId); diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index 0b3984acc2..0d001697d6 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -22,6 +22,9 @@ class Utils "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", ]; + public static $months = [ + 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', + ]; public static function isRegistered() { @@ -97,7 +100,7 @@ class Utils if (Utils::isNinjaProd()) { return false; } - + return \App\Models\Account::first()->hasFeature(FEATURE_WHITE_LABEL); } @@ -649,12 +652,23 @@ class Utils return Utils::getYear($offset); } } + + public static function getMonthOptions() + { + $months = []; + + for ($i=1; $i<=count(static::$months); $i++) { + $month = static::$months[$i-1]; + $number = $i < 10 ? '0' . $i : $i; + $months["2000-{$number}-01"] = trans("texts.{$month}"); + } + + return $months; + } private static function getMonth($offset) { - $months = ['january', 'february', 'march', 'april', 'may', 'june', - 'july', 'august', 'september', 'october', 'november', 'december', ]; - + $months = static::$months; $month = intval(date('n')) - 1; $month += $offset; diff --git a/app/Models/Account.php b/app/Models/Account.php index 4db6511136..b1b9a1ad74 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -69,6 +69,7 @@ class Account extends Eloquent 'enable_second_tax_rate', 'include_item_taxes_inline', 'start_of_week', + 'financial_year_start', ]; /** diff --git a/app/Ninja/Repositories/DashboardRepository.php b/app/Ninja/Repositories/DashboardRepository.php index 455497d130..f7cc88b6ca 100644 --- a/app/Ninja/Repositories/DashboardRepository.php +++ b/app/Ninja/Repositories/DashboardRepository.php @@ -175,29 +175,39 @@ class DashboardRepository return $metrics->groupBy('accounts.id')->first(); } - public function paidToDate($accountId, $userId, $viewAll) + public function paidToDate($account, $userId, $viewAll) { + $accountId = $account->id; $select = DB::raw( - 'SUM('.DB::getQueryGrammar()->wrap('clients.paid_to_date', true).') as value,' + 'SUM('.DB::getQueryGrammar()->wrap('payments.amount', true).' - '.DB::getQueryGrammar()->wrap('payments.refunded', true).') as value,' .DB::getQueryGrammar()->wrap('clients.currency_id', true).' as currency_id' ); - $paidToDate = DB::table('accounts') + $paidToDate = DB::table('payments') ->select($select) - ->leftJoin('clients', 'accounts.id', '=', 'clients.account_id') - ->where('accounts.id', '=', $accountId) - ->where('clients.is_deleted', '=', false); + ->leftJoin('invoices', 'invoices.id', '=', 'payments.invoice_id') + ->leftJoin('clients', 'clients.id', '=', 'invoices.client_id') + ->where('payments.account_id', '=', $accountId) + ->where('clients.is_deleted', '=', false) + ->where('invoices.is_deleted', '=', false) + ->whereNotIn('payments.payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]); if (!$viewAll){ - $paidToDate = $paidToDate->where('clients.user_id', '=', $userId); + $paidToDate->where('invoices.user_id', '=', $userId); } - return $paidToDate->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')) + if ($account->financial_year_start) { + $yearStart = str_replace('2000', date('Y'), $account->financial_year_start); + $paidToDate->where('payments.payment_date', '>=', $yearStart); + } + + 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')) ->get(); } - public function averages($accountId, $userId, $viewAll) + public function averages($account, $userId, $viewAll) { + $accountId = $account->id; $select = DB::raw( 'AVG('.DB::getQueryGrammar()->wrap('invoices.amount', true).') as invoice_avg, ' .DB::getQueryGrammar()->wrap('clients.currency_id', true).' as currency_id' @@ -213,7 +223,12 @@ class DashboardRepository ->where('invoices.is_recurring', '=', false); if (!$viewAll){ - $averageInvoice = $averageInvoice->where('invoices.user_id', '=', $userId); + $averageInvoice->where('invoices.user_id', '=', $userId); + } + + if ($account->financial_year_start) { + $yearStart = str_replace('2000', date('Y'), $account->financial_year_start); + $averageInvoice->where('invoices.invoice_date', '>=', $yearStart); } return $averageInvoice->groupBy('accounts.id') diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 8e80b75c12..587733f4b9 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1369,7 +1369,7 @@ $LANG = array( 'failed_remove_payment_method' => 'Failed to remove the payment method', 'gateway_exists' => 'This gateway already exists', 'manual_entry' => 'Manual entry', - 'start_of_week' => 'First day of the week', + 'start_of_week' => 'First Day of the Week', // Frequencies 'freq_weekly' => 'Weekly', @@ -2174,6 +2174,7 @@ $LANG = array( 'invalid_white_label_license' => 'The white label license is not valid', 'created_by' => 'Created by :name', 'modules' => 'Modules', + 'financial_year_start' => 'First Month of the Year', ); diff --git a/resources/views/accounts/localization.blade.php b/resources/views/accounts/localization.blade.php index f124410f0d..1d196bdec8 100644 --- a/resources/views/accounts/localization.blade.php +++ b/resources/views/accounts/localization.blade.php @@ -42,7 +42,7 @@ {!! Former::select('language_id')->addOption('','') ->fromQuery($languages, 'name', 'id') ->help(trans('texts.translate_app', ['link' => link_to(TRANSIFEX_URL, 'Transifex.com', ['target' => '_blank'])])) !!} -
+
 
{!! Former::select('timezone_id')->addOption('','') ->fromQuery($timezones, 'location', 'id') !!} @@ -50,9 +50,16 @@ ->fromQuery($dateFormats) !!} {!! Former::select('datetime_format_id')->addOption('','') ->fromQuery($datetimeFormats) !!} + {!! Former::checkbox('military_time')->text(trans('texts.enable')) !!} + +
 
+ {!! Former::select('start_of_week')->addOption('','') ->fromQuery($weekdays) !!} - {!! Former::checkbox('military_time')->text(trans('texts.enable')) !!} + {!! Former::select('financial_year_start') + ->addOption('','') + ->options($months) !!} +