(Fix): Fixed the excel reporting system, fixed alot of hardcoded dropdown values in activities, auto selection of departments is also implemented

This commit is contained in:
2025-12-18 10:11:22 +00:00
parent 2a38cf372a
commit 01400ad4e1
10 changed files with 568 additions and 117 deletions

View File

@@ -6,6 +6,7 @@ import { Button } from '../components/ui/Button';
import { Input, Select } from '../components/ui/Input';
import { api } from '../services/api';
import { useDepartments, useSubDepartments } from '../hooks/useDepartments';
import { useActivities } from '../hooks/useActivities';
import { useAuth } from '../contexts/AuthContext';
export const RatesPage: React.FC = () => {
@@ -16,7 +17,6 @@ export const RatesPage: React.FC = () => {
const [contractors, setContractors] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
// Form state
const [formData, setFormData] = useState({
contractorId: '',
@@ -27,6 +27,7 @@ export const RatesPage: React.FC = () => {
});
const [selectedDept, setSelectedDept] = useState('');
const { subDepartments } = useSubDepartments(selectedDept);
const { activities } = useActivities(formData.subDepartmentId);
const [formError, setFormError] = useState('');
const [formLoading, setFormLoading] = useState(false);
const [searchQuery, setSearchQuery] = useState('');
@@ -72,7 +73,24 @@ export const RatesPage: React.FC = () => {
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
const { name, value } = e.target;
setFormData(prev => ({ ...prev, [name]: value }));
// Auto-select department when contractor is selected
if (name === 'contractorId' && value) {
const selectedContractor = contractors.find(c => String(c.id) === value);
if (selectedContractor?.department_id) {
setSelectedDept(String(selectedContractor.department_id));
// Clear sub-department and activity when contractor changes
setFormData(prev => ({ ...prev, [name]: value, subDepartmentId: '', activity: '' }));
} else {
setFormData(prev => ({ ...prev, [name]: value }));
}
}
// Clear activity when sub-department changes
else if (name === 'subDepartmentId') {
setFormData(prev => ({ ...prev, [name]: value, activity: '' }));
} else {
setFormData(prev => ({ ...prev, [name]: value }));
}
setFormError('');
};
@@ -239,7 +257,7 @@ export const RatesPage: React.FC = () => {
<TableCell>{rate.sub_department_name || '-'}</TableCell>
<TableCell>
<span className={`px-2 py-1 rounded text-xs font-medium ${
rate.activity === 'Loading' || rate.activity === 'Unloading'
rate.unit_of_measurement === 'Per Bag'
? 'bg-blue-100 text-blue-700'
: 'bg-gray-100 text-gray-700'
}`}>
@@ -248,7 +266,7 @@ export const RatesPage: React.FC = () => {
</TableCell>
<TableCell>
<span className="text-xs text-gray-500">
{rate.activity === 'Loading' || rate.activity === 'Unloading'
{rate.unit_of_measurement === 'Per Bag'
? 'Per Unit'
: 'Flat Rate'}
</span>
@@ -302,8 +320,8 @@ export const RatesPage: React.FC = () => {
<div className="p-4 bg-blue-50 border border-blue-200 rounded-md">
<h4 className="font-medium text-blue-800 mb-2">Rate Calculation Info</h4>
<ul className="text-sm text-blue-700 space-y-1">
<li><strong>Loading/Unloading:</strong> Total = Units × Rate per Unit</li>
<li><strong>Standard/Other:</strong> Total = Flat Rate (no unit calculation)</li>
<li><strong>Per Bag Activities:</strong> Total = Units × Rate per Unit</li>
<li><strong>Fixed Rate Activities:</strong> Total = Flat Rate (no unit calculation)</li>
</ul>
</div>
@@ -359,18 +377,22 @@ export const RatesPage: React.FC = () => {
name="activity"
value={formData.activity}
onChange={handleInputChange}
disabled={!formData.subDepartmentId}
options={[
{ value: '', label: 'Select Activity (Optional)' },
{ value: 'Loading', label: 'Loading (per unit × rate)' },
{ value: 'Unloading', label: 'Unloading (per unit × rate)' },
{ value: 'Standard', label: 'Standard Work (flat rate)' },
{ value: 'Other', label: 'Other (flat rate)' },
{ value: '', label: formData.subDepartmentId ? 'Select Activity (Optional)' : 'Select Sub-Department First' },
...activities.map(a => ({
value: a.name,
label: `${a.name} (${a.unit_of_measurement === 'Per Bag' ? 'per unit × rate' : 'flat rate'})`
}))
]}
/>
<Input
label={formData.activity === 'Loading' || formData.activity === 'Unloading'
? "Rate per Unit (₹)"
: "Standard Rate (₹)"}
label={(() => {
const selectedActivity = activities.find(a => a.name === formData.activity);
return selectedActivity?.unit_of_measurement === 'Per Bag'
? "Rate per Unit (₹)"
: "Rate Amount (₹)";
})()}
name="rate"
type="number"
value={formData.rate}