create sale
parent
9dc36e08b2
commit
6aaeb37caf
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Customer::query();
|
||||
|
||||
if ($request->q) {
|
||||
$query->where('name', 'like', "%{$request->q}%");
|
||||
}
|
||||
|
||||
if ($request->except_id) {
|
||||
$query->where('id', '!=', $request->except_id);
|
||||
}
|
||||
|
||||
if ($request->all == 1) {
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\Sale;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SaleController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Sale::query()->with(['items.product.category', 'customer']);
|
||||
|
||||
if ($request->q) {
|
||||
$query->where('code', 'like', "%{$request->q}%");
|
||||
}
|
||||
|
||||
$query->orderBy('created_at', 'desc');
|
||||
|
||||
return inertia('Sale/Index', [
|
||||
'query' => $query->paginate(10),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
$products = Product::query()->orderBy('updated_at', 'desc');
|
||||
|
||||
if ($request->q != '') {
|
||||
$products->where('name', 'like', "%$request->q%");
|
||||
}
|
||||
|
||||
return inertia('Sale/Form', [
|
||||
'_products' => $products->paginate(16),
|
||||
'_page' => $request->page ?? 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'date' => 'required|date',
|
||||
'customer_id' => 'nullable|exists:customers,id',
|
||||
'items' => 'required|array',
|
||||
'items.*.id' => 'required|exists:products,id',
|
||||
'items.*.qty' => 'required|numeric'
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
$sale = Sale::create([
|
||||
'code' => Str::upper(Str::random(6)),
|
||||
'date' => $request->date,
|
||||
'customer_id' => $request->customer_id,
|
||||
'total' => collect($request->items)->sum(fn ($item) => $item['qty'] * $item['price'])
|
||||
]);
|
||||
|
||||
foreach($request->items as $item) {
|
||||
$sale->items()->create([
|
||||
"product_id" => $item['id'],
|
||||
"price" => $item['price'],
|
||||
"cost" => $item['cost'],
|
||||
"quantity" => $item['qty'],
|
||||
]);
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('sale.index')
|
||||
->with('message', ['type' => 'success', 'message' => 'Item has beed saved']);
|
||||
}
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
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';
|
||||
|
||||
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';
|
||||
|
||||
export default function Sale(props) {
|
||||
const { _products: { data: products, links}, _page } = props
|
||||
|
||||
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,
|
||||
items: []
|
||||
})
|
||||
|
||||
const addItem = (product) => {
|
||||
const isExist = data.items.find(item => item.id === product.id)
|
||||
if (isExist) {
|
||||
return
|
||||
}
|
||||
setData('items', data.items.concat({
|
||||
...product,
|
||||
qty: 1
|
||||
}))
|
||||
}
|
||||
|
||||
const removeItem = (id) => {
|
||||
setData('items', data.items.filter(item => item.id !== id))
|
||||
}
|
||||
|
||||
const setQuantityItem = (id, qty) => {
|
||||
setData('items', data.items.map(item => {
|
||||
if (item.id === id) {
|
||||
return {
|
||||
...item,
|
||||
qty: qty,
|
||||
}
|
||||
}
|
||||
return item
|
||||
}))
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
post(route('sale.store'))
|
||||
}
|
||||
|
||||
const params = { q: search, page: _page }
|
||||
useEffect(() => {
|
||||
if (preValue) {
|
||||
setLoading(true)
|
||||
router.get(
|
||||
route(route().current()),
|
||||
{ q: search, page: _page },
|
||||
{
|
||||
replace: true,
|
||||
preserveState: true,
|
||||
onSuccess: () => {
|
||||
setLoading(false)
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
}, [search])
|
||||
|
||||
console.log(data)
|
||||
const total = data.items.reduce((amt, item) => amt + (+item.qty * +item.price), 0)
|
||||
|
||||
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">
|
||||
<div className='w-full md:w-7/12'>
|
||||
<div className='mb-4'>
|
||||
<SearchInput
|
||||
onChange={e => setSearch(e.target.value)}
|
||||
value={search}
|
||||
/>
|
||||
</div>
|
||||
{loading ? (
|
||||
<div className='w-full flex flex-row justify-center mt-28'>
|
||||
<Spinner size="xl"/>
|
||||
</div>
|
||||
) : (
|
||||
<div className='grid grid-cols-4 gap-2 text-center'>
|
||||
{products.map(item => (
|
||||
<div
|
||||
className='rounded bg-gray-300 hover:bg-gray-200 shadow-lg px-4 py-2 flex flex-col justify-between'
|
||||
key={item.id}
|
||||
onClick={() => addItem(item)}
|
||||
>
|
||||
<div className='font-bold'>
|
||||
{item.name}
|
||||
</div>
|
||||
<div className="rounded-md bg-gray-800 p-0.5 text-white">
|
||||
Rp. {formatIDR(item.price)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className='w-full mt-4 justify-center'>
|
||||
<div className='mx-auto w-fit'>
|
||||
<Pagination links={links} params={params}/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className='w-full md:w-5/12 flex flex-col'>
|
||||
<CustomerSelectionInput
|
||||
placeholder="Pelanggan"
|
||||
itemSelected={data.customer_id}
|
||||
onItemSelected={(id) => setData('customer_id', id)}
|
||||
error={errors.customer_id}
|
||||
/>
|
||||
<FormInputDate
|
||||
selected={data.date}
|
||||
onChange={(date) => setData("date", date)}
|
||||
placeholder='Tanggal'
|
||||
error={errors.date}
|
||||
/>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
{data.items.map(item => (
|
||||
<div
|
||||
className='flex flex-row justify-between space-x-2 space-y-2 rounded-md shadow items-center p-2'
|
||||
key={item.id}
|
||||
>
|
||||
<div className='w-2/3'>
|
||||
{item.name}
|
||||
</div>
|
||||
<div className='w-1/3 text-right'>
|
||||
<FormInput
|
||||
type="number"
|
||||
min="1"
|
||||
value={item.qty}
|
||||
onChange={(e) => setQuantityItem(item.id, e.target.value)}
|
||||
className="text-right"
|
||||
/>
|
||||
</div>
|
||||
<div className='text-right w-1/3'>
|
||||
{formatIDR(item.qty * item.price)}
|
||||
</div>
|
||||
<div className='text-red-500' onClick={() => removeItem(item.id)}>
|
||||
<HiXCircle className='h-5 w-5'/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className='w-full flex flex-row justify-between font-bold text-2xl'>
|
||||
<div>Total:</div>
|
||||
<div>{formatIDR(total)}</div>
|
||||
</div>
|
||||
<Button
|
||||
disable={processing}
|
||||
onClick={e => handleSubmit()}
|
||||
>
|
||||
Simpan
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AuthenticatedLayout>
|
||||
);
|
||||
}
|
@ -0,0 +1,159 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { router } from '@inertiajs/react';
|
||||
import { usePrevious } from 'react-use';
|
||||
import { Head } from '@inertiajs/react';
|
||||
import { Button, Dropdown } from 'flowbite-react';
|
||||
import { HiEye, HiPencil, HiTrash } from 'react-icons/hi';
|
||||
import { useModalState } from '@/hooks';
|
||||
|
||||
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
|
||||
import Pagination from '@/Components/Pagination';
|
||||
import ModalConfirm from '@/Components/ModalConfirm';
|
||||
import SearchInput from '@/Components/SearchInput';
|
||||
import { formatDate, formatIDR, hasPermission } from '@/utils';
|
||||
|
||||
export default function Sale(props) {
|
||||
const { query: { links, data }, auth } = props
|
||||
|
||||
const [search, setSearch] = useState('')
|
||||
const preValue = usePrevious(search)
|
||||
|
||||
const confirmModal = useModalState()
|
||||
|
||||
const toggleFormModal = () => {
|
||||
router.get(route('sale.create'))
|
||||
}
|
||||
|
||||
const handleDeleteClick = (sale) => {
|
||||
confirmModal.setData(sale)
|
||||
confirmModal.toggle()
|
||||
}
|
||||
|
||||
const onDelete = () => {
|
||||
if(confirmModal.data !== null) {
|
||||
router.delete(route('sale.destroy', confirmModal.data.id))
|
||||
}
|
||||
}
|
||||
|
||||
const params = { q: search }
|
||||
useEffect(() => {
|
||||
if (preValue) {
|
||||
router.get(
|
||||
route(route().current()),
|
||||
{ q: search },
|
||||
{
|
||||
replace: true,
|
||||
preserveState: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
}, [search])
|
||||
|
||||
const canCreate = hasPermission(auth, 'create-sale')
|
||||
const canUpdate = hasPermission(auth, 'update-sale')
|
||||
const canDelete = hasPermission(auth, 'delete-sale')
|
||||
|
||||
return (
|
||||
<AuthenticatedLayout
|
||||
auth={props.auth}
|
||||
errors={props.errors}
|
||||
flash={props.flash}
|
||||
page={'Penjualan'}
|
||||
action={''}
|
||||
>
|
||||
<Head title="Penjualan" />
|
||||
|
||||
<div>
|
||||
<div className="mx-auto sm:px-6 lg:px-8 ">
|
||||
<div className="p-6 overflow-hidden shadow-sm sm:rounded-lg bg-gray-200 dark:bg-gray-800 space-y-4">
|
||||
<div className='flex justify-between'>
|
||||
{canCreate && (
|
||||
<Button size="sm" onClick={() => toggleFormModal()}>Tambah</Button>
|
||||
)}
|
||||
<div className="flex items-center">
|
||||
<SearchInput
|
||||
onChange={e => setSearch(e.target.value)}
|
||||
value={search}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className='overflow-auto'>
|
||||
<div>
|
||||
<table className="w-full text-sm text-left text-gray-500 dark:text-gray-400 mb-4">
|
||||
<thead className="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
|
||||
<tr>
|
||||
<th scope="col" className="py-3 px-6">
|
||||
Kode
|
||||
</th>
|
||||
<th scope="col" className="py-3 px-6">
|
||||
Tanggal
|
||||
</th>
|
||||
<th scope="col" className="py-3 px-6">
|
||||
Pelanggan
|
||||
</th>
|
||||
<th scope="col" className="py-3 px-6">
|
||||
Total
|
||||
</th>
|
||||
<th scope="col" className="py-3 px-6"/>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.map(sale => (
|
||||
<tr className="bg-white border-b dark:bg-gray-800 dark:border-gray-700" key={sale.id}>
|
||||
<td scope="row" className="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
||||
{sale.code}
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
{formatDate(sale.date)}
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
{sale.customer?.name}
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
{formatIDR(sale.total)}
|
||||
</td>
|
||||
<td className="py-4 px-6 flex justify-end">
|
||||
<Dropdown
|
||||
label={"Opsi"}
|
||||
floatingArrow={true}
|
||||
arrowIcon={true}
|
||||
dismissOnClick={true}
|
||||
size={'sm'}
|
||||
>
|
||||
{canUpdate && (
|
||||
<Dropdown.Item onClick={() => toggleFormModal(sale)}>
|
||||
<div className='flex space-x-1 items-center'>
|
||||
<HiEye/>
|
||||
<div>Detail</div>
|
||||
</div>
|
||||
</Dropdown.Item>
|
||||
)}
|
||||
{canDelete && (
|
||||
<Dropdown.Item onClick={() => handleDeleteClick(sale)}>
|
||||
<div className='flex space-x-1 items-center'>
|
||||
<HiTrash/>
|
||||
<div>Hapus</div>
|
||||
</div>
|
||||
</Dropdown.Item>
|
||||
)}
|
||||
</Dropdown>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div className='w-full flex items-center justify-center'>
|
||||
<Pagination links={links} params={params}/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ModalConfirm
|
||||
modalState={confirmModal}
|
||||
onConfirm={onDelete}
|
||||
/>
|
||||
</AuthenticatedLayout>
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue