Choose template to use when emailing multiple invoices at once #472

This commit is contained in:
Hillel Coren 2023-06-11 12:44:18 +03:00
parent 2d7123d2c7
commit 9eb4ad2019
3 changed files with 51 additions and 63 deletions

View File

@ -68,7 +68,7 @@ class InvoiceRepository {
data: json.encode({
'ids': ids,
'action': action.toApiParam(),
if (template != null) 'email_type': 'email_template_$template',
if (template != null) 'email_type': '$template',
}));
final InvoiceListResponse invoiceResponse =

View File

@ -670,68 +670,52 @@ void handleInvoiceAction(BuildContext context, List<BaseEntity> invoices,
..parameters.entityType = EntityType.invoice.apiValue
..parameters.entityId = invoice.id));
} else {
confirmCallback(
context: context,
message: localization.bulkEmailInvoices,
callback: (_) {
store.dispatch(BulkEmailInvoicesRequest(
completer: snackBarCompleter<Null>(
context,
invoiceIds.length == 1
? localization.emailedInvoice
: localization.emailedInvoices),
invoiceIds: invoiceIds,
));
});
final template = await showDialog<EmailTemplate>(
context: context,
builder: (context) {
final settings = getClientSettings(state, client);
final templates = {
EmailTemplate.invoice: localization.initialEmail,
EmailTemplate.reminder1: localization.firstReminder,
EmailTemplate.reminder2: localization.secondReminder,
EmailTemplate.reminder3: localization.thirdReminder,
EmailTemplate.reminder_endless: localization.endlessReminder,
if ((settings.emailSubjectCustom1 ?? '').isNotEmpty)
EmailTemplate.custom1: localization.firstCustom,
if ((settings.emailSubjectCustom2 ?? '').isNotEmpty)
EmailTemplate.custom2: localization.secondCustom,
if ((settings.emailSubjectCustom3 ?? '').isNotEmpty)
EmailTemplate.custom3: localization.thirdCustom,
};
return SimpleDialog(
title: Text(
invoiceIds.length == 1
? localization.emailInvoice
: localization.emailCountInvoices
.replaceFirst(':count', '${invoiceIds.length}'),
),
children: templates.keys
.map((template) => SimpleDialogOption(
child: Text(templates[template]),
onPressed: () {
Navigator.of(context).pop(template);
},
))
.toList(),
);
},
);
if (false) {
final template = await showDialog<EmailTemplate>(
context: context,
builder: (context) {
final settings = getClientSettings(state, client);
final templates = {
EmailTemplate.invoice: localization.initialEmail,
EmailTemplate.reminder1: localization.firstReminder,
EmailTemplate.reminder2: localization.secondReminder,
EmailTemplate.reminder3: localization.thirdReminder,
EmailTemplate.reminder_endless: localization.endlessReminder,
if ((settings.emailSubjectCustom1 ?? '').isNotEmpty)
EmailTemplate.custom1: localization.firstCustom,
if ((settings.emailSubjectCustom2 ?? '').isNotEmpty)
EmailTemplate.custom2: localization.secondCustom,
if ((settings.emailSubjectCustom3 ?? '').isNotEmpty)
EmailTemplate.custom3: localization.thirdCustom,
};
return SimpleDialog(
title: Text(
invoiceIds.length == 1
? localization.emailInvoice
: localization.emailCountInvoices
.replaceFirst(':count', '${invoiceIds.length}'),
),
children: templates.keys
.map((template) => SimpleDialogOption(
child: Text(templates[template]),
onPressed: () {
Navigator.of(context).pop(template);
},
))
.toList(),
);
},
);
if (template != null) {
store.dispatch(BulkEmailInvoicesRequest(
completer: snackBarCompleter<Null>(
navigatorKey.currentContext,
invoiceIds.length == 1
? localization.emailedInvoice
: localization.emailedInvoices),
invoiceIds: invoiceIds,
template: template,
));
}
if (template != null) {
store.dispatch(BulkEmailInvoicesRequest(
completer: snackBarCompleter<Null>(
navigatorKey.currentContext,
invoiceIds.length == 1
? localization.emailedInvoice
: localization.emailedInvoices),
invoiceIds: invoiceIds,
template: template,
));
}
}
break;

View File

@ -385,7 +385,11 @@ Middleware<AppState> _bulkEmailInvoices(InvoiceRepository repository) {
repository
.bulkAction(
store.state.credentials, action.invoiceIds, EntityAction.sendEmail)
store.state.credentials,
action.invoiceIds,
EntityAction.sendEmail,
template: action.template,
)
.then((List<InvoiceEntity> invoices) {
store.dispatch(BulkEmailInvoicesSuccess(invoices));
if (action.completer != null) {