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) {
|
if (frm.doc.is_a_subscription) {
|
||||||
frappe.call({
|
frappe.call({
|
||||||
method: "get_subscription_details",
|
method: "erpnext.accounts.doctype.payment_request.payment_request.get_subscription_details",
|
||||||
doc: frm.doc,
|
args: {"reference_doctype": frm.doc.reference_doctype, "reference_name": frm.doc.reference_name},
|
||||||
freeze: true,
|
freeze: true,
|
||||||
callback: function(r){
|
callback: function(data){
|
||||||
if(!r.exc) {
|
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");
|
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.integrations.utils import get_payment_gateway_controller
|
||||||
from frappe.utils.background_jobs import enqueue
|
from frappe.utils.background_jobs import enqueue
|
||||||
from erpnext.erpnext_integrations.stripe_integration import create_stripe_subscription
|
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):
|
class PaymentRequest(Document):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.validate_reference_document()
|
self.validate_reference_document()
|
||||||
self.validate_payment_request()
|
self.validate_payment_request()
|
||||||
self.validate_currency()
|
self.validate_currency()
|
||||||
|
self.validate_subscription_details()
|
||||||
|
|
||||||
def validate_reference_document(self):
|
def validate_reference_document(self):
|
||||||
if not self.reference_doctype or not self.reference_name:
|
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"):
|
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"))
|
frappe.throw(_("Transaction currency must be same as Payment Gateway currency"))
|
||||||
|
|
||||||
def on_update(self):
|
|
||||||
self.validate_subscription_details()
|
|
||||||
|
|
||||||
def validate_subscription_details(self):
|
def validate_subscription_details(self):
|
||||||
if self.is_a_subscription:
|
if self.is_a_subscription:
|
||||||
amount = 0
|
amount = 0
|
||||||
for subscription_plan in self.subscription_plans:
|
for subscription_plan in self.subscription_plans:
|
||||||
plan = frappe.get_doc("Subscription Plan", subscription_plan.plan)
|
payment_gateway = frappe.db.get_value("Subscription Plan", subscription_plan.plan, "payment_gateway")
|
||||||
if plan.payment_gateway != self.payment_gateway_account:
|
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)))
|
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
|
amount += rate
|
||||||
|
|
||||||
@ -258,15 +257,6 @@ class PaymentRequest(Document):
|
|||||||
if payment_provider == "stripe":
|
if payment_provider == "stripe":
|
||||||
return create_stripe_subscription(gateway_controller, data)
|
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)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def make_payment_request(**args):
|
def make_payment_request(**args):
|
||||||
"""Make payment request"""
|
"""Make payment request"""
|
||||||
@ -407,3 +397,14 @@ def get_dummy_message(doc):
|
|||||||
|
|
||||||
<p>{{ _("Thank you for your business!") }}</p>
|
<p>{{ _("Thank you for your business!") }}</p>
|
||||||
""", dict(doc=doc, payment_url = '{{ payment_url }}'))
|
""", 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 import _
|
||||||
from frappe.model.document import Document
|
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 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):
|
class Subscription(Document):
|
||||||
@ -298,11 +299,11 @@ class Subscription(Document):
|
|||||||
items = []
|
items = []
|
||||||
customer = self.get_customer(self.subscriber)
|
customer = self.get_customer(self.subscriber)
|
||||||
for plan in plans:
|
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:
|
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:
|
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
|
return items
|
||||||
|
|
||||||
|
@ -15,14 +15,20 @@ class SubscriptionPlan(Document):
|
|||||||
if self.billing_interval_count < 1:
|
if self.billing_interval_count < 1:
|
||||||
frappe.throw('Billing Interval Count cannot be less than 1')
|
frappe.throw('Billing Interval Count cannot be less than 1')
|
||||||
|
|
||||||
def get_plan_rate(self, quantity=1, customer=None):
|
@frappe.whitelist()
|
||||||
if self.price_determination == "Fixed rate":
|
def get_plan_rate(plan, quantity=1, customer=None):
|
||||||
return self.cost
|
plan = frappe.get_doc("Subscription Plan", plan)
|
||||||
|
if plan.price_determination == "Fixed rate":
|
||||||
|
return plan.cost
|
||||||
|
|
||||||
elif self.price_determination == "Based on price list":
|
elif plan.price_determination == "Based on price list":
|
||||||
if customer:
|
if customer:
|
||||||
customer_group = frappe.db.get_value("Customer", customer, "customer_group")
|
customer_group = frappe.db.get_value("Customer", customer, "customer_group")
|
||||||
else:
|
else:
|
||||||
customer_group = None
|
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
|
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…
x
Reference in New Issue
Block a user