private void SaveAction(object obj) { try { if (Helpers.ShowMessage("Yakin Menyimpan Data ? ", "Pilih", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { if (IsBusy) { return; } IsBusy = true; pti model = (pti)this; model.FromId = SettingConfiguration.GetIntValue("CityId"); if (IsListValid(model.Collies)) { context.SaveChange(model); Saved = true; PTI p = new PTI { ShiperID = this.ShiperID, RecieverId = this.RecieverId, CreatedDate = this.CreatedDate, Id = this.Id, PayType = this.PayType, Pcs = this.Collies.Sum(O => O.Pcs), Weight = this.Collies.Sum(O => O.Weight), RecieverName = this.Reciever.Name, ShiperName = this.Shiper.Name, Biaya = this.Collies.Sum(O => O.Biaya), User = Authorization.User.Name }; ParentSource.Add(p); Helpers.ShowMessage("PTI Berhasil Disimpan"); if (Helpers.ShowMessage("Input PTI Baru ? ", "Pilih", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { SetNew(); } else { WindowClose(); } } else { Helpers.ShowErrorMessage("Item Data Tidak Lengkap, Periksa Kembali"); } } } catch (Exception ex) { MyMessage.Show(ex.Message, "Error", MessageBoxButton.OK); } finally { IsBusy = false; } }
public void SaveChange(pti item) { DateTime da = DateTime.Now; using (var db = new OcphDbContext()) { var trans = db.BeginTransaction(); try { if (User.CanAccess(MethodBase.GetCurrentMethod())) { if (item.Shiper.Id <= 0) { item.Shiper.CreatedDate = da; item.ShiperID = db.Customers.InsertAndGetLastID(item.Shiper); item.Shiper.Id = item.ShiperID; if (item.ShiperID <= 0) { throw new SystemException("Data Tidak Tersimpan"); } var his = User.GenerateHistory(item.ShiperID, BussinesType.Customer, ChangeType.Create, "Menambah Shiper"); db.Histories.Insert(his); } else { item.ShiperID = item.Shiper.Id; } if (item.Reciever.Id <= 0) { item.Reciever.CreatedDate = da; item.RecieverId = db.Customers.InsertAndGetLastID(item.Reciever); item.Reciever.Id = item.RecieverId; if (item.RecieverId <= 0) { throw new SystemException("Data Tidak Tersimpan"); } var his = User.GenerateHistory(item.RecieverId, BussinesType.Customer, ChangeType.Create, ""); db.Histories.Insert(his); } else { item.RecieverId = item.Reciever.Id; } if (!db.PTI.Insert(item)) { throw new SystemException("Data Tidak Tersimpan"); } foreach (var data in item.Collies) { data.PtiId = item.Id; data.Id = db.Collies.InsertAndGetLastID(data); if (data.Id <= 0) { throw new SystemException("Data Tidak Tersimpan"); } } var history = User.GenerateHistory(item.Id, BussinesType.PTI, ChangeType.Create, ""); db.Histories.Insert(history); trans.Commit(); } else { throw new SystemException(NotHaveAccess); } } catch (Exception ex) { trans.Rollback(); throw new SystemException(ex.Message); } } }