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

275 lines
7.9 KiB
Dart
Raw 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? shipmentID;
String? shipmentNumber;
String? senderID;
String? senderFCSID;
String? senderName;
2021-01-12 16:59:52 +06:30
2021-09-10 14:27:38 +06:30
String? boxNumber;
String? status;
String? cargoDesc;
String? desc;
2020-10-16 10:58:31 +06:30
double width;
double height;
double length;
2021-09-10 14:27:38 +06:30
int? shipmentWeight;
bool? isChecked;
bool? isShipmentCarton;
String? cartonType;
String? fcsID;
String? userName;
String? userID;
String? fcsShipmentID;
String? fcsShipmentNumber;
String? mixCartonID;
String? mixCartonNumber;
String? cartonSizeID;
String? cartonSizeName;
2024-02-01 18:07:40 +06:30
double cartonWeight;
2024-02-09 13:35:32 +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> photos = [];
List<String> photoUrls;
2021-09-10 14:27:38 +06:30
String? remark;
DateTime? arrivedDate;
String? cartonNumber;
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 = [];
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
2021-01-09 19:11:47 +06:30
//for mix box
2021-09-10 14:27:38 +06:30
String? mixBoxType;
2021-01-09 19:11:47 +06:30
List<Carton> mixCartons;
2021-01-10 15:56:27 +06:30
List<String> mixCartonIDs;
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,
2020-10-24 06:14:07 +06:30
this.shipmentID,
2020-06-04 01:36:49 +06:30
this.shipmentNumber,
2021-01-09 19:11:47 +06:30
this.senderID,
2020-06-04 01:36:49 +06:30
this.senderFCSID,
this.senderName,
this.boxNumber,
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.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,
2021-09-10 14:27:38 +06:30
this.shipmentHistory = const [],
this.packages = const [],
this.cargoTypes = 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.mixCartonID,
this.mixCartonNumber,
2020-10-20 06:19:10 +06:30
this.isShipmentCarton = false,
2020-12-11 17:34:56 +06:30
this.deliveryAddress,
this.cartonSizeID,
this.cartonSizeName,
2021-01-09 19:11:47 +06:30
this.mixBoxType,
2021-09-10 14:27:38 +06:30
this.mixCartons = const [],
this.mixCartonIDs = const [],
2024-02-02 18:00: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() {
2021-09-10 14:27:38 +06:30
List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList();
List _packages = packages.map((c) => c.toJson()).toList();
List _mixCartons = mixCartons.map((c) => c.toJson()).toList();
2020-10-16 10:58:31 +06:30
return {
2021-01-09 19:11:47 +06:30
'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,
2020-10-26 04:41:24 +06:30
'delivery_address': deliveryAddress?.toMap(),
2020-10-18 02:38:46 +06:30
'carton_type': cartonType,
2020-10-24 06:14:07 +06:30
'mix_carton_id': mixCartonID,
2021-01-09 19:11:47 +06:30
'mix_box_type': mixBoxType,
'mix_cartons': _mixCartons,
'sender_id': senderID,
'sender_fcs_id': senderFCSID,
'sender_name': senderName
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'] ?? []);
2020-10-18 02:38:46 +06:30
var cargoTypes =
cargoTypesMaps.map((e) => CargoType.fromMap(e, e["id"])).toList();
2021-01-09 19:11:47 +06:30
var mixCartonsMaps =
List<Map<String, dynamic>>.from(map['mix_cartons'] ?? []);
var _mixCartons =
mixCartonsMaps.map((e) => Carton.fromMap(e, e["id"])).toList();
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(
2021-01-09 19:11:47 +06:30
id: docID,
arrivedDate: _arrivedDate != null ? _arrivedDate.toDate() : null,
shipmentID: map['shipment_id'],
shipmentNumber: map['shipment_number'],
2021-01-12 16:59:52 +06:30
// receiverNumber: map['receiver_number'],
2021-01-09 19:11:47 +06:30
boxNumber: map['box_number'],
2021-09-10 14:27:38 +06:30
length: double.tryParse(map['length'].toString()) ?? 0,
width: double.tryParse(map['width'].toString()) ?? 0,
height: double.tryParse(map['height'].toString()) ?? 0,
2021-01-09 19:11:47 +06:30
userName: map['user_name'],
fcsID: map['fcs_id'],
cartonType: map['carton_type'],
cartonNumber: map['carton_number'],
userID: map['user_id'],
fcsShipmentID: map['fcs_shipment_id'],
fcsShipmentNumber: map['fcs_shipment_number'],
isShipmentCarton: map['is_shipment_carton'],
mixCartonID: map['mix_carton_id'],
mixCartonNumber: map['mix_carton_number'],
status: map['status'],
packageIDs: List<String>.from(map['package_ids'] ?? []),
deliveryAddress: _da,
cargoTypes: cargoTypes,
mixBoxType: map['mix_box_type'],
mixCartons: _mixCartons,
senderID: map['sender_id'],
senderFCSID: map['sender_fcs_id'],
senderName: map['sender_name'],
2021-01-10 15:56:27 +06:30
mixCartonIDs: List<String>.from(map['mix_carton_ids'] ?? []),
2021-01-11 19:35:26 +06:30
cartonWeight: (map['carton_weight'] ?? 0).toDouble(),
photoUrls: _photoUrls,
2021-01-09 19:11:47 +06:30
);
}
Map<String, dynamic> toJson() {
List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList();
2021-09-10 14:27:38 +06:30
List _packages = packages.map((c) => c.toJson()).toList();
List _mixCartons = mixCartons.map((c) => c.toJson()).toList();
2021-01-09 19:11:47 +06:30
return {
'id': id,
'fcs_shipment_id': fcsShipmentID,
'user_id': userID,
'cargo_types': _cargoTypes,
'packages': _packages,
'length': length,
'width': width,
'height': height,
'delivery_address': deliveryAddress?.toMap(),
'carton_type': cartonType,
'mix_carton_id': mixCartonID,
'mix_box_type': mixBoxType,
'mix_cartons': _mixCartons,
'sender_id': senderID,
'sender_fcs_id': senderFCSID,
'sender_name': senderName,
"photo_urls": photoUrls
2021-01-09 19:11:47 +06:30
};
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
}