From 42f7320e63deed3154f241a32a600644e202ed60 Mon Sep 17 00:00:00 2001
From: Du Peng <gnepud@gmail.com>
Date: Tue, 13 Sep 2022 13:03:56 +0200
Subject: [PATCH] (feat) show payment info in order

---
 .../components/store/order-item.tsx           |  2 +-
 .../components/store/show-order.tsx           | 32 ++++++++++++++++++-
 app/frontend/src/javascript/models/order.ts   |  4 +++
 app/services/orders/order_service.rb          |  2 +-
 app/views/api/orders/_order.json.jbuilder     |  3 ++
 app/views/api/orders/index.json.jbuilder      |  1 +
 config/locales/app.shared.en.yml              |  8 +++++
 7 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/app/frontend/src/javascript/components/store/order-item.tsx b/app/frontend/src/javascript/components/store/order-item.tsx
index 4f27f0d39..438144091 100644
--- a/app/frontend/src/javascript/components/store/order-item.tsx
+++ b/app/frontend/src/javascript/components/store/order-item.tsx
@@ -65,7 +65,7 @@ export const OrderItem: React.FC<OrderItemProps> = ({ order, currentUser }) => {
       <p className="date">{FormatLib.date(order.created_at)}</p>
       <div className='price'>
         <span>{t('app.shared.store.order_item.total')}</span>
-        <p>{FormatLib.price(order?.total)}</p>
+        <p>{FormatLib.price(order?.paid_total)}</p>
       </div>
       <FabButton onClick={() => showOrder(order)} icon={<i className="fas fa-eye" />} className="is-black" />
     </div>
diff --git a/app/frontend/src/javascript/components/store/show-order.tsx b/app/frontend/src/javascript/components/store/show-order.tsx
index 818668db6..e9d1f094e 100644
--- a/app/frontend/src/javascript/components/store/show-order.tsx
+++ b/app/frontend/src/javascript/components/store/show-order.tsx
@@ -100,6 +100,36 @@ export const ShowOrder: React.FC<ShowOrderProps> = ({ orderId, currentUser, onEr
     }
   };
 
+  /**
+   * Returns order's payment info
+   */
+  const paymentInfo = (): string => {
+    let paymentVerbose = '';
+    if (order.payment_method === 'card') {
+      paymentVerbose = t('app.shared.store.show_order.payment.settlement_by_debit_card');
+    } else if (order.payment_method === 'wallet') {
+      paymentVerbose = t('app.shared.store.show_order.payment.settlement_by_wallet');
+    } else {
+      paymentVerbose = t('app.shared.store.show_order.payment.settlement_done_at_the_reception');
+    }
+    paymentVerbose += ' ' + t('app.shared.store.show_order.payment.on_DATE_at_TIME', {
+      DATE: FormatLib.date(order.payment_date),
+      TIME: FormatLib.time(order.payment_date)
+    });
+    if (order.payment_method !== 'wallet') {
+      paymentVerbose += ' ' + t('app.shared.store.show_order.payment.for_an_amount_of_AMOUNT', { AMOUNT: FormatLib.price(order.paid_total) });
+    }
+    if (order.wallet_amount) {
+      if (order.payment_method === 'wallet') {
+        paymentVerbose += ' ' + t('app.shared.store.show_order.payment.for_an_amount_of_AMOUNT', { AMOUNT: FormatLib.price(order.wallet_amount) });
+      } else {
+        paymentVerbose += ' ' + t('app.shared.store.show_order.payment.and') + ' ' + t('app.shared.store.show_order.payment.by_wallet') + ' ' +
+                                 t('app.shared.store.show_order.payment.for_an_amount_of_AMOUNT', { AMOUNT: FormatLib.price(order.wallet_amount) });
+      }
+    }
+    return paymentVerbose;
+  };
+
   if (!order) {
     return null;
   }
@@ -183,7 +213,7 @@ export const ShowOrder: React.FC<ShowOrderProps> = ({ orderId, currentUser, onEr
       <div className="subgrid">
         <div className="payment-info">
           <label>{t('app.shared.store.show_order.payment_informations')}</label>
-          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum rerum commodi quaerat possimus! Odit, harum.</p>
+          {order.invoice_id && <p>{paymentInfo()}</p>}
         </div>
         <div className="amount">
           <label>{t('app.shared.store.show_order.amount')}</label>
diff --git a/app/frontend/src/javascript/models/order.ts b/app/frontend/src/javascript/models/order.ts
index 82244e639..6ff5ea089 100644
--- a/app/frontend/src/javascript/models/order.ts
+++ b/app/frontend/src/javascript/models/order.ts
@@ -22,6 +22,10 @@ export interface Order {
   created_at?: TDateISO,
   updated_at?: TDateISO,
   invoice_id?: number,
+  payment_method?: string,
+  payment_date?: TDateISO,
+  wallet_amount?: number,
+  paid_total?: number,
   order_items_attributes: Array<{
     id: number,
     orderable_type: string,
diff --git a/app/services/orders/order_service.rb b/app/services/orders/order_service.rb
index da86f15f3..d99d8b62d 100644
--- a/app/services/orders/order_service.rb
+++ b/app/services/orders/order_service.rb
@@ -17,7 +17,7 @@ class Orders::OrderService
       orders = orders.where(statistic_profile_id: current_user.statistic_profile.id)
     end
     orders = orders.where.not(state: 'cart') if current_user.member?
-    orders = orders.order(created_at: filters[:page].present? ? filters[:sort] : 'DESC')
+    orders = orders.order(updated_at: filters[:page].present? ? filters[:sort] : 'DESC')
     orders = orders.page(filters[:page]).per(ORDERS_PER_PAGE) if filters[:page].present?
     {
       data: orders,
diff --git a/app/views/api/orders/_order.json.jbuilder b/app/views/api/orders/_order.json.jbuilder
index ec2dd681a..08ff74afe 100644
--- a/app/views/api/orders/_order.json.jbuilder
+++ b/app/views/api/orders/_order.json.jbuilder
@@ -3,6 +3,9 @@
 json.extract! order, :id, :token, :statistic_profile_id, :operator_profile_id, :reference, :state, :created_at, :updated_at, :invoice_id,
               :payment_method
 json.total order.total / 100.0 if order.total.present?
+json.payment_date order.invoice.created_at if order.invoice_id.present?
+json.wallet_amount order.wallet_amount / 100.0 if order.wallet_amount.present?
+json.paid_total order.paid_total / 100.0 if order.paid_total.present?
 if order.coupon_id
   json.coupon do
     json.extract! order.coupon, :id, :code, :type, :percent_off, :validity_per_user
diff --git a/app/views/api/orders/index.json.jbuilder b/app/views/api/orders/index.json.jbuilder
index a7b57457a..bd4484f5a 100644
--- a/app/views/api/orders/index.json.jbuilder
+++ b/app/views/api/orders/index.json.jbuilder
@@ -7,6 +7,7 @@ json.total_count @result[:total_count]
 json.data @result[:data] do |order|
   json.extract! order, :id, :statistic_profile_id, :reference, :state, :created_at
   json.total order.total / 100.0 if order.total.present?
+  json.paid_total order.paid_total / 100.0 if order.paid_total.present?
   if order&.statistic_profile&.user
     json.user do
       json.id order.statistic_profile.user.id
diff --git a/config/locales/app.shared.en.yml b/config/locales/app.shared.en.yml
index 0ab9102d6..6e9a157c1 100644
--- a/config/locales/app.shared.en.yml
+++ b/config/locales/app.shared.en.yml
@@ -595,3 +595,11 @@ en:
             ready: "Ready"
             collected: "Collected"
             refunded: "Refunded"
+          payment:
+            by_wallet: "by wallet"
+            settlement_by_debit_card: "Settlement by debit card"
+            settlement_done_at_the_reception: "Settlement done at the reception"
+            settlement_by_wallet: "Settlement by wallet"
+            on_DATE_at_TIME: "on {DATE} at {TIME},"
+            for_an_amount_of_AMOUNT: "for an amount of {AMOUNT}"
+            and: 'and'