blob: 41b13845239a248bb16d72df7bd01be19bb50a9b [file] [log] [blame]
Rushabh Mehta156ce602015-09-11 18:49:59 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Chillar Anand915b3432021-09-02 16:44:59 +05304import frappe
Rushabh Mehta156ce602015-09-11 18:49:59 +05305from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05306
Gursheen Anandc18ff5b2024-02-02 21:59:53 +05307from erpnext.accounts.doctype.payment_request.payment_request import get_amount
8
marination9fb61ef2022-02-01 00:39:14 +05309
Rushabh Mehta156ce602015-09-11 18:49:59 +053010def get_context(context):
11 context.no_cache = 1
Kanchan Chauhan11638ba2016-04-20 16:20:49 +053012 context.show_sidebar = True
Rushabh Mehta156ce602015-09-11 18:49:59 +053013 context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
Rushabh Mehta3d766862015-09-16 18:52:52 +053014 if hasattr(context.doc, "set_indicator"):
15 context.doc.set_indicator()
16
Rushabh Mehta156ce602015-09-11 18:49:59 +053017 context.parents = frappe.form_dict.parents
Britlog4c7709e2017-07-28 15:24:22 +020018 context.title = frappe.form_dict.name
Ankush Menat494bd9e2022-03-28 18:52:46 +053019 context.payment_ref = frappe.db.get_value(
20 "Payment Request", {"reference_name": frappe.form_dict.name}, "name"
21 )
Rohit Waghchaure21499e82016-09-21 16:49:58 +053022
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 default_print_format = frappe.db.get_value(
24 "Property Setter",
25 dict(property="default_print_format", doc_type=frappe.form_dict.doctype),
26 "value",
27 )
Charles-Henri Decultote90a1ca2017-12-01 11:49:45 +010028 if default_print_format:
29 context.print_format = default_print_format
30 else:
31 context.print_format = "Standard"
32
Rushabh Mehta257bbbe2016-10-12 10:31:08 +053033 if not frappe.has_website_permission(context.doc):
Rushabh Mehta156ce602015-09-11 18:49:59 +053034 frappe.throw(_("Not Permitted"), frappe.PermissionError)
Ankush Menat4551d7d2021-08-19 13:41:10 +053035
Rohit Waghchaure7800db72023-01-23 14:21:10 +053036 context.available_loyalty_points = 0.0
37 if context.doc.get("customer"):
38 # check for the loyalty program of the customer
39 customer_loyalty_program = frappe.db.get_value(
40 "Customer", context.doc.customer, "loyalty_program"
Chillar Anand915b3432021-09-02 16:44:59 +053041 )
Ankush Menat494bd9e2022-03-28 18:52:46 +053042
Rohit Waghchaure7800db72023-01-23 14:21:10 +053043 if customer_loyalty_program:
44 from erpnext.accounts.doctype.loyalty_program.loyalty_program import (
45 get_loyalty_program_details_with_points,
46 )
Charles-Henri Decultot2de1cd32017-05-19 12:34:10 +020047
Rohit Waghchaure7800db72023-01-23 14:21:10 +053048 loyalty_program_details = get_loyalty_program_details_with_points(
49 context.doc.customer, customer_loyalty_program
50 )
51 context.available_loyalty_points = int(loyalty_program_details.get("loyalty_points"))
52
Gursheen Anandc18ff5b2024-02-02 21:59:53 +053053 context.show_pay_button, context.pay_amount = get_payment_details(context.doc)
Rohit Waghchaure7800db72023-01-23 14:21:10 +053054 context.show_make_pi_button = False
55 if context.doc.get("supplier"):
56 # show Make Purchase Invoice button based on permission
57 context.show_make_pi_button = frappe.has_permission("Purchase Invoice", "create")
Sagar Sharma80080a32022-09-28 15:40:07 +053058
Ankush Menat494bd9e2022-03-28 18:52:46 +053059
Charles-Henri Decultot2de1cd32017-05-19 12:34:10 +020060def get_attachments(dt, dn):
Ankush Menat494bd9e2022-03-28 18:52:46 +053061 return frappe.get_all(
62 "File",
63 fields=["name", "file_name", "file_url", "is_private"],
64 filters={"attached_to_name": dn, "attached_to_doctype": dt, "is_private": 0},
65 )
Gursheen Anandc18ff5b2024-02-02 21:59:53 +053066
67
68def get_payment_details(doc):
69 show_pay_button, amount = (
70 "payments" in frappe.get_installed_apps()
71 and frappe.db.get_single_value("Buying Settings", "show_pay_button")
72 ), 0
73 if not show_pay_button:
74 return show_pay_button, amount
75 amount = get_amount(doc)
76 return bool(amount), amount