/var/www/vhosts/nabawater/common/models
Edit: /var/www/vhosts/nabawater/common/models/Order.php (28635B)
getIsNewRecord() && $this->order_status == self::ORDER_STATUS_REJECTED && $this->oldAttributes['order_status'] != $this->order_status) {
// if($this->order_type == 1 && $this->oldAttributes['order_status'] != Order::ORDER_STATUS_PENDING){
// $status = Order::ORDER_STATUS_REJECTED;
// $responseData = Com::getJsonData('PUT', Configuration::get(Configuration::DELIVERY_BOY_ACCESS_URL) . "order/".$this->order_key."/".$status."?company_id=" . Configuration::get(Configuration::COMPANY_KEY), [], 0);
// }
// $orderItem = OrderItem::find()->where(['order_id' => $this->order_id])->all();
// foreach ($orderItem as $key => $orderItemModel) {
// $itemModel = Item::find()
// ->where(['item_id' => $orderItemModel->item_id])
// ->one();
// if ($itemModel !== null) {
// $itemModel->quantity = $itemModel->quantity + $orderItemModel->quantity;
// $itemModel->save(false);
// }
// }
if ($this->payment_option != Order::PAYMENT_OPTION_COD) {
$user = User::findOne(['user_id' => $this->user_id]);
if ($user != null) {
$modelWallet = WalletTransaction::find()
->where([
'user_key' => $user->user_key,
'pay_type' => WalletTransaction::TRANS_TYPE_REFUND,
'order_number' => $this->order_number
])->one();
if (empty($modelWallet)) {
$modelWallet = new WalletTransaction();
$modelWallet->user_key = $user->user_key;
$modelWallet->pay_type = WalletTransaction::TRANS_TYPE_REFUND;
$modelWallet->amount = $this->order_total;
$modelWallet->transaction_status = WalletTransaction::TRANSACTION_SUCCESS;
$modelWallet->first_name = $user->first_name;
$modelWallet->last_name = $user->last_name;
$modelWallet->email = $user->email;
$modelWallet->mobile_number = $user->mobile_number;
$modelWallet->transaction_type = WalletTransaction::TRANS_TYPE_REFUND;
$modelWallet->order_number = $this->order_number;
$modelWallet->save(false);
$user->wallet_point += $this->order_total;
$user->save(false);
}
}
}
}
if (!$this->getIsNewRecord() && $this->order_status == self::ORDER_STATUS_IS_CUSTOMER_CANCELED && $this->oldAttributes['order_status'] != $this->order_status) {
// if($this->order_type == 1 && $this->oldAttributes['order_status'] != Order::ORDER_STATUS_PENDING){
// $status = Order::ORDER_STATUS_REJECTED;
// $responseData = Com::getJsonData('PUT', Configuration::get(Configuration::DELIVERY_BOY_ACCESS_URL) . "order/".$this->order_key."/".$status."?company_id=" . Configuration::get(Configuration::COMPANY_KEY), [], 0);
// }
if ($this->payment_option != Order::PAYMENT_OPTION_COD) {
//print_r($this); die;
$user = User::findOne(['user_id' => $this->user_id]);
if ($user != null) {
$modelWallet = WalletTransaction::find()
->where([
'user_key' => $user->user_key,
'pay_type' => WalletTransaction::TRANS_TYPE_REFUND,
'order_number' => $this->order_number
])->one();
if (empty($modelWallet)) {
$amount = $this->order_total + $this->total_subscription_price;
$modelWallet = new WalletTransaction();
$modelWallet->user_key = $user->user_key;
$modelWallet->pay_type = WalletTransaction::TRANS_TYPE_REFUND;
$modelWallet->amount = $amount;
$modelWallet->transaction_status = WalletTransaction::TRANSACTION_SUCCESS;
$modelWallet->first_name = $user->first_name;
$modelWallet->last_name = $user->last_name;
$modelWallet->email = $user->email;
$modelWallet->mobile_number = $user->mobile_number;
$modelWallet->transaction_type = WalletTransaction::TRANS_TYPE_REFUND;
$modelWallet->order_number = $this->order_number;
$modelWallet->save(false);
$user->wallet_point += $amount;
$user->save(false);
}
}
}
}
// Rejected to Item Quanity Revert to item End
//loaylity point earn add Start
if (!$this->getIsNewRecord() && $this->order_status == self::ORDER_STATUS_DELIVERED && $this->oldAttributes['order_status'] != $this->order_status) {
$user = User::findOne(['user_id' => $this->user_id]);
if ($user != null) {
$user->loyality_point += $this->earn_loyality_point;
$user->save(false);
if ($this->promotion_amount != 0) {
$modelWallet = WalletTransaction::find()
->where([
'user_key' => $user->user_key,
'pay_type' => WalletTransaction::TRANS_TYPE_PROMATION,
'order_number' => $this->order_number
])->one();
if (empty($modelWallet)) {
$modelWallet = new WalletTransaction();
$modelWallet->user_key = $user->user_key;
$modelWallet->pay_type = WalletTransaction::TRANS_TYPE_PROMATION;
$modelWallet->amount = $this->promotion_amount;
$modelWallet->transaction_status = WalletTransaction::TRANSACTION_SUCCESS;
$modelWallet->first_name = $user->first_name;
$modelWallet->last_name = $user->last_name;
$modelWallet->email = $user->email;
$modelWallet->mobile_number = $user->mobile_number;
$modelWallet->transaction_type = WalletTransaction::TRANS_TYPE_PROMATION;
$modelWallet->order_number = $this->order_number;
$modelWallet->save(false);
// $modelUser->wallet_point -= $modelWallet->amount;
//$modelUser->save(false);
$user->wallet_point += $this->promotion_amount;
$user->save(false);
}
}
}
}
if (!$this->getIsNewRecord() && $this->order_status == self::ORDER_STATUS_DELIVERED && $this->oldAttributes['order_status'] != $this->order_status) {
if (!empty($this->qr_pdf_path)) {
$this->sendMailNotification($this->order_key);
}
}
//loaylity point earn end
// if(!$this->getIsNewRecord()){
// if(in_array($this->order_status,[Order::ORDER_STATUS_ACCEPTED, Order::ORDER_STATUS_DELIVERED])){
// $msg = 'Your Order '. '#'.$this->order_number.' has been ' . $this->getOrderStatusLabelCustom($this->order_status);
// Com::send_order_sms($this->customer_mobile_number, $msg);
// }
// }
return parent::save($runValidation, $attributeNames);
}
private function sendMailNotification($order_key)
{
$modelOrder = Order::find()
->select([
'order_id',
'vendor_id',
'branch_id',
'payment_option',
'delivery_option',
'order_type',
'order_number',
'user_id',
'vat',
'vat_cgst',
'vat_sgst',
'vat_igst',
'vat_percent',
'service_tax',
'service_tax_percent',
'item_total',
'delivery_fee',
'package_price',
'delivery_boy_tip',
'coupon_offer_value',
'order_total',
'order_date_time',
'pickup_delivery_date_time'
])
->orderBy(['order_id' => SORT_DESC])
->where(['order_key' => $order_key])
->one();
$modelUser = User::findOne(['user_id' => $modelOrder->user_id]);
$user_token = Yii::$app->getUser()->getIdentity();
$token = isset($user_token['auth_key']) ? $user_token['auth_key'] : "";
$orderData = ApiHelper::getInstance()
->setRequestMethod(ApiHelper::REQUEST_METHOD_GET)
->setUrl('order/details-new', ['order_key' => $order_key]);
$apiResponse = $orderData->run();
if (!$apiResponse->isSuccess()) {
$message = $apiResponse->getMessage();
$response['status'] = false;
$response['message'] = $message;
Com::failureFlash($message);
}
$results = [];
$ingResult = [];
$quantities = [];
if (!empty($apiResponse->getData())) {
$orderData = $apiResponse->getData();
$orderItem = $orderData['items'];
foreach ($orderItem as $item) {
$result = [
'item_total_price' => $item['item_total_price'],
'item_name' => $item['item_name'],
'item_description' => $item['item_description'],
'item_price' => $item['item_price'],
'ingredients' => $item['ingredients']
];
$results[] = $result;
$quantities[] = $item['quantity'];
}
}
$modelBranchlist = Branch::find()
->where(['branch_id' => $modelOrder->branch_id])
->one();
$modelVendor = Vendor::find()
->where(['vendor_id' => $modelOrder->vendor_id])
->one();
$paymentOption = Order::getPaymentOptionLabel((int)$modelOrder->payment_option);
if ($modelUser != null && $modelUser->email != null) {
$resposeArray = \backend\models\Order::find()
->where(['order_id' => $modelOrder->getPrimaryKey()])
->orderBy(['order_id' => SORT_DESC])
->one();
$qr_path = \common\helpers\QrHelper::getInstance()->generate($this->order_date_time, $this->order_total, $this->vat_cgst);
$finalFile = Yii::getAlias('@approot') . '/' . $this->qr_pdf_path;
// chmod($finalFile, 0775);
MailerQueueHelper::getInstance()
->setTo($modelUser->email)
->setSubject('Order Placed')
->setAttach($finalFile)
->setView('orderPlaced', [
'name' => $modelUser->first_name,
'orderId' => $modelOrder->order_number,
'date' => $modelOrder->order_date_time,
'pickup_delivery' => $modelOrder->pickup_delivery_date_time,
'payment_option' => $modelOrder->payment_option,
'order_type' => $modelOrder->order_type,
'delivery_option' => $modelOrder->delivery_option,
'order_key' => $modelOrder->order_key,
'itemStatus' => $results,
'itemQuantity' => $quantities,
'vat' => $modelOrder->vat,
'cgst' => $modelOrder->vat_cgst,
'sgst' => $modelOrder->vat_sgst,
'igst' => $modelOrder->vat_igst,
'packagingFee' => $modelOrder->package_price,
'itemTotalPrice' => $modelOrder->item_total,
'deliveryFee' => $modelOrder->delivery_fee,
'coupon_offer_value' => $modelOrder->coupon_offer_value,
'deliveryboytip' => $modelOrder->delivery_boy_tip,
'subTotal' => $modelOrder->order_total,
'qr_path' => $qr_path,
'qr_show' => true,
])->push();
}
return;
}
/**
*
* @return array
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors[] = [
'class' => AttributeKeyGeneratorBehaviour::class,
'attributes' => [
'order_key'
]
];
return $behaviors;
}
/**
* @return array
*/
public static function getPaymentStatusList()
{
return [
[
'type' => Order::PAYMENT_STATUS_PENDING,
'label' => Yii::t('backend', 'Pending')
],
[
'type' => Order::PAYMENT_STATUS_SUCCESS,
'label' => Yii::t('backend', 'Success')
],
[
'type' => Order::PAYMENT_STATUS_FAILURE,
'label' => Yii::t('backend', 'Failure')
],
];
}
/**
* @param $type
* @return string
*/
public static function getPaymentStatusLabel($type)
{
$list = self::getPaymentStatusList();
foreach ($list as $value) {
if ($value['type'] === (int)$type) {
return $value['label'];
}
}
return Yii::$app->getFormatter()->format(null, 'text');
}
/**
* @param bool $typeAsIndex
* @return array
*/
public static function getDeliveryOptionList($typeAsIndex = false)
{
$list = [
[
'type' => self::DELIVERY_OPTION_ASAP,
'label' => Yii::t('common', 'ASAP')
],
[
'type' => self::DELIVERY_OPTION_LATER,
'label' => Yii::t('common', 'Later')
]
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param $type
* @return mixed|string
*/
public static function getDeliveryOptionLabel($type)
{
$list = self::getDeliveryOptionList(true);
if (ArrayHelper::keyExists((int)$type, $list)) {
return $list[$type];
}
return Yii::$app->getFormatter()->format(null, 'text');
}
/**
* @param bool $typeAsIndex
* @return array
*/
public static function getOrderTypeList($typeAsIndex = false)
{
$list = [
[
'type' => self::ORDER_TYPE_DELIVERY,
'label' => Yii::t('backend', 'Delivery')
],
[
'type' => self::ORDER_TYPE_OTHERS,
'label' => Yii::t('backend', 'Others')
],
[
'type' => self::ORDER_TYPE_PICKUP,
'label' => Yii::t('backend', 'Pickup')
],
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param $type
* @return string
*/
public static function getOrderTypeLabel($type)
{
$list = self::getOrderTypeList(true);
if (ArrayHelper::keyExists((int)$type, $list)) {
return $list[$type];
}
return '(not set)';
}
/**
* @param $typeAsIndex
* @return array
*/
public static function getOrderStatusList($typeAsIndex = false)
{
$list = [
['type' => self::ORDER_STATUS_PENDING, 'label' => Yii::t('common', 'New Order')],
['type' => self::ORDER_STATUS_ACCEPTED, 'label' => Yii::t('common', 'Confirmed')],
['type' => self::ORDER_STATUS_PREPARED, 'label' => Yii::t('common', 'Preparation')],
['type' => self::ORDER_STATUS_ONTHEWAY, 'label' => Yii::t('common', 'On the Way')],
['type' => self::ORDER_STATUS_DELIVERED, 'label' => Yii::t('common', 'Delivered')],
['type' => self::ORDER_STATUS_REJECTED, 'label' => Yii::t('common', 'Rejected')],
['type' => self::ORDER_STATUS_DELIVERED_TO_RIDER, 'label' => Yii::t('common', 'Delivery to Rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ASSIGNED, 'label' => Yii::t('common', 'Assigned to Rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ACCEPTED, 'label' => Yii::t('common', 'Accepted by delivery boy')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REJECTED, 'label' => Yii::t('common', 'Rejected by delivery boy')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REQUESTED, 'label' => Yii::t('common', 'Delivery boy requested')],
['type' => self::ORDER_STATUS_READY_TO_PICKUP, 'label' => Yii::t('common', 'Ready to pickup')],
['type' => self::ORDER_STATUS_IS_CUSTOMER_CANCELED, 'label' => Yii::t('common', 'Order Cancelled')]
];
/* $list = [
['type' => self::ORDER_STATUS_PENDING, 'label' => Yii::t('common', 'Pending')],
['type' => self::ORDER_STATUS_ACCEPTED, 'label' => Yii::t('common', 'Confirmed')],
['type' => self::ORDER_STATUS_PREPARED, 'label' => Yii::t('common', 'Preparation')],
['type' => self::ORDER_STATUS_ONTHEWAY, 'label' => Yii::t('common', 'On the Way')],
['type' => self::ORDER_STATUS_DELIVERED, 'label' => Yii::t('common', 'Delivered')],
['type' => self::ORDER_STATUS_REJECTED, 'label' => Yii::t('common', 'Rejected')],
['type' => self::ORDER_STATUS_DELIVERED_TO_RIDER, 'label' => Yii::t('common', 'Picked up')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ASSIGNED, 'label' => Yii::t('common', 'Picked up') ],
['type' => self::ORDER_STATUS_DELIVERYBOY_ACCEPTED, 'label' => Yii::t('common', 'Picked up') ],
['type' => self::ORDER_STATUS_DELIVERYBOY_REJECTED, 'label' => Yii::t('common', 'Picked up') ],
['type' => self::ORDER_STATUS_DELIVERYBOY_REQUESTED, 'label' => Yii::t('common', 'Picked up') ],
['type' => self::ORDER_STATUS_READY_TO_PICKUP, 'label' => Yii::t('common', 'Picked up') ]
];*/
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param $typeAsIndex
* @return array
*/
public static function getOrderStatusListNew($typeAsIndex = false)
{
$list = [
['type' => self::ORDER_STATUS_PENDING, 'label' => Yii::t('common', 'New Order')],
['type' => self::ORDER_STATUS_ACCEPTED, 'label' => Yii::t('common', 'Confirmed')],
['type' => self::ORDER_STATUS_PREPARED, 'label' => Yii::t('common', 'Preparation')],
['type' => self::ORDER_STATUS_ONTHEWAY, 'label' => Yii::t('common', 'On the Way')],
['type' => self::ORDER_STATUS_DELIVERED, 'label' => Yii::t('common', 'Delivered')],
['type' => self::ORDER_STATUS_REJECTED, 'label' => Yii::t('common', 'Rejected')],
['type' => self::ORDER_STATUS_DELIVERED_TO_RIDER, 'label' => Yii::t('common', 'Delivery to Rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ASSIGNED, 'label' => Yii::t('common', 'Assigned to Rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ACCEPTED, 'label' => Yii::t('common', 'Accepted by delivery boy')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REJECTED, 'label' => Yii::t('common', 'Rejected by delivery boy')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REQUESTED, 'label' => Yii::t('common', 'Delivery boy requested')],
['type' => self::ORDER_STATUS_READY_TO_PICKUP, 'label' => Yii::t('common', 'Ready to pickup')],
['type' => self::ORDER_STATUS_IS_CUSTOMER_CANCELED, 'label' => Yii::t('common', 'Order Cancelled')]
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param $typeAsIndex
* @return array
*/
public static function getOrderStatusListCustom($typeAsIndex = false)
{
$list = [
['type' => self::ORDER_STATUS_PENDING, 'label' => Yii::t('common', 'New order')],
['type' => self::ORDER_STATUS_ACCEPTED, 'label' => Yii::t('common', 'Confirmed')],
['type' => self::ORDER_STATUS_PREPARED, 'label' => Yii::t('common', 'Preparation')],
['type' => self::ORDER_STATUS_ONTHEWAY, 'label' => Yii::t('common', 'On the way')],
['type' => self::ORDER_STATUS_DELIVERED, 'label' => Yii::t('common', 'Delivered')],
['type' => self::ORDER_STATUS_REJECTED, 'label' => Yii::t('common', 'Rejected')],
['type' => self::ORDER_STATUS_DELIVERED_TO_RIDER, 'label' => Yii::t('common', 'Delivered to rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ASSIGNED, 'label' => Yii::t('common', 'Assigned to rider')],
['type' => self::ORDER_STATUS_DELIVERYBOY_ACCEPTED, 'label' => Yii::t('common', 'Accepted by delivery boy')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REJECTED, 'label' => Yii::t('common', 'Rejected')],
['type' => self::ORDER_STATUS_DELIVERYBOY_REQUESTED, 'label' => Yii::t('common', 'Delivery boy requested')],
['type' => self::ORDER_STATUS_READY_TO_PICKUP, 'label' => Yii::t('common', 'Ready to pickup')],
['type' => self::ORDER_STATUS_IS_CUSTOMER_CANCELED, 'label' => Yii::t('common', 'Order Cancelled')]
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
*
* @param type $type
* @return string
*/
public static function getOrderStatusLabel($type)
{
$list = self::getOrderStatusList();
foreach ($list as $value) {
if ($value['type'] === (int)$type) {
return $value['label'];
}
}
return '(not set)';
}
/**
*
* @param type $type
* @return string
*/
public static function getOrderStatusLabelNew($type)
{
$list = self::getOrderStatusListNew();
foreach ($list as $value) {
if ($value['type'] === (int)$type) {
return $value['label'];
}
}
return '(not set)';
}
/**
*
* @param type $type
* @return string
*/
public static function getOrderStatusLabelCustom($type)
{
$list = self::getOrderStatusListCustom();
foreach ($list as $value) {
if ($value['type'] === (int)$type) {
return $value['label'];
}
}
return '(not set)';
}
/**
* @param bool $typeAsIndex
* @return array
*/
public static function getPaymentOptionList($typeAsIndex = false, $frontend = false)
{
$list = [
[
'type' => self::PAYMENT_OPTION_COD,
'label' => Yii::t('backend', 'Cash On Delivery')
],
[
'type' => self::PAYMENT_OPTION_WALLET,
'label' => Yii::t('backend', 'Wallet')
],
[
'type' => self::PAYMENT_OPTION_ONLINE,
'label' => Yii::t('backend', 'Visa/MasterCard')
],
// [
// 'type' => self::TABAOGO_CREDITS,
// 'label' => Yii::t('backend', 'Tabaogo Credit')
// ],
// [
// 'type' => self::HELP_ME_PAY,
// 'label' => Yii::t('backend', 'Help Me Pay')
// ],
[
'type' => self::PAYMENT_OPTION_MADA,
'label' => Yii::t('backend', 'MADA')
],
[
'type' => self::PAYMENT_OPTION_STC,
'label' => Yii::t('backend', 'STC Pay')
],
[
'type' => self::PAYMENT_OPTION_APPLE_PAY,
'label' => Yii::t('backend', 'Apple Pay')
],
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param bool $typeAsIndex
* @return array
*/
public static function getOrderTypeOptionList($typeAsIndex = false, $frontend = false)
{
$list = [
[
'type' => self::USER_ORDER_TYPE_NORMAL,
'label' => Yii::t('backend', 'Normal Order')
],
[
'type' => self::USER_ORDER_TYPE_SUBSCRIPTION,
'label' => Yii::t('backend', 'Subscription Order')
]
];
if ($typeAsIndex) {
$list = ArrayHelper::map($list, 'type', 'label');
}
return $list;
}
/**
* @param $type
* @return string
*/
public static function getPaymentOptionLabel($type)
{
$list = self::getPaymentOptionList();
foreach ($list as $value) {
if ($value['type'] === (int)$type) {
return $value['label'];
}
}
return '(not set)';
}
public static function getSubscriptionDetails($orderId)
{
$subscriptionDetails = [];
$subscriptionDetails = OrderSubscription::find()
->select(['*'])
->where(['order_id' => $orderId])
->asArray()
->one();
return $subscriptionDetails;
}
}