public int Save(JualProduk obj) { var result = 0; try { _context.BeginTransaction(); var transaction = _context.transaction; obj.jual_id = _context.GetGUID(); obj.total_nota = GetTotalNota(obj); // insert header _context.db.Insert <JualProduk>(obj, transaction); // insert detail foreach (var item in obj.item_jual.Where(f => f.Produk != null)) { if (item.produk_id.Length > 0) { item.item_jual_id = _context.GetGUID(); item.jual_id = obj.jual_id; item.pengguna_id = obj.pengguna_id; _context.db.Insert <ItemJualProduk>(item, transaction); // update entity state item.entity_state = EntityState.Unchanged; } } // jika pembelian tunai, langsung insert ke pembayaran hutang if (obj.tanggal_tempo.IsNull()) { result = SavePembayaranPiutang(obj); if (result > 0) { obj.total_pelunasan = obj.grand_total; } } _context.Commit(); LogicalThreadContext.Properties["NewValue"] = obj.ToJson(); _log.Info("Tambah data"); result = 1; } catch (Exception ex) { _log.Error("Error:", ex); } return(result); }
public int Delete(JualProduk obj) { var result = 0; try { result = _context.db.Delete <JualProduk>(obj) ? 1 : 0; if (result > 0) { LogicalThreadContext.Properties["OldValue"] = obj.ToJson(); _log.Info("Hapus data"); } } catch (Exception ex) { _log.Error("Error:", ex); } return(result); }
public int Update(JualProduk obj) { var result = 0; try { _context.BeginTransaction(); var transaction = _context.transaction; obj.total_nota = GetTotalNota(obj); // update header result = _context.db.Update <JualProduk>(obj, transaction) ? 1 : 0; // delete detail foreach (var item in obj.item_jual_deleted) { result = _context.db.Delete <ItemJualProduk>(item, transaction) ? 1 : 0; } // insert/update detail foreach (var item in obj.item_jual.Where(f => f.Produk != null)) { item.jual_id = obj.jual_id; item.pengguna_id = obj.pengguna_id; if (item.entity_state == EntityState.Added) { item.item_jual_id = _context.GetGUID(); _context.db.Insert <ItemJualProduk>(item, transaction); result = 1; } else if (item.entity_state == EntityState.Modified) { result = _context.db.Update <ItemJualProduk>(item, transaction) ? 1 : 0; } // update entity state item.entity_state = EntityState.Unchanged; } // jika terjadi perubahan status nota dari tunai ke kredit if (obj.tanggal_tempo_old.IsNull() && !obj.tanggal_tempo.IsNull()) { result = HapusPembayaranPiutang(obj); if (result > 0) { obj.total_pelunasan = 0; } } else if (obj.tanggal_tempo.IsNull()) // jika penjualan tunai, langsung update ke pembayaran piutang { result = SavePembayaranPiutang(obj); if (result > 0) { obj.total_pelunasan = obj.grand_total; } } _context.Commit(); LogicalThreadContext.Properties["NewValue"] = obj.ToJson(); _log.Info("Update data"); result = 1; } catch (Exception ex) { result = 0; _log.Error("Error:", ex); } return(result); }