1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

[ongoing] close period for max 1 year + force contiguous periods + fix totals

This commit is contained in:
Sylvain 2019-03-11 16:54:08 +01:00
parent 2a32bc4752
commit 4e7a62bc2b
4 changed files with 13 additions and 11 deletions

View File

@ -659,13 +659,14 @@ Application.Controllers.controller('ClosePeriodModalController', ['$scope', '$ui
function ($scope, $uibModalInstance, $window, Invoice, AccountingPeriod, periods, lastClosingEnd, dialogs, growl, _t) { function ($scope, $uibModalInstance, $window, Invoice, AccountingPeriod, periods, lastClosingEnd, dialogs, growl, _t) {
const YESTERDAY = moment.utc({ h: 0, m: 0, s: 0, ms: 0 }).subtract(1, 'day').toDate(); const YESTERDAY = moment.utc({ h: 0, m: 0, s: 0, ms: 0 }).subtract(1, 'day').toDate();
const LAST_CLOSING = moment.utc(lastClosingEnd.last_end_date).toDate(); const LAST_CLOSING = moment.utc(lastClosingEnd.last_end_date).toDate();
const MAX_END = moment.utc(lastClosingEnd.last_end_date).add(1, 'year').toDate();
/* PUBLIC SCOPE */ /* PUBLIC SCOPE */
// date pickers values are bound to these variables // date pickers values are bound to these variables
$scope.period = { $scope.period = {
start_at: LAST_CLOSING, start_at: LAST_CLOSING,
end_at: YESTERDAY end_at: moment(YESTERDAY).isBefore(MAX_END) ? YESTERDAY : MAX_END,
}; };
// any form errors will come here // any form errors will come here
@ -684,7 +685,7 @@ Application.Controllers.controller('ClosePeriodModalController', ['$scope', '$ui
startOpened: false, startOpened: false,
endOpened: false, endOpened: false,
minDate: LAST_CLOSING, minDate: LAST_CLOSING,
maxDate: YESTERDAY, maxDate: moment(YESTERDAY).isBefore(MAX_END) ? YESTERDAY : MAX_END,
options: { options: {
startingDay: Fablab.weekStartingDay startingDay: Fablab.weekStartingDay
} }
@ -693,10 +694,10 @@ Application.Controllers.controller('ClosePeriodModalController', ['$scope', '$ui
/** /**
* Callback to open the datepicker * Callback to open the datepicker
*/ */
$scope.openDatePicker = function ($event, pickerId) { $scope.toggleDatePicker = function ($event) {
$event.preventDefault(); $event.preventDefault();
$event.stopPropagation(); $event.stopPropagation();
$scope.datePicker[`${pickerId}Opened`] = true; $scope.datePicker.endOpened = !$scope.datePicker.endOpened;
}; };
/** /**

View File

@ -15,10 +15,10 @@
datepicker-options="datePicker.options" datepicker-options="datePicker.options"
is-open="datePicker.startOpened" is-open="datePicker.startOpened"
min-date="datePicker.minDate" min-date="datePicker.minDate"
max-date="datePicker.maxDate" max-date="datePicker.minDate"
init-date="period.start_at" init-date="period.start_at"
placeholder="{{datePicker.format}}" placeholder="{{datePicker.format}}"
ng-click="openDatePicker($event, 'start')" readonly
required/> required/>
</div> </div>
<span class="help-block" ng-show="closePeriodForm.start_at.$dirty && closePeriodForm.start_at.$error.required" translate>{{ 'invoices.start_date_is_required' }}</span> <span class="help-block" ng-show="closePeriodForm.start_at.$dirty && closePeriodForm.start_at.$error.required" translate>{{ 'invoices.start_date_is_required' }}</span>
@ -39,7 +39,7 @@
max-date="datePicker.maxDate" max-date="datePicker.maxDate"
init-date="period.end_at" init-date="period.end_at"
placeholder="{{datePicker.format}}" placeholder="{{datePicker.format}}"
ng-click="openDatePicker($event, 'end')" ng-click="toggleDatePicker($event)"
required/> required/>
</div> </div>
<span class="help-block" ng-show="closePeriodForm.end_at.$dirty && closePeriodForm.end_at.$error.required" translate>{{ 'invoices.end_date_is_required' }}</span> <span class="help-block" ng-show="closePeriodForm.end_at.$dirty && closePeriodForm.end_at.$error.required" translate>{{ 'invoices.end_date_is_required' }}</span>

View File

@ -21,7 +21,7 @@ class AccountingPeriod < ActiveRecord::Base
end end
def invoices def invoices
Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start_at, end_date: end_at) Invoice.where('created_at >= :start_date AND created_at <= :end_date', start_date: start_at, end_date: end_at)
end end
def archive_file def archive_file
@ -70,8 +70,9 @@ class AccountingPeriod < ActiveRecord::Base
end end
def compute_totals def compute_totals
self.period_total = invoices.all.map(&:total).reduce(:+) self.period_total = invoices.all.map(&:total).reduce(:+) || 0
self.perpetual_total = Invoice.where('created_at < :end_date', end_date: end_at).all.map(&:total).reduce(:+) self.perpetual_total = Invoice.where('created_at <= :end_date AND type IS NULL', end_date: end_at)
.all.map(&:total).reduce(:+) || 0
self.footprint = compute_footprint self.footprint = compute_footprint
end end

View File

@ -5,7 +5,7 @@ class DateRangeValidator < ActiveModel::Validator
def validate(record) def validate(record)
the_end = record.start_at the_end = record.start_at
the_start = record.end_at the_start = record.end_at
return unless the_end.present? && the_end >= the_start return unless the_end.present? && the_end > the_start
record.errors[:end_at] << "The end date can't be before the start date. Pick a date after #{the_start}" record.errors[:end_at] << "The end date can't be before the start date. Pick a date after #{the_start}"
end end