148 lines
4.0 KiB
Dart
148 lines
4.0 KiB
Dart
import 'package:fcs/domain/entities/cargo_type.dart';
|
|
import 'package:fcs/domain/entities/carton.dart';
|
|
import 'package:fcs/domain/entities/custom_duty.dart';
|
|
import 'package:fcs/domain/entities/discount.dart';
|
|
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
|
import 'package:fcs/domain/entities/payment_method.dart';
|
|
import 'package:fcs/domain/entities/rate.dart';
|
|
import 'package:fcs/domain/entities/shipment.dart';
|
|
|
|
class Invoice {
|
|
String id;
|
|
String invoiceNumber;
|
|
DateTime invoiceDate;
|
|
String fcsShipmentID;
|
|
String userID;
|
|
String userName;
|
|
String phoneNumber;
|
|
String status;
|
|
|
|
double handlingFee;
|
|
double deliveryFee;
|
|
double paidAmount;
|
|
double amount;
|
|
|
|
List<CustomDuty> customDuties;
|
|
List<Carton> cartons;
|
|
List<CargoType> cargoTypes;
|
|
List<Shipment> shipments;
|
|
Discount discount;
|
|
PaymentMethod paymentMethod;
|
|
|
|
List<CargoType> getCargoTypes(Rate rate) {
|
|
List<CargoType> cargoTypes = [];
|
|
double totalCalWeight = 0;
|
|
cartons.forEach((carton) {
|
|
if (carton.isChecked) {
|
|
var _cartonsTypes =
|
|
carton.getCargoTypeForCalWeight(rate.volumetricRatio);
|
|
_cartonsTypes.forEach((ct) {
|
|
if (cargoTypes.contains(ct)) {
|
|
CargoType existing = cargoTypes.firstWhere((wc) => wc.id == ct.id);
|
|
existing.calWeight += ct.calWeight;
|
|
} else {
|
|
cargoTypes.add(ct.clone());
|
|
}
|
|
totalCalWeight += ct.calWeight;
|
|
});
|
|
}
|
|
});
|
|
|
|
DiscountByWeight discountByWeight =
|
|
rate.getDiscountByWeight(totalCalWeight);
|
|
|
|
cargoTypes.forEach((e) {
|
|
double r =
|
|
e.rate - (discountByWeight != null ? discountByWeight.discount : 0);
|
|
double amount = e.calWeight * r;
|
|
e.calRate = r;
|
|
e.amount = amount;
|
|
});
|
|
return cargoTypes;
|
|
}
|
|
|
|
double getTotal(Rate rate) {
|
|
List<CargoType> cargoTypes = getCargoTypes(rate);
|
|
var total = cargoTypes.fold(0.0, (p, c) => c.amount + p);
|
|
return total;
|
|
}
|
|
|
|
double getNetAmount(Rate rate) {
|
|
List<CargoType> cargoTypes = getCargoTypes(rate);
|
|
var total = cargoTypes.fold(0.0, (p, c) => c.amount + p);
|
|
total += getCustomFee();
|
|
total += getDeliveryFee();
|
|
total += getHandlingFee();
|
|
total -= getDiscount();
|
|
return total;
|
|
}
|
|
|
|
double getHandlingFee() {
|
|
return shipments?.where((sh) => sh.isSelected)?.fold(0, (p, s) {
|
|
return p + (s?.handlingFee ?? 0) - (s?.paidHandlingFee ?? 0);
|
|
});
|
|
}
|
|
|
|
double getTotalBalance(Rate rate) {
|
|
return getNetAmount(rate) - (paidAmount ?? 0);
|
|
}
|
|
|
|
double getCustomFee() {
|
|
return customDuties == null ? 0 : customDuties.fold(0, (p, d) => p + d.fee);
|
|
}
|
|
|
|
double getDeliveryFee() {
|
|
return deliveryFee == null ? 0 : deliveryFee;
|
|
}
|
|
|
|
double getDiscount() => discount == null ? 0 : discount.amount;
|
|
|
|
Invoice({
|
|
this.id,
|
|
this.invoiceNumber,
|
|
this.invoiceDate,
|
|
this.userName,
|
|
this.phoneNumber,
|
|
this.amount,
|
|
this.discount,
|
|
this.status,
|
|
this.customDuties,
|
|
this.cartons,
|
|
this.cargoTypes,
|
|
this.handlingFee,
|
|
this.fcsShipmentID,
|
|
this.shipments,
|
|
});
|
|
|
|
factory Invoice.fromMap(Map<String, dynamic> map, String docID) {
|
|
return Invoice(
|
|
id: docID,
|
|
invoiceNumber: map['invoice_number'],
|
|
invoiceDate: map['invoice_date'],
|
|
userName: map['user_name'],
|
|
phoneNumber: map['phone_number'],
|
|
amount: map['amount'],
|
|
status: map['status'],
|
|
discount: map['discount'],
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList();
|
|
List _customDuties = customDuties?.map((c) => c.toMap())?.toList();
|
|
List _cartons = cartons?.map((c) => c.toMap())?.toList() ?? [];
|
|
return {
|
|
"id": id,
|
|
"invoice_date": invoiceDate,
|
|
"user_id": userID,
|
|
'fcs_shipment_id': fcsShipmentID,
|
|
'cargo_types': _cargoTypes,
|
|
'custom_duties': _customDuties,
|
|
'cartons': _cartons,
|
|
'discount': discount?.toMap(),
|
|
'handling_fee': handlingFee,
|
|
'delivery_fee': deliveryFee,
|
|
};
|
|
}
|
|
}
|