public ActionResult PendingShipping(int?page, string id, string sccess, string bllno)
        {
            PendingShipping        pendingShipping     = new PendingShipping();
            List <PendingShipping> pendingShippingList = new List <PendingShipping>();

            using (EasyBillingEntities db = new EasyBillingEntities())
            {
                List <Placed_Order> placed_Order = db.Placed_Orders.Where(z => z.Orderplaced == false).Distinct().ToList();
                if (id != null)
                {
                    Placed_Order plcdorder = db.Placed_Orders.Where(z => z.Token_number == id).Distinct().FirstOrDefault();
                    if (plcdorder.Orderplaced == false)
                    {
                        plcdorder.Orderplaced  = true;
                        plcdorder.Approve_Date = DateTime.Now.Date;
                        db.SaveChanges();
                        Billing_Master billing        = new Billing_Master();
                        Billing_Detail billing_Detail = new Billing_Detail();
                        Stockout       stockout       = new Stockout();

                        Products_For_Sale products_For_Sale = db.Products_For_Sales.Where(z => z.Token_Number == plcdorder.Item_token).FirstOrDefault();
                        //===============bill master entry=========================//
                        billing.Customer_Token_number = plcdorder.Customer_token;
                        billing.Total_tax             = (products_For_Sale.Amout_after_tax - products_For_Sale.Selling_Price) * plcdorder.Pieces;
                        billing.Rate_including_tax    = (products_For_Sale.Amout_after_tax) * plcdorder.Pieces;
                        billing.Total_discount        = decimal.Parse("0.00") * plcdorder.Pieces;
                        billing.Total_amount          = products_For_Sale.Total * plcdorder.Pieces;
                        if (plcdorder.Ispaid == true)
                        {
                            billing.Payment_Statement = "*** Paid by Paypal ***";
                        }
                        else
                        {
                            billing.Payment_Statement = "*** Pay on delivery ***";
                        }
                        //===============bill details entry=========================//

                        billing_Detail.Product_Token = plcdorder.Item_token;
                        billing_Detail.Pieces        = products_For_Sale.Pieces * plcdorder.Pieces;

                        billing_Detail.Tax       = (products_For_Sale.Amout_after_tax - products_For_Sale.Selling_Price) * plcdorder.Pieces;
                        billing_Detail.Discount  = decimal.Parse("0.00");
                        billing_Detail.Sub_Total = products_For_Sale.Total * plcdorder.Pieces;


                        //===============stockout entry=========================//

                        stockout.Product_Token = plcdorder.Item_token;
                        stockout.Pieces        = products_For_Sale.Pieces * plcdorder.Pieces;

                        stockout.CGST      = (products_For_Sale.Selling_Price * plcdorder.Pieces) + ((products_For_Sale.CGST * plcdorder.Pieces) / 100);
                        stockout.SGST      = (products_For_Sale.Selling_Price * plcdorder.Pieces) + ((products_For_Sale.SGST * plcdorder.Pieces) / 100);
                        stockout.Sub_Total = products_For_Sale.Total * plcdorder.Pieces;

                        //===========================================================//
                        billing.Billing_Details.Add(billing_Detail);
                        billing.Stockouts.Add(stockout);

                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri("http://localhost:8087/api/Billing/PostShippingBill");

                            //HTTP POST
                            var postTask = client.PostAsJsonAsync("http://localhost:8087/api/Billing/PostShippingBill", billing);
                            postTask.Wait();

                            var result = postTask.Result;
                            if (result.IsSuccessStatusCode)
                            {
                                var nmbr = result.Content.ReadAsStringAsync()
                                           .Result
                                           .Replace("\\", "")
                                           .Trim(new char[1] {
                                    '"'
                                });
                                ViewBag.billno  = nmbr;
                                ViewBag.success = "Successfully approved";
                            }
                            else
                            {
                                ViewBag.error = "Something went wrong. Please try again.";
                            }
                        }
                    }
                    else
                    {
                        ViewBag.error = "Already approved";
                    }
                }
                placed_Order = db.Placed_Orders.Where(z => z.Orderplaced == false).Distinct().ToList();
                foreach (var plcdordrpending in placed_Order)
                {
                    pendingShipping.token = plcdordrpending.Token_number;
                    if (plcdordrpending.IsUser == true)
                    {
                        pendingShipping.Customer_name = db.Users.Where(z => z.Token_number == plcdordrpending.Customer_token).Select(z => z.Name).Distinct().FirstOrDefault();
                    }
                    else
                    {
                        pendingShipping.Customer_name = db.Customers.Where(z => z.Token_number == plcdordrpending.Customer_token).Select(z => z.Customer_Name).Distinct().FirstOrDefault();
                    }
                    pendingShipping.tyreName     = db.Products_For_Sales.Where(z => z.Token_Number == plcdordrpending.Item_token).Select(z => z.Product_name).Distinct().FirstOrDefault();
                    pendingShipping.Total_Piece  = plcdordrpending.Pieces;
                    pendingShipping.Total_Price  = (db.Products_For_Sales.Where(z => z.Token_Number == plcdordrpending.Item_token).Select(z => z.Total).Distinct().FirstOrDefault()) * plcdordrpending.Pieces;
                    pendingShipping.Applied_Date = plcdordrpending.Order_Date;
                    pendingShipping.Isuser       = plcdordrpending.IsUser;
                    if (plcdordrpending.Ispaid == true)
                    {
                        pendingShipping.paymentstatement = "Paid by Paypal";
                    }
                    else
                    {
                        pendingShipping.paymentstatement = "Pay on delivery";
                    }
                    pendingShippingList.Add(pendingShipping);
                    pendingShipping = new PendingShipping();
                }
            }
            if (ViewBag.error != null)
            {
                return(RedirectToAction("PendingShipping", new { sccess = ViewBag.error }));
            }
            else if (ViewBag.success != null)
            {
                return(RedirectToAction("PendingShipping", new { sccess = ViewBag.success, bllno = ViewBag.billno, id = "" }));
            }
            else
            {
                ViewBag.success = sccess;
                ViewBag.billno  = bllno;
                ViewBag.error   = sccess;
                return(View(pendingShippingList.ToPagedList(page ?? 1, 5)));
            }
        }
        public IHttpActionResult PostProductForBill1([FromBody] FormCollection frm, [FromUri] FormDataCollection frm1)
        {
            bool status = false;

            Billing_Master        billing = new Billing_Master();
            List <Billing_Detail> detail  = new List <Billing_Detail>();
            Billing_Detail        blldtl  = new Billing_Detail();
            Stockout        stkout        = new Stockout();
            List <Stockout> stkoutlst     = new List <Stockout>();

            Barcode_Master brcdmstr = new Barcode_Master();

            if (!ModelState.IsValid)
            {
                return(BadRequest("Sorry there is some problem. Please check and try again"));
            }

            using (EasyBillingEntities dc = new EasyBillingEntities())
            {
                var    text        = dc.Billing_Masters.OrderByDescending(x => x.Billing_Number).Select(x => x.Billing_Number).Distinct().FirstOrDefault();
                var    fstfr       = text.Substring(0, 3);
                var    lstfr       = text.Substring(text.Length - 8);
                string newlstversn = (int.Parse(lstfr) + 100000001).ToString();
                string fstfr1      = (newlstversn.Substring(newlstversn.Length - 8)).ToString();
                String totalvrsn   = fstfr + fstfr1;
                billing.Billing_Number = totalvrsn;

                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

                Random rd = new Random();

                var chars1 = Enumerable.Range(0, 14)
                             .Select(x => chars[rd.Next(0, chars.Length)]);
                string barcode = new string(chars1.ToArray());

                billing.Token_Number = (Guid.NewGuid()).ToString();
                billing.Date         = DateTime.Now.Date;
                var mrchnttk = (from tkn in dc.Marchent_Accounts
                                where tkn.Email_Id == User.Identity.Name
                                select tkn.Token_number).Distinct().FirstOrDefault();
                billing.Marchent_Token_number = mrchnttk;
                var chkst = (from tkn in dc.Marchent_Accounts
                             join st in dc.States on tkn.State_Code equals st.State_Code
                             where tkn.Email_Id == User.Identity.Name
                             select new { st.SGST, st.CGST, st.IGST, st.UTGST }).Distinct().FirstOrDefault();

                billing.Total_tax          = decimal.Parse("0.00");
                billing.Rate_including_tax = decimal.Parse("0.00");
                billing.Discount_percent   = decimal.Parse("0.00");
                billing.Total_discount     = decimal.Parse("0.00");
                billing.Total_amount       = decimal.Parse("0.00");
                billing.CGST = decimal.Parse("0.00");
                billing.SGST = decimal.Parse("0.00");

                int b = 1;
                int l = 0;

                foreach (var key in frm.Keys)
                {
                    if (l % 2 == 0)
                    {
                        var k = key.ToString();

                        int a = int.Parse(k.Substring(k.Length - 1, 1));
                        if (a == b)
                        {
                        }
                        else
                        {
                            b = a;
                        }
                        var Token  = frm[a - 1];
                        var Sellon = frm[a];
                        var Quant  = int.Parse(frm[a + 1]);

                        var stockitems          = dc.Stocks.Where(x => x.Product_Token == Token).Distinct().FirstOrDefault();
                        var productforsaleitems = dc.Products_For_Sales.Where(x => x.Token_Number == Token).Distinct().FirstOrDefault();


                        if (stockitems != null)
                        {
                            stockitems.Pieces = stockitems.Pieces - (productforsaleitems.Pieces * Quant);

                            stockitems.Date = DateTime.Now.Date;

                            if (stockitems.Pieces < 0)
                            {
                                ModelState.AddModelError(string.Empty, "Please check your quantity");
                            }
                            else
                            {
                                dc.SaveChanges();
                            }
                        }

                        billing.Rate_including_tax = billing.Rate_including_tax + productforsaleitems.Amout_after_tax;
                        billing.Total_discount     = billing.Total_discount;
                        billing.Total_amount       = billing.Total_amount + productforsaleitems.Total;
                        billing.Total_tax          = billing.Total_tax + (productforsaleitems.Amout_after_tax - productforsaleitems.Selling_Price);

                        blldtl.Billing_Token_number = billing.Token_Number;
                        blldtl.Billing_number       = billing.Billing_Number;
                        blldtl.Date          = billing.Date;
                        blldtl.Product_Token = Token;
                        blldtl.Pieces        = productforsaleitems.Pieces * Quant;

                        blldtl.Amount           = productforsaleitems.Selling_Price;
                        blldtl.Taxable_amount   = productforsaleitems.Pieces * productforsaleitems.Selling_Price;
                        blldtl.Tax              = productforsaleitems.Amout_after_tax - productforsaleitems.Selling_Price;
                        blldtl.Discount         = decimal.Parse("0.00");
                        blldtl.Discount_percent = ((productforsaleitems.Amout_after_tax - productforsaleitems.Total) * 100) / productforsaleitems.Amout_after_tax;
                        blldtl.Sub_Total        = productforsaleitems.Total;

                        stkout.Billing_Token_number = billing.Token_Number;
                        stkout.Billing_number       = billing.Billing_Number;
                        stkout.Date          = billing.Date;
                        stkout.Product_Token = Token;
                        stkout.Pieces        = productforsaleitems.Pieces * Quant;

                        stkout.CGST                  = (productforsaleitems.Selling_Price * Quant) + ((productforsaleitems.CGST * Quant) / 100);
                        stkout.SGST                  = (productforsaleitems.Selling_Price * Quant) + ((productforsaleitems.SGST * Quant) / 100);
                        stkout.Sub_Total             = productforsaleitems.Total;
                        stkout.Marchent_Token_number = mrchnttk;

                        detail.Add(blldtl);
                        stkoutlst.Add(stkout);
                    }
                    l++;
                }
                if (chkst.SGST == true && chkst.CGST == true)
                {
                    billing.CGST = billing.Total_tax / 2;
                    billing.SGST = billing.CGST;
                }
                else if (chkst.CGST == true && chkst.IGST == true)
                {
                    billing.CGST = billing.Total_tax / 2;
                }
                else if (chkst.CGST == true && chkst.UTGST == true)
                {
                    billing.CGST = billing.Total_tax / 2;
                }
                else if (chkst.SGST == true && chkst.IGST == true)
                {
                    billing.SGST = billing.Total_tax / 2;
                }
                else if (chkst.SGST == true && chkst.UTGST == true)
                {
                    billing.SGST = billing.Total_tax / 2;
                }

                else
                {
                }

                brcdmstr.Barcode_Number       = barcode;
                brcdmstr.Billing_Number       = billing.Billing_Number;
                brcdmstr.Billing_Token_number = billing.Token_Number;
                brcdmstr.Date = billing.Date;

                billing.Barcode_Number = barcode;

                // image save for barcode///

                using (MemoryStream ms = new MemoryStream())
                {
                    //The Image is drawn based on length of Barcode text.
                    using (Bitmap bitMap = new Bitmap(barcode.Length * 30, 90))
                    {
                        //The Graphics library object is generated for the Image.
                        using (Graphics graphics = Graphics.FromImage(bitMap))
                        {
                            //The installed Barcode font.
                            Font   oFont = new Font("IDAutomationHC39M", 17);
                            PointF point = new PointF(2f, 2f);

                            //White Brush is used to fill the Image with white color.
                            SolidBrush whiteBrush = new SolidBrush(Color.White);
                            graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);

                            //Black Brush is used to draw the Barcode over the Image.
                            SolidBrush blackBrush = new SolidBrush(Color.Black);
                            graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
                        }

                        //The Bitmap is saved to Memory Stream.
                        bitMap.Save(ms, ImageFormat.Png);

                        //The Image is finally converted to Base64 string.


                        byte[] imageBytes = Convert.FromBase64String(Convert.ToBase64String(ms.ToArray()));

                        MemoryStream ms1 = new MemoryStream(imageBytes, 0, imageBytes.Length);
                        ms1.Write(imageBytes, 0, imageBytes.Length);

                        System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                        string spath = System.Web.HttpContext.Current.Server.MapPath("~/BillBarcode");
                        string path  = spath + "/" + billing.Billing_Number + ".jpg";
                        image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

                        //p.photo = imageBytes;
                        billing.Barcode_photo = imageBytes;
                        brcdmstr.Image        = imageBytes;
                    }
                }
                billing.Billing_Details = detail;
                billing.Stockouts       = stkoutlst;
                billing.Barcode_Master.Add(brcdmstr);

                dc.Billing_Masters.Add(billing);
                dc.SaveChanges();
                status = true;
            }

            return(Ok());
        }