Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update refund option and orders state mangement #526

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
{if $refund_allowed && !$hasCompletelyRefunded}
<a id="desc-order-standard_refund" class="btn btn-default" href="#refundForm">
<i class="icon-exchange"></i>
{if $order->hasBeenPaid()}
{if $order->getTotalPaid()|floatval}
{l s='Initiate refund'}
{else}
{l s='Cancel bookings'}
Expand Down Expand Up @@ -517,12 +517,7 @@
<td>{$payment_types[$payment['payment_type']]['name']|escape:'html':'UTF-8'}</td>
<td>{$payment['transaction_id']|escape:'html':'UTF-8'}</td>
<td>{displayPrice price=$payment['real_paid_amount'] currency=$payment['id_currency']}</td>
<td>
{if $payment['invoice_number']}
{$payment['invoice_number']}
{else}
{/if}
</td>
<td>{if isset($payment['invoice_number'])}{$payment['invoice_number']}{else}--{/if}</td>
<td class="actions">
<button class="btn btn-default open_payment_information">
<i class="icon-search"></i>
Expand Down
37 changes: 37 additions & 0 deletions controllers/admin/AdminOrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2422,15 +2422,52 @@ public function ajaxProcessAddProductOnOrder()
)));
}

/**
* This function is called when order is changed (Add/Edit/Delete room on order)
*/
public function sendChangedNotification(Order $order = null)
{
if (is_null($order)) {
$order = new Order(Tools::getValue('id_order'));
}

$this->updateOrderStatusOnOrderChange($order);

// load updated object
$order = new Order(Tools::getValue('id_order'));

Hook::exec('actionOrderEdited', array('order' => $order));
}

/**
* This function is called to manage order status when order is changed
*/
public function updateOrderStatusOnOrderChange($objOrder)
{
// check if new order amount is greater that old order amount and order payment is accepted
// then update order status to partial payment accepted
$currentOrderState = $objOrder->getCurrentOrderState();
$psOsPartialPaymentAccepted = Configuration::get('PS_OS_PARTIAL_PAYMENT_ACCEPTED');
if ($currentOrderState->paid == 1 && $currentOrderState->id != $psOsPartialPaymentAccepted) {
// calculate due amount
$dueAmount = $objOrder->total_paid_tax_incl - $objOrder->total_paid_real;
if ($dueAmount > 0) {
// now change order status to partial payment
$objOrderHistory = new OrderHistory();
$objOrderHistory->id_order = $objOrder->id;
$objOrderHistory->id_employee = (int) $this->context->employee->id;

$useExistingPayment = false;
if (!$objOrder->hasInvoice()) {
$useExistingPayment = true;
}

$objOrderHistory->changeIdOrderState($psOsPartialPaymentAccepted, $objOrder, $useExistingPayment);
$objOrderHistory->add();
}
}
}

public function ajaxProcessLoadProductInformation()
{
// $order_detail = new OrderDetail(Tools::getValue('id_order_detail'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function renderView()

$this->context->smarty->assign(
array (
'hasOrderPaid' => $objOrder->hasBeenPaid(),
'orderTotalPaid' => $objOrder->getTotalPaid(),
'customer_name' => $objCustomer->firstname.' '.$objCustomer->lastname,
'customer_email' => $objCustomer->email,
'orderReturnInfo' => (array)$objOrderReturn,
Expand Down Expand Up @@ -274,13 +274,13 @@ public function postProcess()
$idsReturnDetail = Tools::getValue('id_order_return_detail');
if (Validate::isLoadedObject($objOrderReturn = new OrderReturn($idOrderReturn))) {
$objOrder = new Order($objOrderReturn->id_order);
$hasOrderPaid = $objOrder->hasBeenPaid();
$orderTotalPaid = $objOrder->getTotalPaid();
$idRefundState = Tools::getValue('id_refund_state');
if (Validate::isLoadedObject($objRefundState = new OrderReturnState($idRefundState))) {
if ($objRefundState->refunded) {
$refundedAmounts = Tools::getValue('refund_amounts');

if ($hasOrderPaid) {
if ((float) $orderTotalPaid) {
if ($idsReturnDetail && count($idsReturnDetail)) {
if ($refundedAmounts) {
foreach ($idsReturnDetail as $idRetDetail) {
Expand Down Expand Up @@ -393,7 +393,7 @@ public function postProcess()
$objOrderReturn->id_transaction = $idTransaction;
} elseif (Tools::isSubmit('generateDiscount')) {
$objOrderReturn->payment_mode = 'Voucher';
} elseif (!$hasOrderPaid) {
} elseif (!((float) $orderTotalPaid)) {
$objOrderReturn->payment_mode = 'Unpaid by customer';
$objOrderReturn->id_transaction = '-';
}
Expand All @@ -405,18 +405,25 @@ public function postProcess()
// change state of the order refund
$objOrderReturn->changeIdOrderReturnState($idRefundState);

if ($objRefundState->refunded || $objRefundState->denied) {
if ($objRefundState->refunded) {
// check if order is paid the set status of the order to refunded
$objOrderHistory = new OrderHistory();
$objOrderHistory->id_order = (int)$objOrder->id;

if ($hasOrderPaid
&& $objOrder->hasCompletelyRefunded(OrderReturnState::ORDER_RETURN_STATE_FLAG_REFUNDED)
) {
$objOrderHistory->changeIdOrderState(Configuration::get('PS_OS_REFUND'), $objOrder);
$objOrderHistory->addWithemail();
} elseif ($objOrder->hasCompletelyRefunded(OrderReturnState::ORDER_RETURN_STATE_FLAG_DENIED)) {
$objOrderHistory->changeIdOrderState(Configuration::get('PS_OS_CANCELED'), $objOrder);
if ($objOrder->hasCompletelyRefunded(OrderReturnState::ORDER_RETURN_STATE_FLAG_REFUNDED)) {
$idOrderState = null;
if ((float) $orderTotalPaid) {
$idOrderState = Configuration::get('PS_OS_REFUND');
} else {
$idOrderState = Configuration::get('PS_OS_CANCELED');
}

$useExistingPayment = false;
if (!$objOrder->hasInvoice()) {
$useExistingPayment = true;
}

$objOrderHistory->changeIdOrderState($idOrderState, $objOrder, $useExistingPayment);
$objOrderHistory->addWithemail();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
{displayPrice price=$orderInfo['total_paid_tax_incl'] currency=$orderInfo['id_currency']}
</div>
</div>
<div class="form-group row">
<div class="col-sm-3">
<strong>{l s='Total paid amount' mod='hotelreservationsystem'} :</strong>
</div>
<div class="col-sm-9">
{displayPrice price=$orderTotalPaid currency=$orderInfo['id_currency']}
</div>
</div>
<div class="form-group row">
<div class="col-sm-3">
<strong>{l s='Way of payment' mod='hotelreservationsystem'} :</strong>
Expand Down Expand Up @@ -115,7 +123,7 @@
{if !$isRefundCompleted}
<th>{l s='Rooms cancelation charges' mod='hotelreservationsystem'}</th>
{/if}
{if $hasOrderPaid}
{if $orderTotalPaid|floatval}
<th>{l s='Refund amount' mod='hotelreservationsystem'}</th>
{/if}
</tr>
Expand All @@ -133,7 +141,7 @@
{if !$isRefundCompleted}
<td>{displayPrice price=$booking['cancelation_charge'] currency=$orderCurrency['id']}</td>
{/if}
{if $hasOrderPaid}
{if $orderTotalPaid|floatval}
<td>
<div class="input-group">
{if $isRefundCompleted}
Expand Down Expand Up @@ -228,7 +236,7 @@
</div>

{* Fields to submit refund information *}
{if $hasOrderPaid}
{if $orderTotalPaid|floatval}
<div class="refunded_state_fields" style="display:none;">
<div class="form-group">
<div class="col-sm-3">
Expand Down Expand Up @@ -291,19 +299,17 @@
</div>
</div>
</div>
{if $orderReturnInfo['by_admin']}
<div class="form-group">
<div class="col-sm-3">
</div>
<div class="col-sm-3">
<div class="checkbox">
<label>
<input value="1" type="checkbox" name="generateDiscount" id="generateDiscount"/> &nbsp;{l s='Create Voucher' mod='hotelreservationsystem'}
</label>
</div>
<div class="form-group">
<div class="col-sm-3">
</div>
<div class="col-sm-3">
<div class="checkbox">
<label>
<input value="1" type="checkbox" name="generateDiscount" id="generateDiscount"/> &nbsp;{l s='Create Voucher' mod='hotelreservationsystem'}
</label>
</div>
</div>
{/if}
</div>
</div>
{/if}
{/if}
Expand Down