Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 5 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | |
Gursheen Anand | c18ff5b | 2024-02-02 21:59:53 +0530 | [diff] [blame] | 7 | from erpnext.accounts.doctype.payment_request.payment_request import get_amount |
| 8 | |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 10 | def get_context(context): |
| 11 | context.no_cache = 1 |
Kanchan Chauhan | 11638ba | 2016-04-20 16:20:49 +0530 | [diff] [blame] | 12 | context.show_sidebar = True |
Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 13 | context.doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name) |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 14 | if hasattr(context.doc, "set_indicator"): |
| 15 | context.doc.set_indicator() |
| 16 | |
Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 17 | context.parents = frappe.form_dict.parents |
Britlog | 4c7709e | 2017-07-28 15:24:22 +0200 | [diff] [blame] | 18 | context.title = frappe.form_dict.name |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | context.payment_ref = frappe.db.get_value( |
| 20 | "Payment Request", {"reference_name": frappe.form_dict.name}, "name" |
| 21 | ) |
Rohit Waghchaure | 21499e8 | 2016-09-21 16:49:58 +0530 | [diff] [blame] | 22 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | 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 Decultot | e90a1ca | 2017-12-01 11:49:45 +0100 | [diff] [blame] | 28 | if default_print_format: |
| 29 | context.print_format = default_print_format |
| 30 | else: |
| 31 | context.print_format = "Standard" |
| 32 | |
Rushabh Mehta | 257bbbe | 2016-10-12 10:31:08 +0530 | [diff] [blame] | 33 | if not frappe.has_website_permission(context.doc): |
Rushabh Mehta | 156ce60 | 2015-09-11 18:49:59 +0530 | [diff] [blame] | 34 | frappe.throw(_("Not Permitted"), frappe.PermissionError) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 35 | |
Rohit Waghchaure | 7800db7 | 2023-01-23 14:21:10 +0530 | [diff] [blame] | 36 | context.available_loyalty_points = 0.0 |
| 37 | if context.doc.get("customer"): |
| 38 | # check for the loyalty program of the customer |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame^] | 39 | customer_loyalty_program = frappe.db.get_value("Customer", context.doc.customer, "loyalty_program") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | |
Rohit Waghchaure | 7800db7 | 2023-01-23 14:21:10 +0530 | [diff] [blame] | 41 | if customer_loyalty_program: |
| 42 | from erpnext.accounts.doctype.loyalty_program.loyalty_program import ( |
| 43 | get_loyalty_program_details_with_points, |
| 44 | ) |
Charles-Henri Decultot | 2de1cd3 | 2017-05-19 12:34:10 +0200 | [diff] [blame] | 45 | |
Rohit Waghchaure | 7800db7 | 2023-01-23 14:21:10 +0530 | [diff] [blame] | 46 | loyalty_program_details = get_loyalty_program_details_with_points( |
| 47 | context.doc.customer, customer_loyalty_program |
| 48 | ) |
| 49 | context.available_loyalty_points = int(loyalty_program_details.get("loyalty_points")) |
| 50 | |
Gursheen Anand | c18ff5b | 2024-02-02 21:59:53 +0530 | [diff] [blame] | 51 | context.show_pay_button, context.pay_amount = get_payment_details(context.doc) |
Rohit Waghchaure | 7800db7 | 2023-01-23 14:21:10 +0530 | [diff] [blame] | 52 | context.show_make_pi_button = False |
| 53 | if context.doc.get("supplier"): |
| 54 | # show Make Purchase Invoice button based on permission |
| 55 | context.show_make_pi_button = frappe.has_permission("Purchase Invoice", "create") |
Sagar Sharma | 80080a3 | 2022-09-28 15:40:07 +0530 | [diff] [blame] | 56 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | |
Charles-Henri Decultot | 2de1cd3 | 2017-05-19 12:34:10 +0200 | [diff] [blame] | 58 | def get_attachments(dt, dn): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | return frappe.get_all( |
| 60 | "File", |
| 61 | fields=["name", "file_name", "file_url", "is_private"], |
| 62 | filters={"attached_to_name": dn, "attached_to_doctype": dt, "is_private": 0}, |
| 63 | ) |
Gursheen Anand | c18ff5b | 2024-02-02 21:59:53 +0530 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def get_payment_details(doc): |
| 67 | show_pay_button, amount = ( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame^] | 68 | ( |
| 69 | "payments" in frappe.get_installed_apps() |
| 70 | and frappe.db.get_single_value("Buying Settings", "show_pay_button") |
| 71 | ), |
| 72 | 0, |
| 73 | ) |
Gursheen Anand | c18ff5b | 2024-02-02 21:59:53 +0530 | [diff] [blame] | 74 | if not show_pay_button: |
| 75 | return show_pay_button, amount |
| 76 | amount = get_amount(doc) |
| 77 | return bool(amount), amount |