Files
Kifi/kifi-app/lib/features/inventory/domain/product.dart

217 lines
7.6 KiB
Dart

class Product {
final int? id;
final int? userId;
final int? categoryId;
final int? uomId;
final String? hsnCode;
final String name;
final String? sku;
final String? barcode;
final String? description;
final double? mrp;
final double? sellingPrice;
final double? gstRate;
final String? dimensions;
final String? color;
final String? size;
final double? itemLength;
final double? itemWidth;
final double? itemHeight;
final String? dimensionUom;
final double? packageLength;
final double? packageWidth;
final double? packageHeight;
final String? packageDimensionUom;
final String? manufacturerCode;
final String? material;
final String? brandName;
final String? countryOfOrigin;
final double? weightInG;
final double? packageWeightInG;
final double? volumetricWeightInKg;
final String priceCalcRule;
final bool autoCalculatePrice;
final double? purityFactor;
final double? makingCharges;
final String? makingChargesType;
final double? wastagePercentage;
final double? minStock;
final double? reorderLevel;
final double? reorderQuantity;
final bool trackInventory;
final bool isActive;
final List<int> imageIds;
final double? currentStock;
Product({
this.id,
this.userId,
this.categoryId,
this.uomId,
required this.name,
this.hsnCode,
this.sku,
this.barcode,
this.description,
this.mrp,
this.sellingPrice,
this.gstRate,
this.dimensions,
this.color,
this.size,
this.itemLength,
this.itemWidth,
this.itemHeight,
this.dimensionUom = 'cm',
this.packageLength,
this.packageWidth,
this.packageHeight,
this.packageDimensionUom = 'cm',
this.manufacturerCode,
this.material,
this.brandName,
this.countryOfOrigin,
this.weightInG,
this.packageWeightInG,
this.volumetricWeightInKg,
this.priceCalcRule = 'MANUAL',
this.autoCalculatePrice = false,
this.purityFactor,
this.makingCharges = 0.0,
this.makingChargesType = 'FLAT',
this.wastagePercentage = 0.0,
this.minStock = 0.0,
this.reorderLevel = 0.0,
this.reorderQuantity = 0.0,
this.trackInventory = true,
this.isActive = true,
this.imageIds = const [],
this.currentStock = 0.0,
});
bool get isOutOfStock => trackInventory && (currentStock == null || currentStock! <= 0);
bool get isLowStock {
final alertThreshold = (reorderLevel != null && reorderLevel! > 0)
? reorderLevel!
: (minStock != null && minStock! > 0 ? minStock! : 0.0);
if (!trackInventory || alertThreshold <= 0) return false;
return currentStock != null && currentStock! > 0 && currentStock! <= alertThreshold;
}
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
userId: json['userId'] ?? json['user_id'],
categoryId: json['categoryId'] ?? json['category_id'],
uomId: json['uomId'] ?? json['uom_id'],
name: json['name'] ?? '',
hsnCode: json['hsnCode'] ?? json['hsn_code'],
sku: json['sku'],
barcode: json['barcode'],
description: json['description'],
mrp: json['mrp'] != null
? double.tryParse(json['mrp'].toString())
: (json['mrp_price'] != null ? double.tryParse(json['mrp_price'].toString()) : null),
sellingPrice: json['sellingPrice'] != null
? double.tryParse(json['sellingPrice'].toString())
: (json['selling_price'] != null ? double.tryParse(json['selling_price'].toString()) : null),
gstRate: (json['gstRate'] ?? json['gst_rate'] as num?)?.toDouble(),
dimensions: json['dimensions'],
color: json['color'],
size: json['size'],
itemLength: (json['itemLength'] ?? json['item_length'] as num?)?.toDouble(),
itemWidth: (json['itemWidth'] ?? json['item_width'] as num?)?.toDouble(),
itemHeight: (json['itemHeight'] ?? json['item_height'] as num?)?.toDouble(),
dimensionUom: json['dimensionUom'] ?? json['dimension_uom'] ?? 'cm',
packageLength: (json['packageLength'] ?? json['package_length'] as num?)?.toDouble(),
packageWidth: (json['packageWidth'] ?? json['package_width'] as num?)?.toDouble(),
packageHeight: (json['packageHeight'] ?? json['package_height'] as num?)?.toDouble(),
packageDimensionUom: json['packageDimensionUom'] ?? json['package_dimension_uom'] ?? 'cm',
manufacturerCode: json['manufacturerCode'] ?? json['manufacturer_code'],
material: json['material'],
brandName: json['brandName'] ?? json['brand_name'],
countryOfOrigin: json['countryOfOrigin'] ?? json['country_of_origin'],
weightInG: (json['weightInG'] ?? json['weight_in_g'] as num?)?.toDouble(),
packageWeightInG: (json['packageWeightInG'] ?? json['package_weight_in_g'] as num?)?.toDouble(),
volumetricWeightInKg: (json['volumetricWeightInKg'] ?? json['volumetric_weight_in_kg'] as num?)?.toDouble(),
priceCalcRule: json['priceCalcRule'] ?? json['price_calc_rule'] ?? 'MANUAL',
autoCalculatePrice: json['autoCalculatePrice'] ?? json['auto_calculate_price'] ?? false,
purityFactor: (json['purityFactor'] ?? json['purity_factor'] as num?)?.toDouble(),
makingCharges: (json['makingCharges'] ?? json['making_charges'] as num?)?.toDouble() ?? 0.0,
makingChargesType: json['makingChargesType'] ?? json['making_charges_type'] ?? 'FLAT',
wastagePercentage: (json['wastagePercentage'] ?? json['wastage_percentage'] as num?)?.toDouble() ?? 0.0,
minStock: (json['minStock'] ?? json['min_stock'] as num?)?.toDouble() ?? 0.0,
reorderLevel: (json['reorderLevel'] ?? json['reorder_level'] as num?)?.toDouble() ?? 0.0,
reorderQuantity: (json['reorderQuantity'] ?? json['reorder_quantity'] as num?)?.toDouble() ?? 0.0,
trackInventory: json['trackInventory'] ?? json['track_inventory'] ?? true,
isActive: json['isActive'] ?? json['is_active'] ?? true,
imageIds: json['images'] != null
? (json['images'] as List).map((i) {
if (i is Map) {
return (i['id'] as num).toInt();
} else if (i is num) {
return i.toInt();
}
return 0;
}).where((id) => id > 0).toList()
: [],
currentStock: (json['currentStock'] ?? json['current_stock'] as num?)?.toDouble() ?? 0.0,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'userId': userId,
'categoryId': categoryId,
'uomId': uomId,
'name': name,
'hsnCode': hsnCode,
'sku': sku,
'barcode': barcode,
'description': description,
'mrp': mrp,
'sellingPrice': sellingPrice,
'gstRate': gstRate,
'dimensions': dimensions,
'color': color,
'size': size,
'itemLength': itemLength,
'itemWidth': itemWidth,
'itemHeight': itemHeight,
'dimensionUom': dimensionUom,
'packageLength': packageLength,
'packageWidth': packageWidth,
'packageHeight': packageHeight,
'packageDimensionUom': packageDimensionUom,
'manufacturerCode': manufacturerCode,
'material': material,
'brandName': brandName,
'countryOfOrigin': countryOfOrigin,
'weightInG': weightInG,
'packageWeightInG': packageWeightInG,
'volumetricWeightInKg': volumetricWeightInKg,
'priceCalcRule': priceCalcRule,
'autoCalculatePrice': autoCalculatePrice,
'purityFactor': purityFactor,
'makingCharges': makingCharges,
'makingChargesType': makingChargesType,
'wastagePercentage': wastagePercentage,
'minStock': minStock,
'reorderLevel': reorderLevel,
'reorderQuantity': reorderQuantity,
'trackInventory': trackInventory,
'isActive': isActive,
};
}
}