Files
monica/app/Gift.php
T
Tom Rochette d2c9c75644 Fix tests to prepare for foreign keys (#1289)
* Fix AccountTest tests.
Update the model factories not to expect a constant value for account_id.
Add various missing relationships in the model factories.

* Fix AddressTest after the addition of foreign keys.

* Fix ContactTest after the addition of foreign keys.
Set contact/account before saving a special date using Contact::setSpecialDate or Contact::setSpecialDateFromAge.

* Fix DayTest after the addition of foreign keys.

* Fix EntryTest after the addition of foreign keys.

* Fix GiftTest after the addition of foreign keys.

* Fix JournalEntryTest after the addition of foreign keys.

* Fix NoteTest after the addition of foreign keys.

* Fix NotificationTest after the addition of foreign keys.

* Fix PetTest after the addition of foreign keys.

* Return the account id and not the account object in many model factories definition.

* Fix ReminderTest after the addition of foreign keys.

* Fix SpecialDateTest after the addition of foreign keys.

* Fix UserTest after the addition of foreign keys.

* Make instead of creating in Contact::logEvent() as we cannot create an event without giving it an account_id.

* Fix ActivityTest after the addition of foreign keys.

* Fix SendNotificationsTest after the addition of foreign keys.

* Fix SendNotificationsTest after the addition of foreign keys.
- Remove the test_it_deletes_the_notification_if_contact_does_not_exist test because on deletion of a contact, all his associated notifications will be deleted due to cascade constraints.

* Fix SendRemindersTest after the addition of foreign keys.
- Remove the test_it_deletes_the_reminder_if_contact_doesnt_exist test because on deletion of a contact, all his associated reminders will be deleted due to cascade constraints.

* Fix TagTest after the addition of foreign keys.

* Update the logic in Contact::removeSpecialDate so that a foreign key constraint is not triggered when removing the associated relation.
Set a the partner gender before saving the partner as the gender is mandatory.
Add the ConvertEmptyStringsToNull middleware.
Set the metThroughId default option as empty so it is converted to null by the ConvertEmptyStringsToNull middleware.
Set the activity_type_id as empty so it is converted to null by the ConvertEmptyStringsToNull middleware.
Mark the Reminder title has required.
2018-05-08 21:33:12 -04:00

199 lines
3.9 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property Account $account
* @property Contact $contact
* @property Contact $recipient
* @method static Builder offered()
* @method static Builder isIdea()
*/
class Gift extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'offered_at',
'received_at',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_an_idea' => 'boolean',
'has_been_offered' => 'boolean',
'has_been_received' => 'boolean',
];
/**
* Get the account record associated with the gift.
*
* @return BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class);
}
/**
* Get the contact record associated with the gift.
*
* @return BelongsTo
*/
public function contact()
{
return $this->belongsTo(Contact::class);
}
/**
* Get the contact record associated with the gift.
*
* @return HasOne
*/
public function recipient()
{
return $this->hasOne(Contact::class, 'id', 'is_for');
}
/**
* Limit results to already offered gifts.
*
* @param Builder $query
* @return Builder
*/
public function scopeOffered(Builder $query)
{
return $query->where('has_been_offered', 1);
}
/**
* Limit results to gifts at the idea stage.
*
* @param Builder $query
* @return Builder
*/
public function scopeIsIdea(Builder $query)
{
return $query->where('is_an_idea', 1);
}
/**
* Check whether the gift is meant for a particular member
* of the contact's family.
*
* @return bool
*/
public function hasParticularRecipient()
{
return $this->is_for !== null && $this->is_for !== 0;
}
/**
* Set the recipient for the gift.
*
* @param int $value
* @return string
*/
public function setRecipientAttribute($value)
{
$this->attributes['is_for'] = $value;
}
/**
* Get the name of the recipient for this gift.
*
* @param string $value
* @return string
*/
public function getRecipientNameAttribute()
{
if ($this->hasParticularRecipient()) {
return $this->recipient->first_name;
}
}
/**
* Get the gift name.
*
* @param string $value
* @return string
*/
public function getNameAttribute($value)
{
return $value;
}
/**
* Get the URL of the gift.
*
* @param string $value
* @return string
*/
public function getUrlAttribute($value)
{
return $value;
}
/**
* Get the comment of the gift.
*
* @param string $value
* @return string
*/
public function getCommentAttribute($value)
{
return $value;
}
/**
* Get the value of the gift.
*
* @param string $value
* @return string
*/
public function getValueAttribute($value)
{
return $value;
}
/**
* Toggle a gift between the idea and offered state.
* @return void
*/
public function toggle()
{
$this->has_been_received = false;
if ($this->is_an_idea == 1) {
$this->is_an_idea = false;
$this->has_been_offered = true;
$this->save();
return;
}
$this->is_an_idea = true;
$this->has_been_offered = false;
$this->save();
}
}