/// <summary>
        /// 1. check invoice no
        /// 2. check invoice date****
        /// 3. check vendor****
        /// 4. ต้องเลือก อย่างน้อย 1 PO เพื่อ save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string invoice = textBoxInvoiceNo.Text.Trim();

            if (invoice == "")
            {
                MessageBox.Show("กรุณากรอก ใบกำกับภาษี/ใบส่ง");
                return;
            }
            try
            {
                DateTime?invoiceDate = Library.ConvertTHToENDate(textBoxInvoiceDate.Text);
                if (invoiceDate == null)
                {
                    MessageBox.Show("วันที่ Invoice ไม่ถูกต้อง (Need Date English United Kingdom)");
                    return;
                }
                else
                {
                }
                Console.WriteLine(invoiceDate);
            }
            catch (Exception)
            {
                Console.WriteLine("Error");
                return;
            }
            //if (_VendorId == 0)
            //{
            //    MessageBox.Show("กรุณาเลือก ผู้จำหน่าย");
            //    return;
            //}

            if (listBoxPolist.Items.Count == 0)
            {
                MessageBox.Show("กรุณาเลือก อย่างน้อย 1 PO");
                return;
            }
            string poInList = listBoxPolist.Items[0].ToString();

            DialogResult dr = MessageBox.Show("คุณต้องการบันทึกข้อมูล ใช่หรือไม่ ?",
                                              "คำเตือนจากระบบ", MessageBoxButtons.YesNo);

            switch (dr)
            {
            case DialogResult.Yes:
                string rcvNo = textBoxRcvNo.Text;

                #region กรณีแก้ไข
                if (rcvNo != "")
                {
                    using (SSLsEntities db = new SSLsEntities())
                    {
                        var rcv = db.PORcv.SingleOrDefault(w => w.Enable == true && w.Code == rcvNo);
                        rcv.InvoiceNo   = textBoxInvoiceNo.Text;
                        rcv.InvoiceDate = (DateTime)Library.ConvertTHToENDate(textBoxInvoiceDate.Text);
                        rcv.FKVendor    = _VendorId;
                        string[] listPO = listBoxPolist.Items.OfType <string>().ToArray();
                        rcv.PORefer     = string.Join(",", listPO);
                        rcv.Description = textBoxDesc.Text;

                        rcv.UpdateDate   = DateTime.Now;
                        rcv.UpdateBy     = SingletonAuthen.Instance().Id;
                        rcv.DiscountKey  = textBoxDis.Text;
                        rcv.DiscountBath = decimal.Parse(textBoxDis.Text);
                        rcv.TotalBUnVat  = decimal.Parse(textBoxUnVat.Text);
                        rcv.TotalBHasVat = decimal.Parse(textBoxHasVat.Text);
                        rcv.TotalVat     = decimal.Parse(textBoxVat.Text);
                        // details
                        List <PORcvDetails> details = new List <PORcvDetails>();
                        PORcvDetails        detail;
                        for (int row = 0; row < dataGridView1.Rows.Count; row++)
                        {
                            int     idpo        = int.Parse(dataGridView1.Rows[row].Cells[colIdPo].Value.ToString());
                            int     idProd      = int.Parse(dataGridView1.Rows[row].Cells[colIdProd].Value.ToString());
                            int     number      = int.Parse(dataGridView1.Rows[row].Cells[colNumber].Value.ToString());
                            string  barcode     = dataGridView1.Rows[row].Cells[colCode].Value.ToString();
                            decimal rcvComplete = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvComplete].Value.ToString());
                            string  name        = dataGridView1.Rows[row].Cells[colName].Value.ToString();
                            decimal pz          = decimal.Parse(dataGridView1.Rows[row].Cells[colPz].Value.ToString());
                            string  unit        = dataGridView1.Rows[row].Cells[colUnit].Value.ToString();
                            decimal qty         = decimal.Parse(dataGridView1.Rows[row].Cells[colQty].Value.ToString());
                            decimal gift        = decimal.Parse(dataGridView1.Rows[row].Cells[colGift].Value.ToString());
                            decimal rcvQty      = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvQty].Value.ToString());
                            decimal rcvGift     = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvGift].Value.ToString());
                            decimal price       = decimal.Parse(dataGridView1.Rows[row].Cells[colPrice].Value.ToString());
                            decimal total       = decimal.Parse(dataGridView1.Rows[row].Cells[colTotal].Value.ToString());
                            int     poDtlId     = int.Parse(dataGridView1.Rows[row].Cells[colPODtlId].Value.ToString());
                            // ถ้ามีแล้ว ก็อัพเดท
                            var getDtlRcv = db.PORcvDetails.SingleOrDefault(w => w.Enable == true && w.FKPoDetails == poDtlId);
                            if (getDtlRcv != null)
                            {
                                getDtlRcv.FKProductDtl   = idProd;
                                getDtlRcv.SequenceNumber = number;
                                getDtlRcv.RcvQuantity    = rcvQty;
                                getDtlRcv.GiftQty        = rcvGift;
                                getDtlRcv.NewCost        = price;
                                getDtlRcv.CurrentCost    = price;
                                getDtlRcv.TotalPrice     = total;
                                if ((rcvGift + rcvQty + rcvComplete) == qty + gift)
                                {
                                    getDtlRcv.IsComplete = true;
                                }
                                else
                                {
                                    getDtlRcv.IsComplete = false;
                                }
                                getDtlRcv.RcvComplete     = rcvGift + rcvQty + rcvComplete;
                                db.Entry(getDtlRcv).State = EntityState.Modified;
                            }
                            else     // ถ้าไมีมี
                            {
                                detail                = new PORcvDetails();
                                detail.FKPORcv        = rcv.Id;
                                detail.CreateDate     = DateTime.Now;
                                detail.CreateBy       = Singleton.SingletonAuthen.Instance().Id;
                                detail.UpdateDate     = DateTime.Now;
                                detail.UpdateBy       = Singleton.SingletonAuthen.Instance().Id;
                                detail.Description    = "-";
                                detail.Enable         = true;
                                detail.FKProductDtl   = idProd;
                                detail.SequenceNumber = number;
                                detail.QtyOnPO        = qty;
                                detail.RcvQuantity    = rcvQty;
                                detail.CurrentCost    = price;
                                detail.NewCost        = price;
                                detail.DiscountKey    = 0 + "";
                                detail.DiscountBath   = 0;
                                detail.TotalPrice     = total;
                                detail.GiftOnPo       = gift;
                                detail.GiftQty        = rcvGift; // รับเข้าของแถม
                                detail.FKPoDetails    = poDtlId;
                                if ((rcvGift + rcvQty + rcvComplete) == qty + gift)
                                {
                                    detail.IsComplete = true;
                                }
                                else
                                {
                                    detail.IsComplete = false;
                                }
                                detail.RcvComplete = detail.RcvComplete + rcvGift + rcvQty;
                                detail.FKPOHeader  = idpo;
                                //if (rcvGift + rcvQty > 0)
                                //{
                                //    details.Add(detail);
                                //}
                                db.PORcvDetails.Add(detail);
                            }
                        }
                        db.SaveChanges();
                    }
                    return;
                }
                #endregion
                ///////////////////////////////////////////////////////////////////////////////////////////////////
                #region กรณีเพิ่ม
                using (SSLsEntities db = new SSLsEntities())
                {
                    int    currentYear  = DateTime.Now.Year;
                    int    currentMonth = DateTime.Now.Month;
                    var    running      = db.PORcv.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                    string code         = SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(running, 4);

                    //string poInList = listBoxPolist.Items[0].ToString();
                    var   getPo = db.POHeader.SingleOrDefault(w => w.Enable == true && w.PONo == poInList);
                    PORcv rcv   = new PORcv();
                    rcv.FKPOHeader   = getPo.Id;
                    rcv.Code         = MyConstant.PrefixForGenerateCode.RCVPOS + code;
                    rcv.Description  = textBoxDesc.Text;
                    rcv.Enable       = true;
                    rcv.CreateDate   = DateTime.Now;
                    rcv.CreateBy     = SingletonAuthen.Instance().Id;
                    rcv.UpdateDate   = DateTime.Now;
                    rcv.UpdateBy     = SingletonAuthen.Instance().Id;
                    rcv.DiscountKey  = textBoxDis.Text;
                    rcv.DiscountBath = decimal.Parse(textBoxDis.Text);
                    rcv.TotalBUnVat  = decimal.Parse(textBoxUnVat.Text);
                    rcv.TotalBHasVat = decimal.Parse(textBoxHasVat.Text);
                    rcv.TotalVat     = decimal.Parse(textBoxVat.Text);
                    rcv.TotalGift    = 0;
                    rcv.InvoiceNo    = textBoxInvoiceNo.Text;
                    rcv.InvoiceDate  = (DateTime)Library.ConvertTHToENDate(textBoxInvoiceDate.Text);
                    string[] listPO = listBoxPolist.Items.OfType <string>().ToArray();
                    rcv.PORefer     = string.Join(",", listPO);
                    rcv.FKTransport = MyConstant.Transport.NotChoose;
                    rcv.PrintNumber = 0;
                    rcv.FKVendor    = _VendorId;
                    if (_VendorId == 0)
                    {
                        rcv.FKVendor = null;
                    }

                    // details
                    List <PORcvDetails> details = new List <PORcvDetails>();
                    PORcvDetails        detail;
                    for (int row = 0; row < dataGridView1.Rows.Count; row++)
                    {
                        detail = new PORcvDetails();
                        int     idpo        = int.Parse(dataGridView1.Rows[row].Cells[colIdPo].Value.ToString());
                        int     idProd      = int.Parse(dataGridView1.Rows[row].Cells[colIdProd].Value.ToString());
                        int     number      = int.Parse(dataGridView1.Rows[row].Cells[colNumber].Value.ToString());
                        string  barcode     = dataGridView1.Rows[row].Cells[colCode].Value.ToString();
                        decimal rcvComplete = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvComplete].Value.ToString());
                        string  name        = dataGridView1.Rows[row].Cells[colName].Value.ToString();
                        decimal pz          = decimal.Parse(dataGridView1.Rows[row].Cells[colPz].Value.ToString());
                        string  unit        = dataGridView1.Rows[row].Cells[colUnit].Value.ToString();
                        decimal qty         = decimal.Parse(dataGridView1.Rows[row].Cells[colQty].Value.ToString());
                        decimal gift        = decimal.Parse(dataGridView1.Rows[row].Cells[colGift].Value.ToString());
                        decimal rcvQty      = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvQty].Value.ToString());
                        decimal rcvGift     = decimal.Parse(dataGridView1.Rows[row].Cells[colRcvGift].Value.ToString());
                        decimal price       = decimal.Parse(dataGridView1.Rows[row].Cells[colPrice].Value.ToString());
                        decimal total       = decimal.Parse(dataGridView1.Rows[row].Cells[colTotal].Value.ToString());
                        int     poDtlId     = int.Parse(dataGridView1.Rows[row].Cells[colPODtlId].Value.ToString());

                        detail.CreateDate     = DateTime.Now;
                        detail.CreateBy       = SingletonAuthen.Instance().Id;
                        detail.UpdateDate     = DateTime.Now;
                        detail.UpdateBy       = SingletonAuthen.Instance().Id;
                        detail.Description    = "-";
                        detail.Enable         = true;
                        detail.FKProductDtl   = idProd;
                        detail.SequenceNumber = number;
                        detail.QtyOnPO        = qty;
                        detail.RcvQuantity    = rcvQty;
                        detail.CurrentCost    = price;
                        detail.NewCost        = price;
                        detail.DiscountKey    = 0 + "";
                        detail.DiscountBath   = 0;
                        detail.TotalPrice     = total;
                        detail.GiftOnPo       = gift;
                        detail.GiftQty        = rcvGift; // รับเข้าของแถม
                        detail.FKPoDetails    = poDtlId;
                        if ((rcvGift + rcvQty + rcvComplete) == qty)
                        {
                            detail.IsComplete = true;
                        }
                        else
                        {
                            detail.IsComplete = false;
                        }
                        detail.RcvComplete = rcvGift + rcvQty;
                        detail.FKPOHeader  = idpo;
                        details.Add(detail);
                        //if (rcvGift + rcvQty > 0)
                        //{
                        //    details.Add(detail);
                        //}
                    }
                    rcv.PORcvDetails = details;
                    db.PORcv.Add(rcv);
                    db.SaveChanges();
                    MessageBox.Show("บันทึก " + rcv.Code + "เรียบร้อย");
                }
                #endregion

                break;

            case DialogResult.No:
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// บันทึกรับเข้า และ เพิ่ม Stock Card Wms
        /// </summary>
        private void SaveCommit()
        {
            SSLsEntities db = new SSLsEntities();

            try
            {
                int    currentYear  = DateTime.Now.Year;
                int    currentMonth = DateTime.Now.Month;
                var    running      = db.PORcv.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                string code         = SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(running, 4);

                PORcv rcv = new PORcv();
                rcv.FKPOHeader   = _po.Id;
                rcv.Code         = MyConstant.PrefixForGenerateCode.RCVPOS + code;
                rcv.PORefer      = "" + rcv.Code;
                rcv.Description  = textBoxRemark.Text;
                rcv.Enable       = true;
                rcv.CreateDate   = DateTime.Now;
                rcv.CreateBy     = SingletonAuthen.Instance().Id;
                rcv.UpdateDate   = DateTime.Now;
                rcv.UpdateBy     = SingletonAuthen.Instance().Id;
                rcv.DiscountKey  = textBoxDiscountKey.Text;
                rcv.DiscountBath = decimal.Parse(textBoxDiscountBath.Text);
                rcv.TotalBUnVat  = decimal.Parse(textBoxTotalUnVat.Text);
                rcv.TotalBHasVat = decimal.Parse(textBoxTotalHasVat.Text);
                rcv.TotalVat     = decimal.Parse(textBoxTotalVat.Text);
                rcv.TotalGift    = _totalGift;
                rcv.DriverName   = textBoxDriverName.Text;
                rcv.InvoiceNo    = textBoxInvoice.Text;
                rcv.InvoiceDate  = (DateTime)Library.ConvertTHToENDate(textBoxInvoiceDate.Text);
                if (_idTransport == 0)
                {
                    _idTransport        = MyConstant.Transport.NotChoose;
                    rcv.TransportRemark = "ไม่เลือกบริษัทขนส่ง";
                }
                rcv.FKTransport = _idTransport;
                // details
                //List<PORcvDetails> details = new List<PORcvDetails>();
                PORcvDetails detail;
                decimal      rcvAndGift = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    detail            = new PORcvDetails();
                    detail.Enable     = true;
                    detail.CreateDate = DateTime.Now;
                    detail.CreateBy   = SingletonAuthen.Instance().Id;
                    detail.UpdateDate = DateTime.Now;
                    detail.UpdateBy   = SingletonAuthen.Instance().Id;
                    string proCode = dataGridView1.Rows[i].Cells[colCode].Value.ToString();
                    var    product = Singleton.SingletonProduct.Instance().ProductDetails.FirstOrDefault(w => w.Code == proCode);
                    detail.FKProductDtl   = product.Id;
                    detail.SequenceNumber = int.Parse(dataGridView1.Rows[i].Cells[colNumber].Value.ToString());
                    // รับเข้าในครั้งนี้
                    detail.RcvQuantity = decimal.Parse(dataGridView1.Rows[i].Cells[colQtyRcv].Value.ToString());

                    if (_po.Vendor.FKPOCostType == MyConstant.POCostType.CostOnly) // ตรวจสอบการ ยึดราคาทุน
                    {
                        detail.CurrentCost = product.CostOnly;
                    }
                    else if (_po.Vendor.FKPOCostType == MyConstant.POCostType.CostAndVat)
                    {
                        detail.CurrentCost = product.CostAndVat;
                    }
                    else
                    {
                        detail.CurrentCost = product.CostAndVat;
                    }
                    detail.QtyOnPO      = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    detail.NewCost      = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                    detail.DiscountKey  = dataGridView1.Rows[i].Cells[colDiscountKey].Value.ToString();
                    detail.DiscountBath = decimal.Parse(dataGridView1.Rows[i].Cells[colDiscountBath].Value.ToString());
                    detail.TotalPrice   = decimal.Parse(dataGridView1.Rows[i].Cells[colTotal].Value.ToString());
                    detail.GiftOnPo     = decimal.Parse(dataGridView1.Rows[i].Cells[colGiftOnPo].Value.ToString());
                    detail.GiftQty      = decimal.Parse(dataGridView1.Rows[i].Cells[colGift].Value.ToString());
                    rcv.PORcvDetails.Add(detail);

                    decimal rcvComplet = decimal.Parse(dataGridView1.Rows[i].Cells[colQtyRcvComplete].Value.ToString());
                    // ยอดรับเข้าครั้งนี้ = ยอดรับเข้า+ของแถม
                    rcvAndGift = detail.GiftQty + detail.RcvQuantity;

                    #region Manage WmsStock //////////////////////////////// ใช้ Library ManageStock แทนละ///////////////////////////////////////////////////////
                    /// จัดการ stock card
                    //int productId = product.FKProduct;
                    ///// get wmsStock
                    //var wmsStock = db.WmsStock.FirstOrDefault(w => w.Enable == true && w.FKProduct == productId);
                    //if (wmsStock != null) // ถ้าเคยตั้ง stock แล้ว
                    //{
                    //    ProductDetails productDtl = product;
                    //    /// check wmsStockDetail เอาตัวล่าสุด
                    //    WmsStockDetail wmsDtl = wmsStock.WmsStockDetail.OrderByDescending(w => w.CreateDate).FirstOrDefault(w => w.Enable == true && w.FKProductDetail == productDtl.Id);
                    //    decimal thisResultQty = 0;
                    //    if (wmsDtl != null)
                    //    {
                    //        // ถ้ามีใน wms stock detail แล้ว
                    //        // modify WmsStockDetail
                    //        WmsStockDetail wmsStockDetail = new WmsStockDetail();
                    //        wmsStockDetail.FKItemRemark = MyConstant.ItemRemark.Nornal;
                    //        wmsStockDetail.FKProductDetail = productDtl.Id;
                    //        wmsStockDetail.FKTransaction = MyConstant.WmsTransaction.RCV;
                    //        wmsStockDetail.FKWmsStock = wmsStock.Id;
                    //        wmsStockDetail.CreateDate = DateTime.Now;
                    //        wmsStockDetail.CreateBy = SingletonAuthen.Instance().Id;
                    //        wmsStockDetail.UpdateDate = DateTime.Now;
                    //        wmsStockDetail.UpdateBy = SingletonAuthen.Instance().Id;
                    //        wmsStockDetail.Enable = true;
                    //        wmsStockDetail.Description = "รับเข้าด้วย PO";
                    //        wmsStockDetail.PackSize = productDtl.PackSize;
                    //        wmsStockDetail.ActionQtyUnit = rcvAndGift;
                    //        wmsStockDetail.ActionQty = rcvAndGift * productDtl.PackSize;
                    //        /// Result จะได้ ของยกยอดมา + ของรับเข้าล่าสุด
                    //        wmsStockDetail.ResultQtyUnit = wmsDtl.ResultQtyUnit + rcvAndGift;
                    //        wmsStockDetail.ResultQty = (wmsDtl.ResultQtyUnit + rcvAndGift) * productDtl.PackSize;
                    //        thisResultQty = wmsStockDetail.ResultQty;
                    //        db.WmsStockDetail.Add(wmsStockDetail);
                    //    }
                    //    else // ถ้าไม่มีใน
                    //    {
                    //        // add new WmsStock
                    //        WmsStockDetail wmsStockDetail = new WmsStockDetail();
                    //        wmsStockDetail.FKItemRemark = MyConstant.ItemRemark.Nornal;
                    //        wmsStockDetail.FKProductDetail = productDtl.Id;
                    //        wmsStockDetail.FKTransaction = MyConstant.WmsTransaction.RCV;
                    //        wmsStockDetail.FKWmsStock = wmsStock.Id;
                    //        wmsStockDetail.CreateDate = DateTime.Now;
                    //        wmsStockDetail.CreateBy = SingletonAuthen.Instance().Id;
                    //        wmsStockDetail.UpdateDate = DateTime.Now;
                    //        wmsStockDetail.UpdateBy = SingletonAuthen.Instance().Id;
                    //        wmsStockDetail.Enable = true;
                    //        wmsStockDetail.Description = "รับเข้าด้วย PO";
                    //        wmsStockDetail.PackSize = productDtl.PackSize;
                    //        wmsStockDetail.ActionQtyUnit = rcvAndGift;
                    //        wmsStockDetail.ActionQty = rcvAndGift * productDtl.PackSize;
                    //        /// Result จะได้ ของยกยอดมา + ของรับเข้าล่าสุด
                    //        wmsStockDetail.ResultQtyUnit =  rcvAndGift;
                    //        wmsStockDetail.ResultQty = rcvAndGift * productDtl.PackSize;
                    //        thisResultQty = wmsStockDetail.ResultQty;
                    //        db.WmsStockDetail.Add(wmsStockDetail);
                    //    }

                    //    // Update WmsStock
                    //    wmsStock.OldQty = wmsStock.CurrentQty;
                    //    wmsStock.CurrentQty = thisResultQty;
                    //    wmsStock.UpdateDate = DateTime.Now;
                    //    wmsStock.UpdateBy = SingletonAuthen.Instance().Id;
                    //    db.Entry(wmsStock).State = EntityState.Modified;

                    //}
                    //else // กรณีไม่เคยมีใน Stock
                    //{
                    //    /// ต้อง new stock ใหม่
                    //    // ต้องดัก การ initial Stock ตั้งแต่ new Product
                    //}
                    #endregion
                }
                db.PORcv.Add(rcv);
                List <PORcvDetails> rcvDtl = rcv.PORcvDetails.ToList();
                //Library.MakeValueForUpdateStockWms(rcvDtl);
                // update po status = 3 เคยมีการรับเข้าแล้ว เชคว่าครบแล้วหรือไม่
                var     po              = db.POHeader.SingleOrDefault(w => w.Id == _po.Id);
                decimal checkRcvQty     = 0;
                decimal checkRcvGiftQty = 0;
                foreach (var item in po.PODetail.Where(w => w.Enable == true).ToList())
                {
                    // ดึง
                    var getProductRcv = rcv.PORcvDetails.FirstOrDefault(w => w.FKProductDtl == item.FKProductDetail);
                    item.RcvQty          = item.RcvQty + getProductRcv.RcvQuantity;
                    item.RcvGiftQty      = item.RcvGiftQty + getProductRcv.GiftQty;
                    db.Entry(item).State = EntityState.Modified;

                    checkRcvQty     = checkRcvQty + item.RcvQty;
                    checkRcvGiftQty = checkRcvGiftQty + item.RcvGiftQty;
                }
                if ((checkRcvQty + checkRcvGiftQty) == (po.TotalQty + po.TotalGift))
                {
                    // ถ้ารับเข้าครบ แปลว่า Complete
                    po.FKPOStatus = MyConstant.POStatus.RCVComplete;
                }
                else
                {
                    po.FKPOStatus = MyConstant.POStatus.RCVNotEnd;
                }
                db.Entry(po).State = EntityState.Modified;

                db.SaveChanges();
                // reset form
                ResetRcv();
            }
            catch (Exception)
            {
                MessageBox.Show("ไม่ถูกต้อง พบข้อผิดพลาด กรุณาติดต่อ admin");
            }
            finally
            {
                db.Dispose();
            }
        }
        private void SaveCommit()
        {
            SSLsEntities db = new SSLsEntities();

            try
            {
                var rcvOld = db.PORcv.SingleOrDefault(w => w.Enable == true && w.Code == textBoxRcvNo.Text);

                rcvOld.PORefer     = rcvOld.PORefer + "," + textBoxPONo.Text;
                rcvOld.Description = textBoxDesc.Text;

                rcvOld.UpdateDate   = DateTime.Now;
                rcvOld.UpdateBy     = SingletonAuthen.Instance().Id;
                rcvOld.DiscountKey  = textBoxDiscountKey.Text;
                rcvOld.DiscountBath = decimal.Parse(textBoxDiscountBath.Text);
                rcvOld.TotalBUnVat  = decimal.Parse(textBoxTotalUnVat.Text);
                rcvOld.TotalBHasVat = decimal.Parse(textBoxTotalHasVat.Text);
                rcvOld.TotalVat     = decimal.Parse(textBoxTotalVat.Text);
                rcvOld.TotalGift    = _totalGift;

                // details
                //List<PORcvDetails> details = new List<PORcvDetails>();
                PORcvDetails detail;
                decimal      rcvAndGift = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    detail            = new PORcvDetails();
                    detail.Enable     = true;
                    detail.CreateDate = DateTime.Now;
                    detail.CreateBy   = SingletonAuthen.Instance().Id;
                    detail.UpdateDate = DateTime.Now;
                    detail.UpdateBy   = SingletonAuthen.Instance().Id;
                    string proCode = dataGridView1.Rows[i].Cells[colCode].Value.ToString();
                    var    product = Singleton.SingletonProduct.Instance().ProductDetails.FirstOrDefault(w => w.Code == proCode);
                    detail.FKProductDtl   = product.Id;
                    detail.SequenceNumber = int.Parse(dataGridView1.Rows[i].Cells[colNumber].Value.ToString());
                    // รับเข้าในครั้งนี้
                    detail.RcvQuantity = decimal.Parse(dataGridView1.Rows[i].Cells[colQtyRcv].Value.ToString());

                    if (_po.Vendor.FKPOCostType == MyConstant.POCostType.CostOnly) // ตรวจสอบการ ยึดราคาทุน
                    {
                        detail.CurrentCost = product.CostOnly;
                    }
                    else if (_po.Vendor.FKPOCostType == MyConstant.POCostType.CostAndVat)
                    {
                        detail.CurrentCost = product.CostAndVat;
                    }
                    else
                    {
                        detail.CurrentCost = product.CostAndVat;
                    }
                    detail.QtyOnPO        = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    detail.NewCost        = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                    detail.DiscountKey    = dataGridView1.Rows[i].Cells[colDiscountKey].Value.ToString();
                    detail.DiscountBath   = decimal.Parse(dataGridView1.Rows[i].Cells[colDiscountBath].Value.ToString());
                    detail.TotalPrice     = decimal.Parse(dataGridView1.Rows[i].Cells[colTotal].Value.ToString());
                    detail.GiftOnPo       = decimal.Parse(dataGridView1.Rows[i].Cells[colGiftOnPo].Value.ToString());
                    detail.GiftQty        = decimal.Parse(dataGridView1.Rows[i].Cells[colGift].Value.ToString());
                    detail.SequenceNumber = int.Parse(dataGridView1.Rows[i].Cells[colNumber].Value.ToString());
                    int idPO = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                    // ยอดรับเข้าครั้งนี้ = ยอดรับเข้า+ของแถม
                    rcvAndGift = detail.GiftQty + detail.RcvQuantity;
                    if (idPO != 0)
                    {
                        rcvOld.PORcvDetails.Add(detail);

                        var getProductRcv = db.PODetail.FirstOrDefault(w => w.Id == idPO);
                        getProductRcv.RcvQty          = detail.GiftQty;
                        getProductRcv.RcvGiftQty      = rcvAndGift;
                        db.Entry(getProductRcv).State = EntityState.Modified;
                    }

                    decimal rcvComplet = decimal.Parse(dataGridView1.Rows[i].Cells[colQtyRcvComplete].Value.ToString());
                }
                db.Entry(rcvOld).State = EntityState.Modified;

                //List<PORcvDetails> rcvDtl = rcv.PORcvDetails.ToList();
                //Library.MakeValueForUpdateStockWms(rcvDtl);
                // update po status = 3 เคยมีการรับเข้าแล้ว เชคว่าครบแล้วหรือไม่
                var     po              = db.POHeader.SingleOrDefault(w => w.Id == _po.Id);
                decimal checkRcvQty     = 0;
                decimal checkRcvGiftQty = 0;
                //foreach (var item in po.PODetail.Where(w => w.Enable == true).ToList())
                //{
                //    // ดึง
                //    var getProductRcv = rcvOld.PORcvDetails.FirstOrDefault(w => w.FKProductDtl == item.FKProductDetail);
                //    item.RcvQty = item.RcvQty + getProductRcv.RcvQuantity;
                //    item.RcvGiftQty = item.RcvGiftQty + getProductRcv.GiftQty;
                //    db.Entry(item).State = EntityState.Modified;

                //    checkRcvQty = checkRcvQty + item.RcvQty;
                //    checkRcvGiftQty = checkRcvGiftQty + item.RcvGiftQty;
                //}
                //if ((checkRcvQty + checkRcvGiftQty) == (po.TotalQty + po.TotalGift))
                //{
                //    // ถ้ารับเข้าครบ แปลว่า Complete
                //    po.FKPOStatus = MyConstant.POStatus.RCVComplete;
                //}
                //else
                //{
                //    po.FKPOStatus = MyConstant.POStatus.RCVNotEnd;
                //}

                po.FKPOStatus      = MyConstant.POStatus.RCVNotEnd;
                db.Entry(po).State = EntityState.Modified;
                db.SaveChanges();
                // reset form

                frmMainReport mr = new frmMainReport(this, textBoxRcvNo.Text);
                mr.Show();
            }
            catch (Exception)
            {
                MessageBox.Show("ไม่ถูกต้อง พบข้อผิดพลาด กรุณาติดต่อ admin");
            }
            finally
            {
                db.Dispose();
            }
        }