createArticleRetailFromForm method

Future<ArticleRetail> createArticleRetailFromForm()
inherited

Implementation

Future<ArticleRetail> createArticleRetailFromForm() async {
  final now = DateTime.now();
  ArticleRetail newArticleRetail = ArticleRetail(
    calibreId: _calibre.id,
    id: _calibre.articles.nextId,
    kind: ArticleKind.retail,
    designation: name.trim(),
    price: num.parse(price.trim()),
    cost: 0,
    unitsInOnePiece: 1,
    barcodeEAN: barcodeEAN.trim(),
    creationDate: now,
    status: true,
  );
  if (photoPath.isNotEmpty) {
    final localPhotoFilePath = photoSaver
        .copyPhotoFile(newArticleRetail.designation, photoPath)
        .path;

    final photo = ArticlePhoto(
        calibreId: newArticleRetail.calibreId,
        id: newArticleRetail.id,
        path: localPhotoFilePath,
        source: PhotoSource.file,
        dateUTC: now.toUtc());
    await _articlesStore.upsertPhoto(photo);
  }
  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());
  }

  final articleRetailCreated = await _articlesStore
      .createArticleRetail<ArticleRetail>(newArticleRetail);
  return articleRetailCreated;
}