public int Add(ExpenditureParam param) { int id = 0; Validate.NotNull(param.ExpenditureName, "Expenditure name is required."); var budget = _budgetRepository.Get(param.BudgetId); Validate.NotNull(budget, "Budget name is not found."); Expenditure modelHeader = new Expenditure(); using (_unitOfWork) { var currentData = _expenditureRepository.GetByExpenditureDate(param.ExpenditureDate); if (currentData == null) { Expenditure model = new Expenditure(param.ExpenditureDate, param.Price, param.CreatedBy, param.CreatedDate); _expenditureRepository.Insert(model); id = model.Id; modelHeader = model; //insert detail ExpenditureDetail modelDetail = new ExpenditureDetail(param.ExpenditureName, param.ExpenditureDefinition, param.Price, model, budget, param.CreatedBy, param.CreatedDate); _expenditureDetailRepository.Insert(modelDetail); } else { //insert detail ExpenditureDetail modelDetail = new ExpenditureDetail(param.ExpenditureName, param.ExpenditureDefinition, param.Price, modelHeader, budget, param.CreatedBy, param.CreatedDate); _expenditureDetailRepository.Insert(modelDetail); //add total var dataHeader = _expenditureRepository.Get(param.ExpenditureId); var totalPerDay = CalculateTotalPerDay(param.ExpenditureId); var grandTotal = dataHeader.Total + totalPerDay; dataHeader.AddTotal(grandTotal); } _unitOfWork.Commit(); } return(id); }