diff --git a/src/app/reconcile/reconcile-modal.ts b/src/app/reconcile/reconcile-modal.ts index d35d547..1b21dcd 100644 --- a/src/app/reconcile/reconcile-modal.ts +++ b/src/app/reconcile/reconcile-modal.ts @@ -220,7 +220,7 @@ export class ReconcileModal { this.sessionService.setLoading(false); }, () => { this.sessionService.setLoading(false); - this.activeModal.close(); + this.activeModal.close(txs); }); } } \ No newline at end of file diff --git a/src/app/reconcile/reconcile.html b/src/app/reconcile/reconcile.html index 569e2d9..72f47eb 100644 --- a/src/app/reconcile/reconcile.html +++ b/src/app/reconcile/reconcile.html @@ -52,11 +52,27 @@

Past Reconciliations

-
+
Period: {{rec.startDate | date:"M/d/y"}} - {{rec.endDate | date:"M/d/y"}}
Beginning Balance: {{rec.startBalance | currencyFormat:account.precision:account.currency}}
Ending Balance: {{rec.endBalance | currencyFormat:account.precision:account.currency}}
+ Delete
+ + + + + diff --git a/src/app/reconcile/reconcile.ts b/src/app/reconcile/reconcile.ts index 6100c37..c063262 100644 --- a/src/app/reconcile/reconcile.ts +++ b/src/app/reconcile/reconcile.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, ViewChild, ElementRef } from '@angular/core'; import { Logger } from '../core/logger'; import { Router } from '@angular/router'; import { @@ -19,6 +19,10 @@ import { Util } from '../shared/util'; import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap'; import { ReconcileModal } from './reconcile-modal'; import { Reconciliation } from './reconciliation'; +import { SessionService } from '../core/session.service'; +import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/observable/from'; +import 'rxjs/add/operator/mergeMap'; @Component({ selector: 'app-reconcile', @@ -34,6 +38,7 @@ export class ReconcilePage { public unreconciledTxs: Transaction[]; public error: AppError; private accountTree: AccountTree; + @ViewChild('confirmDeleteModal') confirmDeleteModal: ElementRef; constructor( private router: Router, @@ -42,7 +47,8 @@ export class ReconcilePage { private orgService: OrgService, private txService: TransactionService, private fb: FormBuilder, - private modalService: NgbModal) { + private modalService: NgbModal, + private sessionService: SessionService) { let org = this.orgService.getCurrentOrg(); this.accountForm = fb.group({ @@ -90,8 +96,10 @@ export class ReconcilePage { modal.componentInstance.setData(this.account, rec, this.unreconciledTxs); - modal.result.then((result) => { + modal.result.then((txs) => { this.log.debug('reconcile modal save'); + rec.txs = txs; + this.pastReconciliations.unshift(rec); this.newReconcile.patchValue( @@ -170,6 +178,8 @@ export class ReconcilePage { let r = reconcileMap[endDate.getTime()]; + r.txs.push(tx); + if(this.account.debitBalance) { r.net += split.amount; } else { @@ -223,4 +233,63 @@ export class ReconcilePage { }); } + delete() { + this.modalService.open(this.confirmDeleteModal).result.then((result) => { + this.sessionService.setLoading(true); + + let rec = this.pastReconciliations[0]; + console.log(rec); + + Observable.from(rec.txs).mergeMap(tx => { + let oldId = tx.id; + tx.id = Util.newGuid(); + + let data = tx.getData(); + console.log(data); + + let newSplits = {}; + + for(let splitId in data.reconciledSplits) { + if(tx.splits[splitId].accountId !== this.account.id) { + newSplits[splitId] = tx.splits[splitId]; + } + } + + data.reconciledSplits = newSplits; + + tx.setData(data); + + return this.txService.putTransaction(oldId, tx); + }, 8).subscribe(tx => { + this.log.debug('Saved tx ' + tx.id); + }, err => { + this.error = err; + this.sessionService.setLoading(false); + }, () => { + this.pastReconciliations.shift(); + let lastRec = this.pastReconciliations[0]; + + if(lastRec) { + this.newReconcile.patchValue( + { + startDate: Util.getLocalDateString(lastRec.endDate), + startBalance: lastRec.endBalance / Math.pow(10, this.account.precision) + } + ); + } else { + this.newReconcile.patchValue( + { + startDate: null, + startBalance: 0 + } + ); + } + + this.sessionService.setLoading(false); + }); + }, (reason) => { + this.log.debug('cancel delete'); + }); + } + } \ No newline at end of file diff --git a/src/app/reconcile/reconciliation.ts b/src/app/reconcile/reconciliation.ts index d924610..6f8af7c 100644 --- a/src/app/reconcile/reconciliation.ts +++ b/src/app/reconcile/reconciliation.ts @@ -1,7 +1,10 @@ +import { Transaction } from '../shared/transaction'; + export class Reconciliation { startDate: Date; startBalance: number; endDate: Date; endBalance: number; net: number; + txs: Transaction[] = []; } \ No newline at end of file