This commit is contained in:
Patrick Nagurny
2019-06-17 14:37:10 -04:00
parent cf4c779628
commit ca871a8021
6 changed files with 143 additions and 39 deletions

View File

@@ -63,7 +63,7 @@ export class BalanceSheetReport {
}
this.form = fb.group({
date: [Util.getLocalDateString(this.date), Validators.required],
date: [Util.getLocalDateStringExcl(this.date), Validators.required],
priceSource: [this.priceSource, Validators.required]
});
}
@@ -133,7 +133,7 @@ export class BalanceSheetReport {
this.treeSubscription.unsubscribe();
//this.dataService.setLoading(true);
this.showOptionsForm = false;
this.date = Util.getDateFromLocalDateString(this.form.value.date);
this.date = Util.getDateFromLocalDateStringExcl(this.form.value.date);
this.priceSource = this.form.value.priceSource;
let reportData = this.configService.get('reportData');

View File

@@ -45,10 +45,9 @@ export class IncomeReport {
private configService: ConfigService,
private sessionService: SessionService) {
this.startDate = new Date();
this.startDate.setDate(1);
this.startDate.setHours(0, 0, 0, 0);
this.endDate = new Date(this.startDate);
this.endDate.setMonth(this.startDate.getMonth() + 1);
Util.setFirstOfMonth(this.startDate);
Util.setBeginOfDay(this.startDate);
this.endDate = Util.getOneMonthLater(this.startDate);
let reportData = this.configService.get('reportData');
@@ -65,7 +64,7 @@ export class IncomeReport {
this.form = fb.group({
startDate: [Util.getLocalDateString(this.startDate), Validators.required],
endDate: [Util.getLocalDateString(new Date(this.endDate.getTime() - 1)), Validators.required]
endDate: [Util.getLocalDateStringExcl(this.endDate), Validators.required]
});
}
@@ -92,8 +91,7 @@ export class IncomeReport {
//this.dataService.setLoading(true);
this.showDateForm = false;
this.startDate = Util.getDateFromLocalDateString(this.form.value.startDate);
this.endDate = Util.getDateFromLocalDateString(this.form.value.endDate);
this.endDate.setDate(this.endDate.getDate() + 1);
this.endDate = Util.getDateFromLocalDateStringExcl(this.form.value.endDate);
let reportData = this.configService.get('reportData');

View File

@@ -1,8 +1,15 @@
//import * as moment from 'moment-timezone/builds/moment-timezone-with-data-2012-2022.min';
import * as moment from 'moment-timezone';
const defaultTz = moment.tz.guess();
export class Util {
static getLocalDateString(input: Date) {
let year = input.getFullYear().toString();
let month = (input.getMonth() + 1).toString();
let date = input.getDate().toString();
static getLocalDateString(input: Date, tz: string = defaultTz) {
let m = moment(input).tz(tz);
let year = m.format('YYYY');
let month = m.format('MM');
let date = m.format('DD');
if(month.length < 2) {
month = '0' + month;
@@ -15,14 +22,112 @@ export class Util {
return year + '-' + month + '-' + date;
}
static getDateFromLocalDateString(input: string) {
let parts = input.split('-');
let date = new Date();
date.setHours(0, 0, 0, 0);
date.setFullYear(parseInt(parts[0]));
date.setMonth(parseInt(parts[1]) - 1, parseInt(parts[2]));
static getLocalDateStringExcl(input: Date, tz: string = defaultTz) {
let m = moment(input.getTime() - 1).tz(tz);
return date;
let year = m.format('YYYY');
let month = m.format('MM');
let date = m.format('DD');
if(month.length < 2) {
month = '0' + month;
}
if(date.length < 2) {
date = '0' + date;
}
return year + '-' + month + '-' + date;
}
static getDateFromLocalDateString(input: string, tz: string = defaultTz) {
let parts = input.split('-');
let m = moment().tz(tz);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
m.year(parseInt(parts[0]));
m.month(parseInt(parts[1]) - 1);
m.date(parseInt(parts[2]));
return m.toDate();
}
static getDateFromLocalDateStringExcl(input: string, tz: string = defaultTz) {
let parts = input.split('-');
let m = moment().tz(tz);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
m.year(parseInt(parts[0]));
m.month(parseInt(parts[1]) - 1);
m.date(parseInt(parts[2]) + 1);
return m.toDate();
}
static setFirstOfMonth(input: Date, tz: string = defaultTz) {
let m = moment(input).tz(tz);
m.date(1);
input.setTime(m.valueOf());
}
static setBeginOfDay(input: Date, tz: string = defaultTz) {
let m = moment(input).tz(tz);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
input.setTime(m.valueOf());
}
static setEndOfDay(input: Date, tz: string = defaultTz) {
let m = moment(input).tz(tz);
m.hours(23);
m.minutes(59);
m.seconds(59);
m.milliseconds(999);
input.setTime(m.valueOf());
}
static getOneMonthLater(input: Date, tz: string = defaultTz): Date {
let m = moment(input).tz(tz);
m.month(m.month() + 1);
return m.toDate();
}
static computeTransactionDate(formDate: Date, txDate: Date, tz: string = defaultTz): Date {
if(!formDate || !formDate.getTime()) {
return txDate;
}
let formMoment = moment(formDate).tz(tz);
let txMoment = moment(txDate).tz(tz);
// make the time be at the very end of the day
formMoment.hours(23);
formMoment.minutes(59);
formMoment.seconds(59);
formMoment.milliseconds(999);
let sameDay = formMoment.year() === txMoment.year() &&
formMoment.month() === txMoment.month() &&
formMoment.date() === txMoment.date();
if(!sameDay) {
return formMoment.toDate();
}
return txDate;
}
static newGuid() {

View File

@@ -624,7 +624,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
let date = item.tx.id ? item.tx.date : new Date();
let formDate = Util.getDateFromLocalDateString(item.form.value.date);
date = this.computeTransactionDate(formDate, date);
date = Util.computeTransactionDate(formDate, date);
let tx = new Transaction({
id: item.tx.id,
@@ -731,23 +731,6 @@ export class TxListPage implements OnInit, AfterViewChecked {
}
}
computeTransactionDate(formDate: Date, txDate: Date): Date {
if(formDate.getTime()) {
// make the time be at the very end of the day
formDate.setHours(23, 59, 59, 999);
}
let sameDay = formDate.getFullYear() === txDate.getFullYear() &&
formDate.getMonth() === txDate.getMonth() &&
formDate.getDate() === txDate.getDate();
if(formDate.getTime() && !sameDay) {
txDate = formDate;
}
return txDate;
}
deleteTransaction(item) {
this.modalService.open(this.confirmDeleteModal).result.then((result) => {
this.log.debug('delete');
@@ -846,7 +829,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
item.tx = new Transaction(
{
id: item.tx.id,
date: this.computeTransactionDate(formDate, new Date()),
date: Util.computeTransactionDate(formDate, new Date()),
description: tx.description,
splits: tx.splits
}