createArticleRetailFromForm method

Future createArticleRetailFromForm()
inherited

Implementation

Future<ArticleRetail> createArticleRetailFromForm() async {
  final now = DateTime.now();
  ArticleRetail newArticleRetail = ArticleRetail(
    calibreId: _calibre.id,
    id: _calibre.articles.nextId,
    fullName: name.trim(),
    price: num.parse(price.trim()),
    cost: 0,
    weight: 1,
    barcodeEAN: barcodeEAN.trim(),
    articleCode: _articlesStore.calibres.nextId * 10 + 1,
    creationDate: now,
    updateDate: now,
    status: true,
    statusUpdateDate: now,
  );
  if (photoPath.isNotEmpty) {
    final photo = ArticlePhoto(
        calibreId: newArticleRetail.calibreId,
        id: newArticleRetail.id,
        path: photoPath,
        source: PhotoSource.file);
    await _articlesStore.upsertPhoto(photo);
  }
  if ((cost.isNotEmpty)) {
    newArticleRetail =
        newArticleRetail.copyWith(cost: num.parse(cost.trim()));
  }
  if ((unitsPerPiece.isNotEmpty)) {
    newArticleRetail =
        newArticleRetail.copyWith(weight: double.parse(unitsPerPiece.trim()));
  }
  if (barcodeEAN.isNotEmpty && int.tryParse(barcodeEAN.trim()) != null) {
    newArticleRetail =
        newArticleRetail.copyWith(barcodeEAN: barcodeEAN.trim());
  }

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