diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f55a4e..4f8fc74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog All notable changes to this project will be documented in this file. +## [0.4.1] - 2020-11-26 + +- Bring back calculateTotalOnObjectArray (#11) +- Bypassing default ordering by when generating the report (#10) +- Fix in dates in template and view + + ## [0.4.0] - 2020-11-24 [BREAKING] - Renamed `SampleReportView` to `SlickReportView` diff --git a/slick_reporting/__init__.py b/slick_reporting/__init__.py index 1823624..a3b6a97 100644 --- a/slick_reporting/__init__.py +++ b/slick_reporting/__init__.py @@ -1,6 +1,6 @@ default_app_config = 'slick_reporting.apps.ReportAppConfig' -VERSION = (0, 4, 0) +VERSION = (0, 4, 1) -__version__ = '0.4.0' +__version__ = '0.4.1' diff --git a/slick_reporting/fields.py b/slick_reporting/fields.py index d5e5555..8a5b037 100644 --- a/slick_reporting/fields.py +++ b/slick_reporting/fields.py @@ -153,7 +153,7 @@ def prepare(self, q_filters=None, kwargs_filters=None, **kwargs): def get_queryset(self): queryset = self.report_model.objects - return queryset + return queryset.order_by() def get_annotation_name(self): """ diff --git a/slick_reporting/form_factory.py b/slick_reporting/form_factory.py index a6026aa..8a979f8 100644 --- a/slick_reporting/form_factory.py +++ b/slick_reporting/form_factory.py @@ -117,14 +117,14 @@ def report_form_factory(model, fkeys_filter_func=None, foreign_key_widget_func=N fields['start_date'] = forms.DateTimeField(required=False, label=_('From date'), initial=app_settings.SLICK_REPORTING_DEFAULT_START_DATE, - widget=forms.DateTimeInput(format='%m/%d/%Y %H:%M', - attrs={'autocomplete': "off"}), + widget=forms.DateTimeInput( + attrs={'autocomplete': "off"}), ) fields['end_date'] = forms.DateTimeField(required=False, label=_('To date'), initial=app_settings.SLICK_REPORTING_DEFAULT_END_DATE, - widget=forms.DateTimeInput(format='%m/%d/%Y %H:%M', - attrs={'autocomplete': "off"}) + widget=forms.DateTimeInput( + attrs={'autocomplete': "off"}) ) for name, f_field in fkeys_map.items(): diff --git a/slick_reporting/generator.py b/slick_reporting/generator.py index bd55327..3306ba1 100644 --- a/slick_reporting/generator.py +++ b/slick_reporting/generator.py @@ -164,6 +164,7 @@ def __init__(self, report_model=None, main_queryset=None, start_date=None, end_d self.crosstab_compute_reminder = self.crosstab_compute_reminder if crosstab_compute_reminder is None else crosstab_compute_reminder main_queryset = main_queryset or self.report_model.objects + main_queryset = main_queryset.order_by() self.columns = self.columns or columns or [] self.group_by = self.group_by or group_by diff --git a/slick_reporting/static/slick_reporting/ra.chartsjs.js b/slick_reporting/static/slick_reporting/ra.chartsjs.js index 4edf86d..9ae722d 100644 --- a/slick_reporting/static/slick_reporting/ra.chartsjs.js +++ b/slick_reporting/static/slick_reporting/ra.chartsjs.js @@ -66,6 +66,36 @@ return chartObject } + function calculateTotalOnObjectArray(data, columns) { + // Compute totals in array of objects + // example : + // calculateTotalOnObjectArray ([{ value1:500, value2: 70} , {value:200, value2:15} ], ['value']) + // return {'value1': 700, value2:85} + + let total_container = {}; + for (let r = 0; r < data.length; r++) { + + for (let i = 0; i < columns.length; i++) { + if (typeof total_container[columns[i]] == 'undefined') { + total_container[columns[i]] = 0; + } + let val = data[r][columns[i]]; + if (val === '-') val = 0; + + else if (typeof (val) == 'string') { + try { + val = val.replace(/,/g, ''); + } catch (err) { + console.log(err, val, typeof (val)); + } + } + total_container[columns[i]] += parseFloat(val); + } + } + return total_container; +} + + function extractDataFromResponse(response, chartOptions) { let dataFieldName = chartOptions['data_source']; let titleFieldName = chartOptions['title_source']; @@ -157,7 +187,7 @@ function getObjFromArray(objList, obj_key, key_value, failToFirst) { failToFirst = typeof (failToFirst) !== 'undefined'; if (key_value !== '') { - for (var i = 0; i < objList.length; i++) { + for (let i = 0; i < objList.length; i++) { if (objList[i][obj_key] === key_value) { return objList[i]; } diff --git a/slick_reporting/templates/slick_reporting/simple_report.html b/slick_reporting/templates/slick_reporting/simple_report.html index 415ed4e..55fa2cc 100644 --- a/slick_reporting/templates/slick_reporting/simple_report.html +++ b/slick_reporting/templates/slick_reporting/simple_report.html @@ -102,14 +102,12 @@

Results

$('.raReportDateRange').daterangepicker(setDatePickerObj()).on('apply.daterangepicker', function (ev, picker) { var container = picker.element; - var $startDate = container.find('[name=start_date_0]'); - var $startTime = container.find('[name=start_date_1]'); - var $endDate = container.find('[name=end_date_0]'); - var $endTime = container.find('[name=end_date_1]'); - $startDate.val(picker.startDate.format('YYYY') + '-' + picker.startDate.format('MM') + "-" + picker.startDate.format('DD')) - $startTime.val(picker.startDate.format('HH') + ":" + picker.startDate.format('mm')); - $endDate.val(picker.endDate.format('YYYY') + '-' + picker.endDate.format('MM') + "-" + picker.endDate.format('DD')) - $endTime.val(picker.endDate.format('HH') + ":" + picker.endDate.format('mm')); + var $startDate = container.find('[name=start_date]'); + var $startTime = container.find('[name=start_date]'); + var $endDate = container.find('[name=end_date]'); + var $endTime = container.find('[name=end_date]'); + $startDate.val(picker.startDate.format('YYYY') + '-' + picker.startDate.format('MM') + "-" + picker.startDate.format('DD') + ' ' +picker.startDate.format('HH') + ":" + picker.startDate.format('mm')) + $endDate.val(picker.endDate.format('YYYY') + '-' + picker.endDate.format('MM') + "-" + picker.endDate.format('DD') +' ' + picker.endDate.format('HH') + ":" + picker.endDate.format('mm')) container.parents('.panel').find('.refreshReport').trigger('click'); }) @@ -130,7 +128,7 @@

Results

try { myChart.destroy(); } catch (err) { - console.error(err); + console.info(err); } myChart = new Chart($chart, chartObject); diff --git a/slick_reporting/views.py b/slick_reporting/views.py index a31a94c..33a5190 100644 --- a/slick_reporting/views.py +++ b/slick_reporting/views.py @@ -117,6 +117,8 @@ def get_report_generator(self, queryset, for_print): self.crosstab_compute_reminder = self.form.get_crosstab_compute_reminder() return self.report_generator_class(self.report_model, + start_date=self.form.cleaned_data['start_date'], + end_date=self.form.cleaned_data['end_date'], q_filters=q_filters, kwargs_filters=kw_filters, date_field=self.date_field, diff --git a/tests/models.py b/tests/models.py index 42bbd43..03cb9fc 100644 --- a/tests/models.py +++ b/tests/models.py @@ -35,14 +35,18 @@ class SimpleSales(models.Model): quantity = models.DecimalField(_('quantity'), max_digits=19, decimal_places=2, default=0) price = models.DecimalField(_('price'), max_digits=19, decimal_places=2, default=0) value = models.DecimalField(_('value'), max_digits=19, decimal_places=2, default=0) + created_at = models.DateTimeField(null=True, verbose_name=_('Created at')) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): - self.value = self.quantity * self.price + self.value = self.quantity * self.price super().save(force_insert, force_update, using, update_fields) class Meta: verbose_name = _('Sale') verbose_name_plural = _('Sales') + ordering = ['-created_at'] + # # class Invoice(BaseMovementInfo): diff --git a/tests/tests.py b/tests/tests.py index 9edd281..d0921f0 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -50,14 +50,14 @@ def setUpTestData(cls): SimpleSales.objects.create( doc_date=datetime.datetime(year, 1, 2), client=cls.client1, - product=cls.product1, quantity=10, price=10) + product=cls.product1, quantity=10, price=10, created_at=datetime.datetime(year, 1, 5)) SimpleSales.objects.create( doc_date=datetime.datetime(year, 2, 2), client=cls.client1, - product=cls.product1, quantity=10, price=10) + product=cls.product1, quantity=10, price=10, created_at=datetime.datetime(year, 2, 3)) SimpleSales.objects.create( doc_date=datetime.datetime(year, 3, 2), client=cls.client1, - product=cls.product1, quantity=10, price=10) + product=cls.product1, quantity=10, price=10, created_at=datetime.datetime(year, 3, 3)) # client 2 SimpleSales.objects.create(