private IEnumerable <PembelianReturnModel> MappingObjects(DbContext context, IEnumerable <PembelianReturnModel> listObj)
        {
            if (listObj != null && listObj.ToList().Count > 0)
            {
                var pdRepo = new PembelianReturnDetailRepository(context);

                foreach (var p in listObj)
                {
                    p.PembelianReturnDetails = pdRepo.GetAll(p);
                }
            }

            return(listObj);
        }
        public void Insert(IPembelianReturnModel model)
        {
            var dataAccessStatus = new DataAccessStatus();

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

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

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

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

                    if (insertedId > 0 && model.PembelianReturnDetails.ToList().Count > 0)
                    {
                        model.id = insertedId;
                        model.PembelianReturnDetails = model.PembelianReturnDetails.Map(p => p.pembelian_return_id = model.id).ToList();
                        model.PembelianReturnDetails = model.PembelianReturnDetails.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;
                            }
                            else
                            {
                                var ex = new DataAccessException(dataAccessStatus);
                                SetDataAccessValues(ex, "Salah satu barang yang ingin ditambahkan ke dalam tabel pembelian return tidak ditemukan.");
                                throw ex;
                            }
                        });

                        var prdRepo = new PembelianReturnDetailRepository(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.PembelianReturn = model;

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

                        queryStr = "SELECT * FROM pembelian_detail WHERE pembelian_id = @pembelian_id";
                        var listPembelianDetails = context.Conn.Query <PembelianDetailModel>(queryStr, new { model.pembelian_id }).ToList();

                        foreach (var pd in listPembelianDetails)
                        {
                            foreach (var prd in model.PembelianReturnDetails)
                            {
                                if (pd.barang_id == prd.barang_id)
                                {
                                    pd.qty_return += prd.qty;
                                }
                            }

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

                        if (listPembelianDetails.Any(pd => pd.qty_return > pd.qty))
                        {
                            var ex = new DataAccessException(dataAccessStatus);
                            SetDataAccessValues(ex, "Salah satu qty return pembelian yang ingin disimpan melebihi qty yang terjual.");
                            throw ex;
                        }

                        foreach (var prd in model.PembelianReturnDetails)
                        {
                            prdRepo.Insert(prd, context.Transaction);

                            prd.Barang.stok += prd.qty;

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

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