Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.pos"); |
| 2 | {% include "erpnext/public/js/controllers/taxes_and_totals.js" %} |
| 3 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 4 | frappe.pages['pos'].on_page_load = function (wrapper) { |
Rushabh Mehta | 4c36d73 | 2015-01-01 15:59:34 +0530 | [diff] [blame] | 5 | var page = frappe.ui.make_app_page({ |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 6 | parent: wrapper, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 7 | title: __('Point of Sale'), |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 8 | single_column: true |
| 9 | }); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 10 | |
| 11 | wrapper.pos = new erpnext.pos.PointOfSale(wrapper) |
Rushabh Mehta | 72e1719 | 2014-08-08 15:30:49 +0530 | [diff] [blame] | 12 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 13 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 14 | frappe.pages['pos'].refresh = function (wrapper) { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 15 | window.onbeforeunload = function () { |
| 16 | return wrapper.pos.beforeunload() |
| 17 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 18 | } |
| 19 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 20 | erpnext.pos.PointOfSale = erpnext.taxes_and_totals.extend({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 21 | init: function (wrapper) { |
| 22 | this.page_len = 20; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 23 | this.page = wrapper.page; |
| 24 | this.wrapper = $(wrapper).find('.page-content'); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 25 | this.set_indicator(); |
| 26 | this.onload(); |
| 27 | this.make_menu_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 28 | this.si_docs = this.get_doc_from_localstorage(); |
| 29 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 30 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 31 | beforeunload: function (e) { |
| 32 | if (this.connection_status == false && frappe.get_route()[0] == "pos") { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 33 | e = e || window.event; |
| 34 | |
| 35 | // For IE and Firefox prior to version 4 |
| 36 | if (e) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 37 | e.returnValue = __("You are in offline mode. You will not be able to reload until you have network."); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 38 | return |
| 39 | } |
| 40 | |
| 41 | // For Safari |
| 42 | return __("You are in offline mode. You will not be able to reload until you have network."); |
| 43 | } |
| 44 | }, |
| 45 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 46 | check_internet_connection: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 47 | var me = this; |
| 48 | //Check Internet connection after every 30 seconds |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 49 | setInterval(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 50 | me.set_indicator(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 51 | }, 5000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 52 | }, |
| 53 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 54 | set_indicator: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 55 | var me = this; |
| 56 | // navigator.onLine |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 57 | this.connection_status = false; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 58 | this.page.set_indicator(__("Offline"), "grey") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 59 | frappe.call({ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 60 | method: "frappe.handler.ping", |
| 61 | callback: function (r) { |
| 62 | if (r.message) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 63 | me.connection_status = true; |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 64 | me.page.set_indicator(__("Online"), "green") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | }) |
| 68 | }, |
| 69 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 70 | onload: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 71 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 72 | this.get_data_from_server(function () { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 73 | me.create_new(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 74 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 75 | }, |
| 76 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 77 | make_menu_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 78 | var me = this; |
| 79 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 80 | this.page.add_menu_item(__("New Sales Invoice"), function () { |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 81 | me.save_previous_entry(); |
| 82 | me.create_new(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 83 | }) |
| 84 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 85 | this.page.add_menu_item(__("Sync Master Data"), function () { |
| 86 | me.get_data_from_server(function () { |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 87 | me.load_data(false); |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 88 | me.make_customer(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 89 | me.make_item_list(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 90 | me.set_missing_values(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 91 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 92 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 93 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 94 | this.page.add_menu_item(__("Sync Offline Invoices"), function () { |
Rohit Waghchaure | 82be020 | 2016-10-04 12:46:37 +0530 | [diff] [blame] | 95 | me.sync_sales_invoice() |
| 96 | }); |
| 97 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 98 | this.page.add_menu_item(__("POS Profile"), function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 99 | frappe.set_route('List', 'POS Profile'); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 100 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 101 | }, |
| 102 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 103 | dialog_actions: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 104 | var me = this; |
| 105 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 106 | $(this.list_body).find('.list-column').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 107 | me.name = $(this).parents().attr('invoice-name') |
| 108 | me.edit_record(); |
| 109 | }) |
| 110 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 111 | $(this.list_body).find('.list-select-all').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 112 | me.removed_items = []; |
| 113 | $(me.list_body).find('.list-delete').prop("checked", $(this).is(":checked")) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 114 | if ($(this).is(":checked")) { |
| 115 | $.each(me.si_docs, function (index, data) { |
| 116 | for (key in data) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 117 | me.removed_items.push(key) |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | me.toggle_primary_action(); |
| 123 | }) |
| 124 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 125 | $(this.list_body).find('.list-delete').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 126 | me.name = $(this).parent().parent().attr('invoice-name'); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 127 | if ($(this).is(":checked")) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 128 | me.removed_items.push(me.name); |
| 129 | } else { |
| 130 | me.removed_items.pop(me.name) |
| 131 | } |
| 132 | |
| 133 | me.toggle_primary_action(); |
| 134 | }) |
| 135 | }, |
| 136 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 137 | edit_record: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 138 | var me = this; |
| 139 | |
| 140 | doc_data = this.get_invoice_doc(this.si_docs); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 141 | if (doc_data) { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 142 | this.frm.doc = doc_data[0][this.name]; |
| 143 | this.set_missing_values(); |
| 144 | this.refresh(false); |
| 145 | this.disable_input_field(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 146 | this.list_dialog && this.list_dialog.hide(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 147 | } |
| 148 | }, |
| 149 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 150 | delete_records: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 151 | var me = this; |
| 152 | this.remove_doc_from_localstorage() |
| 153 | this.update_localstorage(); |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 154 | this.dialog_actions(); |
| 155 | this.toggle_primary_action(); |
| 156 | }, |
| 157 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 158 | toggle_primary_action: function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 159 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 160 | if(this.frm.doc.allow_delete) { |
| 161 | if (this.removed_items && this.removed_items.length > 0) { |
| 162 | $(this.wrapper).find('.btn-danger').show(); |
| 163 | } else { |
| 164 | $(this.wrapper).find('.btn-danger').hide(); |
| 165 | } |
| 166 | } |
| 167 | }, |
| 168 | |
| 169 | get_doctype_status: function (doc) { |
| 170 | if (doc.docstatus == 0) { |
| 171 | return { status: "Draft", indicator: "red" } |
| 172 | } else if (doc.outstanding_amount == 0) { |
| 173 | return { status: "Paid", indicator: "green" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 174 | } else { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 175 | return { status: "Submitted", indicator: "blue" } |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 176 | } |
| 177 | }, |
| 178 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 179 | set_missing_values: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 180 | var me = this; |
| 181 | doc = JSON.parse(localStorage.getItem('doc')) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 182 | if (this.frm.doc.payments.length == 0) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 183 | this.frm.doc.payments = doc.payments; |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 184 | this.calculate_outstanding_amount(); |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 185 | } |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 186 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 187 | if (this.frm.doc.customer) { |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 188 | this.party_field.$input.val(this.frm.doc.customer); |
| 189 | } |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 190 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 191 | if (!this.frm.doc.write_off_account) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 192 | this.frm.doc.write_off_account = doc.write_off_account |
| 193 | } |
| 194 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 195 | if (!this.frm.doc.account_for_change_amount) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 196 | this.frm.doc.account_for_change_amount = doc.account_for_change_amount |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 197 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 198 | }, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 199 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 200 | get_invoice_doc: function (si_docs) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 201 | var me = this; |
| 202 | this.si_docs = this.get_doc_from_localstorage(); |
| 203 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 204 | return $.grep(this.si_docs, function (data) { |
| 205 | for (key in data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 206 | return key == me.name |
| 207 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 208 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 209 | }, |
| 210 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 211 | get_data_from_server: function (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 212 | var me = this; |
| 213 | frappe.call({ |
| 214 | method: "erpnext.accounts.doctype.sales_invoice.pos.get_pos_data", |
| 215 | freeze: true, |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 216 | freeze_message: __("Master data syncing, it might take some time"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 217 | callback: function (r) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 218 | me.init_master_data(r) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 219 | localStorage.setItem('doc', JSON.stringify(r.message.doc)); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 220 | me.set_interval_for_si_sync(); |
| 221 | me.check_internet_connection(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 222 | if (callback) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 223 | callback(); |
| 224 | } |
| 225 | } |
| 226 | }) |
| 227 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 228 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 229 | init_master_data: function (r) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 230 | var me = this; |
| 231 | this.meta = r.message.meta; |
| 232 | this.item_data = r.message.items; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 233 | this.item_groups = r.message.item_groups; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 234 | this.customers = r.message.customers; |
| 235 | this.serial_no_data = r.message.serial_no_data; |
| 236 | this.batch_no_data = r.message.batch_no_data; |
| 237 | this.tax_data = r.message.tax_data; |
| 238 | this.price_list_data = r.message.price_list_data; |
| 239 | this.bin_data = r.message.bin_data; |
| 240 | this.pricing_rules = r.message.pricing_rules; |
| 241 | this.print_template = r.message.print_template; |
| 242 | this.pos_profile_data = r.message.pos_profile; |
| 243 | this.default_customer = r.message.default_customer || null; |
rohitwaghchaure | 34e820d | 2016-12-20 16:54:24 +0530 | [diff] [blame] | 244 | this.print_settings = locals[":Print Settings"]["Print Settings"]; |
Rohit Waghchaure | d567e57 | 2016-12-21 13:18:36 +0530 | [diff] [blame] | 245 | this.letter_head = (this.pos_profile_data.length > 0) ? frappe.boot.letter_heads[this.pos_profile_data[letter_head]] : {}; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 246 | }, |
| 247 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 248 | save_previous_entry: function () { |
| 249 | if (this.frm.doc.docstatus < 1 && this.frm.doc.items.length > 0) { |
| 250 | this.create_invoice(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 251 | } |
| 252 | }, |
| 253 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 254 | create_new: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 255 | var me = this; |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 256 | this.frm = {} |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 257 | this.name = null; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 258 | this.load_data(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 259 | this.setup(); |
| 260 | }, |
| 261 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 262 | load_data: function (load_doc) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 263 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 264 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 265 | this.items = this.item_data; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 266 | this.actual_qty_dict = {}; |
| 267 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 268 | if (load_doc) { |
| 269 | this.frm.doc = JSON.parse(localStorage.getItem('doc')); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 270 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 271 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 272 | $.each(this.meta, function (i, data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 273 | frappe.meta.sync(data) |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 274 | locals["DocType"][data.name] = data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 275 | }) |
| 276 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 277 | this.print_template_data = frappe.render_template("print_template", { |
| 278 | content: this.print_template, |
| 279 | title: "POS", base_url: frappe.urllib.get_base_url(), print_css: frappe.boot.print_css, |
| 280 | print_settings: this.print_settings, header: this.letter_head.header, footer: this.letter_head.footer |
| 281 | }) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 282 | }, |
| 283 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 284 | setup: function () { |
| 285 | this.frm.doc.allow_delete = this.pos_profile_data["allow_delete"]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 286 | this.wrapper.html(frappe.render_template("pos", this.frm.doc)); |
| 287 | this.set_transaction_defaults("Customer"); |
| 288 | this.make(); |
| 289 | this.set_primary_action(); |
| 290 | }, |
| 291 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 292 | set_transaction_defaults: function (party) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 293 | var me = this; |
| 294 | this.party = party; |
| 295 | this.price_list = (party == "Customer" ? |
| 296 | this.frm.doc.selling_price_list : this.frm.doc.buying_price_list); |
| 297 | this.price_list_field = (party == "Customer" ? "selling_price_list" : "buying_price_list"); |
| 298 | this.sales_or_purchase = (party == "Customer" ? "Sales" : "Purchase"); |
| 299 | }, |
| 300 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 301 | make: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 302 | this.make_search(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 303 | this.make_list_customers(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 304 | this.make_customer(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 305 | this.make_item_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 306 | this.make_discount_field() |
| 307 | }, |
| 308 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 309 | make_search: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 310 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 311 | this.serach_item = frappe.ui.form.make_control({ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 312 | df: { |
| 313 | "fieldtype": "Data", |
| 314 | "label": "Item", |
| 315 | "fieldname": "pos_item", |
Rohit Waghchaure | 26cf01a | 2016-07-22 13:24:33 +0530 | [diff] [blame] | 316 | "placeholder": __("Search Item") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 317 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 318 | parent: this.wrapper.find(".search-item"), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 319 | only_input: true, |
| 320 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 321 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 322 | this.serach_item.make_input(); |
| 323 | this.serach_item.$input.on("keyup", function () { |
| 324 | setTimeout(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 325 | me.items = me.get_items(); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 326 | me.make_item_list(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 327 | }, 1000); |
| 328 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 329 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 330 | this.search_item_group = frappe.ui.form.make_control({ |
| 331 | df: { |
| 332 | "fieldtype": "Select", |
| 333 | "options": me.item_groups, |
| 334 | "label": __("Item Group"), |
| 335 | "fieldname": "item_group", |
| 336 | "placeholder": __("Item Group") |
| 337 | }, |
| 338 | parent: this.wrapper.find(".search-item-group"), |
| 339 | only_input: true, |
| 340 | }); |
| 341 | |
| 342 | this.search_item_group.make_input(); |
| 343 | this.search_item_group.$input.on("change", function () { |
| 344 | me.page_len = 20; |
| 345 | me.items = me.get_items(); |
| 346 | me.make_item_list(); |
| 347 | }); |
| 348 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 349 | this.party_field = frappe.ui.form.make_control({ |
| 350 | df: { |
| 351 | "fieldtype": "Data", |
| 352 | "options": this.party, |
| 353 | "label": this.party, |
| 354 | "fieldname": this.party.toLowerCase(), |
Rushabh Mehta | e0686b3 | 2016-06-10 12:34:41 +0530 | [diff] [blame] | 355 | "placeholder": __("Select or add new customer") |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 356 | }, |
| 357 | parent: this.wrapper.find(".party-area"), |
| 358 | only_input: true, |
| 359 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 360 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 361 | this.party_field.make_input(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 362 | setTimeout(this.set_focus.bind(this), 500); |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 363 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 364 | this.wrapper.find(".btn-more").on("click", function() { |
| 365 | me.page_len += 20; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 366 | me.items = me.get_items(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 367 | me.make_item_list(); |
| 368 | }) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 369 | }, |
| 370 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 371 | make_list_customers: function () { |
| 372 | var me = this; |
| 373 | this.list_customers_btn = this.wrapper.find('.list-customers-btn'); |
| 374 | this.add_customer_btn = this.wrapper.find('.add-customer-btn'); |
| 375 | this.pos_bill = this.wrapper.find('.pos-bill').hide(); |
| 376 | this.list_customers = this.wrapper.find('.list-customers'); |
| 377 | |
| 378 | this.list_customers_btn.on('click', function () { |
| 379 | $(this).toggleClass("view_customer"); |
| 380 | if($(this).hasClass("view_customer")) { |
| 381 | me.render_list_customers(); |
| 382 | me.bind_delete_event() |
| 383 | me.party_field.$input.attr('disabled', true); |
| 384 | me.list_customers.show(); |
| 385 | me.pos_bill.hide(); |
| 386 | } else { |
| 387 | if(me.frm.doc.docstatus == 0) { |
| 388 | me.party_field.$input.attr('disabled', false); |
| 389 | } |
| 390 | me.pos_bill.show(); |
| 391 | me.list_customers.hide() |
| 392 | } |
| 393 | }); |
| 394 | this.add_customer_btn.on('click', function() { |
| 395 | me.save_previous_entry(); |
| 396 | me.create_new(); |
| 397 | me.refresh(); |
| 398 | me.set_focus(); |
| 399 | }); |
| 400 | }, |
| 401 | |
| 402 | render_list_customers: function () { |
| 403 | var me = this; |
| 404 | |
| 405 | this.removed_items = []; |
| 406 | this.list_customers.empty(); |
| 407 | this.si_docs = this.get_doc_from_localstorage(); |
| 408 | |
| 409 | if (!this.si_docs.length) { |
| 410 | this.list_customers.append( |
| 411 | '<div style="padding: 12px; margin-left:-12px;">' + __("No offline records.") + '</div>' |
| 412 | ) |
| 413 | return; |
| 414 | } |
| 415 | var html = '<div class="row list-row list-row-head pos-invoice-list">\ |
| 416 | <div class="col-xs-1"><input class="list-select-all" type="checkbox"></div>\ |
| 417 | <div class="col-xs-3">Customer</div>\ |
| 418 | <div class="col-xs-2 text-left">Status</div>\ |
| 419 | <div class="col-xs-3 text-right">Paid Amount</div>\ |
| 420 | <div class="col-xs-3 text-right">Grand Total</div>\ |
| 421 | </div>'; |
| 422 | this.si_docs.forEach(function (data, i) { |
| 423 | for (key in data) { |
| 424 | html += frappe.render_template("pos_invoice_list", { |
| 425 | sr: i + 1, |
| 426 | name: key, |
| 427 | customer: data[key].customer, |
| 428 | paid_amount: format_currency(data[key].paid_amount, me.frm.doc.currency), |
| 429 | grand_total: format_currency(data[key].grand_total, me.frm.doc.currency), |
| 430 | data: me.get_doctype_status(data[key]) |
| 431 | }); |
| 432 | } |
| 433 | }); |
| 434 | this.list_customers.append(html); |
| 435 | |
| 436 | this.list_customers.find('.list-column').click(function () { |
| 437 | me.list_customers.hide(); |
| 438 | me.list_customers_btn.toggleClass("view_customer"); |
| 439 | me.pos_bill.show(); |
| 440 | me.list_customers_btn.show(); |
| 441 | me.name = $(this).parents().attr('invoice-name') |
| 442 | me.edit_record(); |
| 443 | }) |
| 444 | |
| 445 | //actions |
| 446 | $(this.wrapper).find('.list-select-all').click(function () { |
| 447 | me.list_customers.find('.list-delete').prop("checked", $(this).is(":checked")) |
| 448 | me.removed_items = []; |
| 449 | if ($(this).is(":checked")) { |
| 450 | $.each(me.si_docs, function (index, data) { |
| 451 | for (key in data) { |
| 452 | me.removed_items.push(key) |
| 453 | } |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | me.toggle_primary_action(); |
| 458 | }); |
| 459 | |
| 460 | $(this.wrapper).find('.list-delete').click(function () { |
| 461 | me.name = $(this).parent().parent().attr('invoice-name'); |
| 462 | if ($(this).is(":checked")) { |
| 463 | me.removed_items.push(me.name); |
| 464 | } else { |
| 465 | me.removed_items.pop(me.name) |
| 466 | } |
| 467 | |
| 468 | me.toggle_primary_action(); |
| 469 | }); |
| 470 | }, |
| 471 | |
| 472 | bind_delete_event: function() { |
| 473 | var me = this; |
| 474 | |
| 475 | $(this.wrapper).find('.btn-danger').click(function(){ |
| 476 | frappe.confirm(__("Delete permanently?"), function () { |
| 477 | me.delete_records(); |
| 478 | me.render_list_customers(); |
| 479 | }) |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 480 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 481 | }, |
| 482 | |
| 483 | set_focus: function () { |
| 484 | if (this.default_customer || this.frm.doc.customer) { |
| 485 | this.serach_item.$input.focus(); |
| 486 | } else { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 487 | this.party_field.$input.focus(); |
| 488 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 489 | }, |
| 490 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 491 | make_customer: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 492 | var me = this; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 493 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 494 | if (this.default_customer && !this.frm.doc.customer) { |
Rohit Waghchaure | 539e913 | 2016-07-07 16:44:40 +0530 | [diff] [blame] | 495 | this.party_field.$input.val(this.default_customer); |
| 496 | this.frm.doc.customer = this.default_customer; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 497 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 498 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 499 | this.party_field.awesomeplete = |
| 500 | new Awesomplete(this.party_field.$input.get(0), { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 501 | minChars: 0, |
| 502 | maxItems: 99, |
| 503 | autoFirst: true, |
| 504 | list: [], |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 505 | filter: function (item, input) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 506 | var value = item.value.toLowerCase(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 507 | if (value.indexOf('is_action') !== -1 || |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 508 | value.indexOf(input) !== -1) { |
| 509 | return true; |
| 510 | } |
| 511 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 512 | item: function (item, input) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 513 | var d = item; |
| 514 | var html = "<span>" + __(d.label || d.value) + "</span>"; |
| 515 | return $('<li></li>') |
| 516 | .data('item.autocomplete', d) |
| 517 | .html('<a><p>' + html + '</p></a>') |
| 518 | .get(0); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 519 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 520 | }); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 521 | var customers = this.customers.map(function (c) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 522 | return { |
| 523 | label: c.name, |
| 524 | value: c.name, |
| 525 | customer_group: c.customer_group, |
| 526 | territory: c.territory |
| 527 | } |
| 528 | }); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 529 | |
| 530 | customers.push({ |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 531 | label: "<span class='text-primary link-option'>" |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 532 | + "<i class='fa fa-plus' style='margin-right: 5px;'></i> " |
| 533 | + __("Create a new Customer") |
| 534 | + "</span>", |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 535 | value: 'is_action', |
| 536 | action: me.new_customer |
| 537 | }); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 538 | this.party_field.awesomeplete.list = customers; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 539 | |
| 540 | this.party_field.$input |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 541 | .on('input', function (e) { |
| 542 | me.party_field.awesomeplete.list = customers; |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 543 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 544 | .on('awesomplete-select', function (e) { |
| 545 | var customer = me.party_field.awesomeplete |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 546 | .get_item(e.originalEvent.text.value); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 547 | if (!customer) return; |
| 548 | // create customer link |
| 549 | if (customer.action) { |
| 550 | customer.action.apply(me); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 551 | return; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 552 | } |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 553 | me.update_customer_data(customer); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 554 | me.refresh(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 555 | me.set_focus(); |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 556 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 557 | .on('change', function (e) { |
| 558 | if (!e.originalEvent.text) { |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 559 | me.frm.doc.customer = $(this).val(); |
| 560 | } |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 561 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 562 | .on('focus', function (e) { |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 563 | $(e.target).val('').trigger('input'); |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 564 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 565 | .on("awesomplete-selectcomplete", function (e) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 566 | var item = me.party_field.awesomeplete |
| 567 | .get_item(e.originalEvent.text.value); |
| 568 | // clear text input if item is action |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 569 | if (item.action) { |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 570 | $(this).val(""); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 571 | } |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 572 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 573 | }, |
| 574 | |
Nabin Hait | 6e9d2a3 | 2017-01-09 17:21:34 +0530 | [diff] [blame] | 575 | new_customer: function () { |
| 576 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 577 | if (!this.connection_status) return; |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 578 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 579 | this.customer_doc = new frappe.ui.Dialog({ |
| 580 | 'title': 'Customer', |
| 581 | fields: [ |
| 582 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 583 | "label": __("Full Name"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 584 | "fieldname": "full_name", |
| 585 | "fieldtype": "Data", |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 586 | "reqd": 1 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 587 | }, |
| 588 | { |
| 589 | "fieldtype": "Section Break" |
| 590 | }, |
| 591 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 592 | "label": __("Email Id"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 593 | "fieldname": "email_id", |
| 594 | "fieldtype": "Data" |
| 595 | }, |
| 596 | { |
| 597 | "fieldtype": "Column Break" |
| 598 | }, |
| 599 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 600 | "label": __("Contact Number"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 601 | "fieldname": "contact_no", |
| 602 | "fieldtype": "Data" |
| 603 | }, |
| 604 | { |
| 605 | "fieldtype": "Section Break" |
| 606 | }, |
| 607 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 608 | "label": __("Address Line 1"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 609 | "fieldname": "address_line1", |
| 610 | "fieldtype": "Data" |
| 611 | }, |
| 612 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 613 | "label": __("Address Line 2"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 614 | "fieldname": "address_line2", |
| 615 | "fieldtype": "Data" |
| 616 | }, |
| 617 | { |
| 618 | "fieldtype": "Column Break" |
| 619 | }, |
| 620 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 621 | "label": __("City"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 622 | "fieldname": "city", |
| 623 | "fieldtype": "Data" |
| 624 | }, |
| 625 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 626 | "label": __("State"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 627 | "fieldname": "state", |
| 628 | "fieldtype": "Data" |
| 629 | }, |
| 630 | { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 631 | "label": __("ZIP Code"), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 632 | "fieldname": "zip_code", |
| 633 | "fieldtype": "Data" |
| 634 | } |
| 635 | ] |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 636 | }) |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 637 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 638 | this.customer_doc.show() |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 639 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 640 | this.customer_doc.set_primary_action(__("Save"), function () { |
| 641 | me.make_offline_customer(); |
| 642 | me.pos_bill.show(); |
| 643 | }); |
| 644 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 645 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 646 | make_offline_customer: function() { |
| 647 | this.frm.doc.customer = this.customer_doc.get_values().full_name; |
| 648 | this.frm.doc.contact_details = JSON.stringify(this.customer_doc.get_values()); |
| 649 | this.party_field.$input.val(this.frm.doc.customer); |
| 650 | this.customers.push({ |
| 651 | name: this.frm.doc.customer, |
| 652 | customer_name: this.frm.doc.customer |
| 653 | }); |
| 654 | |
| 655 | this.customer_doc.hide() |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 656 | }, |
| 657 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 658 | update_customer_data: function (doc) { |
Rohit Waghchaure | fe7a5b3 | 2016-12-27 14:15:52 +0530 | [diff] [blame] | 659 | var me = this; |
| 660 | this.frm.doc.customer = doc.label || doc.name; |
| 661 | this.frm.doc.customer_name = doc.customer_name; |
| 662 | this.frm.doc.customer_group = doc.customer_group; |
| 663 | this.frm.doc.territory = doc.territory; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 664 | this.pos_bill.show(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 665 | }, |
| 666 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 667 | get_customers: function (key) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 668 | var me = this; |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 669 | key = key.toLowerCase().trim() |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 670 | var re = new RegExp('%', 'g'); |
| 671 | var reg = new RegExp(key.replace(re, '\\w*\\s*[a-zA-Z0-9]*')) |
| 672 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 673 | if (key) { |
| 674 | return $.grep(this.customers, function (data) { |
| 675 | if (reg.test(data.name.toLowerCase()) |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 676 | || reg.test(data.customer_name.toLowerCase()) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 677 | || (data.customer_group && reg.test(data.customer_group.toLowerCase()))) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 678 | return data |
| 679 | } |
| 680 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 681 | } else { |
| 682 | customers = this.customers.sort(function (a, b) { return a.idx < b.idx }) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 683 | return customers.slice(0, 20) |
| 684 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 685 | }, |
| 686 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 687 | make_item_list: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 688 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 689 | if (!this.price_list) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 690 | msgprint(__("Price List not found or disabled")); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | me.item_timeout = null; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 695 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 696 | var $wrap = me.wrapper.find(".item-list"); |
| 697 | me.wrapper.find(".item-list").empty(); |
| 698 | |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 699 | if (this.items.length > 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 700 | $.each(this.items, function(index, obj) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 701 | if(index < me.page_len){ |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 702 | $(frappe.render_template("pos_item", { |
| 703 | item_code: obj.name, |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 704 | item_price: format_currency(me.price_list_data[obj.name], me.frm.doc.currency), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 705 | item_name: obj.name === obj.item_name ? "" : obj.item_name, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 706 | item_image: obj.image ? "url('" + obj.image + "')" : null, |
| 707 | color: frappe.get_palette(obj.item_name), |
| 708 | abbr: frappe.get_abbr(obj.item_name) |
| 709 | })).tooltip().appendTo($wrap); |
| 710 | } |
| 711 | }); |
Rohit Waghchaure | 801029e | 2016-11-17 00:14:21 +0530 | [diff] [blame] | 712 | } else { |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 713 | $("<p class='text-muted small' style='padding-left: 10px'>" |
| 714 | +__("Not items found")+"</p>").appendTo($wrap) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 715 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 716 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 717 | if (this.items.length == 1 |
| 718 | && this.serach_item.$input.val()) { |
| 719 | this.serach_item.$input.val(""); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 720 | this.add_to_cart(); |
| 721 | } |
| 722 | |
| 723 | // if form is local then allow this function |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 724 | $(me.wrapper).find("div.pos-item").on("click", function () { |
| 725 | if(me.list_customers_btn.hasClass("view_customer")) return; |
| 726 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 727 | me.customer_validate(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 728 | if (me.frm.doc.docstatus == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 729 | me.items = me.get_items($(this).attr("data-item-code")) |
| 730 | me.add_to_cart(); |
| 731 | } |
| 732 | }); |
| 733 | }, |
| 734 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 735 | get_items: function (item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 736 | // To search item as per the key enter |
| 737 | |
| 738 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 739 | this.item_serial_no = {}; |
| 740 | this.item_batch_no = {}; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 741 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 742 | if (item_code) { |
| 743 | return $.grep(this.item_data, function (item) { |
| 744 | if (item.item_code == item_code) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 745 | return true |
| 746 | } |
| 747 | }) |
| 748 | } |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 749 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 750 | this.items_list = this.apply_category(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 751 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 752 | key = this.serach_item.$input.val().toLowerCase().replace(/[&\/\\#,+()\[\]$~.'":*?<>{}]/g, '\\$&'); |
Rohit Waghchaure | 163e359 | 2016-10-05 13:38:02 +0530 | [diff] [blame] | 753 | var re = new RegExp('%', 'g'); |
Rohit Waghchaure | 6f33dfb | 2016-10-09 15:32:56 +0530 | [diff] [blame] | 754 | var reg = new RegExp(key.replace(re, '[\\w*\\s*[a-zA-Z0-9]*]*')) |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 755 | search_status = true |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 756 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 757 | if (key) { |
| 758 | return $.grep(this.items_list, function (item) { |
| 759 | if (search_status) { |
| 760 | if (in_list(me.batch_no_data[item.item_code], me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 761 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 762 | return me.item_batch_no[item.item_code] = me.serach_item.$input.val() |
| 763 | } else if (me.serial_no_data[item.item_code] |
| 764 | && in_list(Object.keys(me.serial_no_data[item.item_code]), me.serach_item.$input.val())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 765 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 766 | me.item_serial_no[item.item_code] = [me.serach_item.$input.val(), me.serial_no_data[item.item_code][me.serach_item.$input.val()]] |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 767 | return true |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 768 | } else if (item.barcode == me.serach_item.$input.val()) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 769 | search_status = false; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 770 | return item.barcode == me.serach_item.$input.val(); |
| 771 | } else if (reg.test(item.item_code.toLowerCase()) || reg.test(item.description.toLowerCase()) || |
| 772 | reg.test(item.item_name.toLowerCase()) || reg.test(item.item_group.toLowerCase())) { |
Rohit Waghchaure | ea5a32d | 2016-08-10 17:18:52 +0530 | [diff] [blame] | 773 | return true |
| 774 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 775 | } |
| 776 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 777 | } else { |
| 778 | return this.items_list; |
| 779 | } |
| 780 | }, |
Rushabh Mehta | 3714626 | 2017-02-01 18:17:35 +0530 | [diff] [blame] | 781 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 782 | apply_category: function() { |
| 783 | var me = this; |
| 784 | category = this.search_item_group.$input.val(); |
| 785 | |
| 786 | if(category == 'All Item Groups') { |
| 787 | return this.item_data |
| 788 | } else { |
| 789 | return this.item_data.filter(function(element, index, array){ |
| 790 | return element.item_group == category; |
| 791 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 792 | } |
| 793 | }, |
| 794 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 795 | bind_qty_event: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 796 | var me = this; |
| 797 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 798 | $(this.wrapper).find(".pos-item-qty").on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 799 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 800 | var qty = $(this).val(); |
| 801 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 802 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 803 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 804 | $(this.wrapper).find("[data-action='increase-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 805 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 806 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) + 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 807 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 808 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 809 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 810 | $(this.wrapper).find("[data-action='decrease-qty']").on("click", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 811 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 812 | var qty = flt($(this).parents(".pos-bill-item").find('.pos-item-qty').val()) - 1; |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 813 | me.update_qty(item_code, qty) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 814 | }) |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 815 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 816 | $(this.wrapper).find(".pos-item-discount").on("change", function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 817 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 818 | var discount = $(this).val(); |
| 819 | me.update_discount(item_code, discount) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 820 | }) |
| 821 | }, |
| 822 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 823 | update_qty: function (item_code, qty) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 824 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 825 | this.items = this.get_items(item_code); |
| 826 | this.validate_serial_no() |
| 827 | this.set_item_details(item_code, "qty", qty); |
| 828 | }, |
| 829 | |
| 830 | update_discount: function(item_code, discount) { |
| 831 | var me = this; |
| 832 | this.items = this.get_items(item_code); |
| 833 | this.set_item_details(item_code, "discount_percentage", discount); |
| 834 | }, |
| 835 | |
| 836 | update_rate: function () { |
| 837 | var me = this; |
| 838 | |
| 839 | $(this.wrapper).find(".pos-item-rate").on("change", function () { |
| 840 | var item_code = $(this).parents(".pos-bill-item").attr("data-item-code"); |
| 841 | me.set_item_details(item_code, "rate", $(this).val()); |
| 842 | }) |
| 843 | }, |
| 844 | |
| 845 | set_item_details: function (item_code, field, value) { |
| 846 | var me = this; |
| 847 | if (value < 0) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 848 | frappe.throw(__("Enter value must be positive")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 849 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 850 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 851 | this.remove_item = [] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 852 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 853 | if (d.item_code == item_code) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 854 | if (d.serial_no && field == 'qty') { |
| 855 | me.validate_serial_no_qty(d, item_code, field, value) |
| 856 | } |
| 857 | |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 858 | d[field] = flt(value); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 859 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 860 | if (d.qty == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 861 | me.remove_item.push(d.idx) |
| 862 | } |
| 863 | } |
| 864 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 865 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 866 | if (field == 'qty') { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 867 | this.remove_zero_qty_item(); |
| 868 | } |
| 869 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 870 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 871 | }, |
| 872 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 873 | remove_zero_qty_item: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 874 | var me = this; |
| 875 | idx = 0 |
| 876 | this.items = [] |
| 877 | idx = 0 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 878 | $.each(this.frm.doc["items"] || [], function (i, d) { |
| 879 | if (!in_list(me.remove_item, d.idx)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 880 | d.idx = idx; |
| 881 | me.items.push(d); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 882 | idx++; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 883 | } |
| 884 | }); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 885 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 886 | this.frm.doc["items"] = this.items; |
| 887 | }, |
| 888 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 889 | make_discount_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 890 | var me = this; |
| 891 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 892 | this.wrapper.find('input.discount-percentage').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 893 | me.frm.doc.additional_discount_percentage = flt($(this).val(), precision("additional_discount_percentage")); |
| 894 | total = me.frm.doc.grand_total |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 895 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 896 | if (me.frm.doc.apply_discount_on == 'Net Total') { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 897 | total = me.frm.doc.net_total |
| 898 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 899 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 900 | me.frm.doc.discount_amount = flt(total * flt(me.frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 901 | me.wrapper.find('input.discount-amount').val(me.frm.doc.discount_amount) |
| 902 | me.refresh(); |
| 903 | }); |
| 904 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 905 | this.wrapper.find('input.discount-amount').on("change", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 906 | me.frm.doc.discount_amount = flt($(this).val(), precision("discount_amount")); |
| 907 | me.frm.doc.additional_discount_percentage = 0.0; |
| 908 | me.wrapper.find('input.discount-percentage').val(0); |
| 909 | me.refresh(); |
| 910 | }); |
| 911 | }, |
| 912 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 913 | customer_validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 914 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 915 | if (!this.frm.doc.customer) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 916 | frappe.throw(__("Please select customer")) |
| 917 | } |
| 918 | }, |
| 919 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 920 | add_to_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 921 | var me = this; |
| 922 | var caught = false; |
| 923 | var no_of_items = me.wrapper.find(".pos-bill-item").length; |
| 924 | |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 925 | this.customer_validate(); |
| 926 | this.mandatory_batch_no(); |
| 927 | this.validate_serial_no(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 928 | this.validate_warehouse(); |
| 929 | |
| 930 | if (no_of_items != 0) { |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 931 | $.each(this.frm.doc["items"] || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 932 | if (d.item_code == me.items[0].item_code) { |
| 933 | caught = true; |
| 934 | d.qty += 1; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 935 | d.amount = flt(d.rate) * flt(d.qty); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 936 | if (me.item_serial_no[d.item_code]) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 937 | d.serial_no += '\n' + me.item_serial_no[d.item_code][0] |
| 938 | d.warehouse = me.item_serial_no[d.item_code][1] |
| 939 | } |
| 940 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 941 | if (me.item_batch_no.length) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 942 | d.batch_no = me.item_batch_no[d.item_code] |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | }); |
| 946 | } |
| 947 | |
| 948 | // if item not found then add new item |
| 949 | if (!caught) |
| 950 | this.add_new_item_to_grid(); |
| 951 | |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 952 | this.update_paid_amount_status(false) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 953 | }, |
| 954 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 955 | add_new_item_to_grid: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 956 | var me = this; |
| 957 | this.child = frappe.model.add_child(this.frm.doc, this.frm.doc.doctype + " Item", "items"); |
| 958 | this.child.item_code = this.items[0].item_code; |
| 959 | this.child.item_name = this.items[0].item_name; |
| 960 | this.child.stock_uom = this.items[0].stock_uom; |
| 961 | this.child.description = this.items[0].description; |
| 962 | this.child.qty = 1; |
| 963 | this.child.item_group = this.items[0].item_group; |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 964 | this.child.cost_center = this.pos_profile_data['cost_center'] || this.items[0].cost_center; |
| 965 | this.child.income_account = this.pos_profile_data['income_account'] || this.items[0].income_account; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 966 | this.child.warehouse = (this.item_serial_no[this.child.item_code] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 967 | ? this.item_serial_no[this.child.item_code][1] : (this.pos_profile_data['warehouse'] || this.items[0].default_warehouse)); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 968 | this.child.price_list_rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 969 | this.child.rate = flt(this.price_list_data[this.child.item_code], 9) / flt(this.frm.doc.conversion_rate, 9); |
| 970 | this.child.actual_qty = me.get_actual_qty(this.items[0]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 971 | this.child.amount = flt(this.child.qty) * flt(this.child.rate); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 972 | this.child.batch_no = this.item_batch_no[this.child.item_code]; |
| 973 | this.child.serial_no = (this.item_serial_no[this.child.item_code] |
| 974 | ? this.item_serial_no[this.child.item_code][0] : ''); |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 975 | this.child.item_tax_rate = JSON.stringify(this.tax_data[this.child.item_code]); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 976 | }, |
| 977 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 978 | update_paid_amount_status: function (update_paid_amount) { |
| 979 | if (this.name) { |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 980 | update_paid_amount = update_paid_amount ? false : true; |
| 981 | } |
| 982 | |
| 983 | this.refresh(update_paid_amount); |
| 984 | }, |
| 985 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 986 | refresh: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 987 | var me = this; |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 988 | this.refresh_fields(update_paid_amount); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 989 | this.bind_qty_event(); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 990 | this.update_rate(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 991 | this.set_primary_action(); |
| 992 | }, |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 993 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 994 | refresh_fields: function (update_paid_amount) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 995 | this.apply_pricing_rule(); |
| 996 | this.discount_amount_applied = false; |
| 997 | this._calculate_taxes_and_totals(); |
| 998 | this.calculate_discount_amount(); |
| 999 | this.show_items_in_item_cart(); |
| 1000 | this.set_taxes(); |
Rohit Waghchaure | ea278b5 | 2016-07-21 23:28:04 +0530 | [diff] [blame] | 1001 | this.calculate_outstanding_amount(update_paid_amount); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1002 | this.set_totals(); |
| 1003 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1004 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1005 | get_company_currency: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1006 | return erpnext.get_currency(this.frm.doc.company); |
| 1007 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1008 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1009 | show_item_wise_taxes: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1010 | return null; |
| 1011 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1012 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1013 | show_items_in_item_cart: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1014 | var me = this; |
| 1015 | var $items = this.wrapper.find(".items").empty(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1016 | $.each(this.frm.doc.items || [], function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1017 | $(frappe.render_template("pos_bill_item", { |
| 1018 | item_code: d.item_code, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1019 | item_name: (d.item_name === d.item_code || !d.item_name) ? "" : ("<br>" + d.item_name), |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1020 | qty: d.qty, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1021 | discount_percentage: d.discount_percentage || 0.0, |
| 1022 | actual_qty: me.actual_qty_dict[d.item_code] || 0.0, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1023 | projected_qty: d.projected_qty, |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1024 | rate: format_number(d.rate, me.frm.doc.currency), |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1025 | enabled: me.pos_profile_data["allow_user_to_edit_rate"] ? true : false, |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1026 | amount: format_currency(d.amount, me.frm.doc.currency) |
| 1027 | })).appendTo($items); |
| 1028 | }); |
| 1029 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1030 | this.wrapper.find("input.pos-item-qty").on("focus", function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1031 | $(this).select(); |
| 1032 | }); |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1033 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1034 | this.wrapper.find("input.pos-item-discount").on("focus", function () { |
| 1035 | $(this).select(); |
| 1036 | }); |
| 1037 | |
| 1038 | this.wrapper.find("input.pos-item-rate").on("focus", function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1039 | $(this).select(); |
| 1040 | }); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1041 | }, |
| 1042 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1043 | set_taxes: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1044 | var me = this; |
| 1045 | me.frm.doc.total_taxes_and_charges = 0.0 |
| 1046 | |
| 1047 | var taxes = this.frm.doc.taxes || []; |
| 1048 | $(this.wrapper) |
| 1049 | .find(".tax-area").toggleClass("hide", (taxes && taxes.length) ? false : true) |
| 1050 | .find(".tax-table").empty(); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1051 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1052 | $.each(taxes, function (i, d) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1053 | if (d.tax_amount && cint(d.included_in_print_rate) == 0) { |
| 1054 | $(frappe.render_template("pos_tax_row", { |
| 1055 | description: d.description, |
| 1056 | tax_amount: format_currency(flt(d.tax_amount_after_discount_amount), |
| 1057 | me.frm.doc.currency) |
| 1058 | })).appendTo(me.wrapper.find(".tax-table")); |
| 1059 | } |
| 1060 | }); |
| 1061 | }, |
| 1062 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1063 | set_totals: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1064 | var me = this; |
| 1065 | this.wrapper.find(".net-total").text(format_currency(me.frm.doc.total, me.frm.doc.currency)); |
| 1066 | this.wrapper.find(".grand-total").text(format_currency(me.frm.doc.grand_total, me.frm.doc.currency)); |
| 1067 | }, |
| 1068 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1069 | set_primary_action: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1070 | var me = this; |
| 1071 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1072 | if (this.frm.doc.docstatus == 0) { |
| 1073 | this.page.set_primary_action(__("Pay"), function () { |
Rohit Waghchaure | 713e2b7 | 2016-08-19 15:11:36 +0530 | [diff] [blame] | 1074 | me.validate(); |
| 1075 | me.update_paid_amount_status(true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1076 | me.create_invoice(); |
| 1077 | me.make_payment(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1078 | }, "fa fa-credit-card"); |
| 1079 | } else if (this.frm.doc.docstatus == 1) { |
| 1080 | this.page.set_primary_action(__("Print"), function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 1081 | html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1082 | me.print_document(html) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1083 | }) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1084 | } else { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1085 | this.page.clear_primary_action() |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1086 | } |
Rohit Waghchaure | 9fe40d5 | 2016-06-13 21:37:10 +0530 | [diff] [blame] | 1087 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1088 | // this.page.set_secondary_action(__("New"), function () { |
| 1089 | // me.save_previous_entry(); |
| 1090 | // me.create_new(); |
| 1091 | // }, "fa fa-plus").addClass("btn-primary"); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1092 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1093 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1094 | print_dialog: function () { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1095 | var me = this; |
| 1096 | |
| 1097 | msgprint = frappe.msgprint(format('<a class="btn btn-primary print_doc" \ |
| 1098 | style="margin-right: 5px;">{0}</a>\ |
| 1099 | <a class="btn btn-default new_doc">{1}</a>', [ |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1100 | __('Print'), __('New') |
| 1101 | ])); |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1102 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1103 | $('.print_doc').click(function () { |
Rohit Waghchaure | 9f2cc38 | 2016-12-09 11:46:08 +0530 | [diff] [blame] | 1104 | html = frappe.render(me.print_template_data, me.frm.doc) |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1105 | me.print_document(html) |
| 1106 | }) |
| 1107 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1108 | $('.new_doc').click(function () { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1109 | msgprint.hide() |
| 1110 | me.create_new(); |
| 1111 | }) |
| 1112 | }, |
| 1113 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1114 | print_document: function (html) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1115 | var w = window.open(); |
| 1116 | w.document.write(html); |
| 1117 | w.document.close(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1118 | setTimeout(function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1119 | w.print(); |
| 1120 | w.close(); |
| 1121 | }, 1000) |
| 1122 | }, |
| 1123 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1124 | submit_invoice: function () { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1125 | var me = this; |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1126 | this.change_status(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1127 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 60a0532 | 2016-09-12 01:37:46 +0530 | [diff] [blame] | 1128 | this.print_dialog() |
| 1129 | } |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1130 | }, |
| 1131 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1132 | change_status: function () { |
| 1133 | if (this.frm.doc.docstatus == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1134 | this.frm.doc.docstatus = 1; |
| 1135 | this.update_invoice(); |
| 1136 | this.disable_input_field(); |
| 1137 | } |
| 1138 | }, |
| 1139 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1140 | disable_input_field: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1141 | var pointer_events = 'inherit' |
| 1142 | $(this.wrapper).find('input').attr("disabled", false); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1143 | $(this.wrapper).find('select').attr("disabled", false); |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1144 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1145 | if (this.frm.doc.docstatus == 1) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1146 | pointer_events = 'none'; |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1147 | $(this.wrapper).find('input').attr("disabled", true); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1148 | $(this.wrapper).find('select').attr("disabled", true); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1149 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1150 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1151 | $(this.wrapper).find('.pos-bill').css('pointer-events', pointer_events); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1152 | $(this.wrapper).find('.pos-items-section').css('pointer-events', pointer_events); |
| 1153 | this.set_primary_action(); |
| 1154 | }, |
| 1155 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1156 | create_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1157 | var me = this; |
| 1158 | var invoice_data = {} |
| 1159 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1160 | if (this.name) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1161 | this.update_invoice() |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1162 | } else { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1163 | this.name = $.now(); |
Rohit Waghchaure | 377c7ac | 2016-09-05 17:56:44 +0530 | [diff] [blame] | 1164 | this.frm.doc.offline_pos_name = this.name; |
Rohit Waghchaure | 9cd356c | 2016-08-11 16:54:30 +0530 | [diff] [blame] | 1165 | this.frm.doc.posting_date = frappe.datetime.get_today(); |
| 1166 | this.frm.doc.posting_time = frappe.datetime.now_time(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1167 | invoice_data[this.name] = this.frm.doc |
| 1168 | this.si_docs.push(invoice_data) |
| 1169 | this.update_localstorage(); |
| 1170 | this.set_primary_action(); |
| 1171 | } |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1172 | return invoice_data; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1173 | }, |
| 1174 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1175 | update_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1176 | var me = this; |
| 1177 | this.si_docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1178 | $.each(this.si_docs, function (index, data) { |
| 1179 | for (key in data) { |
| 1180 | if (key == me.name) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1181 | me.si_docs[index][key] = me.frm.doc; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1182 | me.update_localstorage(); |
| 1183 | } |
| 1184 | } |
| 1185 | }) |
| 1186 | }, |
| 1187 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1188 | update_localstorage: function () { |
| 1189 | try { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1190 | localStorage.setItem('sales_invoice_doc', JSON.stringify(this.si_docs)); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1191 | } catch (e) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1192 | frappe.throw(__("LocalStorage is full , did not save")) |
| 1193 | } |
| 1194 | }, |
| 1195 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1196 | get_doc_from_localstorage: function () { |
| 1197 | try { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1198 | return JSON.parse(localStorage.getItem('sales_invoice_doc')) || []; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1199 | } catch (e) { |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1200 | return [] |
| 1201 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1202 | }, |
| 1203 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1204 | set_interval_for_si_sync: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1205 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1206 | setInterval(function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1207 | me.sync_sales_invoice() |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1208 | }, 60000) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1209 | }, |
| 1210 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1211 | sync_sales_invoice: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1212 | var me = this; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1213 | this.si_docs = this.get_submitted_invoice(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1214 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1215 | if (this.si_docs.length) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1216 | frappe.call({ |
| 1217 | method: "erpnext.accounts.doctype.sales_invoice.pos.make_invoice", |
| 1218 | args: { |
| 1219 | doc_list: me.si_docs |
| 1220 | }, |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1221 | callback: function (r) { |
| 1222 | if (r.message) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1223 | me.removed_items = r.message; |
| 1224 | me.remove_doc_from_localstorage(); |
| 1225 | } |
| 1226 | } |
| 1227 | }) |
| 1228 | } |
| 1229 | }, |
| 1230 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1231 | get_submitted_invoice: function () { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1232 | var invoices = []; |
| 1233 | var index = 1; |
| 1234 | docs = this.get_doc_from_localstorage(); |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1235 | if (docs) { |
| 1236 | invoices = $.map(docs, function (data) { |
| 1237 | for (key in data) { |
| 1238 | if (data[key].docstatus == 1 && index < 50) { |
Rohit Waghchaure | f2aa176 | 2016-05-20 23:55:45 +0530 | [diff] [blame] | 1239 | index++ |
Rohit Waghchaure | b77a105 | 2016-08-01 18:35:18 +0530 | [diff] [blame] | 1240 | data[key].docstatus = 0; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1241 | return data |
| 1242 | } |
| 1243 | } |
| 1244 | }); |
| 1245 | } |
| 1246 | |
| 1247 | return invoices |
| 1248 | }, |
| 1249 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1250 | remove_doc_from_localstorage: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1251 | var me = this; |
| 1252 | this.si_docs = this.get_doc_from_localstorage(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1253 | this.new_si_docs = []; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1254 | if (this.removed_items) { |
| 1255 | $.each(this.si_docs, function (index, data) { |
| 1256 | for (key in data) { |
| 1257 | if (!in_list(me.removed_items, key)) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1258 | me.new_si_docs.push(data); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | }) |
Rohit Waghchaure | e0934d1 | 2016-05-11 15:04:57 +0530 | [diff] [blame] | 1262 | this.si_docs = this.new_si_docs; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1263 | this.update_localstorage(); |
| 1264 | } |
| 1265 | }, |
| 1266 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1267 | validate: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1268 | var me = this; |
| 1269 | this.customer_validate(); |
| 1270 | this.item_validate(); |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1271 | this.validate_mode_of_payments(); |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1272 | }, |
| 1273 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1274 | item_validate: function () { |
| 1275 | if (this.frm.doc.items.length == 0) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1276 | frappe.throw(__("Select items to save the invoice")) |
| 1277 | } |
| 1278 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1279 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1280 | validate_mode_of_payments: function () { |
| 1281 | if (this.frm.doc.payments.length === 0) { |
Saurabh | 9a6c666 | 2016-06-27 16:51:44 +0530 | [diff] [blame] | 1282 | frappe.throw(__("Payment Mode is not configured. Please check, whether account has been set on Mode of Payments or on POS Profile.")) |
| 1283 | } |
| 1284 | }, |
Rushabh Mehta | 512b85e | 2016-12-27 12:14:26 +0530 | [diff] [blame] | 1285 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1286 | validate_serial_no: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1287 | var me = this; |
| 1288 | var item_code = serial_no = ''; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1289 | for (key in this.item_serial_no) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1290 | item_code = key; |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1291 | serial_no = me.item_serial_no[key][0]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1292 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1293 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1294 | if (this.items[0].has_serial_no && serial_no == "") { |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1295 | this.refresh(); |
| 1296 | frappe.throw(__(repl("Error: Serial no is mandatory for item %(item)s", { |
| 1297 | 'item': this.items[0].item_code |
| 1298 | }))) |
| 1299 | } |
| 1300 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1301 | if (item_code && serial_no) { |
| 1302 | $.each(this.frm.doc.items, function (index, data) { |
| 1303 | if (data.item_code == item_code) { |
| 1304 | if (in_list(data.serial_no.split('\n'), serial_no)) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1305 | frappe.throw(__(repl("Serial no %(serial_no)s is already taken", { |
| 1306 | 'serial_no': serial_no |
| 1307 | }))) |
| 1308 | } |
| 1309 | } |
| 1310 | }) |
| 1311 | } |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1312 | }, |
| 1313 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1314 | validate_serial_no_qty: function (args, item_code, field, value) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1315 | var me = this; |
| 1316 | if (args.item_code == item_code && args.serial_no |
| 1317 | && field == 'qty' && cint(value) != value) { |
| 1318 | args.qty = 0.0; |
| 1319 | this.refresh(); |
| 1320 | frappe.throw(__("Serial no item cannot be a fraction")) |
| 1321 | } |
| 1322 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1323 | if (args.item_code == item_code && args.serial_no && args.serial_no.split('\n').length != cint(value)) { |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1324 | args.qty = 0.0; |
| 1325 | args.serial_no = '' |
| 1326 | this.refresh(); |
Rohit Waghchaure | dd58d53 | 2016-12-21 17:23:01 +0530 | [diff] [blame] | 1327 | frappe.throw(__(repl("Total nos of serial no is not equal to quantity for item %(item)s.", { |
| 1328 | 'item': item_code |
| 1329 | }))) |
Rohit Waghchaure | e4e69ec | 2016-08-17 16:20:13 +0530 | [diff] [blame] | 1330 | } |
| 1331 | }, |
| 1332 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1333 | mandatory_batch_no: function () { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1334 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1335 | if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) { |
Rohit Waghchaure | 03da40b | 2016-07-16 03:02:20 +0530 | [diff] [blame] | 1336 | frappe.throw(__(repl("Error: Batch no is mandatory for item %(item)s", { |
| 1337 | 'item': this.items[0].item_code |
| 1338 | }))) |
| 1339 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1340 | }, |
| 1341 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1342 | apply_pricing_rule: function () { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1343 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1344 | $.each(this.frm.doc["items"], function (n, item) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1345 | pricing_rule = me.get_pricing_rule(item) |
| 1346 | me.validate_pricing_rule(pricing_rule) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1347 | if (pricing_rule.length) { |
| 1348 | item.pricing_rule = pricing_rule[0].name; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1349 | item.margin_type = pricing_rule[0].margin_type; |
| 1350 | item.price_list_rate = pricing_rule[0].price || item.price_list_rate; |
| 1351 | item.margin_rate_or_amount = pricing_rule[0].margin_rate_or_amount; |
| 1352 | item.discount_percentage = pricing_rule[0].discount_percentage || 0.0; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1353 | } else if ((item.discount_percentage > 0 || item.margin_rate_or_amount > 0) && item.pricing_rule) { |
Rohit Waghchaure | 619c40b | 2016-11-07 15:29:17 +0530 | [diff] [blame] | 1354 | item.margin_rate_or_amount = 0.0; |
| 1355 | item.discount_percentage = 0.0; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1356 | item.pricing_rule = null; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1357 | } |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1358 | |
| 1359 | me.apply_pricing_rule_on_item(item) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1360 | }) |
| 1361 | }, |
| 1362 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1363 | get_pricing_rule: function (item) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1364 | var me = this; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1365 | return $.grep(this.pricing_rules, function (data) { |
| 1366 | if (item.qty >= data.min_qty && (item.qty <= (data.max_qty ? data.max_qty : item.qty))) { |
| 1367 | if (data.item_code == item.item_code || in_list(['All Item Groups', item.item_group], data.item_group)) { |
| 1368 | if (in_list(['Customer', 'Customer Group', 'Territory', 'Campaign'], data.applicable_for)) { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1369 | return me.validate_condition(data) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1370 | } else { |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1371 | return true |
| 1372 | } |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | }) |
| 1376 | }, |
| 1377 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1378 | validate_condition: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1379 | //This method check condition based on applicable for |
| 1380 | condition = this.get_mapper_for_pricing_rule(data)[data.applicable_for] |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1381 | if (in_list(condition[1], condition[0])) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1382 | return true |
| 1383 | } |
| 1384 | }, |
| 1385 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1386 | get_mapper_for_pricing_rule: function (data) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1387 | return { |
Rohit Waghchaure | 89ee309 | 2016-07-08 15:17:04 +0530 | [diff] [blame] | 1388 | 'Customer': [data.customer, [this.frm.doc.customer]], |
| 1389 | 'Customer Group': [data.customer_group, [this.frm.doc.customer_group, 'All Customer Groups']], |
| 1390 | 'Territory': [data.territory, [this.frm.doc.territory, 'All Territories']], |
Rohit Waghchaure | baef262 | 2016-08-05 15:41:36 +0530 | [diff] [blame] | 1391 | 'Campaign': [data.campaign, [this.frm.doc.campaign]], |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1392 | } |
| 1393 | }, |
| 1394 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1395 | validate_pricing_rule: function (pricing_rule) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1396 | //This method validate duplicate pricing rule |
| 1397 | var pricing_rule_name = ''; |
| 1398 | var priority = 0; |
| 1399 | var pricing_rule_list = []; |
| 1400 | var priority_list = [] |
| 1401 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1402 | if (pricing_rule.length > 1) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1403 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1404 | $.each(pricing_rule, function (index, data) { |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1405 | pricing_rule_name += data.name + ',' |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1406 | priority_list.push(data.priority) |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1407 | if (priority <= data.priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1408 | priority = data.priority |
| 1409 | pricing_rule_list.push(data) |
| 1410 | } |
| 1411 | }) |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1412 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1413 | count = 0 |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1414 | $.each(priority_list, function (index, value) { |
| 1415 | if (value == priority) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1416 | count++ |
| 1417 | } |
| 1418 | }) |
| 1419 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1420 | if (priority == 0 || count > 1) { |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1421 | frappe.throw(__(repl("Multiple Price Rules exists with same criteria, please resolve conflict by assigning priority. Price Rules: %(pricing_rule)s", { |
| 1422 | 'pricing_rule': pricing_rule_name |
| 1423 | }))) |
| 1424 | } |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1425 | |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1426 | return pricing_rule_list |
| 1427 | } |
| 1428 | }, |
Rushabh Mehta | d0cee1b | 2016-05-20 12:54:44 +0530 | [diff] [blame] | 1429 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1430 | validate_warehouse: function () { |
| 1431 | if (this.items[0].is_stock_item && !this.items[0].default_warehouse && !this.pos_profile_data['warehouse']) { |
Neil Trini Lasrado | 5ddd7ac | 2016-10-24 15:25:33 +0530 | [diff] [blame] | 1432 | frappe.throw(__("Default warehouse is required for selected item")) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1433 | } |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1434 | }, |
| 1435 | |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1436 | get_actual_qty: function (item) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1437 | this.actual_qty = 0.0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1438 | |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1439 | var warehouse = this.pos_profile_data['warehouse'] || item.default_warehouse; |
rohitwaghchaure | c13dbd4 | 2017-02-01 18:07:22 +0530 | [diff] [blame] | 1440 | if (warehouse && this.bin_data[item.item_code]) { |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1441 | this.actual_qty = this.bin_data[item.item_code][warehouse] || 0; |
Rohit Waghchaure | 80f5bb6 | 2016-11-27 12:04:12 +0530 | [diff] [blame] | 1442 | this.actual_qty_dict[item.item_code] = this.actual_qty |
Rohit Waghchaure | a27c417 | 2016-11-20 23:41:13 +0530 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | return this.actual_qty |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 1446 | } |
| 1447 | }) |