Files
kulakpos_app/lib/model/balance_sheet_model.dart

73 lines
2.2 KiB
Dart
Raw Permalink Normal View History

2026-02-07 15:57:09 +07:00
class BalanceSheetModel {
final num? totalAsset;
final List<AssetData>? data;
BalanceSheetModel({this.totalAsset, this.data});
factory BalanceSheetModel.fromJson(Map<String, dynamic> json) {
return BalanceSheetModel(
2026-04-23 21:21:33 +07:00
totalAsset: num.tryParse(json["total_asset"].toString()),
2026-02-07 15:57:09 +07:00
data: json["asset_datas"] == null
? []
: List<AssetData>.from(json["asset_datas"]!.map((x) => AssetData.fromJson(x))),
);
}
}
class AssetData {
final int? id;
final String? name;
final num? amount;
AssetData({this.id, this.name, this.amount});
factory AssetData.fromJson(Map<String, dynamic> json) {
final _source = json["source"]?.toString().trim().toLowerCase();
final _productType = json["product_type"]?.toString().trim().toLowerCase();
final String? _name = switch (_source) {
"product" => json["productName"],
"bank" => json["meta"]?["bank_name"] ?? "Bank Account",
_ => null,
};
num _amount = 0;
if (_source == "product") {
if (_productType == "single" || _productType == "variation") {
final stocks = json["stocks"];
if (stocks is List && stocks.isNotEmpty) {
final _firstStock = stocks.first as Map<String, dynamic>;
2026-04-23 21:21:33 +07:00
final _purchasePrice = num.tryParse(_firstStock["productPurchasePrice"].toString()) ?? 0;
final _stockQty = num.tryParse(_firstStock["productStock"].toString()) ?? 0;
2026-02-07 15:57:09 +07:00
_amount = _purchasePrice * _stockQty;
}
} else if (_productType == "combo") {
final comboProducts = json["combo_products"];
if (comboProducts is List) {
_amount = comboProducts.fold<num>(0, (sum, item) {
if (item is Map<String, dynamic>) {
2026-04-23 21:21:33 +07:00
final _price = num.tryParse(item["purchase_price"].toString()) ?? 0;
final _qty = num.tryParse(item["quantity"].toString()) ?? 0;
2026-02-07 15:57:09 +07:00
return sum + (_price * _qty);
}
return sum;
});
}
}
} else if (_source == "bank") {
2026-04-23 21:21:33 +07:00
_amount = num.tryParse(json["balance"].toString()) ?? 0;
2026-02-07 15:57:09 +07:00
}
return AssetData(
id: json["id"],
name: _name,
amount: _amount,
);
}
}