示例#1
0
        public IPembelianModel GetByNoNota(object noNota)
        {
            var dataAccessStatus = new DataAccessStatus();

            using (var context = new DbContext())
            {
                var queryStr = "SELECT * FROM pembelian WHERE no_nota = @noNota";

                var model = context.Conn.Query <PembelianModel>(queryStr, new { noNota }).FirstOrDefault();

                if (model != null)
                {
                    var supplierModel = context.Conn.Get <SupplierModel>(model.supplier_id);

                    if (supplierModel != null)
                    {
                        model.Supplier = supplierModel;
                    }

                    var pdRepo = new PembelianDetailRepository(context);

                    model.PembelianDetails = pdRepo.GetAll(model);
                }
                return(model);
            }
        }
示例#2
0
        private IEnumerable <PembelianModel> MappingObjects(DbContext context, IEnumerable <PembelianModel> listObjs)
        {
            if (listObjs != null && listObjs.ToList().Count > 0)
            {
                listObjs = listObjs.Map(p => p.Supplier = context.Conn.Get <SupplierModel>(p.supplier_id));

                var pdRepo = new PembelianDetailRepository(context);

                foreach (var p in listObjs)
                {
                    p.PembelianDetails = pdRepo.GetAll(p);
                }
            }

            return(listObjs);
        }
示例#3
0
        public void Insert(IPembelianModel model)
        {
            var dataAccessStatus = new DataAccessStatus();

            using (var context = new DbContext())
            {
                context.BeginTransaction();

                model.no_nota = DbHelper.GetMaxID(context, context.Transaction, "pembelian", "no_nota");
                model.tanggal = DateTime.Now;

                Insert(model, () =>
                {
                    var queryStr = "INSERT INTO pembelian (supplier_id, tanggal, no_nota, diskon) " +
                                   "VALUES (@supplier_id, @tanggal, @no_nota, @diskon);" +
                                   "SELECT LAST_INSERT_ID();";

                    var insertedId = context.Conn.Query <uint>(queryStr, new
                    {
                        model.supplier_id,
                        model.tanggal,
                        model.no_nota,
                        model.diskon
                    }, context.Transaction).Single();

                    if (insertedId > 0 && model.PembelianDetails.ToList().Count > 0)
                    {
                        model.id = insertedId;
                        model.PembelianDetails = model.PembelianDetails.Map(p => p.pembelian_id = model.id).ToList();
                        model.PembelianDetails = model.PembelianDetails.Map(pd =>
                        {
                            var barang = context.Conn.Get <BarangModel>(pd.barang_id);

                            if (barang != null)
                            {
                                barang.Satuan         = context.Conn.Get <SatuanModel>(barang.satuan_id);
                                pd.Barang             = barang;
                                pd.Barang.supplier_id = model.supplier_id;
                            }
                            else
                            {
                                var ex = new DataAccessException(dataAccessStatus);
                                SetDataAccessValues(ex, "Salah satu barang yang ingin ditambahkan ke dalam tabel pembelian tidak ditemukan.");
                                throw ex;
                            }
                        });

                        var pdRepo = new PembelianDetailRepository(context);

                        var laporanStatusBarangModel = new StatusBarangModel();

                        queryStr     = "SELECT SUM(stok) FROM barang";
                        var stokAwal = context.Conn.ExecuteScalar <int>(queryStr);

                        laporanStatusBarangModel.stok_awal = stokAwal;
                        laporanStatusBarangModel.tanggal   = model.tanggal;
                        laporanStatusBarangModel.Pembelian = model;

                        context.Conn.Insert(laporanStatusBarangModel, context.Transaction);

                        foreach (var pd in model.PembelianDetails)
                        {
                            pdRepo.Insert(pd, context.Transaction);

                            pd.Barang.stok += pd.qty;

                            if (pd.hpp > 0)
                            {
                                pd.Barang.hpp = pd.hpp;
                            }

                            context.Conn.Update((BarangModel)pd.Barang, context.Transaction);
                        }

                        context.Commit();
                    }
                }, dataAccessStatus,
                       () => CheckAfterInsert(context, "SELECT COUNT(1) FROM pembelian WHERE no_nota=@no_nota "
                                              + "AND id=(SELECT id FROM pembelian ORDER BY ID DESC LIMIT 1)",
                                              new { model.no_nota }));
            }
        }