Exemplo n.º 1
0
 public static bool RemoveTransaction(TransactionHeadEntity head)
 {
     return(TransactionProvider.RemoveTransaction(p => p.Id == head.Id));
 }
        /// <summary>
        /// Adds or Modifies a transaction in the database.
        /// </summary>
        /// <param name="head">If the Id property equals 0, the function adds a new transaction. If there is a transaction with the same Id in the database, the function modifies it.</param>
        /// <param name="body"></param>
        /// <returns>If the function was successfull, it returns true.</returns>
        public static bool AddOrModifyTransaction(TransactionHeadEntity head, List <TransactionBodyListEntity> body)
        {
            if (head == null || body == null)
            {
                return(false);
            }
            bool result  = false;
            bool newHead = (head.Id == 0);

            using (var db = new InventoryContext(DatabaseConnection.ConnectionString))
            {
                using (var dbTransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        int transactionId = head.Id;

                        // HEAD record
                        if (!newHead) // Modify
                        {
                            var headQuery = db.TransactionHeader.Where(p => p.Id == head.Id);
                            foreach (var record in headQuery)
                            {
                                PropertyInfo[] properties = EntityCloner.GetProperties(typeof(TransactionHeadEntity));
                                foreach (PropertyInfo property in properties)
                                {
                                    property.SetValue(record, property.GetValue(head));
                                }
                            }
                            db.SaveChanges();
                            result = true;
                        }
                        else // Insert
                        {
                            db.TransactionHeader.Add(head);
                            db.SaveChanges();
                            result        = true;
                            transactionId = head.Id;
                        }

                        if (result)
                        {
                            // BODY records
                            result = false;
                            // Set transaction ID
                            foreach (var rec in body)
                            {
                                rec.Body.TransactionId = transactionId;
                            }

                            // Delete or modify existing records
                            var query = db.TransactionBody.Where(p => p.TransactionId == transactionId);
                            foreach (var rec in query)
                            {
                                var newrec = body.Where(p => p.Body.Id == rec.Id).FirstOrDefault();
                                if (newrec == null)
                                {
                                    db.TransactionBody.Remove(rec);
                                }
                                else
                                {
                                    PropertyInfo[] properties = EntityCloner.GetProperties(typeof(TransactionBodyEntity));
                                    foreach (PropertyInfo property in properties)
                                    {
                                        property.SetValue(rec, property.GetValue(newrec.Body));
                                    }
                                    body.Remove(newrec);
                                }
                            }

                            // Insert new records
                            foreach (var rec in body)
                            {
                                db.TransactionBody.Add(rec.Body);
                            }

                            db.SaveChanges();
                            dbTransaction.Commit();
                            result = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        log.Error("Cannot add or modify a transaction.", ex);
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 public static bool AddOrModifyTransaction(TransactionHeadEntity head, List <TransactionBodyListEntity> body)
 {
     return(TransactionProvider.AddOrModifyTransaction(head, body));
 }