Ejemplo n.º 1
0
        private void cmdExchange_Click(object sender, EventArgs e)
        {
            if (CheckIsZero()) return;
            if (!DBConnString.clsDB.IsConnected()) return;
            // แสดงค่ารหัสรอบที่ขาย
            SaleId();
            MaxLogSaleId();

            string AbountDate = lblAbount.Text.Trim();
            string Abount = txtAbount.Text.Trim();
            string strAbount = AbountDate + Abount;

            Sale.frmCalculateMoney frmCal = new KTCERP.Sale.frmCalculateMoney(lblNet.Text);
            frmCal.StartPosition = FormStartPosition.CenterParent;
            frmCal.ShowDialog();
            double CashTotal = clsCash.sCashMoney;
            double Discount = clsCash.sDiscount;

            double CreditTotal = clsCash.sCredit;
            double Exchange = clsCash.sExchange;
            string TypeCash = clsCash.sTypeCash;
            double Deposit_Pay = clsCash.Deposit_Pay;

            float SaleTotalCal = 0;
            float SaleTotal = 0;

            if (clsCash.sStatusCash == "1" || clsCash.sStatusCash == "2" || clsCash.sStatusCash == "3" || clsCash.sStatusCash == "4" || clsCash.sStatusCash == "5")
            {
                frmCal.Left = (frmCal.ClientSize.Width - frmCal.Width) / 2;
                frmCal.Top = (frmCal.ClientSize.Height - frmCal.Height) / 2;
                frmCal.Show();
                if (MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    if (txtSaleAppHeadId.Text != "")
                    {
                        if (GetDataHead().Rows.Count == 0)
                        {
                            MessageBox.Show("รายการนี้ถูกขายไปแล้ว ไม่สามารถทำรายการซ้ำ กรุณาตรวจสอบข้อมูลให้ถูกต้อง");
                            return;
                        }
                    }
                    tr = Conn.BeginTransaction();
                    //SaleId();
                    try
                    {
                        #region INTO SALE_HEAD
                        string sqlSave;
                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO SALE_HEAD(SaleHeadId,SaleHeadDate,CustomerId,SaleHeadStatusId,SaleHeadPayTypeId,Discount,Cash,SaleVat,SaleShipping,SaleVarious,SaleHeadDateTime,AbountId,SaleHeadDrawId,DrawId,Rate,SaleCurrencyId,NoInv,SaleHeadSaleTypeId,Remark,LoadingId,FeederId,FinalDestenationId,UserName,ConfirmId,Deposit_Status,Deposit_Pay )");
                        sb.Append(" VALUES (@SaleHeadId,@SaleHeadDate,@CustomerId,@SaleHeadStatus,@SaleHeadPayType,@Discount,@Cash,@SaleVat,@SaleShipping,@SaleVarious,@SaleHeadDateTime,@AbountId,@SaleHeadDrawId,@DrawId,@Rate,@SaleCurrencyId,@NoInv,@SaleHeadSaleTypeId,@Remark,@LoadingId,@Feeder,@Destinatic,@UserName,@ConfirmId,@Deposit_Status,@Deposit_Pay)");

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = saleId;

                        DateTime dateServer = Convert.ToDateTime(Class.DBConnString.clsDB.QueryExecuteScalarNonAlert("SELECT GETDATE()"));

                        if (dptSaleDate.Value.ToString("dd/MM/yyyy") == dateServer.ToString("dd/MM/yyyy"))
                        {
                            //MessageBox.Show("datnow" + DateTime.Now);
                            com.Parameters.Add("@SaleHeadDateTime", SqlDbType.DateTime).Value = dateServer.ToString("dd/MM/yyyy HH:mm:ss");
                            com.Parameters.Add("@saleHeadDate", SqlDbType.DateTime).Value = dateServer.ToString("dd/MM/yyyy");
                        }
                        else
                        {
                            com.Parameters.Add("@SaleHeadDateTime", SqlDbType.DateTime).Value = dptSaleDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                            com.Parameters.Add("@saleHeadDate", SqlDbType.DateTime).Value = dateServer.ToString("dd/MM/yyyy");
                        }
                        //com.Parameters.Add("@SaleHeadDateTime", SqlDbType.DateTime).Value = dptSaleDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@SaleHeadDrawId", SqlDbType.NVarChar).Value = txtDrawId.Text.Trim();
                        com.Parameters.Add("@AbountId", SqlDbType.NVarChar).Value = strAbount;
                        com.Parameters.Add("@DrawId", SqlDbType.NVarChar).Value = cboDrawId.SelectedValue.ToString();

                        com.Parameters.Add("@CustomerId", SqlDbType.Int).Value = Convert.ToInt16(Class.Function.GetTextId(txtCustomer.Text));
                        if (cboStatus.Text == "ปกติ")
                        {
                            com.Parameters.Add("@SaleHeadStatus", SqlDbType.NVarChar).Value = 1;
                        }
                        if (cboStatus.Text == "จอง")
                        {
                            com.Parameters.Add("@SaleHeadStatus", SqlDbType.NVarChar).Value = 3;
                        }
                        com.Parameters.Add("@SaleHeadPayType", SqlDbType.NVarChar).Value = clsCash.sStatusCash;
                        com.Parameters.Add("@Discount", SqlDbType.Float).Value = Discount;
                        com.Parameters.Add("@Cash", SqlDbType.Float).Value = CashTotal;
                        com.Parameters.Add("@SaleVat", SqlDbType.Float).Value = Convert.ToSingle(lblNetVAT.Text);
                        com.Parameters.Add("@SaleShipping", SqlDbType.Float).Value = Convert.ToSingle(lblShipping.Text);
                        com.Parameters.Add("@SaleVarious", SqlDbType.Float).Value = Convert.ToSingle(lblVarious.Text);
                        com.Parameters.Add("@Rate", SqlDbType.Float).Value = 0;
                        com.Parameters.Add("@SaleCurrencyId", SqlDbType.Int).Value = 0;
                        com.Parameters.Add("@Deposit_Status", SqlDbType.Int).Value = 0;
                        com.Parameters.Add("@Deposit_Pay", SqlDbType.Float).Value = Deposit_Pay;

                        if (txtNoInv.Text.Trim() == "")
                        {
                            com.Parameters.Add("@NoInv", SqlDbType.NVarChar).Value = "0";
                        }
                        else
                        {
                            com.Parameters.Add("@NoInv", SqlDbType.NVarChar).Value = txtNoInv.Text.Trim();
                        }
                        com.Parameters.Add("@SaleHeadSaleTypeId", SqlDbType.Int).Value = cboTypeSale.SelectedValue.ToString();
                        if (rtbRemark.Text != "")
                        {
                            com.Parameters.Add("@Remark", SqlDbType.NVarChar).Value = remark;
                        }
                        else
                        {
                            com.Parameters.Add("@Remark", SqlDbType.NVarChar).Value = "-";
                        }

                        com.Parameters.Add("@LoadingId", SqlDbType.NVarChar).Value = "1";
                        com.Parameters.Add("@Feeder", SqlDbType.NVarChar).Value = "1";
                        com.Parameters.Add("@Destinatic", SqlDbType.NVarChar).Value = "1";
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        com.Parameters.Add("@ConfirmId", SqlDbType.NVarChar).Value = "1";

                        com.ExecuteNonQuery();
                        //MessageBox.Show(sqlSave);
                        #endregion

                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            #region INTO SALE_DETAIL
                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO SALE_DETAIL (SaleHeadId,ProductId,SaleDetailQuantity,SaleDetailPriceUnit,SaleDetailTotal,AmountProduct,SaleDetailQuantityPack,SaleDetailPriceUnitInter )");
                            sb.Append(" VALUES( @SaleHeadId,@ProductId,@SaleDetailQuantity,@SaleDetailPriceUnit,@SaleDetailTotal,@AmountProduct,@SaleDetailQuantityPack,@SaleDetailPriceUnitInter)");

                            sqlSave = sb.ToString();

                            com.Parameters.Clear();
                            com.CommandText = sqlSave;
                            com.Parameters.Add("@SaleHeadId", SqlDbType.Int).Value = saleId;
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            com.Parameters.Add("@SaleDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@SaleDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.0000");
                            com.Parameters.Add("@SaleDetailTotal", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@AmountProduct", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[2].Text.Replace(",", ""));
                            com.Parameters.Add("@SaleDetailQuantityPack", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[10].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@SaleDetailPriceUnitInter", SqlDbType.Float).Value = 0;

                            com.ExecuteNonQuery();
                            #endregion

                            //MessageBox.Show(sqlSave);
                            SaleTotalCal = SaleTotalCal + (Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", ""))) * (Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")));
                            SaleTotal = SaleTotal + (Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", "")));

                            string weightext = lsvProductList.Items[i].SubItems[6].Text.ToString();
                            string[] weighttextarr = weightext.Split('#');
                            int len = weighttextarr.Length;

                            string packtext = lsvProductList.Items[i].SubItems[7].Text.ToString();
                            string[] packtextarr = packtext.Split('#');
                            int lenpack = packtextarr.Length;

                            string balesNumber = lsvProductList.Items[i].SubItems[11].Text.ToString();
                            string[] balesNumberarr = balesNumber.Split('#');
                            int lenbalesNumber = balesNumberarr.Length;

                            if (weightext != "" && packtext != "" && balesNumber != "")
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);

                                    #region  INTO SALE_WEIGHT
                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId,SalePack,BalesNumber)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId,@SalePack,@BalesNumber)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = saleId;
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    com.Parameters.Add("@SalePack", SqlDbType.Float).Value = Convert.ToSingle(packtextarr[j].ToString());
                                    com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt32(balesNumberarr[j].ToString());
                                    com.ExecuteNonQuery();
                                    #endregion
                                }
                            }
                            else if (weightext != "" && packtext == "" && balesNumber != "")
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    #region INTO SALE_WEIGHT
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId,BalesNumber)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId,@BalesNumber)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = saleId;
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt32(balesNumberarr[j].ToString());
                                    com.ExecuteNonQuery();
                                    #endregion
                                }
                            }
                            else if (weightext != "" && packtext == "" && balesNumber == "")
                            {
                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    #region INTO SALE_WEIGHT
                                    sb.Remove(0, sb.Length);
                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = saleId;
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    //com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt32(balesNumberarr[j].ToString());

                                    com.ExecuteNonQuery();

                                    //MessageBox.Show(sqlSave);
                                    #endregion
                                }

                            }

                            #region บันทึกลูกหนี้
                            //if (clsCash.sStatusCash == "2")
                            //{
                            //    sb.Remove(0, sb.Length);

                            //    sb.Append("INSERT INTO RECEIVABLE (ReceivableDate, CustomerId, ReceivebleType, ReceivebleTopic, SaleHeadId, ProductId, Amount, UnitPrice)");
                            //    sb.Append(" VALUES(@ReceivableDate,@CustomerId,@ReceivebleType,@ReceivebleTopic,@SaleHeadId,@ProductId,@Amount,@UnitPrice)");

                            //    sqlSave = sb.ToString();
                            //    com.Parameters.Clear();
                            //    com.CommandText = sqlSave;

                            //    com.Parameters.Add("@ReceivableDate", SqlDbType.DateTime).Value = dptSaleDate.Value.ToString("dd/MM/yyyy");
                            //    com.Parameters.Add("@CustomerId", SqlDbType.Int).Value = CustomerId;

                            //    com.Parameters.Add("@ReceivebleType", SqlDbType.Int).Value = 0; // 0 = ตั้งหนี้จากการขายที่ยังไม่ยืนนยัน , 1= ตั้งหนี้จากการขายที่ยืนยัน , 2=ชำระหนี้//
                            //    com.Parameters.Add("@ReceivebleTopic", SqlDbType.NVarChar).Value = "ค้างชำระค่าสินค้า ";
                            //    com.Parameters.Add("@SaleHeadId", SqlDbType.Int).Value = saleId;
                            //    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            //    com.Parameters.Add("@Amount", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                            //    com.Parameters.Add("@UnitPrice", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.00");
                            //    com.ExecuteNonQuery();
                            //}
                            #endregion

                        }

                        // บันทึก log
                        #region INTO LOGSALE
                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO LOGSALE(LogSaleId,SaleHeadId,LogSaleDateTime,LogSaleTransection,LogsaleTotalCal,LogsaleTotal,UserName)");
                        sb.Append(" VALUES (@LogSaleId,@SaleHeadId,@LogSaleDateTime,@LogSaleTransection,@LogsaleTotalCal,@LogsaleTotal,@UserName)");

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@LogSaleId", SqlDbType.Int).Value = LogSaleId;
                        com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = saleId;
                        com.Parameters.Add("@LogSaleDateTime", SqlDbType.DateTime).Value = dateServer;
                        com.Parameters.Add("@LogSaleTransection", SqlDbType.Int).Value = lsvProductList.Items.Count;
                        com.Parameters.Add("@LogsaleTotalCal", SqlDbType.Float).Value = SaleTotalCal.ToString("#,##0.00");
                        com.Parameters.Add("@LogsaleTotal", SqlDbType.Float).Value = SaleTotal.ToString("#,##0.00");
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        com.ExecuteNonQuery();
                        #endregion

                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            #region INTO LOGSALEDETAIL
                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO LOGSALEDETAIL ( LogSaleId,ProductId,Quantity,UnitPrice)");
                            sb.Append(" VALUES( @LogSaleId,@ProductId,@Quantity,@UnitPrice)");

                            sqlSave = sb.ToString();

                            com.Parameters.Clear();
                            com.CommandText = sqlSave;
                            com.Parameters.Add("@LogSaleId", SqlDbType.Int).Value = LogSaleId;
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            com.Parameters.Add("@Quantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@UnitPrice", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.0000");
                            com.ExecuteNonQuery();
                            #endregion
                        }

                        tr.Commit();

                        insertSaleAppHead();
                        //ต้องการ print หรือไม่
                        DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dlg == DialogResult.OK)
                        {
                            //Sale.rptShowSaleSelect rptSale = new KTCERP.Sale.rptShowSaleSelect();
                            SalePos7CSK.frmSelectReport rptSale = new frmSelectReport();
                            rptSale.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                            rptSale.setSaleId = intSaleId;
                            rptSale.ShowDialog();
                        }

                        ClearProductList();
                        ClearProductDetails();
                        ClearCommit();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //cmdSave.Focus();
                        tr.Rollback();
                    }
                }
                //SaleAppHead

                //
                frmCal.Close();
                ClearProductList();
                clsCash.sStatusCash = "0";
                CustomerId = 1749;

                cusname = "";
                txtCustomer.Text = "";

                this.Text = "หน้าจอขาย  : [" + CustomerId + "] " + cusname + " ยังไม่บันทึกในฐานข้อมูล ";

                pnlSelect.Visible = false;
            }
        }
Ejemplo n.º 2
0
 private void MenuPrint_Click(object sender, EventArgs e)
 {
     if (!DBConnString.clsDB.IsConnected()) return;
     if (SaleId == null)
     {
         MessageBox.Show("กรุณาเลือกรายการก่อน");
     }
     else
     {
         SalePos7CSK.frmSelectReport rpt = new frmSelectReport();
         rpt.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
         rpt.setSaleId = Convert.ToInt32(SaleId);
         rpt.ShowDialog();
     }
 }
Ejemplo n.º 3
0
        private void btnReportSale_Click_1(object sender, EventArgs e)
        {
            if (!DBConnString.clsDB.IsConnected()) return;
            //Sale.rptShowSaleSelect rptShowSaleSelect = new KTCERP.Sale.rptShowSaleSelect();
            //rptShowSaleSelect.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
            //rptShowSaleSelect.setSaleId = Convert.ToInt32(SaleId);
            //rptShowSaleSelect.ShowDialog();

            //SalePos7CSK.rptSaleWeight rpt = new rptSaleWeight();
            //rpt.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
            //rpt.setSaleId = Convert.ToInt32(SaleId);
            //rpt.ShowDialog();

            SalePos7CSK.frmSelectReport rpt = new frmSelectReport();
            rpt.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
            rpt.setSaleId = Convert.ToInt32(SaleId);
            rpt.ShowDialog();
        }
Ejemplo n.º 4
0
        private void cmdExchange_Click(object sender, EventArgs e)
        {
            Sale.frmCalculateMoney frmCal = new KTCERP.Sale.frmCalculateMoney(lblNet.Text);
            frmCal.StartPosition = FormStartPosition.CenterParent;
            frmCal.ShowDialog();
            double CashTotal = clsCash.sCashMoney;
            double Discount = clsCash.sDiscount;
            double CreditTotal = clsCash.sCredit;
            double Exchange = clsCash.sExchange;
            string TypeCash = clsCash.sTypeCash;

            string AbountDate = lblAbount.Text.Trim();
            string Abount = txtAbount.Text.Trim();
            string strAbount = AbountDate+Abount;
            float SaleTotalCal = 0;
            float SaleTotal = 0;
            MaxLogSaleId();
            if (clsCash.sStatusCash == "1" || clsCash.sStatusCash == "2" || clsCash.sStatusCash == "3" || clsCash.sStatusCash == "4" || clsCash.sStatusCash == "5")
            {
                if (lblSaleId.Text.Trim() == "")
                {
                    MessageBox.Show("กรุณาใส่หมายเลขใบสั่งซื้อด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    lblSaleId.Focus();
                    return;
                }
                if (txtCustomer.Text.Trim() == "")
                {
                    MessageBox.Show("กรุณาเลือกชื่อลูกค้าด้วยค่ะ (^_^) !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCustomer.Focus();
                    return;
                }
                if (MessageBox.Show("คุณต้องการบันทึกรายการขายสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    tr = Conn.BeginTransaction();

                    try
                    {
                        sb.Remove(0, sb.Length);
                        sb.Append("UPDATE SALE_HEAD ");
                        sb.Append("SET SaleHeadPayTypeId=@SaleHeadPayType,SaleVat=@NetVAT,SaleShipping=@Shipping,Remark=@Remark, ");
                        sb.Append("SaleVarious=@Various,Discount=@Discount,Cash=@Cash,CustomerId=@CustomerId,");
                        sb.Append("SaleHeadDate=@SaleHeadDate,SaleHeadDateTime=@SaleHeadDateTime,ConfirmId=@ConfirmId, ");
                        //sb.Append("SaleHeadDrawId=@SaleHeadDrawId,AbountId=@AbountId,DrawId=@DrawId,SaleHeadStatusId=@SaleHeadStatus,NoInv=@NoInv,SaleCurrencyId=@SaleCurrencyId,SaleHeadSaleTypeId=@SaleHeadSaleTypeId,UserName=@UserName  ");
                        sb.Append("SaleHeadDrawId=@SaleHeadDrawId,AbountId=@AbountId,DrawId=@DrawId,NoInv=@NoInv,SaleCurrencyId=@SaleCurrencyId,SaleHeadSaleTypeId=@SaleHeadSaleTypeId,UserName=@UserName  ");
                        sb.Append("WHERE SaleHeadId=@SaleHeadId");

                        sb.Append(" DELETE FROM  SALE_DETAIL  ");
                        sb.Append("WHERE SaleHeadId=@SaleHeadId");

                        sb.Append(" DELETE FROM  SALE_WEIGHT  ");
                        sb.Append("WHERE SaleHeadId = @SaleHeadId");
                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave;
                        com.Parameters.Add("@saleHeadId", SqlDbType.NVarChar).Value = lblSaleId.Text.Trim();
                        com.Parameters.Add("@SaleHeadPayType", SqlDbType.NVarChar).Value = clsCash.sStatusCash;
                        com.Parameters.Add("@Shipping", SqlDbType.Float).Value = Convert.ToSingle(lblShipping.Text.Trim());
                        com.Parameters.Add("@Various", SqlDbType.Float).Value = Convert.ToSingle(lblVarious.Text.Trim());
                        com.Parameters.Add("@NetVAT", SqlDbType.Float).Value = Convert.ToSingle(lblNetVAT.Text.Trim());
                        com.Parameters.Add("@Discount", SqlDbType.Float).Value = Convert.ToSingle(Discount);
                        com.Parameters.Add("@Cash", SqlDbType.Float).Value = Convert.ToSingle(CashTotal);

                        DateTime dateServer = Convert.ToDateTime(Class.DBConnString.clsDB.QueryExecuteScalarNonAlert("SELECT GETDATE()"));

                        com.Parameters.Add("@SaleHeadDate", SqlDbType.DateTime).Value = dateServer.ToString("dd/MM/yyyy");
                        com.Parameters.Add("@SaleHeadDateTime", SqlDbType.DateTime).Value = dptSaleDate.Value.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@SaleHeadDrawId", SqlDbType.NVarChar).Value = txtDrawId.Text.Trim();
                        com.Parameters.Add("@AbountId", SqlDbType.NVarChar).Value = strAbount;
                        com.Parameters.Add("@DrawId", SqlDbType.NVarChar).Value = cboDrawId.SelectedValue.ToString();
                        //com.Parameters.Add("@SaleHeadStatus", SqlDbType.Int).Value = 1;
                        com.Parameters.Add("@NoInv", SqlDbType.NVarChar).Value = txtNoInv.Text.Trim();
                        com.Parameters.Add("@SaleCurrencyId", SqlDbType.Int).Value = 1;
                        com.Parameters.Add("@CustomerId", SqlDbType.Int).Value = intCustomerId;
                        com.Parameters.Add("@SaleHeadSaleTypeId", SqlDbType.Int).Value = cboTypeSale.SelectedValue.ToString();
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        if (rtbRemark.Text != "")
                        {
                            com.Parameters.Add("@Remark", SqlDbType.NVarChar).Value = remark;
                        }
                        else
                        {
                            com.Parameters.Add("@Remark", SqlDbType.NVarChar).Value = "-";
                        }
                        com.Parameters.Add("@ConfirmId", SqlDbType.NVarChar).Value = "1";
                        com.ExecuteNonQuery();
                       // MessageBox.Show(sqlSave+"**salehead**");

                        //
                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO SALE_DETAIL (SaleHeadId,ProductId,SaleDetailQuantity,SaleDetailPriceUnit,SaleDetailTotal,AmountProduct,SaleDetailQuantityPack,SaleDetailPriceUnitInter )");
                            sb.Append(" VALUES( @SaleHeadId,@ProductId,@SaleDetailQuantity,@SaleDetailPriceUnit,@SaleDetailTotal,@AmountProduct,@SaleDetailQuantityPack,@SaleDetailPriceUnitInter)");

                            sqlSave = sb.ToString();
                            com.Parameters.Clear();
                            com.CommandText = sqlSave;
                            com.Parameters.Add("@SaleHeadId", SqlDbType.Int).Value = lblSaleId.Text.Trim();
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            com.Parameters.Add("@SaleDetailQuantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@SaleDetailPriceUnit", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.0000");
                            decimal to = Convert.ToDecimal(lsvProductList.Items[i].SubItems[9].Text.Replace(",", ""));
                            string str = to.ToString("#,##0.00");
                            com.Parameters.Add("@SaleDetailTotal", SqlDbType.Float).Value = str;
                            com.Parameters.Add("@AmountProduct", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[2].Text.Replace(",", ""));
                            com.Parameters.Add("@SaleDetailPriceUnitInter", SqlDbType.Float).Value = 0;
                            if (lsvProductList.Items[i].SubItems[10].Text.Replace(",", "")!="")
                            {
                                com.Parameters.Add("@SaleDetailQuantityPack", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[10].Text.Replace(",", "")).ToString("#,##0.00");
                            }
                            else
                            {
                                com.Parameters.Add("@SaleDetailQuantityPack", SqlDbType.Float).Value = 0;
                            }
                            com.ExecuteNonQuery();
                            //MessageBox.Show(sqlSave+"**saledetail**");

                             SaleTotalCal = SaleTotalCal + (Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")))*(Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")));
                            SaleTotal = SaleTotal + (Convert.ToSingle(lsvProductList.Items[i].SubItems[9].Text.Replace(",", "")));

                            string weightext = lsvProductList.Items[i].SubItems[6].Text.Replace(" ", "#");
                            string[] weighttextarr = weightext.Split('#');
                            int len = weighttextarr.Length;

                            string packtext = lsvProductList.Items[i].SubItems[7].Text.ToString();
                            string[] packtextarr = packtext.Split('#');
                            int lenpack = packtextarr.Length;

                            string balesNumber = lsvProductList.Items[i].SubItems[11].Text.ToString();
                            string[] balesNumberarr = balesNumber.Split('#');
                            int lenbalesNumber = balesNumberarr.Length;

                            if (weightext != "" && packtext != "" && balesNumber != "" )
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);

                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId,SalePack,BalesNumber)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId,@SalePack,@BalesNumber)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.NVarChar).Value = lblSaleId.Text.Trim();
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    com.Parameters.Add("@SalePack", SqlDbType.Float).Value = Convert.ToSingle(packtextarr[j].ToString());
                                    com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt32(balesNumberarr[j].ToString());
                                    com.ExecuteNonQuery();

                                    //MessageBox.Show(sqlSave);
                                }
                            }
                            else if (weightext != "" && packtext == "" && balesNumber !="")
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);

                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId,BalesNumber)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId,@BalesNumber)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.NVarChar).Value = lblSaleId.Text.Trim();
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt32(balesNumberarr[j].ToString());

                                    com.ExecuteNonQuery();

                                    //MessageBox.Show(sqlSave);
                                }

                            }
                            else if (weightext != "" && packtext == "" && balesNumber == "")
                            {

                                for (int j = 0; j < weighttextarr.Length - 1; j++)
                                {
                                    sb.Remove(0, sb.Length);

                                    sb.Append("INSERT INTO SALE_WEIGHT (SaleWeight,ProductId,SaleHeadId)");
                                    sb.Append(" VALUES(@saleWeight,@ProductId,@saleHeadId)");
                                    sqlSave = sb.ToString();
                                    com.Parameters.Clear();
                                    com.CommandText = sqlSave;
                                    com.Parameters.Add("@saleHeadId", SqlDbType.NVarChar).Value = lblSaleId.Text.Trim();
                                    com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                                    com.Parameters.Add("@saleWeight", SqlDbType.Float).Value = Convert.ToSingle(weighttextarr[j].ToString()).ToString("#,##0.00");
                                    //com.Parameters.Add("@BalesNumber", SqlDbType.Int).Value = Convert.ToInt16(balesNumberarr[j].ToString());

                                    com.ExecuteNonQuery();

                                    //MessageBox.Show(sqlSave);
                                }

                            }

                            //บันทึกลูกหนี้
                            if (clsCash.sStatusCash == "2")
                            {
                                sb.Remove(0, sb.Length);
                                sb.Append("DELETE FROM  RECEIVABLE  ");
                                sb.Append("WHERE SaleHeadId = @SaleHeadId");
                                sb.Append(" INSERT INTO RECEIVABLE (ReceivableDate, CustomerId, ReceivebleType, ReceivebleTopic, SaleHeadId, ProductId, Amount, UnitPrice)");
                                sb.Append(" VALUES(@ReceivableDate,@CustomerId,@ReceivebleType,@ReceivebleTopic,@SaleHeadId,@ProductId,@Amount,@UnitPrice)");

                                sqlSave = sb.ToString();
                                com.Parameters.Clear();
                                com.CommandText = sqlSave;

                                com.Parameters.Add("@ReceivableDate", SqlDbType.DateTime).Value = dptSaleDate.Value.ToString("dd/MM/yyyy");

                                com.Parameters.Add("@CustomerId", SqlDbType.Int).Value = intCustomerId;

                                com.Parameters.Add("@ReceivebleType", SqlDbType.Int).Value = 0; // 0 = ตั้งหนี้จากการขายที่ยังไม่ยืนนยัน , 1= ตั้งหนี้จากการขายที่ยืนยัน , 2=ชำระหนี้//
                                com.Parameters.Add("@ReceivebleTopic", SqlDbType.NVarChar).Value = "ค้างชำระค่าสินค้า ";
                                com.Parameters.Add("@SaleHeadId", SqlDbType.Int).Value = lblSaleId.Text.Trim();
                                com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", ""); ;
                                com.Parameters.Add("@Amount", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                                com.Parameters.Add("@UnitPrice", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.00");
                                com.ExecuteNonQuery();
                                //MessageBox.Show(sqlSave);

                            }

                        }

                        // บันทึก log

                        sb.Remove(0, sb.Length);
                        sb.Append("INSERT INTO LOGSALE(LogSaleId,SaleHeadId,LogSaleDateTime,LogSaleTransection,LogsaleTotalCal,LogsaleTotal,UserName)");
                        sb.Append(" VALUES (@LogSaleId,@SaleHeadId,@LogSaleDateTime,@LogSaleTransection,@LogsaleTotalCal,@LogsaleTotal,@UserName)");

                        sqlSave = sb.ToString();

                        com.Parameters.Clear();
                        com.Transaction = tr;
                        com.CommandText = sqlSave;

                        com.Parameters.Add("@LogSaleId", SqlDbType.Int).Value = LogSaleId;
                        //MessageBox.Show(LogSaleId.ToString());
                        com.Parameters.Add("@saleHeadId", SqlDbType.Int).Value = lblSaleId.Text.Trim();
                        com.Parameters.Add("@LogSaleDateTime", SqlDbType.DateTime).Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                        com.Parameters.Add("@LogSaleTransection", SqlDbType.Int).Value = lsvProductList.Items.Count;
                        com.Parameters.Add("@LogsaleTotalCal", SqlDbType.Float).Value = SaleTotalCal.ToString("#,##0.00");
                        com.Parameters.Add("@LogsaleTotal", SqlDbType.Float).Value = SaleTotal.ToString("#,##0.00");
                        com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userId;
                        //MessageBox.Show(userId);
                        com.ExecuteNonQuery();
                        for (int i = 0; i <= lsvProductList.Items.Count - 1; i++)
                        {
                            sb.Remove(0, sb.Length);
                            sb.Append("INSERT INTO LOGSALEDETAIL ( LogSaleId,ProductId,Quantity,UnitPrice)");
                            sb.Append(" VALUES( @LogSaleId,@ProductId,@Quantity,@UnitPrice)");

                            sqlSave = sb.ToString();

                            com.Parameters.Clear();
                            com.CommandText = sqlSave;
                            com.Parameters.Add("@LogSaleId", SqlDbType.Int).Value = LogSaleId;
                            com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = lsvProductList.Items[i].SubItems[0].Text.Replace(",", "");
                            com.Parameters.Add("@Quantity", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[4].Text.Replace(",", "")).ToString("#,##0.00");
                            com.Parameters.Add("@UnitPrice", SqlDbType.Float).Value = Convert.ToSingle(lsvProductList.Items[i].SubItems[8].Text.Replace(",", "")).ToString("#,##0.00");
                            com.ExecuteNonQuery();

                        }

                        tr.Commit();

                        //                      ต้องการ print หรือไม่

                        ///*

                        insertSaleAppHead();
                        DialogResult dlg = MessageBox.Show("ต้องการพิมพ์รายงานหรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dlg == DialogResult.OK)
                        {
                            SalePos7CSK.frmSelectReport rptSale = new frmSelectReport();

                            //Sale.rptShowSaleSelect rptSale = new Sale.rptShowSaleSelect();
                            rptSale.StartPosition = FormStartPosition.CenterScreen;   // กำหนด frm ย่อย ให้อยู่ตรงกลาง
                            rptSale.setSaleId = Convert.ToInt32(lblSaleId.Text.Trim());
                            rptSale.ShowDialog();
                        }
                        else
                        {

                            MessageBox.Show("ปรับปรุงสำเร็จ");

                            Close();

                        }

                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        tr.Rollback();

                    }
                }
                Close();

            }
        }