Skip to content

Commit

Permalink
Merge pull request #872 from fproldan/backport-865-to-diamoerp-staging
Browse files Browse the repository at this point in the history
[Backport diamoerp-staging] Comisiones de Pago
  • Loading branch information
fproldan authored Nov 29, 2024
2 parents f5eabb9 + 1fd87ea commit eef8561
Show file tree
Hide file tree
Showing 11 changed files with 618 additions and 223 deletions.
107 changes: 107 additions & 0 deletions erpnext/accounts/doctype/payment_entry/payment_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,17 @@ frappe.ui.form.on('Payment Entry', {
filters: filters
};
});

frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
frm.add_fetch("sales_person", "commission_rate", "commission_rate");
},

refresh: function(frm) {
erpnext.hide_company();
frm.events.hide_unhide_fields(frm);
frm.events.set_dynamic_labels(frm);
frm.events.show_general_ledger(frm);
frm.events.calculate_commission(frm);
},

validate_company: (frm) => {
Expand Down Expand Up @@ -367,6 +371,22 @@ frappe.ui.form.on('Payment Entry', {
() => frm.set_value("party_balance", r.message.party_balance),
() => frm.set_value("party_name", r.message.party_name),
() => frm.clear_table("references"),
() => {
frm.clear_table("sales_team");
r.message.sales_team.forEach((data) => {
let new_row = frm.add_child('sales_team');
new_row.sales_person = data.sales_person;
new_row.allocated_percentage = data.allocated_percentage;
frappe.db.get_value('Sales Person', data.sales_person, 'commission_rate')
.then(r => {
let commission_rate = r.message.commission_rate || 0;
new_row.commission_rate = commission_rate;
frm.refresh_field('sales_team');
});
});

frm.refresh_field('sales_team');
},
() => frm.events.hide_unhide_fields(frm),
() => frm.events.set_dynamic_labels(frm),
() => {
Expand Down Expand Up @@ -578,6 +598,7 @@ frappe.ui.form.on('Payment Entry', {
frm.set_value("base_paid_amount", flt(frm.doc.paid_amount) * flt(frm.doc.source_exchange_rate));
frm.trigger("reset_received_amount");
frm.events.hide_unhide_fields(frm);
frm.events.calculate_commission(frm);
},

received_amount: function(frm) {
Expand Down Expand Up @@ -1331,8 +1352,94 @@ frappe.ui.form.on('Payment Entry', {

return current_tax_amount;
},
/**
COMMISSION
**/
commission_rate: function(frm) {
frm.events.calculate_commission(frm);
},
calculate_commission: function(frm) {
if (!frm.fields_dict.commission_rate) return;

if (frm.doc.commission_rate > 100) {
frm.set_value("commission_rate", 100);
frappe.throw(`${__(frappe.meta.get_label(
this.frm.doc.doctype, "commission_rate", frm.doc.name
))} ${__("cannot be greater than 100")}`);
}

frm.doc.amount_eligible_for_commission = frm.doc.paid_amount

frm.doc.total_commission = flt(
frm.doc.amount_eligible_for_commission * frm.doc.commission_rate / 100.0,
precision("total_commission")
);

refresh_field(["amount_eligible_for_commission", "total_commission"]);
},
total_commission: function(frm) {
frappe.model.round_floats_in(frm.doc, ["amount_eligible_for_commission", "total_commission"]);

const { amount_eligible_for_commission } = frm.doc;
if (!amount_eligible_for_commission) return;

frm.set_value(
"commission_rate", flt(
frm.doc.total_commission * 100.0 / amount_eligible_for_commission
)
);
},
calculate_contribution: function(frm) {
$.each(frm.doc.doctype.sales_team || [], function(i, sales_person) {
frappe.model.round_floats_in(sales_person);
if (!sales_person.allocated_percentage) return;

sales_person.allocated_amount = flt(
frm.doc.amount_eligible_for_commission
* sales_person.allocated_percentage
/ 100.0,
precision("allocated_amount", sales_person)
);
});
},
calculate_incentive: function(frm, row) {
if (row.allocated_amount) {
row.incentives = flt(row.allocated_amount * row.commission_rate / 100.0, precision("incentives", row));
}

var total_incentives = frm.doc.paid_amount

if (total_incentives) {
row.incentives = flt((row.allocated_percentage * flt(total_incentives, precision("incentives", row))) / 100);
}
},
});

frappe.ui.form.on('Sales Team', {
allocated_percentage: function(frm, cdt, cdn) {
var sales_person = frappe.get_doc(cdt, cdn);
if(sales_person.allocated_percentage) {

sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
precision("allocated_percentage", sales_person));

sales_person.allocated_amount = flt(frm.doc.amount_eligible_for_commission *
sales_person.allocated_percentage / 100.0,
precision("allocated_amount", sales_person));
refresh_field(["allocated_amount"], sales_person);

frm.events.calculate_incentive(frm, sales_person);
refresh_field(["allocated_percentage", "allocated_amount", "commission_rate","incentives"], sales_person.name,
sales_person.parentfield);
}
},
sales_person: function(frm, cdt, cdn) {
var row = frappe.get_doc(cdt, cdn);
frm.events.calculate_incentive(frm, row);
refresh_field("incentives", row.name,row.parentfield);
},

})

frappe.ui.form.on('Payment Entry Reference', {
reference_doctype: function(frm, cdt, cdn) {
Expand Down
67 changes: 64 additions & 3 deletions erpnext/accounts/doctype/payment_entry/payment_entry.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
"cost_center",
"dimension_col_break",
"project",
"sales_team_section_break",
"sales_partner",
"column_break_74",
"amount_eligible_for_commission",
"commission_rate",
"total_commission",
"sales_team_section",
"sales_team",
"section_break_12",
"status",
"custom_remarks",
Expand Down Expand Up @@ -473,7 +481,6 @@
{
"fieldname": "project",
"fieldtype": "Link",
"hidden": 0,
"label": "Project",
"options": "Project",
"print_hide": 1
Expand Down Expand Up @@ -708,12 +715,65 @@
"label": "Received Amount After Tax (Company Currency)",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"collapsible": 1,
"collapsible_depends_on": "sales_partner",
"fieldname": "sales_team_section_break",
"fieldtype": "Section Break",
"label": "Commission",
"options": "fa fa-group",
"print_hide": 1
},
{
"fieldname": "sales_partner",
"fieldtype": "Link",
"label": "Sales Partner",
"options": "Sales Partner",
"print_hide": 1
},
{
"fieldname": "column_break_74",
"fieldtype": "Column Break"
},
{
"fieldname": "amount_eligible_for_commission",
"fieldtype": "Currency",
"label": "Amount Eligible for Commission",
"read_only": 1
},
{
"fieldname": "commission_rate",
"fieldtype": "Float",
"label": "Commission Rate (%)",
"print_hide": 1
},
{
"fieldname": "total_commission",
"fieldtype": "Currency",
"label": "Total Commission",
"options": "Company:company:default_currency",
"print_hide": 1
},
{
"collapsible": 1,
"collapsible_depends_on": "eval:1 === 1",
"fieldname": "sales_team_section",
"fieldtype": "Section Break",
"label": "Sales Team",
"print_hide": 1
},
{
"fieldname": "sales_team",
"fieldtype": "Table",
"label": "Sales Contributions and Incentives",
"options": "Sales Team"
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2021-08-06 08:57:45.018591",
"modified": "2024-09-02 09:22:39.609826",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Payment Entry",
Expand Down Expand Up @@ -752,12 +812,13 @@
"write": 1
},
{
"export": 1,
"read": 1,
"report": 1,
"export": 1,
"role": "Usuario Contador"
}
],
"route": "app/doctype/Payment%20Entry",
"show_name_in_global_search": 1,
"sort_field": "modified",
"sort_order": "DESC",
Expand Down
56 changes: 55 additions & 1 deletion erpnext/accounts/doctype/payment_entry/payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ def calculate_taxes(self):
if self.get('taxes'):
self.paid_amount_after_tax = self.get('taxes')[-1].base_total

self.calculate_commission()
self.calculate_contribution()

def get_current_tax_amount(self, tax):
tax_rate = tax.rate

Expand Down Expand Up @@ -1035,6 +1038,47 @@ def get_current_tax_fraction(self, tax):
current_tax_fraction *= -1.0

return current_tax_fraction

def calculate_commission(self):
if not self.meta.get_field("commission_rate"):
return

self.round_floats_in(
self, ("amount_eligible_for_commission", "commission_rate")
)

if not (0 <= self.commission_rate <= 100.0):
throw("{} {}".format(_(self.meta.get_label("commission_rate")), _("must be between 0 and 100"),))

self.amount_eligible_for_commission = self.paid_amount

self.total_commission = flt(
self.amount_eligible_for_commission * self.commission_rate / 100.0,
self.precision("total_commission")
)

def calculate_contribution(self):
if not self.meta.get_field("sales_team"):
return

total = 0.0
sales_team = self.get("sales_team")
for sales_person in sales_team:
self.round_floats_in(sales_person)

sales_person.allocated_amount = flt(
self.amount_eligible_for_commission * sales_person.allocated_percentage / 100.0,
self.precision("allocated_amount", sales_person))

if sales_person.commission_rate:
sales_person.incentives = flt(
sales_person.allocated_amount * flt(sales_person.commission_rate) / 100.0,
self.precision("incentives", sales_person))

total += sales_person.allocated_percentage

if sales_team and total != 100.0:
throw(_("Total allocated percentage for sales team should be 100"))

def validate_inclusive_tax(tax, doc):
def _on_previous_row_error(row_range):
Expand Down Expand Up @@ -1302,13 +1346,23 @@ def get_party_details(company, party_type, party, date, cost_center=None):
if party_type in ["Customer", "Supplier"]:
bank_account = get_party_bank_account(party_type, party)

sales_team = []
if party_type=="Customer":
sales_team = []
for d in frappe.get_all("Sales Person", {"user": frappe.session.user}, pluck="name"):
sales_team.append({
"sales_person": d,
"allocated_percentage": 100,
})

return {
"party_account": party_account,
"party_name": party_name,
"party_account_currency": account_currency,
"party_balance": party_balance,
"account_balance": account_balance,
"bank_account": bank_account
"bank_account": bank_account,
"sales_team": sales_team,
}


Expand Down
6 changes: 3 additions & 3 deletions erpnext/payroll/doctype/contributions/contributions.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"fieldname": "order_or_invoice",
"fieldtype": "Dynamic Link",
"in_list_view": 1,
"label": "Order / Invoice",
"label": "Order / Invoice / EP",
"options": "document_type",
"read_only": 1
},
Expand Down Expand Up @@ -89,13 +89,13 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2021-09-14 19:11:43.548342",
"modified": "2024-09-02 09:57:20.539959",
"modified_by": "Administrator",
"module": "Payroll",
"restrict_to_domain": "Vendedores y Socios",
"name": "Contributions",
"owner": "Administrator",
"permissions": [],
"restrict_to_domain": "Vendedores y Socios",
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"fieldname": "commission_based_on",
"fieldtype": "Select",
"label": "Commission Based on",
"options": "Sales Order\nSales Invoice"
"options": "Sales Order\nSales Invoice\nPayment Entry"
},
{
"default": "0",
Expand All @@ -99,7 +99,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-06-21 12:21:11.120005",
"modified": "2024-09-02 09:57:58.062144",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Process Sales Commission",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"fieldname": "commission_based_on",
"fieldtype": "Select",
"label": "Commission Based on",
"options": "Sales Order\nSales Invoice"
"options": "Sales Order\nSales Invoice\nPayment Entry"
},
{
"fieldname": "total_contribution",
Expand Down Expand Up @@ -228,7 +228,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-06-16 11:21:34.018262",
"modified": "2024-09-02 09:54:05.798824",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Sales Commission",
Expand Down
Loading

0 comments on commit eef8561

Please sign in to comment.