Skip to content

Commit

Permalink
added attribute casting
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Nov 13, 2020
1 parent 9deac6b commit 2d7c18b
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 98 deletions.
16 changes: 8 additions & 8 deletions app/Models/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ class User extends Authenticatable
protected $fillable = ['name', 'email', 'password', 'locale', 'enabled', 'landing_page'];

/**
* The attributes that should be hidden for arrays.
* The attributes that should be cast.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
protected $casts = [
'enabled' => 'boolean',
];

/**
* The attributes that should be mutated to dates.
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $dates = ['last_logged_in_at', 'created_at', 'updated_at', 'deleted_at'];
protected $hidden = ['password', 'remember_token'];

/**
* The attributes that should be cast.
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];
protected $dates = ['last_logged_in_at', 'created_at', 'updated_at', 'deleted_at'];

/**
* Sortable columns.
Expand Down
21 changes: 10 additions & 11 deletions app/Models/Banking/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Account extends Model
*/
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'opening_balance' => 'double',
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down Expand Up @@ -63,17 +73,6 @@ public function scopeNumber($query, $number)
return $query->where('number', '=', $number);
}

/**
* Convert opening balance to double.
*
* @param string $value
* @return void
*/
public function setOpeningBalanceAttribute($value)
{
$this->attributes['opening_balance'] = (double) $value;
}

/**
* Get the current balance.
*
Expand Down
21 changes: 10 additions & 11 deletions app/Models/Banking/Reconciliation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class Reconciliation extends Model
*/
protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'closing_balance' => 'double',
'reconciled' => 'boolean',
];

/**
* Sortable columns.
*
Expand All @@ -28,15 +38,4 @@ public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}

/**
* Convert closing balance to double.
*
* @param string $value
* @return void
*/
public function setClosingBalanceAttribute($value)
{
$this->attributes['closing_balance'] = (double) $value;
}
}
32 changes: 10 additions & 22 deletions app/Models/Banking/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Transaction extends Model
*/
protected $fillable = ['company_id', 'type', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'document_id', 'contact_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'amount' => 'double',
'currency_rate' => 'double',
];

/**
* Sortable columns.
*
Expand Down Expand Up @@ -225,28 +235,6 @@ public function onCloning($src, $child = null)
$this->document_id = null;
}

/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}

/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}

/**
* Convert amount to double.
*
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Common/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class Contact extends Model
*/
protected $fillable = ['company_id', 'type', 'name', 'email', 'user_id', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Common/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class Dashboard extends Model
*/
protected $fillable = ['company_id', 'name', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down
33 changes: 11 additions & 22 deletions app/Models/Common/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class Item extends Model
*/
protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'tax_id', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'sale_price' => 'double',
'purchase_price' => 'double',
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down Expand Up @@ -60,28 +71,6 @@ public function scopeName($query, $name)
return $query->where('name', '=', $name);
}

/**
* Convert sale price to double.
*
* @param string $value
* @return void
*/
public function setSalePriceAttribute($value)
{
$this->attributes['sale_price'] = (double) $value;
}

/**
* Convert purchase price to double.
*
* @param string $value
* @return void
*/
public function setPurchasePriceAttribute($value)
{
$this->attributes['purchase_price'] = (double) $value;
}

/**
* Get the item id.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Common/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Report extends Model
protected $fillable = ['company_id', 'class', 'name', 'description', 'settings'];

/**
* The attributes that should be casted to native types.
* The attributes that should be cast.
*
* @var array
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Common/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Widget extends Model
protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings'];

/**
* The attributes that should be casted to native types.
* The attributes that should be cast.
*
* @var array
*/
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class Module extends Model
*/
protected $fillable = ['company_id', 'alias', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];

/**
* Scope alias.
*
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Setting/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class Category extends Model
*/
protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down
21 changes: 10 additions & 11 deletions app/Models/Setting/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class Currency extends Model
*/
protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'rate' => 'double',
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down Expand Up @@ -72,17 +82,6 @@ public function vendors()
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
}

/**
* Convert rate to double.
*
* @param string $value
* @return void
*/
public function setRateAttribute($value)
{
$this->attributes['rate'] = (double) $value;
}

/**
* Get the current precision.
*
Expand Down
21 changes: 10 additions & 11 deletions app/Models/Setting/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class Tax extends Model
*/
protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled'];

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'rate' => 'double',
'enabled' => 'boolean',
];

/**
* Sortable columns.
*
Expand Down Expand Up @@ -101,17 +111,6 @@ public function scopeNotWithholding($query)
return $query->where($this->table . '.type', '<>', 'withholding');
}

/**
* Convert rate to double.
*
* @param string $value
* @return void
*/
public function setRateAttribute($value)
{
$this->attributes['rate'] = (double) $value;
}

/**
* Get the name including rate.
*
Expand Down

0 comments on commit 2d7c18b

Please sign in to comment.