void ThreadPrint()
        {
            int number = int.Parse(textBoxDocQty.Text.Trim());
            using (SSLsEntities db = new SSLsEntities())
            {
                foreach (var inv in _Inv)
                {
                    for (int i = 0; i < number; i++)
                    {
                        #region Print Invoice From Number
                        var createBy = Singleton.SingletonAuthen.Instance().Id;
                        var branch = Singleton.SingletonAuthen.Instance().MyBranch;
                        PosHeader data = db.PosHeader.FirstOrDefault(w => w.InvoiceNo == inv);
                        //  get lass order In data base by CreateBy
                        Model.ReceiptPrint rp = new Model.ReceiptPrint();
                        //rp.CompanyName = "ร้านสหกรณ์จังหวัดตราด จำกัด";
                        //rp.CompanyBranch = "สาขา 00002";
                        //rp.CompanyAddress = "99/9 หมู่ที่ 2 ตำบลเนินทราย อำเภอเมือง";
                        //rp.CompanyTel = "โทร. 039-513666-9";
                        //rp.CompanyFax = "แฟกซ์ 039-513664, 039-513665";
                        //rp.CompanyLineId = "ID Line: 0618648686";
                        //rp.CompanyTaxId = "Tax ID :0994000272383";
                        //rp.CompanyRegId = "Reg ID :E561655613561";
                        rp.CompanyName = branch.Name;
                        rp.CompanyBranch = "สาขา " + branch.BranchNo;
                        rp.CompanyAddress = branch.Address;
                        rp.CompanyTel = "โทร. " + branch.Tel;
                        rp.CompanyFax = "แฟกซ์ " + branch.Fax;
                        rp.CompanyLineId = "ID Line: " + branch.LineId;
                        rp.CompanyTaxId = "Tax ID :" + branch.TaxNo;
                        rp.CompanyRegId = "Reg ID :E561655613561";
                        List<Model.ProductInReceipt> pir = new List<Model.ProductInReceipt>();
                        string vatType = "";
                        string isPro = "";

                        foreach (var item in data.PosDetails.Where(w => w.Enable == true).ToList())
                        {
                            if (item.ProductDetails.Products.FKProductVatType == MyConstant.ProductVatType.HasVat)
                            {
                                vatType = "I";
                            }
                            else
                            {
                                vatType = "#";
                            }
                            if (item.IsPromotion == true)
                            {
                                isPro = "#C";
                            }
                            else
                            {
                                isPro = "";
                            }
                            pir.Add(new ProductInReceipt()
                            {
                                Code = isPro + item.ProductDetails.Code,
                                Qty = Library.ConvertDecimalToStringForm(item.Qty),
                                Name = item.ProductDetails.Products.ThaiName,
                                SalePrice = Library.ConvertDecimalToStringForm(item.PricePerUnit),
                                ProductVatType = vatType,
                                Total = Library.ConvertDecimalToStringForm(item.TotalPrice)
                            });
                            // แสดงว่ามี คูปอง
                            if (item.DiscountCoupon > 0)
                            {
                                pir.Add(new ProductInReceipt()
                                {
                                    Code = item.ProductDetails.Code,
                                    Qty = "1.00",
                                    Name = "[ลดคูปอง]" + item.Description,
                                    SalePrice = Library.ConvertDecimalToStringForm(-item.DiscountCoupon),
                                    ProductVatType = vatType,
                                    Total = Library.ConvertDecimalToStringForm(-item.DiscountCoupon)
                                });
                            }
                            // แสดงว่ามี ลดทางร้าน
                            if (item.DiscountShop > 0)
                            {
                                pir.Add(new ProductInReceipt()
                                {
                                    Code = item.ProductDetails.Code,
                                    Qty = "1.00",
                                    Name = "[ส่วนลด]" + item.Description,
                                    SalePrice = Library.ConvertDecimalToStringForm(-item.DiscountShop),
                                    ProductVatType = vatType,
                                    Total = Library.ConvertDecimalToStringForm(-item.DiscountShop)
                                });
                            }
                        }

                        rp.ProductInReceipts = pir;
                        rp.TotalBalance = Library.ConvertDecimalToStringForm(data.TotalBalance);
                        rp.TotalCash = Library.ConvertDecimalToStringForm(data.Cash);
                        rp.TotalChange = Library.ConvertDecimalToStringForm(data.Change);
                        rp.MemberId = data.Member.Code;
                        rp.MemberName = data.Member.Name;
                        rp.OrderType = data.PosType.Name;
                        rp.TotalUnVat = Library.ConvertDecimalToStringForm(data.TotaNoVat);
                        rp.TotalHasVat = Library.ConvertDecimalToStringForm(data.TotalBeforeVat);
                        rp.TotalVat = Library.ConvertDecimalToStringForm(data.TotalVat);
                        rp.TotalList = Library.ConvertDecimalToStringForm(pir.Count);
                        //decimal sumQty = data.PosDetails.Where(w => w.Enable == true).ToList()
                        rp.TotalUnit = Library.ConvertDecimalToStringForm(data.PosDetails.Where(w => w.Enable == true).Sum(w => w.Qty));
                        rp.CashierName = Singleton.SingletonAuthen.Instance().Name;
                        rp.OrderNo = data.InvoiceNo;

                        rp.OrderDate = Library.ConvertDateToThaiDate(data.CreateDate, true);


                        if (data.PrintSequence == 0)
                        {
                            Repository.ReceiptPrint.PrintNow(rp);
                            // update
                            data.PrintSequence += 1;
                            db.Entry(data).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                            Repository.ReceiptPrint.PrintNow(rp, true, data.PrintSequence);
                            // update
                            data.PrintSequence += 1;
                            db.Entry(data).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        #endregion

                    }
                }

            }


        }
        /// <summary>
        /// หลังจากยืนยัน จะต้อง add เข้า pos ด้วย เพราะเป็นการจบการขาย เชื่อ
        /// </summary>
        /// <param name="stackDocNo"></param>
        private void ConfirmOrder(IEnumerable<string> stackDocNo)
        {
            SSLsEntities dbS = new SSLsEntities();
            try
            {
                using (SSLsEntities db = new SSLsEntities())
                {
                    PosHeader pos;
                    List<PosDetails> details;
                    foreach (var item in stackDocNo)
                    {
                        pos = new PosHeader();
                        details = new List<PosDetails>();

                        var order = db.SaleOrderWarehouse.Include("SaleOrderWarehouseDtl.ProductDetails.Products.ProductVatType").SingleOrDefault(w => w.Code == item && w.Enable == true && w.InvoiceNo == null && w.ConfirmOrderDate == null);
                        // call library tin tin
                        DataTable dataMap = Library.ConvertToDataTable(MapSaleOrderWHForLibrary(order));
                        //TradPickBigCar(string tb_cusno, string tb_sentno, string tb_po_distcount, string tb_statusno, int UserId, string tb_costall, string detail, DataTable dt, string invoice)
                        //TradPickBigCar(order.FKMember.ToString(), order.FKBranch.ToString(), 0 + "", "ยืนยัน", int.Parse(order.CreateBy), order.TotalBalance.ToString(), order.Description, dataMap, order.Code);

                        var ds = db.SaleOrderWarehouseDtl.Where(w => w.Enable == true & w.SaleOrderWarehouse.Enable == true &
                        w.ProductDetails.Enable == true & w.ProductDetails.Products.Enable == true &
                        w.SaleOrderWarehouse.InvoiceNoWH == null & w.SaleOrderWarehouse.Code == item)
                        .Select(s => new { PRODUCT_ID = s.ProductDetails.Products.FKRowID, PRODUCT_NO = s.ProductDetails.Code, PRODUCT_NAME = s.ProductDetails.Name, UNIT_NAME = s.ProductDetails.ProductUnit.Name, QTY = s.Qty, GIVEAWAY = "0", COST = s.PricePerUnit, DISCOUNT = (s.PercentDiscount + s.BathDiscount), COST_TOTAL = s.TotalPrice }).ToList();
                        DataTable dt = Library.ConvertToDataTable(ds);

                        foreach (DataRow row in dt.Rows)
                        {
                            Console.WriteLine(row["PRODUCT_NO"].ToString());
                        }
                        if (dt.Rows.Count > 0)
                        {
                            var dshd = db.SaleOrderWarehouseDtl.Where(w => w.Enable == true & w.SaleOrderWarehouse.Enable == true & w.ProductDetails.Enable == true & w.ProductDetails.Products.Enable == true & w.SaleOrderWarehouse.InvoiceNoWH == null & w.SaleOrderWarehouse.Code == item).FirstOrDefault();
                            if (dshd != null)
                            {
                                var check = Library.TradPickBigCar(dshd.SaleOrderWarehouse.Member.Code.ToString(), "1", "0", "1", int.Parse(dshd.SaleOrderWarehouse.CreateBy), dshd.SaleOrderWarehouse.TotalBeforeVat.ToString(), dshd.SaleOrderWarehouse.Description.ToString(), dt, dshd.SaleOrderWarehouse.Code.ToString());

                                if (check == "OK")
                                {
                                    Console.WriteLine(check);
                                }
                                else
                                {
                                    Console.WriteLine(check);
                                }
                            }
                        }

                        // call เสด ReCal
                        Recal(order.Code);
                        /// ถ้า ไป เชคกะ จารตินแล้ว ไม่ได้ของ จิงๆ ก็ คอนเฟิมไปนะแหละ    
                        List<SaleOrderWarehouseDtl> getNewList = dbS.SaleOrderWarehouseDtl.Where(w => w.Enable == true && w.FKSaleOrderWarehouse == order.Id).ToList();
                        if (getNewList.Sum(w => w.QtyAllow) == 0)
                        {
                            order.FKSaleOrderWarehouseStatus = MyConstant.SaleOrderWarehouseStatus.ConfirmOrder;
                            order.UpdateDate = DateTime.Now;
                            order.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
                            order.ConfirmOrderDate = DateTime.Now;
                            order.ConfirmOrderBy = Singleton.SingletonAuthen.Instance().Id;
                            db.Entry(order).State = EntityState.Modified;
                            db.SaveChanges();
                            return;
                        }
                        // Add To Pos   
                        pos.Qty = getNewList.Sum(w => w.QtyAllow);
                        pos.QtyList = getNewList.Count();
                        pos.TotalCost = 0;
                        pos.FKPosType = MyConstant.POsType.Creadit;
                        pos.FKPosMachine = _posMachine.Id;
                        pos.FKBudgetYear = Singleton.SingletonThisBudgetYear.Instance().ThisYear.Id;
                        pos.PrintSequence = 0;
                        foreach (var dtl in getNewList)
                        {
                            PosDetails dd = new PosDetails();
                            dd.Enable = true;
                            dd.Description = "รถใหญ่";
                            dd.CreateDate = DateTime.Now;
                            dd.CreateBy = Singleton.SingletonAuthen.Instance().Id;
                            dd.UpdateDate = DateTime.Now;
                            dd.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
                            dd.FKProductDetails = dtl.FKProductDtl;
                            dd.Qty = dtl.QtyAllow;
                            dd.Cost = dtl.ProductDetails.CostAndVat;
                            dd.PricePerUnit = dtl.PricePerUnit;
                            dd.DiscountCoupon = 0;
                            dd.DiscountShop = dtl.BathDiscount;
                            if (dtl.QtyAllow < dd.Qty)
                            {
                                // ถ้าไม่ได้ตามเป้า 
                                dd.DiscountShop = 0;
                            }
                            dd.TotalPrice = dtl.TotalPrice;
                            if (dtl.ProductDetails.Products.ProductVatType.Id == MyConstant.ProductVatType.HasVat)
                            {
                                decimal removeVat = dd.TotalPrice * MyConstant.MyVat.Vat / MyConstant.MyVat.VatRemove;
                                dd.TotalBeforeVat = dd.TotalPrice - removeVat;
                                dd.TotalVat = removeVat;
                            }
                            else
                            {
                                dd.TotalBeforeVat = dd.TotalPrice;
                                dd.TotalVat = 0;
                            }
                            dd.IsPromotion = false;
                            details.Add(dd);
                        }
                        var posCode = Library.GetGeneratePosNo(this._posMachine);
                        pos.InvoiceNo = posCode;
                        pos.Enable = true;
                        pos.Description = order.Description;
                        pos.CreateDate = DateTime.Now;
                        pos.CreateBy = Singleton.SingletonAuthen.Instance().Id;
                        pos.UpdateDate = DateTime.Now;
                        pos.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
                        pos.FKMember = order.FKMember;
                        pos.FKDebtor = order.FKDebtor;
                        pos.TotalBalance = order.TotalBalance;
                        pos.PayDate = null;
                        pos.TotalVat = order.TotalVat;
                        pos.TotaNoVat = order.TotaNoVat;
                        pos.Total = order.Total;
                        pos.TotalDiscountCoupon = 0;
                        pos.TotalDiscountShop = 0;
                        pos.TotalBeforeVat = order.TotalBeforeVat;
                        pos.Discount = 0;
                        pos.Cash = order.TotalBalance;
                        pos.Change = 0;
                        pos.TotalDiscountShop = details.Sum(w => w.DiscountShop);
                        pos.PosDetails = details;
                        //pos.Total = getNewList.Sum(w => w.QtyAllow);
                        order.FKSaleOrderWarehouseStatus = MyConstant.SaleOrderWarehouseStatus.ConfirmOrder;
                        order.UpdateDate = DateTime.Now;
                        order.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
                        order.ConfirmOrderDate = DateTime.Now;
                        order.ConfirmOrderBy = Singleton.SingletonAuthen.Instance().Id;
                        order.InvoiceNo = posCode;

                        // add to post เพื่อขาย
                        db.PosHeader.Add(pos);
                        // อีพเดท สถาน
                        db.Entry(order).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    LoadGrid();
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                dbS.Dispose();
            }

        }