/var/www/vhosts/nabawater/backend/models
Edit: /var/www/vhosts/nabawater/backend/models/Branch.php (18932B)
[
self::SCENARIO_CREATE
]
],
[
[
'vendor_id',
'latitude',
'longitude',
'contact_email',
'contact_number',
//'pickup_time',
//'preparation_time',
//s 'user_name' ,
'delivery_time',
'approve_status',
'branch_status',
'branch_availability_status',
'branch_type',
'minimum_cart_value',
//'order_type'
],
'required',
'on' => [
self::SCENARIO_UPDATE
]
],
[
[
'assigned_stock',
'no_of_vehicles',
'vehicles_capacity,'
],
'safe',
'on' => [
self::SCENARIO_CREATE,
self::SCENARIO_UPDATE,
]
],
['contact_email', 'email'],
[['contact_email', 'contact_number', 'user_name'], 'checkunique'],
[
['contact_number'],
'match',
'pattern' => '/^[0-9]{7,15}$/',
'message' => Yii::t('backend', 'Mobile accept only 7 to 15 digit numbers'),
'skipOnEmpty' => true,
'skipOnError' => true
],
[
[
'first_name',
'last_name',
//'user_name',
'contact_number',
],
'required',
'on' => [self::SCENARIO_PROFILE_UPDATE],
],
[
[
'delivery_time',
'minimum_cart_value',
// 'assigned_stock',
//'no_of_vehicles',
],
'number',
'min' => 0,
'max' => 99999
],
[
[
'assigned_stock',
'no_of_vehicles',
'vehicles_capacity',
],
'integer',
'min' => 0,
'max' => 10000000
],
['password', 'string', 'min' => 6],
[
[
'password',
'confirm_password',
],
'required',
'on' => [
self::SCENARIO_CHANGE_PASSWORD
]
],
[
'confirm_password',
'compare',
'compareAttribute' => 'password',
'skipOnEmpty' => false,
'skipOnError' => false,
'message' => "Oops, That's not the same password as the first one",
'on' => [self::SCENARIO_CHANGE_PASSWORD]
],
];
// ['contact_number', 'validateNumber']
return ArrayHelper::merge(parent::rules(), $rules);
}
public function checkunique($attribute)
{
$query = self::find()->where([
$attribute => $this->{$attribute}
])->andWhere([
'<>',
'branch_status',
Item::DELETE
]);
if (!$this->isNewRecord) {
$query->andWhere([
'<>',
'branch_id',
$this->branch_id
]);
}
$model = $query->asArray()->all();
if (count($model) > 0) {
$this->addError($attribute, 'Already exists');
}
}
/**
* @return string
*/
public function validateNumber()
{
$phoneNumber = $this->contact_number;
$phoneNumUtil = PhoneNumberUtil::getInstance();
$parsedPhoneNum = $phoneNumUtil->parse($phoneNumber, "MY");
$isValid = $phoneNumUtil->isValidNumber($parsedPhoneNum);
if (!$isValid) {
$this->addError('contact_number', 'Please Enter Valid Malaysian Number');
}
}
/**
*
* @param type $index
* @return array
*/
public function getAvailabilityStatus($index = '')
{
$availabilityStatus = [
1 => Yii::t('backend', 'Open'),
2 => Yii::t('backend', 'Closed'),
/* 3 => 'PreOrder', */
4 => Yii::t('backend', 'Busy')
];
($index == '') ? ($availabilityStatus = $availabilityStatus) : ($availabilityStatus = ArrayHelper::getValue($availabilityStatus, $index));
return $availabilityStatus;
}
/**
*
* @return array
*/
public function attributeLabels()
{
return [
'vendor_id' => yii::t('backend', 'Vendor Name'),
'cuisine_id' => yii::t('backend', 'Cuisine Name'),
'busy_reason' => yii::t('backend', 'Reason'),
'delivery_charge_additional_per_km' => yii::t('backend', 'Delivery Charge Extra Per Km'),
'delivery_charge_base' => yii::t('backend', 'Delivery Base Km'),
'branch_name' => Yii::t('backend', 'Branch Name'),
'vendor_name' => Yii::t('backend', 'Vendor Name'),
'contact_email' => Yii::t('backend', 'Contact Email'),
'branch_status' => Yii::t('backend', 'Branch Status'),
'branch_availability_status' => Yii::t('backend', 'Branch Availability Status'),
'contact_number' => Yii::t('backend', 'Contact Number'),
'latitude' => Yii::t('backend', 'Latitude'),
'longitude' => Yii::t('backend', 'Longitude'),
'delivery_time' => Yii::t('backend', 'Delivery Time'),
'preparation_time' => Yii::t('backend', 'Preparation Time'),
'pickup_time' => Yii::t('backend', 'Pickup Time'),
'order_type' => Yii::t('backend', 'Order Type'),
'password' => Yii::t('backend', 'Password'),
'confirm_password' => Yii::t('backend', 'Confirm Password'),
'assigned_stock' => Yii::t('backend', 'Assigned Stock'),
'no_of_vehicles' => Yii::t('backend', 'No of Vechicles'),
'approve_status' => Yii::t('backend', 'Approved Status'),
'delivery_time' => Yii::t('backend', 'Delivery Time (Mins)')
];
}
/**
*
* @param type $index
* @return type
*/
public function getApprovalStatus($index = '')
{
$appStatus = [
1 => 'Pending',
2 => 'Approved',
3 => 'Rejected'
];
($index == '') ? ($appStatus = $appStatus) : ($appStatus = ArrayHelper::getValue($appStatus, $index));
return $appStatus;
}
/**
*
* @param type $index
* @return type
*/
public function getBranchUserTypeStatus($index = '')
{
$appStatus = [
1 => 'Branch',
2 => 'distributor',
];
($index == '') ? ($appStatus = $appStatus) : ($appStatus = ArrayHelper::getValue($appStatus, $index));
return $appStatus;
}
/**
*
* @param type $index
* @return type
*/
public function getBranchAvailabilityStatus($index = '')
{
$brnAvailabilityStatus = [
1 => 'open',
2 => 'closed',
/* 3 => 'pre-order',*/
4 => 'busy'
];
($index == '') ? ($brnAvailabilityStatus = $brnAvailabilityStatus) : ($brnAvailabilityStatus = ArrayHelper::getValue($brnAvailabilityStatus, $index));
return $brnAvailabilityStatus;
}
/**
*
* @param type $index
* @return type
*/
public function getOrderType($index = '')
{
$brnOrderType = [
1 => 'Delivery',
2 => 'Pick Up',
3 => 'Both'
];
($index == '') ? ($brnOrderType = $brnOrderType) : ($brnOrderType = ArrayHelper::getValue($brnOrderType, $index));
return $brnOrderType;
}
/**
*
* @return array
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_CREATE] = [
'vendor_id',
'latitude',
'longitude',
'contact_email',
'contact_number',
//'pickup_time',
//'preparation_time',
'delivery_time',
'approve_status',
'branch_status',
'branch_availability_status',
//'order_type',
// 'user_name',
'password',
'confirm_password',
'assigned_stock',
'no_of_vehicles',
'branch_type',
'vehicles_capacity',
'minimum_cart_value',
];
$scenarios[self::SCENARIO_UPDATE] = [
'vendor_id',
'latitude',
'longitude',
'contact_email',
'contact_number',
//'pickup_time',
'delivery_time',
//'preparation_time',
'approve_status',
'branch_status',
'branch_availability_status',
'assigned_stock',
'no_of_vehicles',
'branch_type',
'vehicles_capacity',
'minimum_cart_value',
//'order_type',
//'user_name'
];
$scenarios[self::SCENARIO_PROFILE_UPDATE] = [
'first_name',
'user_name',
'user_type',
'contact_email',
'last_name',
'contact_number',
'minimum_cart_value'
];
$scenarios[self::SCENARIO_CHANGE_PASSWORD] = [
'password',
'confirm_password'
];
return $scenarios;
}
/**
*
* @param type $index
* @return array
*/
public static function getBranchData()
{
if (AdminUser::isVendor()) {
$branchData = Branch::find()
->alias('B')
->select([
'B.branch_id',
'BL.branch_name'
])
->innerJoin(['BL' => BranchLang::tableName()], 'BL.branch_id = B.branch_id')
->where(['B.branch_status' => Branch::ACTIVE])
->where(['B.vendor_id' => Yii::$app->getUser()->getIdentity()->vendor_id])
->asArray()
->all();
} else {
$branchData = Branch::find()
->alias('B')
->select([
'B.branch_id',
'BL.branch_name'
])
->innerJoin(['BL' => BranchLang::tableName()], 'BL.branch_id = B.branch_id')
->where(['B.branch_status' => Branch::ACTIVE])
->asArray()
->all();
}
if (!empty($branchData)) {
return ArrayHelper::map($branchData, 'branch_id', 'branch_name');
} else {
return [];
}
}
public static function TotalCount()
{
$branchData = Branch::find()
->alias('B')
->where(['<>', 'B.branch_status', Branch::DELETE])
->count();
return $branchData;
}
/**
* Finds an identity by the given ID.
* @param string|int $id the ID to be looked for
* @return IdentityInterface the identity object that matches the given ID.
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentity($id)
{
return static::findOne(['branch_id' => $id, 'branch_status' => self::ACTIVE]);
}
/**
* Finds an identity by the given token.
* @param mixed $token the token to be looked for
* @param mixed $type the type of the token. The value of this parameter depends on the implementation.
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
* @return IdentityInterface the identity object that matches the given token.
* @throws \OutOfBoundsException
* @throws \BadMethodCallException
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$id = null;
$jwtObj = Jwt::getInstance();
if ($jwtObj->isValid($token)) {
$id = $jwtObj->getPrivateClaim($token);
}
return self::findOne($id);
}
/**
* Finds user by user_name
*
* @param string $user_name
* @return static|null
*/
/**
* @param $user_name
* @return array|null|\yii\db\ActiveRecord
*/
public static function findByUsername($user_name)
{
return static::find()
->where(['OR', ['user_name' => $user_name], ['contact_email' => $user_name]])
->andWhere(['branch_status' => self::ACTIVE])
->one();
}
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
if (!static::isPasswordResetTokenValid($token)) {
return null;
}
return static::findOne([
'password_reset_token' => $token,
'branch_status' => self::ACTIVE,
]);
}
/**
* Finds out if password reset token is valid
*
* @param string $token password reset token
* @return bool
*/
public static function isPasswordResetTokenValid($token)
{
if (empty($token)) {
return false;
}
$timestamp = (int) substr($token, strrpos($token, '_') + 1);
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
return $timestamp + $expire >= time();
}
/**
* Returns a key that can be used to check the validity of a given identity ID.
*
* The key should be unique for each individual user, and should be persistent
* so that it can be used to check the validity of the user identity.
*
* The space of such keys should be big enough to defeat potential identity attacks.
*
* This is required if [[AdminUser::enableAutoLogin]] is enabled.
* @return string a key that is used to check the validity of a given identity ID.
* @see validateAuthKey()
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* Returns an ID that can uniquely identify a user identity.
* @return string|int an ID that uniquely identifies a user identity.
*/
public function getId()
{
return $this->getPrimaryKey();
}
/**
* Validates the given auth key.
*
* This is required if [[AdminUser::enableAutoLogin]] is enabled.
* @param string $authKey the given auth key
* @return bool whether the given auth key is valid.
* @see getAuthKey()
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
* @throws \yii\base\InvalidParamException
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
* @throws \yii\base\Exception
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Generates new password reset token
* @throws \yii\base\Exception
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
/**
* @param $event \yii\web\User
* @throws \MongoDB\Driver\Exception\InvalidArgumentException
* @throws \yii\base\InvalidParamException
*/
protected static function afterLogin($event)
{
/* @var $model self */
$model = $event->identity;
//$model->last_login = date('Y-m-d H:i:s');
$model->save();
$permission_json = '[]';
$modelRole = Role::findOne($model->user_type);
if ($modelRole != null) {
$permission_json = $modelRole->backend_role_json;
}
if ($permission_json === null) {
$permission_json = '[]';
}
Com::setSession(ACCESS_RULE, $permission_json);
}
}