diff --git a/app/Account.php b/app/Account.php index d793038b149..d131998dff1 100644 --- a/app/Account.php +++ b/app/Account.php @@ -583,4 +583,74 @@ public function replaceGender(Gender $genderToDelete, Gender $genderToReplaceWit return true; } + + /** + * Get the statistics of the number of calls grouped by year. + * + * @return json + */ + public function getYearlyCallStatistics() + { + $callsStatistics = collect([]); + $calls = $this->calls()->latest('called_at')->get(); + $years = []; + + // Create a table that contains the combo year/number of + foreach ($calls as $call) { + $yearStatistic = $call->called_at->format('Y'); + $foundInYear = false; + + foreach ($years as $year => $number) { + if ($year == $yearStatistic) { + $years[$year] = $number + 1; + $foundInYear = true; + } + } + + if (! $foundInYear) { + $years[$yearStatistic] = 1; + } + } + + foreach ($years as $year => $number) { + $callsStatistics->put($year, $number); + } + + return $callsStatistics; + } + + /** + * Get the statistics of the number of activities grouped by year. + * + * @return json + */ + public function getYearlyActivitiesStatistics() + { + $activitiesStatistics = collect([]); + $activities = $this->activities()->latest('date_it_happened')->get(); + $years = []; + + // Create a table that contains the combo year/number of + foreach ($activities as $call) { + $yearStatistic = $call->date_it_happened->format('Y'); + $foundInYear = false; + + foreach ($years as $year => $number) { + if ($year == $yearStatistic) { + $years[$year] = $number + 1; + $foundInYear = true; + } + } + + if (! $foundInYear) { + $years[$yearStatistic] = 1; + } + } + + foreach ($years as $year => $number) { + $activitiesStatistics->put($year, $number); + } + + return $activitiesStatistics; + } } diff --git a/app/Http/Controllers/Api/ApiActivityController.php b/app/Http/Controllers/Api/ApiActivityController.php index 31d349623df..ca0d85f0f48 100644 --- a/app/Http/Controllers/Api/ApiActivityController.php +++ b/app/Http/Controllers/Api/ApiActivityController.php @@ -31,7 +31,9 @@ public function index(Request $request) return $this->respondInvalidQuery(); } - return ActivityResource::collection($activities); + return ActivityResource::collection($activities)->additional(['meta' => [ + 'statistics' => auth()->user()->account->getYearlyActivitiesStatistics(), + ]]); } /** @@ -250,7 +252,9 @@ public function activities(Request $request, $contactId) return $this->respondInvalidQuery(); } - return ActivityResource::collection($activities); + return ActivityResource::collection($activities)->additional(['meta' => [ + 'statistics' => auth()->user()->account->getYearlyActivitiesStatistics(), + ]]); } /** diff --git a/app/Http/Controllers/Api/ApiCallController.php b/app/Http/Controllers/Api/ApiCallController.php index 90dc04712cc..378b6d1bc9a 100644 --- a/app/Http/Controllers/Api/ApiCallController.php +++ b/app/Http/Controllers/Api/ApiCallController.php @@ -27,7 +27,9 @@ public function index(Request $request) return $this->respondInvalidQuery(); } - return CallResource::collection($calls); + return CallResource::collection($calls)->additional(['meta' => [ + 'statistics' => auth()->user()->account->getYearlyCallStatistics(), + ]]); } /** @@ -169,8 +171,11 @@ public function calls(Request $request, $contactId) } $calls = $contact->calls() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); - return CallResource::collection($calls); + return CallResource::collection($calls)->additional(['meta' => [ + 'statistics' => auth()->user()->account->getYearlyCallStatistics(), + ]]); } } diff --git a/app/Http/Controllers/Api/ApiDebtController.php b/app/Http/Controllers/Api/ApiDebtController.php index c3fc4d3043d..0b0d7705333 100644 --- a/app/Http/Controllers/Api/ApiDebtController.php +++ b/app/Http/Controllers/Api/ApiDebtController.php @@ -180,6 +180,7 @@ public function debts(Request $request, $contactId) } $debts = $contact->debts() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); return DebtResource::collection($debts); diff --git a/app/Http/Controllers/Api/ApiGiftController.php b/app/Http/Controllers/Api/ApiGiftController.php index 4e6aabbeacd..39938e2f497 100644 --- a/app/Http/Controllers/Api/ApiGiftController.php +++ b/app/Http/Controllers/Api/ApiGiftController.php @@ -190,6 +190,7 @@ public function gifts(Request $request, $contactId) } $gifts = $contact->gifts() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); return GiftResource::collection($gifts); diff --git a/app/Http/Controllers/Api/ApiNoteController.php b/app/Http/Controllers/Api/ApiNoteController.php index 410785c5edd..39b5e44f4b2 100644 --- a/app/Http/Controllers/Api/ApiNoteController.php +++ b/app/Http/Controllers/Api/ApiNoteController.php @@ -180,6 +180,7 @@ public function notes(Request $request, $contactId) } $notes = $contact->notes() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); return NoteResource::collection($notes); diff --git a/app/Http/Controllers/Api/ApiReminderController.php b/app/Http/Controllers/Api/ApiReminderController.php index 1cd125c4921..e8b1541c443 100644 --- a/app/Http/Controllers/Api/ApiReminderController.php +++ b/app/Http/Controllers/Api/ApiReminderController.php @@ -182,6 +182,7 @@ public function reminders(Request $request, $contactId) } $reminders = $contact->reminders() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); return ReminderResource::collection($reminders); diff --git a/app/Http/Controllers/Api/ApiTaskController.php b/app/Http/Controllers/Api/ApiTaskController.php index 5d9c9fd4d5b..a3d26820540 100644 --- a/app/Http/Controllers/Api/ApiTaskController.php +++ b/app/Http/Controllers/Api/ApiTaskController.php @@ -171,6 +171,7 @@ public function tasks(Request $request, $contactId) } $tasks = $contact->tasks() + ->orderBy($this->sort, $this->sortDirection) ->paginate($this->getLimitPerPage()); return TaskResource::collection($tasks); diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php index 05f6c9ae425..fd55363bb3b 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -31,13 +31,12 @@ public function index(Request $request) $user->updateContactViewPreference($sort); } - $date_flag = false; - $date_sort = null; + $dateFlag = false; if (str_contains($sort, 'lastactivitydate')) { $date_sort = str_after($sort, 'lastactivitydate'); $sort = 'firstnameAZ'; - $date_flag = true; + $dateFlag = true; } $tags = null; @@ -85,7 +84,7 @@ public function index(Request $request) $contacts = $user->account->contacts()->real()->sortedBy($sort)->get(); } - if ($date_flag) { + if ($dateFlag) { foreach ($contacts as $contact) { $contact['sort_date'] = $contact->getLastActivityDate(); } @@ -241,7 +240,7 @@ public function update(Request $request, Contact $contact) ->withErrors($validator); } - if (! $contact->setName($request->input('firstname'), $request->input('lastname'))) { + if (! $contact->setName($request->input('firstname'), null, $request->input('lastname'))) { return back() ->withInput() ->withErrors('There has been a problem with saving the name.'); @@ -266,7 +265,7 @@ public function update(Request $request, Contact $contact) $specialDate = $contact->setSpecialDate('deceased_date', $request->input('deceased_date_year'), $request->input('deceased_date_month'), $request->input('deceased_date_day')); if ($request->input('addReminderDeceased') != '') { - $specialDate->setReminder('year', 1, trans('people.deceased_reminder_title', ['name' => $contact->first_name])); + $newReminder = $specialDate->setReminder('year', 1, trans('people.deceased_reminder_title', ['name' => $contact->first_name])); } } } @@ -276,6 +275,8 @@ public function update(Request $request, Contact $contact) // Handling the case of the birthday $contact->removeSpecialDate('birthdate'); switch ($request->input('birthdate')) { + case 'unknown': + break; case 'approximate': $specialDate = $contact->setSpecialDateFromAge('birthdate', $request->input('age')); break; @@ -286,7 +287,7 @@ public function update(Request $request, Contact $contact) $request->input('month'), $request->input('day') ); - $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name])); + $newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name])); break; case 'exact': $birthdate = $request->input('birthdayDate'); @@ -297,10 +298,7 @@ public function update(Request $request, Contact $contact) $birthdate->month, $birthdate->day ); - $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name])); - break; - case 'unknown': - default: + $newReminder = $specialDate->setReminder('year', 1, trans('people.people_add_birthday_reminder', ['name' => $contact->first_name])); break; } @@ -465,7 +463,7 @@ public function search(Request $request) public function vCard(Contact $contact) { if (config('app.debug')) { - \Barryvdh\Debugbar\Facade::disable(); + \Debugbar::disable(); } $vcard = VCardHelper::prepareVCard($contact); diff --git a/tests/Unit/AccountTest.php b/tests/Unit/AccountTest.php index 77e91cc7a5d..145fdd792f3 100644 --- a/tests/Unit/AccountTest.php +++ b/tests/Unit/AccountTest.php @@ -403,4 +403,52 @@ public function test_it_populates_the_account_with_the_right_default_reminder_ru ['number_of_days_before' => 30] ); } + + public function test_it_retrieves_yearly_call_statistics() + { + $account = factory(Account::class)->create([]); + $contact = factory('App\Call', 4)->create([ + 'account_id' => $account->id, + 'called_at' => '2018-03-02', + ]); + + $contact = factory('App\Call', 2)->create([ + 'account_id' => $account->id, + 'called_at' => '1992-03-02', + ]); + + $statistics = $account->getYearlyCallStatistics(); + + $this->assertTrue( + $statistics->contains(4) + ); + + $this->assertTrue( + $statistics->contains(2) + ); + } + + public function test_it_retrieves_yearly_activities_statistics() + { + $account = factory(Account::class)->create([]); + $contact = factory('App\Activity', 4)->create([ + 'account_id' => $account->id, + 'date_it_happened' => '2018-03-02', + ]); + + $contact = factory('App\Activity', 2)->create([ + 'account_id' => $account->id, + 'date_it_happened' => '1992-03-02', + ]); + + $statistics = $account->getYearlyActivitiesStatistics(); + + $this->assertTrue( + $statistics->contains(4) + ); + + $this->assertTrue( + $statistics->contains(2) + ); + } }