public async Task <IActionResult> RecordSales(SalesRecordModel data)
        {
            try
            {
                var salesHistory = new List <InventoryActivityLog>();
                if (data != null && data.Products != null)
                {
                    foreach (var item in data.Products)
                    {
                        var qty     = item.Quantity;
                        var product = _db.Product.FirstOrDefault(pr => pr.ProductId == item.ProductId);
                        if (item.SaleTypeId == SalesTypes.BulkPurchase)
                        {
                            qty = product.BulkUnits * item.Quantity;
                        }

                        if (product.Quantity < item.Quantity)
                        {
                            return(Ok(new { success = false, errors = new List <string> {
                                                $"Selected Quantity for {item.ProductName} is more than available amount"
                                            } }));
                        }
                        product.Quantity -= qty;

                        var activityLog = new InventoryActivityLog
                        {
                            Quantity          = item.Quantity,
                            ProductId         = item.ProductId,
                            InventoryActionId = InventoryActionType.Purchase,
                            CreatedAt         = DateTime.Now,
                            TotalAmount       = item.TotalAmount
                        };
                        salesHistory.Add(activityLog);
                    }
                    ;
                    salesHistory.ForEach(s =>
                    {
                        _db.InventoryActivityLog.Add(s);
                    });
                    //save sales record HERE
                    var record = data.ToSalesRecord();
                    var items  = data.Products.Select(p => p.ToSalesRecordItem()).ToList();

                    _db.SalesRecord.Add(record);
                    await _db.SaveChangesAsync();

                    items.ForEach(i =>
                    {
                        i.SalesRecordId = record.SalesRecordId;
                        _db.SalesRecordItem.Add(i);
                    });
                    await _db.SaveChangesAsync();

                    return(Ok(new { success = true }));
                }
            }
            catch (Exception e)
            {
                _logger.logError(e);
            }
            return(Ok(new { success = false, errors = new List <string> {
                                $"Sorry, an error occured, please try again"
                            } }));
        }
        public async Task <IActionResult> Create(ProductModel data)
        {
            try
            {
                long productId = 0;
                //  var prodcodelower = data.Barcode.ToLower();
                var qty = data.Quantity * data.BulkUnits;
                ///var qty = data.Quantity;
                var unitCost = data.Cost / qty;
                if (_db.Product.Any(d => d.ProductId == data.ProductId && d.Active == true))
                {
                    var entity = _db.Product.FirstOrDefault(d => d.ProductId == data.ProductId && d.Active == true);
                    entity.Name        = data.Name;
                    entity.CategoryId  = data.CategoryId;
                    entity.TypeId      = data.TypeId;
                    entity.Description = data.Description;
                    entity.Cost        = unitCost;
                    entity.UnitPrice   = data.UnitPrice;
                    entity.Quantity   += qty;
                    productId          = entity.ProductId;
                    entity.BulkUnits   = data.BulkUnits;
                    entity.BulkPrice   = data.BulkPrice;
                }
                else
                {
                    var entity = new Product
                    {
                        Name        = data.Name,
                        CategoryId  = data.CategoryId,
                        TypeId      = data.TypeId,
                        Description = data.Description,
                        Cost        = unitCost,
                        UnitPrice   = data.UnitPrice,
                        Quantity    = qty,
                        //    Barcode = data.Barcode,
                        Active    = true,
                        BulkUnits = data.BulkUnits,
                        BulkPrice = data.BulkPrice
                    };
                    await _db.Product.AddAsync(entity);

                    await _db.SaveChangesAsync();

                    productId = entity.ProductId;
                }
                var activityLog = new InventoryActivityLog
                {
                    InventoryActionId = 1,
                    CreatedAt         = DateTime.Now,
                    TotalAmount       = data.Cost,
                    Quantity          = qty,
                    ProductId         = productId
                };
                _db.InventoryActivityLog.Add(activityLog);
                await _db.SaveChangesAsync();

                return(Ok(new { Success = true }));
            }
            catch (Exception e)
            {
                _logger.logError(e);
            }
            return(Ok(new { Success = false }));
        }