Skip to content

Commit

Permalink
#724 - use template and prefill missing template values
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocker8 committed Jan 18, 2023
1 parent 61df2fa commit 01d0832
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
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;
Expand All @@ -22,6 +25,7 @@
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;

Expand Down Expand Up @@ -55,16 +59,18 @@ private static class ReturnValues
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;
this.transactionService = transactionService;
this.dateService = dateService;
this.accountService = accountService;
this.iconService = iconService;
this.helpers = helpers;
}

@GetMapping
Expand Down Expand Up @@ -134,7 +140,8 @@ public String deleteTemplate(WebRequest request, @PathVariable("ID") Integer ID)
}

@GetMapping("/{ID}/select")
public String selectTemplate(Model model,
public String selectTemplate(WebRequest request,
Model model,
@CookieValue("currentDate") String cookieDate,
@PathVariable("ID") Integer ID)
{
Expand Down Expand Up @@ -162,6 +169,8 @@ public String selectTemplate(Model model,
newTransaction.setIsExpenditure(true);
}

overrideFieldsFromCsvTransaction(request, newTransaction);

final LocalDate date = dateService.getDateTimeFromCookie(cookieDate);
transactionService.prepareModelNewOrEdit(model, false, date, false, newTransaction, accountService.getAllActivatedAccountsAsc());

Expand All @@ -172,6 +181,20 @@ public String selectTemplate(Model model,
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static class ReturnValues
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
Expand Down Expand Up @@ -254,6 +255,22 @@ public String newTransaction(WebRequest request,
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,
Expand Down Expand Up @@ -287,6 +304,7 @@ private Transaction createTransactionFromCsvTransaction(CsvTransaction csvTransa
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;
Expand Down

0 comments on commit 01d0832

Please sign in to comment.