Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 1 | frappe.provide("erpnext.stock"); |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 2 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 3 | erpnext.stock.ItemDashboard = class ItemDashboard { |
| 4 | constructor(opts) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 5 | $.extend(this, opts); |
| 6 | this.make(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 7 | } |
| 8 | make() { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 9 | var me = this; |
| 10 | this.start = 0; |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 11 | if (!this.sort_by) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 12 | this.sort_by = "projected_qty"; |
| 13 | this.sort_order = "asc"; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 14 | } |
| 15 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 16 | this.content = $(frappe.render_template("item_dashboard")).appendTo(this.parent); |
| 17 | this.result = this.content.find(".result"); |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 18 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 19 | this.content.on("click", ".btn-move", function () { |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 20 | handle_move_add($(this), "Move"); |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 21 | }); |
| 22 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 23 | this.content.on("click", ".btn-add", function () { |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 24 | handle_move_add($(this), "Add"); |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 25 | }); |
| 26 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 27 | this.content.on("click", ".btn-edit", function () { |
| 28 | let item = unescape($(this).attr("data-item")); |
| 29 | let warehouse = unescape($(this).attr("data-warehouse")); |
| 30 | let company = unescape($(this).attr("data-company")); |
| 31 | frappe.db.get_value( |
| 32 | "Putaway Rule", |
| 33 | { |
| 34 | item_code: item, |
| 35 | warehouse: warehouse, |
| 36 | company: company, |
| 37 | }, |
| 38 | "name", |
| 39 | (r) => { |
| 40 | frappe.set_route("Form", "Putaway Rule", r.name); |
| 41 | } |
| 42 | ); |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 43 | }); |
| 44 | |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 45 | function handle_move_add(element, action) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 46 | let item = unescape(element.attr("data-item")); |
| 47 | let warehouse = unescape(element.attr("data-warehouse")); |
| 48 | let actual_qty = unescape(element.attr("data-actual_qty")); |
| 49 | let disable_quick_entry = Number(unescape(element.attr("data-disable_quick_entry"))); |
s-aga-r | dc0ddf8 | 2023-02-03 18:08:34 +0530 | [diff] [blame] | 50 | let entry_type = action === "Move" ? "Material Transfer" : "Material Receipt"; |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 51 | |
Shivam Mishra | fd6ff62 | 2019-07-16 12:32:50 +0530 | [diff] [blame] | 52 | if (disable_quick_entry) { |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 53 | open_stock_entry(item, warehouse, entry_type); |
Shivam Mishra | fd6ff62 | 2019-07-16 12:32:50 +0530 | [diff] [blame] | 54 | } else { |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 55 | if (action === "Add") { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 56 | let rate = unescape($(this).attr("data-rate")); |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 57 | erpnext.stock.move_item(item, null, warehouse, actual_qty, rate, function () { |
| 58 | me.refresh(); |
| 59 | }); |
| 60 | } else { |
| 61 | erpnext.stock.move_item(item, warehouse, null, actual_qty, null, function () { |
| 62 | me.refresh(); |
| 63 | }); |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 64 | } |
Shivam Mishra | fd6ff62 | 2019-07-16 12:32:50 +0530 | [diff] [blame] | 65 | } |
Shivam Mishra | 5738c93 | 2019-07-16 12:52:19 +0530 | [diff] [blame] | 66 | } |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 67 | |
| 68 | function open_stock_entry(item, warehouse, entry_type) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 69 | frappe.model.with_doctype("Stock Entry", function () { |
| 70 | var doc = frappe.model.get_new_doc("Stock Entry"); |
s-aga-r | dc0ddf8 | 2023-02-03 18:08:34 +0530 | [diff] [blame] | 71 | if (entry_type) { |
| 72 | doc.stock_entry_type = entry_type; |
| 73 | } |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 74 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 75 | var row = frappe.model.add_child(doc, "items"); |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 76 | row.item_code = item; |
s-aga-r | dc0ddf8 | 2023-02-03 18:08:34 +0530 | [diff] [blame] | 77 | |
| 78 | if (entry_type === "Material Transfer") { |
| 79 | row.s_warehouse = warehouse; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 80 | } else { |
s-aga-r | dc0ddf8 | 2023-02-03 18:08:34 +0530 | [diff] [blame] | 81 | row.t_warehouse = warehouse; |
| 82 | } |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 83 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 84 | frappe.set_route("Form", doc.doctype, doc.name); |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 85 | }); |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame] | 86 | } |
| 87 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 88 | // more |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 89 | this.content.find(".btn-more").on("click", function () { |
marination | a5d8d32 | 2020-12-09 16:27:18 +0530 | [diff] [blame] | 90 | me.start += me.page_length; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 91 | me.refresh(); |
| 92 | }); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 93 | } |
| 94 | refresh() { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 95 | if (this.before_refresh) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 96 | this.before_refresh(); |
| 97 | } |
| 98 | |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 99 | let args = { |
| 100 | item_code: this.item_code, |
| 101 | warehouse: this.warehouse, |
| 102 | parent_warehouse: this.parent_warehouse, |
| 103 | item_group: this.item_group, |
| 104 | company: this.company, |
| 105 | start: this.start, |
| 106 | sort_by: this.sort_by, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 107 | sort_order: this.sort_order, |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 108 | }; |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 109 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 110 | var me = this; |
| 111 | frappe.call({ |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 112 | method: this.method, |
| 113 | args: args, |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 114 | callback: function (r) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 115 | me.render(r.message); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 116 | if (me.after_refresh) { |
Devin Slauenwhite | 123920d | 2022-12-29 00:01:26 -0500 | [diff] [blame] | 117 | me.after_refresh(); |
| 118 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 119 | }, |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 120 | }); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 121 | } |
| 122 | render(data) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 123 | if (this.start === 0) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 124 | this.max_count = 0; |
| 125 | this.result.empty(); |
| 126 | } |
| 127 | |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 128 | let context = ""; |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 129 | if (this.page_name === "warehouse-capacity-summary") { |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 130 | context = this.get_capacity_dashboard_data(data); |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 131 | } else { |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 132 | context = this.get_item_dashboard_data(data, this.max_count, true); |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 133 | } |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 134 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 135 | // show more button |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 136 | if (data && data.length === this.page_length + 1) { |
| 137 | this.content.find(".more").removeClass("hidden"); |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 138 | |
| 139 | // remove the last element |
| 140 | data.splice(-1); |
| 141 | } else { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 142 | this.content.find(".more").addClass("hidden"); |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 143 | } |
| 144 | |
Vinayak Jethe | 2c8ce5a | 2018-01-24 13:51:26 +0530 | [diff] [blame] | 145 | // If not any stock in any warehouses provide a message to end user |
Stavros Anastasiadis | 58b5878 | 2017-10-05 16:14:50 +0300 | [diff] [blame] | 146 | if (context.data.length > 0) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 147 | this.content.find(".result").css("text-align", "unset"); |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 148 | $(frappe.render_template(this.template, context)).appendTo(this.result); |
Stavros Anastasiadis | 58b5878 | 2017-10-05 16:14:50 +0300 | [diff] [blame] | 149 | } else { |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 150 | var message = __("No Stock Available Currently"); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 151 | this.content.find(".result").css("text-align", "center"); |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 152 | |
marination | a97eb6a | 2021-02-07 22:27:40 +0530 | [diff] [blame] | 153 | $(`<div class='text-muted' style='margin: 20px 5px;'> |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 154 | ${message} </div>`).appendTo(this.result); |
Stavros Anastasiadis | 58b5878 | 2017-10-05 16:14:50 +0300 | [diff] [blame] | 155 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 156 | } |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 157 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 158 | get_item_dashboard_data(data, max_count, show_item) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 159 | if (!max_count) max_count = 0; |
| 160 | if (!data) data = []; |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 161 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 162 | data.forEach(function (d) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 163 | d.actual_or_pending = |
| 164 | d.projected_qty + |
| 165 | d.reserved_qty + |
| 166 | d.reserved_qty_for_production + |
| 167 | d.reserved_qty_for_sub_contract; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 168 | d.pending_qty = 0; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 169 | d.total_reserved = |
| 170 | d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract; |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 171 | if (d.actual_or_pending > d.actual_qty) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 172 | d.pending_qty = d.actual_or_pending - d.actual_qty; |
| 173 | } |
| 174 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 175 | max_count = Math.max(d.actual_or_pending, d.actual_qty, d.total_reserved, max_count); |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 176 | }); |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 177 | |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 178 | let can_write = 0; |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 179 | if (frappe.boot.user.can_write.indexOf("Stock Entry") >= 0) { |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 180 | can_write = 1; |
| 181 | } |
| 182 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 183 | return { |
| 184 | data: data, |
| 185 | max_count: max_count, |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 186 | can_write: can_write, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 187 | show_item: show_item || false, |
marination | b8aeb9e | 2021-01-05 12:16:25 +0530 | [diff] [blame] | 188 | }; |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 189 | } |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 190 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 191 | get_capacity_dashboard_data(data) { |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 192 | if (!data) data = []; |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 193 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 194 | data.forEach(function (d) { |
| 195 | d.color = d.percent_occupied >= 80 ? "#f8814f" : "#2490ef"; |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 196 | }); |
| 197 | |
| 198 | let can_write = 0; |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 199 | if (frappe.boot.user.can_write.indexOf("Putaway Rule") >= 0) { |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 200 | can_write = 1; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 201 | } |
marination | 1087d97 | 2020-11-26 10:45:44 +0530 | [diff] [blame] | 202 | |
| 203 | return { |
| 204 | data: data, |
| 205 | can_write: can_write, |
marination | 0f3cfc5 | 2020-12-08 19:11:51 +0530 | [diff] [blame] | 206 | }; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 207 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 208 | }; |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 209 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 210 | erpnext.stock.move_item = function (item, source, target, actual_qty, rate, callback) { |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 211 | var dialog = new frappe.ui.Dialog({ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 212 | title: target ? __("Add Item") : __("Move Item"), |
| 213 | fields: [ |
| 214 | { |
| 215 | fieldname: "item_code", |
| 216 | label: __("Item"), |
| 217 | fieldtype: "Link", |
| 218 | options: "Item", |
| 219 | read_only: 1, |
| 220 | }, |
| 221 | { |
| 222 | fieldname: "source", |
| 223 | label: __("Source Warehouse"), |
| 224 | fieldtype: "Link", |
| 225 | options: "Warehouse", |
| 226 | read_only: 1, |
| 227 | }, |
| 228 | { |
| 229 | fieldname: "target", |
| 230 | label: __("Target Warehouse"), |
| 231 | fieldtype: "Link", |
| 232 | options: "Warehouse", |
| 233 | reqd: 1, |
| 234 | get_query() { |
| 235 | return { |
| 236 | filters: { |
| 237 | is_group: 0, |
| 238 | }, |
| 239 | }; |
| 240 | }, |
| 241 | }, |
| 242 | { |
| 243 | fieldname: "qty", |
| 244 | label: __("Quantity"), |
| 245 | reqd: 1, |
| 246 | fieldtype: "Float", |
| 247 | description: __("Available {0}", [actual_qty]), |
| 248 | }, |
| 249 | { |
| 250 | fieldname: "rate", |
| 251 | label: __("Rate"), |
| 252 | fieldtype: "Currency", |
| 253 | hidden: 1, |
| 254 | }, |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 255 | ], |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 256 | }); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 257 | dialog.show(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 258 | dialog.get_field("item_code").set_input(item); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 259 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 260 | if (source) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 261 | dialog.get_field("source").set_input(source); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 262 | } else { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 263 | dialog.get_field("source").df.hidden = 1; |
| 264 | dialog.get_field("source").refresh(); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 267 | if (rate) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 268 | dialog.get_field("rate").set_value(rate); |
| 269 | dialog.get_field("rate").df.hidden = 0; |
| 270 | dialog.get_field("rate").refresh(); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 271 | } |
| 272 | |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 273 | if (target) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 274 | dialog.get_field("target").df.read_only = 1; |
| 275 | dialog.get_field("target").value = target; |
| 276 | dialog.get_field("target").refresh(); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 277 | } |
| 278 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 279 | dialog.set_primary_action(__("Create Stock Entry"), function () { |
Devin Slauenwhite | cdf631b | 2022-06-29 02:36:47 -0400 | [diff] [blame] | 280 | if (source && (dialog.get_value("qty") == 0 || dialog.get_value("qty") > actual_qty)) { |
| 281 | frappe.msgprint(__("Quantity must be greater than zero, and less or equal to {0}", [actual_qty])); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | if (dialog.get_value("source") === dialog.get_value("target")) { |
| 286 | frappe.msgprint(__("Source and target warehouse must be different")); |
| 287 | return; |
| 288 | } |
| 289 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 290 | frappe.model.with_doctype("Stock Entry", function () { |
| 291 | let doc = frappe.model.get_new_doc("Stock Entry"); |
| 292 | doc.from_warehouse = dialog.get_value("source"); |
| 293 | doc.to_warehouse = dialog.get_value("target"); |
Ankush Menat | 0bed592 | 2022-01-26 01:01:33 +0530 | [diff] [blame] | 294 | doc.stock_entry_type = doc.from_warehouse ? "Material Transfer" : "Material Receipt"; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 295 | let row = frappe.model.add_child(doc, "items"); |
| 296 | row.item_code = dialog.get_value("item_code"); |
| 297 | row.s_warehouse = dialog.get_value("source"); |
| 298 | row.t_warehouse = dialog.get_value("target"); |
| 299 | row.qty = dialog.get_value("qty"); |
Ankush Menat | 0bed592 | 2022-01-26 01:01:33 +0530 | [diff] [blame] | 300 | row.conversion_factor = 1; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 301 | row.transfer_qty = dialog.get_value("qty"); |
| 302 | row.basic_rate = dialog.get_value("rate"); |
| 303 | frappe.set_route("Form", doc.doctype, doc.name); |
Shivam Mishra | 444091b | 2019-07-10 10:22:08 +0530 | [diff] [blame] | 304 | }); |
| 305 | }); |
Anurag Mishra | abf974d | 2021-03-17 18:40:21 +0530 | [diff] [blame] | 306 | }; |