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());
        }
        public IHttpActionResult PostShippingBill([FromBody] Billing_Master billing)
        {
            bool           status   = false;
            Barcode_Master brcdmstr = new Barcode_Master();

            using (EasyBillingEntities dc = new EasyBillingEntities())
            {
                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();
                bool ischk = dc.Billing_Masters.Any();
                if (ischk == false)
                {
                    billing.Billing_Number = "INV00000001";
                }
                else
                {
                    var text = (from a in dc.Billing_Masters
                                orderby a.Billing_Number descending
                                select a.Billing_Number).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;
                }
                billing.Date = DateTime.Now.Date;
                var mrchnttk = (from tkn in dc.Marchent_Accounts
                                where tkn.Email_Id == "*****@*****.**" || tkn.Email_Id == "*****@*****.**"
                                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 == "*****@*****.**" || tkn.Email_Id == "*****@*****.**"
                             select new { st.SGST, st.CGST, st.IGST, st.UTGST }).Distinct().FirstOrDefault();
                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
                {
                }
                billing.Narration = "Thank you for purchase. Please visit our shop again...";

                var dtls    = billing.Billing_Details.Distinct().ToList();
                var stkouts = billing.Stockouts.Distinct().ToList();

                foreach (var echdtls in dtls)
                {
                    Products_For_Sale prdct = dc.Products_For_Sales.Where(a => a.Token_Number == echdtls.Product_Token).Distinct().FirstOrDefault();

                    if (prdct != null)
                    {
                        echdtls.Billing_Token_number = billing.Token_Number;
                        echdtls.Billing_number       = billing.Billing_Number;
                        echdtls.Date          = DateTime.Now.Date;
                        echdtls.Product_Token = prdct.Token_Number;
                        if (echdtls.Pieces == prdct.Pieces)
                        {
                            billing.Rate_including_tax = billing.Rate_including_tax + prdct.Amout_after_tax;
                            echdtls.Amount             = prdct.Selling_Price;
                            echdtls.Discount_percent   = (((prdct.Amout_after_tax) - (prdct.Total)) * 100) / (prdct.Amout_after_tax);
                        }
                        else
                        {
                            int pcsincrs = echdtls.Pieces / prdct.Pieces;
                            billing.Rate_including_tax = billing.Rate_including_tax = billing.Rate_including_tax + (prdct.Amout_after_tax * pcsincrs);
                            echdtls.Amount             = prdct.Selling_Price * pcsincrs;
                            echdtls.Discount_percent   = (((prdct.Amout_after_tax * pcsincrs) - (prdct.Total * pcsincrs)) * 100) / (prdct.Amout_after_tax * pcsincrs);
                        }
                    }
                }
                foreach (var stks in stkouts)
                {
                    stks.Billing_Token_number = billing.Token_Number;
                    stks.Date                  = DateTime.Now.Date;
                    stks.Billing_number        = billing.Billing_Number;
                    stks.Marchent_Token_number = billing.Marchent_Token_number;
                }
                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, ImageFormat.Jpeg);

                        //p.photo = imageBytes;
                        billing.Barcode_photo = imageBytes;
                        brcdmstr.Image        = imageBytes;
                    }
                }

                billing.Barcode_Master.Add(brcdmstr);

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

            return(Ok(billing.Billing_Number));
        }
        public IHttpActionResult PostNewMerchant([FromBody] Billing_Master billing)
        {
            bool           status   = false;
            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 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();

                var dtls = billing.Billing_Details.Distinct().ToList();
                foreach (var echdtls in dtls)
                {
                    bool chk = dc.Stocks.Where(a => a.Product_Token == echdtls.Product_Token).Any();
                    if (chk == true)
                    {
                        Stock stk = dc.Stocks.Where(a => a.Product_Token == echdtls.Product_Token).FirstOrDefault();
                        stk.Date   = echdtls.Date;
                        stk.Pieces = stk.Pieces - echdtls.Pieces;

                        stk.Product_Token = echdtls.Product_Token;

                        if (stk.Pieces < 0)
                        {
                            return(BadRequest("Sorry there is some problem. Please check and try again"));
                        }
                        dc.SaveChanges();
                    }
                    else
                    {
                        return(NotFound());
                    }
                }

                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.Barcode_Master.Add(brcdmstr);

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

            return(Ok());
        }