public static bool Cancel(ProductSlip productSlip)
        {
            MySqlTransaction tr = null;

            try
            {
                using (var conn = new MySqlConnection(Globals.CONN_STR))
                {
                    conn.Open();
                    tr = conn.BeginTransaction();
                    var sql = "";

                    sql = @"UPDATE product_slip
								SET  active=@active 
								WHERE product_slip_no=@product_slip_no"                                ;
                    var cmd = new MySqlCommand(sql, conn)
                    {
                        Transaction = tr
                    };
                    cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                    cmd.Parameters.AddWithValue("active", productSlip.Active);
                    var affRow = cmd.ExecuteNonQuery();

                    tr.Commit();
                }
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void LoadData()
        {
            txtProductSlipNo.Enabled = false;
            Order order = OrderController.GetOrder(this.orderNo);

            if (order != null)
            {
                txtProductSlipNo.Text      = "";
                txtOrderNo.Text            = order.OrderNo;
                dtpProductSlipDate.Value   = order.RequestDate;
                cboCustomer.SelectedValue  = order.Customer.CustomerCode;
                chkActive.Checked          = order.Active;
                dtpProductSlipDate.Enabled = false;
            }
            else
            {
                BtnCancel.Visible = false;
            }

            ProductSlip productSlip = ProductSlipController.GetProductSlipByOrderNo(orderNo);

            if (productSlip != null)
            {
                txtProductSlipNo.Text = productSlip.ProductSlipNo;
            }
            else
            {
                BtnCancel.Visible = false;
            }

            LoadDetail();
        }
        private void SaveProductSlip()
        {
            try
            {
                var productSlipItems = new List <ProductSlipItem>();
                int seq = 0;
                foreach (DataRow row in dtProductSlipItem.Rows)
                {
                    seq++;
                    var xx = new ProductSlipItem();


                    xx.ProductSlipNo = txtProductSlipNo.Text;
                    xx.Seq           = seq;
                    xx.Product       = new Product
                    {
                        ProductCode = row[ConstColumns.PRODUCT_CODE].ToString(),
                        ProductName = row[ConstColumns.PRODUCT_NAME].ToString(),
                    };
                    xx.Location = new Location
                    {
                        LocationCode = int.Parse(row[ConstColumns.LOCATION_CODE].ToString()),
                        LocationName = row[ConstColumns.LOCATION_NAME].ToString(),
                    };
                    xx.LotNo = row[ConstColumns.LOT_NO].ToString();
                    xx.Qty   = row[ConstColumns.ISSUE_UNIT_METHOD].ToString() == "Q" ? Convert.ToInt16(row[ConstColumns.QTY_WGH_LOCATION]) : 0;
                    xx.Wgh   = row[ConstColumns.ISSUE_UNIT_METHOD].ToString() == "W" ? Convert.ToDecimal(row[ConstColumns.QTY_WGH_LOCATION]) : 0;
                    productSlipItems.Add(xx);
                }
                var productSlip = new ProductSlip
                {
                    ProductSlipNo   = txtProductSlipNo.Text,
                    ProductSlipDate = dtpProductSlipDate.Value,
                    RefDocumentNo   = txtOrderNo.Text,
                    ProductSlipFlag = 0,
                    Active          = chkActive.Checked,
                    CreateBy        = "system",
                    ModifiedBy      = "system",
                    ProductSlipItem = productSlipItems
                };

                if (string.IsNullOrEmpty(txtProductSlipNo.Text))
                {
                    txtProductSlipNo.Text = ProductSlipController.Insert(productSlip);
                }
                else
                {
                    ProductSlipController.Update(productSlip);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 private void CancelProductSlip()
 {
     try
     {
         var productSlip = new ProductSlip
         {
             ProductSlipNo   = txtProductSlipNo.Text,
             ProductSlipDate = dtpProductSlipDate.Value,
             ProductSlipFlag = 0,
             Active          = false,
             CreateBy        = "system",
             ModifiedBy      = "system"
         };
         ProductSlipController.Cancel(productSlip);
     }
     catch (Exception)
     {
         throw;
     }
 }
        public static bool Update(ProductSlip productSlip)
        {
            MySqlTransaction tr = null;

            try
            {
                using (var conn = new MySqlConnection(Globals.CONN_STR))
                {
                    conn.Open();
                    tr = conn.BeginTransaction();
                    var sql = @"SELECT product_slip_flag FROM product_slip WHERE product_slip_no=@product_slip_no";
                    var cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                    var productSlipFlag = (int)cmd.ExecuteScalar();

                    if (productSlipFlag > 0)
                    {
                        throw new Exception("ไม่สามารถบันทึกเอกสารได้ \n\t เนื่องจากเอกสารได้นำไปใช้งานแล้ว");
                    }
                    sql = @"UPDATE slaughterhouse.product_slip
							SET 
								product_slip_date = @product_slip_date,
								ref_document_no = @ref_document_no,
								product_slip_flag = @product_slip_flag,
								active=@active,
								modified_at=CURRENT_TIMESTAMP,
								modified_by=@modified_by
							WHERE product_slip_no = @product_slip_no
						  "                        ;
                    cmd = new MySqlCommand(sql, conn)
                    {
                        Transaction = tr
                    };
                    cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                    cmd.Parameters.AddWithValue("product_slip_date", productSlip.ProductSlipDate.ToString("yyyy-MM-dd"));
                    cmd.Parameters.AddWithValue("ref_document_no", productSlip.RefDocumentNo);
                    cmd.Parameters.AddWithValue("product_slip_flag", productSlip.ProductSlipFlag);
                    cmd.Parameters.AddWithValue("active", productSlip.Active);
                    cmd.Parameters.AddWithValue("modified_by", productSlip.ModifiedBy);
                    var affRow = cmd.ExecuteNonQuery();

                    sql = @"Delete From product_slip_item 
								WHERE product_slip_no=@product_slip_no"                                ;
                    cmd = new MySqlCommand(sql, conn)
                    {
                        Transaction = tr
                    };
                    cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                    cmd.ExecuteNonQuery();

                    sql = @"INSERT INTO slaughterhouse.product_slip_item
							(product_slip_no,
								product_code,
								location_code,
								seq,
								create_by )
							VALUES
							   (@product_slip_no ,
								@product_code ,
								@location_code ,
								@seq , 
								@create_by ) 
							"                            ;

                    foreach (var item in productSlip.ProductSlipItem)
                    {
                        cmd = new MySqlCommand(sql, conn)
                        {
                            Transaction = tr
                        };
                        cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                        cmd.Parameters.AddWithValue("product_code", item.Product.ProductCode);
                        cmd.Parameters.AddWithValue("location_code", item.Location);
                        cmd.Parameters.AddWithValue("seq", item.Seq);
                        cmd.Parameters.AddWithValue("create_by", productSlip.CreateBy);
                        cmd.ExecuteNonQuery();
                    }
                    tr.Commit();
                }
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string Insert(ProductSlip productSlip)
        {
            MySqlTransaction tr = null;

            try
            {
                using (var conn = new MySqlConnection(Globals.CONN_STR))
                {
                    productSlip.ProductSlipNo = DocumentGenerate.GetDocumentRunning("PDS");
                    conn.Open();
                    tr = conn.BeginTransaction();
                    var sql = @"INSERT INTO product_slip
									(product_slip_no,
									product_slip_date,
									ref_document_no,
									product_slip_flag,
									active,
									create_by)
								VALUES
									(@product_slip_no,
									 @product_slip_date,
									 @ref_document_no,
									 @product_slip_flag,
									 @active,
									 @create_by ) 
							   "                            ;
                    var cmd = new MySqlCommand(sql, conn)
                    {
                        Transaction = tr
                    };
                    cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                    cmd.Parameters.AddWithValue("product_slip_date", productSlip.ProductSlipDate.ToString("yyyy-MM-dd"));
                    cmd.Parameters.AddWithValue("ref_document_no", productSlip.RefDocumentNo);
                    cmd.Parameters.AddWithValue("product_slip_flag", productSlip.ProductSlipFlag);
                    cmd.Parameters.AddWithValue("active", productSlip.Active);
                    cmd.Parameters.AddWithValue("create_by", productSlip.CreateBy);
                    cmd.ExecuteNonQuery();

                    sql = @"INSERT INTO slaughterhouse.product_slip_item
							   (product_slip_no,
								product_code,
								location_code,
								lot_no,
								seq, qty, wgh,
								create_by )
							VALUES
							   (@product_slip_no ,
								@product_code ,
								@location_code ,
								@lot_no ,
								@seq , @qty , @wgh , 
								@create_by )
							"                            ;

                    foreach (var item in productSlip.ProductSlipItem)
                    {
                        cmd = new MySqlCommand(sql, conn)
                        {
                            Transaction = tr
                        };
                        cmd.Parameters.AddWithValue("product_slip_no", productSlip.ProductSlipNo);
                        cmd.Parameters.AddWithValue("product_code", item.Product.ProductCode);
                        cmd.Parameters.AddWithValue("location_code", item.Location.LocationCode);
                        cmd.Parameters.AddWithValue("lot_no", item.LotNo);
                        cmd.Parameters.AddWithValue("seq", item.Seq);
                        cmd.Parameters.AddWithValue("qty", item.Qty);
                        cmd.Parameters.AddWithValue("wgh", item.Wgh);
                        cmd.Parameters.AddWithValue("create_by", productSlip.CreateBy);
                        cmd.ExecuteNonQuery();
                    }
                    tr.Commit();
                }
                return(productSlip.ProductSlipNo);
            }
            catch (Exception)
            {
                tr.Rollback();
                throw;
            }
        }