Skip to content

Commit a31dcae

Browse files
fproldangithub-actions[bot]
authored andcommitted
La factura no obtiene el precio correcto en la moneda de la empresa desde una suscripción con lista de precios en moneda extranjera. (#874)
* fix: price list subs * featL validate_plans_price_lists
1 parent c112f6f commit a31dcae

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

erpnext/accounts/doctype/subscription/subscription.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ def validate(self):
274274
self.validate_end_date()
275275
self.validate_to_follow_calendar_months()
276276
self.cost_center = erpnext.get_default_cost_center(self.get('company'))
277+
self.validate_plans_price_lists()
278+
279+
def validate_plans_price_lists(self):
280+
if len(self.plans) <= 1:
281+
return
282+
283+
price_list = [frappe.get_value("Subscription Plan", plan.plan, 'price_list') for plan in self.plans]
284+
price_list = list(set(list(filter(lambda x: x is not None, price_list))))
285+
if len(price_list) > 1:
286+
frappe.throw(_("Solo se pueden tener Planes con la misma Lista de Precio en una Suscripción."))
277287

278288
def validate_trial_period(self):
279289
"""
@@ -341,14 +351,13 @@ def create_invoice(self, prorate):
341351
invoice = frappe.new_doc(doctype)
342352
invoice.subscription = self.name
343353

344-
# Si solo tiene un plan aplicamos esa lista de precios a la factura
345-
if len(self.plans) == 1:
346-
price_list = frappe.get_value("Subscription Plan", self.plans[0].plan, 'price_list')
347-
if price_list:
348-
if doctype == "Sales Invoice":
349-
invoice.selling_price_list = price_list
350-
else:
351-
invoice.buying_price_list = price_list
354+
# Aplicamos esa lista de precios a la factura
355+
price_list = frappe.get_value("Subscription Plan", self.plans[0].plan, 'price_list')
356+
if price_list:
357+
if doctype == "Sales Invoice":
358+
invoice.selling_price_list = price_list
359+
else:
360+
invoice.buying_price_list = price_list
352361

353362
# For backward compatibility
354363
# Earlier subscription didn't had any company field
@@ -457,6 +466,10 @@ def get_items_from_plans(self, plans, prorate=0):
457466

458467
items = []
459468
party = self.party
469+
470+
doctype = "Sales Invoice" if self.party_type == "Customer" else "Purchase Invoice"
471+
company = self.company
472+
460473
for plan in plans:
461474
plan_doc = frappe.get_doc('Subscription Plan', plan.plan)
462475

@@ -471,10 +484,12 @@ def get_items_from_plans(self, plans, prorate=0):
471484

472485
if not prorate:
473486
item = {'item_code': item_code, 'qty': plan.qty, 'rate': get_plan_rate(plan.plan, plan.qty, party,
474-
self.current_invoice_start, self.current_invoice_end), 'cost_center': plan_doc.cost_center}
487+
self.current_invoice_start, self.current_invoice_end, 1, doctype, company), 'cost_center': plan_doc.cost_center}
475488
else:
476489
item = {'item_code': item_code, 'qty': plan.qty, 'rate': get_plan_rate(plan.plan, plan.qty, party,
477-
self.current_invoice_start, self.current_invoice_end, prorate_factor), 'cost_center': plan_doc.cost_center}
490+
self.current_invoice_start, self.current_invoice_end, prorate_factor, doctype, company), 'cost_center': plan_doc.cost_center}
491+
492+
478493

479494
if deferred:
480495
item.update({

erpnext/accounts/doctype/subscription_plan/subscription_plan.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from frappe import _
99
from frappe.model.document import Document
1010
from frappe.utils import date_diff, flt, get_first_day, get_last_day, getdate
11-
11+
from erpnext.stock.get_item_details import get_item_details
1212
from erpnext.utilities.product import get_price
1313

1414

@@ -21,7 +21,7 @@ def validate_interval_count(self):
2121
frappe.throw(_('Billing Interval Count cannot be less than 1'))
2222

2323
@frappe.whitelist()
24-
def get_plan_rate(plan, quantity=1, customer=None, start_date=None, end_date=None, prorate_factor=1):
24+
def get_plan_rate(plan, quantity=1, customer=None, start_date=None, end_date=None, prorate_factor=1, doctype=None, company=None):
2525
plan = frappe.get_doc("Subscription Plan", plan)
2626
if plan.price_determination == "Fixed Rate":
2727
return plan.cost * prorate_factor
@@ -32,11 +32,27 @@ def get_plan_rate(plan, quantity=1, customer=None, start_date=None, end_date=Non
3232
else:
3333
customer_group = None
3434

35-
price = get_price(item_code=plan.item, price_list=plan.price_list, customer_group=customer_group, company=None, qty=quantity)
36-
if not price:
37-
return 0
35+
if plan.currency != frappe.get_value("Price List", plan.price_list, "currency"):
36+
args = frappe._dict({
37+
'doctype': doctype,
38+
"company": company,
39+
"transaction_date": str(start_date),
40+
"currency": plan.currency,
41+
"selling_price_list": plan.price_list,
42+
"item_code": plan.item,
43+
"customer": customer
44+
})
45+
try:
46+
item_details = get_item_details(args)
47+
price = (item_details.price_list_rate * quantity) * prorate_factor
48+
except:
49+
return 0
3850
else:
39-
return price.price_list_rate * prorate_factor
51+
price = get_price(item_code=plan.item, price_list=plan.price_list, customer_group=customer_group, company=None, qty=quantity)
52+
if not price:
53+
return 0
54+
else:
55+
return price.price_list_rate * prorate_factor
4056

4157
elif plan.price_determination == 'Monthly Rate':
4258
start_date = getdate(start_date)

0 commit comments

Comments
 (0)