Skip to content

Commit 3dacadd

Browse files
authored
Merge pull request #27 from kainotomo/bug/user-permissions
2 parents 46ce4ad + a860cee commit 3dacadd

File tree

10 files changed

+180
-73
lines changed

10 files changed

+180
-73
lines changed

invoice2erpnext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
__version__ = '2.3.0'
2+
__version__ = '2.3.1'
33

invoice2erpnext/invoice2erpnext/doctype/invoice2erpnext_log/invoice2erpnext_log.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
],
9898
"index_web_pages_for_search": 1,
9999
"links": [],
100-
"modified": "2025-04-01 09:13:06.986016",
100+
"modified": "2025-04-07 09:35:29.661482",
101101
"modified_by": "Administrator",
102102
"module": "Invoice2Erpnext",
103103
"name": "Invoice2Erpnext Log",
@@ -114,6 +114,29 @@
114114
"role": "System Manager",
115115
"share": 1,
116116
"write": 1
117+
},
118+
{
119+
"create": 1,
120+
"email": 1,
121+
"export": 1,
122+
"print": 1,
123+
"read": 1,
124+
"report": 1,
125+
"role": "Accounts Manager",
126+
"share": 1,
127+
"write": 1
128+
},
129+
{
130+
"create": 1,
131+
"delete": 1,
132+
"email": 1,
133+
"export": 1,
134+
"print": 1,
135+
"read": 1,
136+
"report": 1,
137+
"role": "Accounts User",
138+
"share": 1,
139+
"write": 1
117140
}
118141
],
119142
"sort_field": "modified",

invoice2erpnext/invoice2erpnext/doctype/invoice2erpnext_settings/invoice2erpnext_settings.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,48 @@ frappe.ui.form.on('Invoice2Erpnext Settings', {
2828
});
2929
});
3030

31+
// Populate the available_credits HTML field
32+
frappe.call({
33+
method: "invoice2erpnext.invoice2erpnext.doctype.invoice2erpnext_settings.invoice2erpnext_settings.get_available_credits",
34+
callback: function(r) {
35+
if (r.message && r.message.value !== undefined) {
36+
// Get credits value
37+
let credits = r.message.value;
38+
39+
// Format manually without using frappe.format for currency
40+
let currencySymbol = frappe.boot.sysdefaults.currency_symbol || '€';
41+
let formattedValue = parseFloat(credits).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
42+
let formattedCredits = currencySymbol + ' ' + formattedValue;
43+
44+
// Create a clean, simple HTML structure
45+
let html = `
46+
<div style="padding: 15px 0;">
47+
<div style="font-size: 24px; font-weight: 600; color: #2490EF; text-align: left;">${formattedCredits}</div>
48+
<div style="margin-top: 15px;">
49+
<a href="https://kainotomo.com/invoice2erpnext/shop" target="_blank"
50+
class="btn btn-primary btn-sm">Buy Credits</a>
51+
</div>
52+
</div>
53+
`;
54+
55+
// Set the HTML in the available_credits field
56+
$(frm.fields_dict.available_credits.wrapper).html(html);
57+
} else {
58+
// Handle error or zero credits case
59+
let currencySymbol = frappe.boot.sysdefaults.currency_symbol || '€';
60+
let formattedCredits = currencySymbol + ' 0.00';
61+
62+
$(frm.fields_dict.available_credits.wrapper).html(`
63+
<div style="padding: 15px 0;">
64+
<div style="font-size: 24px; font-weight: 600; color: #8D99A6; text-align: left;">${formattedCredits}</div>
65+
<div style="margin-top: 15px;">
66+
<a href="https://kainotomo.com/invoice2erpnext/shop" target="_blank"
67+
class="btn btn-primary btn-sm">Buy Credits</a>
68+
</div>
69+
</div>
70+
`);
71+
}
72+
}
73+
});
3174
},
3275
});

invoice2erpnext/invoice2erpnext/doctype/invoice2erpnext_settings/invoice2erpnext_settings.json

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"doctype": "DocType",
66
"engine": "InnoDB",
77
"field_order": [
8+
"credits_section",
9+
"column_break_ttnt",
10+
"available_credits",
11+
"column_break_hyuy",
812
"api_credentials_section",
913
"api_key",
1014
"enabled",
@@ -99,12 +103,30 @@
99103
"fieldname": "defaults_section_section",
100104
"fieldtype": "Section Break",
101105
"label": "Defaults"
106+
},
107+
{
108+
"fieldname": "credits_section",
109+
"fieldtype": "Section Break",
110+
"label": "Credits"
111+
},
112+
{
113+
"fieldname": "available_credits",
114+
"fieldtype": "HTML",
115+
"label": "Available Credits"
116+
},
117+
{
118+
"fieldname": "column_break_ttnt",
119+
"fieldtype": "Column Break"
120+
},
121+
{
122+
"fieldname": "column_break_hyuy",
123+
"fieldtype": "Column Break"
102124
}
103125
],
104126
"index_web_pages_for_search": 1,
105127
"issingle": 1,
106128
"links": [],
107-
"modified": "2025-04-05 16:43:36.071064",
129+
"modified": "2025-04-07 10:25:54.056689",
108130
"modified_by": "Administrator",
109131
"module": "Invoice2Erpnext",
110132
"name": "Invoice2Erpnext Settings",
@@ -119,6 +141,21 @@
119141
"role": "System Manager",
120142
"share": 1,
121143
"write": 1
144+
},
145+
{
146+
"email": 1,
147+
"print": 1,
148+
"read": 1,
149+
"role": "Accounts Manager",
150+
"share": 1,
151+
"write": 1
152+
},
153+
{
154+
"email": 1,
155+
"print": 1,
156+
"read": 1,
157+
"role": "Accounts User",
158+
"share": 1
122159
}
123160
],
124161
"sort_field": "modified",

invoice2erpnext/invoice2erpnext/doctype/invoice2erpnext_settings/invoice2erpnext_settings.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,53 @@ def test_connection(self):
9797

9898
self.save()
9999

100-
return result
100+
return result
101+
102+
103+
# Add a global function that doesn't require document permissions
104+
@frappe.whitelist(allow_guest=False)
105+
def get_available_credits():
106+
"""Get available credits - accessible to all authenticated users"""
107+
try:
108+
# Check if settings exists - don't need document permissions for this check
109+
if not frappe.db.exists("Invoice2Erpnext Settings", "Invoice2Erpnext Settings"):
110+
return {
111+
"value": 0,
112+
"fieldtype": "Currency",
113+
}
114+
115+
# Create a new instance without checking permissions
116+
settings = frappe.new_doc("Invoice2Erpnext Settings")
117+
settings.flags.ignore_permissions = True
118+
119+
# Load the document values from DB directly
120+
settings_dict = frappe.db.get_singles_dict("Invoice2Erpnext Settings")
121+
for key, value in settings_dict.items():
122+
if key != "doctype":
123+
settings.set(key, value)
124+
125+
# Get credentials explicitly from DB
126+
for field in ["api_key", "api_secret"]:
127+
value = frappe.db.get_value("__Auth", {"doctype": "Invoice2Erpnext Settings", "fieldname": field}, "password")
128+
if value:
129+
settings.set(field, value)
130+
131+
# Get credits using the instance method
132+
result = settings.get_credits()
133+
134+
# Extract credits from result if successful
135+
credits = 0
136+
if result.get("success") and "credits" in result:
137+
credits = result["credits"]
138+
139+
# Return formatted response for number card
140+
return {
141+
"value": credits,
142+
"fieldtype": "Currency",
143+
}
144+
except Exception as e:
145+
frappe.log_error(f"Error fetching credits for all users: {str(e)}", "Invoice2Erpnext Credits")
146+
return {
147+
"value": 0,
148+
"fieldtype": "Currency",
149+
}

invoice2erpnext/invoice2erpnext/number_card/available_credits/available_credits.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

invoice2erpnext/invoice2erpnext/number_card/available_credits/available_credits.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

invoice2erpnext/invoice2erpnext/workspace/invoice2erpnext/invoice2erpnext.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"charts": [],
3-
"content": "[{\"id\":\"sxJmXGFSt6\",\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\">Invoice2Erpnext</span>\",\"col\":12}},{\"id\":\"3sCUU4rAB6\",\"type\":\"number_card\",\"data\":{\"number_card_name\":\"Available Credits\",\"col\":4}},{\"id\":\"rAHLFl3j3O\",\"type\":\"paragraph\",\"data\":{\"text\":\"<a href=\\\"https://kainotomo.com/invoice2erpnext/shop\\\" class=\\\"btn btn-lg btn-primary\\\" target=\\\"_blank\\\">Purchase Credits</a>\",\"col\":12}},{\"id\":\"w1IJ8kd86B\",\"type\":\"spacer\",\"data\":{\"col\":12}},{\"id\":\"jhv7lDXtTh\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Logs\",\"col\":3}},{\"id\":\"1AWrWcS5a1\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Settings\",\"col\":3}}]",
3+
"content": "[{\"id\":\"sxJmXGFSt6\",\"type\":\"header\",\"data\":{\"text\":\"<span class=\\\"h4\\\">Invoice2Erpnext</span>\",\"col\":12}},{\"id\":\"jhv7lDXtTh\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Logs\",\"col\":3}},{\"id\":\"1AWrWcS5a1\",\"type\":\"shortcut\",\"data\":{\"shortcut_name\":\"Settings\",\"col\":3}}]",
44
"creation": "2023-09-02 07:58:22.131565",
55
"custom_blocks": [],
66
"docstatus": 0,
@@ -12,16 +12,11 @@
1212
"is_hidden": 0,
1313
"label": "Invoice2Erpnext",
1414
"links": [],
15-
"modified": "2025-04-04 07:23:43.673812",
15+
"modified": "2025-04-07 10:32:54.850098",
1616
"modified_by": "Administrator",
1717
"module": "Invoice2Erpnext",
1818
"name": "Invoice2Erpnext",
19-
"number_cards": [
20-
{
21-
"label": "Available Credits",
22-
"number_card_name": "Available Credits"
23-
}
24-
],
19+
"number_cards": [],
2520
"owner": "Administrator",
2621
"parent_page": "",
2722
"public": 1,

invoice2erpnext/public/js/purchase_invoice_list.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
frappe.listview_settings['Purchase Invoice'] = {
22
onload: function(listview) {
33
// Check if integration is enabled before showing the upload button
4-
frappe.call({
5-
method: 'frappe.client.get_single_value',
6-
args: {
7-
doctype: 'Invoice2Erpnext Settings',
8-
field: 'enabled'
9-
},
10-
callback: function(r) {
11-
if (r.message === 1) {
4+
frappe.xcall('invoice2erpnext.utils.check_settings_enabled')
5+
.then(enabled => {
6+
if (enabled) {
127
// Add the upload option under a dropdown menu
138
listview.page.add_menu_item(__('Upload (Auto)'), function() {
149
new frappe.ui.FileUploader({
@@ -29,8 +24,10 @@ frappe.listview_settings['Purchase Invoice'] = {
2924
});
3025
});
3126
}
32-
}
33-
});
27+
})
28+
.catch(() => {
29+
// Silently fail - don't show buttons if there's an error
30+
});
3431
}
3532
};
3633

invoice2erpnext/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,17 @@ def format_currency_value(value):
4949
# Default format if none of the above
5050
formatted_value = value_str
5151

52-
return formatted_value
52+
return formatted_value
53+
54+
@frappe.whitelist()
55+
def check_settings_enabled():
56+
"""Safely check if Invoice2Erpnext is enabled without permission errors"""
57+
# First check if user has permission to read the settings
58+
if not frappe.has_permission("Invoice2Erpnext Settings", "read"):
59+
return 0
60+
61+
try:
62+
return frappe.db.get_single_value('Invoice2Erpnext Settings', 'enabled')
63+
except:
64+
# Return 0 (disabled) if any error occurs
65+
return 0

0 commit comments

Comments
 (0)