2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

fix: Undo currency formatting / parsing

This commit is contained in:
Faris Ansari 2019-10-30 02:23:19 +05:30
parent 36db289c6e
commit 15e4dc6314
16 changed files with 57 additions and 152 deletions

View File

@ -11,14 +11,12 @@ module.exports = class LedgerPosting {
async debit(account, amount, referenceType, referenceName) {
const entry = this.getEntry(account, referenceType, referenceName);
amount = frappe.parseNumber(amount);
entry.debit += amount;
await this.setAccountBalanceChange(account, 'debit', amount);
}
async credit(account, amount, referenceType, referenceName) {
const entry = this.getEntry(account, referenceType, referenceName);
amount = frappe.parseNumber(amount);
entry.credit += amount;
await this.setAccountBalanceChange(account, 'credit', amount);
}

View File

@ -73,7 +73,7 @@ module.exports = {
label: 'Balance',
fieldtype: 'Currency',
default: '0',
disabled: true
readOnly: 1
},
{
fieldname: 'isGroup',

View File

@ -14,8 +14,7 @@ module.exports = {
label: 'Company Name',
fieldname: 'companyName',
fieldtype: 'Data',
required: 1,
disabled: 0
required: 1
},
{
@ -39,16 +38,6 @@ module.exports = {
required: 0
},
{
fieldname: 'numberFormat',
fieldtype: 'Data'
},
{
fieldname: 'symbol',
fieldtype: 'Data'
},
{
fieldname: 'fullname',
label: 'Name',

View File

@ -43,7 +43,7 @@ module.exports = {
fieldtype: 'Code',
formula: doc => doc.getJson(),
required: 1,
disabled: 1,
readOnly: 1,
rows: 15
}
],

View File

@ -92,10 +92,8 @@ module.exports = {
label: 'Amount',
fieldtype: 'Currency',
required: 1,
disabled: true,
formula: doc => {
return frappe.format(doc.getSum('for', 'amount'), 'Currency');
}
readOnly: 1,
formula: doc => doc.getSum('for', 'amount')
},
{
fieldname: 'writeoff',

View File

@ -7,9 +7,8 @@ module.exports = class PaymentServer extends BaseDocument {
if (changed === 'for') {
this.amount = 0;
for (let paymentReference of this.for) {
this.amount += frappe.parseNumber(paymentReference.amount);
this.amount += paymentReference.amount;
}
this.amount = frappe.format(this.amount, 'Currency');
}
}
@ -23,7 +22,7 @@ module.exports = class PaymentServer extends BaseDocument {
}
async beforeSubmit() {
if (this.for.length > 0)
if (this.for.length > 0) {
for (let row of this.for) {
if (['SalesInvoice', 'PurchaseInvoice'].includes(row.referenceType)) {
let { outstandingAmount, grandTotal } = await frappe.getDoc(
@ -33,41 +32,36 @@ module.exports = class PaymentServer extends BaseDocument {
if (outstandingAmount === null) {
outstandingAmount = grandTotal;
}
if (
0 >= frappe.parseNumber(this.amount) ||
frappe.parseNumber(this.amount) >
frappe.parseNumber(outstandingAmount)
) {
frappe.call({
method: 'show-dialog',
args: {
title: 'Invalid Payment Entry',
message: `Payment amount (${
this.amount
}) should be greater than 0 and less than Outstanding amount (${outstandingAmount})`
}
});
throw new Error();
if (this.amount <= 0 || this.amount > outstandingAmount) {
// frappe.call({
// method: 'show-dialog',
// args: {
// title: 'Invalid Payment Entry',
// message: `Payment amount (${this.amount}) should be greater than 0 and less than Outstanding amount (${outstandingAmount})`
// }
// });
throw new Error(
`Payment amount (${this.amount}) should be greater than 0 and less than Outstanding amount (${outstandingAmount})`
);
} else {
await frappe.db.setValue(
row.referenceType,
row.referenceName,
'outstandingAmount',
frappe.parseNumber(outstandingAmount) -
frappe.parseNumber(this.amount)
outstandingAmount - this.amount
);
}
}
}
else {
frappe.call({
method: 'show-dialog',
args: {
title: 'Invalid Payment Entry',
message: `No reference for the payment.`
}
});
throw new Error();
} else {
// frappe.call({
// method: 'show-dialog',
// args: {
// title: 'Invalid Payment Entry',
// message: `No reference for the payment.`
// }
// });
throw new Error(`No reference for the payment.`);
}
}

View File

@ -50,7 +50,7 @@ module.exports = {
},
{
fieldname: 'currency',
label: 'Customer Currency',
label: 'Supplier Currency',
fieldtype: 'Link',
target: 'Currency',
hidden: 1,
@ -60,7 +60,7 @@ module.exports = {
fieldname: 'exchangeRate',
label: 'Exchange Rate',
fieldtype: 'Float',
placeholder: '1 USD = [?] INR',
description: '1 USD = [?] INR',
hidden: doc => !doc.isForeignTransaction()
},
{
@ -75,7 +75,6 @@ module.exports = {
label: 'Net Total (INR)',
fieldtype: 'Currency',
formula: async doc => await doc.getBaseNetTotal(),
disabled: true,
readOnly: 1
},
{
@ -83,9 +82,7 @@ module.exports = {
label: 'Net Total (USD)',
fieldtype: 'Currency',
hidden: doc => !doc.isForeignTransaction(),
formula: async doc =>
await doc.formatIntoCustomerCurrency(doc.getSum('items', 'amount')),
disabled: true,
formula: doc => doc.getSum('items', 'amount'),
readOnly: 1
},
{
@ -101,7 +98,7 @@ module.exports = {
<div class='row' v-for='row in value'>
<div class='col-6'>{{ row.account }} ({{row.rate}}%)</div>
<div class='col-6 text-right'>
{{ row.amount }}
{{ frappe.format(row.amount, 'Currency') }}
</div>
</div>
</div>
@ -113,7 +110,6 @@ module.exports = {
label: 'Grand Total (INR)',
fieldtype: 'Currency',
formula: async doc => await doc.getBaseGrandTotal(),
disabled: true,
readOnly: 1
},
{
@ -122,7 +118,6 @@ module.exports = {
fieldtype: 'Currency',
hidden: doc => !doc.isForeignTransaction(),
formula: async doc => await doc.getGrandTotal(),
disabled: true,
readOnly: 1
},
{

View File

@ -8,19 +8,13 @@ module.exports = class PurchaseInvoiceServer extends PurchaseInvoice {
await entries.credit(this.account, this.baseGrandTotal);
for (let item of this.items) {
const baseItemAmount = frappe.format(
frappe.parseNumber(item.amount) * this.exchangeRate,
'Currency'
);
const baseItemAmount = item.amount * this.exchangeRate;
await entries.debit(item.account, baseItemAmount);
}
if (this.taxes) {
for (let tax of this.taxes) {
const baseTaxAmount = frappe.format(
frappe.parseNumber(tax.amount) * this.exchangeRate,
'Currency'
);
const baseTaxAmount = tax.amount * this.exchangeRate;
await entries.debit(tax.account, baseTaxAmount);
}
}

View File

@ -65,12 +65,7 @@ module.exports = {
label: 'Amount',
fieldtype: 'Currency',
readOnly: 1,
disabled: true,
formula: async (row, doc) => {
return await doc.formatIntoCustomerCurrency(
row.quantity * frappe.parseNumber(row.rate)
);
}
formula: (row, doc) => row.quantity * row.rate
},
{
fieldname: 'taxAmount',

View File

@ -71,7 +71,6 @@ module.exports = {
label: 'Net Total (INR)',
fieldtype: 'Currency',
formula: async doc => await doc.getBaseNetTotal(),
disabled: true,
readOnly: 1
},
{
@ -79,9 +78,7 @@ module.exports = {
label: 'Net Total (USD)',
fieldtype: 'Currency',
hidden: doc => !doc.isForeignTransaction(),
formula: async doc =>
await doc.formatIntoCustomerCurrency(doc.getSum('items', 'amount')),
disabled: true,
formula: doc => doc.getSum('items', 'amount'),
readOnly: 1
},
{
@ -97,7 +94,7 @@ module.exports = {
<div class='row' v-for='row in value'>
<div class='col-6'>{{ row.account }} ({{row.rate}}%)</div>
<div class='col-6 text-right'>
{{ row.amount }}
{{ frappe.format(row.amount, 'Currency') }}
</div>
</div>
</div>
@ -109,7 +106,6 @@ module.exports = {
label: 'Base Grand Total',
fieldtype: 'Currency',
formula: async doc => await doc.getBaseGrandTotal(),
disabled: true,
readOnly: 1
},
{
@ -118,7 +114,6 @@ module.exports = {
fieldtype: 'Currency',
hidden: doc => !doc.isForeignTransaction(),
formula: async doc => await doc.getGrandTotal(),
disabled: true,
readOnly: 1
},
{

View File

@ -10,18 +10,12 @@ module.exports = class SalesInvoice extends BaseDocument {
if (item.rate && this.exchangeRate) {
const itemRate = await this.getFrom('Item', item.item, 'rate');
item.rate = frappe.parseNumber(itemRate) / this.exchangeRate;
item.rate = itemRate / this.exchangeRate;
if (item.quantity) {
item.amount = item.rate * item.quantity;
}
item.amount = await this.formatIntoCustomerCurrency(item.amount);
item.rate = await this.formatIntoCustomerCurrency(item.rate);
}
}
this.netTotal = await this.formatIntoCustomerCurrency(this.netTotal);
this.grandTotal = await this.formatIntoCustomerCurrency(
this.grandTotal
);
}
}
@ -31,19 +25,6 @@ module.exports = class SalesInvoice extends BaseDocument {
}
}
async formatIntoCustomerCurrency(value) {
const companyCurrency = frappe.AccountingSettings.currency;
if (this.currency && this.currency.length && this.currency !== companyCurrency) {
const { numberFormat, symbol } = await this.getCustomerCurrencyInfo();
return frappe.format(value, {
fieldtype: 'Currency',
currencyInfo: { numberFormat, symbol }
});
} else {
return frappe.format(value, 'Currency');
}
}
isForeignTransaction() {
return this.currency
? this.currency !== frappe.AccountingSettings.currency
@ -52,20 +33,6 @@ module.exports = class SalesInvoice extends BaseDocument {
: 0;
}
async getCustomerCurrencyInfo() {
if (this.numberFormat || this.symbol) {
return { numberFormat: this.numberFormat, symbol: this.symbol };
}
const { numberFormat, symbol } = await frappe.getDoc(
'Currency',
this.currency
);
this.numberFormat = numberFormat;
this.symbol = symbol;
return { numberFormat, symbol };
}
async getExchangeRate() {
const companyCurrency = frappe.AccountingSettings.currency;
return this.currency === companyCurrency ? 1.0 : undefined;
@ -73,14 +40,9 @@ module.exports = class SalesInvoice extends BaseDocument {
async getBaseNetTotal() {
if (this.isForeignTransaction()) {
return frappe.format(
this.getSum('items', 'amount') * (this.exchangeRate || 0),
'Currency'
);
return this.getSum('items', 'amount') * (this.exchangeRate || 0);
} else {
return await this.formatIntoCustomerCurrency(
this.getSum('items', 'amount')
);
return this.getSum('items', 'amount');
}
}
@ -89,11 +51,10 @@ module.exports = class SalesInvoice extends BaseDocument {
let tax = await this.getTax(row.tax);
let taxAmount = [];
for (let d of tax.details || []) {
const amt = (frappe.parseNumber(row.amount) * d.rate) / 100;
taxAmount.push({
account: d.account,
rate: d.rate,
amount: await this.formatIntoCustomerCurrency(amt)
amount: (row.amount * d.rate) / 100
});
}
return JSON.stringify(taxAmount);
@ -128,12 +89,7 @@ module.exports = class SalesInvoice extends BaseDocument {
for (let taxDetail of this.taxes) {
if (taxDetail.account === rowTaxDetail.account) {
taxDetail.rate = rowTaxDetail.rate;
taxDetail.amount =
frappe.parseNumber(taxDetail.amount) +
frappe.parseNumber(rowTaxDetail.amount);
taxDetail.amount = await this.formatIntoCustomerCurrency(
taxDetail.amount
);
taxDetail.amount = taxDetail.amount + rowTaxDetail.amount;
found = true;
}
}
@ -156,27 +112,25 @@ module.exports = class SalesInvoice extends BaseDocument {
async getGrandTotal() {
await this.makeTaxSummary();
let grandTotal = frappe.parseNumber(this.netTotal);
let grandTotal = this.netTotal;
if (this.taxes) {
for (let row of this.taxes) {
grandTotal += frappe.parseNumber(row.amount);
grandTotal += row.amount;
}
}
grandTotal = Math.floor(grandTotal * 100) / 100;
return await this.formatIntoCustomerCurrency(grandTotal);
return grandTotal;
}
async getBaseGrandTotal() {
await this.makeTaxSummary();
let baseGrandTotal = frappe.parseNumber(this.baseNetTotal);
let baseGrandTotal = this.baseNetTotal;
if (this.taxes) {
for (let row of this.taxes) {
baseGrandTotal += frappe.parseNumber(row.amount) * this.exchangeRate;
baseGrandTotal += row.amount * this.exchangeRate;
}
}
baseGrandTotal = Math.floor(baseGrandTotal * 100) / 100;
return frappe.format(baseGrandTotal, 'Currency');
return baseGrandTotal;
}
};

View File

@ -20,7 +20,6 @@ export default {
fieldname: 'status',
fieldtype: 'Select',
size: 'small',
options: ['Status..', 'Paid', 'Pending'],
render(doc) {
let status = 'Pending';
let color = 'orange';

View File

@ -8,19 +8,13 @@ module.exports = class SalesInvoiceServer extends SalesInvoice {
if (this.isForeignTransaction()) {
for (let item of this.items) {
const baseItemAmount = frappe.format(
frappe.parseNumber(item.amount) * this.exchangeRate,
'Currency'
);
const baseItemAmount = item.amount * this.exchangeRate;
await entries.credit(item.account, baseItemAmount);
}
if (this.taxes) {
for (let tax of this.taxes) {
const baseTaxAmount = frappe.format(
frappe.parseNumber(tax.amount) * this.exchangeRate,
'Currency'
);
const baseTaxAmount = tax.amount * this.exchangeRate;
await entries.credit(tax.account, baseTaxAmount);
}
}

View File

@ -65,12 +65,7 @@ module.exports = {
label: 'Amount',
fieldtype: 'Currency',
readOnly: 1,
disabled: true,
formula: async (row, doc) => {
return await doc.formatIntoCustomerCurrency(
row.quantity * frappe.parseNumber(row.rate)
);
}
formula: (row, doc) => row.quantity * row.rate
},
{
fieldname: 'taxAmount',

View File

@ -19,7 +19,8 @@ export default {
red: 'bg-red-100 text-red-600',
yellow: 'bg-yellow-100 text-yellow-600',
orange: 'bg-orange-100 text-orange-600',
blue: 'bg-blue-100 text-blue-600'
blue: 'bg-blue-100 text-blue-600',
green: 'bg-green-100 text-green-600',
}[this.color];
}
}

View File

@ -49,6 +49,10 @@ const config = {
label: _('Invoice'),
route: '/list/SalesInvoice'
},
{
label: _('Payments'),
route: '/list/Payment'
},
{
label: _('Customers'),
route: '/list/Customer'