public int CreateBill(Bill bill, IList <BillingItem> billingItems)
        {
            try
            {
                bill.CreatedDate = DateTime.Now;
                Bills.Add(bill);
                //SaveChanges();

                foreach (var item in billingItems)
                {
                    //item.BillingItemId = 0;
                    item.CreatedDate = DateTime.Now;

                    item.Bill   = bill;
                    item.BillId = bill.BillId;
                    BillingItems.Add(item);
                }

                SaveChanges();

                return(bill.BillId);
            }catch (Exception ex)
            {
                throw;
            }
            return(0);
        }
        public int UpdateBill(Bill bill, IList <BillingItem> billingItems)
        {
            try{
                var oldbill = Bills.Single(b => b.BillId == bill.BillId);

                if (oldbill == null)
                {
                    throw new Exception("Bill not available");
                }

                BillingAudits.Add(Map(oldbill));
                oldbill.OutStandingAmount = bill.OutStandingAmount;
                oldbill.Date           = bill.Date;
                oldbill.UpdatedDate    = bill.UpdatedDate;
                oldbill.ShopCustomerId = bill.ShopCustomerId;
                oldbill.TotalPrice     = bill.TotalPrice;
                oldbill.TotalQuantity  = bill.TotalQuantity;


                // SaveChanges();

                foreach (var newItem in billingItems)
                {
                    var oldItem = BillingItems.FirstOrDefault(bi => bi.BillingItemId == newItem.BillingItemId);
                    var temp    = BillingItems.First();

                    if (oldItem == null)
                    {
                        newItem.BillId        = oldbill.BillId;
                        newItem.CreatedDate   = DateTime.Now;
                        newItem.BillingItemId = 0;
                        BillingItems.Add(newItem);
                    }
                    else
                    {
                        BillingItemAudits.Add(Map(oldItem));
                        oldItem.CGST          = newItem.CGST;
                        oldItem.SGST          = newItem.SGST;
                        oldItem.ItemId        = newItem.ItemId;
                        oldItem.OriginalPrice = newItem.OriginalPrice;
                        oldItem.SellingPrice  = newItem.SellingPrice;
                        oldItem.Quantity      = newItem.Quantity;
                        oldItem.TotalPrice    = newItem.TotalPrice;
                        oldItem.UpdatedDate   = DateTime.Now;
                        oldItem.BillId        = bill.BillId;
                        //BillingItems.Add(oldItem);
                    }
                }

                SaveChanges();
            }
            catch (Exception ex)
            {
                return(0);
            }

            return(bill.BillId);
        }
        public IEnumerable <BillingItem> GetBillingItems(int billNo)
        {
            var billingItems = BillingItems.Where(b => b.BillId == billNo);

            foreach (var r in billingItems)
            {
                r.Item = Items.Single(i => i.ItemId == r.ItemId);
                r.Bill = Bills.Single(b => b.BillId == r.BillId);
            }

            return(billingItems);
        }
        public void DeleteBill(int billId)
        {
            var billingItems = BillingItems.Where(b => b.BillId == billId).ToList();

            if (!billingItems.Any())
            {
                BillingItems.RemoveRange(billingItems);
            }

            var bill = Bills.Single(b => b.BillId == billId);

            if (bill == null)
            {
                throw new Exception($"Not able find the bill for billId {billId}");
            }

            Bills.Remove(bill);

            SaveChanges();
        }