Files
fcs/lib/domain/entities/invoice.dart

201 lines
6.3 KiB
Dart
Raw Normal View History

2020-10-26 04:41:24 +06:30
import 'package:cloud_firestore/cloud_firestore.dart';
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';
2020-10-28 05:11:06 +06:30
import 'package:fcs/domain/entities/payment.dart';
2020-10-24 06:14:07 +06:30
import 'package:fcs/domain/entities/payment_method.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/domain/entities/rate.dart';
2020-10-24 06:14:07 +06:30
import 'package:fcs/domain/entities/shipment.dart';
2020-06-02 14:56:51 +06:30
class Invoice {
2021-09-10 14:27:38 +06:30
String? id;
String? invoiceNumber;
DateTime? invoiceDate;
String? fcsShipmentID;
String? userID;
String? fcsID;
String? userName;
String? phoneNumber;
String? status;
2020-10-24 06:14:07 +06:30
2020-10-22 04:14:53 +06:30
double handlingFee;
double deliveryFee;
double paidAmount;
2020-10-24 06:14:07 +06:30
double amount;
2020-06-02 14:56:51 +06:30
2020-10-22 04:14:53 +06:30
List<CustomDuty> customDuties;
List<Carton> cartons;
2020-10-24 06:14:07 +06:30
List<CargoType> cargoTypes;
2021-09-10 17:14:59 +06:30
List<Shipment?>? shipments;
2020-10-28 05:11:06 +06:30
List<Payment> payments;
2021-09-10 14:27:38 +06:30
Discount? discount;
PaymentMethod? paymentMethod;
String? invoiceURL;
2020-10-22 04:14:53 +06:30
List<CargoType> getCargoTypes(Rate rate) {
2024-01-23 16:28:08 +06:30
if (cargoTypes.isNotEmpty) return cargoTypes;
2020-10-26 04:41:24 +06:30
List<CargoType> _cargoTypes = [];
2020-10-24 06:14:07 +06:30
double totalCalWeight = 0;
cartons.forEach((carton) {
2021-09-10 14:27:38 +06:30
if (carton.isChecked ?? false) {
2020-10-24 06:14:07 +06:30
var _cartonsTypes =
carton.getCargoTypeForCalWeight(rate.volumetricRatio);
_cartonsTypes.forEach((ct) {
2020-10-26 04:41:24 +06:30
if (_cargoTypes.contains(ct)) {
CargoType existing = _cargoTypes.firstWhere((wc) => wc.id == ct.id);
2020-10-24 06:14:07 +06:30
existing.calWeight += ct.calWeight;
} else {
2020-10-26 04:41:24 +06:30
_cargoTypes.add(ct.clone());
2020-10-24 06:14:07 +06:30
}
totalCalWeight += ct.calWeight;
});
}
2020-10-22 04:14:53 +06:30
});
2020-10-24 06:14:07 +06:30
DiscountByWeight discountByWeight =
rate.getDiscountByWeight(totalCalWeight);
2020-10-22 04:14:53 +06:30
2020-10-26 04:41:24 +06:30
_cargoTypes.forEach((e) {
2024-01-23 16:28:08 +06:30
double r = e.rate - discountByWeight.discount;
2020-10-22 04:14:53 +06:30
e.calRate = r;
});
2020-10-26 04:41:24 +06:30
return _cargoTypes;
2020-10-22 04:14:53 +06:30
}
double getTotal(Rate rate) {
List<CargoType> cargoTypes = getCargoTypes(rate);
2021-09-10 14:27:38 +06:30
double total = cargoTypes.fold(0.0, (p, c) => c.calAmount + p);
2020-10-22 04:14:53 +06:30
return total;
}
2021-09-10 14:27:38 +06:30
double get balance => amount - paidAmount;
2020-10-28 05:11:06 +06:30
2020-10-22 04:14:53 +06:30
double getNetAmount(Rate rate) {
List<CargoType> cargoTypes = getCargoTypes(rate);
2021-09-10 14:27:38 +06:30
double total = cargoTypes.fold(0.0, (p, c) => c.calAmount + p);
2020-10-22 04:14:53 +06:30
total += getCustomFee();
total += getDeliveryFee();
total += getHandlingFee();
total -= getDiscount();
return total;
}
2020-10-24 06:14:07 +06:30
double getHandlingFee() {
2021-09-10 17:14:59 +06:30
return shipments!
.where((sh) => sh!.isSelected)
.fold(0, (p, s) => p + (s!.handlingFee - s.paidHandlingFee));
2020-10-24 06:14:07 +06:30
}
2020-10-22 04:14:53 +06:30
double getTotalBalance(Rate rate) {
2021-09-10 14:27:38 +06:30
return getNetAmount(rate) - paidAmount;
2020-10-22 04:14:53 +06:30
}
double getCustomFee() {
2024-01-23 16:28:08 +06:30
return customDuties.isEmpty ? 0 : customDuties.fold(0, (p, d) => p + d.fee);
2020-10-22 04:14:53 +06:30
}
double getDeliveryFee() {
2024-01-23 16:28:08 +06:30
return deliveryFee;
2020-10-22 04:14:53 +06:30
}
2021-09-10 14:27:38 +06:30
double getDiscount() => discount == null ? 0 : discount!.amount;
2020-06-24 16:06:15 +06:30
2020-10-26 04:41:24 +06:30
Invoice(
{this.id,
this.invoiceNumber,
this.invoiceDate,
this.fcsID,
this.userName,
this.phoneNumber,
2021-09-10 14:27:38 +06:30
this.amount = 0,
this.paidAmount = 0,
2020-10-26 04:41:24 +06:30
this.discount,
this.status,
2021-09-10 14:27:38 +06:30
this.customDuties = const [],
this.cartons = const [],
this.cargoTypes = const [],
this.handlingFee = 0,
this.deliveryFee = 0,
2020-10-26 04:41:24 +06:30
this.fcsShipmentID,
2021-09-10 14:27:38 +06:30
this.shipments = const [],
2020-10-26 04:41:24 +06:30
this.invoiceURL,
2021-09-10 14:27:38 +06:30
this.payments = const [],
2020-10-26 04:41:24 +06:30
this.paymentMethod});
2020-10-16 13:46:02 +06:30
factory Invoice.fromMap(Map<String, dynamic> map, String docID) {
2020-10-26 04:41:24 +06:30
var invd = (map['invoice_date'] as Timestamp);
var cargoTypesMaps =
List<Map<String, dynamic>>.from(map['cargo_types'] ?? []);
var cargoTypes =
cargoTypesMaps.map((e) => CargoType.fromMap(e, e["id"])).toList();
var customDutiesMap =
List<Map<String, dynamic>>.from(map['custom_duties'] ?? []);
var customDuties = customDutiesMap
.map((e) => CustomDuty.fromMap(e, e["id"] ?? ""))
.toList();
2020-10-26 04:41:24 +06:30
var handlingShipmentsMap =
List<Map<String, dynamic>>.from(map['handling_fee_shipments'] ?? []);
var handingShipments =
handlingShipmentsMap.map((e) => Shipment.fromMap(e, e["id"])).toList();
var cartonsMap = List<Map<String, dynamic>>.from(map['cartons'] ?? []);
var cartons = cartonsMap.map((e) => Carton.fromMap(e, e["id"])).toList();
var paymentMethodMap = map['payment_method'];
var paymentMethod = paymentMethodMap != null
? PaymentMethod.fromMap(paymentMethodMap, paymentMethodMap['id'])
: null;
var discountMap = map['discount'];
var discount = Discount.fromMap(discountMap, discountMap['id']);
2020-10-28 05:11:06 +06:30
var paymentMaps = List<Map<String, dynamic>>.from(map['payments'] ?? []);
var payments = paymentMaps.map((e) => Payment.fromMap(e, e["id"])).toList();
2020-10-16 13:46:02 +06:30
return Invoice(
id: docID,
invoiceNumber: map['invoice_number'] ?? "",
2021-09-10 14:27:38 +06:30
invoiceDate: invd.toDate(),
userName: map['user_name'] ?? "",
fcsID: map['fcs_id'] ?? "",
phoneNumber: map['phone_number'] ?? "",
amount: map['amount'] ?? 0,
2020-10-28 05:11:06 +06:30
paidAmount: double.tryParse(map['paid_amount'].toString()) ?? 0,
status: map['status'] ?? "",
2020-10-26 04:41:24 +06:30
cartons: cartons,
cargoTypes: cargoTypes,
shipments: handingShipments,
customDuties: customDuties,
deliveryFee: map['delivery_fee'] ?? 0,
invoiceURL: map['invoice_url'] ?? "",
2020-10-26 04:41:24 +06:30
paymentMethod: paymentMethod,
discount: discount,
2020-10-28 05:11:06 +06:30
payments: payments,
2020-10-16 13:46:02 +06:30
);
}
2020-10-24 06:14:07 +06:30
Map<String, dynamic> toMap() {
List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList();
2021-09-10 14:27:38 +06:30
List _customDuties = customDuties.map((c) => c.toMap()).toList();
List _cartons = cartons.map((c) => c.toMap()).toList();
2021-09-10 17:14:59 +06:30
List _shipments = shipments!.map((s) => s!.toMap()).toList();
2020-10-24 06:14:07 +06:30
return {
"id": id,
2021-09-10 14:27:38 +06:30
"invoice_date": invoiceDate?.toUtc().toIso8601String(),
2020-10-24 06:14:07 +06:30
"user_id": userID,
2020-10-26 04:41:24 +06:30
"user_name": userName,
"invoice_number": invoiceNumber,
2020-10-24 06:14:07 +06:30
'fcs_shipment_id': fcsShipmentID,
'cargo_types': _cargoTypes,
'custom_duties': _customDuties,
2020-10-26 04:41:24 +06:30
'handling_fee_shipments': _shipments,
2020-10-24 06:14:07 +06:30
'cartons': _cartons,
'discount': discount?.toMap(),
2020-10-26 04:41:24 +06:30
'amount': amount,
2020-10-24 06:14:07 +06:30
'handling_fee': handlingFee,
'delivery_fee': deliveryFee,
2020-10-26 04:41:24 +06:30
'invoice_url': invoiceURL,
'payment_method': paymentMethod?.toMap(),
2020-10-24 06:14:07 +06:30
};
}
2020-06-02 14:56:51 +06:30
}