summaryrefslogtreecommitdiff
path: root/src/lib/transaction/components/Transaction.jsx
blob: 55e959fa56e8f9bf2d7424aee35c2b12410b3d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import Spinner from '@/core/components/elements/Spinner/Spinner'
import useTransaction from '../hooks/useTransaction'
import TransactionStatusBadge from './TransactionStatusBadge'
import Divider from '@/core/components/elements/Divider/Divider'
import { useMemo, useRef, useState } from 'react'
import { downloadPurchaseOrder, downloadQuotation } from '../utils/transactions'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import uploadPoApi from '../api/uploadPoApi'
import { toast } from 'react-hot-toast'
import getFileBase64 from '@/core/utils/getFileBase64'
import currencyFormat from '@/core/utils/currencyFormat'
import VariantGroupCard from '@/lib/variant/components/VariantGroupCard'
import { ChevronDownIcon, ChevronRightIcon, ChevronUpIcon } from '@heroicons/react/24/outline'
import Link from '@/core/components/elements/Link/Link'
import Alert from '@/core/components/elements/Alert/Alert'
import checkoutPoApi from '../api/checkoutPoApi'
import cancelTransactionApi from '../api/cancelTransactionApi'

const Transaction = ({ id }) => {
  const { transaction } = useTransaction({ id })

  const poNumber = useRef('')
  const poFile = useRef('')
  const [uploadPo, setUploadPo] = useState(false)
  const openUploadPo = () => setUploadPo(true)
  const closeUploadPo = () => setUploadPo(false)
  const submitUploadPo = async () => {
    const file = poFile.current.files[0]
    const name = poNumber.current.value
    if (typeof file === 'undefined' || !name) {
      toast.error('Nomor dan Dokumen PO harus diisi', { position: 'bottom-center' })
      return
    }
    if (file.size > 5000000) {
      toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' })
      return
    }
    const data = { name, file: await getFileBase64(file) }
    const isUploaded = await uploadPoApi({ id, data })
    if (isUploaded) {
      toast.success('Berhasil upload PO')
      transaction.refetch()
      closeUploadPo()
      return
    }
    toast.error('Terjadi kesalahan internal, coba lagi nanti atau hubungi kami')
  }

  const [cancelTransaction, setCancelTransaction] = useState(false)
  const openCancelTransaction = () => setCancelTransaction(true)
  const closeCancelTransaction = () => setCancelTransaction(false)
  const submitCancelTransaction = async () => {
    const isCancelled = await cancelTransactionApi({ transaction: transaction.data })
    if (isCancelled) {
      toast.success('Berhasil batalkan transaksi')
      transaction.refetch()
    }
    closeCancelTransaction()
  }

  const checkout = async () => {
    if (!transaction.data?.purchaseOrderFile) {
      toast.error('Mohon upload dokumen PO anda sebelum melanjutkan pesanan')
      return
    }
    await checkoutPoApi({ id })
    toast.success('Berhasil melanjutkan pesanan')
    transaction.refetch()
  }

  const memoizeVariantGroupCard = useMemo(() => (
    <div className='p-4 pt-0 flex flex-col gap-y-3'>
      <VariantGroupCard
        variants={transaction.data?.products}
        buyMore
      />
      <div className='flex justify-between mt-3 font-medium'>
        <p>Total Belanja</p>
        <p>{currencyFormat(transaction.data?.amountTotal)}</p>
      </div>
    </div>
  ), [transaction.data])

  if (transaction.isLoading) {
    return (
      <div className='flex justify-center my-6'>
        <Spinner className='w-6 text-gray_r-12/50 fill-gray_r-12' />
      </div>
    )
  }

  return (
    transaction.data?.name && (
      <>
        <div className='flex flex-col gap-y-4 p-4'>
          <DescriptionRow label='Status Transaksi'>
            <div className='flex justify-end'>
              <TransactionStatusBadge status={transaction.data?.status} />
            </div>
          </DescriptionRow>
          <DescriptionRow label='No Transaksi'>{transaction.data?.name}</DescriptionRow>
          <DescriptionRow label='Ketentuan Pembayaran'>
            {transaction.data?.paymentTerm}
          </DescriptionRow>
          <DescriptionRow label='Nama Sales'>{transaction.data?.sales}</DescriptionRow>
          <DescriptionRow label='Waktu Transaksi'>{transaction.data?.dateOrder}</DescriptionRow>
        </div>

        <Divider />

        <div className='p-4 flex flex-col gap-y-4'>
          <DescriptionRow label='Purchase Order'>
            {transaction.data?.purchaseOrderName || '-'}
          </DescriptionRow>
          <div className='flex items-center'>
            <p className='text-gray_r-11 leading-none'>Dokumen PO</p>
            <button
              type='button'
              className='btn-light py-1.5 px-3 ml-auto'
              onClick={
                transaction.data?.purchaseOrderFile
                  ? () => downloadPurchaseOrder(transaction.data)
                  : openUploadPo
              }
            >
              {transaction.data?.purchaseOrderFile ? 'Download' : 'Upload'}
            </button>
          </div>
        </div>

        <Divider />

        <div className='font-medium p-4'>Detail Produk</div>

        { memoizeVariantGroupCard }

        <Divider />

        <SectionAddress address={transaction.data?.address} />

        <Divider />

        <div className='p-4'>
          <p className='font-medium'>Invoice</p>
          <div className='flex flex-col gap-y-3 mt-4'>
            {transaction.data?.invoices?.map((invoice, index) => (
              <Link
                href={`/my/invoice/${invoice.id}`}
                key={index}
              >
                <div className='shadow rounded-md p-4 text-gray_r-12 font-normal flex justify-between'>
                  <div>
                    <p className='mb-2'>{invoice?.name}</p>
                    <div className='flex items-center gap-x-1'>
                      {invoice.amountResidual > 0 ? (
                        <div className='badge-red'>Belum Lunas</div>
                      ) : (
                        <div className='badge-green'>Lunas</div>
                      )}
                      <p className='text-caption-2 text-gray_r-11'>
                        {currencyFormat(invoice.amountTotal)}
                      </p>
                    </div>
                  </div>
                  <ChevronRightIcon className='w-5 stroke-2' />
                </div>
              </Link>
            ))}
            {transaction.data?.invoices?.length === 0 && (
              <Alert
                type='info'
                className='text-center'
              >
                Belum ada Invoice
              </Alert>
            )}
          </div>
        </div>

        <Divider />

        <div className='p-4 pt-0'>
          {transaction.data?.status == 'draft' && (
            <button
              className='btn-yellow w-full mt-4'
              onClick={checkout}
            >
              Lanjutkan Transaksi
            </button>
          )}
          <button
            className='btn-light w-full mt-4'
            disabled={transaction.data?.status != 'draft'}
            onClick={downloadQuotation}
          >
            Download Quotation
          </button>
          {transaction.data?.status != 'draft' && (
            <button
              className='btn-light w-full mt-4'
              disabled={transaction.data?.status != 'waiting'}
              onClick={openCancelTransaction}
            >
              Batalkan Transaksi
            </button>
          )}
        </div>

        <BottomPopup
          active={cancelTransaction}
          close={closeCancelTransaction}
          title='Batalkan Transaksi'
        >
          <div className='leading-7 text-gray_r-12/80'>
            Apakah anda yakin membatalkan transaksi{' '}
            <span className='underline'>{transaction.data?.name}</span>?
          </div>
          <div className='flex mt-6 gap-x-4'>
            <button
              className='btn-solid-red flex-1'
              type='button'
              onClick={submitCancelTransaction}
            >
              Ya, Batalkan
            </button>
            <button
              className='btn-light flex-1'
              type='button'
              onClick={closeCancelTransaction}
            >
              Batal
            </button>
          </div>
        </BottomPopup>

        <BottomPopup
          title='Upload PO'
          close={closeUploadPo}
          active={uploadPo}
        >
          <div>
            <label>Nomor PO</label>
            <input
              type='text'
              className='form-input mt-3'
              ref={poNumber}
            />
          </div>
          <div className='mt-4'>
            <label>Dokumen PO</label>
            <input
              type='file'
              className='form-input mt-3 py-2'
              ref={poFile}
            />
          </div>
          <div className='grid grid-cols-2 gap-x-3 mt-6'>
            <button
              type='button'
              className='btn-light w-full'
              onClick={closeUploadPo}
            >
              Batal
            </button>
            <button
              type='button'
              className='btn-solid-red w-full'
              onClick={submitUploadPo}
            >
              Upload
            </button>
          </div>
        </BottomPopup>
      </>
    )
  )
}

const SectionAddress = ({ address }) => {
  const [section, setSection] = useState({
    customer: false,
    invoice: false,
    shipping: false
  })
  const toggleSection = (name) => {
    setSection({ ...section, [name]: !section[name] })
  }

  return (
    <>
      <SectionButton
        label='Detail Pelanggan'
        active={section.customer}
        toggle={() => toggleSection('customer')}
      />

      {section.customer && <SectionContent address={address?.customer} />}

      <Divider />

      <SectionButton
        label='Detail Pengiriman'
        active={section.shipping}
        toggle={() => toggleSection('shipping')}
      />

      {section.shipping && <SectionContent address={address?.shipping} />}

      <Divider />

      <SectionButton
        label='Detail Penagihan'
        active={section.invoice}
        toggle={() => toggleSection('invoice')}
      />
      {section.invoice && <SectionContent address={address?.invoice} />}
    </>
  )
}

const SectionButton = ({ label, active, toggle }) => (
  <button
    className='p-4 font-medium flex justify-between w-full'
    onClick={toggle}
  >
    <span>{label}</span>
    {active ? <ChevronUpIcon className='w-5' /> : <ChevronDownIcon className='w-5' />}
  </button>
)

const SectionContent = ({ address }) => {
  let fullAddress = []
  if (address?.street) fullAddress.push(address.street)
  if (address?.subDistrict?.name) fullAddress.push(address.subDistrict.name)
  if (address?.district?.name) fullAddress.push(address.district.name)
  if (address?.city?.name) fullAddress.push(address.city.name)
  fullAddress = fullAddress.join(', ')

  return (
    <div className='flex flex-col gap-y-4 p-4 border-t border-gray_r-6'>
      <DescriptionRow label='Nama'>{address.name}</DescriptionRow>
      <DescriptionRow label='Email'>{address.email || '-'}</DescriptionRow>
      <DescriptionRow label='No Telepon'>{address.mobile || '-'}</DescriptionRow>
      <DescriptionRow label='Alamat'>{fullAddress}</DescriptionRow>
    </div>
  )
}

const DescriptionRow = ({ children, label }) => (
  <div className='grid grid-cols-2'>
    <span className='text-gray_r-11'>{label}</span>
    <span className='text-right'>{children}</span>
  </div>
)

export default Transaction