feat: implement flexible payment processing with partial cash payments and automatic balance deduction in reports
This commit is contained in:
@@ -52,6 +52,7 @@ model Submission {
|
|||||||
order_id String
|
order_id String
|
||||||
user_id String
|
user_id String
|
||||||
bill Int?
|
bill Int?
|
||||||
|
paid_amount Int?
|
||||||
payment_status String @default("BELUM_BAYAR")
|
payment_status String @default("BELUM_BAYAR")
|
||||||
order Order @relation(fields: [order_id], references: [id], onDelete: Cascade)
|
order Order @relation(fields: [order_id], references: [id], onDelete: Cascade)
|
||||||
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
|
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
|
||||||
|
|||||||
@@ -0,0 +1,176 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { getBalancesAsCreator, getBalancesAsSubmittor, getSessionUser } from '@/app/actions'
|
||||||
|
import { Card, CardContent } from '@/components/ui/card'
|
||||||
|
import { Wallet, ArrowDownRight, ArrowUpRight, Loader2 } from 'lucide-react'
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
|
export default function BalancesPage() {
|
||||||
|
const [activeTab, setActiveTab] = useState<'SUBMITTOR' | 'CREATOR'>('SUBMITTOR')
|
||||||
|
const [userId, setUserId] = useState<string | null>(null)
|
||||||
|
const [submittorBalances, setSubmittorBalances] = useState<any[]>([])
|
||||||
|
const [creatorBalances, setCreatorBalances] = useState<any[]>([])
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const init = async () => {
|
||||||
|
const user = await getSessionUser()
|
||||||
|
if (user?.id) {
|
||||||
|
setUserId(user.id)
|
||||||
|
loadData(user.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
init()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const loadData = async (id: string) => {
|
||||||
|
setLoading(true)
|
||||||
|
const [subRes, creRes] = await Promise.all([
|
||||||
|
getBalancesAsSubmittor(id),
|
||||||
|
getBalancesAsCreator(id)
|
||||||
|
])
|
||||||
|
setSubmittorBalances(subRes)
|
||||||
|
setCreatorBalances(creRes)
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatRupiah = (n: number) =>
|
||||||
|
new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(n)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6 animate-in fade-in duration-500 pb-12">
|
||||||
|
<div>
|
||||||
|
<span className="text-xs font-semibold text-slate-400">Keuangan / Saldo</span>
|
||||||
|
<h2 className="text-xl font-black text-slate-900 dark:text-white flex items-center gap-2 tracking-tight">
|
||||||
|
<Wallet className="w-6 h-6 text-[#1B2CC1]" />
|
||||||
|
Buku Saldo
|
||||||
|
</h2>
|
||||||
|
<p className="text-sm text-slate-500 mt-1">
|
||||||
|
Pantau riwayat saldo lebih atau kurang dari transaksi pesanan.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex bg-slate-100 dark:bg-slate-900 p-1 rounded-xl w-full max-w-sm border border-slate-200 dark:border-slate-800">
|
||||||
|
<button
|
||||||
|
onClick={() => setActiveTab('SUBMITTOR')}
|
||||||
|
className={cn(
|
||||||
|
"flex-1 text-xs font-bold py-2 rounded-lg transition-all",
|
||||||
|
activeTab === 'SUBMITTOR'
|
||||||
|
? "bg-white dark:bg-slate-800 shadow-sm text-slate-900 dark:text-white"
|
||||||
|
: "text-slate-500 hover:text-slate-700 dark:hover:text-slate-300"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Saldo Saya (Penitip)
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setActiveTab('CREATOR')}
|
||||||
|
className={cn(
|
||||||
|
"flex-1 text-xs font-bold py-2 rounded-lg transition-all",
|
||||||
|
activeTab === 'CREATOR'
|
||||||
|
? "bg-white dark:bg-slate-800 shadow-sm text-slate-900 dark:text-white"
|
||||||
|
: "text-slate-500 hover:text-slate-700 dark:hover:text-slate-300"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Saldo Orang (Kreator)
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{loading ? (
|
||||||
|
<div className="flex flex-col items-center justify-center min-h-[30vh] gap-3">
|
||||||
|
<Loader2 className="animate-spin text-[#1B2CC1] w-8 h-8" />
|
||||||
|
<span className="text-xs text-slate-500 font-semibold">Memuat saldo...</span>
|
||||||
|
</div>
|
||||||
|
) : activeTab === 'SUBMITTOR' ? (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="bg-blue-50 dark:bg-blue-950/30 p-4 rounded-xl border border-blue-100 dark:border-blue-900/50 mb-6">
|
||||||
|
<h3 className="font-bold text-blue-900 dark:text-blue-100 text-sm mb-1">Saldo Anda di Kreator Lain</h3>
|
||||||
|
<p className="text-xs text-blue-700 dark:text-blue-300 leading-relaxed">
|
||||||
|
Jika saldo <strong>positif</strong> (hijau), Anda memiliki deposit yang bisa digunakan untuk pesanan berikutnya di kreator tersebut. Jika <strong>negatif</strong> (merah), Anda berhutang.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{submittorBalances.length === 0 ? (
|
||||||
|
<div className="text-center p-12 rounded-3xl border-2 border-dashed border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm">
|
||||||
|
<p className="text-xs text-slate-500 font-medium">Belum ada catatan saldo.</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
|
||||||
|
{submittorBalances.map((b, i) => (
|
||||||
|
<Card key={i} className="rounded-2xl border-slate-200/90 dark:border-slate-800 overflow-hidden hover:border-[#1B2CC1]/30 transition-all shadow-sm">
|
||||||
|
<div className="p-4 flex items-center gap-3 bg-slate-50/50 dark:bg-slate-800/40 border-b border-slate-100 dark:border-slate-800">
|
||||||
|
{b.creator.photo ? (
|
||||||
|
<img src={b.creator.photo} alt={b.creator.name} className="w-10 h-10 rounded-full object-cover ring-2 ring-white dark:ring-slate-900 shadow-sm" />
|
||||||
|
) : (
|
||||||
|
<div className="w-10 h-10 rounded-full bg-[#1B2CC1]/10 text-[#1B2CC1] flex items-center justify-center font-bold text-sm ring-2 ring-white dark:ring-slate-900 shadow-sm">
|
||||||
|
{b.creator.name.charAt(0).toUpperCase()}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<p className="font-bold text-slate-900 dark:text-white text-sm">{b.creator.name}</p>
|
||||||
|
<p className="text-[10px] font-semibold text-slate-500 uppercase tracking-wider">Kreator</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<CardContent className="p-4 bg-white dark:bg-slate-900">
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<span className="text-[11px] font-bold text-slate-400 uppercase tracking-wider">Total Saldo</span>
|
||||||
|
<div className={cn("text-xl font-black flex items-center gap-1.5", b.amount > 0 ? "text-emerald-600" : "text-rose-600")}>
|
||||||
|
{b.amount > 0 ? <ArrowUpRight className="w-5 h-5" /> : <ArrowDownRight className="w-5 h-5" />}
|
||||||
|
{formatRupiah(b.amount)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="bg-amber-50 dark:bg-amber-950/30 p-4 rounded-xl border border-amber-100 dark:border-amber-900/50 mb-6">
|
||||||
|
<h3 className="font-bold text-amber-900 dark:text-amber-100 text-sm mb-1">Saldo Orang Lain di Anda</h3>
|
||||||
|
<p className="text-xs text-amber-700 dark:text-amber-300 leading-relaxed">
|
||||||
|
Jika saldo <strong>positif</strong> (merah bagi Anda), artinya Anda memegang uang lebih milik penitip (Hutang Anda ke mereka). Jika <strong>negatif</strong> (hijau), mereka berhutang ke Anda.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{creatorBalances.length === 0 ? (
|
||||||
|
<div className="text-center p-12 rounded-3xl border-2 border-dashed border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm">
|
||||||
|
<p className="text-xs text-slate-500 font-medium">Belum ada penitip yang memiliki catatan saldo dengan Anda.</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
|
||||||
|
{creatorBalances.map((b, i) => (
|
||||||
|
<Card key={i} className="rounded-2xl border-slate-200/90 dark:border-slate-800 overflow-hidden hover:border-[#1B2CC1]/30 transition-all shadow-sm">
|
||||||
|
<div className="p-4 flex items-center gap-3 bg-slate-50/50 dark:bg-slate-800/40 border-b border-slate-100 dark:border-slate-800">
|
||||||
|
{b.user.photo ? (
|
||||||
|
<img src={b.user.photo} alt={b.user.name} className="w-10 h-10 rounded-full object-cover ring-2 ring-white dark:ring-slate-900 shadow-sm" />
|
||||||
|
) : (
|
||||||
|
<div className="w-10 h-10 rounded-full bg-slate-200 dark:bg-slate-700 text-slate-600 dark:text-slate-300 flex items-center justify-center font-bold text-sm ring-2 ring-white dark:ring-slate-900 shadow-sm">
|
||||||
|
{b.user.name.charAt(0).toUpperCase()}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
<p className="font-bold text-slate-900 dark:text-white text-sm">{b.user.name}</p>
|
||||||
|
<p className="text-[10px] font-semibold text-slate-500 uppercase tracking-wider">Penitip</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<CardContent className="p-4 bg-white dark:bg-slate-900">
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<span className="text-[11px] font-bold text-slate-400 uppercase tracking-wider">Saldo Penitip</span>
|
||||||
|
{/* From Creator perspective: positive balance of submittor is bad for creator (creator holds their money) */}
|
||||||
|
<div className={cn("text-xl font-black flex items-center gap-1.5", b.amount > 0 ? "text-rose-600" : "text-emerald-600")}>
|
||||||
|
{b.amount > 0 ? <ArrowDownRight className="w-5 h-5" /> : <ArrowUpRight className="w-5 h-5" />}
|
||||||
|
{formatRupiah(b.amount)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useParams, useRouter } from 'next/navigation'
|
import { useParams, useRouter } from 'next/navigation'
|
||||||
import { getOrderDetail, updateSubmissionPayment, updateOrderStatus, updateOrder, deleteOrder, getSessionUser } from '@/app/actions'
|
import { getOrderDetail, updateSubmissionPayment, updateOrderStatus, updateOrder, deleteOrder, getSessionUser, getBalancesAsCreator } from '@/app/actions'
|
||||||
import { Card, CardContent } from '@/components/ui/card'
|
import { Card, CardContent } from '@/components/ui/card'
|
||||||
import { Button, buttonVariants } from '@/components/ui/button'
|
import { Button, buttonVariants } from '@/components/ui/button'
|
||||||
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
|
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
|
||||||
@@ -11,7 +11,7 @@ import { Input } from '@/components/ui/input'
|
|||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { Checkbox } from '@/components/ui/checkbox'
|
import { Checkbox } from '@/components/ui/checkbox'
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||||
import { format } from 'date-fns'
|
import { format, isToday } from 'date-fns'
|
||||||
import {
|
import {
|
||||||
Copy,
|
Copy,
|
||||||
Loader2,
|
Loader2,
|
||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
ToggleLeft,
|
ToggleLeft,
|
||||||
AlertCircle,
|
AlertCircle,
|
||||||
Pencil,
|
Pencil,
|
||||||
|
ShoppingBag,
|
||||||
PlusCircle,
|
PlusCircle,
|
||||||
MinusCircle,
|
MinusCircle,
|
||||||
Trash2,
|
Trash2,
|
||||||
@@ -38,6 +39,7 @@ export default function OrderDetailPage() {
|
|||||||
const [userId, setUserId] = useState<string | null>(null)
|
const [userId, setUserId] = useState<string | null>(null)
|
||||||
const [order, setOrder] = useState<any>(null)
|
const [order, setOrder] = useState<any>(null)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [balances, setBalances] = useState<any[]>([])
|
||||||
const [copiedPerson, setCopiedPerson] = useState(false)
|
const [copiedPerson, setCopiedPerson] = useState(false)
|
||||||
const [copiedItem, setCopiedItem] = useState(false)
|
const [copiedItem, setCopiedItem] = useState(false)
|
||||||
const [updatingStatus, setUpdatingStatus] = useState(false)
|
const [updatingStatus, setUpdatingStatus] = useState(false)
|
||||||
@@ -52,15 +54,22 @@ export default function OrderDetailPage() {
|
|||||||
if (user?.id) {
|
if (user?.id) {
|
||||||
setUserId(user.id)
|
setUserId(user.id)
|
||||||
}
|
}
|
||||||
if (orderId) loadOrder()
|
if (orderId) loadOrder(user?.id)
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [orderId])
|
}, [orderId])
|
||||||
|
|
||||||
const loadOrder = async () => {
|
const loadOrder = async (currentUserId?: string) => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const data = await getOrderDetail(orderId)
|
const data = await getOrderDetail(orderId)
|
||||||
setOrder(data)
|
setOrder(data)
|
||||||
|
|
||||||
|
const idToUse = currentUserId || userId
|
||||||
|
if (data && idToUse === data.creator_id) {
|
||||||
|
const bals = await getBalancesAsCreator(idToUse)
|
||||||
|
setBalances(bals)
|
||||||
|
}
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,46 +239,52 @@ export default function OrderDetailPage() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Hero Overview Card */}
|
{/* Hero Overview Card */}
|
||||||
<Card className="rounded-3xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden">
|
<Card className="rounded-2xl border border-slate-200/80 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden mb-6">
|
||||||
<div className="p-6 sm:p-8 flex flex-col md:flex-row justify-between items-start md:items-center gap-4 border-b border-slate-100 dark:border-slate-800">
|
<div className="p-5 sm:p-6 flex flex-col md:flex-row justify-between items-start md:items-center gap-5 border-b border-slate-100 dark:border-slate-800">
|
||||||
<div className="space-y-2">
|
<div className="space-y-1.5 w-full md:w-auto">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2 mb-2">
|
||||||
<span className={cn(
|
<span className={cn(
|
||||||
"inline-flex items-center px-3 py-0.5 rounded-full text-xs font-black tracking-wide",
|
"inline-flex items-center px-2.5 py-0.5 rounded-md text-[10px] font-black tracking-wider uppercase",
|
||||||
order.status === 'OPEN' ? 'bg-emerald-50 text-emerald-700 border border-emerald-200 dark:bg-emerald-950/50 dark:text-emerald-400' :
|
order.status === 'OPEN' ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-950/60 dark:text-emerald-400' :
|
||||||
order.status === 'CLOSE' ? 'bg-rose-50 text-rose-700 border border-rose-200 dark:bg-rose-950/50 dark:text-rose-400' :
|
order.status === 'CLOSE' ? 'bg-rose-100 text-rose-700 dark:bg-rose-950/60 dark:text-rose-400' :
|
||||||
'bg-slate-100 text-slate-700 border border-slate-200 dark:bg-slate-800 dark:text-slate-300'
|
'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300'
|
||||||
)}>
|
)}>
|
||||||
● {order.status}
|
{order.status}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-xs text-slate-400 font-medium">
|
<span className="text-xs text-slate-400 font-medium">
|
||||||
{format(new Date(order.date), 'dd MMMM yyyy')}
|
{format(new Date(order.date), 'dd MMM yyyy')}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<h1 className="text-2xl sm:text-3xl font-black text-slate-900 dark:text-white tracking-tight">
|
<h1 className="text-xl sm:text-2xl font-black text-slate-900 dark:text-white tracking-tight leading-tight">
|
||||||
{order.title}
|
{order.title}
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-xs text-slate-500 font-medium">
|
<p className="text-xs text-slate-500 font-medium pt-0.5">
|
||||||
Dibuat oleh: <span className="text-slate-800 dark:text-slate-200 font-bold">{order.creator.name}</span>
|
Oleh <span className="text-slate-800 dark:text-slate-200 font-bold">{order.creator.name}</span>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-4 bg-slate-50 dark:bg-slate-800/50 p-4 rounded-2xl border border-slate-200/80 dark:border-slate-700/60">
|
<div className="flex w-full md:w-auto items-center gap-3">
|
||||||
<div className="text-center px-2">
|
<div className="flex-1 md:flex-none flex items-center justify-between gap-3 bg-slate-50 dark:bg-slate-800/40 p-3 rounded-xl border border-slate-100 dark:border-slate-700/50 min-w-[120px]">
|
||||||
<span className="text-[10px] uppercase font-bold text-slate-400 block">Total Pemesan</span>
|
<div className="flex flex-col">
|
||||||
<span className="text-2xl font-black text-[#1B2CC1] dark:text-blue-400">{order.submissions.length}</span>
|
<span className="text-[10px] font-bold text-slate-400 uppercase tracking-wider">Pemesan</span>
|
||||||
|
<span className="text-lg font-black text-[#1B2CC1] dark:text-blue-400 leading-none mt-1">{order.submissions.length}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="h-8 w-px bg-slate-200 dark:bg-slate-700" />
|
<Users className="w-5 h-5 text-slate-300 dark:text-slate-600" />
|
||||||
<div className="text-center px-2">
|
</div>
|
||||||
<span className="text-[10px] uppercase font-bold text-slate-400 block">Total Menu</span>
|
|
||||||
<span className="text-2xl font-black text-slate-800 dark:text-white">{order.available_items.length}</span>
|
<div className="flex-1 md:flex-none flex items-center justify-between gap-3 bg-slate-50 dark:bg-slate-800/40 p-3 rounded-xl border border-slate-100 dark:border-slate-700/50 min-w-[120px]">
|
||||||
|
<div className="flex flex-col">
|
||||||
|
<span className="text-[10px] font-bold text-slate-400 uppercase tracking-wider">Menu</span>
|
||||||
|
<span className="text-lg font-black text-slate-700 dark:text-slate-300 leading-none mt-1">{order.available_items.length}</span>
|
||||||
|
</div>
|
||||||
|
<ShoppingBag className="w-5 h-5 text-slate-300 dark:text-slate-600" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Creator Control Toolbar (Status, Edit, Delete) */}
|
{/* Creator Control Toolbar (Status, Edit, Delete) */}
|
||||||
{isCreator && (
|
{isCreator && (
|
||||||
<div className="p-4 sm:px-8 bg-slate-50/70 dark:bg-slate-800/40 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3">
|
<div className="p-4 sm:px-8 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<ToggleLeft className="w-4 h-4 text-[#1B2CC1]" />
|
<ToggleLeft className="w-4 h-4 text-[#1B2CC1]" />
|
||||||
<span className="text-xs font-bold text-slate-700 dark:text-slate-300">
|
<span className="text-xs font-bold text-slate-700 dark:text-slate-300">
|
||||||
@@ -278,8 +293,8 @@ export default function OrderDetailPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-2 flex-wrap">
|
<div className="flex items-center gap-2 flex-wrap">
|
||||||
{/* Edit Button (Available when not CLOSE) */}
|
{/* Edit Button (Available when not CLOSE and date is today) */}
|
||||||
{!isClosed && (
|
{!isClosed && isToday(new Date(order.date)) && (
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@@ -292,7 +307,7 @@ export default function OrderDetailPage() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Status Change Buttons */}
|
{/* Status Change Buttons */}
|
||||||
{order.status === 'DRAFT' && (
|
{order.status === 'DRAFT' && isToday(new Date(order.date)) && (
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={() => handleStatusChange('OPEN')}
|
onClick={() => handleStatusChange('OPEN')}
|
||||||
@@ -316,7 +331,7 @@ export default function OrderDetailPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{order.status === 'CLOSE' && (
|
{order.status === 'CLOSE' && isToday(new Date(order.date)) && (
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={() => handleStatusChange('OPEN')}
|
onClick={() => handleStatusChange('OPEN')}
|
||||||
@@ -395,15 +410,19 @@ export default function OrderDetailPage() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filteredSubmissions.map((sub: any) => (
|
return filteredSubmissions.map((sub: any) => {
|
||||||
|
const userBalance = balances.find(b => b.user.id === sub.user.id)?.amount || 0
|
||||||
|
return (
|
||||||
<SubmissionRow
|
<SubmissionRow
|
||||||
key={sub.id}
|
key={sub.id}
|
||||||
sub={sub}
|
sub={sub}
|
||||||
isCreator={isCreator}
|
isCreator={isCreator}
|
||||||
isClosed={isClosed}
|
isClosed={isClosed}
|
||||||
onUpdate={loadOrder}
|
currentBalance={userBalance}
|
||||||
|
onUpdate={() => loadOrder(userId || undefined)}
|
||||||
/>
|
/>
|
||||||
))
|
)
|
||||||
|
})
|
||||||
})()}
|
})()}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -474,43 +493,88 @@ function SubmissionRow({
|
|||||||
sub,
|
sub,
|
||||||
isCreator,
|
isCreator,
|
||||||
isClosed,
|
isClosed,
|
||||||
|
currentBalance,
|
||||||
onUpdate
|
onUpdate
|
||||||
}: {
|
}: {
|
||||||
sub: any
|
sub: any
|
||||||
isCreator: boolean
|
isCreator: boolean
|
||||||
isClosed: boolean
|
isClosed: boolean
|
||||||
|
currentBalance?: number
|
||||||
onUpdate: () => void
|
onUpdate: () => void
|
||||||
}) {
|
}) {
|
||||||
const [bill, setBill] = useState(sub.bill ?? '')
|
const [bill, setBill] = useState(sub.bill ?? '')
|
||||||
|
const [paidAmount, setPaidAmount] = useState(sub.paid_amount ?? '')
|
||||||
const [status, setStatus] = useState(sub.payment_status || 'BELUM_BAYAR')
|
const [status, setStatus] = useState(sub.payment_status || 'BELUM_BAYAR')
|
||||||
const [saving, setSaving] = useState(false)
|
const [saving, setSaving] = useState(false)
|
||||||
|
|
||||||
const handleSave = async () => {
|
// Sync state when props change after save
|
||||||
|
useEffect(() => {
|
||||||
|
setBill(sub.bill ?? '')
|
||||||
|
setPaidAmount(sub.paid_amount ?? '')
|
||||||
|
setStatus(sub.payment_status || 'BELUM_BAYAR')
|
||||||
|
}, [sub])
|
||||||
|
|
||||||
|
const handleSave = async (overrideStatus?: string, overridePaid?: string | number) => {
|
||||||
setSaving(true)
|
setSaving(true)
|
||||||
await updateSubmissionPayment(sub.id, bill !== '' ? parseInt(bill as string) : null, status)
|
const finalBill = bill !== '' ? parseInt(bill as string) : null
|
||||||
|
let finalStatus = overrideStatus || status
|
||||||
|
|
||||||
|
let finalPaid: number | null = null
|
||||||
|
if (overridePaid !== undefined) {
|
||||||
|
finalPaid = overridePaid !== '' ? parseInt(overridePaid as string) : null
|
||||||
|
} else {
|
||||||
|
finalPaid = paidAmount !== '' ? parseInt(paidAmount as string) : null
|
||||||
|
}
|
||||||
|
|
||||||
|
// AUTO-LUNAS LOGIC: Jika Uang Diterima >= Tagihan, otomatis set jadi LUNAS
|
||||||
|
if (finalPaid !== null && finalBill !== null && finalPaid >= finalBill && finalBill > 0) {
|
||||||
|
finalStatus = 'LUNAS'
|
||||||
|
setStatus('LUNAS')
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await updateSubmissionPayment(sub.id, finalBill, finalStatus, finalPaid)
|
||||||
|
|
||||||
|
// Tampilkan pesan error jika gagal (contoh: Prisma error)
|
||||||
|
if (res && res.error) {
|
||||||
|
alert("Gagal menyimpan: " + res.error + "\n\nPastikan Anda sudah me-restart server (npm run dev) jika baru ada perubahan database.")
|
||||||
|
}
|
||||||
|
|
||||||
onUpdate()
|
onUpdate()
|
||||||
setSaving(false)
|
setSaving(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleUseBalance = () => {
|
||||||
|
setPaidAmount('0')
|
||||||
|
setStatus('LUNAS')
|
||||||
|
handleSave('LUNAS', '0')
|
||||||
|
}
|
||||||
|
|
||||||
const formatRupiah = (angka: number) => {
|
const formatRupiah = (angka: number) => {
|
||||||
return new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(angka)
|
return new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(angka)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden hover:border-[#1B2CC1]/30 transition-all">
|
<Card className="rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden hover:border-[#1B2CC1]/30 transition-all">
|
||||||
<div className="p-5 flex flex-col md:flex-row gap-5 items-start md:items-center">
|
<div className="p-4 flex flex-col md:flex-row gap-4 items-start md:items-stretch">
|
||||||
{/* Left: User & Order items */}
|
{/* Left: User & Order items */}
|
||||||
<div className="flex-1 space-y-3 w-full">
|
<div className="flex-1 space-y-2 w-full flex flex-col justify-center">
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
{sub.user.photo ? (
|
{sub.user.photo ? (
|
||||||
<img src={sub.user.photo} alt={sub.user.name} className="w-10 h-10 rounded-full object-cover ring-2 ring-slate-100 dark:ring-slate-800" />
|
<img src={sub.user.photo} alt={sub.user.name} className="w-9 h-9 rounded-full object-cover ring-2 ring-slate-100 dark:ring-slate-800" />
|
||||||
) : (
|
) : (
|
||||||
<div className="w-10 h-10 rounded-full bg-[#1B2CC1]/10 text-[#1B2CC1] flex items-center justify-center font-bold text-sm ring-2 ring-slate-100 dark:ring-slate-800">
|
<div className="w-9 h-9 rounded-full bg-[#1B2CC1]/10 text-[#1B2CC1] flex items-center justify-center font-bold text-sm ring-2 ring-slate-100 dark:ring-slate-800">
|
||||||
{sub.user.name.charAt(0).toUpperCase()}
|
{sub.user.name.charAt(0).toUpperCase()}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div>
|
<div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
<p className="font-bold text-slate-900 dark:text-white text-sm">{sub.user.name}</p>
|
<p className="font-bold text-slate-900 dark:text-white text-sm">{sub.user.name}</p>
|
||||||
|
{currentBalance !== undefined && currentBalance !== 0 && (
|
||||||
|
<span className={cn("text-[9px] font-bold px-1.5 py-0.5 rounded-full", currentBalance > 0 ? "bg-emerald-50 text-emerald-700 border border-emerald-200" : "bg-rose-50 text-rose-700 border border-rose-200")}>
|
||||||
|
Saldo: {formatRupiah(currentBalance)}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
<div className="flex items-center gap-2 mt-0.5">
|
<div className="flex items-center gap-2 mt-0.5">
|
||||||
<span className={cn(
|
<span className={cn(
|
||||||
"inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-black",
|
"inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-black",
|
||||||
@@ -529,7 +593,7 @@ function SubmissionRow({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="bg-slate-50 dark:bg-slate-800/50 p-3 rounded-xl border border-slate-200/70 dark:border-slate-800">
|
<div className="bg-slate-50 dark:bg-slate-800/50 p-2.5 rounded-xl border border-slate-200/70 dark:border-slate-800 mt-2">
|
||||||
<ul className="space-y-1 text-xs">
|
<ul className="space-y-1 text-xs">
|
||||||
{sub.items.map((item: any) => (
|
{sub.items.map((item: any) => (
|
||||||
<li key={item.id} className="flex justify-between items-center font-medium">
|
<li key={item.id} className="flex justify-between items-center font-medium">
|
||||||
@@ -545,43 +609,103 @@ function SubmissionRow({
|
|||||||
|
|
||||||
{/* Right: Bill input if Creator */}
|
{/* Right: Bill input if Creator */}
|
||||||
{isCreator && (
|
{isCreator && (
|
||||||
<div className="w-full md:w-60 border-t md:border-t-0 md:border-l pt-4 md:pt-0 md:pl-5 border-slate-200/80 dark:border-slate-800 space-y-2.5 shrink-0">
|
<div className="w-full md:w-[380px] border-t md:border-t-0 md:border-l pt-3 md:pt-0 md:pl-4 border-slate-200/80 dark:border-slate-800 space-y-2 shrink-0 flex flex-col justify-center">
|
||||||
{isClosed ? (
|
{isClosed ? (
|
||||||
<>
|
<>
|
||||||
|
<div className="grid grid-cols-3 gap-2">
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<label className="text-[11px] font-bold uppercase tracking-wider text-slate-400">
|
<label className="text-[10px] font-bold uppercase tracking-wider text-slate-400 block mb-1">
|
||||||
Nominal Tagihan (Rp)
|
Tagihan
|
||||||
</label>
|
</label>
|
||||||
<Input
|
<Input
|
||||||
type="number"
|
type="number"
|
||||||
placeholder="Contoh: 25000"
|
placeholder="Rp"
|
||||||
value={bill}
|
value={bill}
|
||||||
onChange={e => setBill(e.target.value)}
|
disabled={sub.payment_status === 'LUNAS'}
|
||||||
className="h-9 rounded-lg font-bold text-xs"
|
onChange={e => {
|
||||||
|
const val = e.target.value
|
||||||
|
setBill(val)
|
||||||
|
const pBill = val !== '' ? parseInt(val) : 0
|
||||||
|
const pPaid = paidAmount !== '' ? parseInt(paidAmount as string) : 0
|
||||||
|
if (val !== '' && pPaid >= pBill && pBill > 0) {
|
||||||
|
setStatus('LUNAS')
|
||||||
|
} else {
|
||||||
|
setStatus('BELUM_BAYAR')
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
className="h-8 rounded-md font-bold text-xs"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<label className="text-[11px] font-bold uppercase tracking-wider text-slate-400">
|
<label className="text-[10px] font-bold uppercase tracking-wider text-slate-400">
|
||||||
Status Bayar
|
Diterima
|
||||||
</label>
|
</label>
|
||||||
<Select value={status} onValueChange={setStatus}>
|
<Input
|
||||||
<SelectTrigger className="h-9 rounded-lg text-xs font-semibold">
|
type="number"
|
||||||
|
placeholder="Rp"
|
||||||
|
value={paidAmount}
|
||||||
|
disabled={sub.payment_status === 'LUNAS'}
|
||||||
|
onChange={e => {
|
||||||
|
const val = e.target.value
|
||||||
|
setPaidAmount(val)
|
||||||
|
const pBill = bill !== '' ? parseInt(bill as string) : 0
|
||||||
|
const pPaid = val !== '' ? parseInt(val) : 0
|
||||||
|
if (val !== '' && pPaid >= pBill && pBill > 0) {
|
||||||
|
setStatus('LUNAS')
|
||||||
|
} else {
|
||||||
|
setStatus('BELUM_BAYAR')
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
className="h-8 rounded-md font-bold text-xs"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="text-[10px] font-bold uppercase tracking-wider text-slate-400">
|
||||||
|
Status
|
||||||
|
</label>
|
||||||
|
<Select value={status} onValueChange={setStatus} disabled={sub.payment_status === 'LUNAS'}>
|
||||||
|
<SelectTrigger className="h-8 rounded-md text-xs font-semibold px-2">
|
||||||
<SelectValue />
|
<SelectValue />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="BELUM_BAYAR">Belum Bayar</SelectItem>
|
<SelectItem value="BELUM_BAYAR">Belum</SelectItem>
|
||||||
<SelectItem value="LUNAS">Lunas</SelectItem>
|
<SelectItem value="LUNAS">Lunas</SelectItem>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex gap-2 w-full mt-1">
|
||||||
|
{sub.payment_status !== 'LUNAS' ? (
|
||||||
|
<>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={handleSave}
|
onClick={() => handleSave()}
|
||||||
disabled={saving}
|
disabled={saving}
|
||||||
className="w-full h-9 rounded-lg bg-[#1B2CC1] hover:bg-[#15229E] text-white font-bold text-xs shadow-sm gap-1.5 mt-1"
|
className="flex-1 h-8 rounded-md bg-[#1B2CC1] hover:bg-[#15229E] text-white font-bold text-xs shadow-sm gap-1.5"
|
||||||
>
|
>
|
||||||
{saving ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <><Save className="w-3.5 h-3.5" /> Simpan Tagihan</>}
|
{saving ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <><Save className="w-3.5 h-3.5" /> Simpan</>}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
{currentBalance !== undefined && currentBalance > 0 && status !== 'LUNAS' && (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
onClick={handleUseBalance}
|
||||||
|
disabled={saving || !bill}
|
||||||
|
variant="outline"
|
||||||
|
className="flex-1 h-8 rounded-md border-emerald-200 text-emerald-700 hover:bg-emerald-50 font-bold text-xs shadow-sm"
|
||||||
|
title={!bill ? "Isi nominal tagihan dulu" : "Potong saldo"}
|
||||||
|
>
|
||||||
|
Pakai Saldo
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<div className="w-full bg-emerald-50 dark:bg-emerald-900/30 text-emerald-600 dark:text-emerald-400 border border-emerald-200 dark:border-emerald-800 rounded-md h-8 flex items-center justify-center text-xs font-bold gap-1.5 cursor-not-allowed opacity-80">
|
||||||
|
<Check className="w-4 h-4" /> Telah Lunas
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<div className="p-3 bg-slate-50 dark:bg-slate-800/40 rounded-xl text-center border border-dashed border-slate-200 dark:border-slate-800">
|
<div className="p-3 bg-slate-50 dark:bg-slate-800/40 rounded-xl text-center border border-dashed border-slate-200 dark:border-slate-800">
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from
|
|||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { Checkbox } from '@/components/ui/checkbox'
|
import { Checkbox } from '@/components/ui/checkbox'
|
||||||
import { format } from 'date-fns'
|
import { format, isToday } from 'date-fns'
|
||||||
import { id as idLocale } from 'date-fns/locale'
|
import { id as idLocale } from 'date-fns/locale'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import {
|
import {
|
||||||
@@ -306,10 +306,10 @@ export default function MyOrdersPage() {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={order.id}
|
key={order.id}
|
||||||
className="flex flex-col md:flex-row h-full md:h-auto rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm hover:shadow-md hover:border-[#1B2CC1]/40 transition-all overflow-hidden"
|
className="flex flex-col md:flex-row shrink-0 rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm hover:shadow-md hover:border-[#1B2CC1]/40 transition-all overflow-hidden"
|
||||||
>
|
>
|
||||||
{/* Left: Info */}
|
{/* Left: Info */}
|
||||||
<div className="flex-1 p-5 md:border-r border-slate-100 dark:border-slate-800/80 flex flex-col justify-center">
|
<div className="w-full md:flex-1 p-5 md:border-r border-slate-100 dark:border-slate-800/80 flex flex-col justify-center">
|
||||||
<div className="flex justify-between items-start gap-4">
|
<div className="flex justify-between items-start gap-4">
|
||||||
<h3 className="text-xl font-bold text-slate-900 dark:text-white line-clamp-2 leading-snug flex-1 min-w-0">
|
<h3 className="text-xl font-bold text-slate-900 dark:text-white line-clamp-2 leading-snug flex-1 min-w-0">
|
||||||
{order.title}
|
{order.title}
|
||||||
@@ -358,14 +358,14 @@ export default function MyOrdersPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Right: Actions */}
|
{/* Right: Actions */}
|
||||||
<div className="w-full md:w-64 p-5 bg-slate-50/70 dark:bg-slate-800/40 flex flex-col justify-center gap-3 md:border-l border-slate-100 dark:border-slate-800/80 mt-auto md:mt-0">
|
<div className="w-full md:w-[260px] p-4 sm:p-5 bg-slate-50/70 dark:bg-slate-800/40 flex flex-col justify-center gap-3 shrink-0">
|
||||||
<div className="grid grid-cols-2 gap-2 w-full">
|
<div className="flex flex-row gap-2 w-full">
|
||||||
{/* Detail Link */}
|
{/* Detail Link */}
|
||||||
<Link
|
<Link
|
||||||
href={`/my-orders/${order.id}`}
|
href={`/my-orders/${order.id}`}
|
||||||
className={cn(
|
className={cn(
|
||||||
buttonVariants({ variant: "outline", size: "sm" }),
|
buttonVariants({ variant: "outline", size: "sm" }),
|
||||||
"w-full h-10 rounded-xl border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 font-bold text-xs gap-1.5 hover:bg-blue-50/60 hover:text-[#1B2CC1] hover:border-[#1B2CC1]/40 shadow-2xs transition-all flex items-center justify-center px-2"
|
"flex-1 h-10 rounded-xl border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 font-bold text-xs gap-1.5 hover:bg-blue-50/60 hover:text-[#1B2CC1] hover:border-[#1B2CC1]/40 shadow-sm transition-all flex items-center justify-center px-2"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<FileText className="w-4 h-4 text-[#1B2CC1] shrink-0" />
|
<FileText className="w-4 h-4 text-[#1B2CC1] shrink-0" />
|
||||||
@@ -373,12 +373,18 @@ export default function MyOrdersPage() {
|
|||||||
</Link>
|
</Link>
|
||||||
|
|
||||||
{/* Status Action Button */}
|
{/* Status Action Button */}
|
||||||
<div className="w-full">
|
<div className="flex-1">
|
||||||
{order.status === 'DRAFT' && (
|
{order.status === 'DRAFT' && (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => handleStatusChange(order.id, 'OPEN')}
|
onClick={() => handleStatusChange(order.id, 'OPEN')}
|
||||||
className="w-full h-10 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs shadow-sm gap-1.5 transition-all cursor-pointer px-2"
|
disabled={!isToday(new Date(order.date))}
|
||||||
title="Buka PO"
|
className={cn(
|
||||||
|
"w-full h-10 rounded-xl font-bold text-xs shadow-sm gap-1.5 transition-all px-2",
|
||||||
|
isToday(new Date(order.date))
|
||||||
|
? "bg-emerald-600 hover:bg-emerald-700 text-white cursor-pointer"
|
||||||
|
: "bg-slate-200 text-slate-500 cursor-not-allowed opacity-50"
|
||||||
|
)}
|
||||||
|
title={isToday(new Date(order.date)) ? "Buka PO" : "Hanya PO hari ini yang bisa dibuka"}
|
||||||
>
|
>
|
||||||
<Sparkles className="w-3.5 h-3.5 shrink-0" />
|
<Sparkles className="w-3.5 h-3.5 shrink-0" />
|
||||||
<span className="truncate">Buka</span>
|
<span className="truncate">Buka</span>
|
||||||
@@ -397,8 +403,14 @@ export default function MyOrdersPage() {
|
|||||||
{order.status === 'CLOSE' && (
|
{order.status === 'CLOSE' && (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => handleStatusChange(order.id, 'OPEN')}
|
onClick={() => handleStatusChange(order.id, 'OPEN')}
|
||||||
className="w-full h-10 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs shadow-sm gap-1.5 transition-all cursor-pointer px-2"
|
disabled={!isToday(new Date(order.date))}
|
||||||
title="Buka Kembali PO"
|
className={cn(
|
||||||
|
"w-full h-10 rounded-xl font-bold text-xs shadow-sm gap-1.5 transition-all px-2",
|
||||||
|
isToday(new Date(order.date))
|
||||||
|
? "bg-emerald-600 hover:bg-emerald-700 text-white cursor-pointer"
|
||||||
|
: "bg-slate-200 text-slate-500 cursor-not-allowed opacity-50"
|
||||||
|
)}
|
||||||
|
title={isToday(new Date(order.date)) ? "Buka Kembali PO" : "Hanya PO hari ini yang bisa dibuka"}
|
||||||
>
|
>
|
||||||
<Sparkles className="w-3.5 h-3.5 shrink-0" />
|
<Sparkles className="w-3.5 h-3.5 shrink-0" />
|
||||||
<span className="truncate">Buka</span>
|
<span className="truncate">Buka</span>
|
||||||
@@ -408,33 +420,33 @@ export default function MyOrdersPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Utility Tools Row */}
|
{/* Utility Tools Row */}
|
||||||
<div className="grid grid-cols-3 gap-1.5 pt-2 border-t border-slate-200/60 dark:border-slate-800/60">
|
<div className="flex items-center justify-between gap-1 pt-3 border-t border-slate-200/60 dark:border-slate-800/60">
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
disabled={isClosed}
|
disabled={isClosed || !isToday(new Date(order.date))}
|
||||||
onClick={() => setEditingOrder(order)}
|
onClick={() => setEditingOrder(order)}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-8 rounded-lg font-bold text-[11px] gap-1 px-1.5 transition-all",
|
"flex-1 h-8 rounded-lg font-bold text-[10px] sm:text-[11px] gap-1 px-1 transition-all",
|
||||||
isClosed
|
(isClosed || !isToday(new Date(order.date)))
|
||||||
? "opacity-35 cursor-not-allowed text-slate-400"
|
? "opacity-35 cursor-not-allowed text-slate-400"
|
||||||
: "text-slate-600 hover:text-[#1B2CC1] hover:bg-[#1B2CC1]/10 dark:text-slate-300"
|
: "text-slate-600 hover:text-[#1B2CC1] hover:bg-[#1B2CC1]/10 dark:text-slate-300"
|
||||||
)}
|
)}
|
||||||
title={isClosed ? "PO sudah CLOSE" : "Edit PO"}
|
title={isClosed ? "PO sudah CLOSE" : !isToday(new Date(order.date)) ? "Hanya PO hari ini yang bisa diedit" : "Edit PO"}
|
||||||
>
|
>
|
||||||
<Pencil className="w-3.5 h-3.5" />
|
<Pencil className="w-3.5 h-3.5 shrink-0" />
|
||||||
<span>Edit</span>
|
<span className="truncate">Edit</span>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={() => setOrderToDuplicate(order)}
|
onClick={() => setOrderToDuplicate(order)}
|
||||||
className="h-8 rounded-lg font-bold text-[11px] gap-1 px-1.5 text-slate-600 hover:text-slate-900 hover:bg-slate-200/60 dark:text-slate-300 transition-all"
|
className="flex-1 h-8 rounded-lg font-bold text-[10px] sm:text-[11px] gap-1 px-1 text-slate-600 hover:text-slate-900 hover:bg-slate-200/60 dark:text-slate-300 transition-all"
|
||||||
title="Duplikasi PO ini"
|
title="Duplikasi PO ini"
|
||||||
>
|
>
|
||||||
<Copy className="w-3.5 h-3.5" />
|
<Copy className="w-3.5 h-3.5 shrink-0" />
|
||||||
<span>Duplikat</span>
|
<span className="truncate">Duplikat</span>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
@@ -443,15 +455,15 @@ export default function MyOrdersPage() {
|
|||||||
disabled={!canDelete}
|
disabled={!canDelete}
|
||||||
onClick={() => setOrderToDelete(order)}
|
onClick={() => setOrderToDelete(order)}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-8 rounded-lg font-bold text-[11px] gap-1 px-1.5 transition-all",
|
"flex-1 h-8 rounded-lg font-bold text-[10px] sm:text-[11px] gap-1 px-1 transition-all",
|
||||||
!canDelete
|
!canDelete
|
||||||
? "opacity-35 cursor-not-allowed text-slate-400"
|
? "opacity-35 cursor-not-allowed text-slate-400"
|
||||||
: "text-red-600 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-950/40"
|
: "text-red-600 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-950/40"
|
||||||
)}
|
)}
|
||||||
title={!canDelete ? "PO OPEN tidak dapat dihapus" : "Hapus PO"}
|
title={!canDelete ? "PO OPEN tidak dapat dihapus" : "Hapus PO"}
|
||||||
>
|
>
|
||||||
<Trash2 className="w-3.5 h-3.5" />
|
<Trash2 className="w-3.5 h-3.5 shrink-0" />
|
||||||
<span>Hapus</span>
|
<span className="truncate">Hapus</span>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { getCreatorReport, getSubmittorReport, getSessionUser } from '@/app/actions'
|
import { getCreatorReport, getSubmittorReport, getSessionUser, getBalancesAsCreator } from '@/app/actions'
|
||||||
import { Card } from '@/components/ui/card'
|
import { Card } from '@/components/ui/card'
|
||||||
import { Loader2, TrendingUp, TrendingDown, Wallet, ArrowRightLeft, Calendar as CalendarIcon } from 'lucide-react'
|
import { Loader2, TrendingUp, TrendingDown, Wallet, ArrowRightLeft, Calendar as CalendarIcon } from 'lucide-react'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
@@ -60,6 +60,7 @@ export default function ReportsPage() {
|
|||||||
const [userId, setUserId] = useState<string | null>(null)
|
const [userId, setUserId] = useState<string | null>(null)
|
||||||
const [submittorData, setSubmittorData] = useState<any[]>([])
|
const [submittorData, setSubmittorData] = useState<any[]>([])
|
||||||
const [creatorData, setCreatorData] = useState<any[]>([])
|
const [creatorData, setCreatorData] = useState<any[]>([])
|
||||||
|
const [creatorBalances, setCreatorBalances] = useState<any[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -79,12 +80,14 @@ export default function ReportsPage() {
|
|||||||
const loadData = async (id: string) => {
|
const loadData = async (id: string) => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const interval = getIntervalFromFilter(filterType, filterValue)
|
const interval = getIntervalFromFilter(filterType, filterValue)
|
||||||
const [subRes, creRes] = await Promise.all([
|
const [subRes, creRes, balRes] = await Promise.all([
|
||||||
getSubmittorReport(id, interval?.start, interval?.end),
|
getSubmittorReport(id, interval?.start, interval?.end),
|
||||||
getCreatorReport(id, interval?.start, interval?.end)
|
getCreatorReport(id, interval?.start, interval?.end),
|
||||||
|
getBalancesAsCreator(id)
|
||||||
])
|
])
|
||||||
setSubmittorData(subRes)
|
setSubmittorData(subRes)
|
||||||
setCreatorData(creRes)
|
setCreatorData(creRes)
|
||||||
|
setCreatorBalances(balRes)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,6 +287,8 @@ export default function ReportsPage() {
|
|||||||
) : (
|
) : (
|
||||||
<ReportByPersonGrid
|
<ReportByPersonGrid
|
||||||
data={creatorData}
|
data={creatorData}
|
||||||
|
balancesData={creatorBalances}
|
||||||
|
creatorId={userId!}
|
||||||
onUpdate={() => { if (userId) loadData(userId) }}
|
onUpdate={() => { if (userId) loadData(userId) }}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
+176
-3
@@ -381,36 +381,151 @@ export async function getOrderDetail(order_id: string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateSubmissionPayment(submission_id: string, bill: number | null, payment_status: string) {
|
export async function updateSubmissionPayment(submission_id: string, bill: number | null, payment_status: string, paid_amount: number | null = null) {
|
||||||
try {
|
try {
|
||||||
await prisma.submission.update({
|
await prisma.submission.update({
|
||||||
where: { id: submission_id },
|
where: { id: submission_id },
|
||||||
data: { bill, payment_status }
|
data: { bill, payment_status, paid_amount }
|
||||||
})
|
})
|
||||||
revalidatePath(`/my-orders`)
|
revalidatePath(`/my-orders`)
|
||||||
revalidatePath(`/my-purchases`)
|
revalidatePath(`/my-purchases`)
|
||||||
revalidatePath(`/reports`)
|
revalidatePath(`/reports`)
|
||||||
|
revalidatePath(`/balances`)
|
||||||
return { success: true }
|
return { success: true }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e); return { success: false, error: 'Gagal menyimpan tagihan.' }
|
console.error(e); return { success: false, error: 'Gagal menyimpan tagihan.' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateBulkSubmissionPayment(submission_ids: string[], payment_status: string) {
|
export async function updateBulkSubmissionPayment(submission_ids: string[], payment_status: string, use_balance: boolean = false) {
|
||||||
try {
|
try {
|
||||||
|
if (payment_status === 'LUNAS') {
|
||||||
|
const submissions = await prisma.submission.findMany({ where: { id: { in: submission_ids } } })
|
||||||
|
const ops = submissions.map(sub => prisma.submission.update({
|
||||||
|
where: { id: sub.id },
|
||||||
|
data: {
|
||||||
|
payment_status,
|
||||||
|
paid_amount: use_balance ? 0 : (sub.paid_amount != null ? sub.paid_amount : sub.bill)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
await prisma.$transaction(ops)
|
||||||
|
} else {
|
||||||
await prisma.submission.updateMany({
|
await prisma.submission.updateMany({
|
||||||
where: { id: { in: submission_ids } },
|
where: { id: { in: submission_ids } },
|
||||||
data: { payment_status }
|
data: { payment_status }
|
||||||
})
|
})
|
||||||
|
}
|
||||||
revalidatePath(`/my-orders`)
|
revalidatePath(`/my-orders`)
|
||||||
revalidatePath(`/my-purchases`)
|
revalidatePath(`/my-purchases`)
|
||||||
revalidatePath(`/reports`)
|
revalidatePath(`/reports`)
|
||||||
|
revalidatePath(`/balances`)
|
||||||
return { success: true }
|
return { success: true }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
return { success: false, error: 'Gagal mengubah status tagihan massal.' }
|
return { success: false, error: 'Gagal mengubah status tagihan massal.' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function processBulkPayment(data: {
|
||||||
|
submission_ids: string[];
|
||||||
|
cash_amount: number;
|
||||||
|
use_balance: boolean;
|
||||||
|
creator_id: string;
|
||||||
|
user_id: string;
|
||||||
|
}) {
|
||||||
|
try {
|
||||||
|
let currentBalance = 0
|
||||||
|
if (data.use_balance) {
|
||||||
|
const balanceData = await getBalancesAsCreator(data.creator_id)
|
||||||
|
const userBalance = balanceData.find(b => b.user.id === data.user_id)
|
||||||
|
if (userBalance) currentBalance = userBalance.amount
|
||||||
|
}
|
||||||
|
|
||||||
|
const submissions = await prisma.submission.findMany({
|
||||||
|
where: { id: { in: data.submission_ids } },
|
||||||
|
include: { order: { select: { date: true } } }
|
||||||
|
})
|
||||||
|
|
||||||
|
// Sort by order date ascending (oldest first)
|
||||||
|
submissions.sort((a, b) => new Date(a.order.date).getTime() - new Date(b.order.date).getTime())
|
||||||
|
|
||||||
|
let remainingBalance = currentBalance
|
||||||
|
let remainingCash = data.cash_amount
|
||||||
|
let totalAvailable = remainingBalance + remainingCash
|
||||||
|
|
||||||
|
const ops = []
|
||||||
|
|
||||||
|
for (let i = 0; i < submissions.length; i++) {
|
||||||
|
const sub = submissions[i]
|
||||||
|
const isLast = i === submissions.length - 1
|
||||||
|
|
||||||
|
const subBill = sub.bill || 0
|
||||||
|
const prevPaid = sub.paid_amount || 0
|
||||||
|
const amountToCover = Math.max(0, subBill - prevPaid)
|
||||||
|
|
||||||
|
if (totalAvailable >= amountToCover && amountToCover > 0) {
|
||||||
|
// Fully covered -> LUNAS
|
||||||
|
let balanceToUse = Math.min(remainingBalance, amountToCover)
|
||||||
|
remainingBalance -= balanceToUse
|
||||||
|
|
||||||
|
let cashToUse = amountToCover - balanceToUse
|
||||||
|
remainingCash -= cashToUse
|
||||||
|
totalAvailable -= amountToCover
|
||||||
|
|
||||||
|
let finalPaid = prevPaid + cashToUse
|
||||||
|
if (isLast && remainingCash > 0) {
|
||||||
|
finalPaid += remainingCash
|
||||||
|
remainingCash = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
ops.push(prisma.submission.update({
|
||||||
|
where: { id: sub.id },
|
||||||
|
data: { payment_status: 'LUNAS', paid_amount: finalPaid }
|
||||||
|
}))
|
||||||
|
} else if (totalAvailable >= amountToCover && amountToCover === 0) {
|
||||||
|
// It's already fully paid somehow, just mark LUNAS. Give excess cash if last.
|
||||||
|
let finalPaid = prevPaid
|
||||||
|
if (isLast && remainingCash > 0) {
|
||||||
|
finalPaid += remainingCash
|
||||||
|
remainingCash = 0
|
||||||
|
}
|
||||||
|
ops.push(prisma.submission.update({
|
||||||
|
where: { id: sub.id },
|
||||||
|
data: { payment_status: 'LUNAS', paid_amount: finalPaid }
|
||||||
|
}))
|
||||||
|
} else if (totalAvailable > 0) {
|
||||||
|
// Partially covered -> BELUM_BAYAR. Only use cash.
|
||||||
|
let finalPaid = prevPaid + remainingCash
|
||||||
|
remainingCash = 0
|
||||||
|
totalAvailable = remainingBalance // only balance left, which can't be used
|
||||||
|
|
||||||
|
ops.push(prisma.submission.update({
|
||||||
|
where: { id: sub.id },
|
||||||
|
data: { payment_status: 'BELUM_BAYAR', paid_amount: finalPaid }
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
// totalAvailable == 0. No more money. Just leave it as is, or update to BELUM_BAYAR.
|
||||||
|
ops.push(prisma.submission.update({
|
||||||
|
where: { id: sub.id },
|
||||||
|
data: { payment_status: 'BELUM_BAYAR' }
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.$transaction(ops)
|
||||||
|
|
||||||
|
revalidatePath(`/my-orders`)
|
||||||
|
revalidatePath(`/my-purchases`)
|
||||||
|
revalidatePath(`/reports`)
|
||||||
|
revalidatePath(`/balances`)
|
||||||
|
|
||||||
|
return { success: true }
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
return { success: false, error: 'Gagal memproses pembayaran massal cerdas.' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// === SUBMISSION (PESANAN SAYA) ===
|
// === SUBMISSION (PESANAN SAYA) ===
|
||||||
export async function getUserSubmission(order_id: string, user_id: string) {
|
export async function getUserSubmission(order_id: string, user_id: string) {
|
||||||
return await prisma.submission.findFirst({
|
return await prisma.submission.findFirst({
|
||||||
@@ -524,3 +639,61 @@ export async function getCreatorReport(creator_id: string, startDate?: Date, end
|
|||||||
orderBy: { date: 'desc' }
|
orderBy: { date: 'desc' }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === BALANCE ACTIONS ===
|
||||||
|
export async function getBalancesAsCreator(creator_id: string) {
|
||||||
|
const submissions = await prisma.submission.findMany({
|
||||||
|
where: {
|
||||||
|
order: { creator_id },
|
||||||
|
payment_status: 'LUNAS'
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: { select: { id: true, name: true, photo: true } }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const balanceMap = new Map<string, { user: any, amount: number }>()
|
||||||
|
|
||||||
|
submissions.forEach(sub => {
|
||||||
|
if (sub.paid_amount == null || sub.bill == null) return
|
||||||
|
const diff = sub.paid_amount - sub.bill
|
||||||
|
if (!balanceMap.has(sub.user.id)) {
|
||||||
|
balanceMap.set(sub.user.id, { user: sub.user, amount: diff })
|
||||||
|
} else {
|
||||||
|
balanceMap.get(sub.user.id)!.amount += diff
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.from(balanceMap.values()).filter(b => b.amount !== 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getBalancesAsSubmittor(user_id: string) {
|
||||||
|
const submissions = await prisma.submission.findMany({
|
||||||
|
where: {
|
||||||
|
user_id,
|
||||||
|
payment_status: 'LUNAS'
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
order: {
|
||||||
|
include: {
|
||||||
|
creator: { select: { id: true, name: true, photo: true } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const balanceMap = new Map<string, { creator: any, amount: number }>()
|
||||||
|
|
||||||
|
submissions.forEach(sub => {
|
||||||
|
if (sub.paid_amount == null || sub.bill == null) return
|
||||||
|
const diff = sub.paid_amount - sub.bill
|
||||||
|
const creator = sub.order.creator
|
||||||
|
if (!balanceMap.has(creator.id)) {
|
||||||
|
balanceMap.set(creator.id, { creator: creator, amount: diff })
|
||||||
|
} else {
|
||||||
|
balanceMap.get(creator.id)!.amount += diff
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.from(balanceMap.values()).filter(b => b.amount !== 0)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const navItems = [
|
|||||||
{ name: 'Jasa Order Saya', href: '/my-orders' },
|
{ name: 'Jasa Order Saya', href: '/my-orders' },
|
||||||
{ name: 'Pesanan Saya', href: '/my-purchases' },
|
{ name: 'Pesanan Saya', href: '/my-purchases' },
|
||||||
{ name: 'Laporan', href: '/reports' },
|
{ name: 'Laporan', href: '/reports' },
|
||||||
|
{ name: 'Buku Saldo', href: '/balances' },
|
||||||
{ name: 'Profile', href: '/profile' },
|
{ name: 'Profile', href: '/profile' },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { ChevronDown, ChevronUp, Users, CheckCircle2, Package, InboxIcon, Chevro
|
|||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from '@/components/ui/dialog'
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from '@/components/ui/dialog'
|
||||||
import { updateBulkSubmissionPayment } from '@/app/actions'
|
import { processBulkPayment } from '@/app/actions'
|
||||||
import { format } from 'date-fns'
|
import { format } from 'date-fns'
|
||||||
|
|
||||||
const ITEMS_PER_PAGE = 5
|
const ITEMS_PER_PAGE = 5
|
||||||
@@ -14,13 +14,14 @@ const ITEMS_PER_PAGE = 5
|
|||||||
const formatRupiah = (value: number) =>
|
const formatRupiah = (value: number) =>
|
||||||
new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(value)
|
new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(value)
|
||||||
|
|
||||||
export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; onUpdate: () => void }) {
|
export default function ReportByPersonGrid({ data, balancesData = [], creatorId, onUpdate }: { data: any[]; balancesData?: any[]; creatorId: string; onUpdate: () => void }) {
|
||||||
const [expandedRows, setExpandedRows] = useState<Record<string, boolean>>({})
|
const [expandedRows, setExpandedRows] = useState<Record<string, boolean>>({})
|
||||||
const [currentPage, setCurrentPage] = useState(1)
|
const [currentPage, setCurrentPage] = useState(1)
|
||||||
const [searchQuery, setSearchQuery] = useState('')
|
const [searchQuery, setSearchQuery] = useState('')
|
||||||
const [modalOpen, setModalOpen] = useState(false)
|
const [modalOpen, setModalOpen] = useState(false)
|
||||||
const [saving, setSaving] = useState(false)
|
const [saving, setSaving] = useState(false)
|
||||||
const [target, setTarget] = useState<{ ids: string[]; label: string; amount: number }>({ ids: [], label: '', amount: 0 })
|
const [target, setTarget] = useState<{ ids: string[]; label: string; amount: number; userId: string }>({ ids: [], label: '', amount: 0, userId: '' })
|
||||||
|
const [cashInput, setCashInput] = useState<string>('')
|
||||||
|
|
||||||
// Reset page + search when data changes (e.g. filter changed)
|
// Reset page + search when data changes (e.g. filter changed)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -75,16 +76,24 @@ export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; on
|
|||||||
const totalPages = Math.ceil(filteredUserList.length / ITEMS_PER_PAGE)
|
const totalPages = Math.ceil(filteredUserList.length / ITEMS_PER_PAGE)
|
||||||
const paginatedList = filteredUserList.slice((currentPage - 1) * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE)
|
const paginatedList = filteredUserList.slice((currentPage - 1) * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE)
|
||||||
|
|
||||||
const openModal = (e: React.MouseEvent, ids: string[], label: string, amount: number) => {
|
const openModal = (e: React.MouseEvent, ids: string[], label: string, amount: number, userId: string) => {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
setTarget({ ids, label, amount })
|
setTarget({ ids, label, amount, userId })
|
||||||
|
setCashInput('')
|
||||||
setModalOpen(true)
|
setModalOpen(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleConfirm = async () => {
|
const handleConfirm = async () => {
|
||||||
if (!target.ids.length) return
|
if (!target.ids.length) return
|
||||||
setSaving(true)
|
setSaving(true)
|
||||||
const res = await updateBulkSubmissionPayment(target.ids, 'LUNAS')
|
const cash = Number(cashInput.replace(/\D/g, '')) || 0
|
||||||
|
const res = await processBulkPayment({
|
||||||
|
submission_ids: target.ids,
|
||||||
|
cash_amount: cash,
|
||||||
|
use_balance: true,
|
||||||
|
creator_id: creatorId,
|
||||||
|
user_id: target.userId
|
||||||
|
})
|
||||||
setSaving(false)
|
setSaving(false)
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
setModalOpen(false)
|
setModalOpen(false)
|
||||||
@@ -92,6 +101,9 @@ export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; on
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const targetUserBalanceObj = balancesData.find(b => b.user.id === target.userId)
|
||||||
|
const targetUserBalance = targetUserBalanceObj ? targetUserBalanceObj.amount : 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card className="rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden">
|
<Card className="rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden">
|
||||||
@@ -182,7 +194,7 @@ export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; on
|
|||||||
<td className="px-6 py-4 text-center">
|
<td className="px-6 py-4 text-center">
|
||||||
{unpaid.length > 0 ? (
|
{unpaid.length > 0 ? (
|
||||||
<Button
|
<Button
|
||||||
onClick={(e) => openModal(e, unpaid.map((s: any) => s.id), `Semua tagihan ${userObj.user.name}`, userObj.totalPiutang)}
|
onClick={(e) => openModal(e, unpaid.map((s: any) => s.id), `Semua tagihan ${userObj.user.name}`, userObj.totalPiutang, userObj.user.id)}
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-8 text-[11px] font-bold bg-[#1B2CC1] text-white hover:bg-[#121E85] shadow-sm shadow-[#1B2CC1]/30 gap-1 px-3"
|
className="h-8 text-[11px] font-bold bg-[#1B2CC1] text-white hover:bg-[#121E85] shadow-sm shadow-[#1B2CC1]/30 gap-1 px-3"
|
||||||
>
|
>
|
||||||
@@ -235,7 +247,7 @@ export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; on
|
|||||||
<td className="px-4 py-2.5 text-center">
|
<td className="px-4 py-2.5 text-center">
|
||||||
{isUnpaid ? (
|
{isUnpaid ? (
|
||||||
<Button
|
<Button
|
||||||
onClick={(e) => openModal(e, [sub.id], `PO: ${sub.orderTitle}`, Number(sub.bill) || 0)}
|
onClick={(e) => openModal(e, [sub.id], `PO: ${sub.orderTitle}`, Number(sub.bill) || 0, sub.user.id)}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-6 text-[10px] px-2 font-bold border-[#1B2CC1]/40 text-[#1B2CC1] hover:bg-[#1B2CC1] hover:text-white transition-colors"
|
className="h-6 text-[10px] px-2 font-bold border-[#1B2CC1]/40 text-[#1B2CC1] hover:bg-[#1B2CC1] hover:text-white transition-colors"
|
||||||
@@ -297,15 +309,74 @@ export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; on
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<div className="bg-slate-50 dark:bg-slate-900/50 p-4 rounded-xl border border-slate-100 dark:border-slate-800 my-2">
|
<div className="bg-slate-50 dark:bg-slate-900/50 p-4 rounded-xl border border-slate-100 dark:border-slate-800 my-2">
|
||||||
<p className="text-xs font-bold text-slate-500 uppercase tracking-wider mb-1">Total Nominal</p>
|
<p className="text-xs font-bold text-slate-500 uppercase tracking-wider mb-1">Total Tagihan</p>
|
||||||
<p className="text-2xl font-black text-emerald-600 dark:text-emerald-400">{formatRupiah(target.amount)}</p>
|
<p className="text-2xl font-black text-rose-600 dark:text-rose-400">{formatRupiah(target.amount)}</p>
|
||||||
|
|
||||||
|
<div className="mt-4 pt-4 border-t border-slate-200 dark:border-slate-700 flex flex-col gap-3">
|
||||||
|
<div className="flex flex-col gap-1.5">
|
||||||
|
<label className="text-xs font-bold text-slate-500">Nominal Dibayar (Cash/Transfer)</label>
|
||||||
|
<div className="relative">
|
||||||
|
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-sm font-bold text-slate-500">Rp</span>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={cashInput}
|
||||||
|
onChange={(e) => {
|
||||||
|
const val = e.target.value.replace(/\D/g, '')
|
||||||
|
setCashInput(val ? new Intl.NumberFormat('id-ID').format(Number(val)) : '')
|
||||||
|
}}
|
||||||
|
placeholder="0"
|
||||||
|
className="w-full bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-700 rounded-lg h-10 pl-9 pr-3 text-sm font-bold focus:outline-none focus:ring-2 focus:ring-[#1B2CC1]"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<DialogFooter className="gap-2 sm:gap-0">
|
</div>
|
||||||
|
|
||||||
|
{targetUserBalance > 0 && (
|
||||||
|
<div className="flex justify-between items-center p-2 rounded-lg border border-slate-200 dark:border-slate-700 bg-emerald-50/50 dark:bg-emerald-900/20">
|
||||||
|
<span className="text-sm font-bold text-slate-600 dark:text-slate-300">Dipotong dari Saldo (Otomatis)</span>
|
||||||
|
<span className="text-sm font-black text-emerald-600 dark:text-emerald-400">{formatRupiah(targetUserBalance)}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="bg-white dark:bg-slate-800 p-3 rounded-lg border border-slate-200 dark:border-slate-700 mt-1 flex justify-between items-center shadow-sm">
|
||||||
|
<div className="flex flex-col">
|
||||||
|
<span className="text-xs font-bold text-slate-500">Total Pembayaran</span>
|
||||||
|
{targetUserBalance > 0 && (
|
||||||
|
<span className="text-[10px] font-medium text-slate-400 leading-none mt-0.5">(Nominal Dibayar + Saldo)</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<span className="text-lg font-black text-[#1B2CC1] dark:text-blue-400">
|
||||||
|
{formatRupiah((Number(cashInput.replace(/\D/g, '')) || 0) + targetUserBalance)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{(() => {
|
||||||
|
const totalBayar = (Number(cashInput.replace(/\D/g, '')) || 0) + targetUserBalance
|
||||||
|
const kurang = target.amount - totalBayar
|
||||||
|
if (kurang > 0) {
|
||||||
|
return (
|
||||||
|
<div className="flex justify-between items-center px-2 py-1">
|
||||||
|
<span className="text-xs font-bold text-rose-500">Masih Kurang (Sisa Hutang)</span>
|
||||||
|
<span className="text-xs font-black text-rose-500">{formatRupiah(kurang)}</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
} else if (target.amount > 0) {
|
||||||
|
return (
|
||||||
|
<div className="flex justify-between items-center px-2 py-1">
|
||||||
|
<span className="text-xs font-bold text-emerald-500">Status</span>
|
||||||
|
<span className="text-xs font-black text-emerald-500">Akan Lunas Sepenuhnya</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
})()}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<DialogFooter className="gap-2 sm:gap-0 mt-2">
|
||||||
<Button variant="outline" onClick={() => setModalOpen(false)} disabled={saving} className="rounded-xl font-bold">
|
<Button variant="outline" onClick={() => setModalOpen(false)} disabled={saving} className="rounded-xl font-bold">
|
||||||
Batal
|
Batal
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={handleConfirm} disabled={saving} className="rounded-xl font-bold bg-emerald-600 hover:bg-emerald-700 text-white shadow-md shadow-emerald-600/20">
|
<Button onClick={handleConfirm} disabled={saving} className="rounded-xl font-bold bg-[#1B2CC1] hover:bg-[#121E85] text-white shadow-md shadow-[#1B2CC1]/20">
|
||||||
{saving ? 'Menyimpan...' : 'Ya, Simpan Pelunasan'}
|
{saving ? 'Memproses...' : 'Proses Pelunasan'}
|
||||||
</Button>
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
|
|||||||
@@ -19,13 +19,15 @@ import {
|
|||||||
X,
|
X,
|
||||||
ArrowUpRight,
|
ArrowUpRight,
|
||||||
Info,
|
Info,
|
||||||
BarChart2
|
BarChart2,
|
||||||
|
Wallet
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
|
|
||||||
const menuItems = [
|
const menuItems = [
|
||||||
{ name: 'Open Order', href: '/', icon: Store, desc: 'Daftar PO live hari ini' },
|
{ name: 'Open Order', href: '/', icon: Store, desc: 'Daftar PO live hari ini' },
|
||||||
{ name: 'Jasa Order Saya', href: '/my-orders', icon: ClipboardList, desc: 'Kelola PO buatan Anda' },
|
{ name: 'Jasa Order Saya', href: '/my-orders', icon: ClipboardList, desc: 'Kelola PO buatan Anda' },
|
||||||
{ name: 'Pesanan Saya', href: '/my-purchases', icon: ShoppingBag, desc: 'Riwayat titipan Anda' },
|
{ name: 'Pesanan Saya', href: '/my-purchases', icon: ShoppingBag, desc: 'Riwayat titipan Anda' },
|
||||||
|
{ name: 'Buku Saldo', href: '/balances', icon: Wallet, desc: 'Pantau riwayat saldo' },
|
||||||
{ name: 'Laporan', href: '/reports', icon: BarChart2, desc: 'Ringkasan transaksi' },
|
{ name: 'Laporan', href: '/reports', icon: BarChart2, desc: 'Ringkasan transaksi' },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user