1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/models/EntityModel.php

90 lines
1.6 KiB
PHP
Raw Normal View History

2013-12-04 17:20:14 +01:00
<?php
class EntityModel extends Eloquent
{
protected $softDelete = true;
2014-01-09 11:39:20 +01:00
public $timestamps = true;
2014-01-08 21:09:47 +01:00
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();
2014-05-25 16:48:00 +02:00
if ($parent)
2014-01-09 14:44:55 +01:00
{
2013-12-10 18:18:35 +01:00
$entity->user_id = $parent->user_id;
$entity->account_id = $parent->account_id;
2014-01-09 14:44:55 +01:00
}
else if (Auth::check())
{
2014-01-09 12:24:03 +01:00
$entity->user_id = Auth::user()->id;
$entity->account_id = Auth::user()->account_id;
2014-01-09 14:44:55 +01:00
}
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');
}
2014-01-09 11:39:20 +01:00
public function getActivityKey()
2014-01-08 21:09:47 +01:00
{
return $this->getEntityType() . ':' . $this->public_id . ':' . $this->getName();
}
/*
public function getEntityType()
{
return '';
}
2013-12-04 17:20:14 +01:00
public function getNmae()
{
return '';
}
2014-01-08 21:09:47 +01:00
*/
2013-12-04 17:20:14 +01:00
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;
}
}