From b5097ec161bdd3fcf6d83c6ec4eb0d62a7f3c231 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Wed, 1 Mar 2017 01:13:09 +0530 Subject: [PATCH] offline email for POS --- erpnext/accounts/doctype/sales_invoice/pos.py | 34 ++++++-- erpnext/accounts/page/pos/pos.js | 83 +++++++++++++++++-- erpnext/public/js/pos/pos.html | 8 +- erpnext/public/js/pos/pos_selected_item.html | 1 - 4 files changed, 110 insertions(+), 16 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py index 070dbeb2b8..476ce45a1a 100644 --- a/erpnext/accounts/doctype/sales_invoice/pos.py +++ b/erpnext/accounts/doctype/sales_invoice/pos.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import frappe, json from frappe.utils import nowdate from erpnext.setup.utils import get_exchange_rate +from frappe.core.doctype.communication.email import make from erpnext.stock.get_item_details import get_pos_profile from erpnext.accounts.party import get_party_account_currency from erpnext.controllers.accounts_controller import get_taxes_and_charges @@ -257,12 +258,14 @@ def get_pricing_rule_data(doc): return pricing_rules @frappe.whitelist() -def make_invoice(doc_list): +def make_invoice(doc_list, email_queue_list): if isinstance(doc_list, basestring): doc_list = json.loads(doc_list) - name_list = [] + if isinstance(email_queue_list, basestring): + email_queue = json.loads(email_queue_list) + name_list = [] for docs in doc_list: for name, doc in docs.items(): if not frappe.db.exists('Sales Invoice', {'offline_pos_name': name}): @@ -270,17 +273,37 @@ def make_invoice(doc_list): si_doc = frappe.new_doc('Sales Invoice') si_doc.offline_pos_name = name si_doc.update(doc) - submit_invoice(si_doc, name) + submit_invoice(si_doc, name, doc) name_list.append(name) else: name_list.append(name) - return name_list + email_queue = make_email_queue(email_queue) + return { + 'invoice': name_list, + 'email_queue': email_queue + } def validate_records(doc): validate_customer(doc) validate_item(doc) +def make_email_queue(email_queue): + name_list = [] + for key, data in email_queue.items(): + name = frappe.db.get_value('Sales Invoice', {'offline_pos_name': key}, 'name') + data = json.loads(data) + sender = frappe.session.user + print_format = "POS Invoice" + attachments = [frappe.attach_print('Sales Invoice', name, print_format= print_format)] + + make(subject = data.get('subject'), content = data.get('content'), recipients = data.get('recipients'), + sender=sender,attachments = attachments, send_email=True, + doctype='Sales Invoice', name=name) + name_list.append(key) + + return name_list + def validate_customer(doc): if not frappe.db.exists('Customer', doc.get('customer')): customer_doc = frappe.new_doc('Customer') @@ -328,7 +351,8 @@ def validate_item(doc): item_doc.save(ignore_permissions=True) frappe.db.commit() -def submit_invoice(si_doc, name): + +def submit_invoice(si_doc, name, doc): try: si_doc.insert() si_doc.submit() diff --git a/erpnext/accounts/page/pos/pos.js b/erpnext/accounts/page/pos/pos.js index e405ae038f..064c5f7621 100644 --- a/erpnext/accounts/page/pos/pos.js +++ b/erpnext/accounts/page/pos/pos.js @@ -95,11 +95,65 @@ erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ me.sync_sales_invoice() }); + this.page.add_menu_item(__("Email"), function () { + if(me.frm.doc.docstatus == 1) { + me.email_prompt() + } + }); + this.page.add_menu_item(__("POS Profile"), function () { frappe.set_route('List', 'POS Profile'); }); }, + email_prompt: function() { + var me = this; + fields = [{label:__("To"), fieldtype:"Data", reqd: 0, fieldname:"recipients",length:524288}, + {fieldtype: "Section Break", collapsible: 1, label: "CC & Standard Reply"}, + {fieldtype: "Section Break"}, + {label:__("Subject"), fieldtype:"Data", reqd: 1, + fieldname:"subject",length:524288}, + {fieldtype: "Section Break"}, + {label:__("Message"), fieldtype:"Text Editor", reqd: 1, + fieldname:"content"}, + {fieldtype: "Section Break"}, + {fieldtype: "Column Break"}]; + + this.email_dialog = new frappe.ui.Dialog({ + title: "Email", + fields: fields, + primary_action_label: __("Send"), + primary_action: function() { + me.send_action(); + } + }); + + this.email_dialog.show() + }, + + send_action: function() { + this.email_queue = this.get_email_queue() + this.email_queue[this.frm.doc.offline_pos_name] = JSON.stringify(this.email_dialog.get_values()) + this.update_email_queue() + this.email_dialog.hide() + }, + + update_email_queue: function () { + try { + localStorage.setItem('email_queue', JSON.stringify(this.email_queue)); + } catch (e) { + frappe.throw(__("LocalStorage is full , did not save")) + } + }, + + get_email_queue: function () { + try { + return JSON.parse(localStorage.getItem('email_queue')) || {}; + } catch (e) { + return {} + } + }, + dialog_actions: function () { var me = this; @@ -1282,18 +1336,22 @@ erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ sync_sales_invoice: function () { var me = this; - this.si_docs = this.get_submitted_invoice(); + this.si_docs = this.get_submitted_invoice() || []; + this.email_queue_list = this.get_email_queue() || {}; - if (this.si_docs.length) { + if (this.si_docs.length || this.email_queue_list) { frappe.call({ method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", args: { - doc_list: me.si_docs + doc_list: me.si_docs, + email_queue_list: me.email_queue_list }, callback: function (r) { if (r.message) { - me.removed_items = r.message; + me.removed_items = r.message.invoice; + me.removed_email = r.message.email_queue me.remove_doc_from_localstorage(); + me.remove_email_queue_from_localstorage(); } } }) @@ -1323,10 +1381,10 @@ erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ var me = this; this.si_docs = this.get_doc_from_localstorage(); this.new_si_docs = []; - if (this.removed_items) { + if (this.removed_email) { $.each(this.si_docs, function (index, data) { for (key in data) { - if (!in_list(me.removed_items, key)) { + if (!in_list(me.removed_email, key)) { me.new_si_docs.push(data); } } @@ -1336,6 +1394,19 @@ erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ } }, + remove_email_queue_from_localstorage: function() { + var me = this; + this.email_queue = this.get_email_queue() + if (this.removed_email) { + $.each(this.email_queue_list, function (index, data) { + if (in_list(me.removed_email, index)) { + delete me.email_queue[index] + } + }) + this.update_email_queue(); + } + }, + validate: function () { var me = this; this.customer_validate(); diff --git a/erpnext/public/js/pos/pos.html b/erpnext/public/js/pos/pos.html index 1019647cdb..8a08a8b590 100644 --- a/erpnext/public/js/pos/pos.html +++ b/erpnext/public/js/pos/pos.html @@ -65,10 +65,10 @@
-
+
-
diff --git a/erpnext/public/js/pos/pos_selected_item.html b/erpnext/public/js/pos/pos_selected_item.html index 833cdd9211..4260cf7c8d 100644 --- a/erpnext/public/js/pos/pos_selected_item.html +++ b/erpnext/public/js/pos/pos_selected_item.html @@ -1,4 +1,3 @@ -
Item {%= item_name %}