removeFromDoc method

Future<FirestoreErrors?> removeFromDoc()

Implementation

Future<FirestoreErrors?> removeFromDoc() async {
  if (id == null || !isEditable) return null;
  final compneyID = B2bAuthUser.inUse?.compneyIdInUse;
  if (compneyID == null) return null;
  if (B2bAuthUser.inUse?.hasOwnerPermission != 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.arrayRemove([id]),
      ...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),
    },
  );
}