Commit 51456a3c authored by Jérome Perrin's avatar Jérome Perrin

Fix for invoices with date <= 1900

Because the helper script for invoice, (order, packing lists, etc) printout is using `DateTime.strftime` it does not support dates before 1900.
This is a [documented behavior](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) in python that dates before 1900 cannot be used.

In these cases it's not supposed to happen, but if by mistake user enter a date before 1900, printing the invoice would fail, which is not good.

These patterns were very simple and did not need strftime.

/reviewed-on nexedi/erp5!694
parents ce6223bc ef2aec16
...@@ -63,13 +63,15 @@ def getSocialCapital(reg_cap): ...@@ -63,13 +63,15 @@ def getSocialCapital(reg_cap):
preferred_date_order = context.getPortalObject().portal_preferences\ preferred_date_order = context.getPortalObject().portal_preferences\
.getPreferredDateOrder() or 'ymd' .getPreferredDateOrder() or 'ymd'
separator = '/'
def getOrderedDate(date): def getOrderedDate(date):
if date is None: if date is None:
return '' return ''
pattern = separator.join(['%%%s' % s for s in list(preferred_date_order)]) date_parts = {
pattern = pattern.replace('y', 'Y') 'y': '%04d' % date.year(),
return date.strftime(pattern) 'm': '%02d' % date.month(),
'd': '%02d' % date.day(),
}
return '/'.join([date_parts[part] for part in preferred_date_order])
def getPaymentConditionText(order): def getPaymentConditionText(order):
if 'custom' == order.getPaymentConditionTradeDate(): if 'custom' == order.getPaymentConditionTradeDate():
......
...@@ -73,13 +73,15 @@ def getCareerId(career_title): ...@@ -73,13 +73,15 @@ def getCareerId(career_title):
preferred_date_order = context.getPortalObject().portal_preferences\ preferred_date_order = context.getPortalObject().portal_preferences\
.getPreferredDateOrder() or 'ymd' .getPreferredDateOrder() or 'ymd'
separator = '/'
def getOrderedDate(date): def getOrderedDate(date):
if date is None: if date is None:
return '' return ''
pattern = separator.join(['%%%s' % s for s in list(preferred_date_order)]) date_parts = {
pattern = pattern.replace('y', 'Y') 'y': '%04d' % date.year(),
return date.strftime(pattern) 'm': '%02d' % date.month(),
'd': '%02d' % date.day(),
}
return '/'.join([date_parts[part] for part in preferred_date_order])
def getPaymentConditionText(order): def getPaymentConditionText(order):
if order.getPaymentConditionPaymentEndOfMonth(): if order.getPaymentConditionPaymentEndOfMonth():
......
...@@ -100,13 +100,15 @@ year_to_date_total_employer_tax = paysheet.PaySheetTransaction_getYearToDateMove ...@@ -100,13 +100,15 @@ year_to_date_total_employer_tax = paysheet.PaySheetTransaction_getYearToDateMove
preferred_date_order = portal.portal_preferences\ preferred_date_order = portal.portal_preferences\
.getPreferredDateOrder() or 'ymd' .getPreferredDateOrder() or 'ymd'
separator = '/'
def getOrderedDate(date): def getOrderedDate(date):
if date is None: if date is None:
return '' return ''
pattern = separator.join(['%%%s' % s for s in list(preferred_date_order)]) date_parts = {
pattern = pattern.replace('y', 'Y') 'y': '%04d' % date.year(),
return date.strftime(pattern) 'm': '%02d' % date.month(),
'd': '%02d' % date.day(),
}
return '/'.join([date_parts[part] for part in preferred_date_order])
def getPaymentConditionText(paysheet): def getPaymentConditionText(paysheet):
date = '' date = ''
......
...@@ -1762,6 +1762,53 @@ class TestInvoice(TestInvoiceMixin): ...@@ -1762,6 +1762,53 @@ class TestInvoice(TestInvoiceMixin):
if err_list: if err_list:
self.fail(''.join(err_list)) self.fail(''.join(err_list))
def test_Invoice_viewAsODT_date_before_1900(self):
# Regression test for invoices with a date before 1900, which
# python-2.7's strftime does not support.
resource = self.portal.getDefaultModule(
self.resource_portal_type
).newContent(
portal_type=self.resource_portal_type,
title='Resource',)
file_data = FileUpload(__file__)
client = self.portal.organisation_module.newContent(
portal_type='Organisation',
title='Client')
vendor = self.portal.organisation_module.newContent(
portal_type='Organisation',
title='Vendor')
invoice = self.portal.getDefaultModule(
self.invoice_portal_type
).newContent(
portal_type=self.invoice_portal_type,
start_date=DateTime(102, 12, 31),
stop_date=DateTime(103, 12, 31),
title='Invoice',
specialise=self.business_process,
source_value=vendor,
source_section_value=vendor,
destination_value=client,
destination_section_value=client)
invoice.newContent(
portal_type=self.invoice_line_portal_type,
resource_value=resource,
quantity=10,
price=3)
invoice.confirm()
self.tic()
data_dict = invoice.Invoice_getODTDataDict()
self.assertEqual('0102/12/31', data_dict['start_date'])
self.assertEqual('0103/12/31', data_dict['stop_date'])
# rendering is valid odf
odt = invoice.Invoice_viewAsODT()
from Products.ERP5OOo.tests.utils import Validator
odf_validator = Validator()
err_list = odf_validator.validate(odt)
if err_list:
self.fail(''.join(err_list))
def test_invoice_building_with_cells(self): def test_invoice_building_with_cells(self):
# if the order has cells, the invoice built from that order must have # if the order has cells, the invoice built from that order must have
# cells too # cells too
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment