add placeholders for subject of booking

This commit is contained in:
Frederik Jaeckel 2024-03-09 19:34:44 +01:00
parent 42aeefaa19
commit acc8911a8d
5 changed files with 88 additions and 14 deletions

View file

@ -106,6 +106,10 @@ msgctxt "view:cashbook.planner:"
msgid "Booking text" msgid "Booking text"
msgstr "Buchungstext" msgstr "Buchungstext"
msgctxt "view:cashbook.planner:"
msgid "Available placeholders: ${date} ${month} ${year} ${amount} ${quantity}"
msgstr "verfügbare Platzhalter: ${date} ${month} ${year} ${amount} ${quantity}"
msgctxt "field:cashbook.planner,company:" msgctxt "field:cashbook.planner,company:"
msgid "Company" msgid "Company"
msgstr "Unternehmen" msgstr "Unternehmen"

View file

@ -82,6 +82,10 @@ msgctxt "view:cashbook.planner:"
msgid "Booking text" msgid "Booking text"
msgstr "Booking text" msgstr "Booking text"
msgctxt "view:cashbook.planner:"
msgid "Available placeholders: ${date} ${month} ${year} ${amount} ${quantity}"
msgstr "Available placeholders: ${date} ${month} ${year} ${amount} ${quantity}"
msgctxt "field:cashbook.planner,company:" msgctxt "field:cashbook.planner,company:"
msgid "Company" msgid "Company"
msgstr "Company" msgstr "Company"

View file

@ -5,6 +5,7 @@
from decimal import Decimal from decimal import Decimal
from datetime import date, timedelta from datetime import date, timedelta
from string import Template
from dateutil.rrule import ( from dateutil.rrule import (
rrule, YEARLY, MONTHLY, WEEKLY, DAILY, MO, TU, WE, TH, FR, SA, SU) rrule, YEARLY, MONTHLY, WEEKLY, DAILY, MO, TU, WE, TH, FR, SA, SU)
from trytond.model import ModelSQL, ModelView, fields, Index, DeactivableMixin from trytond.model import ModelSQL, ModelView, fields, Index, DeactivableMixin
@ -430,6 +431,60 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
IrDate = Pool().get('ir.date') IrDate = Pool().get('ir.date')
return IrDate.today() return IrDate.today()
@classmethod
def fill_placeholder(cls, linedata):
""" replace placeholder in description
Args:
description (str): booking text of planned booking
allowed substitution strings:
${date}, ${month}, ${year}, ${amount}, ${quantity}
Returns:
str: booking text
"""
pool = Pool()
IrDate = pool.get('ir.date')
Cashbook = pool.get('cashbook.book')
line_date = linedata.get('date', IrDate.today())
amount = linedata.get('amount', None)
from_book = linedata.get('cashbook', None)
if from_book:
from_book = Cashbook(from_book)
to_book = linedata.get('booktransf', None)
if to_book:
to_book = Cashbook(to_book)
quantity_txt = '-'
quantity = linedata.get('quantity', None)
if quantity is not None:
uom = (
to_book.quantity_uom if to_book and to_book.quantity_uom
else from_book.quantity_uom
if from_book and from_book.quantity_uom else None)
uom_digits = (
to_book.quantity_digits
if to_book and to_book.quantity_digits is not None
else from_book.quantity_digits
if from_book and from_book.quantity_digits is not None
else 2)
if uom:
quantity_txt = Report.format_number_symbol(
quantity, lang=None, symbol=uom, digits=uom_digits)
else:
quantity_txt = Report.format_number(
quantity, lang=None, digits=uom_digits)
return Template(linedata.get('description')).safe_substitute({
'date': Report.format_date(line_date, lang=None),
'month': line_date.month,
'year': line_date.year,
'amount': Report.format_currency(
amount, lang=None, currency=from_book.currency)
if (amount is not None) and from_book else '-',
'quantity': quantity_txt})
@classmethod @classmethod
def update_next_occurence(cls, records, query_date=None): def update_next_occurence(cls, records, query_date=None):
""" compute date of next execution, create/update nextrun-record, """ compute date of next execution, create/update nextrun-record,
@ -573,6 +628,7 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
if record.booktransf.feature == 'asset': if record.booktransf.feature == 'asset':
line.update(add_asset_values( line.update(add_asset_values(
line, record.cashbook, record.booktransf)) line, record.cashbook, record.booktransf))
line['description'] = cls.fill_placeholder(line)
if record.wfcheck: if record.wfcheck:
to_create_check.append(line) to_create_check.append(line)

View file

@ -353,6 +353,23 @@ class PlannerTestCase(object):
IrDate.today = MagicMock(return_value=date.today()) IrDate.today = MagicMock(return_value=date.today())
@with_transaction()
def test_planner_check_description_template(self):
""" check replacement of template strings in description
"""
pool = Pool()
Planner = pool.get('cashbook.planner')
asset_book = self.prep_planner_asset_book()
self.assertEqual(Planner.fill_placeholder({
'date': date(2022, 5, 2),
'amount': Decimal('12.4567'),
'quantity': Decimal('32.4423'),
'cashbook': asset_book.id,
'description': '- ${amount} - ${date} - ${month} - ' +
'${year} - ${quantity}'}),
'- usd12.46 - 05/02/2022 - 5 - 2022 - 32.4423\xa0u')
@with_transaction() @with_transaction()
def test_planner_cronjobs_booking_with_category(self): def test_planner_cronjobs_booking_with_category(self):
""" create job, configure booking with category, run job """ create job, configure booking with category, run job
@ -397,8 +414,7 @@ class PlannerTestCase(object):
self.assertEqual(len(job.cashbook.lines), 1) self.assertEqual(len(job.cashbook.lines), 1)
self.assertEqual( self.assertEqual(
job.cashbook.lines[0].rec_name, job.cashbook.lines[0].rec_name,
"06/01/2022|Exp|-10.00 usd|booking " + "06/01/2022|Exp|-10.00 usd|booking 6/2022, 06/01/2022 [Cat1]")
"${month}/${year}, ${date} [Cat1]")
self.assertEqual(job.cashbook.lines[0].state, 'check') self.assertEqual(job.cashbook.lines[0].state, 'check')
with Transaction().set_context({'date': date(2022, 6, 1)}): with Transaction().set_context({'date': date(2022, 6, 1)}):
@ -464,8 +480,8 @@ class PlannerTestCase(object):
self.assertEqual(len(cashbook.lines), 1) self.assertEqual(len(cashbook.lines), 1)
self.assertEqual( self.assertEqual(
cashbook.lines[0].rec_name, cashbook.lines[0].rec_name,
"06/01/2022|to|-10.00 usd|booking ${month}/${year}, " + "06/01/2022|to|-10.00 usd|booking 6/2022, 06/01/2022 " +
"${date} [Book 2 | 10.00 usd | Open]") "[Book 2 | 10.00 usd | Open]")
self.assertEqual(cashbook.lines[0].state, 'check') self.assertEqual(cashbook.lines[0].state, 'check')
target_book, = Cashbook.browse([job.booktransf]) target_book, = Cashbook.browse([job.booktransf])
@ -473,8 +489,8 @@ class PlannerTestCase(object):
self.assertEqual(len(target_book.lines), 1) self.assertEqual(len(target_book.lines), 1)
self.assertEqual( self.assertEqual(
target_book.lines[0].rec_name, target_book.lines[0].rec_name,
"06/01/2022|from|10.00 usd|booking ${month}/${year}, " + "06/01/2022|from|10.00 usd|booking 6/2022, 06/01/2022 " +
"${date} [Book 1 | -10.00 usd | Open]") "[Book 1 | -10.00 usd | Open]")
self.assertEqual(target_book.lines[0].state, 'check') self.assertEqual(target_book.lines[0].state, 'check')
self.assertEqual(target_book.lines[0].state, 'check') self.assertEqual(target_book.lines[0].state, 'check')
@ -775,12 +791,4 @@ class PlannerTestCase(object):
IrDate.today = MagicMock(return_value=date.today()) IrDate.today = MagicMock(return_value=date.today())
@with_transaction()
def test_planner_cronjobs_booking_transfer_asset_eur_usd2(self):
""" create job, configure transfer-booking to asset-cashbook,
from euro to usd,
asset-unit = u, cashbook-unit = 10x u
"""
raise ValueError('stop')
# end PlannerTestCase # end PlannerTestCase

View file

@ -58,6 +58,8 @@ full copyright notices and license terms. -->
<separator name="subject" colspan="4" string="Booking text"/> <separator name="subject" colspan="4" string="Booking text"/>
<field name="subject" colspan="4"/> <field name="subject" colspan="4"/>
<label id="lab1" colspan="4" xalign="0.0"
string="Available placeholders: ${date} ${month} ${year} ${amount} ${quantity}"/>
</page> </page>
<page name="description" col="1" string="Description"> <page name="description" col="1" string="Description">
<field name="description"/> <field name="description"/>