Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions app/Http/Controllers/Api/ApiActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]]);
}

/**
Expand Down Expand Up @@ -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(),
]]);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions app/Http/Controllers/Api/ApiCallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]]);
}

/**
Expand Down Expand Up @@ -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(),
]]);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiDebtController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiGiftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiNoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiReminderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/ApiTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 10 additions & 12 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.');
Expand All @@ -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]));
}
}
}
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}