| Server IP : 14.225.216.240 / Your IP : 216.73.217.114 Web Server : nginx/1.20.1 System : Linux localhost.localdomain 5.14.0-596.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jun 27 16:02:33 UTC 2025 x86_64 User : taidh ( 1001) PHP Version : 8.3.19 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/pts/app/Module/Crm/Model/Classification/ |
Upload File : |
<?php
// phpcs:ignore
namespace App\Module\Crm\Model\Classification;
use App\Module\Crm\Model\Base;
use App\PTS\Utils\Strings;
use PDO;
use stdClass;
/**
* Loyalty Model
*
* @category Class
* @package App\Module\Crm\Model\Classification
* @author Phung, Truong K <truongkimphung1982@gmail.com>
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
* @link https://pts-digital.com/ PTS Digital Technology Co.,Ltd
*/
class Loyalty extends Base
{
const TYPE_SELL = 0;
const TYPE_USAGE = 1;
static $removed = [];
/**
* Reformat object
*
* @param mixed $item : The object loyalty
*
* @return mixed $object
*/
public static function format($item)
{
$item = Loyalty::baseFormat($item);
if (isset($item->status)) {
$item->status = Loyalty::status($item->status ?? -1);
}
Loyalty::props($item, self::$removed);
return $item;
}
/**
* List of all loyalties
* ----------------------------------------------------------------
*
* @param mixed $args : The arguments for simple search
* - q
* - page
* - limit
*
* @return object
*/
public function getLoyalties($args=null)
{
$statusCode = self::SUCCESS;
$response = new stdClass;
$this->auth();
if (self::$authId > 0) {
// Getting parameters in your request
$args = (object)($args ?? []);
$keyword = $args->q ?? '';
$page = ($args->page ?? 1);
$limit = (int)($args->limit ?? config('constant.limit.default'));
$this->prepare(
'CALL classification_loyalties_getall(
?,?,?,?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(3, $keyword, PDO::PARAM_STR, 255);
$this->bindParam(4, $page, PDO::PARAM_INT, 11);
$this->bindParam(5, $limit, PDO::PARAM_INT, 11);
$result = $this->fetchAll($statusCode);
if (isset($result) && $statusCode == self::SUCCESS) {
$items = $this->transform($result[1] ?? []);
$response->offset = $result[0][0]->offset ?? 0;
$response->total = $result[0][0]->foundRows ?? 0;
$response->count = count($items);
$response->items = $items;
}
}
return $response;
}
/**
* List of all loyalties by search
* ------------------------------------------------------------
*
* @param mixed $args : The arguments for advance search
* - $status
* - $name
* - page
* - limit
* @param int $statusCode : SQL exception
*
* @return object
*/
public function getLoyaltiesBySearch($args, &$statusCode=0)
{
$response = new stdClass;
$this->auth();
if (self::$authId > 0) {
// Getting parameters in your request
$args = (object)($args ?? []);
$status = $args->status ?? '';
$field = $args->field ?? '';
$sort = (int)($args->sort ?? -1);
$name = $args->name ?? '';
$page = ($args->page ?? 1);
$limit = (int)($args->limit ?? config('constant.limit.default'));
// convert from string to int
$status = (!empty($status) && $status != -1) ? self::findStatusIndex($status) : -1;
$this->prepare(
'CALL classification_loyalties_getall_search(
?,?,?,?,?,
?,?,?,?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(3, self::$authAdRole, PDO::PARAM_INT, 4);
$this->bindParam(4, self::$authRole, PDO::PARAM_INT, 4);
$this->bindParam(5, $status, PDO::PARAM_INT, 4);
$this->bindParam(6, $name, PDO::PARAM_STR, 50);
$this->bindParam(7, $field, PDO::PARAM_STR, 64);
$this->bindParam(8, $sort, PDO::PARAM_INT, 11);
$this->bindParam(9, $page, PDO::PARAM_INT, 11);
$this->bindParam(10, $limit, PDO::PARAM_INT, 11);
$result = $this->fetchAll($statusCode);
if (isset($result) && $statusCode == self::SUCCESS) {
$items = $this->transform($result[1] ?? []);
$response->offset = $result[0][0]->offset ?? 0;
$response->total = $result[0][0]->foundRows ?? 0;
$response->count = count($items);
$response->items = $items;
}
}
return $response;
}
/**
* List of all loyalties by active
* ------------------------------------------------------------
*
* @return mixed $array
*/
public function getLoyaltiesByActive()
{
$this->auth();
$this->prepare('CALL classification_loyalties_getall_active(?);');
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
return $this->fetchAll();
}
/**
* Getting loyalty information by specific ID's
* ------------------------------------------------------------
*
* @param int $id : The ID's loyalty
*
* @return mixed $object
*/
public function getLoyaltyById($id)
{
$response = null;
$statusCode = self::SUCCESS;
$this->prepare(
'CALL classification_loyalties_get(
?
);'
);
$this->bindParam(1, $id, PDO::PARAM_INT, 20);
$rs = $this->fetchOne($statusCode);
if ($statusCode == self::SUCCESS) {
$response = $this->format($rs);
}
return $response;
}
/**
* Getting loyalty information by specific GUID's
* ------------------------------------------------------------
*
* @param string $guid : The GUID's loyalty
*
* @return mixed $object
*/
public function getLoyaltyByGuid($guid)
{
$response = null;
$statusCode = self::SUCCESS;
$this->prepare(
'CALL classification_loyalties_get_guid(
?
);'
);
$this->bindParam(1, $guid, PDO::PARAM_STR, 36);
$rs = $this->fetchOne($statusCode);
if ($statusCode == self::SUCCESS) {
$response = $this->format($rs);
}
return $response;
}
/**
* Getting loyalty information by specific the type
* ------------------------------------------------------------
*
* @param int $applicationId : The ID's application
* @param int $type : The type of loyalty. 0: sell, 1: usage
*
* @return mixed $object
*/
public function getLoyaltyByType($applicationId=0, $type=0)
{
$statusCode = self::SUCCESS;
if ($applicationId < 1) {
$this->auth();
$applicationId = self::$appId;
}
$this->prepare(
'CALL classification_loyalties_get_type(
?,?
);'
);
$this->bindParam(1, $applicationId, PDO::PARAM_INT, 20);
$this->bindParam(2, $type, PDO::PARAM_STR, 36);
return $this->fetchOne($statusCode);
}
/**
* Create a new loyalty
* Update loyalty information by specific ID's
* ------------------------------------------------------------
*
* @param mixed $args : Loyalty model
* - $id
* - $name
* - $tags
* - $status
* @param int $statusCode : Exception SQL
*
* @return object
*/
public function addUpdate($args, &$statusCode = 0)
{
$this->auth();
$response = new stdClass;
if (self::$authId > 0) {
$args = (object) ($args ?? []);
$id = (int)($args->id ?? 0);
$locale = $args->locale ?? app()->getLocale();
$amount = Strings::number($args->amount ?? '', $locale);
$point = Strings::number($args->point ?? '', $locale);
$effectiveDate = Strings::dateTime($args->effective_date ?? null, $locale);
$expiryDate = Strings::dateTime($args->expiry_date ?? null, $locale);
$type = (int)($args->type ?? 0);
$status = $args->status ?? '';
// convert from string to int
$status = self::findStatusIndex($status);
$this->prepare(
'CALL classification_loyalties_addupdate(
?,?,?,?,?,
?,?,?,?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(3, self::$authFullName, PDO::PARAM_STR, 64);
$this->bindParam(4, $id, PDO::PARAM_INT, 20);
$this->bindParam(5, $amount, PDO::PARAM_STR, 20);
$this->bindParam(6, $point, PDO::PARAM_STR, 20);
$this->bindParam(7, $effectiveDate, PDO::PARAM_STR, 10);
$this->bindParam(8, $expiryDate, PDO::PARAM_STR, 10);
$this->bindParam(9, $type, PDO::PARAM_INT, 4);
$this->bindParam(10, $status, PDO::PARAM_INT, 4);
$response = $this->fetchOne($statusCode);
} else {
$statusCode = self::FORBIDDEN;
}
return $response;
}
/**
* Mask as "status" to other status
* ----------------------------------------------------------------
*
* @param mixed $id : The GUID's loyalty
* @param mixed $status : Value options < active, inactive >
* @param mixed $message : The remark for change status
* @param int $statusCode : SQL exception
*
* @return boolean
*/
public function changeStatus($id, &$status, $message=null, &$statusCode=0)
{
$this->auth();
$response = false;
if (self::$authId > 0) {
if (empty($id) || empty($status)) {
$statusCode = self::BAD_REQUEST;
}
if ($statusCode == self::SUCCESS) {
$status = self::findStatusIndex($status);
$this->prepare(
'CALL classification_loyalties_update_status(
?,?,?,?,?,
?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(3, self::$authFullName, PDO::PARAM_STR, 64);
$this->bindParam(4, $id, PDO::PARAM_STR, 36);
$this->bindParam(5, $status, PDO::PARAM_INT, 4);
$this->bindParam(6, $message, PDO::PARAM_STR, 4000);
$response = $this->execute($statusCode);
if ($statusCode == self::SUCCESS) {
$status = self::status($status);
}
}
} else {
$statusCode = self::FORBIDDEN;
}
return $response;
}
/**
* Mask as "status" to deleted
* ----------------------------------------------------------------
*
* @param string $guid : The GUID of loyalty
* @param mixed $message : The remark for delete data
* @param int $statusCode : SQL exception
*
* @return boolean
*/
public function markAsDelete($guid, $message=null, &$statusCode=0)
{
$this->auth();
$response = false;
if (self::$authId > 0) {
if (empty($guid)) {
$statusCode = self::BAD_REQUEST;
}
if ($statusCode == self::SUCCESS) {
$this->prepare(
'CALL classification_loyalties_delete(
?,?,?,?,?,
?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 4);
$this->bindParam(2, self::$authAdRole, PDO::PARAM_INT, 4);
$this->bindParam(3, self::$authRole, PDO::PARAM_INT, 4);
$this->bindParam(4, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(5, self::$authFullName, PDO::PARAM_STR, 64);
$this->bindParam(6, $guid, PDO::PARAM_STR, 36);
$this->bindParam(7, $message, PDO::PARAM_STR, 4000);
$response = $this->execute($statusCode);
}
} else {
$statusCode = self::FORBIDDEN;
}
return $response;
}
}