Skip to content

Commit

Permalink
Merge pull request #877 from fproldan/backport-874-to-diamoerp-staging
Browse files Browse the repository at this point in the history
[Backport diamoerp-staging] La factura no obtiene el precio correcto en la moneda de la empresa desde una suscripción con lista de precios en moneda extranjera.
  • Loading branch information
fproldan authored Oct 4, 2024
2 parents 2827aa2 + a31dcae commit 7a82c80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
35 changes: 25 additions & 10 deletions erpnext/accounts/doctype/subscription/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ def validate(self):
self.validate_end_date()
self.validate_to_follow_calendar_months()
self.cost_center = erpnext.get_default_cost_center(self.get('company'))
self.validate_plans_price_lists()

def validate_plans_price_lists(self):
if len(self.plans) <= 1:
return

price_list = [frappe.get_value("Subscription Plan", plan.plan, 'price_list') for plan in self.plans]
price_list = list(set(list(filter(lambda x: x is not None, price_list))))
if len(price_list) > 1:
frappe.throw(_("Solo se pueden tener Planes con la misma Lista de Precio en una Suscripción."))

def validate_trial_period(self):
"""
Expand Down Expand Up @@ -341,14 +351,13 @@ def create_invoice(self, prorate):
invoice = frappe.new_doc(doctype)
invoice.subscription = self.name

# Si solo tiene un plan aplicamos esa lista de precios a la factura
if len(self.plans) == 1:
price_list = frappe.get_value("Subscription Plan", self.plans[0].plan, 'price_list')
if price_list:
if doctype == "Sales Invoice":
invoice.selling_price_list = price_list
else:
invoice.buying_price_list = price_list
# Aplicamos esa lista de precios a la factura
price_list = frappe.get_value("Subscription Plan", self.plans[0].plan, 'price_list')
if price_list:
if doctype == "Sales Invoice":
invoice.selling_price_list = price_list
else:
invoice.buying_price_list = price_list

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

items = []
party = self.party

doctype = "Sales Invoice" if self.party_type == "Customer" else "Purchase Invoice"
company = self.company

for plan in plans:
plan_doc = frappe.get_doc('Subscription Plan', plan.plan)

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

if not prorate:
item = {'item_code': item_code, 'qty': plan.qty, 'rate': get_plan_rate(plan.plan, plan.qty, party,
self.current_invoice_start, self.current_invoice_end), 'cost_center': plan_doc.cost_center}
self.current_invoice_start, self.current_invoice_end, 1, doctype, company), 'cost_center': plan_doc.cost_center}
else:
item = {'item_code': item_code, 'qty': plan.qty, 'rate': get_plan_rate(plan.plan, plan.qty, party,
self.current_invoice_start, self.current_invoice_end, prorate_factor), 'cost_center': plan_doc.cost_center}
self.current_invoice_start, self.current_invoice_end, prorate_factor, doctype, company), 'cost_center': plan_doc.cost_center}



if deferred:
item.update({
Expand Down
28 changes: 22 additions & 6 deletions erpnext/accounts/doctype/subscription_plan/subscription_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from frappe import _
from frappe.model.document import Document
from frappe.utils import date_diff, flt, get_first_day, get_last_day, getdate

from erpnext.stock.get_item_details import get_item_details
from erpnext.utilities.product import get_price


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

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

price = get_price(item_code=plan.item, price_list=plan.price_list, customer_group=customer_group, company=None, qty=quantity)
if not price:
return 0
if plan.currency != frappe.get_value("Price List", plan.price_list, "currency"):
args = frappe._dict({
'doctype': doctype,
"company": company,
"transaction_date": str(start_date),
"currency": plan.currency,
"selling_price_list": plan.price_list,
"item_code": plan.item,
"customer": customer
})
try:
item_details = get_item_details(args)
price = (item_details.price_list_rate * quantity) * prorate_factor
except:
return 0
else:
return price.price_list_rate * prorate_factor
price = get_price(item_code=plan.item, price_list=plan.price_list, customer_group=customer_group, company=None, qty=quantity)
if not price:
return 0
else:
return price.price_list_rate * prorate_factor

elif plan.price_determination == 'Monthly Rate':
start_date = getdate(start_date)
Expand Down

0 comments on commit 7a82c80

Please sign in to comment.