2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00

refactor: rename "Payment For" to "References"

- formatting
- change default values to function defaults
- add migration script for the Payment table
This commit is contained in:
18alantom 2021-11-08 15:15:27 +05:30
parent 09d4b305eb
commit 8f64d95055
8 changed files with 114 additions and 82 deletions

View File

@ -24,48 +24,48 @@ export default {
'Excise Entry', 'Excise Entry',
'Write Off Entry', 'Write Off Entry',
'Opening Entry', 'Opening Entry',
'Depreciation Entry' 'Depreciation Entry',
], ],
required: 1 required: 1,
}, },
{ {
fieldname: 'date', fieldname: 'date',
label: 'Date', label: 'Date',
fieldtype: 'Date', fieldtype: 'Date',
default: DateTime.local().toISODate() default: () => DateTime.local().toISODate(),
}, },
{ {
fieldname: 'accounts', fieldname: 'accounts',
label: 'Account Entries', label: 'Account Entries',
fieldtype: 'Table', fieldtype: 'Table',
childtype: 'JournalEntryAccount', childtype: 'JournalEntryAccount',
required: true required: true,
}, },
{ {
fieldname: 'referenceNumber', fieldname: 'referenceNumber',
label: 'Reference Number', label: 'Reference Number',
fieldtype: 'Data' fieldtype: 'Data',
}, },
{ {
fieldname: 'referenceDate', fieldname: 'referenceDate',
label: 'Reference Date', label: 'Reference Date',
fieldtype: 'Date' fieldtype: 'Date',
}, },
{ {
fieldname: 'userRemark', fieldname: 'userRemark',
label: 'User Remark', label: 'User Remark',
fieldtype: 'Text', fieldtype: 'Text',
placeholder: 'User Remark' placeholder: 'User Remark',
} },
], ],
actions: [ actions: [
{ {
label: 'Revert', label: 'Revert',
condition: doc => doc.submitted, condition: (doc) => doc.submitted,
action(doc) { action(doc) {
doc.revert(); doc.revert();
}
}, },
ledgerLink },
] ledgerLink,
],
}; };

View File

@ -15,13 +15,13 @@ export default {
label: 'Party', label: 'Party',
fieldtype: 'Link', fieldtype: 'Link',
target: 'Party', target: 'Party',
required: 1 required: 1,
}, },
{ {
fieldname: 'date', fieldname: 'date',
label: 'Posting Date', label: 'Posting Date',
fieldtype: 'Date', fieldtype: 'Date',
default: new Date().toISOString() default: () => new Date().toISOString(),
}, },
{ {
fieldname: 'account', fieldname: 'account',
@ -37,14 +37,14 @@ export default {
return { accountType: ['in', ['Bank', 'Cash']], isGroup: 0 }; return { accountType: ['in', ['Bank', 'Cash']], isGroup: 0 };
} }
} }
} },
}, },
{ {
fieldname: 'paymentType', fieldname: 'paymentType',
label: 'Payment Type', label: 'Payment Type',
fieldtype: 'Select', fieldtype: 'Select',
options: ['', 'Receive', 'Pay'], options: ['', 'Receive', 'Pay'],
required: 1 required: 1,
}, },
{ {
fieldname: 'paymentAccount', fieldname: 'paymentAccount',
@ -62,11 +62,11 @@ export default {
} }
} }
}, },
formula: doc => { formula: (doc) => {
if (doc.paymentMethod === 'Cash') { if (doc.paymentMethod === 'Cash') {
return 'Cash'; return 'Cash';
} }
} },
}, },
{ {
fieldname: 'paymentMethod', fieldname: 'paymentMethod',
@ -74,34 +74,34 @@ export default {
placeholder: 'Payment Method', placeholder: 'Payment Method',
fieldtype: 'Select', fieldtype: 'Select',
options: ['', 'Cash', 'Cheque', 'Transfer'], options: ['', 'Cash', 'Cheque', 'Transfer'],
required: 1 required: 1,
}, },
{ {
fieldname: 'referenceId', fieldname: 'referenceId',
label: 'Ref. / Cheque No.', label: 'Ref. / Cheque No.',
placeholder: 'Ref. / Cheque No.', placeholder: 'Ref. / Cheque No.',
fieldtype: 'Data', fieldtype: 'Data',
required: (doc) => doc.paymentMethod !== 'Cash' // TODO: UNIQUE required: (doc) => doc.paymentMethod !== 'Cash', // TODO: UNIQUE
}, },
{ {
fieldname: 'referenceDate', fieldname: 'referenceDate',
label: 'Ref. Date', label: 'Ref. Date',
placeholder: 'Ref. Date', placeholder: 'Ref. Date',
fieldtype: 'Date' fieldtype: 'Date',
}, },
{ {
fieldname: 'clearanceDate', fieldname: 'clearanceDate',
label: 'Clearance Date', label: 'Clearance Date',
placeholder: 'Clearance Date', placeholder: 'Clearance Date',
fieldtype: 'Date', fieldtype: 'Date',
hidden: (doc) => doc.paymentMethod === 'Cash' hidden: (doc) => doc.paymentMethod === 'Cash',
}, },
{ {
fieldname: 'amount', fieldname: 'amount',
label: 'Amount', label: 'Amount',
fieldtype: 'Currency', fieldtype: 'Currency',
required: 1, required: 1,
formula: doc => doc.getSum('for', 'amount'), formula: (doc) => doc.getSum('for', 'amount'),
validate(value, doc) { validate(value, doc) {
if (doc.for.length === 0) return; if (doc.for.length === 0) return;
const amount = doc.getSum('for', 'amount'); const amount = doc.getSum('for', 'amount');
@ -118,24 +118,27 @@ export default {
} else if (value === 0) { } else if (value === 0) {
throw new frappe.errors.ValidationError( throw new frappe.errors.ValidationError(
frappe._( frappe._(
`Payment amount cannot be ${frappe.format(value, 'Currency')}. Amount has been reset to max viable amount.` `Payment amount cannot be ${frappe.format(
value,
'Currency'
)}. Amount has been reset to max viable amount.`
) )
); );
} }
} },
}, },
{ {
fieldname: 'writeoff', fieldname: 'writeoff',
label: 'Write Off / Refund', label: 'Write Off / Refund',
fieldtype: 'Currency' fieldtype: 'Currency',
}, },
{ {
fieldname: 'for', fieldname: 'for',
label: 'Payment For', label: 'Payment Reference',
fieldtype: 'Table', fieldtype: 'Table',
childtype: 'PaymentFor', childtype: 'PaymentFor',
required: 0, required: 0,
} },
], ],
quickEditFields: [ quickEditFields: [
@ -150,58 +153,58 @@ export default {
'clearanceDate', 'clearanceDate',
'amount', 'amount',
'writeoff', 'writeoff',
'for' 'for',
], ],
layout: [ layout: [
{ {
columns: [ columns: [
{ {
fields: ['party', 'account'] fields: ['party', 'account'],
}, },
{ {
fields: ['date', 'paymentAccount'] fields: ['date', 'paymentAccount'],
} },
] ],
}, },
{ {
columns: [ columns: [
{ {
fields: ['paymentMethod'] fields: ['paymentMethod'],
}, },
{ {
fields: ['paymentType'] fields: ['paymentType'],
}, },
{ {
fields: ['referenceId'] fields: ['referenceId'],
} },
] ],
}, },
{ {
columns: [ columns: [
{ {
fields: ['referenceDate'] fields: ['referenceDate'],
}, },
{ {
fields: ['clearanceDate'] fields: ['clearanceDate'],
} },
] ],
}, },
{ {
columns: [ columns: [
{ {
fields: ['for'] fields: ['for'],
} },
] ],
}, },
{ {
columns: [ columns: [
{ {
fields: ['amount', 'writeoff'] fields: ['amount', 'writeoff'],
} },
] ],
} },
], ],
links: [utils.ledgerLink] links: [utils.ledgerLink],
}; };

View File

@ -20,8 +20,8 @@ export default class PaymentServer extends BaseDocument {
} }
async beforeSubmit() { async beforeSubmit() {
if (!this.for.length) { if (!this.for || !this.for.length) {
throw new Error(`No reference for the payment.`); return;
} }
for (let row of this.for) { for (let row of this.for) {
if (!['SalesInvoice', 'PurchaseInvoice'].includes(row.referenceType)) { if (!['SalesInvoice', 'PurchaseInvoice'].includes(row.referenceType)) {

View File

@ -26,7 +26,7 @@ export default {
fieldname: 'date', fieldname: 'date',
label: 'Date', label: 'Date',
fieldtype: 'Date', fieldtype: 'Date',
default: new Date().toISOString().slice(0, 10) default: () => new Date().toISOString().slice(0, 10)
}, },
{ {
fieldname: 'supplier', fieldname: 'supplier',

View File

@ -19,20 +19,20 @@ export default {
fieldname: 'name', fieldname: 'name',
fieldtype: 'Data', fieldtype: 'Data',
required: 1, required: 1,
readOnly: 1 readOnly: 1,
}, },
{ {
fieldname: 'date', fieldname: 'date',
label: 'Date', label: 'Date',
fieldtype: 'Date', fieldtype: 'Date',
default: new Date().toISOString().slice(0, 10) default: () => new Date().toISOString().slice(0, 10),
}, },
{ {
fieldname: 'customer', fieldname: 'customer',
label: 'Customer', label: 'Customer',
fieldtype: 'Link', fieldtype: 'Link',
target: 'Customer', target: 'Customer',
required: 1 required: 1,
}, },
{ {
fieldname: 'account', fieldname: 'account',
@ -40,90 +40,90 @@ export default {
fieldtype: 'Link', fieldtype: 'Link',
target: 'Account', target: 'Account',
disableCreation: true, disableCreation: true,
formula: doc => doc.getFrom('Party', doc.customer, 'defaultAccount'), formula: (doc) => doc.getFrom('Party', doc.customer, 'defaultAccount'),
getFilters: () => { getFilters: () => {
return { return {
isGroup: 0, isGroup: 0,
accountType: 'Receivable' accountType: 'Receivable',
}; };
} },
}, },
{ {
fieldname: 'currency', fieldname: 'currency',
label: 'Customer Currency', label: 'Customer Currency',
fieldtype: 'Link', fieldtype: 'Link',
target: 'Currency', target: 'Currency',
formula: doc => doc.getFrom('Party', doc.customer, 'currency'), formula: (doc) => doc.getFrom('Party', doc.customer, 'currency'),
formulaDependsOn: ['customer'] formulaDependsOn: ['customer'],
}, },
{ {
fieldname: 'exchangeRate', fieldname: 'exchangeRate',
label: 'Exchange Rate', label: 'Exchange Rate',
fieldtype: 'Float', fieldtype: 'Float',
formula: doc => doc.getExchangeRate(), formula: (doc) => doc.getExchangeRate(),
readOnly: true readOnly: true,
}, },
{ {
fieldname: 'items', fieldname: 'items',
label: 'Items', label: 'Items',
fieldtype: 'Table', fieldtype: 'Table',
childtype: 'SalesInvoiceItem', childtype: 'SalesInvoiceItem',
required: true required: true,
}, },
{ {
fieldname: 'netTotal', fieldname: 'netTotal',
label: 'Net Total', label: 'Net Total',
fieldtype: 'Currency', fieldtype: 'Currency',
formula: doc => doc.getSum('items', 'amount'), formula: (doc) => doc.getSum('items', 'amount'),
readOnly: 1, readOnly: 1,
getCurrency: doc => doc.currency getCurrency: (doc) => doc.currency,
}, },
{ {
fieldname: 'baseNetTotal', fieldname: 'baseNetTotal',
label: 'Net Total (Company Currency)', label: 'Net Total (Company Currency)',
fieldtype: 'Currency', fieldtype: 'Currency',
formula: doc => doc.netTotal * doc.exchangeRate, formula: (doc) => doc.netTotal * doc.exchangeRate,
readOnly: 1 readOnly: 1,
}, },
{ {
fieldname: 'taxes', fieldname: 'taxes',
label: 'Taxes', label: 'Taxes',
fieldtype: 'Table', fieldtype: 'Table',
childtype: 'TaxSummary', childtype: 'TaxSummary',
formula: doc => doc.getTaxSummary(), formula: (doc) => doc.getTaxSummary(),
readOnly: 1 readOnly: 1,
}, },
{ {
fieldname: 'grandTotal', fieldname: 'grandTotal',
label: 'Grand Total', label: 'Grand Total',
fieldtype: 'Currency', fieldtype: 'Currency',
formula: doc => doc.getGrandTotal(), formula: (doc) => doc.getGrandTotal(),
readOnly: 1, readOnly: 1,
getCurrency: doc => doc.currency getCurrency: (doc) => doc.currency,
}, },
{ {
fieldname: 'baseGrandTotal', fieldname: 'baseGrandTotal',
label: 'Grand Total (Company Currency)', label: 'Grand Total (Company Currency)',
fieldtype: 'Currency', fieldtype: 'Currency',
formula: doc => doc.grandTotal * doc.exchangeRate, formula: (doc) => doc.grandTotal * doc.exchangeRate,
readOnly: 1 readOnly: 1,
}, },
{ {
fieldname: 'outstandingAmount', fieldname: 'outstandingAmount',
label: 'Outstanding Amount', label: 'Outstanding Amount',
fieldtype: 'Currency', fieldtype: 'Currency',
formula: doc => { formula: (doc) => {
if (doc.submitted) return; if (doc.submitted) return;
return doc.baseGrandTotal; return doc.baseGrandTotal;
}, },
readOnly: 1 readOnly: 1,
}, },
{ {
fieldname: 'terms', fieldname: 'terms',
label: 'Notes', label: 'Notes',
fieldtype: 'Text' fieldtype: 'Text',
} },
], ],
actions: getActions('SalesInvoice') actions: getActions('SalesInvoice'),
}; };

View File

@ -0,0 +1 @@
v0_0_3/makePaymentRefIdNullable

View File

@ -0,0 +1,28 @@
import frappe from 'frappejs';
export default async function execute() {
// Since sqlite has no ALTER TABLE to change column meta
// the table has to be _Prestiged_.
const tableInfo = await frappe.db.sql('pragma table_info("Payment")');
const referenceId = tableInfo.find(({ name }) => name === 'referenceId');
if (!referenceId || !referenceId.notnull) {
return;
}
await frappe.db.createTable('Payment', '__Payment');
await frappe.db.sql('insert into __Payment select * from Payment');
const mainCount = await frappe.db.knex
.table('Payment')
.count('name as count');
const replCount = await frappe.db.knex
.table('__Payment')
.count('name as count');
if (mainCount[0].count === replCount[0].count) {
await frappe.db.knex.schema.dropTable('Payment');
await frappe.db.knex.schema.renameTable('__Payment', 'Payment');
} else {
await frappe.db.knex.schema.dropTable('__Payment');
}
}

View File

@ -6,11 +6,11 @@ const requirePatch = require.context('../patches', true, /\w+\.(js)$/);
export default async function runMigrate() { export default async function runMigrate() {
let patchOrder = patchesTxt.split('\n'); let patchOrder = patchesTxt.split('\n');
let allPatches = {}; let allPatches = {};
requirePatch.keys().forEach(fileName => { requirePatch.keys().forEach((fileName) => {
if (fileName === './index.js') return; if (fileName === './index.js') return;
let method; let method;
try { try {
method = requirePatch(fileName); method = requirePatch(fileName).default;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
method = null; method = null;