Skip to content

Commit

Permalink
feat: create party link from customer/supplier (#28387)
Browse files Browse the repository at this point in the history
* feat: create party link from customer/supplier

* test: create_party_link method
  • Loading branch information
nextchamp-saqib authored Nov 22, 2021
1 parent 7677d6d commit 78851ec
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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

9 changes: 2 additions & 7 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,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 @@ -2308,13 +2309,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 @@ -158,5 +164,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();
}
});

0 comments on commit 78851ec

Please sign in to comment.