Corrections following Saurabh's comments
This commit is contained in:
parent
90669a03b3
commit
a0b7236f66
@ -55,11 +55,16 @@ frappe.ui.form.on("Payment Request", "is_a_subscription", function(frm) {
|
||||
|
||||
if (frm.doc.is_a_subscription) {
|
||||
frappe.call({
|
||||
method: "get_subscription_details",
|
||||
doc: frm.doc,
|
||||
method: "erpnext.accounts.doctype.payment_request.payment_request.get_subscription_details",
|
||||
args: {"reference_doctype": frm.doc.reference_doctype, "reference_name": frm.doc.reference_name},
|
||||
freeze: true,
|
||||
callback: function(r){
|
||||
if(!r.exc) {
|
||||
callback: function(data){
|
||||
if(!data.exc) {
|
||||
$.each(data.message || [], function(i, v){
|
||||
var d = frappe.model.add_child(frm.doc, "Subscription Plan Detail", "subscription_plans");
|
||||
d.qty = v.qty;
|
||||
d.plan = v.plan;
|
||||
});
|
||||
frm.refresh_field("subscription_plans");
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,14 @@ from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_ent
|
||||
from frappe.integrations.utils import get_payment_gateway_controller
|
||||
from frappe.utils.background_jobs import enqueue
|
||||
from erpnext.erpnext_integrations.stripe_integration import create_stripe_subscription
|
||||
from erpnext.accounts.doctype.subscription_plan.subscription_plan import get_plan_rate
|
||||
|
||||
class PaymentRequest(Document):
|
||||
def validate(self):
|
||||
self.validate_reference_document()
|
||||
self.validate_payment_request()
|
||||
self.validate_currency()
|
||||
self.validate_subscription_details()
|
||||
|
||||
def validate_reference_document(self):
|
||||
if not self.reference_doctype or not self.reference_name:
|
||||
@ -34,18 +36,15 @@ class PaymentRequest(Document):
|
||||
if self.payment_account and ref_doc.currency != frappe.db.get_value("Account", self.payment_account, "account_currency"):
|
||||
frappe.throw(_("Transaction currency must be same as Payment Gateway currency"))
|
||||
|
||||
def on_update(self):
|
||||
self.validate_subscription_details()
|
||||
|
||||
def validate_subscription_details(self):
|
||||
if self.is_a_subscription:
|
||||
amount = 0
|
||||
for subscription_plan in self.subscription_plans:
|
||||
plan = frappe.get_doc("Subscription Plan", subscription_plan.plan)
|
||||
if plan.payment_gateway != self.payment_gateway_account:
|
||||
payment_gateway = frappe.db.get_value("Subscription Plan", subscription_plan.plan, "payment_gateway")
|
||||
if payment_gateway != self.payment_gateway_account:
|
||||
frappe.throw(_('The payment gateway account in plan {0} is different from the payment gateway account in this payment request'.format(plan.name)))
|
||||
|
||||
rate = plan.get_plan_rate()
|
||||
rate = get_plan_rate(subscription_plan.plan, quantity=subscription_plan.qty)
|
||||
|
||||
amount += rate
|
||||
|
||||
@ -258,15 +257,6 @@ class PaymentRequest(Document):
|
||||
if payment_provider == "stripe":
|
||||
return create_stripe_subscription(gateway_controller, data)
|
||||
|
||||
def get_subscription_details(self):
|
||||
if self.reference_doctype == "Sales Invoice":
|
||||
subscriptions = frappe.db.sql("""SELECT parent as sub_name FROM `tabSubscription Invoice` WHERE invoice=%s""", self.reference_name, as_dict=1)
|
||||
self.subscription_plans = []
|
||||
for subscription in subscriptions:
|
||||
plans = frappe.get_doc("Subscription", subscription.sub_name).plans
|
||||
for plan in plans:
|
||||
self.append('subscription_plans', plan)
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def make_payment_request(**args):
|
||||
"""Make payment request"""
|
||||
@ -407,3 +397,14 @@ def get_dummy_message(doc):
|
||||
|
||||
<p>{{ _("Thank you for your business!") }}</p>
|
||||
""", dict(doc=doc, payment_url = '{{ payment_url }}'))
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_subscription_details(reference_doctype, reference_name):
|
||||
if reference_doctype == "Sales Invoice":
|
||||
subscriptions = frappe.db.sql("""SELECT parent as sub_name FROM `tabSubscription Invoice` WHERE invoice=%s""",reference_name, as_dict=1)
|
||||
subscription_plans = []
|
||||
for subscription in subscriptions:
|
||||
plans = frappe.get_doc("Subscription", subscription.sub_name).plans
|
||||
for plan in plans:
|
||||
subscription_plans.append(plan)
|
||||
return subscription_plans
|
@ -8,6 +8,7 @@ import frappe
|
||||
from frappe import _
|
||||
from frappe.model.document import Document
|
||||
from frappe.utils.data import nowdate, getdate, cint, add_days, date_diff, get_last_day, add_to_date, flt
|
||||
from erpnext.accounts.doctype.subscription_plan.subscription_plan import get_plan_rate
|
||||
|
||||
|
||||
class Subscription(Document):
|
||||
@ -298,11 +299,11 @@ class Subscription(Document):
|
||||
items = []
|
||||
customer = self.get_customer(self.subscriber)
|
||||
for plan in plans:
|
||||
subscription_plan = frappe.get_doc("Subscription Plan", plan.plan)
|
||||
item_code = frappe.db.get_value("Subscription Plan", plan.plan, "item")
|
||||
if not prorate:
|
||||
items.append({'item_code': subscription_plan.item, 'qty': plan.qty, 'rate': subscription_plan.get_plan_rate(customer)})
|
||||
items.append({'item_code': item_code, 'qty': plan.qty, 'rate': get_plan_rate(plan.plan, plan.qty, customer)})
|
||||
else:
|
||||
items.append({'item_code': subscription_plan.item, 'qty': plan.qty, 'rate': (subscription_plan.get_plan_rate(customer) * prorate_factor)})
|
||||
items.append({'item_code': item_code, 'qty': plan.qty, 'rate': (get_plan_rate(plan.plan, plan.qty, customer) * prorate_factor)})
|
||||
|
||||
return items
|
||||
|
||||
|
@ -15,14 +15,20 @@ class SubscriptionPlan(Document):
|
||||
if self.billing_interval_count < 1:
|
||||
frappe.throw('Billing Interval Count cannot be less than 1')
|
||||
|
||||
def get_plan_rate(self, quantity=1, customer=None):
|
||||
if self.price_determination == "Fixed rate":
|
||||
return self.cost
|
||||
@frappe.whitelist()
|
||||
def get_plan_rate(plan, quantity=1, customer=None):
|
||||
plan = frappe.get_doc("Subscription Plan", plan)
|
||||
if plan.price_determination == "Fixed rate":
|
||||
return plan.cost
|
||||
|
||||
elif self.price_determination == "Based on price list":
|
||||
if customer:
|
||||
customer_group = frappe.db.get_value("Customer", customer, "customer_group")
|
||||
else:
|
||||
customer_group = None
|
||||
|
||||
return get_price(item_code=self.item, price_list=self.price_list, customer_group=customer_group, company=None, qty=quantity).price_list_rate
|
||||
elif plan.price_determination == "Based on price list":
|
||||
if customer:
|
||||
customer_group = frappe.db.get_value("Customer", customer, "customer_group")
|
||||
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
|
||||
else:
|
||||
return price.price_list_rate
|
||||
|
Loading…
Reference in New Issue
Block a user