addPrices method

  1. @protected
Future<void> addPrices({
  1. required String bearerToken,
  2. required int proofId,
  3. required LocalDatabase localDatabase,
})

Implementation

@protected
Future<void> addPrices({
  required String bearerToken,
  required int proofId,
  required LocalDatabase localDatabase,
}) async {
  // Now we split: one item, one task
  final bool split = barcodes.length > 1;
  if (split) {
    for (int i = 0; i < barcodes.length; i++) {
      final String barcode = barcodes[i];
      await PriceToOxF.updateOxF(
        localDatabase: localDatabase,
        initialBarcodes: <String>[barcode],
        locationOSMId: locationOSMId,
        locationOSMType: locationOSMType,
      );
      await BackgroundTaskAddOtherPrice.addTask(
        context: null,
        localDatabase: localDatabase,
        proofId: proofId,
        date: date,
        currency: currency,
        locationOSMId: locationOSMId,
        locationOSMType: locationOSMType,
        barcodes: <String>[barcode],
        categories: <String>[categories[i]],
        origins: <List<String>>[origins[i]],
        labels: <List<String>>[labels[i]],
        pricePers: <String>[pricePers[i]],
        pricesAreDiscounted: <bool>[pricesAreDiscounted[i]],
        prices: <double>[prices[i]],
        pricesWithoutDiscount: <double?>[pricesWithoutDiscount[i]],
        discountTypes: <String>[discountTypes[i]],
      );
    }
    return;
  }
  // For the record, we have only one item here anyway.
  for (int i = 0; i < barcodes.length; i++) {
    final String barcode = barcodes[i];
    final bool isProduct = barcode.isNotEmpty;
    final bool priceIsDiscounted = pricesAreDiscounted[i];
    double price = prices[i];
    double? priceWithoutDiscount = pricesWithoutDiscount[i];
    final DiscountType? discountType = priceIsDiscounted
        ? DiscountType.fromOffTag(discountTypes[i])
        : null;
    if (priceIsDiscounted) {
      if (priceWithoutDiscount != null) {
        if (price > priceWithoutDiscount) {
          final double tmp = price;
          price = priceWithoutDiscount;
          priceWithoutDiscount = tmp;
        } else if (price == priceWithoutDiscount) {
          priceWithoutDiscount = null;
        }
      }
    }
    final Price newPrice = Price()
      ..date = date
      ..currency = currency
      ..locationOSMId = locationOSMId
      ..locationOSMType = locationOSMType
      ..proofId = proofId
      ..productCode = isProduct ? barcode : null
      ..categoryTag = isProduct ? null : categories[i]
      ..originsTags = isProduct ? null : origins[i]
      ..labelsTags = isProduct ? null : labels[i]
      ..pricePer = isProduct ? null : PricePer.fromOffTag(pricePers[i])
      ..type = isProduct ? PriceType.product : PriceType.category
      ..priceIsDiscounted = priceIsDiscounted
      ..price = _fixPriceDecimals(price)
      ..discountType = discountType
      ..priceWithoutDiscount = priceWithoutDiscount == null
          ? null
          : _fixPriceDecimals(priceWithoutDiscount);

    // create price
    final MaybeError<Price?> addedPrice =
        await OpenPricesAPIClient.createPrice(
          price: newPrice,
          bearerToken: bearerToken,
          uriHelper: ProductQuery.uriPricesHelper,
        );
    if (addedPrice.isError) {
      throw Exception('Could not add price: ${addedPrice.error}');
    }
    if (isProduct) {
      ProductPriceRefresher.setLatestUpdate(barcode);
    }
  }
  localDatabase.notifyListeners();
}