'use client'; import { FC, useEffect, useMemo, useState } from "react"; import { BillAttachment, BilledTo, BillingLocation, UserSettings } from "../lib/db-types"; import { formatYearMonth } from "../lib/format"; import { formatCurrency } from "../lib/formatStrings"; import { useTranslations } from "next-intl"; import { ViewBillBadge } from "./ViewBillBadge"; import { Pdf417Barcode } from "./Pdf417Barcode"; import { EncodePayment, PaymentParams } from "hub-3a-payment-encoder"; import Link from "next/link"; import { DocumentIcon } from "@heroicons/react/24/outline"; import { uploadUtilBillsProofOfPayment } from "../lib/actions/locationActions"; export interface ViewLocationCardProps { location: BillingLocation; userSettings: UserSettings | null; } export const ViewLocationCard:FC = ({location, userSettings}) => { const { _id, name: locationName, yearMonth, bills, tenantName, tenantStreet, tenantTown, generateTenantCode, // NOTE: only the fileName is projected from the DB to reduce data transfer utilBillsProofOfPaymentAttachment } = location; const t = useTranslations("home-page.location-card"); const [isUploading, setIsUploading] = useState(false); const [uploadError, setUploadError] = useState(null); const [attachmentFilename, setAttachmentFilename] = useState(utilBillsProofOfPaymentAttachment?.fileName); const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; // Validate file type if (file.type !== 'application/pdf') { setUploadError('Only PDF files are accepted'); e.target.value = ''; // Reset input return; } setIsUploading(true); setUploadError(null); try { const formData = new FormData(); formData.append('utilBillsProofOfPaymentAttachment', file); const result = await uploadUtilBillsProofOfPayment(_id, formData); if (result.success) { setAttachmentFilename(file.name); } else { setUploadError(result.error || 'Upload failed'); } } catch (error: any) { setUploadError(error.message || 'Upload failed'); } finally { setIsUploading(false); e.target.value = ''; // Reset input } }; // sum all the billAmounts (only for bills billed to tenant) const monthlyExpense = bills.reduce((acc, bill) => (bill.paid && (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant) ? acc + (bill.payedAmount ?? 0) : acc, 0); const hub3a_text = useMemo(() => { if(!userSettings?.show2dCodeInMonthlyStatement || !generateTenantCode) { return ""; } const locationNameTrimmed_max20 = locationName.trimEnd().trimEnd().substring(0,19); const paymentParams:PaymentParams = { Iznos: (monthlyExpense/100).toFixed(2).replace(".",","), ImePlatitelja: tenantName ?? "", AdresaPlatitelja: tenantStreet ?? "", SjedistePlatitelja: tenantTown ?? "", Primatelj: userSettings?.ownerName ?? "", AdresaPrimatelja: userSettings?.ownerStreet ?? "", SjedistePrimatelja: userSettings?.ownerTown ?? "", IBAN: userSettings?.ownerIBAN ?? "", ModelPlacanja: "HR00", PozivNaBroj: formatYearMonth(yearMonth), SifraNamjene: "", OpisPlacanja: `Režije-${locationNameTrimmed_max20}-${formatYearMonth(yearMonth)}`, // max length 35 = "Režije-" (7) + locationName (20) + "-" (1) + "YYYY-MM" (7) }; return(EncodePayment(paymentParams)); }, [userSettings?.show2dCodeInMonthlyStatement, generateTenantCode, locationName, tenantName, tenantStreet, tenantTown, userSettings, monthlyExpense, yearMonth]); return(

{formatYearMonth(yearMonth)} {locationName}

{ bills.filter(bill => (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant).map(bill => ) }
{ monthlyExpense > 0 ?

{ t("payed-total-label") } {formatCurrency(monthlyExpense, userSettings?.currency)}

: null } { userSettings?.show2dCodeInMonthlyStatement && generateTenantCode ? <>

{t("payment-info-header")}

  • {t("payment-iban-label")}
    {paymentParams.IBAN}
  • {t("payment-recipient-label")}
    {paymentParams.Primatelj}
  • {t("payment-recipient-address-label")}
    {paymentParams.AdresaPrimatelja}
  • {t("payment-recipient-city-label")}
    {paymentParams.SjedistePrimatelja}
  • {t("payment-amount-label")}
    {paymentParams.Iznos}
  • {t("payment-description-label")}
    {paymentParams.OpisPlacanja}
  • {t("payment-model-label")}
    {paymentParams.ModelPlacanja}
  • {t("payment-reference-label")}
    {paymentParams.PozivNaBroj}
  • {t("payment-purpose-code-label")}
    {paymentParams.SifraNamjene}
: null }
{t("upload-proof-of-payment-legend")} {attachmentFilename ? (
{decodeURIComponent(attachmentFilename)}
) : (
{isUploading && ( )}
{uploadError && (

{uploadError}

)}
)}
); };