addEntryToDoc method

Future<FirestoreErrors?> addEntryToDoc(
  1. BuildContext context
)

Implementation

Future<FirestoreErrors?> addEntryToDoc(BuildContext context) async {
  final now = DateTimeString.fromDateTime(DateTime.now());
  final date = await showDatePicker(
    context: context,
    initialDate: now.dateTime,
    firstDate: now.firstDayOfMonth,
    lastDate: now.lastDayOfMonth,
  );
  if (date == null) return null;
  final json = _toJson();
  json["-bTD"] = date.toIso8601String();
  if (id != null) return null;
  final compneyID = B2bAuthUser.inUse?.compneyIdInUse;
  if (compneyID == null) return null;
  if (B2bAuthUser.inUse?.hasWorkerPermission != true) return null;
  final buyInPayment = buyIn;
  final sellOutPayment = sellOut;
  final boxesInDue = dueBoxes;
  final walletChanges = _walletChanges;
  final boxChanges = _boxChanges;
  final invChanges = <String, FieldValue>{};
  final changesApplyed = _inventoryChangesApplyed;
  if (changesApplyed != null) {
    for (var e in changesApplyed) {
      invChanges["inventory.${e.id}.q"] =
          FieldValue.increment(e.quntity.quntity);
      invChanges["inventory.${e.id}.p"] =
          FieldValue.increment(e.pack.quntity);
    }
  }
  return await firestore.updateDoc(
    docPath: "B2B/$compneyID/DATA/STATE",
    data: {
      "entries": FieldValue.arrayUnion([jsonEncode(json)]),
      ...invChanges,
      if (buyInPayment != null)
        "buyInDue.${buyInPayment.sellerID}": FieldValue.increment(
          (buyInPayment.due ? 1 : -1) * buyInPayment.amount.money,
        ),
      if (sellOutPayment != null)
        "sellOutDue.${sellOutPayment.buyerNumber}.m": FieldValue.increment(
          (sellOutPayment.due ? 1 : -1) * sellOutPayment.amount.money,
        ),
      if (boxesInDue != null)
        "sellOutDue.${boxesInDue.buyerNumber}.b": FieldValue.increment(
          (boxesInDue.due ? 1 : -1) * boxesInDue.boxes.quntity,
        ),
      if (walletChanges != null)
        "walletMoney": FieldValue.increment(walletChanges),
      if (boxChanges != null) "boxes": FieldValue.increment(boxChanges),
    },
  );
}