Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create button on purpose transfer #49

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

frappe.ui.form.off("Asset Movement", "set_required_fields");
frappe.ui.form.on("Asset Movement", {
refresh: function(frm) {
if(frm.doc.purpose == "Issue" && frm.doc.docstatus == 1){
if(frm.doc.purpose == "Transfer" && frm.doc.docstatus == 1){
if (!frm.doc.custom_journal_entry){
frm.add_custom_button(__("Make Journal Entry"), function(){
frappe.confirm('Are you sure you want to proceed?',
Expand All @@ -10,7 +10,8 @@ frappe.ui.form.on("Asset Movement", {
method: "asset_customizations.asset_modification.customizations.asset_movement.asset_movement.create_journal_entry",
args: {
"name": frm.doc.name,
"transaction_date": frm.doc.transaction_date
"transaction_date": frm.doc.transaction_date,
"company": frm.doc.company
},
callback: function (r) {
frm.set_value("custom_journal_entry", r.message)
Expand Down Expand Up @@ -53,6 +54,42 @@ frappe.ui.form.on("Asset Movement", {
}, __("Create"));
}
},

set_required_fields: (frm, cdt, cdn) => {
let fieldnames_to_be_altered;
if (frm.doc.purpose === "Transfer") {
fieldnames_to_be_altered = {
target_location: { read_only: 0, reqd: 1 },
source_location: { read_only: 1, reqd: 1 },
from_employee: { read_only: 1, reqd: 0 },
to_employee: { read_only: 0, reqd: 0 },
};
} else if (frm.doc.purpose === "Receipt") {
fieldnames_to_be_altered = {
target_location: { read_only: 0, reqd: 1 },
source_location: { read_only: 1, reqd: 0 },
from_employee: { read_only: 0, reqd: 0 },
to_employee: { read_only: 1, reqd: 0 },
};
} else if (frm.doc.purpose === "Issue") {
fieldnames_to_be_altered = {
target_location: { read_only: 1, reqd: 0 },
source_location: { read_only: 1, reqd: 0 },
from_employee: { read_only: 1, reqd: 0 },
to_employee: { read_only: 0, reqd: 1 },
};
}
if (fieldnames_to_be_altered) {
Object.keys(fieldnames_to_be_altered).forEach((fieldname) => {
let property_to_be_altered = fieldnames_to_be_altered[fieldname];
Object.keys(property_to_be_altered).forEach((property) => {
let value = property_to_be_altered[property];
frm.fields_dict["assets"].grid.update_docfield_property(fieldname, property, value);
});
});
frm.refresh_field("assets");
}
},
});


Expand All @@ -68,28 +105,25 @@ frappe.ui.form.on("Asset Movement Item", {

// on manual entry of an asset auto sets their source location / employee
const asset_name = locals[cdt][cdn].asset;
console.log("Selected Asset:", asset_name);

if (asset_name) {
frappe.db.get_doc("Asset", asset_name)
.then(asset_doc => {
if (asset_doc.location) {
console.log("Setting Source Location:", asset_doc.location);
frappe.model.set_value(cdt, cdn, "source_location", asset_doc.location);
frappe.model.set_value(cdt, cdn, "target_location", asset_doc.location);
}
if (asset_doc.custodian) {
frappe.model.set_value(cdt, cdn, "from_employee", asset_doc.custodian);
}
if (asset_doc.cost_center) {
console.log("Setting From Cost Center:", asset_doc.cost_center);
frappe.model.set_value(cdt, cdn, "custom_from_cost_center", asset_doc.cost_center);
}

// Dynamically set other fields
field_names.forEach(field => {
const original_field = field.replace("from_", "");
if (asset_doc[original_field]) {
console.log(`Setting ${field}:`, asset_doc[original_field]);
frappe.model.set_value(cdt, cdn, field, asset_doc[original_field]);
}
});
Expand Down
Loading