fix: create custom pos fields
diff --git a/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_custom_fields.py b/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_custom_fields.py
new file mode 100644
index 0000000..f3410e1
--- /dev/null
+++ b/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_custom_fields.py
@@ -0,0 +1,44 @@
+import frappe
+from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
+
+def create_custom_pos_fields():
+ """
+ Create custom fields corresponding to POS Settings and POS Invoice
+ """
+ pos_field = {
+ "POS Invoice": [
+ {
+ "fieldname": "request_for_payment",
+ "label": "Request for Payment",
+ "fieldtype": "Button",
+ "hidden": 1,
+ "insert_after": "contact_email"
+ },
+ ]
+ }
+ if not frappe.get_meta("POS Invoice").has_field("request_for_payment"):
+ create_custom_fields(pos_field)
+
+ record_dict = [{
+ "doctype": "POS Field",
+ "fieldname": "contact_mobile",
+ "label": "Mobile No",
+ "fieldtype": "Data",
+ "options": "Phone",
+ "parent": "POS Settings"
+ },
+ {
+ "doctype": "POS Field",
+ "fieldname": "request_for_payment",
+ "label": "Request for Payment",
+ "fieldtype": "Button",
+ "parent": "POS Settings"
+ }
+ ]
+ create_pos_settings(record_dict)
+
+def create_pos_settings(record_dict):
+ for record in record_dict:
+ if frappe.db.exists("POS Field", {"fieldname": record.get("fieldname")}):
+ continue
+ frappe.get_doc(record).insert()
\ No newline at end of file
diff --git a/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_settings.py b/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_settings.py
index de5df1f..fb48cb5 100644
--- a/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_settings.py
+++ b/erpnext/erpnext_integrations/doctype/mpesa_settings/mpesa_settings.py
@@ -15,15 +15,21 @@
from frappe.integrations.utils import create_request_log, create_payment_gateway
from frappe.utils import get_request_site_address
from frappe.utils.password import get_decrypted_password
+from erpnext.erpnext_integrations.utils import create_mode_of_payment
from erpnext.erpnext_integrations.doctype.mpesa_settings.mpesa_connector import MpesaConnector
+from erpnext.erpnext_integrations.doctype.mpesa_settings.mpesa_custom_fields import create_custom_pos_fields
class MpesaSettings(Document):
supported_currencies = ["KSh"]
def validate(self):
create_payment_gateway('Mpesa-' + self.payment_gateway_name, settings='Mpesa Settings', controller=self.payment_gateway_name)
+ create_mode_of_payment('Mpesa-' + self.payment_gateway_name)
call_hook_method('payment_gateway_enabled', gateway='Mpesa-' + self.payment_gateway_name)
def validate_transaction_currency(self, currency):
if currency not in self.supported_currencies:
frappe.throw(_("Please select another payment method. Mpesa does not support transactions in currency '{0}'").format(currency))
+
+ def on_update(self):
+ create_custom_pos_fields()
\ No newline at end of file
diff --git a/erpnext/erpnext_integrations/utils.py b/erpnext/erpnext_integrations/utils.py
index 84f7f5a..78a5fce 100644
--- a/erpnext/erpnext_integrations/utils.py
+++ b/erpnext/erpnext_integrations/utils.py
@@ -3,6 +3,7 @@
from frappe import _
import base64, hashlib, hmac
from six.moves.urllib.parse import urlparse
+from erpnext import get_default_company
def validate_webhooks_request(doctype, hmac_key, secret_key='secret'):
def innerfn(fn):
@@ -41,3 +42,22 @@
server_url = '{uri.scheme}://{uri.netloc}/api/method/{endpoint}'.format(uri=urlparse(url), endpoint=endpoint)
return server_url
+
+def create_mode_of_payment(gateway):
+ payment_gateway_account = frappe.db.get_value("Payment Gateway Account", {
+ "payment_gateway": gateway
+ }, ['payment_account'])
+
+ if not frappe.db.exists("Mode of Payment", gateway) and payment_gateway_account:
+ mode_of_payment = frappe.get_doc({
+ "doctype": "Mode of Payment",
+ "mode_of_payment": gateway,
+ "enabled": 1,
+ "type": "General",
+ "account": {
+ "doctype": "Mode of Payment Account",
+ "company": get_default_company(),
+ "default_account": payment_gateway_account
+ }
+ })
+ mode_of_payment.insert(ignore_permissions=True)
\ No newline at end of file