diff --git a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java index d56959c704dc7eaac4bed5acd6e88281364928fb..c85eb843ce281dd1ce795254e839bc2378376099 100644 --- a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java +++ b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/templates/TemplateController.java @@ -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) { diff --git a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionImportController.java b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionImportController.java index ff03e523315f53339b8470100921cb9446000d8d..5f7d1b788d8c5222bc5ec0802fc0f8d6494792d2 100644 --- a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionImportController.java +++ b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionImportController.java @@ -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;