2020-10-16 17:57:58 +06:30
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-16 10:58:31 +06:30
|
|
|
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/rate.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/vo/shipment_status.dart';
|
2020-10-08 11:38:05 +06:30
|
|
|
import 'package:fcs/domain/vo/delivery_address.dart';
|
2020-06-04 01:36:49 +06:30
|
|
|
|
2020-10-15 03:06:13 +06:30
|
|
|
import 'cargo_type.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package.dart';
|
2020-06-04 01:36:49 +06:30
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
class Carton {
|
2020-06-04 01:36:49 +06:30
|
|
|
String id;
|
|
|
|
|
String shipmentNumber;
|
|
|
|
|
String senderFCSID;
|
|
|
|
|
String senderName;
|
|
|
|
|
String receiverFCSID;
|
|
|
|
|
String receiverName;
|
|
|
|
|
String receiverAddress;
|
|
|
|
|
String receiverNumber;
|
|
|
|
|
String boxNumber;
|
|
|
|
|
String status;
|
|
|
|
|
String cargoDesc;
|
2020-10-14 16:53:16 +06:30
|
|
|
String desc;
|
2020-10-16 10:58:31 +06:30
|
|
|
double width;
|
|
|
|
|
double height;
|
|
|
|
|
double length;
|
2020-06-29 16:15:25 +06:30
|
|
|
int shipmentWeight;
|
2020-10-14 16:53:16 +06:30
|
|
|
bool isChecked;
|
2020-10-20 06:19:10 +06:30
|
|
|
bool isShipmentCarton;
|
2020-10-14 16:53:16 +06:30
|
|
|
String cartonType;
|
|
|
|
|
String fcsID;
|
|
|
|
|
String userName;
|
2020-10-19 05:13:49 +06:30
|
|
|
String userID;
|
|
|
|
|
String fcsShipmentID;
|
|
|
|
|
String fcsShipmentNumber;
|
2020-06-04 01:36:49 +06:30
|
|
|
|
|
|
|
|
int rate;
|
|
|
|
|
int weight;
|
|
|
|
|
String packageType;
|
|
|
|
|
String pickUpID;
|
|
|
|
|
List<String> photos;
|
|
|
|
|
String remark;
|
|
|
|
|
DateTime arrivedDate;
|
2020-10-19 05:13:49 +06:30
|
|
|
String cartonNumber;
|
2020-10-20 06:19:10 +06:30
|
|
|
List<String> packageIDs;
|
2020-06-04 01:36:49 +06:30
|
|
|
|
|
|
|
|
List<Package> packages;
|
|
|
|
|
|
2020-10-15 03:06:13 +06:30
|
|
|
List<CargoType> cargoTypes;
|
2020-10-20 06:19:10 +06:30
|
|
|
List<Carton> cartons;
|
2020-06-25 16:19:23 +06:30
|
|
|
|
2020-10-16 10:58:31 +06:30
|
|
|
DeliveryAddress deliveryAddress;
|
2020-06-26 16:17:40 +06:30
|
|
|
|
2020-06-04 01:36:49 +06:30
|
|
|
int get amount => rate != null && weight != null ? rate * weight : 0;
|
|
|
|
|
|
|
|
|
|
String get packageNumber =>
|
|
|
|
|
shipmentNumber + "-" + receiverNumber + " #" + boxNumber;
|
|
|
|
|
double get price => rate.toDouble() * weight;
|
|
|
|
|
|
2020-10-16 10:58:31 +06:30
|
|
|
double get actualWeight =>
|
|
|
|
|
cargoTypes == null ? 0 : cargoTypes.fold(0, (p, e) => e.weight + p);
|
|
|
|
|
|
2020-10-13 07:50:25 +06:30
|
|
|
double getShipmentWeight(double volumetricRatio) {
|
|
|
|
|
if (length == null ||
|
|
|
|
|
length <= 0 ||
|
|
|
|
|
width == null ||
|
|
|
|
|
width <= 0 ||
|
|
|
|
|
height == null ||
|
|
|
|
|
height <= 0 ||
|
|
|
|
|
volumetricRatio == null ||
|
|
|
|
|
volumetricRatio <= 0) return 0;
|
|
|
|
|
|
|
|
|
|
return (length * width * height) / volumetricRatio;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 10:58:31 +06:30
|
|
|
/// calAmount returns total amount
|
|
|
|
|
double calAmount(Rate rate) {
|
|
|
|
|
// get shipment weight
|
|
|
|
|
double volume = (length ?? 0) * (width ?? 0) * (height ?? 0);
|
|
|
|
|
double sw = volume / rate.volumetricRatio ?? 0;
|
|
|
|
|
|
|
|
|
|
// get actual weight
|
|
|
|
|
double aw = cargoTypes.fold(0.0, (p, c) => p + c.weight);
|
|
|
|
|
if (aw == 0 || sw == 0) return 0;
|
|
|
|
|
|
|
|
|
|
DiscountByWeight discountByWeight =
|
|
|
|
|
rate.getDiscountByWeight(sw > aw ? sw : aw);
|
|
|
|
|
|
|
|
|
|
double total = 0;
|
|
|
|
|
cargoTypes.forEach((e) {
|
|
|
|
|
double cargoWeight = aw > sw ? e.weight : e.weight / aw * sw;
|
|
|
|
|
double r =
|
|
|
|
|
e.rate - (discountByWeight != null ? discountByWeight.discount : 0);
|
|
|
|
|
double amount = cargoWeight * r;
|
|
|
|
|
total += amount;
|
|
|
|
|
});
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 02:33:06 +06:30
|
|
|
List<ShipmentStatus> shipmentHistory;
|
2020-06-04 01:36:49 +06:30
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
Carton(
|
2020-06-04 01:36:49 +06:30
|
|
|
{this.id,
|
|
|
|
|
this.shipmentNumber,
|
|
|
|
|
this.senderFCSID,
|
|
|
|
|
this.senderName,
|
|
|
|
|
this.receiverFCSID,
|
|
|
|
|
this.receiverName,
|
|
|
|
|
this.receiverNumber,
|
|
|
|
|
this.receiverAddress,
|
|
|
|
|
this.boxNumber,
|
2020-10-14 16:53:16 +06:30
|
|
|
this.desc,
|
2020-06-25 16:19:23 +06:30
|
|
|
this.width,
|
|
|
|
|
this.height,
|
|
|
|
|
this.length,
|
2020-06-29 16:15:25 +06:30
|
|
|
this.shipmentWeight,
|
2020-10-14 16:53:16 +06:30
|
|
|
this.isChecked = false,
|
|
|
|
|
this.cartonType,
|
|
|
|
|
this.fcsID,
|
2020-10-19 05:13:49 +06:30
|
|
|
this.userID,
|
2020-10-14 16:53:16 +06:30
|
|
|
this.userName,
|
2020-10-16 17:57:58 +06:30
|
|
|
this.rate = 0,
|
|
|
|
|
this.weight = 0,
|
2020-06-04 01:36:49 +06:30
|
|
|
this.packageType,
|
|
|
|
|
this.pickUpID,
|
|
|
|
|
this.remark,
|
|
|
|
|
this.status,
|
|
|
|
|
this.arrivedDate,
|
|
|
|
|
this.cargoDesc,
|
2020-10-07 02:33:06 +06:30
|
|
|
this.shipmentHistory,
|
2020-06-25 16:19:23 +06:30
|
|
|
this.packages,
|
2020-06-26 16:17:40 +06:30
|
|
|
this.cargoTypes,
|
2020-10-19 05:13:49 +06:30
|
|
|
this.cartonNumber,
|
|
|
|
|
this.fcsShipmentID,
|
|
|
|
|
this.fcsShipmentNumber,
|
2020-10-20 06:19:10 +06:30
|
|
|
this.cartons,
|
|
|
|
|
this.packageIDs,
|
|
|
|
|
this.isShipmentCarton = false,
|
2020-10-16 10:58:31 +06:30
|
|
|
this.deliveryAddress});
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
|
List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList();
|
2020-10-19 12:41:13 +06:30
|
|
|
List _packages = packages?.map((c) => c.toJson())?.toList();
|
2020-10-16 10:58:31 +06:30
|
|
|
return {
|
|
|
|
|
"id": id,
|
2020-10-20 06:19:10 +06:30
|
|
|
'fcs_shipment_id': fcsShipmentID,
|
|
|
|
|
'user_id': userID,
|
2020-10-16 10:58:31 +06:30
|
|
|
'cargo_types': _cargoTypes,
|
2020-10-19 05:13:49 +06:30
|
|
|
'packages': _packages,
|
2020-10-16 10:58:31 +06:30
|
|
|
'length': length,
|
|
|
|
|
'width': width,
|
|
|
|
|
'height': height,
|
|
|
|
|
'delivery_address': deliveryAddress.toMap(),
|
2020-10-18 02:38:46 +06:30
|
|
|
'carton_type': cartonType,
|
2020-10-16 10:58:31 +06:30
|
|
|
};
|
|
|
|
|
}
|
2020-10-16 17:57:58 +06:30
|
|
|
|
2020-10-18 02:38:46 +06:30
|
|
|
factory Carton.fromMap(Map<String, dynamic> map, String docID) {
|
2020-10-16 17:57:58 +06:30
|
|
|
var _arrivedDate = (map['arrived_date'] as Timestamp);
|
2020-10-18 02:38:46 +06:30
|
|
|
var da = map['delivery_address'];
|
|
|
|
|
var _da = da != null ? DeliveryAddress.fromMap(da, da["id"]) : null;
|
|
|
|
|
var cargoTypesMaps = List<Map<String, dynamic>>.from(map['cargo_types']);
|
|
|
|
|
var cargoTypes =
|
|
|
|
|
cargoTypesMaps.map((e) => CargoType.fromMap(e, e["id"])).toList();
|
|
|
|
|
return Carton(
|
2020-10-16 17:57:58 +06:30
|
|
|
id: docID,
|
|
|
|
|
arrivedDate: _arrivedDate != null ? _arrivedDate.toDate() : null,
|
|
|
|
|
shipmentNumber: map['shipment_number'],
|
|
|
|
|
receiverNumber: map['receiver_number'],
|
|
|
|
|
boxNumber: map['box_number'],
|
|
|
|
|
length: map['length'],
|
|
|
|
|
width: map['width'],
|
|
|
|
|
height: map['height'],
|
|
|
|
|
userName: map['user_name'],
|
|
|
|
|
fcsID: map['fcs_id'],
|
2020-10-18 02:38:46 +06:30
|
|
|
cartonType: map['carton_type'],
|
2020-10-19 05:13:49 +06:30
|
|
|
cartonNumber: map['carton_number'],
|
2020-10-20 06:19:10 +06:30
|
|
|
userID: map['user_id'],
|
2020-10-19 05:13:49 +06:30
|
|
|
fcsShipmentID: map['fcs_shipment_id'],
|
|
|
|
|
fcsShipmentNumber: map['fcs_shipment_number'],
|
2020-10-20 06:19:10 +06:30
|
|
|
isShipmentCarton: map['is_shipment_carton'],
|
|
|
|
|
status: map['status'],
|
|
|
|
|
packageIDs: List<String>.from(map['package_ids'] ?? []),
|
2020-10-18 02:38:46 +06:30
|
|
|
deliveryAddress: _da,
|
|
|
|
|
cargoTypes: cargoTypes);
|
2020-10-16 17:57:58 +06:30
|
|
|
}
|
2020-06-04 01:36:49 +06:30
|
|
|
}
|