private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                PriceSchedule ps           = new PriceSchedule();
                int           currentYear  = DateTime.Now.Year;
                int           currentMonth = DateTime.Now.Month;
                var           pro          = db.PriceSchedule.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                string        proCodeGen   = SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(pro, 3);

                ps.Code        = proCodeGen;
                ps.Enable      = true;
                ps.Name        = textBoxProName.Text;
                ps.Description = textBoxRemark.Text;
                ps.CreateDate  = DateTime.Now;
                ps.CreateBy    = SingletonAuthen.Instance().Id;
                ps.UpdateDate  = DateTime.Now;
                ps.UpdateBy    = SingletonAuthen.Instance().Id;
                ps.FKCampaign  = MyConstant.CampaignType.FullyAmountAndDiscount;
                // dd/MM/yyyy
                string[] date = dateTimePickerStart.Text.Split('/');
                ps.StartDate = Library.ConvertDateTime(date[0], date[1], date[2]);
                //ps.StartDate = DateTime.Parse(dateTimePickerStart.Text);
                date       = dateTimePickerEnd.Text.Split('/');
                ps.EndDate = Library.ConvertDateTime(date[0], date[1], date[2]);
                //ps.EndDate = DateTime.Parse(dateTimePickerEnd.Text);
                ps.Limited   = 0;
                ps.Notice    = textBoxProNotice.Text;
                ps.FullPrice = decimal.Parse(textBoxProAmount.Text);
                ps.FullQty   = 0;
                ps.Discount  = decimal.Parse(textBoxProDiscount.Text);
                ps.IsStop    = false;
                List <SellingPrice> details = new List <SellingPrice>();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    details.Add(new SellingPrice()
                    {
                        CreateBy        = SingletonAuthen.Instance().Id,
                        CreateDate      = DateTime.Now,
                        Description     = dataGridView1.Rows[i].Cells[colRemark].Value.ToString(),
                        Enable          = true,
                        FKProduct       = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString()),
                        GetCurrentPrice = decimal.Parse(dataGridView1.Rows[i].Cells[colCurrentPrice].Value.ToString()),
                        Name            = textBoxProName.Text,
                        SpecialPrice    = 0,
                        UpdateDate      = DateTime.Now,
                        UpdateBy        = SingletonAuthen.Instance().Id
                    });
                    if (i >= dataGridView1.Rows.Count - 2)
                    {
                        break;
                    }
                }
                ps.SellingPrice = details;
                db.PriceSchedule.Add(ps);
                db.SaveChanges();
            }
            // reset all
            ResetForm();
        }
        private void CampaignFullyQtyAndGiftForm_Load(object sender, EventArgs e)
        {
            SingletonProduct.Instance();
            dateTimePickerStart.MinDate = DateTime.Now;
            var thisBudget = SingletonThisBudgetYear.Instance().ThisYear;

            using (SSLsEntities db = new SSLsEntities())
            {
                int currentYear  = DateTime.Now.Year;
                int currentMonth = DateTime.Now.Month;
                var pro          = db.PriceSchedule.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                textBoxProCode.Text = thisBudget.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(pro, 3);
            }
        }
 private void CampaignDiscountDayForm_Load(object sender, EventArgs e)
 {
     dateTimePickerStart.MinDate = DateTime.Now;
     SingletonProduct.Instance();
     //using (SSLsEntities db = new SSLsEntities())
     //{
     //    string proCodeGen = SingletonPriority1.Instance().ThisBudgetYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(db.PriceSchedule.Count() + 1, 3);
     //    textBoxProCode.Text = proCodeGen;
     //}
     using (SSLsEntities db = new SSLsEntities())
     {
         int    currentYear  = DateTime.Now.Year;
         int    currentMonth = DateTime.Now.Month;
         var    pro          = db.PriceSchedule.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
         string code         = SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(pro, 3);
         textBoxProCode.Text = code;
     }
 }
 void ResetForm()
 {
     textBoxProName.Text       = "";
     textBoxProCode.Text       = "";
     textBoxProNotice.Text     = "";
     dateTimePickerStart.Value = DateTime.Now;
     dateTimePickerEnd.Value   = DateTime.Now;
     textBoxRemark.Text        = "";
     dataGridView1.Rows.Clear();
     dataGridView1.Refresh();
     // Generate Code New
     using (SSLsEntities db = new SSLsEntities())
     {
         int    currentYear  = DateTime.Now.Year;
         int    currentMonth = DateTime.Now.Month;
         var    pro          = db.PriceSchedule.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
         string code         = SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(pro, 3);
         textBoxProCode.Text = code;
     }
 }
Ejemplo n.º 5
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();
            }
        }
        /// <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;
            }
        }
Ejemplo n.º 7
0
        private void WasteManageForm_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(1);
            textBoxRemark.Select();
            textBoxDate.Text = Library.ConvertDateToThaiDate(DateTime.Now);

            //comboBoxWarehouse.DataSource = SingletonWarehouse.Instance().Warehouses;
            //comboBoxWarehouse.DisplayMember = "Name";
            //comboBoxWarehouse.ValueMember = "Id";
            //// first active data
            //textBoxDescWH.Text = SingletonWarehouse.Instance().Warehouses.FirstOrDefault().Description;

            //comboBoxReason.DataSource = SingletonWasteReason.Instance().WasteReasons;
            //comboBoxReason.DisplayMember = "Name";
            //comboBoxReason.ValueMember = "Id";

            using (SSLsEntities db = new SSLsEntities())
            {
                Singleton.SingletonWasteReason.Instance();
                Singleton.SingletonWarehouse.Instance();

                int    currentYear  = DateTime.Now.Year;
                int    currentMonth = DateTime.Now.Month;
                var    tw           = db.StoreFrontTransferWaste.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                string code         = MyConstant.PrefixForGenerateCode.TransferWaste + SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(tw, 3);
                textBoxCode.Text = code;
                textBoxDate.Text = Library.ConvertDateToThaiDate(DateTime.Now);
            }
        }
        private void SaveCommit()
        {
            var thisBudget = SingletonThisBudgetYear.Instance().ThisYear;

            using (SSLsEntities db = new SSLsEntities())
            {
                int    currentYear  = DateTime.Now.Year;
                int    currentMonth = DateTime.Now.Month;
                var    pro          = db.PriceSchedule.Where(w => w.CreateDate.Year == currentYear && w.CreateDate.Month == currentMonth).Count() + 1;
                string code         = thisBudget.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(pro, 3);

                PriceSchedule header = new PriceSchedule();
                header.Code        = code;
                header.Enable      = true;
                header.Name        = textBoxProName.Text;
                header.Description = textBoxRemark.Text;
                header.CreateDate  = DateTime.Now;
                header.CreateBy    = SingletonAuthen.Instance().Id;
                header.UpdateBy    = SingletonAuthen.Instance().Id;
                header.UpdateDate  = DateTime.Now;
                header.FKCampaign  = MyConstant.CampaignType.FullyQtyAndGift;
                header.StartDate   = dateTimePickerStart.Value;
                header.EndDate     = dateTimePickerEnd.Value;
                header.Limited     = 0;
                header.Notice      = textBoxProNotice.Text;
                header.FullPrice   = 0;
                header.FullQty     = 0;
                header.Discount    = 0;
                header.IsStop      = false;
                header.StopReason  = null;

                /// ต้อง Row เท่ากัน
                if (dataGridView1.Rows.Count == dataGridView2.Rows.Count)
                {
                    SellingPrice        selling;
                    SellingPriceDetails details;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        selling                 = new SellingPrice();
                        selling.CreateDate      = DateTime.Now;
                        selling.CreateBy        = SingletonAuthen.Instance().Id;
                        selling.Enable          = true;
                        selling.Name            = header.Name;
                        selling.Description     = null;
                        selling.UpdateDate      = DateTime.Now;
                        selling.UpdateBy        = SingletonAuthen.Instance().Id;
                        selling.FKProduct       = int.Parse(dataGridView1.Rows[i].Cells[col1Id].Value.ToString());
                        selling.GetCurrentPrice = decimal.Parse(dataGridView1.Rows[i].Cells[col1Price].Value.ToString());
                        selling.SpecialPrice    = 0;
                        selling.FullyQty        = decimal.Parse(dataGridView1.Rows[i].Cells[col1Qty].Value.ToString());

                        /// ส่วนของแถม
                        details                 = new SellingPriceDetails();
                        details.Enable          = true;
                        details.Name            = header.Name;
                        details.CreateDate      = DateTime.Now;
                        details.CreateBy        = SingletonAuthen.Instance().Id;
                        details.UpdateDate      = DateTime.Now;
                        details.UpdateBy        = SingletonAuthen.Instance().Id;
                        details.FKProduct       = int.Parse(dataGridView2.Rows[i].Cells[col2Id].Value.ToString());
                        details.GiftQty         = decimal.Parse(dataGridView2.Rows[i].Cells[col2Qty].Value.ToString());
                        details.GetCurrentPrice = decimal.Parse(dataGridView2.Rows[i].Cells[col2Price].Value.ToString());
                        details.SpecialPrice    = 0;

                        selling.SellingPriceDetails.Add(details);
                        header.SellingPrice.Add(selling);
                        if (i >= dataGridView1.Rows.Count - 2)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("พบข้อผิดพลาดกรุณาตรวจสอบดูอีกครั้ง");
                }
                db.PriceSchedule.Add(header);
                db.SaveChanges();
                // reset all
                ResetForm();
            }
        }