示例#1
0
        public Document GenerateDocument()
        {
            Document doc = new Document();
            doc.Id = TarpaDataProvider.GetLastDocumentId() + 1;
            doc.BillId = BillId;
            doc.Time = BillDate;
            List<Export> exports = new List<Export>();

            Export e = new Export();
            double totalSum = 0.0;

            foreach (BillRow row in Rows)
            {
                Export export = new Export();
                if (Type == 0)
                {
                    export.Credit = row.GetSum();
                    export.Debet = 0.0;
                    totalSum += row.GetSum();
                    export.DocumentId = doc.Id;
                    export.AccountId = row.AccountId;
                }

                exports.Add(export);
            }

            doc.Exports = exports;
            doc.Optimize();

            switch (PaymentType)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                default:
                    break;
            }

            return doc;
        }
        /// <summary>
        /// Updates the given document to the database.
        /// </summary>
        /// <param name="document">The document to be updated.</param>
        public static void UpdateDocument(Document document)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
            using (MySqlConnection con = new MySqlConnection(connectionString))
            {

                try
                {
                    con.Open();

                    MySqlCommand command = new MySqlCommand();
                    command.Connection = con;

                    command.CommandText = "UPDATE documents SET date=@date, billId=@billId WHERE documentId=@documentId";
                    command.Parameters.AddWithValue("@documentId", document.Id);
                    command.Parameters.AddWithValue("@date", document.Time);
                    command.Parameters.AddWithValue("@billId", document.BillId);
                    command.ExecuteNonQuery();

                }
                catch (MySqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                }

            }
        }
        /// <summary>
        /// Returns the list of documents stored in the database.
        /// </summary>
        /// <returns>List of documents</returns>
        public static List<Document> GetAllDocuments()
        {
            List<Document> documents = new List<Document>();
            string connectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;

            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                MySqlCommand command = new MySqlCommand();

                try
                {
                    con.Open();
                    command.Connection = con;

                    command.CommandText = "SELECT * FROM documents";

                    MySqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        Document document = new Document();
                        document.Id = reader.GetInt16("documentId");
                        document.BillId = reader.GetInt16("billId");
                        document.Time = reader.GetDateTime("date");
                        documents.Add(document);
                    }

                }
                catch (MySqlException ex)
                {
                    throw ex;
                } finally
                {
                    con.Close();
                }

            }
            return documents;
        }
        /// <summary>
        /// Saves the document object to the database.
        /// </summary>
        /// <param name="document">Document to be saved</param>
        public static void SaveDocument(Document document)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
            using (MySqlConnection con = new MySqlConnection(connectionString))
            {

                try
                {
                    con.Open();

                    MySqlCommand command = new MySqlCommand();
                    command.Connection = con;

                    command.CommandText = "INSERT INTO documents (documentId, billId, date) VALUES (@documentId, @billId, @date)";
                    command.Parameters.AddWithValue("@documentId", document.Id);
                    command.Parameters.AddWithValue("@date", document.Time);
                    command.Parameters.AddWithValue("@billId", document.BillId);
                    command.ExecuteNonQuery();

                }
                catch (MySqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();

                }

            }
        }
        /// <summary>
        /// Deletes the row representing the given document from the database.
        /// Removes also all the exports attached to the document id.
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static bool DeleteDocument(Document document)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                try
                {
                    con.Open();

                    MySqlCommand command = new MySqlCommand();
                    command.Connection = con;
                    command.CommandText = "DELETE FROM documents WHERE documentId=@id";
                    command.Parameters.AddWithValue("@id", document.Id);
                    command.ExecuteNonQuery();

                    DeleteAllExportsFromDocument(document);
                }
                catch (MySqlException ex)
                {
                    return false;
                } finally
                {
                    con.Close();
                }

            }

            return true;
        }