| 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;
/**
* Membership 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 Membership extends Base
{
static $removed = [];
/**
* Reformat object
*
* @param mixed $item : The object membership
*
* @return mixed $object
*/
public static function format($item)
{
$item = Membership::baseFormat($item);
if (isset($item->status)) {
$item->status = Membership::status($item->status ?? -1);
}
Membership::props($item, self::$removed);
return $item;
}
/**
* List of all memberships
* ----------------------------------------------------------------
*
* @param mixed $args : The arguments for simple search
*
* @return object
*/
public function getMemberships($args)
{
$statusCode = self::SUCCESS;
$response = new stdClass;
$this->auth();
if (self::$authId > 0) {
// Getting parameters in your request
$args = (object)($args ?? []);
$locale = $args->locale ?? app()->getLocale();
$keyword = $args->q ?? '';
$page = ($args->page ?? 1);
$limit = (int)($args->limit ?? config('constant.limit.default'));
$this->prepare(
'CALL classification_memberships_getall(
?,?,?,?,?,
?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, self::$authId, PDO::PARAM_INT, 20);
$this->bindParam(3, $locale, PDO::PARAM_STR, 10);
$this->bindParam(4, $keyword, PDO::PARAM_STR, 255);
$this->bindParam(5, $page, PDO::PARAM_INT, 11);
$this->bindParam(6, $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 memberships by search
* ------------------------------------------------------------
*
* @param mixed $args : The arguments for advance search
* @param int $statusCode : The SQL exception
*
* @return object
*/
public function getMembershipsBySearch($args, &$statusCode=0)
{
$response = new stdClass;
$this->auth();
if (self::$authId > 0) {
// Getting parameters in your request
$args = (object)($args ?? []);
$locale = $args->locale ?? config('app.locale');
$status = $args->status ?? '';
$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_memberships_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, $locale, PDO::PARAM_STR, 10);
$this->bindParam(6, $status, PDO::PARAM_INT, 4);
$this->bindParam(7, $name, PDO::PARAM_STR, 50);
$this->bindParam(8, $type, PDO::PARAM_INT, 4);
$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 memberships by active
* ------------------------------------------------------------
*
* @param string $locale : The locale
*
* @return mixed $array
*/
public function getMembershipsByActive($locale=null)
{
$this->auth();
$locale = $locale ?? app()->getLocale();
$this->prepare('CALL classification_memberships_getall_active(?,?,?);');
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, $locale, PDO::PARAM_STR, 10);
return $this->fetchAll();
}
/**
* Getting membership information by specific ID's
* ------------------------------------------------------------
*
* @param int $id : The ID's membership
* @param string $locale : The locale
*
* @return mixed $object
*/
public function getMembershipById($id, $locale=null)
{
$response = null;
$this->auth();
$locale = $locale ?? app()->getLocale();
$statusCode = self::SUCCESS;
$this->prepare(
'CALL classification_memberships_get(
?,?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, $id, PDO::PARAM_INT, 20);
$this->bindParam(3, $locale, PDO::PARAM_STR, 10);
$rs = $this->fetchOne($statusCode);
if ($statusCode == self::SUCCESS) {
$response = $this->format($rs);
}
return $response;
}
/**
* Getting membership information by specific GUID's
* ------------------------------------------------------------
*
* @param string $guid : The GUID's membership
* @param string $locale : The locale
*
* @return mixed $object
*/
public function getMembershipByGuid($guid, $locale=null)
{
$response = null;
$locale = $locale ?? app()->getLocale();
$statusCode = self::SUCCESS;
$this->auth();
$this->prepare(
'CALL classification_memberships_get_guid(
?,?,?
);'
);
$this->bindParam(1, self::$appId, PDO::PARAM_INT, 20);
$this->bindParam(2, $guid, PDO::PARAM_STR, 36);
$this->bindParam(3, $locale, PDO::PARAM_STR, 10);
$rs = $this->fetchOne($statusCode);
if ($statusCode == self::SUCCESS) {
$response = $this->format($rs);
}
return $response;
}
/**
* Create a new membership
* Update membership information by specific ID's
* ------------------------------------------------------------
*
* @param mixed $args : Membership model
* - $id
* - $locale
* - $name
* - $code
* - $description
* - $iseq
* - $fn_type
* - $effective_date
* - $expiry_date
* - $upgrade_amount_min
* - $upgrade_amount_max
* - $upgrade_amount_month
* - $discount_amount
* - $discount_percent
* - $loyalty_amount
* - $loyalty_point
* - $dob_amount
* - $dob_percent
* - $dob_voucher_id
* - $image_front
* - $image_back
* - $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 ?? []);
// validate required field
$name = $args->name ?? '';
if (empty($name)) {
$statusCode = self::BAD_REQUEST;
}
if ($statusCode == self::SUCCESS) {
$id = (int)($args->id ?? 0);
$locale = $args->locale ?? app()->getLocale();
$code = $args->code ?? null;
$imageFront = $args->image_front ?? null;
$imageBack = $args->image_back ?? null;
$description = json_encode((array)($args->description ?? []));
$fnType = Strings::arr2Int($args->fn_type ?? []);
$effectiveDate = Strings::dateTime($args->effective_date ?? null);
$expiryDate = Strings::dateTime($args->expiry_date ?? null);
$upgradeAmountMin = Strings::number($args->upgrade_amount_min ?? 0, $locale);
$upgradeAmountMax = Strings::number($args->upgrade_amount_max ?? 0, $locale);
$upgradeMonth = Strings::number($args->upgrade_month ?? 0, $locale);
$discountAmount = Strings::number($args->discount_amount ?? 0, $locale);
$discountPercent = Strings::number($args->discount_percent ?? 0, $locale);
$loyaltyAmount = Strings::number($args->loyalty_amount ?? 0, $locale);
$loyaltyPoint = Strings::number($args->loyalty_point ?? 0, $locale);
$dobAmount = Strings::number($args->dob_amount ?? 0, $locale);
$dobPercent = Strings::number($args->dob_percent ?? 0, $locale);
$dobVoucherId = (int)($args->dob_voucher_id ?? 0);
$iseq = Strings::number($args->iseq ?? 0, $locale);
$status = $args->status ?? '';
// convert from string to int
$status = self::findStatusIndex($status);
$this->prepare(
'CALL classification_memberships_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, $locale, PDO::PARAM_STR, 10);
$this->bindParam(6, $name, PDO::PARAM_STR, 255);
$this->bindParam(7, $code, PDO::PARAM_STR, 64);
$this->bindParam(8, $iseq, PDO::PARAM_INT, 4);
$this->bindParam(9, $fnType, PDO::PARAM_INT, 20);
$this->bindParam(10, $effectiveDate, PDO::PARAM_STR, 10);
$this->bindParam(11, $expiryDate, PDO::PARAM_STR, 10);
$this->bindParam(12, $upgradeAmountMin, PDO::PARAM_STR, 18);
$this->bindParam(13, $upgradeAmountMax, PDO::PARAM_STR, 18);
$this->bindParam(14, $upgradeMonth, PDO::PARAM_INT, 4);
$this->bindParam(15, $discountAmount, PDO::PARAM_STR, 18);
$this->bindParam(16, $discountPercent, PDO::PARAM_STR, 5);
$this->bindParam(17, $loyaltyAmount, PDO::PARAM_STR, 18);
$this->bindParam(18, $loyaltyPoint, PDO::PARAM_STR, 10);
$this->bindParam(19, $dobAmount, PDO::PARAM_STR, 18);
$this->bindParam(20, $dobPercent, PDO::PARAM_STR, 5);
$this->bindParam(21, $dobVoucherId, PDO::PARAM_INT, 20);
$this->bindParam(22, $imageFront, PDO::PARAM_STR, 512);
$this->bindParam(23, $imageBack, PDO::PARAM_STR, 512);
$this->bindParam(24, $description, PDO::PARAM_STR);
$this->bindParam(25, $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 membership
* @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_memberships_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 membership
* @param mixed $message : The remark for change status
* @param int $statusCode : SQL exception
*
* @return object
*/
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_memberships_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;
}
/**
* Lock/ Unlock membership
* ----------------------------------------------------------------
*
* @param mixed $id : The GUID's membership
* @param int $isLocked : 0: unlocking, 1: locking
* @param mixed $message : The remark for change status
* @param int $statusCode : SQL exception
*
* @return boolean
*/
public function changeLocked($id, &$isLocked, $message=null, &$statusCode=0)
{
$this->auth();
$response = false;
if (self::$authId > 0) {
$this->prepare(
'CALL classification_memberships_update_locked(
?,?,?,?,?,
?
);'
);
$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, $isLocked, PDO::PARAM_INT, 1);
$this->bindParam(6, $message, PDO::PARAM_STR, 4000);
$response = $this->execute($statusCode);
} else {
$statusCode = self::FORBIDDEN;
}
return $response;
}
}