1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 18:01:35 +02:00
invoiceninja/app/models/EntityModel.php

71 lines
1.3 KiB
PHP
Raw Normal View History

2013-12-04 17:20:14 +01:00
<?php
class EntityModel extends Eloquent
{
protected $softDelete = true;
2014-01-02 09:27:48 +01:00
protected $hidden = ['id', 'created_at', 'deleted_at', 'updated_at'];
2013-12-10 18:18:35 +01:00
public static function createNew($parent = false)
{
2013-12-04 17:20:14 +01:00
$className = get_called_class();
$entity = new $className();
2013-12-10 18:18:35 +01:00
if (Auth::check()) {
$entity->user_id = Auth::user()->id;
$entity->account_id = Auth::user()->account_id;
} else if ($parent) {
$entity->user_id = $parent->user_id;
$entity->account_id = $parent->account_id;
} else {
2014-01-02 00:12:33 +01:00
Utils::fatalError();
2013-12-10 18:18:35 +01:00
}
2013-12-11 12:11:59 +01:00
$lastEntity = $className::withTrashed()->scope(false, $entity->account_id)->orderBy('public_id', 'DESC')->first();
2013-12-04 17:20:14 +01:00
if ($lastEntity)
{
$entity->public_id = $lastEntity->public_id + 1;
}
else
{
$entity->public_id = 1;
}
2013-12-10 18:18:35 +01:00
2013-12-04 17:20:14 +01:00
return $entity;
}
public static function getPrivateId($publicId)
{
$className = get_called_class();
return $className::scope($publicId)->pluck('id');
}
public function getNmae()
{
return '';
}
2013-12-10 18:18:35 +01:00
public function scopeScope($query, $publicId = false, $accountId = false)
2013-12-04 17:20:14 +01:00
{
2013-12-29 00:33:48 +01:00
if (!$accountId)
{
2013-12-10 18:18:35 +01:00
$accountId = Auth::user()->account_id;
}
2013-12-29 00:33:48 +01:00
2013-12-10 18:18:35 +01:00
$query->whereAccountId($accountId);
2013-12-04 17:20:14 +01:00
if ($publicId)
{
if (is_array($publicId))
{
$query->whereIn('public_id', $publicId);
}
else
{
$query->wherePublicId($publicId);
}
}
return $query;
}
}