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

feat: create party link from customer/supplier (backport #28387) #28481

Merged
Merged
Show file tree
Hide file tree
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
Expand Up @@ -20,6 +20,9 @@ def validate(self):
frappe.db.set_default("add_taxes_from_item_tax_template",
self.get("add_taxes_from_item_tax_template", 0))

frappe.db.set_default("enable_common_party_accounting",
self.get("enable_common_party_accounting", 0))

self.validate_stale_days()
self.enable_payment_schedule_in_print()
self.toggle_discount_accounting_fields()
Expand Down
14 changes: 14 additions & 0 deletions erpnext/accounts/doctype/party_link/party_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ def validate(self):
if existing_party_link:
frappe.throw(_('{} {} is already linked with another {}')
.format(self.primary_role, self.primary_party, existing_party_link[0]))


@frappe.whitelist()
def create_party_link(primary_role, primary_party, secondary_party):
party_link = frappe.new_doc('Party Link')
party_link.primary_role = primary_role
party_link.primary_party = primary_party
party_link.secondary_role = 'Customer' if primary_role == 'Supplier' else 'Supplier'
party_link.secondary_party = secondary_party

party_link.save(ignore_permissions=True)

return party_link

Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,7 @@ def test_sales_invoice_against_supplier(self):
from erpnext.accounts.doctype.opening_invoice_creation_tool.test_opening_invoice_creation_tool import (
make_customer,
)
from erpnext.accounts.doctype.party_link.party_link import create_party_link
from erpnext.buying.doctype.supplier.test_supplier import create_supplier

# create a customer
Expand All @@ -2245,13 +2246,7 @@ def test_sales_invoice_against_supplier(self):
supplier = create_supplier(supplier_name="_Test Common Supplier").name

# create a party link between customer & supplier
# set primary role as supplier
party_link = frappe.new_doc("Party Link")
party_link.primary_role = "Supplier"
party_link.primary_party = supplier
party_link.secondary_role = "Customer"
party_link.secondary_party = customer
party_link.save()
party_link = create_party_link("Supplier", supplier, customer)

# enable common party accounting
frappe.db.set_value('Accounts Settings', None, 'enable_common_party_accounting', 1)
Expand Down
43 changes: 43 additions & 0 deletions erpnext/buying/doctype/supplier/supplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ frappe.ui.form.on("Supplier", {
frm.trigger("get_supplier_group_details");
}, __('Actions'));

if (cint(frappe.defaults.get_default("enable_common_party_accounting"))) {
frm.add_custom_button(__('Link with Customer'), function () {
frm.trigger('show_party_link_dialog');
}, __('Actions'));
}

// indicators
erpnext.utils.set_party_dashboard_indicators(frm);
}
Expand Down Expand Up @@ -128,5 +134,42 @@ frappe.ui.form.on("Supplier", {
else {
frm.toggle_reqd("represents_company", false);
}
},
show_party_link_dialog: function(frm) {
const dialog = new frappe.ui.Dialog({
title: __('Select a Customer'),
fields: [{
fieldtype: 'Link', label: __('Customer'),
options: 'Customer', fieldname: 'customer', reqd: 1
}],
primary_action: function({ customer }) {
frappe.call({
method: 'erpnext.accounts.doctype.party_link.party_link.create_party_link',
args: {
primary_role: 'Supplier',
primary_party: frm.doc.name,
secondary_party: customer
},
freeze: true,
callback: function() {
dialog.hide();
frappe.msgprint({
message: __('Successfully linked to Customer'),
alert: true
});
},
error: function() {
dialog.hide();
frappe.msgprint({
message: __('Linking to Customer Failed. Please try again.'),
title: __('Linking Failed'),
indicator: 'red'
});
}
});
},
primary_action_label: __('Create Link')
});
dialog.show();
}
});
43 changes: 43 additions & 0 deletions erpnext/selling/doctype/customer/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ frappe.ui.form.on("Customer", {
frm.trigger("get_customer_group_details");
}, __('Actions'));

if (cint(frappe.defaults.get_default("enable_common_party_accounting"))) {
frm.add_custom_button(__('Link with Supplier'), function () {
frm.trigger('show_party_link_dialog');
}, __('Actions'));
}

// indicator
erpnext.utils.set_party_dashboard_indicators(frm);

Expand All @@ -159,5 +165,42 @@ frappe.ui.form.on("Customer", {
}
});

},
show_party_link_dialog: function(frm) {
const dialog = new frappe.ui.Dialog({
title: __('Select a Supplier'),
fields: [{
fieldtype: 'Link', label: __('Supplier'),
options: 'Supplier', fieldname: 'supplier', reqd: 1
}],
primary_action: function({ supplier }) {
frappe.call({
method: 'erpnext.accounts.doctype.party_link.party_link.create_party_link',
args: {
primary_role: 'Customer',
primary_party: frm.doc.name,
secondary_party: supplier
},
freeze: true,
callback: function() {
dialog.hide();
frappe.msgprint({
message: __('Successfully linked to Supplier'),
alert: true
});
},
error: function() {
dialog.hide();
frappe.msgprint({
message: __('Linking to Supplier Failed. Please try again.'),
title: __('Linking Failed'),
indicator: 'red'
});
}
});
},
primary_action_label: __('Create Link')
});
dialog.show();
}
});