public ActionResult OnGet(int BhId)
        {
            try
            {
                COD = SD.COD;
                Bi  = _db.ExternalBillItems
                      .Include(bill => bill.ExternalBillHeader)
                      .Include(bill => bill.ExternalBillHeader.Customer)
                      .Include(bill => bill.ProdInfo)
                      .Include(bill => bill.ExternalBillHeader.ApplicationUser)
                      .Where(bill => bill.HeaderId == BhId).ToList();

                ExternalBillItems BillItem = Bi[0];

                ExternalBillHeader = BillItem.ExternalBillHeader;

                CustomerName = BillItem.ExternalBillHeader.Customer.CompanyName;
                CustomerId   = BillItem.ExternalBillHeader.Customer.Id;
                Warehouse Wh = _db.Warehouse.FirstOrDefault(wh => wh.Id == BillItem.WhId);
                SelectedWh = Wh.WhName;
                WhId       = Wh.Id;

                UnitPriceTypesList = _db.PricingType.ToList();

                PaymentTermsList.Add(SD.COD);
                PaymentTermsList.Add(SD.Postpaid);
            }
            catch
            {
            }
            return(Page());
        }
        public async Task <string> CreateExternalBillOLD(ExternalBillHeader ExternalHeader, List <ExternalBillItems> ExternalBill, int WhId)
        {
            try
            {
                ShowRoom = _db.Warehouse.Include(wh => wh.WhType).FirstOrDefault(wh => wh.WhType.Type == SD.StoreRoom);

                ExternalHeader.CreatedById     = GetLoggedInUserId();
                ExternalHeader.CreatedDataTime = DateTime.Now;

                // double TotalAmt = 0;

                // getting the unit price of each item in the bill items
                //foreach (ExternalBillItems item in ExternalBill)
                //{

                //    TotalAmt += item.TotalAmt;
                //}

                //// price before discount
                //ExternalHeader.TotalAmt = TotalAmt;

                //// in case there is a discount
                //TotalAmt = TotalAmt - ExternalHeader.Discount;
                //ExternalHeader.TotalNetAmt = TotalAmt;

                if (ExternalHeader.TotalNetAmt == ExternalHeader.PaidAmt)
                {
                    ExternalHeader.Status = SD.Completed;
                }
                else
                {
                    ExternalHeader.Status = SD.OpenBill;
                }


                _db.ExternalBillHeader.Add(ExternalHeader);

                await _db.SaveChangesAsync();

                // updatinga customer Acc
                double DebtAmt = ExternalHeader.TotalNetAmt - ExternalHeader.PaidAmt;
                UpdateCustomerAcc(ExternalHeader.CustId ?? 0, ExternalHeader.PaidAmt, DebtAmt, "New");

                // add a new payment to bill payments table
                // AddBillPayment(Header.CustId ?? 0, Header.Id, Header.PaidAmt);


                // Creating Bill items
                foreach (ExternalBillItems item in ExternalBill)
                {
                    ExternalBillItems Bill = new ExternalBillItems
                    {
                        HeaderId  = ExternalHeader.Id,
                        ProdName  = item.ProdName,
                        Qty       = item.Qty,
                        UnitPrice = item.UnitPrice,
                        TotalAmt  = item.UnitPrice * item.Qty,
                        Note      = item.Note
                    };
                    _db.ExternalBillItems.Add(Bill);
                }


                await _db.SaveChangesAsync();


                return("تمت اضافة فاتورة مبيعات جديدة");
            }

            catch
            {
                return("حصل خطأ لم يتم اضافة الفاتورة");
            }
        }
        // External Billing

        // this function will create an external bill.. external bill is a bill issues for a customers
        // for purchasing items that do not belong to the company. basically it is supplied by another or a
        // third party company. creating external bills will have no effect on Inventory at all

        public async Task <string> CreateExternalBill(ExternalBillHeader ExternalHeader, List <ExternalBillItems> ExternalBill, int WhId, string Type, int?OldBhId)
        {
            try
            {
                ExternalHeader.Id              = new int();
                ExternalHeader.CreatedById     = GetLoggedInUserId();
                ExternalHeader.CreatedDataTime = DateTime.Now;

                // if the amount is fully paid then the bill is completed, otherwise is open
                if (ExternalHeader.TotalNetAmt == ExternalHeader.PaidAmt)
                {
                    ExternalHeader.Status = SD.Completed;
                }
                else
                {
                    ExternalHeader.Status = SD.OpenBill;
                }


                _db.ExternalBillHeader.Add(ExternalHeader);

                await _db.SaveChangesAsync();

                // updatinga customer Acc
                double DebtAmt = ExternalHeader.TotalNetAmt - ExternalHeader.PaidAmt;
                UpdateCustomerAcc(ExternalHeader.CustId ?? 0, ExternalHeader.PaidAmt, DebtAmt, "New");

                // add a new payment to bill payments table
                AddExternalBillPayment(ExternalHeader.CustId ?? 0, ExternalHeader.Id, ExternalHeader.PaidAmt);


                // Creating Bill items
                foreach (ExternalBillItems item in ExternalBill)
                {
                    ExternalBillItems Bill = new ExternalBillItems
                    {
                        HeaderId  = ExternalHeader.Id,
                        Qty       = item.Qty,
                        UnitPrice = item.UnitPrice,
                        WhId      = WhId,
                        TotalAmt  = item.UnitPrice * item.Qty,
                        Note      = item.Note
                    };

                    if (item.IsExternal == false)
                    {
                        Bill.ProdId     = item.ProdId;
                        Bill.IsExternal = false;
                        // decrease stock qty of that item
                        DecreaseStockQty(Bill.ProdId ?? 0, WhId, Bill.Qty);

                        // create inv transaction
                        CreateInvTransaction(Bill.ProdId ?? 0, WhId, Bill.Qty, ExternalHeader.Id, SD.Sales);
                        _db.ExternalBillItems.Add(Bill);
                    }
                    else
                    {
                        ExternalHeader.HasExternalProd = true;
                        Bill.CostPrice = item.CostPrice;
                        if (Type == "Edit")
                        {
                            Bill.ProdName = item.ProdName;
                        }
                        else
                        {
                            Bill.ProdName = item.ProdInfo.ProdCode;
                        }

                        Bill.IsExternal = true;
                        _db.ExternalBillItems.Add(Bill);
                    }
                }


                await _db.SaveChangesAsync();

                if (Type == "Edit")
                {
                    DeleteExternalBill(OldBhId ?? 0).GetAwaiter().GetResult();
                    return("تم التعديل على الفاتورة");
                }

                return("تمت اضافة فاتورة مبيعات جديدة");
            }

            catch (Exception x)
            {
                try
                {
                    _db.ExternalBillHeader.Remove(ExternalHeader);
                    _db.ExternalBillItems.RemoveRange(ExternalBill);
                    _db.SaveChanges();
                }
                catch
                {
                }
                return("Error! حصل خطأ لم يتم اضافة الفاتورة");
            }
        }