-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Menambahkan fungsi untuk tambah dan sunting pembelian barang (#74)
* Restruktur data dan menambahkan tipe `ProductMovement` * Menyempurnakan komponen `<ConfirmationModal />` - Menambah warna `default` - Menyembunyikan tombol tutup (`hideCloseButton`) - Menonaktifkan `isDismissable` * Menambahkan variabel env `NEXT_PUBLIC_APP_NAME` * Mendaftarkan file `.env` pada `.gitignore` * Menambahkan navigasi ke halaman pembelian * Menambahakan halaman daftar pembelian * Menambahkan fungsi untuk tambah dan sunting pembelian barang
- Loading branch information
1 parent
4bf8d31
commit 9684d7a
Showing
28 changed files
with
496 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
NEXT_PUBLIC_APP_NAME="Sensasi POS" | ||
SENTRY_AUTH_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
type Base64Blob = string | ||
|
||
export default Base64Blob | ||
export type Base64Blob = string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ISODate = string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/(authenticated user)/data/products/_components/product-form/_props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/app/(authenticated user)/purchases/(form)/[id]/page-hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use client' | ||
|
||
import type { ProductMovement } from '@/models/table-types/product-movement' | ||
import type { FormValues } from '../_types/form-values' | ||
|
||
import db from '@/models/db' | ||
import { useEffect, useState } from 'react' | ||
import { useRouter } from 'next/navigation' | ||
import { updateStocksOnReceived } from '../_functions/update-stocks-on-received' | ||
import { validateFormValues } from '../_functions/validate-form-values' | ||
|
||
export function usePageHook(idParam: string) { | ||
const router = useRouter() | ||
|
||
const [formValues, setFormValues] = useState<FormValues>() | ||
|
||
useEffect(() => { | ||
if (isNaN(parseInt(idParam))) { | ||
throw new Error('Invalid ID') | ||
} | ||
|
||
getData(parseInt(idParam)) | ||
.then(setFormValues) | ||
.catch(error => { | ||
throw error | ||
}) | ||
|
||
return () => { | ||
setFormValues(undefined) | ||
} | ||
}, [idParam]) | ||
|
||
return { | ||
formValues, | ||
|
||
handleCancel: () => router.back(), | ||
|
||
handleSubmit: () => { | ||
if (!formValues) return | ||
|
||
const validated = validateFormValues(formValues) | ||
|
||
db.productMovements | ||
.update(validated.id, { | ||
...validated, | ||
updated_at: new Date().toISOString(), | ||
}) | ||
.then(() => { | ||
if (validated.type !== 'purchase') { | ||
throw new Error('Invalid type') | ||
} | ||
|
||
if (validated.additional_info.received_at) { | ||
updateStocksOnReceived(validated) | ||
} | ||
}) | ||
.then(() => router.back()) | ||
.catch(error => { | ||
throw error | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
async function getData(id: number) { | ||
return new Promise<ProductMovement>((resolve, reject) => { | ||
db.productMovements | ||
.get(id) | ||
.then(data => { | ||
if (!data) { | ||
reject(new Error('not found')) | ||
return | ||
} | ||
|
||
if (data.type !== 'purchase') { | ||
reject(new Error('not found')) | ||
return | ||
} | ||
|
||
if (data.additional_info.received_at ?? data.additional_info.paid_at) { | ||
reject( | ||
new Error( | ||
'Tidak dapat mengubah data yang sudah diterima atau dibayar', | ||
), | ||
) | ||
} | ||
|
||
resolve(data) | ||
}) | ||
.catch(reject) | ||
}) | ||
} |
44 changes: 44 additions & 0 deletions
44
src/app/(authenticated user)/purchases/(form)/[id]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use client' | ||
|
||
import { | ||
Button, | ||
Card, | ||
CardBody, | ||
CardFooter, | ||
CardHeader, | ||
} from '@nextui-org/react' | ||
import { usePageHook } from './page-hook' | ||
|
||
const FORM_ID = 'purchase-form' | ||
|
||
export default function PurchaseFormPage({ | ||
params: { id }, | ||
}: { | ||
params: { id: string } | ||
}) { | ||
const { handleSubmit, handleCancel, formValues } = usePageHook(id) | ||
|
||
return ( | ||
<Card> | ||
<CardHeader>Sunting Pembelian — NO. {formValues?.id}</CardHeader> | ||
|
||
<CardBody> | ||
<form | ||
id={FORM_ID} | ||
onSubmit={() => { | ||
handleSubmit() | ||
}}></form> | ||
</CardBody> | ||
|
||
<CardFooter> | ||
<Button variant="light" color="primary" onClick={handleCancel}> | ||
Batal | ||
</Button> | ||
|
||
<Button color="primary" form={FORM_ID} type="submit"> | ||
Simpan | ||
</Button> | ||
</CardFooter> | ||
</Card> | ||
) | ||
} |
37 changes: 37 additions & 0 deletions
37
src/app/(authenticated user)/purchases/(form)/_functions/update-stocks-on-received.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import db from '@/models/db' | ||
import { ProductMovement } from '@/models/table-types/product-movement' | ||
|
||
export function updateStocksOnReceived(productMovement: ProductMovement) { | ||
productMovement.items.forEach(item => { | ||
db.products | ||
.update(item.product_state.id, product => { | ||
const inQty = item.qty | ||
const inWorth = item.qty * item.price | ||
|
||
const existsStock = product.stocks.find( | ||
stock => stock.warehouse_id === productMovement.warehouse_state.id, | ||
) | ||
|
||
const existsQty = existsStock?.qty ?? 0 | ||
const existsWorth = existsStock?.cost ?? 0 | ||
|
||
const newQty = existsQty + inQty | ||
const newWorth = existsWorth + inWorth | ||
const newCost = newWorth / newQty | ||
|
||
if (existsStock) { | ||
existsStock.qty = newQty | ||
existsStock.cost = newCost | ||
} else { | ||
product.stocks.push({ | ||
warehouse_id: productMovement.warehouse_state.id, | ||
qty: newQty, | ||
cost: newCost, | ||
}) | ||
} | ||
}) | ||
.catch(error => { | ||
throw error | ||
}) | ||
}) | ||
} |
6 changes: 6 additions & 0 deletions
6
src/app/(authenticated user)/purchases/(form)/_functions/validate-form-values.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { ProductMovement } from '@/models/table-types/product-movement' | ||
import type { FormValues } from '../_types/form-values' | ||
|
||
export function validateFormValues(formValues: FormValues) { | ||
return formValues as ProductMovement | ||
} |
3 changes: 3 additions & 0 deletions
3
src/app/(authenticated user)/purchases/(form)/_types/form-values.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { ProductMovement } from '@/models/table-types/product-movement' | ||
|
||
export type FormValues = Partial<ProductMovement> |
Oops, something went wrong.