mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
Compare commits
5 Commits
9fd7140e4a
...
lineitems
| Author | SHA1 | Date | |
|---|---|---|---|
| a97d867610 | |||
| 021a9ec0c6 | |||
| f056c27bc5 | |||
| 681747e08b | |||
| eb6beea5fa |
0
app/controllers/line_item_controller.rb
Normal file
0
app/controllers/line_item_controller.rb
Normal file
7
app/models/line_item.rb
Normal file
7
app/models/line_item.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class LineItem < ApplicationRecord
|
||||
belongs_to :issue
|
||||
|
||||
validates :description, presence: true
|
||||
validates :quantity, numericality: { greater_than: 0 }
|
||||
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
||||
end
|
||||
@@ -7,3 +7,6 @@
|
||||
<p>
|
||||
<%= select_estimate %>
|
||||
</p>
|
||||
|
||||
|
||||
<%= render "line_items/issue_form", f: f %>
|
||||
34
app/views/line_items/_issue_form.html.erb
Normal file
34
app/views/line_items/_issue_form.html.erb
Normal file
@@ -0,0 +1,34 @@
|
||||
<% @issue.line_items.build if @issue.line_items.empty? %>
|
||||
|
||||
<div class="box tabular" data-nested-form data-wrapper-selector=".line-item">
|
||||
<p><strong>Line Items</strong></p>
|
||||
|
||||
<table class="list line-items-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Description</th>
|
||||
<th style="width:120px;">Quantity</th>
|
||||
<th style="width:150px;">Unit Price</th>
|
||||
<th style="width:80px;"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody data-nested-form-container>
|
||||
<%= f.fields_for :line_items do |item_form| %>
|
||||
<%= render "line_items/line_item_fields", f: item_form %>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<template data-nested-form-template>
|
||||
<%= f.fields_for :line_items, LineItem.new, child_index: "NEW_RECORD" do |item_form| %>
|
||||
<%= render "line_items/line_item_fields", f: item_form %>
|
||||
<% end %>
|
||||
</template>
|
||||
|
||||
<p>
|
||||
<button type="button" class="icon icon-add" data-nested-form-add>
|
||||
Add Line Item
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
34
app/views/line_items/_line_item_fields.html.erb
Normal file
34
app/views/line_items/_line_item_fields.html.erb
Normal file
@@ -0,0 +1,34 @@
|
||||
<tr class="line-item">
|
||||
<%= f.hidden_field :id %>
|
||||
<%= f.hidden_field :_destroy %>
|
||||
|
||||
<td>
|
||||
<%= f.text_field :description,
|
||||
size: 50,
|
||||
placeholder: "Description",
|
||||
:no_label => true %>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<%= f.number_field :quantity,
|
||||
step: 1,
|
||||
min: 1,
|
||||
style: "width:90px;",
|
||||
:no_label => true %>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<%= f.number_field :unit_price,
|
||||
step: 0.01,
|
||||
style: "width:120px;",
|
||||
:no_label => true %>
|
||||
</td>
|
||||
|
||||
<td style="text-align:center;">
|
||||
<button type="button"
|
||||
class="icon-only icon-del"
|
||||
title="Remove"
|
||||
data-nested-form-remove>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
53
assets/javascripts/nested_form_controller.js
Normal file
53
assets/javascripts/nested_form_controller.js
Normal file
@@ -0,0 +1,53 @@
|
||||
(function () {
|
||||
function initNestedForms() {
|
||||
document.querySelectorAll("[data-nested-form]").forEach(function (wrapper) {
|
||||
if (wrapper.dataset.initialized === "true") return;
|
||||
wrapper.dataset.initialized = "true";
|
||||
|
||||
const container = wrapper.querySelector("[data-nested-form-container]");
|
||||
const template = wrapper.querySelector("[data-nested-form-template]");
|
||||
|
||||
if (!container || !template) return;
|
||||
|
||||
wrapper.addEventListener("click", function (event) {
|
||||
const addButton = event.target.closest("[data-nested-form-add]");
|
||||
const removeButton = event.target.closest("[data-nested-form-remove]");
|
||||
|
||||
// ADD
|
||||
if (addButton) {
|
||||
event.preventDefault();
|
||||
|
||||
const content = template.innerHTML.replace(
|
||||
/NEW_RECORD/g,
|
||||
Date.now().toString()
|
||||
);
|
||||
|
||||
container.insertAdjacentHTML("beforeend", content);
|
||||
}
|
||||
|
||||
// REMOVE
|
||||
if (removeButton) {
|
||||
event.preventDefault();
|
||||
|
||||
const lineItem = removeButton.closest(wrapper.dataset.wrapperSelector);
|
||||
if (!lineItem) return;
|
||||
|
||||
const destroyField = lineItem.querySelector("input[name*='_destroy']");
|
||||
|
||||
if (destroyField) {
|
||||
destroyField.value = "1";
|
||||
lineItem.style.display = "none";
|
||||
} else {
|
||||
lineItem.remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Works for full load
|
||||
document.addEventListener("DOMContentLoaded", initNestedForms);
|
||||
|
||||
// Works for Turbo navigation
|
||||
document.addEventListener("turbo:load", initNestedForms);
|
||||
})();
|
||||
@@ -1,26 +0,0 @@
|
||||
#The License
|
||||
#
|
||||
#Copyright (c) 2016 - 2026 Rick Barrette - All Rights Reserved
|
||||
#
|
||||
#Unauthorized copying of this software and associated documentation files (the "Software"), via any medium is strictly prohibited.
|
||||
#
|
||||
#Proprietary and confidential
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
class CreateLineItems < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
create_table :line_items do |t|
|
||||
t.integer :item_id
|
||||
t.float :amount
|
||||
t.string :description
|
||||
t.float :unit_price
|
||||
t.float :quantity
|
||||
t.boolean :billed
|
||||
end
|
||||
|
||||
add_reference :line_items, :issues, index: true
|
||||
end
|
||||
end
|
||||
@@ -12,6 +12,5 @@ class RemoveQboItems < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
drop_table :qbo_items
|
||||
drop_table :qbo_purchases
|
||||
drop_table :line_items
|
||||
end
|
||||
end
|
||||
|
||||
42
db/migrate/043_create_line_items.rb
Normal file
42
db/migrate/043_create_line_items.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2016 - 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
class CreateLineItems < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :line_items do |t|
|
||||
t.integer :issue_id, null: false
|
||||
|
||||
t.text :description, null: false
|
||||
|
||||
t.decimal :quantity,
|
||||
precision: 15,
|
||||
scale: 4,
|
||||
null: false,
|
||||
default: 0
|
||||
|
||||
t.decimal :unit_price,
|
||||
precision: 15,
|
||||
scale: 4,
|
||||
null: false,
|
||||
default: 0
|
||||
|
||||
t.decimal :line_total,
|
||||
precision: 15,
|
||||
scale: 4,
|
||||
null: false,
|
||||
default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :line_items, :issue_id
|
||||
add_foreign_key :line_items, :issues
|
||||
end
|
||||
end
|
||||
1
init.rb
1
init.rb
@@ -25,6 +25,7 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
Issue.safe_attributes :estimate_id
|
||||
Issue.safe_attributes :invoice_id
|
||||
User.safe_attributes :employee_id
|
||||
Issue.safe_attributes :line_items_attributes
|
||||
TimeEntry.safe_attributes :billed
|
||||
|
||||
# set per_page globally
|
||||
|
||||
@@ -62,7 +62,8 @@ module RedmineQbo
|
||||
locals: {
|
||||
search_customer: search_customer,
|
||||
customer_id: customer_id,
|
||||
select_estimate: select_estimate
|
||||
select_estimate: select_estimate,
|
||||
f: context[:form]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -18,7 +18,8 @@ module RedmineQbo
|
||||
safe_join([
|
||||
javascript_include_tag( 'application.js', plugin: :redmine_qbo),
|
||||
javascript_include_tag( 'autocomplete-rails.js', plugin: :redmine_qbo),
|
||||
javascript_include_tag( 'checkbox_controller.js', plugin: :redmine_qbo)
|
||||
javascript_include_tag( 'checkbox_controller.js', plugin: :redmine_qbo),
|
||||
javascript_include_tag( 'nested_form_controller.js', plugin: :redmine_qbo)
|
||||
])
|
||||
end
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ module RedmineQbo
|
||||
belongs_to :customer_token, primary_key: :id
|
||||
belongs_to :estimate, primary_key: :id
|
||||
has_and_belongs_to_many :invoices
|
||||
has_many :line_items, dependent: :destroy
|
||||
accepts_nested_attributes_for :line_items, allow_destroy: true
|
||||
|
||||
before_save :titlize_subject
|
||||
after_commit :enqueue_billing, on: :update
|
||||
|
||||
Reference in New Issue
Block a user