From 03cf1cf98dc89c448a6129a6b1909f4eac34f186 Mon Sep 17 00:00:00 2001 From: Duradundi Hadimani Date: Fri, 14 Nov 2025 17:05:28 +0530 Subject: [PATCH] Latest Changes --- src/pages/AssetDetail.tsx | 301 ++++++++++++++++++++++++++++++++++- src/services/assetService.ts | 10 ++ 2 files changed, 305 insertions(+), 6 deletions(-) diff --git a/src/pages/AssetDetail.tsx b/src/pages/AssetDetail.tsx index 1eb9f87..fc9d956 100644 --- a/src/pages/AssetDetail.tsx +++ b/src/pages/AssetDetail.tsx @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react'; import { useParams, useNavigate, useSearchParams } from 'react-router-dom'; import { useAssetDetails, useAssetMutations } from '../hooks/useAsset'; import { FaArrowLeft, FaSave, FaEdit, FaQrcode } from 'react-icons/fa'; -import type { CreateAssetData } from '../services/assetService'; +import type { CreateAssetData, AssetFinanceBookRow } from '../services/assetService'; import LinkField from '../components/LinkField'; import apiService from '../services/apiService'; // ✅ your ApiService @@ -51,6 +51,15 @@ const AssetDetail: React.FC = () => { available_for_use_date: isNewAsset ? new Date().toISOString().split('T')[0] : undefined }); + const emptyFinanceRow = { + finance_book: "", + depreciation_method: "", + total_number_of_depreciations: 0, + frequency_of_depreciation: "", + depreciation_start_date: "", + }; + + // Load user details on mount useEffect(() => { async function loadUserDetails() { @@ -65,6 +74,38 @@ const AssetDetail: React.FC = () => { loadUserDetails(); }, []); + const addFinanceRow = () => { + setFormData(prev => ({ + ...prev, + finance_books: [ + ...(prev.finance_books || []), + { + finance_book: '', + depreciation_method: '', + total_number_of_depreciations: 0, + frequency_of_depreciation: '', + depreciation_start_date: '' + } + ] + })); + }; + + const removeFinanceRow = (index: number) => { + setFormData(prev => { + const rows = [...(prev.finance_books || [])]; + rows.splice(index, 1); + return { ...prev, finance_books: rows }; + }); + }; + + const updateFinanceRow = (index: number, patch: Partial) => { + setFormData(prev => { + const rows = [...(prev.finance_books || [])]; + rows[index] = { ...(rows[index] || {}), ...patch }; + return { ...prev, finance_books: rows }; + }); + }; + // Update department filters when company or userSiteName changes useEffect(() => { const filters: Record = {}; @@ -436,7 +477,8 @@ const AssetDetail: React.FC = () => { -
+
+ {/*
*/} @@ -452,6 +494,22 @@ const AssetDetail: React.FC = () => {

)}
+
+ + +
@@ -619,7 +677,7 @@ const AssetDetail: React.FC = () => { disabled={!isEditing} /> -
+ {/*
@@ -632,9 +690,9 @@ const AssetDetail: React.FC = () => { disabled={!isEditing} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 dark:disabled:bg-gray-700 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" /> -
+
*/} -
+ {/*
@@ -644,7 +702,7 @@ const AssetDetail: React.FC = () => { disabled={!isEditing} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 dark:disabled:bg-gray-700 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" /> -
+
*/}
+ {/* Asset Finance Book child table — shown only when checkbox checked */} + {formData.calculate_depreciation && ( +
+ + {/* Header with Add Row button */} +
+

+ Asset Finance Book +

+ + {isEditing && ( + + )} +
+ + {/* TABLE HEADER */} +
+
Finance Book
+
Depreciation Method
+
Total Depreciations
+
Frequency
+
Start Date
+
Action
+
+ + {/* TABLE ROWS */} +
+ + {(formData.finance_books || []).map((row: AssetFinanceBookRow, idx: number) => ( +
+ {/* Finance Book */} + updateFinanceRow(idx, { finance_book: e.target.value })} + disabled={!isEditing} + className="w-full px-2 py-1 border rounded-md dark:bg-gray-700 dark:text-white" + /> + + {/* Depreciation Method */} + + + {/* Total Depreciations */} + + updateFinanceRow(idx, { + total_number_of_depreciations: Number(e.target.value), + }) + } + disabled={!isEditing} + className="w-full px-2 py-1 border rounded-md dark:bg-gray-700 dark:text-white" + /> + + {/* Frequency */} + + updateFinanceRow(idx, { frequency_of_depreciation: e.target.value }) + } + disabled={!isEditing} + className="w-full px-2 py-1 border rounded-md dark:bg-gray-700 dark:text-white" + /> + + {/* Start Date */} + + updateFinanceRow(idx, { depreciation_start_date: e.target.value }) + } + disabled={!isEditing} + className="w-full px-2 py-1 border rounded-md dark:bg-gray-700 dark:text-white" + /> + + {/* REMOVE BUTTON */} + {isEditing && ( +
+ +
+ )} +
+ ))} + +
+
+ )} + + {/* {formData.calculate_depreciation && ( +
+
+

Asset Finance Book

+ {isEditing && ( + + )} +
+ +
+ {(formData.finance_books || []).length === 0 && ( +
No finance book rows added.
+ )} + + + {(formData.finance_books || []).map((row: AssetFinanceBookRow, idx: number) => ( +
+
+ +
+ + updateFinanceRow(idx, { finance_book: e.target.value })} + disabled={!isEditing} + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" + /> +
+ + +
+ + +
+ + +
+ + updateFinanceRow(idx, { total_number_of_depreciations: Number(e.target.value) })} + disabled={!isEditing} + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" + /> +
+ + +
+ + updateFinanceRow(idx, { frequency_of_depreciation: e.target.value })} + disabled={!isEditing} + placeholder="e.g. Monthly, Yearly" + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" + /> +
+ + +
+ + updateFinanceRow(idx, { depreciation_start_date: e.target.value })} + disabled={!isEditing} + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-white" + /> +
+
+ + {/* Row actions */} + {/* {isEditing && ( +
+ +
+ )} +
+ ))} +
+
+ )} */} + diff --git a/src/services/assetService.ts b/src/services/assetService.ts index 5aeab11..a56833d 100644 --- a/src/services/assetService.ts +++ b/src/services/assetService.ts @@ -67,6 +67,15 @@ export interface AssetStats { total_amount: number; } +// Add child row type +export interface AssetFinanceBookRow { + finance_book?: string; + depreciation_method?: string; + total_number_of_depreciations?: number; + frequency_of_depreciation?: string; + depreciation_start_date?: string; // YYYY-MM-DD +} + export interface CreateAssetData { asset_name: string; company: string; @@ -87,6 +96,7 @@ export interface CreateAssetData { custom_site_contractor?: string; custom_total_amount?: number; calculate_depreciation?: boolean; + finance_books?: AssetFinanceBookRow[]; [key: string]: any; }