updateSellThruOrder method
Implementation
Future<void> updateSellThruOrder(cart.CartModel data) async {
var totalAmount = data.data!.totalRetailPrice; // subTotal
var totalNetAmount = data.data!.totalSellingPrice; // strike through
double totalTax = 0.0; //
double ctw = 0.0; // - value
var salesTax = "";
var ctwNegativeTax = ""; // red color
var localCurrency = data.data!.grandTotalInLocalCurrency!.price!.currency;
var currency = data.data!.currency;
var localGrandTotal = data.data!.grandTotalInLocalCurrency!.price!.amount;
var totalDiscount = 0.0;
double? ctwPercentage;
if (data.data!.taxes != null) {
// total discount
data.data!.taxes!.forEach((element) {
if (element.name! == "COMMISSION" || element.name! == "Commission") {
totalDiscount = element.totalPrice != null
? double.parse(element.totalPrice!)
: 0.0;
}
if (element.name! == "WHT" || element.name! == "CTW") {
ctwPercentage = element.percentage;
var sign = 1;
if (element.whtBearer != null &&
element.whtBearer!.isNotEmpty &&
element.whtBearer! == "DEALER") {
sign = -1;
}
ctw = element.totalPrice != null
? double.parse(element.totalPrice!) * sign
: 0.0;
}
});
}
double overallSaleTaxPercentage = 0.0;
totalTax = data.data!.overallSaleTax ?? 0.0;
overallSaleTaxPercentage = data.data!.overallSaleTaxPercentage ?? 0.0;
// ctw = data.data!.totalWHT ?? 0.0;
// only for spot sell thru
// data.data!.taxes!.forEach((tax) {
// if (tax.name == "SalesTax") {
// salesTax = tax.name!;
// totalTax = double.parse(tax.totalPrice!);
// }
// if (tax.name!.toLowerCase() == "ctw") {
// ctwNegativeTax = tax.name!;
// ctw = double.parse(tax.totalPrice!);
// }
// });
var totalAmountPaid = data.data!.grandTotal ?? "0.0";
var products = data.data!.products ?? [];
RxList<ProductContentModel> temp = <ProductContentModel>[].obs;
var tempIccids = <String>[].obs;
if (products.isNotEmpty) {
temp.addAll(productDataScannedList); // assigning order prodcut to temp.
products.forEach((product) {
product.assetList!.forEach((element) {
tempIccids.add(element.id!);
});
// tempIccids.addAll(product.assetList!);
if (temp.isEmpty) {
// new product
ProductContentModel productModel = ProductContentModel();
productModel.productCode = product.productCode ?? "";
productModel.productId = product.productId ?? "";
productModel.productImage = product.productImage ?? "";
productModel.productName = product.name ?? "";
productModel.totalOrderedQuantity = 0;
productModel.itemAmount =
product.unitPrice != null ? double.parse(product.unitPrice!) : 0;
productModel.unitSellingPrice = product.sellingUnitPrice != null
? double.parse(product.sellingUnitPrice!)
: 0;
productModel.sellingPrice = product.sellingPrice != null
? double.parse(product.sellingPrice!)
: 0;
productModel.itemTax =
product.unitTax != null ? double.parse(product.unitTax!) : 0;
productModel.scannedQty = product.quantity ?? 0;
List<SingleScannedModel> iccidList = [];
product.assetList!.forEach((element) {
iccidList.add(SingleScannedModel(
iccid: element.id,
purchaseOrderDate: element.purchaseOrderDate));
});
productModel.iccidList = iccidList;
temp.add(productModel); // adding new product on temp
} else {
var isProductExists = false;
var existingProductIndex = 0;
for (int i = 0; i < temp.length; i++) {
if (product.productCode == temp[i].productCode) {
isProductExists = true;
existingProductIndex = i;
break;
}
}
if (isProductExists) {
List<SingleScannedModel> iccidList = [];
product.assetList!.forEach((element) {
iccidList.add(SingleScannedModel(
iccid: element.id,
purchaseOrderDate: element.purchaseOrderDate));
});
ProductContentModel productModel = ProductContentModel();
productModel = temp[existingProductIndex].copyWith(
productImage: product.productImage ?? "",
productId: product.productId ?? "",
iccidList: iccidList,
rangeList: [],
scannedQty: product.quantity!,
itemAmount: product.unitPrice != null
? double.parse(product.unitPrice!)
: 0,
itemTax: product.unitTax != null
? double.parse(product.unitTax!)
: 0,
unitSellingPrice: product.sellingUnitPrice != null
? double.parse(product.sellingUnitPrice!)
: 0,
sellingPrice: product.sellingPrice != null
? double.parse(product.sellingPrice!)
: 0);
temp[existingProductIndex] =
productModel; // updating existing product on temp
} else {
// new product
ProductContentModel productModel = ProductContentModel();
productModel.productCode = product.productCode ?? "";
productModel.productId = product.productId ?? "";
productModel.productImage = product.productImage ?? "";
productModel.productName = product.name ?? "";
productModel.totalOrderedQuantity = 0;
productModel.itemAmount = product.unitPrice != null
? double.parse(product.unitPrice!)
: 0;
productModel.unitSellingPrice = product.sellingUnitPrice != null
? double.parse(product.sellingUnitPrice!)
: 0;
productModel.sellingPrice = product.sellingPrice != null
? double.parse(product.sellingPrice!)
: 0;
productModel.itemTax =
product.unitTax != null ? double.parse(product.unitTax!) : 0;
productModel.scannedQty = product.quantity ?? 0;
List<SingleScannedModel> iccidList = [];
product.assetList!.forEach((element) {
iccidList.add(SingleScannedModel(
iccid: element.id,
purchaseOrderDate: element.purchaseOrderDate));
});
productModel.iccidList = iccidList;
temp.add(productModel); // adding new product on temp
}
}
});
} else {
temp.addAll(originalProductList);
}
scannedIccidList = tempIccids; // assigning all the scanned iccids;
productDataScannedList.value = [...temp]; // assigning product list
RxList<ProductContentModel> onlyScannedProducts =
<ProductContentModel>[].obs;
productDataScannedList.forEach((element) {
// validating for scanQty>0
if (element.scannedQty > 0) {
onlyScannedProducts.add(element);
}
});
sellThruOrder.value = sellThruOrder.value.copyWith(
currency: currency,
localCurrency: localCurrency,
localCurrencyGrandTotal: double.parse(localGrandTotal!),
ctwNegativeTax: ctwNegativeTax,
ctw: ctw,
salesTax: salesTax,
content: onlyScannedProducts,
totalAmount: double.parse(totalAmount.toString()),
totalNetAmount: totalNetAmount,
totalTax: totalTax,
taxPercentage: ctwPercentage?.toString(),
overallSaleTaxPercentage: overallSaleTaxPercentage,
totalDiscount: totalDiscount,
totalAmountPaid: double.parse(totalAmountPaid),
);
}