Skip to content

Commit

Permalink
Merge pull request #843 from frappe/reports_db_patch_fix
Browse files Browse the repository at this point in the history
Reports and Ledger DateTime Bug Fix
  • Loading branch information
Isaac-GC authored Feb 12, 2024
2 parents 6440527 + b45214a commit bf9b127
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion backend/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default [
},
{
name: 'fixLedgerDateTime',
version: '0.21.1',
version: '0.21.2',
patch: fixLedgerDateTime,
},
] as Patch[];
48 changes: 27 additions & 21 deletions backend/patches/v0_21_0/fixLedgerDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ import { DatabaseManager } from '../../database/manager';

/* eslint-disable */
async function execute(dm: DatabaseManager) {
await dm.db!.knex!('AccountingLedgerEntry')
.select('name', 'date')
.then((trx: Array<{name: string; date: Date;}> ) => {
trx.forEach(async entry => {
const entryDate = new Date(entry['date']);
const timeZoneOffset = entryDate.getTimezoneOffset();
const offsetMinutes = timeZoneOffset % 60;
const offsetHours = (timeZoneOffset - offsetMinutes) / 60;

let daysToAdd = 0; // If behind or at GMT/Zulu time, don't need to add a day
if (timeZoneOffset < 0) {
// If ahead of GMT/Zulu time, need to advance a day forward first
daysToAdd = 1;
}
const sourceTables = [
"PurchaseInvoice",
"SalesInvoice",
"JournalEntry",
"Payment",
"StockMovement",
"StockTransfer"
];

entryDate.setDate(entryDate.getDate() + daysToAdd);
entryDate.setHours(0 - offsetHours);
entryDate.setMinutes(0 - offsetMinutes);
entryDate.setSeconds(0);
entryDate.setMilliseconds(0);
await dm.db!.knex!('AccountingLedgerEntry')
.select('name', 'date', 'referenceName')
.then((trx: Array<{name: string; date: Date; referenceName: string;}> ) => {
trx.forEach(async entry => {

await dm.db!.knex!('AccountingLedgerEntry')
.where({ name: entry['name'] })
.update({ date: entryDate.toISOString() });
sourceTables.forEach(async table => {
await dm.db!.knex!
.select('name','date')
.from(table)
.where({ name: entry['referenceName'] })
.then(async (resp: Array<{name: string; date: Date;}>) => {
if (resp.length !== 0) {

const dateTimeValue = new Date(resp[0]['date']);
await dm.db!.knex!('AccountingLedgerEntry')
.where({ name: entry['name'] })
.update({ date: dateTimeValue.toISOString() });
}
})
});
});
});
}
Expand Down
7 changes: 2 additions & 5 deletions build/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as vite from 'vite';
import { getMainProcessCommonConfig } from './helpers.mjs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import frappeBooksConfig from '../../electron-builder-config.mjs';

const dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(dirname, '..', '..');
Expand Down Expand Up @@ -154,11 +155,7 @@ async function packageApp() {
}

let buildOptions = {
config: {
directories: { output: packageDirPath, app: buildDirPath },
files: ['**'],
extends: null,
},
config: frappeBooksConfig,
...builderArgs,
};

Expand Down
22 changes: 18 additions & 4 deletions electron-builder.ts → electron-builder-config.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { Configuration } from 'electron-builder';
// App is tagged with a .mjs extension to allow
import path from 'path';
import { fileURLToPath } from 'url';

/**
* electron-builder doesn't look for the APPLE_TEAM_ID environment variable for some reason.
* This workaround allows an environment variable to be added to the electron-builder.yml config
* collection. See: https://github.com/electron-userland/electron-builder/issues/7812
*/

const config: Configuration = {
const dirname = path.dirname(fileURLToPath(import.meta.url));
// const root = path.join(dirname, '..', '..');
const root = dirname; // redundant, but is meant to keep with the previous line
const buildDirPath = path.join(root, 'dist_electron', 'build');
const packageDirPath = path.join(root, 'dist_electron', 'bundled');

const frappeBooksConfig = {
productName: 'Frappe Books',
appId: 'io.frappe.books',
asarUnpack: '**/*.node',
Expand All @@ -15,6 +23,12 @@ const config: Configuration = {
{ from: 'translations', to: '../translations' },
{ from: 'templates', to: '../templates' },
],
files: '**',
extends: null,
directories: {
output: packageDirPath,
app: buildDirPath,
},
mac: {
type: 'distribution',
category: 'public.app-category.finance',
Expand All @@ -34,7 +48,7 @@ const config: Configuration = {
signDlls: true,
icon: 'build/icon.ico',
publish: ['github'],
target: ['portable', 'nsis'],
target: ['nsis', 'portable'],
},
nsis: {
oneClick: false,
Expand All @@ -52,4 +66,4 @@ const config: Configuration = {
},
};

export default config;
export default frappeBooksConfig;
34 changes: 16 additions & 18 deletions models/Transactional/LedgerPosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ export class LedgerPosting {
this._validateIsEqual();
}

timezoneDateTimeAdjuster(setDate: string | Date) {
const dateTimeValue = new Date(setDate);

const dtFixedValue = dateTimeValue;
const dtMinutes = dtFixedValue.getTimezoneOffset() % 60;
const dtHours = (dtFixedValue.getTimezoneOffset() - dtMinutes) / 60;
// Forcing the time to always be set to 00:00.000 for locale time
dtFixedValue.setHours(0 - dtHours);
dtFixedValue.setMinutes(0 - dtMinutes);
dtFixedValue.setSeconds(0);
dtFixedValue.setMilliseconds(0);

return dtFixedValue;
}

async makeRoundOffEntry() {
const { debit, credit } = this._getTotalDebitAndCredit();
const difference = debit.sub(credit);
Expand Down Expand Up @@ -90,31 +105,14 @@ export class LedgerPosting {
return map[account];
}

// Timezone inconsistency fix (very ugly code for now)
const entryDateTime = this.refDoc.date as string | Date;
let dateTimeValue: Date;
if (typeof entryDateTime === 'string' || entryDateTime instanceof String) {
dateTimeValue = new Date(entryDateTime);
} else {
dateTimeValue = entryDateTime;
}
const dtFixedValue = dateTimeValue;
const dtMinutes = dtFixedValue.getTimezoneOffset() % 60;
const dtHours = (dtFixedValue.getTimezoneOffset() - dtMinutes) / 60;
// Forcing the time to always be set to 00:00.000 for locale time
dtFixedValue.setHours(0 - dtHours);
dtFixedValue.setMinutes(0 - dtMinutes);
dtFixedValue.setSeconds(0);
dtFixedValue.setMilliseconds(0);

// end ugly timezone fix code

const ledgerEntry = this.fyo.doc.getNewDoc(
ModelNameEnum.AccountingLedgerEntry,
{
account: account,
party: (this.refDoc.party as string) ?? '',
date: dtFixedValue,
date: this.timezoneDateTimeAdjuster(this.refDoc.date as string | Date),
referenceType: this.refDoc.schemaName,
referenceName: this.refDoc.name!,
reverted: this.reverted,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frappe-books",
"version": "0.21.1",
"version": "0.21.2",
"description": "Simple book-keeping app for everyone",
"author": {
"name": "Frappe Technologies Pvt. Ltd.",
Expand Down

0 comments on commit bf9b127

Please sign in to comment.