From 0d1cfdb88a903eaba3b48de36355cd0e009d874d Mon Sep 17 00:00:00 2001 From: Christian Anetzberger Date: Tue, 27 Feb 2024 07:53:35 +0000 Subject: [PATCH] Added Scaled Pricing Dialog to item --- label_printing/api.py | 12 +-- label_printing/label_printing/api.py | 4 - label_printing/modules.txt | 3 +- .../public/js/label_printing.bundle.js | 3 +- label_printing/public/js/scaled_pricing.js | 90 +++++++++++++++++++ label_printing/scaled_pricing/__init__.py | 0 .../scaled_pricing/scaled_pricing.py | 47 ++++++++++ 7 files changed, 142 insertions(+), 17 deletions(-) create mode 100644 label_printing/public/js/scaled_pricing.js create mode 100644 label_printing/scaled_pricing/__init__.py create mode 100644 label_printing/scaled_pricing/scaled_pricing.py diff --git a/label_printing/api.py b/label_printing/api.py index 49bdd16..21ed36d 100644 --- a/label_printing/api.py +++ b/label_printing/api.py @@ -84,14 +84,4 @@ def print_label_by_server(doctype, name, qty, printer_setting, print_format=None @frappe.whitelist() def get_associated_stockentry(workorder): - return frappe.get_last_doc('Stock Entry', filters={"work_order": workorder}) - -# @frappe.whitelist() -# def get_label_printers(): -# label_printing_settings = frappe.get_doc('Label Printing Settings') -# label_printer_names = [] -# -# for x in label_printing_settings.label_printers: -# label_printer_names.append(x.label_printer) -# -# return(label_printer_names) + return frappe.get_last_doc('Stock Entry', filters={"work_order": workorder}) \ No newline at end of file diff --git a/label_printing/label_printing/api.py b/label_printing/label_printing/api.py index d16e487..84f5491 100644 --- a/label_printing/label_printing/api.py +++ b/label_printing/label_printing/api.py @@ -7,9 +7,6 @@ import json def create_labels(values): values = json.loads(values) - print(values) - - print(values["item_code"]) doc = frappe.new_doc('Label') doc.item_code = values["item_code"] @@ -20,7 +17,6 @@ def create_labels(values): ignore_if_duplicate=True, # dont insert if DuplicateEntryError is thrown ignore_mandatory=True # insert even if mandatory fields are not set ) - print(res) # print_by_server("Label", "Label-00001", "labelprinter", "Labels") return 200 diff --git a/label_printing/modules.txt b/label_printing/modules.txt index a1ac6e4..7add0ce 100644 --- a/label_printing/modules.txt +++ b/label_printing/modules.txt @@ -1 +1,2 @@ -Label Printing \ No newline at end of file +Label Printing +Scaled Pricing \ No newline at end of file diff --git a/label_printing/public/js/label_printing.bundle.js b/label_printing/public/js/label_printing.bundle.js index 5bf2e98..e2d2177 100644 --- a/label_printing/public/js/label_printing.bundle.js +++ b/label_printing/public/js/label_printing.bundle.js @@ -1 +1,2 @@ -import './label_printing_desk'; \ No newline at end of file +import './label_printing_desk'; +import './scaled_pricing'; \ No newline at end of file diff --git a/label_printing/public/js/scaled_pricing.js b/label_printing/public/js/scaled_pricing.js new file mode 100644 index 0000000..87ae048 --- /dev/null +++ b/label_printing/public/js/scaled_pricing.js @@ -0,0 +1,90 @@ +const table_fields = [ + { + fieldname: "min_qty", + fieldtype: "Float", + in_list_view: 1, + label: "Min Qty", + mandatory: true + }, + { + fieldname: "rate", + fieldtype: "Currency", + in_list_view: 1, + label: "Rate", + mandatory: true + }, + { + fieldname: "priority", + fieldtype: "Int", + in_list_view: 1, + label: "Priority", + read_only: true + }, + { + fieldname: "name", + fieldtype: "Link", + options: "Pricing Rule", + in_list_view: 1, + label: "Pricing Rule", + read_only: true + }, +]; + + + +frappe.ui.form.on('Item', { + refresh(frm) { + frm.add_custom_button(__('Staggered Prices'), function () { + + frappe.xcall('label_printing.scaled_pricing.scaled_pricing.get_price_lists', { + item_code: frm.doc.item_code + }).then((result) => { + generate_dialog(result, frm) + }) + }); + } +}) + +function generate_dialog(data, frm) { + let d = new frappe.ui.Dialog({ + title: frm.doc.name, + fields: [ + { + fieldname: "scaled_prices", + fieldtype: "Table", + label: "Scaled Prices", + cannot_add_rows: false, + in_place_edit: true, + reqd: 0, + data: data, + fields: table_fields, + }, + ], + size: 'large', + primary_action_label: 'Submit', + primary_action(values) { + if (values.scaled_prices.length > 20) { + frappe.throw(__('You can add a maximum of 20 Scaled Prices!')) + } else { + frappe.xcall('label_printing.scaled_pricing.scaled_pricing.change_price_lists', { + values: values, + frm: frm.doc.name + }) + .then((result) => { + d.hide() + if (result === 200) { + frappe.show_alert({ + message: __('Price Lists have been modified!'), + indicator: 'green' + }, 5); + d.hide(); + } else { + frappe.throw(__("Couldn't change Pricelists!")) + } + }) + } + } + }); + + d.show(); +} \ No newline at end of file diff --git a/label_printing/scaled_pricing/__init__.py b/label_printing/scaled_pricing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/label_printing/scaled_pricing/scaled_pricing.py b/label_printing/scaled_pricing/scaled_pricing.py new file mode 100644 index 0000000..c8b231d --- /dev/null +++ b/label_printing/scaled_pricing/scaled_pricing.py @@ -0,0 +1,47 @@ +import frappe +import json + + +@frappe.whitelist() +def get_price_lists(item_code): + pricing_rule_item_codes = frappe.db.get_all("Pricing Rule Item Code", filters={'item_code': item_code}, fields=["parent"]) + + pricing_rules = [] + + for item in pricing_rule_item_codes: + pricing_rule = frappe.get_doc('Pricing Rule', item.parent) + pricing_rules.append(pricing_rule) + + + return pricing_rules + + +@frappe.whitelist() +def change_price_lists(values, frm): + values = json.loads(values) + scaled_prices = values['scaled_prices'] + + scaled_prices = sorted(scaled_prices, key=lambda d: d['min_qty']) + + for index, scaled_price in enumerate(scaled_prices): + doc = None + if frappe.db.exists('Pricing Rule', scaled_price["name"]): + doc = frappe.get_doc('Pricing Rule', scaled_price["name"]) + + else: + doc = frappe.new_doc('Pricing Rule') + doc.append("items", { + "item_code": frm, + "uom": "Stk" + }) + doc.selling = 1 + + + doc.min_qty = scaled_price["min_qty"] + doc.rate = scaled_price["rate"] + doc.title = "%s - %s" % (scaled_price["rate"], frm) + doc.priority = index + 1 + + doc.save() + + return 200 \ No newline at end of file