Example #1
0
        public static UnitSalesFormViewModel CreateUnitSalesFormViewModel(IMCustomerRepository mCustomerRepository, IMCostCenterRepository mCostCenterRepository, ITTransUnitRepository tTransUnitRepository, string unitId)
        {
            UnitSalesFormViewModel viewModel = new UnitSalesFormViewModel();

            TTransUnit t = null;

            if (!string.IsNullOrEmpty(unitId))
            {
                t = tTransUnitRepository.GetByUnitId(unitId);
            }
            if (t == null)
            {
                t = new TTransUnit();
                t.SetAssignedIdTo(Guid.NewGuid().ToString());
                t.TransUnitDate = DateTime.Today;
            }
            viewModel.TransUnit = t;
            IList <MCustomer> listCust = mCustomerRepository.GetAll();
            var vals = from customer in listCust
                       select new { ID = customer.Id, Name = customer.PersonId.PersonName };

            //MCustomer cust = new MCustomer();
            //cust.CustomerDesc = "-Pilih Pembeli-";
            //vals.Union( .Insert(0, cust));
            viewModel.CustomerList = new SelectList(vals, "ID", "Name", t.CustomerId != null ? t.CustomerId.Id : null);

            IList <MCostCenter> list       = mCostCenterRepository.GetAll();
            MCostCenter         costCenter = new MCostCenter();

            costCenter.CostCenterName = "-Pilih Cost Center-";
            list.Insert(0, costCenter);
            viewModel.CostCenterList = new SelectList(list, "Id", "CostCenterName", t.CostCenterId != null ? t.CostCenterId.Id : null);

            var values = from EnumPaymentMethod e in Enum.GetValues(typeof(EnumPaymentMethod))
                         select new { ID = e, Name = e.ToString() };

            viewModel.PaymentMethodList = new SelectList(values, "Id", "Name", t.TransUnitPaymentMethod);

            return(viewModel);
        }
        public ActionResult UnitSales(TTransUnit viewModel, FormCollection formCollection, string unitId)
        {
            _tTransUnitRepository.DbContext.BeginTransaction();

            TUnit unit = _tUnitRepository.Get(unitId);

            TTransUnit transUnit = _tTransUnitRepository.GetByUnitId(unitId);
            bool       isSave    = true;

            if (transUnit == null)
            {
                transUnit = new TTransUnit();
                transUnit.SetAssignedIdTo(Guid.Empty.ToString());
                transUnit.CreatedDate = DateTime.Now;
                transUnit.CreatedBy   = User.Identity.Name;
                transUnit.DataStatus  = EnumDataStatus.New.ToString();
            }
            else
            {
                isSave = false;
                transUnit.ModifiedDate = DateTime.Now;
                transUnit.ModifiedBy   = User.Identity.Name;
                transUnit.DataStatus   = EnumDataStatus.Updated.ToString();
            }
            transUnit.UnitId = unit;
            if (!string.IsNullOrEmpty(formCollection["CustomerId"]))
            {
                transUnit.CustomerId = _mCustomerRepository.Get(formCollection["CustomerId"]);
            }
            transUnit.TransUnitDate          = viewModel.TransUnitDate;
            transUnit.TransUnitFactur        = viewModel.TransUnitFactur;
            transUnit.TransUnitPrice         = Convert.ToDecimal(formCollection["TransUnitPrice"].Replace(",", ""));
            transUnit.TransUnitDesc          = viewModel.TransUnitDesc;
            transUnit.TransUnitPaymentMethod = viewModel.TransUnitPaymentMethod;
            transUnit.CostCenterId           = unit.CostCenterId;

            //change unit status
            unit.UnitStatus   = EnumUnitStatus.Sale.ToString();
            unit.ModifiedDate = DateTime.Now;
            unit.ModifiedBy   = User.Identity.Name;
            unit.DataStatus   = EnumDataStatus.Updated.ToString();
            _tUnitRepository.Update(unit);

            try
            {
                if (isSave)
                {
                    _tTransUnitRepository.Save(transUnit);
                }
                else
                {
                    _tTransUnitRepository.Update(transUnit);
                }
                _tTransUnitRepository.DbContext.CommitTransaction();
                TempData[EnumCommonViewData.SaveState.ToString()] = EnumSaveState.Success;
            }
            catch (Exception ex)
            {
                _tTransUnitRepository.DbContext.RollbackTransaction();
                TempData[EnumCommonViewData.SaveState.ToString()] = EnumSaveState.Failed;
                throw ex;
                //return Content(ex.Message);
            }
            return(RedirectToAction("UnitSales", new RouteValueDictionary(new { unitId })));
        }