add component support

This commit is contained in:
King Kévin 2023-01-30 04:18:56 +01:00
parent 23b83144a8
commit 7c5aa6e8f3
3 changed files with 86 additions and 49 deletions

View File

@ -42,6 +42,10 @@
<tbody id="properties"></tbody> <tbody id="properties"></tbody>
</table> </table>
<div class="hscroll" id="attachments"></div> <div class="hscroll" id="attachments"></div>
<table>
<thead><tr><th>component</th><th>quantity</th></tr></thead>
<tbody id="components"></tbody>
</table>
<button type="button" onclick="delete_part()">delete part (including children)</button> <button type="button" onclick="delete_part()">delete part (including children)</button>
<button type="button" onclick="update_part()">update part (or add if not selected)</button> <button type="button" onclick="update_part()">update part (or add if not selected)</button>
</div> </div>

View File

@ -11,7 +11,9 @@ var parts = null;
// part field to populate // part field to populate
const fields = ["name", "description", "details", "package", "manufacturer", "mpn", "family", "datasheet", "page", "location", "stock"]; const fields = ["name", "description", "details", "package", "manufacturer", "mpn", "family", "datasheet", "page", "location", "stock"];
// URLs to set // URLs to set
const urls = ["page","datasheet"]; const urls = ["page", "datasheet"];
// tables with key value information
const tables = ["distributors", "properties", "components"];
function search() function search()
{ {
@ -71,15 +73,11 @@ function clear()
const a = document.getElementById('url_' + field); const a = document.getElementById('url_' + field);
a.href = null; a.href = null;
} }
// clear distributors // clear tables
const distributors = document.getElementById('distributors'); for (const table of tables) {
distributors.innerHTML = null; const body = document.getElementById(table);
// clear properties body.innerHTML = null;
const properties = document.getElementById('properties'); }
properties.innerHTML = null;
// clear attachments
const attachments = document.getElementById('attachments');
attachments.innerHTML = null;
} }
function select_part() function select_part()
@ -137,42 +135,42 @@ function select_part()
tr.appendChild(td_url); tr.appendChild(td_url);
distributors.appendChild(tr); distributors.appendChild(tr);
} }
// add properties // add properties and components
const properties = document.getElementById('properties'); const kv_tables = ['properties', 'components'];
properties.innerHTML = null; for (const kv_table of kv_tables) {
for (const prop in part["properties"]) { const table = document.getElementById(kv_table);
const tr = document.createElement('tr'); table.innerHTML = null;
let td = document.createElement('td'); part[kv_table][""] = ""; // empty field to add
let input = document.createElement('input'); for (const key in part[kv_table]) {
input.type = "text"; const tr = document.createElement('tr');
input.style.width = "95%"; let td = document.createElement('td');
input.value = prop; let input = document.createElement('input');
td.appendChild(input); input.type = "text";
tr.appendChild(td); input.style.width = "95%";
td = document.createElement('td'); input.value = key;
input = document.createElement('input'); td.appendChild(input);
input.type = "text"; tr.appendChild(td);
input.style.width = "95%"; td = document.createElement('td');
input.value = part["properties"][prop].join(","); input = document.createElement('input');
td.appendChild(input); if ('components' == kv_table) {
tr.appendChild(td); input.type = "number";
properties.appendChild(tr); input.min = "0";
input.step = "1";
input.value = part[kv_table][key]
} else {
input.type = "text";
if ("object" == typeof part[kv_table][key]) {
input.value = part[kv_table][key].join(",");
} else {
input.value = part[kv_table][key];
}
}
input.style.width = "95%";
td.appendChild(input);
tr.appendChild(td);
table.appendChild(tr);
}
} }
// add empty property field
let tr = document.createElement('tr');
let td = document.createElement('td');
let input = document.createElement('input');
input.type = "text";
input.style.width = "95%";
td.appendChild(input);
tr.appendChild(td);
td = document.createElement('td');
input = document.createElement('input');
input.type = "text";
input.style.width = "95%";
td.appendChild(input);
tr.appendChild(td);
properties.appendChild(tr);
// add attachments // add attachments
const attachments = document.getElementById('attachments'); const attachments = document.getElementById('attachments');
attachments.innerHTML = null; attachments.innerHTML = null;
@ -245,6 +243,16 @@ function update_part()
part.properties[name] = value.split(","); part.properties[name] = value.split(",");
} }
} }
// get components
part.components = {};
const components = document.getElementById('components');
for (const component of components.rows) {
const name = component.cells[0].firstChild.value;
const quantity = component.cells[1].firstChild.value;
if (name && name.length > 0) {
part.components[name] = parseInt(quantity);
}
}
console.log(part); console.log(part);
var post = new XMLHttpRequest(); var post = new XMLHttpRequest();

View File

@ -139,6 +139,17 @@ def get_part_by_id(id)
if parent then if parent then
part["attachments"] += parent["attachments"] part["attachments"] += parent["attachments"]
end end
# add components for assembly
part["components"] = {}
if parent then
parent["components"].each do |k,v|
part["components"][k] ||= v
end
end
statement = @db.prepare("SELECT part.name AS name, assembly.quantity AS quantity FROM assembly JOIN part ON part.id = assembly.component WHERE assembly.assembled = ?")
statement.execute(id).each do |row|
part["components"][row["name"]] = row["quantity"]
end
# clean up # clean up
delete = ["parent"] delete = ["parent"]
delete.each do |k| delete.each do |k|
@ -297,7 +308,6 @@ def add_part(part)
# update inventory # update inventory
field = "location" field = "location"
part[field] = nil if part[field] and 0 == part[field].length part[field] = nil if part[field] and 0 == part[field].length
part["location"] = nil if part["stock"] and 0 == part["stock"].length
if part[field] then if part[field] then
statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?") statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?")
ref = statement.execute(part[field].downcase).to_a[0] ref = statement.execute(part[field].downcase).to_a[0]
@ -336,10 +346,9 @@ def add_part(part)
end end
# update properties # update properties
field = "properties" field = "properties"
part[field] = nil if part[field] and 0 == part[field].length
delete = @db.prepare("DELETE FROM property_value WHERE part = ?")
delete.execute(part["id"])
if part[field] then if part[field] then
delete = @db.prepare("DELETE FROM property_value WHERE part = ?")
delete.execute(part["id"])
part[field].each do |name,values| part[field].each do |name,values|
next unless values and !values.empty? next unless values and !values.empty?
statement = @db.prepare("SELECT id FROM property WHERE LOWER(name) = ?") statement = @db.prepare("SELECT id FROM property WHERE LOWER(name) = ?")
@ -356,6 +365,22 @@ def add_part(part)
end end
end end
end end
# update components
field = "components"
if part[field] then
delete = @db.prepare("DELETE FROM assembly WHERE assembled = ?")
delete.execute(part["id"])
part[field].each do |name,quantity|
next unless name
statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?")
ref = statement.execute(name.downcase).to_a[0]
#raise StandardError.new("component #{name} does not exist") unless ref
next unless ref
quantity ||= 0
insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)");
insert.execute(part["id"], ref["id"], quantity)
end
end
end end
post '/part' do post '/part' do