Add credit slip to voucher flow

This commit is contained in:
Abhishek Kumar
2023-01-19 16:53:19 +05:30
parent 8a05bf90c9
commit e910d33cd6
19 changed files with 780 additions and 220 deletions

View File

@@ -26,6 +26,9 @@
class OrderReturnCore extends ObjectModel
{
/** @var int */
public $id_order_return;
/** @var int */
public $id_customer;
@@ -48,12 +51,22 @@ class OrderReturnCore extends ObjectModel
/** @var int if request raised by the admin */
public $by_admin;
/** @var int id_cart_rule or id_order_slip */
public $id_return_type;
/** @var int whether $id_return_type is cart_rule or order_slip */
public $return_type;
/** @var string Object creation date */
public $date_add;
/** @var string Object last modification date */
public $date_upd;
/** possible values for $return_type */
const RETURN_TYPE_CART_RULE = 1;
const RETURN_TYPE_ORDER_SLIP = 2;
/**
* @see ObjectModel::$definition
*/
@@ -69,6 +82,8 @@ class OrderReturnCore extends ObjectModel
'refunded_amount' => array('type' => self::TYPE_FLOAT),
'state' => array('type' => self::TYPE_INT),
'by_admin' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_return_type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'return_type' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
@@ -232,14 +247,16 @@ class OrderReturnCore extends ObjectModel
if (!$context) {
$context = Context::getContext();
}
$data = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'order_return`
WHERE `id_customer` = '.(int)$customer_id.
($only_customer ? ' AND `by_admin` = 0' : '').
($order_id ? ' AND `id_order` = '.(int)$order_id : '').
($no_denied ? ' AND `state` != 4' : '').'
ORDER BY `date_add` DESC');
$sql = 'SELECT *
FROM `'._DB_PREFIX_.'order_return` orr
LEFT JOIN `'._DB_PREFIX_.'order_slip` ors
ON (ors.`id_order_slip` = orr.`id_return_type` AND orr.`return_type` = '.(int) self::RETURN_TYPE_ORDER_SLIP.')
WHERE orr.`id_customer` = '.(int)$customer_id.
($only_customer ? ' AND orr.`by_admin` = 0' : '').
($order_id ? ' AND orr.`id_order` = '.(int)$order_id : '').
($no_denied ? ' AND orr.`state` != 4' : '').'
ORDER BY orr.`date_add` DESC';
$data = Db::getInstance()->executeS($sql);
foreach ($data as $k => $or) {
$state = new OrderReturnState($or['state']);
$data[$k]['state_name'] = $state->name[$context->language->id];
@@ -373,144 +390,112 @@ class OrderReturnCore extends ObjectModel
/**
* Sets the new order return state
* @param int $newOrderReturnState
* @param int/object $idOrderReturn
*/
public function changeIdOrderReturnState($newOrderReturnState, $idOrderReturn, $idLang = 0)
public function changeIdOrderReturnState($newOrderReturnState, $idLang = 0)
{
if (Validate::isLoadedObject($objOrderReturn = new OrderReturn((int)$idOrderReturn))) {
$objOrder = new Order($objOrderReturn->id_order);
$objOrder = new Order($this->id_order);
if (!$idLang) {
$idLang = $objOrder->id_lang;
}
if (Validate::isLoadedObject(
$objOrderReturnState = new OrderReturnState((int)$newOrderReturnState, $idLang)
)) {
if ($this->state != $newOrderReturnState) {
$this->state = $newOrderReturnState;
$this->save();
if (!$idLang) {
$idLang = $objOrder->id_lang;
}
if (Validate::isLoadedObject(
$objOrderReturnState = new OrderReturnState((int)$newOrderReturnState, $idLang)
)) {
if ($objOrderReturn->state != $newOrderReturnState) {
$objOrderReturn->state = $newOrderReturnState;
$objOrderReturn->save();
if ($objOrderReturnState->send_email_to_customer
|| $objOrderReturnState->send_email_to_superadmin
|| $objOrderReturnState->send_email_to_employee
|| $objOrderReturnState->send_email_to_hotelier
) {
// Lets create data for the email templates
$objMail = new Mail();
$idHotel = 0;
if ($objOrderReturnState->send_email_to_customer
|| $objOrderReturnState->send_email_to_superadmin
|| $objOrderReturnState->send_email_to_employee
|| $objOrderReturnState->send_email_to_hotelier
) {
// Lets create data for the email templates
$objMail = new Mail();
$idHotel = 0;
$data = array (
'{status_name}' => $objOrderReturnState->name,
'{status_color}' => $objOrderReturnState->color,
'{order_reference}' => $objOrder->reference,
'{order_date}' => date('Y-m-d h:i:s', strtotime($objOrder->date_add)),
'{refunded_amount}' => Tools::displayPrice($this->refunded_amount, new Currency($objOrder->id_currency)),
'{payment_mode}' => $this->payment_mode,
'{id_transaction}' => $this->id_transaction,
);
$data = array (
'{status_name}' => $objOrderReturnState->name,
'{status_color}' => $objOrderReturnState->color,
'{order_reference}' => $objOrder->reference,
'{order_date}' => date('Y-m-d h:i:s', strtotime($objOrder->date_add)),
'{refunded_amount}' => Tools::displayPrice($objOrderReturn->refunded_amount, new Currency($objOrder->id_currency)),
'{payment_mode}' => $objOrderReturn->payment_mode,
'{id_transaction}' => $objOrderReturn->id_transaction,
);
// if mail is true for the customer then send mail to customer with selected template
$objCustomer = new Customer($objOrderReturn->id_customer);
if ($objOrderReturnState->send_email_to_customer && $objOrderReturnState->customer_template) {
if ($refundReqBookings = $objOrderReturn->getOrderRefundRequestedBookings(
$objOrderReturn->id_order,
$objOrderReturn->id,
0,
1
)) {
$idHotel = reset($refundReqBookings)['id_hotel'];
}
$refBookHtml = $objMail->getEmailTemplateContent('refund_request_detail_customer', Mail::TYPE_HTML, $refundReqBookings);
$refBookTxt = $objMail->getEmailTemplateContent('refund_request_detail_customer', Mail::TYPE_TEXT, $refundReqBookings);
$data['{refundBookingHtml}'] = $refBookHtml;
$data['{refundBookingTxt}'] = $refBookTxt;
// send customer information
$link = new Link();
$data['{refund_reqests_url}'] = $link->getPageLink('order-follow');
$data['{firstname}'] = $objCustomer->firstname;
$data['{lastname}'] = $objCustomer->lastname;
Mail::Send(
(int)$idLang,
$objOrderReturnState->customer_template,
$objOrderReturnState->name,
$data,
$objCustomer->email,
$objCustomer->firstname.' '.$objCustomer->lastname,
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
// if mail is true for the customer then send mail to customer with selected template
$objCustomer = new Customer($this->id_customer);
if ($objOrderReturnState->send_email_to_customer && $objOrderReturnState->customer_template) {
if ($refundReqBookings = $this->getOrderRefundRequestedBookings(
$this->id_order,
$this->id,
0,
1
)) {
$idHotel = reset($refundReqBookings)['id_hotel'];
}
if ($objOrderReturnState->admin_template) {
if ($refundReqBookings = $objOrderReturn->getOrderRefundRequestedBookings(
$objOrderReturn->id_order,
$objOrderReturn->id
)) {
$idHotel = $refundReqBookings[0]['id_hotel'];
}
$refBookHtml = $objMail->getEmailTemplateContent('refund_request_detail_customer', Mail::TYPE_HTML, $refundReqBookings);
$refBookTxt = $objMail->getEmailTemplateContent('refund_request_detail_customer', Mail::TYPE_TEXT, $refundReqBookings);
$refBookHtml = $objMail->getEmailTemplateContent('refund_request_detail_admin', Mail::TYPE_HTML, $refundReqBookings);
$refBookTxt = $objMail->getEmailTemplateContent('refund_request_detail_admin', Mail::TYPE_TEXT, $refundReqBookings);
$data['{refundBookingHtml}'] = $refBookHtml;
$data['{refundBookingTxt}'] = $refBookTxt;
$data['{cancelation_reason}'] = $objOrderReturn->question;
$data['{refundBookingHtml}'] = $refBookHtml;
$data['{refundBookingTxt}'] = $refBookTxt;
// send customer information
$link = new Link();
$data['{refund_reqests_url}'] = $link->getPageLink('order-follow');
$data['{firstname}'] = $objCustomer->firstname;
$data['{lastname}'] = $objCustomer->lastname;
// send mail to the super admin
if ($objOrderReturnState->send_email_to_superadmin) {
// send superadmin information
if (Validate::isLoadedObject($superAdmin = new Employee(_PS_ADMIN_PROFILE_))) {
if (Validate::isEmail($superAdmin->email)) {
$data['{customer_name}'] = $objCustomer->firstname.' '.$objCustomer->lastname;
$data['{customer_email}'] = $objCustomer->email;
$data['{firstname}'] = $superAdmin->firstname;
$data['{lastname}'] = $superAdmin->lastname;
Mail::Send(
(int)$idLang,
$objOrderReturnState->customer_template,
$objOrderReturnState->name,
$data,
$objCustomer->email,
$objCustomer->firstname.' '.$objCustomer->lastname,
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
}
Mail::Send(
(int)$idLang,
$objOrderReturnState->admin_template,
$objOrderReturnState->name,
$data,
$superAdmin->email,
$superAdmin->firstname.' '.$superAdmin->lastname,
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
}
}
}
if ($idHotel
&& Validate::isLoadedObject($objHotel = new HotelBranchInformation($idHotel))
) {
// send mail to the hotelier
if ($objOrderReturnState->send_email_to_hotelier) {
if ($objOrderReturnState->admin_template) {
if ($refundReqBookings = $this->getOrderRefundRequestedBookings(
$this->id_order,
$this->id
)) {
$idHotel = $refundReqBookings[0]['id_hotel'];
}
$refBookHtml = $objMail->getEmailTemplateContent('refund_request_detail_admin', Mail::TYPE_HTML, $refundReqBookings);
$refBookTxt = $objMail->getEmailTemplateContent('refund_request_detail_admin', Mail::TYPE_TEXT, $refundReqBookings);
$data['{cancelation_reason}'] = $this->question;
$data['{refundBookingHtml}'] = $refBookHtml;
$data['{refundBookingTxt}'] = $refBookTxt;
// send mail to the super admin
if ($objOrderReturnState->send_email_to_superadmin) {
// send superadmin information
if (Validate::isLoadedObject($superAdmin = new Employee(_PS_ADMIN_PROFILE_))) {
if (Validate::isEmail($superAdmin->email)) {
$data['{customer_name}'] = $objCustomer->firstname.' '.$objCustomer->lastname;
$data['{customer_email}'] = $objCustomer->email;
$data['{firstname}'] = '';
$data['{lastname}'] = '';
$data['{email}'] = $objHotel->email;
$data['{firstname}'] = $superAdmin->firstname;
$data['{lastname}'] = $superAdmin->lastname;
Mail::Send(
(int)$idLang,
$objOrderReturnState->admin_template,
$objOrderReturnState->name,
$data,
$objHotel->email,
null,
$superAdmin->email,
$superAdmin->firstname.' '.$superAdmin->lastname,
null,
null,
null,
@@ -520,38 +505,66 @@ class OrderReturnCore extends ObjectModel
(int)$objOrder->id_shop
);
}
}
}
if ($idHotel
&& Validate::isLoadedObject($objHotel = new HotelBranchInformation($idHotel))
) {
// send mail to the hotelier
if ($objOrderReturnState->send_email_to_hotelier) {
$data['{customer_name}'] = $objCustomer->firstname.' '.$objCustomer->lastname;
$data['{customer_email}'] = $objCustomer->email;
$data['{firstname}'] = '';
$data['{lastname}'] = '';
$data['{email}'] = $objHotel->email;
// send mail to the employee
if ($objOrderReturnState->send_email_to_employee) {
if ($htlAccesses = $objHotel->getHotelAccess($idHotel)) {
$data['{customer_name}'] = $objCustomer->firstname.' '.$objCustomer->lastname;
$data['{customer_email}'] = $objCustomer->email;
foreach ($htlAccesses as $access) {
if ($access['id_profile'] != _PS_ADMIN_PROFILE_) {
if ($htlEmployees = Employee::getEmployeesByProfile($access['id_profile'])) {
foreach ($htlEmployees as $empl) {
if (Validate::isEmail($empl['email'])) {
// send hotel manager (employee) have permission for this hotel
$data['{firstname}'] = $empl['firstname'];
$data['{lastname}'] = $empl['lastname'];
$data['{email}'] = $empl['email'];
Mail::Send(
(int)$idLang,
$objOrderReturnState->admin_template,
$objOrderReturnState->name,
$data,
$objHotel->email,
null,
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
}
Mail::Send(
(int)$idLang,
$objOrderReturnState->admin_template,
$objOrderReturnState->name,
$data,
$empl['email'],
$empl['firstname'].' '.$empl['lastname'],
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
}
// send mail to the employee
if ($objOrderReturnState->send_email_to_employee) {
if ($htlAccesses = $objHotel->getHotelAccess($idHotel)) {
$data['{customer_name}'] = $objCustomer->firstname.' '.$objCustomer->lastname;
$data['{customer_email}'] = $objCustomer->email;
foreach ($htlAccesses as $access) {
if ($access['id_profile'] != _PS_ADMIN_PROFILE_) {
if ($htlEmployees = Employee::getEmployeesByProfile($access['id_profile'])) {
foreach ($htlEmployees as $empl) {
if (Validate::isEmail($empl['email'])) {
// send hotel manager (employee) have permission for this hotel
$data['{firstname}'] = $empl['firstname'];
$data['{lastname}'] = $empl['lastname'];
$data['{email}'] = $empl['email'];
Mail::Send(
(int)$idLang,
$objOrderReturnState->admin_template,
$objOrderReturnState->name,
$data,
$empl['email'],
$empl['firstname'].' '.$empl['lastname'],
null,
null,
null,
null,
_PS_MAIL_DIR_,
false,
(int)$objOrder->id_shop
);
}
}
}
@@ -561,18 +574,21 @@ class OrderReturnCore extends ObjectModel
}
}
}
// executes hook
Hook::exec(
'actionOrderReFundStatusPostUpdate',
array(
'new_order_return_status' => $newOrderReturnState,
'id_order_return' => (int)$idOrderReturn
)
);
}
// executes hook
Hook::exec(
'actionOrderReFundStatusPostUpdate',
array(
'new_order_return_status' => $newOrderReturnState,
'id_order_return' => (int) $this->id_order_return,
)
);
return true;
}
}
return false;
}

View File

@@ -27,7 +27,7 @@
class OrderSlipCore extends ObjectModel
{
/** @var int */
public $id;
public $id_order_slip;
/** @var int */
public $id_customer;
@@ -62,6 +62,12 @@ class OrderSlipCore extends ObjectModel
/** @var int */
public $partial;
/** @var int */
public $redeem_status = self::REDEEM_STATUS_ACTIVE;
/** @var int */
public $id_cart_rule;
/** @var string Object creation date */
public $date_add;
@@ -71,6 +77,9 @@ class OrderSlipCore extends ObjectModel
/** @var int */
public $order_slip_type = 0;
const REDEEM_STATUS_ACTIVE = 1;
const REDEEM_STATUS_REDEEMED = 2;
/**
* @see ObjectModel::$definition
*/
@@ -90,6 +99,8 @@ class OrderSlipCore extends ObjectModel
'shipping_cost_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
'amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
'partial' => array('type' => self::TYPE_INT),
'redeem_status' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_cart_rule' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'order_slip_type' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
@@ -336,6 +347,7 @@ class OrderSlipCore extends ObjectModel
$order_slip->{'total_products_tax_'.$inc_or_ex_1} = 0;
$order_slip->{'total_products_tax_'.$inc_or_ex_2} = 0;
$total_products = array();
foreach ($booking_list as &$booking) {
$objHtlBooking = new HotelBookingDetail($booking['id_htl_booking']);
$order_detail = new OrderDetail((int)$objHtlBooking->id_order_detail);
@@ -543,6 +555,95 @@ class OrderSlipCore extends ObjectModel
return $ecotax_detail;
}
/**
* generate voucher and return id of the created rule if successful, false otherwise
*/
public function generateVoucher($sendMail = true)
{
if (!Validate::isLoadedObject($this)) {
return false;
}
if ($this->redeem_status != self::REDEEM_STATUS_ACTIVE) {
return false;
}
$objOrder = new Order($this->id_order);
if (!Validate::isLoadedObject($objOrder)) {
return false;
}
$objCustomer = new Customer($this->id_customer);
if (!Validate::isLoadedObject($objCustomer)) {
return false;
}
$context = Context::getContext();
$objCartRule = new CartRule();
$idsLanguage = Language::getIDs();
$creditSlipPrefix = Configuration::get('PS_CREDIT_SLIP_PREFIX', $context->language->id);
$creditSlipID = sprintf(('%1$s%2$06d'), $creditSlipPrefix, (int) $this->id_order_slip);
$dateFrom = $this->date_add;
$dateTo = date('Y-m-d H:i:s', strtotime($dateFrom) + (3600 * 24 * 365.25)); /* 1 year */
foreach ($idsLanguage as $idLang) {
$objCartRule->name[$idLang] = sprintf('Voucher for credit slip #%s', $creditSlipID);
}
$objCartRule->description = sprintf('Order: #%s', $objOrder->id);
$objCartRule->code = 'CS'.(int) ($this->id_order_slip).'C'.(int) ($objCustomer->id).'O'.(int) ($objOrder->id);
$objCartRule->quantity = 1;
$objCartRule->quantity_per_user = 1;
$objCartRule->id_customer = $objCustomer->id;
$objCartRule->date_from = $dateFrom;
$objCartRule->date_to = $dateTo;
$objCartRule->highlight = 1;
$objCartRule->active = 1;
$objCartRule->reduction_amount = $this->amount;
$objCartRule->reduction_tax = true;
$objCartRule->minimum_amount_currency = $objOrder->id_currency;
$objCartRule->reduction_currency = $objOrder->id_currency;
if (!$objCartRule->save()) {
return false;
}
$this->redeem_status = self::REDEEM_STATUS_REDEEMED;
$this->id_cart_rule = (int) $objCartRule->id;
$this->save();
if ($sendMail) {
$objCartRule = new CartRule($this->id_cart_rule);
$objCustomer = new Customer($objCartRule->id_customer);
$objCurrency = new Currency($objCartRule->reduction_currency, $context->language->id);
$mailVars['{firstname}'] = $objCustomer->firstname;
$mailVars['{lastname}'] = $objCustomer->lastname;
$mailVars['{credit_slip_id}'] = $creditSlipID;
$mailVars['{voucher_code}'] = $objCartRule->code;
$mailVars['{voucher_amount}'] = Tools::displayPrice($objCartRule->reduction_amount, $objCurrency, false);
Mail::Send(
$context->language->id,
'credit_slip_voucher',
sprintf(Mail::l('New voucher for your credit slip #%s', $context->language->id), $creditSlipID),
$mailVars,
$objCustomer->email,
$objCustomer->firstname.' '.$objCustomer->lastname,
null,
null,
null,
null,
_PS_MAIL_DIR_,
true
);
}
return $this->id_cart_rule;
}
public function getWsOrderSlipDetails()
{
$query = 'SELECT id_order_slip as id, id_order_detail, product_quantity, amount_tax_excl, amount_tax_incl