Skip to content

Commit

Permalink
Restruktur data dan menambahkan tipe ProductMovement
Browse files Browse the repository at this point in the history
  • Loading branch information
sensasi-delight committed Sep 13, 2024
1 parent 4bf8d31 commit 15d98a8
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 43 deletions.
4 changes: 1 addition & 3 deletions src/@types/base-64-blob.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
type Base64Blob = string

export default Base64Blob
export type Base64Blob = string
1 change: 1 addition & 0 deletions src/@types/iso-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ISODate = string
2 changes: 1 addition & 1 deletion src/app/(authenticated user)/data/products/[id]/_hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// types
import type Product from '@/models/table-types/product'
import type { Product } from '@/models/table-types/product'
import type { ProductFormProps } from '../_components/product-form/_props'
// vendors
import { useRouter } from 'next/navigation'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

// types
import type Product from '@/models/table-types/product'
import type { Product } from '@/models/table-types/product'
// vendors
import {
Card,
Expand All @@ -23,16 +23,7 @@ import PageUrlEnum from '@/enums/page-url'
import { useHook } from './_hook'

export function ProductCard({
data: {
id,
name,
base_cost,
default_price,
qty_unit,
qty,
category,
image_file,
},
data: { id, name, default_price, qty_unit, category, image_file, stocks },
className,
as,
}: {
Expand All @@ -43,6 +34,12 @@ export function ProductCard({
}) {
const { handleOpenDeleteModal, deleteConfirmationModal } = useHook(id)

const totalStock = stocks.reduce((acc, { qty }) => acc + qty, 0)
const cost =
totalStock === 0
? 0
: stocks.reduce((acc, { qty, cost }) => acc + cost * qty, 0) / totalStock

return (
<Card
as={as}
Expand Down Expand Up @@ -76,14 +73,15 @@ export function ProductCard({
<Chip size="sm">{category ?? 'Tanpa Kategori'}</Chip>
<span></span>
<span>
{formatNumber(qty)} {qty_unit}
{formatNumber(totalStock)}
{qty_unit}
</span>
</div>
</div>

<div className="flex flex-col justify-center col-span-3 md:col-span-2">
<h3 className="text-sm">HPP</h3>
<p className="text-xl">{formatNumber(base_cost)}</p>
<p className="text-xl">{formatNumber(cost)}</p>
</div>

<div className="flex flex-col justify-center items-center col-span-3 md:col-span-2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Product from '@/models/table-types/product'
import type { Product } from '@/models/table-types/product'
import { type FormEvent, useState } from 'react'
import type { ProductFormProps } from './_props'
import { useDebouncedCallback } from 'use-debounce'
Expand Down Expand Up @@ -53,14 +53,6 @@ export function useHook(
formValues.code = formValues.id.toString()
}

if (!formValues.base_cost) {
formValues.base_cost = 0
}

if (!formValues.qty) {
formValues.qty = 0
}

onSubmit?.(formValues)
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Product from '@/models/table-types/product'
import type { Product } from '@/models/table-types/product'

export interface ProductFormProps {
id: HTMLFormElement['id']
Expand Down
2 changes: 1 addition & 1 deletion src/components/image-input/image-input.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import Base64Blob from '@/@types/base-64-blob'
import { Base64Blob } from '@/@types/base-64-blob'
import resizeImage from '@/components/image-input/functions/resize-image'
import { Button, Image, Tooltip } from '@nextui-org/react'
import {
Expand Down
6 changes: 6 additions & 0 deletions src/enums/page-url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
enum PageUrlEnum {
HOME = '/',

PURCHASE_LIST = '/purchases',
PURCHASE_CREATE = '/purchases/create',
PURCHASE_EDIT = '/purchases/:id',

APP_SETTING_PAGE_URL = '/settings',

PRODUCT_LIST = '/data/products',
PRODUCT_CREATE = '/data/products/create',
PRODUCT_EDIT = '/data/products/:id',

WAREHOUSE_LIST = '/data/warehouses',

USER_LIST = '/data/users',

BACKUPS = '/data/backups',
Expand Down
12 changes: 9 additions & 3 deletions src/models/db.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Dexie, { type EntityTable } from 'dexie'
import type Product from './table-types/product'
import type { Product } from './table-types/product'
import type { Warehouse } from './table-types/warehouse'
import type { ProductMovement } from './table-types/product-movement'

interface Tables {
warehouses: EntityTable<Warehouse, 'id'>
products: EntityTable<Product, 'id'>
productMovements: EntityTable<ProductMovement, 'uuid'>
}

const db = new Dexie('sensasi-pos-db') as Dexie & Tables
Expand All @@ -11,8 +15,10 @@ const db = new Dexie('sensasi-pos-db') as Dexie & Tables
* @see https://dexie.org/docs/Version/Version.stores()
*/
db.version(1).stores({
products:
'++id, &code, &barcode_reg_id, &name, description, category, created_at, updated_at, deleted_at',
warehouses: '++id, &name, note',
products: '++id, &code, &barcode_reg_id, &name, description, category',
productMovements:
'&uuid, at, type, note, warehouse_state, details.product_state',
})

export default db
35 changes: 35 additions & 0 deletions src/models/table-types/product-movement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { UUID } from 'crypto'
import type { Warehouse } from './warehouse'
import type { Product } from './product'
import type { Base64Blob } from '@/@types/base-64-blob'
import type { ISODate } from '@/@types/iso-date'

export interface ProductMovement {
uuid: Readonly<UUID>

at: ISODate
type: 'sale' | 'purchase' | 'return' | 'adjustment'
note?: string

warehouse_state: Readonly<Warehouse>

details: {
product_state: Readonly<Product>

qty: number
price: number
}[]

additional_costs: {
name: string
value: number
}[]

created_at: ISODate
updated_at: ISODate

files: {
blob: Base64Blob
description?: string
}[]
}
33 changes: 21 additions & 12 deletions src/models/table-types/product.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import type Base64Blob from '@/@types/base-64-blob'
import type { Base64Blob } from '@/@types/base-64-blob'
import type { ISODate } from '@/@types/iso-date'
import type { Warehouse } from './warehouse'

export interface Product {
id: Readonly<number>

interface Product {
id: number
code?: string
barcode_reg_id?: string

name: string
description?: string
category?: string

qty: number
image_file?: Base64Blob

created_at: ISODate
updated_at: ISODate
deleted_at?: ISODate

qty_unit: string
default_price: number // per unit

base_cost: number
default_price: number
low_qty_threshold?: number

image_file?: Base64Blob
stocks: {
warehouse_id: Warehouse['id']

created_at: string
updated_at: string
deleted_at?: string
qty: number
cost: number // per unit
default_price?: number // use parent product.default_price_per_unit if not set
low_qty_threshold?: number // use parent product.low_qty_threshold if not set
}[]
}

export default Product
11 changes: 11 additions & 0 deletions src/models/table-types/warehouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { ISODate } from '@/@types/iso-date'

export interface Warehouse {
id: Readonly<number>
name: string
note?: string

created_at: ISODate
updated_at: ISODate
deleted_at?: ISODate
}

0 comments on commit 15d98a8

Please sign in to comment.