Пример #1
0
        public int Save(BeliProduk obj)
        {
            var result = 0;

            try
            {
                _context.BeginTransaction();

                var transaction = _context.transaction;

                obj.beli_produk_id = _context.GetGUID();
                obj.total_nota     = GetTotalNota(obj);

                // insert header
                _context.db.Insert <BeliProduk>(obj, transaction);

                // insert detail
                foreach (var item in obj.item_beli.Where(f => f.Produk != null))
                {
                    if (item.produk_id.Length > 0)
                    {
                        item.item_beli_produk_id = _context.GetGUID();
                        item.beli_produk_id      = obj.beli_produk_id;
                        item.pengguna_id         = obj.pengguna_id;

                        _context.db.Insert <ItemBeliProduk>(item, transaction);

                        // update entity state
                        item.entity_state = EntityState.Unchanged;
                    }
                }

                // jika pembelian tunai, langsung insert ke pembayaran hutang
                if (obj.tanggal_tempo.IsNull())
                {
                    result = SavePembayaranHutang(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);
        }
Пример #2
0
        public int Delete(BeliProduk obj)
        {
            var result = 0;

            try
            {
                result = _context.db.Delete <BeliProduk>(obj) ? 1 : 0;

                if (result > 0)
                {
                    LogicalThreadContext.Properties["OldValue"] = obj.ToJson();
                    _log.Info("Hapus data");
                }
            }
            catch (Exception ex)
            {
                _log.Error("Error:", ex);
            }

            return(result);
        }
Пример #3
0
        public int Update(BeliProduk obj)
        {
            var result = 0;

            try
            {
                _context.BeginTransaction();

                var transaction = _context.transaction;

                obj.total_nota = GetTotalNota(obj);

                // update header
                result = _context.db.Update <BeliProduk>(obj, transaction) ? 1 : 0;

                // delete detail
                foreach (var item in obj.item_beli_deleted)
                {
                    result = _context.db.Delete <ItemBeliProduk>(item, transaction) ? 1 : 0;
                }

                // insert/update detail
                foreach (var item in obj.item_beli.Where(f => f.Produk != null))
                {
                    item.beli_produk_id = obj.beli_produk_id;
                    item.pengguna_id    = obj.pengguna_id;

                    if (item.entity_state == EntityState.Added)
                    {
                        item.item_beli_produk_id = _context.GetGUID();

                        _context.db.Insert <ItemBeliProduk>(item, transaction);

                        result = 1;
                    }
                    else if (item.entity_state == EntityState.Modified)
                    {
                        result = _context.db.Update <ItemBeliProduk>(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 = HapusPembayaranHutang(obj);
                    if (result > 0)
                    {
                        obj.total_pelunasan = 0;
                    }
                }
                else if (obj.tanggal_tempo.IsNull()) // jika pembelian tunai, langsung update ke pembayaran hutang
                {
                    result = SavePembayaranHutang(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)
            {
                _log.Error("Error:", ex);
            }

            return(result);
        }