|
34 | 34 | f.inputs do |
35 | 35 | f.input :customer, as: :select, collection: Customer.pluck(:company_name, :id) |
36 | 36 | f.input :user, as: :select, collection: User.order(:email).map { |u| ["#{u.email} (#{u.name})", u.id] } |
37 | | - f.input :categories, as: :check_boxes, collection: Category.pluck(:name, :id) |
| 37 | + f.input :categories, as: :check_boxes, |
| 38 | + collection: Category.order(:name).pluck(:name, :id) + [['Items without category', 'other']], |
| 39 | + wrapper_html: { class: 'categories-wrapper' } |
38 | 40 | end |
39 | 41 | div do |
40 | 42 | button_tag 'Load Items from Category', type: 'button', id: 'load-items-button', class: 'button' |
41 | 43 | end |
42 | 44 | f.actions |
43 | | - # JS to load items dynamically |
44 | | - script do |
45 | | - raw <<-JS |
46 | | - document.addEventListener("DOMContentLoaded", function () { |
47 | | - const button = document.getElementById("load-items-button"); |
48 | | - button?.addEventListener("click", function () { |
49 | | - const selectedCategories = Array.from(document.querySelectorAll("input[name='quote[category_ids][]']:checked")).map(cb => cb.value); |
50 | | -
|
51 | | - if (selectedCategories.length === 0) { |
52 | | - alert("Please select at least one category."); |
53 | | - return; |
54 | | - } |
55 | | -
|
56 | | - fetch("/admin/quotes/load_items_from_categories", { |
57 | | - method: "POST", |
58 | | - headers: { |
59 | | - "Content-Type": "application/json", |
60 | | - "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').getAttribute("content") |
61 | | - }, |
62 | | - body: JSON.stringify({ category_ids: selectedCategories }) |
63 | | - }) |
64 | | - .then(response => response.json()) |
65 | | - .then(items => { |
66 | | - const container = document.querySelector(".has_many_container.quote_items"); |
67 | | -
|
68 | | - items.forEach(item => { |
69 | | - const addButton = container.querySelector(".has_many_add"); |
70 | | - addButton.click(); |
71 | | -
|
72 | | - setTimeout(() => { |
73 | | - const lastItemGroup = container.querySelectorAll(".has_many_fields").item(-1); |
74 | | - const itemSelect = lastItemGroup.querySelector("select[id$='_item_id']"); |
75 | | - const pricingParams = lastItemGroup.querySelector("input[id$='_pricing_parameters']"); |
76 | | - const discount = lastItemGroup.querySelector("input[id$='_discount']"); |
77 | | -
|
78 | | - if (itemSelect) { |
79 | | - itemSelect.value = item.item_id; |
80 | | - } |
81 | | - if (pricingParams) { |
82 | | - pricingParams.value = item.pricing_parameters; |
83 | | - } |
84 | | - if (discount) { |
85 | | - discount.value = item.discount; |
86 | | - } |
87 | | - }, 100); |
88 | | - }); |
89 | | - }); |
90 | | - }); |
91 | | - }); |
92 | | - JS |
93 | | - end |
94 | 45 | end |
95 | 46 |
|
96 | 47 | show do |
|
117 | 68 |
|
118 | 69 | collection_action :load_items_from_categories, method: :post do |
119 | 70 | category_ids = params[:category_ids] || [] |
120 | | - items = Item.where(category_id: category_ids) |
| 71 | + |
| 72 | + items = Item.where(category_id: category_ids.reject { |id| id == 'other' }) |
| 73 | + items += Item.where(category_id: nil) if category_ids.include?('other') |
121 | 74 |
|
122 | 75 | render json: items.map { |item| |
123 | 76 | { |
124 | 77 | category_id: item.category_id, |
125 | 78 | item_id: item.id, |
126 | 79 | item_name: item.name, |
127 | | - _pricing_parameters: '', |
| 80 | + item_fixed_parameters: item.fixed_parameters, |
| 81 | + item_pricing_options: item.pricing_options, |
| 82 | + item_open_parameters_label: item.open_parameters_label, |
| 83 | + pricing_parameters: {}, |
128 | 84 | discount: 0 |
129 | 85 | } |
130 | 86 | } |
|
0 commit comments