bharat_erp/lib/shared/models/asset_model.dart

194 lines
7.2 KiB
Dart

import 'package:freezed_annotation/freezed_annotation.dart';
part 'asset_model.freezed.dart';
part 'asset_model.g.dart';
String _idFromJson(Object? value) => value?.toString() ?? '';
int? _intFromJsonNullable(Object? value) {
if (value == null) return null;
if (value is int) return value;
if (value is num) return value.toInt();
return int.tryParse(value.toString());
}
double? _doubleFromJsonNullable(Object? value) {
if (value == null) return null;
if (value is double) return value;
if (value is num) return value.toDouble();
return double.tryParse(value.toString());
}
DateTime _dateFromJson(Object? value) {
if (value is DateTime) return value;
return DateTime.parse(value.toString());
}
DateTime? _dateFromJsonNullable(Object? value) {
if (value == null) return null;
if (value is DateTime) return value;
return DateTime.tryParse(value.toString());
}
Object? _readNestedName(Map<dynamic, dynamic> json, String nestedKey) {
final nested = json[nestedKey];
if (nested is Map) return nested['name'];
return null;
}
Object? _readAssetCategoryName(Map<dynamic, dynamic> json, String key) {
final flat = json['asset_category_name'];
if (flat is String && flat.isNotEmpty) return flat;
return _readNestedName(json, 'asset_category');
}
Object? _readPlantName(Map<dynamic, dynamic> json, String key) {
final flat = json['plant_name'];
if (flat is String && flat.isNotEmpty) return flat;
return _readNestedName(json, 'plant');
}
Object? _readAssetCategoryId(Map<dynamic, dynamic> json, String key) {
final flat = json['asset_category_id'];
if (flat != null) return flat;
final nested = json['asset_category'];
if (nested is Map) return nested['id'];
return null;
}
Object? _readPlantId(Map<dynamic, dynamic> json, String key) {
final flat = json['plant_id'];
if (flat != null) return flat;
final nested = json['plant'];
if (nested is Map) return nested['id'];
return null;
}
@freezed
class AssetCategoryModel with _$AssetCategoryModel {
const factory AssetCategoryModel({
@JsonKey(fromJson: _idFromJson) required String id,
required String code,
required String name,
@JsonKey(name: 'code_prefix') String? codePrefix,
@JsonKey(name: 'default_useful_life_years', fromJson: _intFromJsonNullable)
int? defaultUsefulLifeYears,
@JsonKey(name: 'default_depreciation_method') String? defaultDepreciationMethod,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
DateTime? createdAt,
DateTime? updatedAt,
}) = _AssetCategoryModel;
factory AssetCategoryModel.fromJson(Map<String, dynamic> json) =>
_$AssetCategoryModelFromJson(json);
}
@freezed
class AssetModel with _$AssetModel {
const factory AssetModel({
@JsonKey(fromJson: _idFromJson) required String id,
@JsonKey(name: 'asset_name') required String assetName,
@JsonKey(name: 'asset_code') String? assetCode,
@JsonKey(
name: 'asset_category_id',
readValue: _readAssetCategoryId,
fromJson: _intFromJsonNullable,
)
int? assetCategoryId,
@JsonKey(name: 'asset_category_name', readValue: _readAssetCategoryName)
String? assetCategoryName,
@JsonKey(name: 'plant_id', readValue: _readPlantId, fromJson: _intFromJsonNullable)
int? plantId,
@JsonKey(name: 'plant_name', readValue: _readPlantName) String? plantName,
@JsonKey(name: 'warranty_expiry_date', fromJson: _dateFromJsonNullable)
DateTime? warrantyExpiryDate,
@JsonKey(name: 'purchase_cost', fromJson: _doubleFromJsonNullable) double? purchaseCost,
String? status,
@JsonKey(name: 'is_active') @Default(true) bool isActive,
@JsonKey(name: 'created_at', fromJson: _dateFromJsonNullable) DateTime? createdAt,
@JsonKey(name: 'updated_at', fromJson: _dateFromJsonNullable) DateTime? updatedAt,
}) = _AssetModel;
factory AssetModel.fromJson(Map<String, dynamic> json) => _$AssetModelFromJson(json);
}
@freezed
class AmcContractModel with _$AmcContractModel {
const factory AmcContractModel({
@JsonKey(fromJson: _idFromJson) required String id,
@JsonKey(name: 'asset_id', fromJson: _idFromJson) String? assetId,
@JsonKey(name: 'vendor_id', fromJson: _intFromJsonNullable) int? vendorId,
@JsonKey(name: 'vendor_name') String? vendorName,
@JsonKey(name: 'contract_no') String? contractNo,
@JsonKey(name: 'contract_type') String? contractType,
@JsonKey(name: 'start_date', fromJson: _dateFromJson) required DateTime startDate,
@JsonKey(name: 'end_date', fromJson: _dateFromJson) required DateTime endDate,
@JsonKey(name: 'annual_cost', fromJson: _doubleFromJsonNullable) double? annualCost,
@Default('active') String status,
DateTime? createdAt,
}) = _AmcContractModel;
factory AmcContractModel.fromJson(Map<String, dynamic> json) =>
_$AmcContractModelFromJson(json);
}
@freezed
class ServiceVisitModel with _$ServiceVisitModel {
const factory ServiceVisitModel({
@JsonKey(fromJson: _idFromJson) required String id,
@JsonKey(name: 'asset_id', fromJson: _idFromJson) String? assetId,
@JsonKey(name: 'visit_type') required String visitType,
@JsonKey(name: 'visit_date', fromJson: _dateFromJson) required DateTime visitDate,
@JsonKey(name: 'complaint_no') String? complaintNo,
@JsonKey(name: 'work_done') String? workDone,
@JsonKey(name: 'next_service_date', fromJson: _dateFromJsonNullable)
DateTime? nextServiceDate,
@Default('COMPLETED') String status,
DateTime? createdAt,
}) = _ServiceVisitModel;
factory ServiceVisitModel.fromJson(Map<String, dynamic> json) =>
_$ServiceVisitModelFromJson(json);
}
@freezed
class InsurancePolicyModel with _$InsurancePolicyModel {
const factory InsurancePolicyModel({
@JsonKey(fromJson: _idFromJson) required String id,
@JsonKey(name: 'asset_id', fromJson: _idFromJson) String? assetId,
@JsonKey(name: 'policy_no') required String policyNo,
@JsonKey(name: 'insurer_name') required String insurerName,
@JsonKey(name: 'policy_type') String? policyType,
@JsonKey(name: 'sum_insured', fromJson: _doubleFromJsonNullable) double? sumInsured,
@JsonKey(name: 'policy_start_date', fromJson: _dateFromJson)
required DateTime policyStartDate,
@JsonKey(name: 'policy_end_date', fromJson: _dateFromJson) required DateTime policyEndDate,
@Default('active') String status,
DateTime? createdAt,
}) = _InsurancePolicyModel;
factory InsurancePolicyModel.fromJson(Map<String, dynamic> json) =>
_$InsurancePolicyModelFromJson(json);
}
@freezed
class AssetAlertModel with _$AssetAlertModel {
const factory AssetAlertModel({
@JsonKey(fromJson: _idFromJson) required String id,
@JsonKey(name: 'asset_id', fromJson: _idFromJson) String? assetId,
@JsonKey(name: 'asset_name') String? assetName,
@JsonKey(name: 'asset_code') String? assetCode,
String? type,
String? title,
String? message,
@JsonKey(name: 'expiry_date', fromJson: _dateFromJsonNullable) DateTime? expiryDate,
@JsonKey(name: 'due_date', fromJson: _dateFromJsonNullable) DateTime? dueDate,
@JsonKey(name: 'days_remaining', fromJson: _intFromJsonNullable) int? daysRemaining,
String? status,
@JsonKey(name: 'plant_name') String? plantName,
}) = _AssetAlertModel;
factory AssetAlertModel.fromJson(Map<String, dynamic> json) =>
_$AssetAlertModelFromJson(json);
}