// Make payment on external bills
        public async Task <string> MakePaymentOnExternalBill(int ExternalHeaderId, double NewPaymentAmt)
        {
            try
            {
                ExternalBillHeader ExternalHeader = _db.ExternalBillHeader.FirstOrDefault(h => h.Id == ExternalHeaderId);

                CustAcc Acc = _db.CustAcc.FirstOrDefault(ac => ac.CustId == ExternalHeader.CustId);

                // updating bill header will the payment
                ExternalHeader.PaidAmt += NewPaymentAmt;

                // if the bill paid all, change status to completed
                if (ExternalHeader.TotalNetAmt == ExternalHeader.PaidAmt)
                {
                    ExternalHeader.Status = SD.Completed;
                }

                // updating customer Acc
                //Acc.Paid += PaidAmt;
                //Acc.Debt -= PaidAmt;
                UpdateCustomerAcc(ExternalHeader.CustId ?? 0, NewPaymentAmt, NewPaymentAmt, "Old");

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

                await _db.SaveChangesAsync();

                return("تمت عملية الدفع");
            }
            catch (Exception ex)
            {
                return("لم تتم عملية الدفع");
            }
        }
Пример #2
0
        public ActionResult OnPost(ExternalBillHeader ExternalBillHeader)
        {
            string path = Request.Host.Value;

            if (ExternalBillHeader.Id != 0)
            {
                //return RedirectToPage("/Sales/Billings/PrintBill", new { BhId = ExternalBillHeader.Id });

                var body = RazorPage.RenderToString("http://" + path + "/Sales/ExternalBillings/InvoicePrint?BhId=" + ExternalBillHeader.Id);

                var converter = new HtmlToPdf();
                converter.Options.PdfPageSize        = PdfPageSize.A4;
                converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
                converter.Options.WebPageWidth       = 1024;
                converter.Options.WebPageHeight      = 0;
                converter.Options.WebPageFixedSize   = false;

                converter.Options.AutoFitWidth  = HtmlToPdfPageFitMode.ShrinkOnly;
                converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.NoAdjustment;
                // converter.Options.PdfPageCustomSize = new System.Drawing.SizeF(816, 1020);
                PdfDocument doc = converter.ConvertHtmlString(body, "http://" + path + "/Sales/ExternalBillings/InvoicePrint?BhId=" + ExternalBillHeader.Id);

                byte[] pdf = doc.Save();
                doc.Close();

                return(new FileContentResult(pdf, "application/pdf")
                {
                    FileDownloadName = ExternalBillHeader.Id + "-" + "فاتورة.pdf"
                });
            }
            return(RedirectToPage("/Sales/ExternalBillings/Details", new { BhId = ExternalBillHeader.Id }));
        }
        // this function will close an external bill manually
        public async Task <string> CloseExternalBillManually(int ExternalHeaderId)
        {
            try
            {
                ExternalBillHeader ExternalHeader = _db.ExternalBillHeader.FirstOrDefault(h => h.Id == ExternalHeaderId);
                ExternalHeader.Status = SD.Completed;
                await _db.SaveChangesAsync();

                return("تم اغلاق الفاتورة بنجاح");
            }
            catch
            {
                return("Error! حصل خطأ لم يتم اغلاق الفاتورة");
            }
        }
        public async Task <ActionResult> OnGet(int BhId)
        {
            ExternalBillHeader ExternalBillHeader = await _db.ExternalBillHeader.Include(h => h.Customer)
                                                    .FirstOrDefaultAsync(h => h.Id == BhId);

            ExternalHeaderId = ExternalBillHeader.Id;
            CompanyName      = ExternalBillHeader.Customer.CompanyName;
            TotalNetAmt      = ExternalBillHeader.TotalNetAmt;
            PaidAmt          = ExternalBillHeader.PaidAmt;
            //NewPayment =;
            BalanceAmt = TotalNetAmt - PaidAmt;


            return(Page());
        }
        public async Task <string> DeleteExternalBill(int HeaderId)
        {
            try
            {
                ExternalBillHeader       ExternalHeader = _db.ExternalBillHeader.FirstOrDefault(h => h.Id == HeaderId);
                List <ExternalBillItems> BillItemList   = _db.ExternalBillItems.Where(i => i.HeaderId == HeaderId).ToList();

                // reverting customer Acc
                double DebtAmt = ExternalHeader.TotalNetAmt - ExternalHeader.PaidAmt;
                RevertCustomerAcc(ExternalHeader.CustId ?? 0, ExternalHeader.PaidAmt, DebtAmt);

                // remove bill payment of bill payments table
                DeleteBillPayment(ExternalHeader.Id);

                // Creating Bill items
                foreach (ExternalBillItems Item in BillItemList)
                {
                    if (Item.IsExternal == false)
                    {
                        // revert stock qty of that item
                        RevertStockQty(Item.ProdId ?? 0, Item.WhId, Item.Qty);
                    }
                }
                // remove inv transaction list from InvTransaction Table
                DeleteInvTransaction(HeaderId, SD.Sales);

                // removing bill item list
                _db.ExternalBillItems.RemoveRange(BillItemList);

                //remove header
                _db.ExternalBillHeader.RemoveRange(ExternalHeader);

                await _db.SaveChangesAsync();


                return("تم حذف الفاتورة");
            }

            catch
            {
                return("Error! حصل خطأ لم يتم حذف الفاتورة");
            }
        }
        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! حصل خطأ لم يتم اضافة الفاتورة");
            }
        }