diff --git a/Aberwyn/Controllers/BudgetApiController.cs b/Aberwyn/Controllers/BudgetApiController.cs index 742c6a4..4897967 100644 --- a/Aberwyn/Controllers/BudgetApiController.cs +++ b/Aberwyn/Controllers/BudgetApiController.cs @@ -434,52 +434,6 @@ namespace Aberwyn.Controllers } - // POST: api/budget/copy/byname/{targetName}?from={sourceName} - [HttpPost("copy/byname/{targetName}")] - public async Task CopyFromNamedBudget(string targetName, [FromQuery] string from) - { - var targetPeriod = await _context.BudgetPeriods - .Include(p => p.Categories) - .ThenInclude(c => c.Items) - .FirstOrDefaultAsync(p => p.Name != null && p.Name.ToLower() == targetName.ToLower()); - - if (targetPeriod != null && targetPeriod.Categories.Any()) - return BadRequest("Det finns redan data för denna budget."); - - var sourcePeriod = await _context.BudgetPeriods - .Include(p => p.Categories) - .ThenInclude(c => c.Items) - .FirstOrDefaultAsync(p => p.Name != null && p.Name.ToLower() == from.ToLower()); - - if (sourcePeriod == null) - return NotFound("Ingen budget hittades att kopiera från."); - - var newPeriod = new BudgetPeriod - { - Name = targetName, - Categories = sourcePeriod.Categories.Select(cat => new BudgetCategory - { - Name = cat.Name, - Color = cat.Color, - Order = cat.Order, - BudgetCategoryDefinitionId = cat.BudgetCategoryDefinitionId, - Items = cat.Items.Select(item => new BudgetItem - { - Name = item.Name, - Amount = item.Amount, - IsExpense = item.IsExpense, - IncludeInSummary = item.IncludeInSummary, - Order = item.Order, - BudgetItemDefinitionId = item.BudgetItemDefinitionId - }).ToList() - }).ToList() - }; - - _context.BudgetPeriods.Add(newPeriod); - await _context.SaveChangesAsync(); - - return Ok(new { id = newPeriod.Id }); - } [HttpDelete("item/{id}")] @@ -629,5 +583,131 @@ namespace Aberwyn.Controllers + + // Gemensam intern metod + private async Task CopyBudgetAsync( + BudgetPeriod targetPeriod, + BudgetPeriod sourcePeriod) + { + if (sourcePeriod == null) return null; + + targetPeriod.Categories = sourcePeriod.Categories.Select(cat => new BudgetCategory + { + Name = cat.Name, + Color = cat.Color, + Order = cat.Order, + BudgetCategoryDefinitionId = cat.BudgetCategoryDefinitionId, + Items = cat.Items.Select(item => new BudgetItem + { + Name = item.Name, + Amount = item.Amount, + IsExpense = item.IsExpense, + IncludeInSummary = item.IncludeInSummary, + Order = item.Order, + BudgetItemDefinitionId = item.BudgetItemDefinitionId + }).ToList() + }).ToList(); + + await _context.SaveChangesAsync(); + return targetPeriod; + } + [HttpPost("copy/byname/{targetName}")] + public async Task CopyToNamedBudget( +string targetName, +[FromQuery] string? from, +[FromQuery] int? fromYear, +[FromQuery] int? fromMonth) + { + var targetPeriod = await _context.BudgetPeriods + .Include(p => p.Categories) + .ThenInclude(c => c.Items) + .FirstOrDefaultAsync(p => p.Name != null && p.Name.ToLower() == targetName.ToLower()); + + if (targetPeriod == null) + { + targetPeriod = new BudgetPeriod { Name = targetName }; + _context.BudgetPeriods.Add(targetPeriod); + } + else if (targetPeriod.Categories.Any()) + { + return BadRequest("Det finns redan data för denna budget."); + } + + // Hämta källperiod + var sourcePeriod = await FindSourcePeriod(from, fromYear, fromMonth); + if (sourcePeriod == null) + return NotFound("Ingen budget hittades att kopiera från."); + + await CopyBudgetAsync(targetPeriod, sourcePeriod); + return Ok(new { id = targetPeriod.Id }); + } + [HttpPost("copy/{year:int}/{month:int}")] + public async Task CopyToYearMonth( + int year, + int month, + [FromQuery] string? from, + [FromQuery] int? fromYear, + [FromQuery] int? fromMonth) + { + var targetPeriod = await _context.BudgetPeriods + .Include(p => p.Categories) + .ThenInclude(c => c.Items) + .FirstOrDefaultAsync(p => p.Year == year && p.Month == month); + + if (targetPeriod == null) + { + targetPeriod = new BudgetPeriod { Year = year, Month = month }; + _context.BudgetPeriods.Add(targetPeriod); + } + else if (targetPeriod.Categories.Any()) + { + return BadRequest("Det finns redan data för denna månad."); + } + + // Hämta källperiod + var sourcePeriod = await FindSourcePeriod(from, fromYear, fromMonth, year, month); + if (sourcePeriod == null) + return NotFound("Ingen data att kopiera från."); + + await CopyBudgetAsync(targetPeriod, sourcePeriod); + return Ok(new { id = targetPeriod.Id }); + } + private async Task FindSourcePeriod( + string? from, + int? fromYear, + int? fromMonth, + int? defaultYear = null, + int? defaultMonth = null) + { + BudgetPeriod? sourcePeriod = null; + + if (!string.IsNullOrEmpty(from)) + { + sourcePeriod = await _context.BudgetPeriods + .Include(p => p.Categories) + .ThenInclude(c => c.Items) + .FirstOrDefaultAsync(p => p.Name != null && p.Name.ToLower() == from.ToLower()); + } + else if (fromYear.HasValue && fromMonth.HasValue) + { + sourcePeriod = await _context.BudgetPeriods + .Include(p => p.Categories) + .ThenInclude(c => c.Items) + .FirstOrDefaultAsync(p => p.Year == fromYear && p.Month == fromMonth); + } + else if (defaultYear.HasValue && defaultMonth.HasValue) + { + var previous = new DateTime(defaultYear.Value, defaultMonth.Value, 1).AddMonths(-1); + sourcePeriod = await _context.BudgetPeriods + .Include(p => p.Categories) + .ThenInclude(c => c.Items) + .FirstOrDefaultAsync(p => p.Year == previous.Year && p.Month == previous.Month); + } + + return sourcePeriod; + } + + + } } diff --git a/Aberwyn/Views/Budget/Index.cshtml b/Aberwyn/Views/Budget/Index.cshtml index 0ef1989..063cf62 100644 --- a/Aberwyn/Views/Budget/Index.cshtml +++ b/Aberwyn/Views/Budget/Index.cshtml @@ -28,9 +28,8 @@ Lista - - {{ budget.name }} - + + -
-

Det finns ingen budgetdata för - {{ budget.name || (selectedMonth + '/' + selectedYear) }}. + + +

+

+ +

- +
+ -
+
+ + +
+

+ Budgeten {{ budget.name || (getMonthName(selectedMonth) + " " + selectedYear) }} har inga kategorier än. +

+ +
+ + +
+ + + + + +