Files
luos/workflow/engine/src/ProcessMaker/Model/Application.php

191 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace ProcessMaker\Model;
use Illuminate\Database\Eloquent\Model;
2019-05-23 16:30:49 -04:00
use Illuminate\Support\Facades\DB;
class Application extends Model
{
protected $table = "APPLICATION";
protected $primaryKey = 'APP_NUMBER';
public $incrementing = false;
// No timestamps
public $timestamps = false;
2019-06-10 15:04:14 -04:00
// Status id
const STATUS_DRAFT = 1;
const STATUS_TODO = 2;
const STATUS_COMPLETED = 3;
const STATUS_CANCELED = 4;
2020-10-21 16:56:11 -04:00
// Status name and status id
public static $app_status_values = ['DRAFT' => 1, 'TO_DO' => 2, 'COMPLETED' => 3, 'CANCELLED' => 4];
public function delegations()
{
return $this->hasMany(Delegation::class, 'APP_UID', 'APP_UID');
}
2020-02-17 14:09:54 -04:00
public function currentUser()
{
2020-02-17 14:09:54 -04:00
return $this->belongsTo(User::class, 'APP_CUR_USER', 'USR_UID');
}
2020-02-17 14:09:54 -04:00
public function creatorUser()
{
2020-02-17 14:09:54 -04:00
return $this->belongsTo(User::class, 'APP_INIT_USER', 'USR_UID');
}
2019-05-23 16:30:49 -04:00
2020-10-23 17:32:12 -04:00
/**
* Scope for query to get the positive cases
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePositivesCases($query)
{
$result = $query->where('APP_NUMBER', '>', 0);
return $result;
}
2019-05-23 16:30:49 -04:00
/**
2020-01-06 14:30:41 -04:00
* Scope for query to get the application by APP_UID.
2020-02-17 14:09:54 -04:00
*
2020-01-06 14:30:41 -04:00
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $appUid
2020-02-17 14:09:54 -04:00
*
2020-01-06 14:30:41 -04:00
* @return \Illuminate\Database\Eloquent\Builder
2019-05-23 16:30:49 -04:00
*/
2020-01-06 14:30:41 -04:00
public function scopeAppUid($query, $appUid)
2019-05-23 16:30:49 -04:00
{
2020-01-06 14:30:41 -04:00
$result = $query->where('APP_UID', '=', $appUid);
return $result;
2019-05-23 16:30:49 -04:00
}
2020-10-23 17:32:12 -04:00
/**
* Scope for query to get the application by status Id
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $status
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeStatusId($query, int $status)
{
$result = $query->where('APP_STATUS_ID', '=', $status);
return $result;
}
2019-05-23 16:30:49 -04:00
/**
* Scope for query to get the applications by PRO_UID.
2020-01-06 14:30:41 -04:00
*
2019-05-23 16:30:49 -04:00
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $proUid
2020-01-06 14:30:41 -04:00
*
2019-05-23 16:30:49 -04:00
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeProUid($query, $proUid)
{
$result = $query->where('PRO_UID', '=', $proUid);
return $result;
}
2020-01-06 14:30:41 -04:00
/**
* Get Applications by PRO_UID, ordered by APP_NUMBER.
*
* @param string $proUid
*
* @return object
* @see ReportTables->populateTable()
*/
public static function getByProUid($proUid)
{
$query = Application::query()
->select()
->proUid($proUid)
2020-10-30 18:00:08 +00:00
->positivesCases()
2020-01-06 14:30:41 -04:00
->orderBy('APP_NUMBER', 'ASC');
return $query->get();
}
/**
* Get information related to the created case
*
* @param string $appUid
*
* @return array|bool
*/
public static function getCase($appUid)
{
$query = Application::query()->select(['APP_STATUS', 'APP_INIT_USER']);
$query->appUid($appUid);
$result = $query->get()->toArray();
$firstElement = head($result);
return $firstElement;
}
2020-02-17 14:09:54 -04:00
2020-12-16 17:47:42 -04:00
/**
* Get app number
*
* @param string $appUid
*
* @return int
*/
public static function getCaseNumber($appUid)
{
$query = Application::query()->select(['APP_NUMBER'])
->appUid($appUid)
->limit(1);
$results = $query->get();
$caseNumber = 0;
$results->each(function ($item) use (&$caseNumber) {
$caseNumber = $item->APP_NUMBER;
});
return $caseNumber;
}
2020-02-17 14:09:54 -04:00
/**
* Update properties
*
* @param string $appUid
* @param array $fields
*
* @return array
*/
public static function updateColumns($appUid, $fields)
{
$properties = [];
$properties['APP_ROUTING_DATA'] = !empty($fields['APP_ROUTING_DATA']) ? serialize($fields['APP_ROUTING_DATA']) : serialize([]);
// This column will to update only when the thread is related to the user
if (!empty($fields['APP_CUR_USER'])) {
$properties['APP_CUR_USER'] = $fields['APP_CUR_USER'];
}
Application::query()->appUid($appUid)->update($properties);
return $properties;
}
2020-10-23 17:32:12 -04:00
/**
* Get Applications by PRO_UID, ordered by APP_NUMBER.
*
* @param string $proUid
* @param int $status
*
* @return object
* @see ReportTables->populateTable()
*/
public static function getCountByProUid(string $proUid, $status = 2)
{
$query = Application::query()
->select()
->proUid($proUid)
->statusId($status)
->positivesCases();
2020-12-16 17:47:42 -04:00
return $query->get()->count(['APP_NUMBER']);
2020-10-23 17:32:12 -04:00
}
}