addScannedAsset method
Implementation
addScannedAsset(
List<ScanContentModel> assets, int? serial, int? start, int? end) {
var range = RangeScannedModel();
var iccids = <SingleScannedModel>[];
var scannedIccids = <int>[];
if (selectedScanType.value == "Range") {
range.boxRangeId = "${start!} - ${end!}";
range.id = "$start - ${end.toString().substring(
end.toString().length - 2,
end.toString().length,
)}";
}
for (var asset in assets) {
var scannedProduct = productDataScannedList.firstWhere(
(element) => element.productCode == asset.productCode,
orElse: () => ProductContentModel(
iccidList: [], rangeList: [], scannedserialNums: []),
);
if (scannedProduct.productCode != null) {
if (selectedScanType.value == "Single") {
if (scannedProduct.scannedserialNums!.contains(asset.serialNumber)) {
Helper.assetSuccessDialog(
Get.context!,
"Failed to add asset.",
"Scanned serial number already present",
const AssetImage(
"assets/images/failed.png",
));
continue;
} else {
scannedIccidList.add(asset.serialNumber!.toString());
scannedProduct.scannedserialNums!.add(asset.serialNumber!);
scannedProduct.iccidList!
.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
ProductContentModel updatedProduct = scannedProduct.copyWith(
scannedserialNums: scannedProduct.scannedserialNums,
scannedQty: scannedProduct.scannedQty + 1,
iccidList: scannedProduct.iccidList);
var temp = productDataScannedList;
int index = temp.indexOf(scannedProduct);
temp[index] = updatedProduct;
productDataScannedList = temp;
Helper.assetSuccessDialog(
Get.context!,
"Asset added successfully",
"Product ID: ${asset.serialNumber}",
const AssetImage(
"assets/images/success.png",
));
}
} else {
// var diff = end! - start!;
bool someAssetsAlreadyScanned = false;
for (final data in scannedProduct.scannedserialNums!) {
if (start! <= data && data <= end!) {
someAssetsAlreadyScanned = true;
break;
}
}
if (someAssetsAlreadyScanned) {
Helper.assetSuccessDialog(
Get.context!,
"Failed to add asset.",
"Please use different range, some assets are already scanned",
const AssetImage(
"assets/images/failed.png",
));
continue;
}
scannedIccids.add(asset.serialNumber!);
iccids.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
scannedIccidList.add(asset.serialNumber!.toString());
if (asset.serialNumber == end) {
scannedProduct.scannedserialNums!.addAll(scannedIccids);
range.iccidList = iccids;
scannedProduct.rangeList!.add(range);
ProductContentModel updatedProduct = scannedProduct.copyWith(
scannedserialNums: scannedProduct.scannedserialNums,
scannedQty: scannedProduct.scannedserialNums!.length,
rangeList: scannedProduct.rangeList);
var temp = productDataScannedList;
int index = temp.indexOf(scannedProduct);
temp[index] = updatedProduct;
productDataScannedList = temp;
Helper.assetSuccessDialog(
Get.context!,
"Asset added successfully",
"Serials: $start - $end",
const AssetImage(
"assets/images/success.png",
));
}
}
} else {
if (selectedScanType.value == "Single") {
scannedIccidList.add(asset.serialNumber!.toString());
scannedProduct.scannedserialNums!.add(asset.serialNumber!);
scannedProduct.iccidList!
.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
ProductContentModel updatedProduct = scannedProduct.copyWith(
scannedserialNums: scannedProduct.scannedserialNums,
scannedQty: 1,
iccidList: scannedProduct.iccidList,
totalOrderedQuantity: 0,
id: asset.productId,
productCode: asset.productCode,
productName: asset.productName,
rangeList: [],
orderDetails: []);
var temp = productDataScannedList;
temp.add(updatedProduct);
productDataScannedList = temp;
Helper.assetSuccessDialog(
Get.context!,
"Asset added successfully",
"Product ID: ${asset.serialNumber}",
const AssetImage(
"assets/images/success.png",
));
} else {
// var diff = end! - start!;
scannedIccids.add(asset.serialNumber!);
iccids.add(SingleScannedModel(iccid: asset.serialNumber!.toString()));
scannedIccidList.add(asset.serialNumber!.toString());
if (asset.serialNumber == end) {
scannedProduct.scannedserialNums!.addAll(scannedIccids);
range.iccidList = iccids;
scannedProduct.rangeList!.add(range);
ProductContentModel updatedProduct = scannedProduct.copyWith(
scannedserialNums: scannedProduct.scannedserialNums,
scannedQty: scannedProduct.scannedserialNums!.length,
rangeList: scannedProduct.rangeList,
id: asset.productId,
productCode: asset.productCode,
productName: asset.productName,
iccidList: [],
orderDetails: [],
totalOrderedQuantity: 0);
var temp = productDataScannedList;
temp.add(updatedProduct);
productDataScannedList = temp;
Helper.assetSuccessDialog(
Get.context!,
"Asset added successfully",
"Serials: $start - $end",
const AssetImage(
"assets/images/success.png",
));
}
}
}
}
}