Skip to content
Snippets Groups Projects
Commit 01d08329 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#724 - use template and prefill missing template values

parent 61df2fa6
No related branches found
No related tags found
No related merge requests found
......@@ -6,9 +6,12 @@ import de.deadlocker8.budgetmaster.accounts.AccountService;
import de.deadlocker8.budgetmaster.controller.BaseController;
import de.deadlocker8.budgetmaster.icon.IconService;
import de.deadlocker8.budgetmaster.services.DateService;
import de.deadlocker8.budgetmaster.services.HelpersService;
import de.deadlocker8.budgetmaster.templategroup.TemplateGroupService;
import de.deadlocker8.budgetmaster.transactions.Transaction;
import de.deadlocker8.budgetmaster.transactions.TransactionImportController;
import de.deadlocker8.budgetmaster.transactions.TransactionService;
import de.deadlocker8.budgetmaster.transactions.csvimport.CsvTransaction;
import de.deadlocker8.budgetmaster.utils.Mappings;
import de.deadlocker8.budgetmaster.utils.ResourceNotFoundException;
import de.deadlocker8.budgetmaster.utils.WebRequestUtils;
......@@ -22,6 +25,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.server.ResponseStatusException;
......@@ -55,9 +59,10 @@ public class TemplateController extends BaseController
private final DateService dateService;
private final AccountService accountService;
private final IconService iconService;
private final HelpersService helpers;
@Autowired
public TemplateController(TemplateService templateService, TemplateGroupService templateGroupService, TransactionService transactionService, DateService dateService, AccountService accountService, IconService iconService)
public TemplateController(TemplateService templateService, TemplateGroupService templateGroupService, TransactionService transactionService, DateService dateService, AccountService accountService, IconService iconService, HelpersService helpers)
{
this.templateService = templateService;
this.templateGroupService = templateGroupService;
......@@ -65,6 +70,7 @@ public class TemplateController extends BaseController
this.dateService = dateService;
this.accountService = accountService;
this.iconService = iconService;
this.helpers = helpers;
}
@GetMapping
......@@ -134,7 +140,8 @@ public class TemplateController extends BaseController
}
@GetMapping("/{ID}/select")
public String selectTemplate(Model model,
public String selectTemplate(WebRequest request,
Model model,
@CookieValue("currentDate") String cookieDate,
@PathVariable("ID") Integer ID)
{
......@@ -162,6 +169,8 @@ public class TemplateController extends BaseController
newTransaction.setIsExpenditure(true);
}
overrideFieldsFromCsvTransaction(request, newTransaction);
final LocalDate date = dateService.getDateTimeFromCookie(cookieDate);
transactionService.prepareModelNewOrEdit(model, false, date, false, newTransaction, accountService.getAllActivatedAccountsAsc());
......@@ -172,6 +181,20 @@ public class TemplateController extends BaseController
return ReturnValues.NEW_TRANSACTION_NORMAL;
}
private void overrideFieldsFromCsvTransaction(WebRequest request, Transaction transaction)
{
final Object currentCsvTransaction = request.getAttribute(TransactionImportController.RequestAttributeNames.CURRENT_CSV_TRANSACTION, RequestAttributes.SCOPE_SESSION);
if(currentCsvTransaction != null)
{
final CsvTransaction csvTransaction = (CsvTransaction) currentCsvTransaction;
transaction.setDate(csvTransaction.getDate());
transaction.setAmount(csvTransaction.getAmount());
transaction.setIsExpenditure(csvTransaction.getAmount() <= 0);
// TODO: set category from CsvTransaction
}
}
@GetMapping("/newTemplate")
public String newTemplate(Model model)
{
......
......@@ -43,6 +43,7 @@ public class TransactionImportController extends BaseController
public static final String REDIRECT_CANCEL = "redirect:/transactionImport/cancel";
public static final String NEW_TRANSACTION_NORMAL = "transactions/newTransactionNormal";
public static final String NEW_TRANSACTION_TRANSFER = "transactions/newTransactionTransfer";
public static final String REDIRECT_TEMPLATES = "redirect:/templates";
}
public static class RequestAttributeNames
......@@ -254,6 +255,22 @@ public class TransactionImportController extends BaseController
return ReturnValues.NEW_TRANSACTION_NORMAL;
}
@GetMapping("/{index}/newFromTemplate")
public String newFromTemplate(WebRequest request,
@PathVariable("index") Integer index)
{
final Optional<CsvTransaction> transactionOptional = getTransactionByIndex(request, index);
if(transactionOptional.isEmpty())
{
return ReturnValues.REDIRECT_IMPORT;
}
final CsvTransaction csvTransaction = transactionOptional.get();
request.setAttribute(RequestAttributeNames.CURRENT_CSV_TRANSACTION, csvTransaction, RequestAttributes.SCOPE_SESSION);
return ReturnValues.REDIRECT_TEMPLATES;
}
@PostMapping("/{index}/newTransactionInPlace")
public String newTransactionInPlace(WebRequest request,
@PathVariable("index") Integer index,
......@@ -287,6 +304,7 @@ public class TransactionImportController extends BaseController
newTransaction.setAmount(csvTransaction.getAmount());
newTransaction.setIsExpenditure(csvTransaction.getAmount() <= 0);
newTransaction.setAccount(helpers.getCurrentAccountOrDefault());
// TODO: set category from CsvTransaction
newTransaction.setCategory(categoryService.findByType(CategoryType.NONE));
return newTransaction;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment