You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
9.3 KiB
React

2 years ago
import React, { useEffect, useState } from 'react'
import { Head, Link, router, useForm } from '@inertiajs/react'
import { usePrevious } from 'react-use'
import { HiXCircle } from 'react-icons/hi'
2 years ago
2 years ago
import { dateToString, formatIDR } from '@/utils'
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'
import SearchInput from '@/Components/SearchInput'
import Button from '@/Components/Button'
import FormInputDate from '@/Components/FormInputDate'
import Pagination from '@/Components/Pagination'
import FormInput from '@/Components/FormInput'
import CustomerSelectionInput from '../Customer/SelectionInput'
import { Spinner } from 'flowbite-react'
import TextArea from '@/Components/TextArea'
2 years ago
export default function Sale(props) {
2 years ago
const {
_products: { data: products, links },
_page,
} = props
2 years ago
const [loading, setLoading] = useState(false)
const [search, setSearch] = useState('')
const preValue = usePrevious(search)
const { data, setData, post, processing, errors } = useForm({
date: dateToString(new Date()),
customer_id: null,
2 years ago
items: [],
note: '',
2 years ago
})
const addItem = (product) => {
2 years ago
const isExist = data.items.find((item) => item.id === product.id)
2 years ago
if (isExist) {
return
}
2 years ago
setData(
'items',
data.items.concat({
...product,
qty: 1,
})
)
2 years ago
}
const removeItem = (id) => {
2 years ago
setData(
'items',
data.items.filter((item) => item.id !== id)
)
2 years ago
}
const setQuantityItem = (id, qty) => {
2 years ago
setData(
'items',
data.items.map((item) => {
if (item.id === id) {
return {
...item,
qty: qty,
}
2 years ago
}
2 years ago
return item
})
)
2 years ago
}
const handleSubmit = () => {
post(route('sale.store'))
}
const params = { q: search }
2 years ago
useEffect(() => {
if (preValue) {
setLoading(true)
router.get(
route(route().current()),
{ q: search },
2 years ago
{
replace: true,
preserveState: true,
2 years ago
onSuccess: () => {
setLoading(false)
2 years ago
},
}
)
}
}, [search])
2 years ago
const total = data.items.reduce(
(amt, item) => amt + +item.qty * +item.price,
0
)
2 years ago
return (
<AuthenticatedLayout
auth={props.auth}
errors={props.errors}
flash={props.flash}
page={'Penjualan'}
action={'Transaksi'}
>
<Head title="Penjualan" />
<div className="mx-auto sm:px-6 lg:px-8 w-full">
<div className="flex flex-row p-6 shadow-sm sm:rounded-lg bg-white w-full space-x-2">
2 years ago
<div className="w-full md:w-7/12">
<div className="mb-4">
2 years ago
<SearchInput
2 years ago
onChange={(e) => setSearch(e.target.value)}
2 years ago
value={search}
/>
</div>
{loading ? (
2 years ago
<div className="w-full flex flex-row justify-center mt-28">
<Spinner size="xl" />
2 years ago
</div>
) : (
<div className="grid grid-cols-4 gap-2 text-center h-[320px]">
2 years ago
{products.map((item) => (
<div
className="rounded bg-gray-300 hover:bg-gray-200 shadow-lg px-4 py-2 flex flex-col justify-between"
2 years ago
key={item.id}
onClick={() => addItem(item)}
>
2 years ago
<div className="font-bold">
2 years ago
{item.name}
</div>
<div className="rounded-md bg-gray-800 p-0.5 text-white">
Rp. {formatIDR(item.price)}
</div>
</div>
))}
</div>
)}
2 years ago
<div className="w-full mt-4 justify-center">
<div className="mx-auto w-fit">
<Pagination links={links} params={params} />
2 years ago
</div>
</div>
<div className="w-full mt-4">
<TextArea
value={data.note}
onChange={(e) =>
setData('note', e.target.value)
}
rows={4}
placeholder="Catatan"
/>
</div>
2 years ago
</div>
2 years ago
<div className="w-full md:w-5/12 flex flex-col">
2 years ago
<CustomerSelectionInput
placeholder="Pelanggan"
itemSelected={data.customer_id}
onItemSelected={(id) => setData('customer_id', id)}
error={errors.customer_id}
/>
<FormInputDate
selected={data.date}
2 years ago
onChange={(date) => setData('date', date)}
placeholder="Tanggal"
2 years ago
error={errors.date}
/>
2 years ago
<div className="my-4 h-[350px] overflow-y-scroll">
<div className="flex flex-row justify-between space-x-2 space-y-2 rounded-md shadow items-center p-2">
<div className="w-2/3">Nama</div>
<div className="w-1/3">Jumlah</div>
<div className="">Subtotal</div>
<div className="text-transparent">
<div className="h-5 w-5"></div>
2 years ago
</div>
</div>
2 years ago
{data.items.map((item) => (
<div
className="flex flex-row justify-between space-x-2 space-y-2 rounded-md shadow items-center p-2"
2 years ago
key={item.id}
>
2 years ago
<div className="w-2/3">{item.name}</div>
<div className="w-1/3 text-right">
2 years ago
<FormInput
type="number"
min="1"
value={item.qty}
2 years ago
onChange={(e) =>
setQuantityItem(
item.id,
e.target.value
)
}
2 years ago
className="text-right"
/>
</div>
2 years ago
<div className="text-right w-1/3">
2 years ago
{formatIDR(item.qty * item.price)}
</div>
2 years ago
<div
className="text-red-500"
onClick={() => removeItem(item.id)}
>
<HiXCircle className="h-5 w-5" />
2 years ago
</div>
</div>
))}
</div>
2 years ago
<div className="w-full flex flex-row justify-between font-bold text-2xl">
2 years ago
<div>Total:</div>
<div>{formatIDR(total)}</div>
</div>
<Button
disable={processing}
2 years ago
onClick={(e) => handleSubmit()}
2 years ago
>
Simpan
</Button>
</div>
</div>
<div className="flex flex-row p-6 shadow-sm sm:rounded-lg bg-white w-full space-x-2 mt-2 invisible">
<div className="w-full">
<TextArea
value={data.note}
onChange={(e) => setData('note', e.target.value)}
rows={4}
label="Catatan"
/>
</div>
</div>
2 years ago
</div>
</AuthenticatedLayout>
2 years ago
)
}