You've already forked openaccounting-web
forked from cybercinch/openaccounting-web
separate out date functions
This commit is contained in:
@@ -7,7 +7,7 @@ import { SessionService } from '../core/session.service';
|
|||||||
import { Transaction, Split } from '../shared/transaction';
|
import { Transaction, Split } from '../shared/transaction';
|
||||||
import { Org } from '../shared/org';
|
import { Org } from '../shared/org';
|
||||||
import { Account, AccountTree } from '../shared/account';
|
import { Account, AccountTree } from '../shared/account';
|
||||||
import { Util } from '../shared/util';
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { TxListPage } from '../transaction/list';
|
import { TxListPage } from '../transaction/list';
|
||||||
import { IncomeReport } from '../reports/income';
|
import { IncomeReport } from '../reports/income';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
@@ -51,7 +51,7 @@ export class DashboardPage implements OnInit {
|
|||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
this.log.debug('org', this.org);
|
this.log.debug('org', this.org);
|
||||||
|
|
||||||
let periodStart = Util.getPeriodStart(this.org.timezone);
|
let periodStart = DateUtil.getPeriodStart(this.org.timezone);
|
||||||
|
|
||||||
let tree$ = this.accountService.getAccountTreeWithPeriodBalance(periodStart);
|
let tree$ = this.accountService.getAccountTreeWithPeriodBalance(periodStart);
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { Org } from '../shared/org';
|
|||||||
import { Invite } from '../shared/invite';
|
import { Invite } from '../shared/invite';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-org',
|
selector: 'app-org',
|
||||||
@@ -42,8 +43,8 @@ export class OrgPage {
|
|||||||
private fb: FormBuilder
|
private fb: FormBuilder
|
||||||
) {
|
) {
|
||||||
|
|
||||||
this.timezones = Util.getTimezones();
|
this.timezones = DateUtil.getTimezones();
|
||||||
this.defaultTz = Util.getDefaultTimezone();
|
this.defaultTz = DateUtil.getDefaultTimezone();
|
||||||
|
|
||||||
console.log('defaultTz', this.defaultTz);
|
console.log('defaultTz', this.defaultTz);
|
||||||
this.invites = null;
|
this.invites = null;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
AbstractControl
|
AbstractControl
|
||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { PriceService } from '../core/price.service';
|
import { PriceService } from '../core/price.service';
|
||||||
import { Price } from '../shared/price';
|
import { Price } from '../shared/price';
|
||||||
import { SessionService } from '../core/session.service';
|
import { SessionService } from '../core/session.service';
|
||||||
@@ -36,7 +37,7 @@ export class PriceModal {
|
|||||||
private fb: FormBuilder
|
private fb: FormBuilder
|
||||||
) {
|
) {
|
||||||
this.org = this.sessionService.getOrg();
|
this.org = this.sessionService.getOrg();
|
||||||
let dateString = Util.getLocalDateString(new Date(), this.org.timezone);
|
let dateString = DateUtil.getLocalDateString(new Date(), this.org.timezone);
|
||||||
|
|
||||||
this.form = fb.group({
|
this.form = fb.group({
|
||||||
'id': [null],
|
'id': [null],
|
||||||
@@ -52,7 +53,7 @@ export class PriceModal {
|
|||||||
this.form.patchValue({
|
this.form.patchValue({
|
||||||
id: data.id,
|
id: data.id,
|
||||||
currency: data.currency,
|
currency: data.currency,
|
||||||
date: Util.getLocalDateString(data.date, this.org.timezone),
|
date: DateUtil.getLocalDateString(data.date, this.org.timezone),
|
||||||
price: data.price
|
price: data.price
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -61,11 +62,11 @@ export class PriceModal {
|
|||||||
this.error = null;
|
this.error = null;
|
||||||
|
|
||||||
let date = this.form.value.id ? this.originalDate : new Date();
|
let date = this.form.value.id ? this.originalDate : new Date();
|
||||||
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
||||||
|
|
||||||
if(formDate.getTime() && !Util.isSameDay(date, formDate, this.org.timezone)) {
|
if(formDate.getTime() && !DateUtil.isSameDay(date, formDate, this.org.timezone)) {
|
||||||
// make the time be at the very end of the day
|
// make the time be at the very end of the day
|
||||||
Util.setEndOfDay(formDate, this.org.timezone);
|
DateUtil.setEndOfDay(formDate, this.org.timezone);
|
||||||
date = formDate;
|
date = formDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import { PriceService } from '../core/price.service';
|
|||||||
import { Price } from '../shared/price';
|
import { Price } from '../shared/price';
|
||||||
import { Org } from '../shared/org';
|
import { Org } from '../shared/org';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Util } from '../shared/util';
|
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import 'rxjs/add/observable/forkJoin';
|
import 'rxjs/add/observable/forkJoin';
|
||||||
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { Transaction } from '../shared/transaction';
|
|||||||
import { Org } from '../shared/org';
|
import { Org } from '../shared/org';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
||||||
import { ReconcileModal } from './reconcile-modal';
|
import { ReconcileModal } from './reconcile-modal';
|
||||||
import { Reconciliation } from './reconciliation';
|
import { Reconciliation } from './reconciliation';
|
||||||
@@ -87,8 +88,8 @@ export class ReconcilePage {
|
|||||||
let value = this.newReconcile.getRawValue();
|
let value = this.newReconcile.getRawValue();
|
||||||
|
|
||||||
let rec = new Reconciliation();
|
let rec = new Reconciliation();
|
||||||
rec.startDate = Util.getDateFromLocalDateString(value.startDate, this.org.timezone);
|
rec.startDate = DateUtil.getDateFromLocalDateString(value.startDate, this.org.timezone);
|
||||||
rec.endDate = Util.getDateFromLocalDateString(value.endDate, this.org.timezone);
|
rec.endDate = DateUtil.getDateFromLocalDateString(value.endDate, this.org.timezone);
|
||||||
rec.startBalance = Math.round(parseFloat(value.startBalance) * Math.pow(10, this.account.precision));
|
rec.startBalance = Math.round(parseFloat(value.startBalance) * Math.pow(10, this.account.precision));
|
||||||
rec.endBalance = Math.round(parseFloat(value.endBalance) * Math.pow(10, this.account.precision));
|
rec.endBalance = Math.round(parseFloat(value.endBalance) * Math.pow(10, this.account.precision));
|
||||||
|
|
||||||
@@ -106,7 +107,7 @@ export class ReconcilePage {
|
|||||||
|
|
||||||
this.newReconcile.patchValue(
|
this.newReconcile.patchValue(
|
||||||
{
|
{
|
||||||
startDate: Util.getLocalDateString(rec.endDate, this.org.timezone),
|
startDate: DateUtil.getLocalDateString(rec.endDate, this.org.timezone),
|
||||||
startBalance: rec.endBalance / Math.pow(10, this.account.precision),
|
startBalance: rec.endBalance / Math.pow(10, this.account.precision),
|
||||||
endBalance: 0,
|
endBalance: 0,
|
||||||
endDate: ''
|
endDate: ''
|
||||||
@@ -203,7 +204,7 @@ export class ReconcilePage {
|
|||||||
|
|
||||||
if(!dates.length) {
|
if(!dates.length) {
|
||||||
if(firstStartDate) {
|
if(firstStartDate) {
|
||||||
this.newReconcile.patchValue({startDate: Util.getLocalDateString(firstStartDate, this.org.timezone)});
|
this.newReconcile.patchValue({startDate: DateUtil.getLocalDateString(firstStartDate, this.org.timezone)});
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -228,7 +229,7 @@ export class ReconcilePage {
|
|||||||
|
|
||||||
this.newReconcile.patchValue(
|
this.newReconcile.patchValue(
|
||||||
{
|
{
|
||||||
startDate: Util.getLocalDateString(lastRec.endDate, this.org.timezone),
|
startDate: DateUtil.getLocalDateString(lastRec.endDate, this.org.timezone),
|
||||||
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
|
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -272,7 +273,7 @@ export class ReconcilePage {
|
|||||||
if(lastRec) {
|
if(lastRec) {
|
||||||
this.newReconcile.patchValue(
|
this.newReconcile.patchValue(
|
||||||
{
|
{
|
||||||
startDate: Util.getLocalDateString(lastRec.endDate, this.org.timezone),
|
startDate: DateUtil.getLocalDateString(lastRec.endDate, this.org.timezone),
|
||||||
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
|
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import {
|
|||||||
ValidationErrors
|
ValidationErrors
|
||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Util } from '../shared/util';
|
import { DateUtil } from '../shared/dateutil';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-balancesheet',
|
selector: 'app-balancesheet',
|
||||||
@@ -65,7 +65,7 @@ export class BalanceSheetReport {
|
|||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
|
|
||||||
this.form = fb.group({
|
this.form = fb.group({
|
||||||
date: [Util.getLocalDateStringExcl(this.date, this.org.timezone), Validators.required],
|
date: [DateUtil.getLocalDateStringExcl(this.date, this.org.timezone), Validators.required],
|
||||||
priceSource: [this.priceSource, Validators.required]
|
priceSource: [this.priceSource, Validators.required]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ export class BalanceSheetReport {
|
|||||||
this.treeSubscription.unsubscribe();
|
this.treeSubscription.unsubscribe();
|
||||||
//this.dataService.setLoading(true);
|
//this.dataService.setLoading(true);
|
||||||
this.showOptionsForm = false;
|
this.showOptionsForm = false;
|
||||||
this.date = Util.getDateFromLocalDateStringExcl(this.form.value.date, this.org.timezone);
|
this.date = DateUtil.getDateFromLocalDateStringExcl(this.form.value.date, this.org.timezone);
|
||||||
this.priceSource = this.form.value.priceSource;
|
this.priceSource = this.form.value.priceSource;
|
||||||
|
|
||||||
let reportData = this.configService.get('reportData');
|
let reportData = this.configService.get('reportData');
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import {
|
|||||||
ValidationErrors
|
ValidationErrors
|
||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Util } from '../shared/util';
|
import { DateUtil } from '../shared/dateutil';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-income',
|
selector: 'app-income',
|
||||||
@@ -51,9 +51,9 @@ export class IncomeReport {
|
|||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
|
|
||||||
this.startDate = new Date();
|
this.startDate = new Date();
|
||||||
Util.setFirstOfMonth(this.startDate, this.org.timezone);
|
DateUtil.setFirstOfMonth(this.startDate, this.org.timezone);
|
||||||
Util.setBeginOfDay(this.startDate, this.org.timezone);
|
DateUtil.setBeginOfDay(this.startDate, this.org.timezone);
|
||||||
this.endDate = Util.getOneMonthLater(this.startDate, this.org.timezone);
|
this.endDate = DateUtil.getOneMonthLater(this.startDate, this.org.timezone);
|
||||||
|
|
||||||
let reportData = this.configService.get('reportData');
|
let reportData = this.configService.get('reportData');
|
||||||
|
|
||||||
@@ -69,8 +69,8 @@ export class IncomeReport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.form = this.fb.group({
|
this.form = this.fb.group({
|
||||||
startDate: [Util.getLocalDateString(this.startDate, this.org.timezone), Validators.required],
|
startDate: [DateUtil.getLocalDateString(this.startDate, this.org.timezone), Validators.required],
|
||||||
endDate: [Util.getLocalDateStringExcl(this.endDate, this.org.timezone), Validators.required]
|
endDate: [DateUtil.getLocalDateStringExcl(this.endDate, this.org.timezone), Validators.required]
|
||||||
});
|
});
|
||||||
|
|
||||||
this.treeSubscription = this.accountService.getAccountTreeWithPeriodBalance(this.startDate, this.endDate)
|
this.treeSubscription = this.accountService.getAccountTreeWithPeriodBalance(this.startDate, this.endDate)
|
||||||
@@ -91,8 +91,8 @@ export class IncomeReport {
|
|||||||
this.treeSubscription.unsubscribe();
|
this.treeSubscription.unsubscribe();
|
||||||
//this.dataService.setLoading(true);
|
//this.dataService.setLoading(true);
|
||||||
this.showDateForm = false;
|
this.showDateForm = false;
|
||||||
this.startDate = Util.getDateFromLocalDateString(this.form.value.startDate, this.org.timezone);
|
this.startDate = DateUtil.getDateFromLocalDateString(this.form.value.startDate, this.org.timezone);
|
||||||
this.endDate = Util.getDateFromLocalDateStringExcl(this.form.value.endDate, this.org.timezone);
|
this.endDate = DateUtil.getDateFromLocalDateStringExcl(this.form.value.endDate, this.org.timezone);
|
||||||
|
|
||||||
let reportData = this.configService.get('reportData');
|
let reportData = this.configService.get('reportData');
|
||||||
|
|
||||||
|
|||||||
175
src/app/shared/dateutil.ts
Normal file
175
src/app/shared/dateutil.ts
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
//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 DateUtil {
|
||||||
|
static getLocalDateString(input: Date, tz: string) {
|
||||||
|
let m = moment(input).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
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 getLocalDateStringExcl(input: Date, tz: string) {
|
||||||
|
let m = moment(input.getTime() - 1).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
let parts = input.split('-');
|
||||||
|
|
||||||
|
let m = moment().tz(tz || defaultTz);
|
||||||
|
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) {
|
||||||
|
let parts = input.split('-');
|
||||||
|
|
||||||
|
let m = moment().tz(tz || defaultTz);
|
||||||
|
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) {
|
||||||
|
let m = moment(input).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
m.date(1);
|
||||||
|
input.setTime(m.valueOf());
|
||||||
|
}
|
||||||
|
|
||||||
|
static setBeginOfDay(input: Date, tz: string) {
|
||||||
|
let m = moment(input).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
m.hours(0);
|
||||||
|
m.minutes(0);
|
||||||
|
m.seconds(0);
|
||||||
|
m.milliseconds(0);
|
||||||
|
input.setTime(m.valueOf());
|
||||||
|
}
|
||||||
|
|
||||||
|
static setEndOfDay(input: Date, tz: string) {
|
||||||
|
let m = moment(input).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
m.hours(23);
|
||||||
|
m.minutes(59);
|
||||||
|
m.seconds(59);
|
||||||
|
m.milliseconds(999);
|
||||||
|
input.setTime(m.valueOf());
|
||||||
|
}
|
||||||
|
|
||||||
|
static getOneMonthLater(input: Date, tz: string): Date {
|
||||||
|
let m = moment(input).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
m.month(m.month() + 1);
|
||||||
|
|
||||||
|
return m.toDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
static computeTransactionDate(formDate: Date, txDate: Date, tz: string): Date {
|
||||||
|
if(!formDate || !formDate.getTime()) {
|
||||||
|
return txDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
let formMoment = moment(formDate).tz(tz || defaultTz);
|
||||||
|
let txMoment = moment(txDate).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
// 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 txDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(formDate < txDate) {
|
||||||
|
// make time end of day for past dates
|
||||||
|
formMoment.hours(23);
|
||||||
|
formMoment.minutes(59);
|
||||||
|
formMoment.seconds(59);
|
||||||
|
formMoment.milliseconds(999);
|
||||||
|
} else {
|
||||||
|
// make time beginning of day for future dates
|
||||||
|
formMoment.hours(0);
|
||||||
|
formMoment.minutes(0);
|
||||||
|
formMoment.seconds(0);
|
||||||
|
formMoment.milliseconds(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return formMoment.toDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
static isSameDay(date1: Date, date2: Date, tz: string) {
|
||||||
|
let m1 = moment(date1).tz(tz || defaultTz);
|
||||||
|
let m2 = moment(date2).tz(tz || defaultTz);
|
||||||
|
|
||||||
|
return m1.year() === m2.year() &&
|
||||||
|
m1.month() === m2.month() &&
|
||||||
|
m1.date() === m2.date();
|
||||||
|
}
|
||||||
|
|
||||||
|
static getPeriodStart(tz: string): Date {
|
||||||
|
let m = moment().tz(tz || defaultTz);
|
||||||
|
m.date(1);
|
||||||
|
m.hours(0);
|
||||||
|
m.minutes(0);
|
||||||
|
m.seconds(0);
|
||||||
|
m.milliseconds(0);
|
||||||
|
|
||||||
|
return m.toDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
static getTimezones(): string[] {
|
||||||
|
let timezones = [''];
|
||||||
|
return timezones.concat(moment.tz.names());
|
||||||
|
}
|
||||||
|
|
||||||
|
static getDefaultTimezone(): string {
|
||||||
|
return defaultTz;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,178 +1,4 @@
|
|||||||
//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 {
|
export class Util {
|
||||||
static getLocalDateString(input: Date, tz: string) {
|
|
||||||
let m = moment(input).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
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 getLocalDateStringExcl(input: Date, tz: string) {
|
|
||||||
let m = moment(input.getTime() - 1).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
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) {
|
|
||||||
let parts = input.split('-');
|
|
||||||
|
|
||||||
let m = moment().tz(tz || defaultTz);
|
|
||||||
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) {
|
|
||||||
let parts = input.split('-');
|
|
||||||
|
|
||||||
let m = moment().tz(tz || defaultTz);
|
|
||||||
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) {
|
|
||||||
let m = moment(input).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
m.date(1);
|
|
||||||
input.setTime(m.valueOf());
|
|
||||||
}
|
|
||||||
|
|
||||||
static setBeginOfDay(input: Date, tz: string) {
|
|
||||||
let m = moment(input).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
m.hours(0);
|
|
||||||
m.minutes(0);
|
|
||||||
m.seconds(0);
|
|
||||||
m.milliseconds(0);
|
|
||||||
input.setTime(m.valueOf());
|
|
||||||
}
|
|
||||||
|
|
||||||
static setEndOfDay(input: Date, tz: string) {
|
|
||||||
let m = moment(input).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
m.hours(23);
|
|
||||||
m.minutes(59);
|
|
||||||
m.seconds(59);
|
|
||||||
m.milliseconds(999);
|
|
||||||
input.setTime(m.valueOf());
|
|
||||||
}
|
|
||||||
|
|
||||||
static getOneMonthLater(input: Date, tz: string): Date {
|
|
||||||
let m = moment(input).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
m.month(m.month() + 1);
|
|
||||||
|
|
||||||
return m.toDate();
|
|
||||||
}
|
|
||||||
|
|
||||||
static computeTransactionDate(formDate: Date, txDate: Date, tz: string): Date {
|
|
||||||
if(!formDate || !formDate.getTime()) {
|
|
||||||
return txDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
let formMoment = moment(formDate).tz(tz || defaultTz);
|
|
||||||
let txMoment = moment(txDate).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
// 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 txDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(formDate < txDate) {
|
|
||||||
// make time end of day for past dates
|
|
||||||
formMoment.hours(23);
|
|
||||||
formMoment.minutes(59);
|
|
||||||
formMoment.seconds(59);
|
|
||||||
formMoment.milliseconds(999);
|
|
||||||
} else {
|
|
||||||
// make time beginning of day for future dates
|
|
||||||
formMoment.hours(0);
|
|
||||||
formMoment.minutes(0);
|
|
||||||
formMoment.seconds(0);
|
|
||||||
formMoment.milliseconds(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return formMoment.toDate();
|
|
||||||
}
|
|
||||||
|
|
||||||
static isSameDay(date1: Date, date2: Date, tz: string) {
|
|
||||||
let m1 = moment(date1).tz(tz || defaultTz);
|
|
||||||
let m2 = moment(date2).tz(tz || defaultTz);
|
|
||||||
|
|
||||||
return m1.year() === m2.year() &&
|
|
||||||
m1.month() === m2.month() &&
|
|
||||||
m1.date() === m2.date();
|
|
||||||
}
|
|
||||||
|
|
||||||
static getPeriodStart(tz: string): Date {
|
|
||||||
let m = moment().tz(tz || defaultTz);
|
|
||||||
m.date(1);
|
|
||||||
m.hours(0);
|
|
||||||
m.minutes(0);
|
|
||||||
m.seconds(0);
|
|
||||||
m.milliseconds(0);
|
|
||||||
|
|
||||||
return m.toDate();
|
|
||||||
}
|
|
||||||
|
|
||||||
static getTimezones(): string[] {
|
|
||||||
let timezones = [''];
|
|
||||||
return timezones.concat(moment.tz.names());
|
|
||||||
}
|
|
||||||
|
|
||||||
static getDefaultTimezone(): string {
|
|
||||||
return defaultTz;
|
|
||||||
}
|
|
||||||
|
|
||||||
static newGuid() {
|
static newGuid() {
|
||||||
let arr = new Uint8Array(16);
|
let arr = new Uint8Array(16);
|
||||||
window.crypto.getRandomValues(arr);
|
window.crypto.getRandomValues(arr);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
AbstractControl
|
AbstractControl
|
||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { OrgService } from '../core/org.service';
|
import { OrgService } from '../core/org.service';
|
||||||
import { TransactionService } from '../core/transaction.service';
|
import { TransactionService } from '../core/transaction.service';
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ export class AdvancedEdit {
|
|||||||
|
|
||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
|
|
||||||
let dateString = Util.getLocalDateString(item.tx.date, this.org.timezone);
|
let dateString = DateUtil.getLocalDateString(item.tx.date, this.org.timezone);
|
||||||
|
|
||||||
this.form = new FormGroup({
|
this.form = new FormGroup({
|
||||||
date: new FormControl(dateString),
|
date: new FormControl(dateString),
|
||||||
@@ -110,9 +111,9 @@ export class AdvancedEdit {
|
|||||||
this.error = null;
|
this.error = null;
|
||||||
|
|
||||||
let date = this.item.tx.id ? this.item.tx.date : new Date();
|
let date = this.item.tx.id ? this.item.tx.date : new Date();
|
||||||
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
||||||
|
|
||||||
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
|
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
|
||||||
|
|
||||||
let tx = new Transaction({
|
let tx = new Transaction({
|
||||||
id: this.item.tx.id,
|
id: this.item.tx.id,
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import 'rxjs/add/operator/mergeMap';
|
import 'rxjs/add/operator/mergeMap';
|
||||||
import { AdvancedEdit } from './advancededit';
|
import { AdvancedEdit } from './advancededit';
|
||||||
@@ -76,8 +77,6 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
this.accountId = this.route.snapshot.paramMap.get('id'); //+this.route.snapshot.paramMap.get('id');
|
this.accountId = this.route.snapshot.paramMap.get('id'); //+this.route.snapshot.paramMap.get('id');
|
||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
|
|
||||||
console.log(this.org);
|
|
||||||
|
|
||||||
this.accountService.getAccountTree().subscribe(tree => {
|
this.accountService.getAccountTree().subscribe(tree => {
|
||||||
this.account = tree.accountMap[this.accountId];
|
this.account = tree.accountMap[this.accountId];
|
||||||
this.selectAccounts = tree.getFlattenedAccounts().filter(account => {
|
this.selectAccounts = tree.getFlattenedAccounts().filter(account => {
|
||||||
@@ -95,7 +94,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
splits: []
|
splits: []
|
||||||
});
|
});
|
||||||
|
|
||||||
Util.setEndOfDay(newTx.date, this.org.timezone);
|
DateUtil.setEndOfDay(newTx.date, this.org.timezone);
|
||||||
|
|
||||||
newTx.splits.push(new Split({
|
newTx.splits.push(new Split({
|
||||||
accountId: this.account.id
|
accountId: this.account.id
|
||||||
@@ -410,7 +409,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
|
|
||||||
item.editing = true;
|
item.editing = true;
|
||||||
|
|
||||||
let dateString = Util.getLocalDateString(item.tx.date, this.org.timezone);
|
let dateString = DateUtil.getLocalDateString(item.tx.date, this.org.timezone);
|
||||||
|
|
||||||
this.log.debug(item);
|
this.log.debug(item);
|
||||||
let debit = this.getDebit(item);
|
let debit = this.getDebit(item);
|
||||||
@@ -629,9 +628,9 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let date = item.tx.id ? item.tx.date : new Date();
|
let date = item.tx.id ? item.tx.date : new Date();
|
||||||
let formDate = Util.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
|
let formDate = DateUtil.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
|
||||||
|
|
||||||
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
|
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
|
||||||
|
|
||||||
let tx = new Transaction({
|
let tx = new Transaction({
|
||||||
id: item.tx.id,
|
id: item.tx.id,
|
||||||
@@ -714,7 +713,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
splits: []
|
splits: []
|
||||||
});
|
});
|
||||||
|
|
||||||
Util.setEndOfDay(newTx.date, this.org.timezone);
|
DateUtil.setEndOfDay(newTx.date, this.org.timezone);
|
||||||
|
|
||||||
newTx.splits.push(new Split({
|
newTx.splits.push(new Split({
|
||||||
accountId: this.account.id
|
accountId: this.account.id
|
||||||
@@ -832,11 +831,11 @@ export class TxListPage implements OnInit, AfterViewChecked {
|
|||||||
autocomplete(item: TxItem, tx: Transaction) {
|
autocomplete(item: TxItem, tx: Transaction) {
|
||||||
this.log.debug('chose tx', tx);
|
this.log.debug('chose tx', tx);
|
||||||
|
|
||||||
let formDate = Util.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
|
let formDate = DateUtil.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
|
||||||
item.tx = new Transaction(
|
item.tx = new Transaction(
|
||||||
{
|
{
|
||||||
id: item.tx.id,
|
id: item.tx.id,
|
||||||
date: Util.computeTransactionDate(formDate, new Date(), this.org.timezone),
|
date: DateUtil.computeTransactionDate(formDate, new Date(), this.org.timezone),
|
||||||
description: tx.description,
|
description: tx.description,
|
||||||
splits: tx.splits
|
splits: tx.splits
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { TransactionService } from '../core/transaction.service';
|
|||||||
import { OrgService } from '../core/org.service';
|
import { OrgService } from '../core/org.service';
|
||||||
import { Account, AccountApi, AccountTree } from '../shared/account';
|
import { Account, AccountApi, AccountTree } from '../shared/account';
|
||||||
import { Util } from '../shared/util';
|
import { Util } from '../shared/util';
|
||||||
|
import { DateUtil } from '../shared/dateutil';
|
||||||
import { AppError } from '../shared/error';
|
import { AppError } from '../shared/error';
|
||||||
import { Transaction, Split } from '../shared/transaction';
|
import { Transaction, Split } from '../shared/transaction';
|
||||||
import { Logger } from '../core/logger';
|
import { Logger } from '../core/logger';
|
||||||
@@ -62,7 +63,7 @@ export class NewTransactionPage {
|
|||||||
this.numAccountsShown = 3;
|
this.numAccountsShown = 3;
|
||||||
this.org = this.orgService.getCurrentOrg();
|
this.org = this.orgService.getCurrentOrg();
|
||||||
|
|
||||||
let dateString = Util.getLocalDateString(new Date(), this.org.timezone);
|
let dateString = DateUtil.getLocalDateString(new Date(), this.org.timezone);
|
||||||
this.form = this.fb.group({
|
this.form = this.fb.group({
|
||||||
type: ['', Validators.required],
|
type: ['', Validators.required],
|
||||||
firstAccountPrimary: [null, Validators.required],
|
firstAccountPrimary: [null, Validators.required],
|
||||||
@@ -226,9 +227,9 @@ export class NewTransactionPage {
|
|||||||
this.error = null;
|
this.error = null;
|
||||||
|
|
||||||
let date = new Date();
|
let date = new Date();
|
||||||
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
|
||||||
|
|
||||||
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
|
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
|
||||||
|
|
||||||
let tx = new Transaction({
|
let tx = new Transaction({
|
||||||
id: Util.newGuid(),
|
id: Util.newGuid(),
|
||||||
|
|||||||
Reference in New Issue
Block a user