2020-10-22 04:14:53 +06:30
|
|
|
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/rate.dart';
|
|
|
|
|
|
2020-06-02 14:56:51 +06:30
|
|
|
import 'package.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'receipt.dart';
|
2020-06-02 14:56:51 +06:30
|
|
|
|
|
|
|
|
class Invoice {
|
2020-10-16 13:46:02 +06:30
|
|
|
String id;
|
2020-06-02 14:56:51 +06:30
|
|
|
String invoiceNumber;
|
|
|
|
|
DateTime invoiceDate;
|
|
|
|
|
String customerName;
|
|
|
|
|
String customerPhoneNumber;
|
|
|
|
|
double amount;
|
|
|
|
|
String status;
|
|
|
|
|
String paymentAttachment;
|
2020-10-22 04:14:53 +06:30
|
|
|
double handlingFee;
|
|
|
|
|
double deliveryFee;
|
|
|
|
|
double paidAmount;
|
2020-06-02 14:56:51 +06:30
|
|
|
|
|
|
|
|
List<Package> packages;
|
2020-06-24 16:06:15 +06:30
|
|
|
List<Receipt> receipts;
|
|
|
|
|
List<String> receiptPhotos;
|
2020-10-22 04:14:53 +06:30
|
|
|
List<CustomDuty> customDuties;
|
|
|
|
|
List<Carton> cartons;
|
|
|
|
|
Discount discount;
|
|
|
|
|
|
|
|
|
|
List<CargoType> getCargoTypes(Rate rate) {
|
|
|
|
|
List<CargoType> cargoTypes = [];
|
|
|
|
|
double actualWeight = 0;
|
|
|
|
|
double shipmentWeight = 0;
|
|
|
|
|
cartons.forEach((c) {
|
|
|
|
|
c.cargoTypes.forEach((tc) {
|
|
|
|
|
if (cargoTypes.contains(tc)) {
|
|
|
|
|
CargoType existing = cargoTypes.firstWhere((wc) => wc.id == tc.id);
|
|
|
|
|
existing.weight += tc.weight;
|
|
|
|
|
} else {
|
2020-10-22 05:27:03 +06:30
|
|
|
cargoTypes.add(tc.clone());
|
2020-10-22 04:14:53 +06:30
|
|
|
}
|
|
|
|
|
actualWeight += tc.weight;
|
|
|
|
|
});
|
|
|
|
|
double volume = (c.length ?? 0) * (c.width ?? 0) * (c.height ?? 0);
|
|
|
|
|
double sw = volume / rate.volumetricRatio ?? 0;
|
|
|
|
|
shipmentWeight += sw;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
DiscountByWeight discountByWeight = rate.getDiscountByWeight(
|
|
|
|
|
shipmentWeight > actualWeight ? shipmentWeight : actualWeight);
|
|
|
|
|
|
|
|
|
|
cargoTypes.forEach((e) {
|
|
|
|
|
print(actualWeight > shipmentWeight);
|
|
|
|
|
double cargoWeight = actualWeight > shipmentWeight
|
|
|
|
|
? e.weight
|
|
|
|
|
: e.weight / actualWeight * shipmentWeight;
|
|
|
|
|
double r =
|
|
|
|
|
e.rate - (discountByWeight != null ? discountByWeight.discount : 0);
|
|
|
|
|
double amount = cargoWeight * r;
|
|
|
|
|
e.calRate = r;
|
|
|
|
|
e.calWeight = cargoWeight;
|
|
|
|
|
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 getTotalBalance(Rate rate) {
|
|
|
|
|
return getNetAmount(rate) - (paidAmount ?? 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getCustomFee() {
|
|
|
|
|
return customDuties == null ? 0 : customDuties.fold(0, (p, d) => p + d.fee);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getHandlingFee() {
|
|
|
|
|
return handlingFee == null ? 0 : handlingFee;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getDeliveryFee() {
|
|
|
|
|
return deliveryFee == null ? 0 : deliveryFee;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double getDiscount() => discount == null ? 0 : discount.amount;
|
2020-06-24 16:06:15 +06:30
|
|
|
|
2020-06-02 14:56:51 +06:30
|
|
|
Invoice(
|
2020-10-16 13:46:02 +06:30
|
|
|
{this.id,
|
|
|
|
|
this.invoiceNumber,
|
2020-06-02 14:56:51 +06:30
|
|
|
this.invoiceDate,
|
|
|
|
|
this.customerName,
|
|
|
|
|
this.customerPhoneNumber,
|
|
|
|
|
this.amount,
|
|
|
|
|
this.discount,
|
|
|
|
|
this.status,
|
|
|
|
|
this.paymentAttachment,
|
2020-06-24 16:06:15 +06:30
|
|
|
this.packages,
|
|
|
|
|
this.receiptPhotos,
|
2020-10-22 04:14:53 +06:30
|
|
|
this.customDuties,
|
|
|
|
|
this.cartons,
|
|
|
|
|
this.handlingFee,
|
2020-06-24 16:06:15 +06:30
|
|
|
this.receipts});
|
2020-06-03 00:42:31 +06:30
|
|
|
|
|
|
|
|
double get getAmount => packages.fold(0, (p, e) => (e.rate * e.weight) + p);
|
2020-10-16 13:46:02 +06:30
|
|
|
|
|
|
|
|
factory Invoice.fromMap(Map<String, dynamic> map, String docID) {
|
|
|
|
|
return Invoice(
|
|
|
|
|
id: docID,
|
|
|
|
|
invoiceNumber: map['invoice_number'],
|
|
|
|
|
invoiceDate: map['invoice_date'],
|
|
|
|
|
customerName: map['customer_name'],
|
|
|
|
|
customerPhoneNumber: map['phone_number'],
|
|
|
|
|
amount: map['amount'],
|
|
|
|
|
status: map['status'],
|
|
|
|
|
discount: map['discount'],
|
|
|
|
|
paymentAttachment: map['payment_attachment'],
|
|
|
|
|
packages: map['packages'],
|
|
|
|
|
receiptPhotos: map['receiptPhotos'],
|
|
|
|
|
receipts: map['receipts'],
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-06-02 14:56:51 +06:30
|
|
|
}
|