updateArticleRetailFromForm method

Future<ArticleRetail> updateArticleRetailFromForm({
  1. bool isLinkedAndIsInternetAvailable = true,
})
inherited

Implementation

Future<ArticleRetail> updateArticleRetailFromForm(
    {bool isLinkedAndIsInternetAvailable = true}) async {
  final now = DateTime.now();
  ArticleRetail newArticleRetail = _articleRetail.copyWith(
    designation: name.trim(),
    price: num.parse(price.trim()),
    barcodeEAN: barcodeEAN.trim(),
    updateDate: now,
  );

  final calibre = _articlesStore.calibresFull
      .firstWhereOrNull((e) => e.id == newArticleRetail.calibreId);
  if (calibre == null) {
    throw 'no calibre match id: ${newArticleRetail.calibreId}';
  }

  /// ARTICLE
  if (cost.isNotEmpty) {
    newArticleRetail =
        newArticleRetail.copyWith(cost: num.parse(cost.trim()));
  }
  if (unitsInOnePiece.isNotEmpty) {
    newArticleRetail = newArticleRetail.copyWith(
        unitsInOnePiece: double.parse(unitsInOnePiece.trim()));
  }
  if (barcodeEAN.isNotEmpty) {
    newArticleRetail =
        newArticleRetail.copyWith(barcodeEAN: barcodeEAN.trim());
  }
  if (isSingle) {
    final temp = CalibreWeebi<ArticleRetail>(
        id: calibre.id,
        title: newArticleRetail
            .designation, // important designation carries the title since single
        articles: [newArticleRetail],
        kind: calibre.kind,
        stockUnit: stockUnit,
        creationDate: calibre.creationDate,
        updateDate: now,
        status: calibre.status,
        statusUpdateDate: calibre.statusUpdateDate);
    await _articlesStore.updateCalibre<ArticleRetail>(temp,
        isInternetAvailable: isLinkedAndIsInternetAvailable);
  } else {
    await _articlesStore.updateArticle<ArticleRetail>(newArticleRetail,
        isInternetAvailable: isLinkedAndIsInternetAvailable);
  }

  // PHOTO

  if (photoPath != initialPhotoPath) {
    if (photoPath.isNotEmpty) {
      // check it is not a demo product
      if (calibre.creationDate != DatesWeebi.defaultDate) {
        final localPhotoFilePath = photoSaver
            .copyPhotoFile(newArticleRetail.designation, photoPath)
            .path;
        final newPhoto = ArticlePhoto(
            calibreId: newArticleRetail.calibreId,
            id: newArticleRetail.id,
            path: localPhotoFilePath,
            source: PhotoSource.file,
            dateUTC: now.toUtc());
        try {
          await _articlesStore.upsertPhoto(newPhoto,
              isInternetAvailable: isLinkedAndIsInternetAvailable);
        } catch (e) {
          print(e);
        }
      }
    } else {
      // photo has been removed
      print('in the delete photo');
      if (_articlesStore.photos.any((p) =>
          p.calibreId == newArticleRetail.calibreId &&
          p.id == newArticleRetail.id)) {
        final photo = _articlesStore.photos.firstWhereOrNull((p) =>
            p.calibreId == newArticleRetail.calibreId &&
            p.id == newArticleRetail.id);
        if (photo != null) {
          await _articlesStore.deletePhoto(photo, isInternetAvailable: false);
        }
      }
    }
  }

  // CATEGORY
  for (final categoryStore in _articlesStore.categories) {
    // calibre was included in category
    if (categoryStore.calibresIds.contains(newArticleRetail.calibreId)) {
      // but removed in update view so we need to update category
      if (categories.none((c) => c.title == categoryStore.title)) {
        final calibreIds = categoryStore.calibresIds
          ..remove(newArticleRetail.calibreId);
        final c = categoryStore.copyWith(calibresIds: calibreIds);
        await _articlesStore.updateCategory(c);
      }
      // not changed do nothing
    }
    // add the ones added in update view
  }
  if (categories.isNotEmpty) {
    for (final category in categories) {
      final calibreIds = category.calibresIds
        ..add(newArticleRetail.calibreId);
      final c = category.copyWith(calibresIds: calibreIds);
      await _articlesStore.updateCategory(c);
    }
  }

  return newArticleRetail;
}