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

feat: add entry duplication

This commit is contained in:
18alantom 2022-03-17 11:55:01 +05:30 committed by Alan
parent 0412095342
commit 77e226363d
2 changed files with 69 additions and 1 deletions

View File

@ -710,6 +710,41 @@ module.exports = class BaseDocument extends Observable {
return obj; return obj;
}, {}); }, {});
} }
async duplicate() {
const updateMap = {};
const fieldValueMap = this.getValidDict();
const keys = this.meta.fields.map((f) => f.fieldname);
for (const key of keys) {
let value = fieldValueMap[key];
if (!value) {
continue;
}
if (isPesa(value)) {
value = value.copy();
}
if (value instanceof Array) {
value.forEach((row) => {
delete row.name;
delete row.parent;
});
}
updateMap[key] = value;
}
if (this.numberSeries) {
delete updateMap.name;
} else {
updateMap.name = updateMap.name + ' CPY';
}
const doc = frappe.getNewDoc(this.doctype, false);
await doc.set(updateMap);
await doc.insert();
}
}; };
function getPreDefaultValues(fieldtype) { function getPreDefaultValues(fieldtype) {

View File

@ -227,7 +227,40 @@ export function getActionsForDocument(doc) {
}, },
}; };
let actions = [...(doc.meta.actions || []), deleteAction, cancelAction] const isSubmittable = !!doc.meta.isSubmittable;
const duplicateAction = {
label: frappe.t`Duplicate`,
condition: (doc) =>
((isSubmittable && doc && doc.submitted) || !isSubmittable) &&
!doc._notInserted &&
!(doc.cancelled || false),
action: () => {
showMessageDialog({
message: t`Duplicate ${doc.doctype} ${doc.name}?`,
buttons: [
{
label: t`Yes`,
async action() {
doc.duplicate();
},
},
{
label: t`No`,
action() {
resolve(false);
},
},
],
});
},
};
let actions = [
...(doc.meta.actions || []),
duplicateAction,
deleteAction,
cancelAction,
]
.filter((d) => (d.condition ? d.condition(doc) : true)) .filter((d) => (d.condition ? d.condition(doc) : true))
.map((d) => { .map((d) => {
return { return {