Files

262 lines
7.2 KiB
Dart
Raw Permalink Normal View History

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-24 06:14:07 +06:30
import 'package:fcs/domain/entities/shipment.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 {
2021-09-10 14:27:38 +06:30
String? id;
String? cartonNumber;
String? fcsShipmentID;
String? fcsShipmentNumber;
2021-09-10 14:27:38 +06:30
String? senderID;
String? senderFCSID;
String? senderName;
2021-01-12 16:59:52 +06:30
String? consigneeFCSID;
String? consigneeName;
String? consigneeID;
2020-10-16 10:58:31 +06:30
double width;
double height;
double length;
String? status;
String? cargoDesc;
String? desc;
2021-09-10 14:27:38 +06:30
int? shipmentWeight;
bool? isChecked;
bool? isShipmentCarton;
String? cartonType;
2021-09-10 14:27:38 +06:30
String? mixCartonNumber;
String? lastMile;
2024-02-09 17:09:05 +06:30
String? cartonSizeType;
2024-02-09 17:40:51 +06:30
2024-02-01 18:07:40 +06:30
double cartonWeight;
2024-02-09 17:40:51 +06:30
String? billTo;
2024-02-02 18:00:51 +06:30
bool isSelected;
2020-06-04 01:36:49 +06:30
int rate;
int weight;
2021-09-10 14:27:38 +06:30
String? packageType;
String? pickUpID;
List<String> photoUrls;
2021-09-10 14:27:38 +06:30
String? remark;
DateTime? arrivedDate;
2020-06-04 01:36:49 +06:30
2020-10-22 04:14:53 +06:30
List<String> packageIDs;
2020-06-04 01:36:49 +06:30
List<Package> packages;
2021-09-10 14:27:38 +06:30
List<CargoType> cargoTypes = [];
List<CargoType> surchareItems = [];
2020-06-25 16:19:23 +06:30
2021-09-10 14:27:38 +06:30
DeliveryAddress? deliveryAddress;
Shipment? shipment;
2020-06-26 16:17:40 +06:30
//for mix carton
List<Carton> cartons;
List<String> cartonIDs;
2021-01-09 19:11:47 +06:30
2024-01-23 16:28:08 +06:30
int get amount => (rate * weight);
2020-06-04 01:36:49 +06:30
2021-01-12 16:59:52 +06:30
// String get packageNumber =>
// shipmentNumber + "-" + receiverNumber + " #" + boxNumber;
2020-06-04 01:36:49 +06:30
2021-09-13 08:45:50 +06:30
double get actualWeight =>
2024-01-23 16:28:08 +06:30
cargoTypes.isEmpty ? 0 : cargoTypes.fold(0, (p, e) => e.weight + p);
2020-10-16 10:58:31 +06:30
2021-01-04 17:19:01 +06:30
int getShipmentWeight(double volumetricRatio) {
2024-01-23 16:28:08 +06:30
if (length <= 0 || width <= 0 || height <= 0 || volumetricRatio <= 0)
return 0;
2021-01-07 18:15:39 +06:30
2021-01-04 17:19:01 +06:30
return ((length * width * height) / volumetricRatio).round();
2020-10-13 07:50:25 +06:30
}
2020-10-24 06:14:07 +06:30
/// getCargoTypeForCalWeight returns carton with shipment weight
List<CargoType> getCargoTypeForCalWeight(double volumetricRatio) {
// get shipment weight
2021-09-10 14:27:38 +06:30
double volume = length * width * height;
double sw = volume / volumetricRatio;
2020-10-24 06:14:07 +06:30
// get actual weight
double aw = cargoTypes.fold(0.0, (p, c) => p + c.weight);
if (aw == 0 || sw == 0) return [];
cargoTypes.forEach((e) {
2021-09-10 14:27:38 +06:30
double calWeight = aw > sw ? e.weight : (e.weight / aw) * sw;
2020-10-24 06:14:07 +06:30
e.calWeight = calWeight;
});
return cargoTypes;
}
2020-10-16 10:58:31 +06:30
/// calAmount returns total amount
double calAmount(Rate rate) {
// get shipment weight
2021-01-04 17:19:01 +06:30
int sw = getShipmentWeight(rate.volumetricRatio);
2020-10-16 10:58:31 +06:30
// get actual weight
double aw = cargoTypes.fold(0.0, (p, c) => p + c.weight);
if (aw == 0 || sw == 0) return 0;
2021-01-04 17:19:01 +06:30
double wd = sw - aw;
wd = wd - rate.diffDiscountWeight;
double wdAmount = wd > 0 ? wd * rate.diffWeightRate : 0;
DiscountByWeight discountByWeight = rate.getDiscountByWeight(aw);
2020-10-16 10:58:31 +06:30
double total = 0;
cargoTypes.forEach((e) {
2024-01-23 16:28:08 +06:30
double r = e.rate - (discountByWeight.discount);
2021-01-04 17:19:01 +06:30
double amount = e.weight * r;
2020-10-16 10:58:31 +06:30
total += amount;
});
2021-01-04 17:19:01 +06:30
return total + wdAmount;
2020-10-16 10:58:31 +06:30
}
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,
2021-01-09 19:11:47 +06:30
this.senderID,
2020-06-04 01:36:49 +06:30
this.senderFCSID,
this.senderName,
2020-10-14 16:53:16 +06:30
this.desc,
2021-09-10 14:27:38 +06:30
this.width = 0,
this.height = 0,
this.length = 0,
2020-06-29 16:15:25 +06:30
this.shipmentWeight,
2020-10-14 16:53:16 +06:30
this.isChecked = false,
this.cartonType,
this.consigneeFCSID,
this.consigneeID,
this.consigneeName,
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,
2021-09-10 14:27:38 +06:30
this.shipmentHistory = const [],
this.packages = const [],
this.cargoTypes = const [],
this.surchareItems = const [],
2020-10-19 05:13:49 +06:30
this.cartonNumber,
2024-02-09 13:35:32 +06:30
this.billTo,
2020-10-19 05:13:49 +06:30
this.fcsShipmentID,
this.fcsShipmentNumber,
2021-09-10 14:27:38 +06:30
this.packageIDs = const [],
2020-10-21 02:59:10 +06:30
this.mixCartonNumber,
2020-10-20 06:19:10 +06:30
this.isShipmentCarton = false,
2020-12-11 17:34:56 +06:30
this.deliveryAddress,
2024-02-09 17:09:05 +06:30
this.cartonSizeType,
this.lastMile,
this.cartons = const [],
this.cartonIDs = const [],
2024-02-09 17:40:51 +06:30
this.cartonWeight = 0,
this.photoUrls = const [],
2024-02-02 18:00:51 +06:30
this.isSelected = false});
2020-10-16 10:58:31 +06:30
Map<String, dynamic> toMap() {
var _types = cargoTypes.where((t) => t.weight != 0).toList();
2025-03-12 17:49:27 +06:30
var _cargoTypes = _types.map((c) => c.toMapForCarton()).toList();
2025-03-24 18:23:17 +06:30
// var _packagesIds = packages.map((c) => c.id).toList();
var _surchareItems =
surchareItems.map((c) => c.toMapForSurcharge()).toList();
2020-10-16 10:58:31 +06:30
return {
2021-01-09 19:11:47 +06:30
'id': id,
'carton_type': cartonType,
2020-10-20 06:19:10 +06:30
'fcs_shipment_id': fcsShipmentID,
'sender_user_id': senderID,
'consignee_user_id': consigneeID,
'bill_to': billTo,
'last_mile': lastMile,
2020-10-16 10:58:31 +06:30
'length': length,
'width': width,
'height': height,
2025-03-24 18:23:17 +06:30
// 'package_ids': _packagesIds,
'cargo_types': _cargoTypes,
'surcharge_items': _surchareItems,
};
}
Map<String, dynamic> toMapForMix() {
var _cartonIds = cartons.map((c) => c.id).toList();
return {
'id': id,
2020-10-18 02:38:46 +06:30
'carton_type': cartonType,
'fcs_shipment_id': fcsShipmentID,
'length': length,
'width': width,
'height': height,
'carton_ids': _cartonIds,
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) {
2021-09-13 08:45:50 +06:30
var _arrivedDate =
map['arrived_date'] == null ? null : (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;
2020-10-21 02:59:10 +06:30
var cargoTypesMaps =
List<Map<String, dynamic>>.from(map['cargo_types'] ?? []);
var cargoTypes = cargoTypesMaps
.map((e) => CargoType.fromMapForCarton(e, e["id"]))
.toList();
var surchargeItemMaps =
List<Map<String, dynamic>>.from(map['surcharge_items'] ?? []);
var surchageItems = surchargeItemMaps
.map((e) => CargoType.fromMapForsurcharge(e, e["id"]))
.toList();
2024-02-09 17:40:51 +06:30
List<String> _photoUrls =
map['photo_urls'] == null ? [] : List.from(map['photo_urls']);
2021-01-09 19:11:47 +06:30
2020-10-18 02:38:46 +06:30
return Carton(
id: docID,
arrivedDate: _arrivedDate != null ? _arrivedDate.toDate() : null,
length: double.tryParse(map['length'].toString()) ?? 0,
width: double.tryParse(map['width'].toString()) ?? 0,
height: double.tryParse(map['height'].toString()) ?? 0,
cartonType: map['carton_type'],
cartonNumber: map['carton_number'],
fcsShipmentID: map['fcs_shipment_id'],
fcsShipmentNumber: map['fcs_shipment_number'],
status: map['status'],
packageIDs: List<String>.from(map['package_ids'] ?? []),
deliveryAddress: _da,
cargoTypes: cargoTypes,
surchareItems: surchageItems,
senderID: map['sender_user_id'],
senderFCSID: map['sender_fcs_id'],
senderName: map['sender_user_name'],
consigneeID: map['consignee_user_id'],
consigneeName: map['consignee_user_name'],
consigneeFCSID: map['consignee_fcs_id'],
cartonIDs: List<String>.from(map['carton_ids'] ?? []),
cartonWeight: (map['carton_weight'] ?? 0).toDouble(),
photoUrls: _photoUrls,
billTo: map['bill_to'] ?? '',
lastMile: map['last_mile'] ?? "");
2020-10-16 17:57:58 +06:30
}
2020-10-22 04:14:53 +06:30
@override
bool operator ==(Object other) => other is Carton && other.id == id;
@override
int get hashCode => id.hashCode;
2020-06-04 01:36:49 +06:30
}