public Purchases_Invoice getPurchaseInvoice(SqlConnection con, int InvoiceID)
        {
            Purchases_Invoice obPurchaseInvoice = new Purchases_Invoice();
            DataTable         dt = new DataTable();

            try
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * from T_Purchase_Invoice WHERE InvoiceID = @InvoiceID", con);
                da.SelectCommand.Parameters.Add("@InvoiceID", SqlDbType.Int).Value = InvoiceID;
                da.Fill(dt);
                da.Dispose();
                if (dt.Rows.Count == 0)
                {
                    return(null);
                }
                obPurchaseInvoice.InvoiceID         = dt.Rows[0].Field <int>("InvoiceID");
                obPurchaseInvoice.InvoiceNo         = dt.Rows[0].Field <string>("InvoiceNo");
                obPurchaseInvoice.InvoiceType       = dt.Rows[0].Field <string>("InvoiceType");
                obPurchaseInvoice.SupplierAccountID = dt.Rows[0].Field <int>("SupplierAccount");

                obPurchaseInvoice.InvoiceDate         = dt.Rows[0].Field <DateTime>("InvoiceDate");
                obPurchaseInvoice.PurchasesAccountID  = dt.Rows[0].Field <int>("PurchaseAccount");
                obPurchaseInvoice.PurchasesAccount2ID = GlobalFunctions.isNull(dt.Rows[0].Field <object>("PurchaseAccount2"), 0);

                obPurchaseInvoice.TransAmmount = Convert.ToDouble(dt.Rows[0].Field <object>("TransAmount"));

                if (Convert.ToInt32(dt.Rows[0].Field <object>("TransRefID")) == 0)
                {
                    obPurchaseInvoice.TransRefID = 0;
                }
                else
                {
                    obPurchaseInvoice.TransRefID = Convert.ToInt32(dt.Rows[0].Field <object>("TransRefID"));
                }
                obPurchaseInvoice.StockRefID = Convert.ToInt32(dt.Rows[0].Field <object>("StockRefID"));
                obPurchaseInvoice.CurrencyID = Convert.ToInt32(dt.Rows[0].Field <int>("CurrencyID"));
                obPurchaseInvoice.Remarks    = dt.Rows[0].Field <string>("Remarks");
                obPurchaseInvoice.Rate       = Convert.ToDouble(dt.Rows[0].Field <object>("Rate"));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return(obPurchaseInvoice);
        }
        public int SaveUpdatePurchase_Invoice(Purchases_Invoice obPurchaseInvoice, SqlConnection con, SqlTransaction trans)
        {
            SqlCommand com    = null;
            int        LastID = 0;

            try
            {
                com             = new SqlCommand("spSaveUpdatePurchaseInvoice", con, trans);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add("@InvoiceID", SqlDbType.Int).Value           = obPurchaseInvoice.InvoiceID;
                com.Parameters.Add("@InvoiceType", SqlDbType.VarChar, 50).Value = obPurchaseInvoice.InvoiceType;
                com.Parameters.Add("@InvoiceNo", SqlDbType.VarChar, 100).Value  = obPurchaseInvoice.InvoiceNo;

                com.Parameters.Add("@InvoiceDate", SqlDbType.DateTime).Value  = obPurchaseInvoice.InvoiceDate;
                com.Parameters.Add("@SupplierAccountID", SqlDbType.Int).Value = obPurchaseInvoice.SupplierAccountID;
                com.Parameters.Add("@PurchaseAccountID", SqlDbType.Int).Value = obPurchaseInvoice.PurchasesAccountID;

                if (obPurchaseInvoice.CurrencyID <= 0)
                {
                    com.Parameters.Add("@CurrencyID", SqlDbType.Int).Value = DBNull.Value;
                }
                else
                {
                    com.Parameters.Add("@CurrencyID", SqlDbType.Int).Value = obPurchaseInvoice.CurrencyID;
                }

                com.Parameters.Add("@TransAmount", SqlDbType.Money).Value = obPurchaseInvoice.TransAmmount;
                if (obPurchaseInvoice.TransRefID <= 0)
                {
                    com.Parameters.Add("@TransRefID", SqlDbType.Int).Value = DBNull.Value;
                }
                else
                {
                    com.Parameters.Add("@TransRefID", SqlDbType.Int).Value = obPurchaseInvoice.TransRefID;
                }

                if (obPurchaseInvoice.StockRefID <= 0)
                {
                    com.Parameters.Add("@StockRefID", SqlDbType.Int).Value = DBNull.Value;
                }
                else
                {
                    com.Parameters.Add("@StockRefID", SqlDbType.Int).Value = obPurchaseInvoice.StockRefID;
                }
                com.Parameters.Add("@Remarks", SqlDbType.VarChar, 1000).Value = obPurchaseInvoice.Remarks;

                com.Parameters.Add("@CompanyID", SqlDbType.Int).Value          = LogInInfo.CompanyID;
                com.Parameters.Add("@UserID", SqlDbType.Int).Value             = LogInInfo.UserID;
                com.Parameters.Add("@Rate", SqlDbType.Money).Value             = obPurchaseInvoice.Rate;
                com.Parameters.Add("@PurchaseAccount2ID", SqlDbType.Int).Value = obPurchaseInvoice.PurchasesAccount2ID;

                com.ExecuteNonQuery();
                //if (obSalesInvoice.InvoiceID == 0)
                //{
                //    LastID = ConnectionHelper.GetID(con, "InvoiceID", "T_Sales_Invoice");
                //}
                //else
                //    LastID = obSalesInvoice.InvoiceID;
                if (obPurchaseInvoice.InvoiceID == 0)
                {
                    SqlCommand cmd = new SqlCommand("SELECT ISNULL(MAX(InvoiceID),0) FROM T_Purchase_Invoice", con, trans);
                    LastID = Convert.ToInt32(cmd.ExecuteScalar());
                }
                else
                {
                    LastID = obPurchaseInvoice.InvoiceID;
                }

                return(LastID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }