public void Update(Purchase purchase) { Transaction tx = null; try { using (var em = EntityManagerFactory.CreateInstance(ds)) { tx = em.BeginTransaction(); string[] columns = { "PurchaseCode", "PurchaseDate", "SupplierId", "PaymentMethod", "Status", "Notes", "GrandTotal", "AmountInWords", "DueDate", "TermOfPayment", "ModifiedDate", "ModifiedBy" }; object[] values = { purchase.Code, purchase.Date, purchase.SupplierId, purchase.PaymentMethod, purchase.Status == true?1:0, purchase.Notes, purchase.GrandTotal, purchase.AmountInWords, purchase.DueDate.ToShortDateString(), purchase.TermOfPayment, DateTime.Now.ToShortDateString(), Store.ActiveUser }; var q = new Query().Select(columns).From(tableName).Update(values) .Where("ID").Equal("{" + purchase.ID + "}"); em.ExecuteNonQuery(q.ToSql(), tx); //detail dihapus -> update qty var list = purchaseItemRepository.GetByPurchaseId(purchase.ID); foreach (var purchaseItem in list) { productQtyRepository.UpdateQtyIn(Store.ActiveMonth, Store.ActiveYear, purchaseItem.ProductId, purchaseItem.Qty, false); } purchaseItemRepository.Delete(em, tx, purchase.ID); foreach (var purchaseItem in purchase.PurchaseItems) { purchaseItem.PurchaseId = purchase.ID; purchaseItemRepository.Save(em, tx, purchaseItem); //update product productQtyRepository.UpdateQtyIn(Store.ActiveMonth, Store.ActiveYear, purchaseItem.ProductId, purchaseItem.Qty, true); } UpdateGrandTotal(em, tx, purchase.ID, purchase.GrandTotal); tx.Commit(); } } catch (Exception ex) { tx.Rollback(); throw ex; } }
// DELETE: api/Employee/5 public void Delete(int id) { var tonerJob = repository.Get(filter: (t) => t.Id == id, includeProperties: "PurchasedItems.StockItem,Toners").Single(); var itemsToDelete = tonerJob.PurchasedItems.ToList(); tonerJob.ReturnPurchaseItems(); repository.Delete(tonerJob); itemsToDelete.ForEach(p => purchaseItemRepository.Delete(p)); }