mirror of
https://github.com/inventree/inventree-app.git
synced 2025-05-02 15:28:53 +00:00
Merge pull request #162 from inventree/bom-display
Allows displays of Bill of Materials for assembled parts
This commit is contained in:
commit
9c4f6710ff
69
lib/inventree/bom.dart
Normal file
69
lib/inventree/bom.dart
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import "package:inventree/inventree/model.dart";
|
||||||
|
import "package:inventree/inventree/part.dart";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class representing the BomItem database model
|
||||||
|
*/
|
||||||
|
class InvenTreeBomItem extends InvenTreeModel {
|
||||||
|
|
||||||
|
InvenTreeBomItem() : super();
|
||||||
|
|
||||||
|
InvenTreeBomItem.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
||||||
|
|
||||||
|
@override
|
||||||
|
InvenTreeModel createFromJson(Map<String, dynamic> json) {
|
||||||
|
return InvenTreeBomItem.fromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get URL => "bom/";
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, String> defaultListFilters() {
|
||||||
|
return {
|
||||||
|
"sub_part_detail": "true",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, String> defaultGetFilters() {
|
||||||
|
return {
|
||||||
|
"sub_part_detail": "true",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the 'quantity' value associated with this BomItem
|
||||||
|
double get quantity => double.tryParse(jsondata["quantity"].toString()) ?? 0;
|
||||||
|
|
||||||
|
// Extract the ID of the related part
|
||||||
|
int get partId => int.tryParse(jsondata["part"].toString()) ?? -1;
|
||||||
|
|
||||||
|
// Return a Part instance for the referenced part
|
||||||
|
InvenTreePart? get part {
|
||||||
|
if (jsondata.containsKey("part_detail")) {
|
||||||
|
dynamic data = jsondata["part_detail"] ?? {};
|
||||||
|
if (data is Map<String, dynamic>) {
|
||||||
|
return InvenTreePart.fromJson(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a Part instance for the referenced sub-part
|
||||||
|
InvenTreePart? get subPart {
|
||||||
|
if (jsondata.containsKey("sub_part_detail")) {
|
||||||
|
dynamic data = jsondata["sub_part_detail"] ?? {};
|
||||||
|
if (data is Map<String, dynamic>) {
|
||||||
|
return InvenTreePart.fromJson(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the ID of the related sub-part
|
||||||
|
int get subPartId => int.tryParse(jsondata["sub_part"].toString()) ?? -1;
|
||||||
|
}
|
@ -10,6 +10,9 @@ import "package:inventree/l10.dart";
|
|||||||
import "package:inventree/inventree/model.dart";
|
import "package:inventree/inventree/model.dart";
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class representing the PartCategory database model
|
||||||
|
*/
|
||||||
class InvenTreePartCategory extends InvenTreeModel {
|
class InvenTreePartCategory extends InvenTreeModel {
|
||||||
|
|
||||||
InvenTreePartCategory() : super();
|
InvenTreePartCategory() : super();
|
||||||
@ -70,6 +73,9 @@ class InvenTreePartCategory extends InvenTreeModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class representing the PartTestTemplate database model
|
||||||
|
*/
|
||||||
class InvenTreePartTestTemplate extends InvenTreeModel {
|
class InvenTreePartTestTemplate extends InvenTreeModel {
|
||||||
|
|
||||||
InvenTreePartTestTemplate() : super();
|
InvenTreePartTestTemplate() : super();
|
||||||
@ -122,6 +128,9 @@ class InvenTreePartTestTemplate extends InvenTreeModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class representing the Part database model
|
||||||
|
*/
|
||||||
class InvenTreePart extends InvenTreeModel {
|
class InvenTreePart extends InvenTreeModel {
|
||||||
|
|
||||||
InvenTreePart() : super();
|
InvenTreePart() : super();
|
||||||
@ -219,7 +228,6 @@ class InvenTreePart extends InvenTreeModel {
|
|||||||
return _supplierParts;
|
return _supplierParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Cached list of test templates
|
// Cached list of test templates
|
||||||
List<InvenTreePartTestTemplate> testingTemplates = [];
|
List<InvenTreePartTestTemplate> testingTemplates = [];
|
||||||
|
|
||||||
@ -303,9 +311,6 @@ class InvenTreePart extends InvenTreeModel {
|
|||||||
// Get the number of units being build for this Part
|
// Get the number of units being build for this Part
|
||||||
double get building => double.tryParse(jsondata["building"].toString()) ?? 0;
|
double get building => double.tryParse(jsondata["building"].toString()) ?? 0;
|
||||||
|
|
||||||
// Get the number of BOM items in this Part (if it is an assembly)
|
|
||||||
int get bomItemCount => (jsondata["bom_items"] ?? 0) as int;
|
|
||||||
|
|
||||||
// Get the number of BOMs this Part is used in (if it is a component)
|
// Get the number of BOMs this Part is used in (if it is a component)
|
||||||
int get usedInCount => (jsondata["used_in"] ?? 0) as int;
|
int get usedInCount => (jsondata["used_in"] ?? 0) as int;
|
||||||
|
|
||||||
|
@ -451,7 +451,14 @@ class InvenTreeStockItem extends InvenTreeModel {
|
|||||||
|
|
||||||
String quantityString({bool includeUnits = false}){
|
String quantityString({bool includeUnits = false}){
|
||||||
|
|
||||||
String q = simpleNumberString(quantity);
|
String q = "";
|
||||||
|
|
||||||
|
if (allocated > 0) {
|
||||||
|
q += simpleNumberString(available);
|
||||||
|
q += " / ";
|
||||||
|
}
|
||||||
|
|
||||||
|
q += simpleNumberString(quantity);
|
||||||
|
|
||||||
if (includeUnits && units.isNotEmpty) {
|
if (includeUnits && units.isNotEmpty) {
|
||||||
q += " ${units}";
|
q += " ${units}";
|
||||||
@ -460,6 +467,10 @@ class InvenTreeStockItem extends InvenTreeModel {
|
|||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double get allocated => double.tryParse(jsondata["allocated"].toString()) ?? 0;
|
||||||
|
|
||||||
|
double get available => quantity - allocated;
|
||||||
|
|
||||||
int get locationId => (jsondata["location"] ?? -1) as int;
|
int get locationId => (jsondata["location"] ?? -1) as int;
|
||||||
|
|
||||||
bool isSerialized() => serialNumber.isNotEmpty && quantity.toInt() == 1;
|
bool isSerialized() => serialNumber.isNotEmpty && quantity.toInt() == 1;
|
||||||
@ -467,9 +478,11 @@ class InvenTreeStockItem extends InvenTreeModel {
|
|||||||
String serialOrQuantityDisplay() {
|
String serialOrQuantityDisplay() {
|
||||||
if (isSerialized()) {
|
if (isSerialized()) {
|
||||||
return "SN ${serialNumber}";
|
return "SN ${serialNumber}";
|
||||||
|
} else if (allocated > 0) {
|
||||||
|
return "${available} / ${quantity}";
|
||||||
|
} else {
|
||||||
|
return simpleNumberString(quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
return simpleNumberString(quantity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String get locationName {
|
String get locationName {
|
||||||
|
@ -655,6 +655,9 @@
|
|||||||
"description": "Quantity"
|
"description": "Quantity"
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"quantityAvailable": "Quantity Available",
|
||||||
|
"@quantityAvailable": {},
|
||||||
|
|
||||||
"quantityEmpty": "Quantity is empty",
|
"quantityEmpty": "Quantity is empty",
|
||||||
"@quantityEmpty": {},
|
"@quantityEmpty": {},
|
||||||
|
|
||||||
@ -1119,6 +1122,9 @@
|
|||||||
"valueRequired": "Value is required",
|
"valueRequired": "Value is required",
|
||||||
"@valueRequired": {},
|
"@valueRequired": {},
|
||||||
|
|
||||||
|
"variants": "Variants",
|
||||||
|
"@variants": {},
|
||||||
|
|
||||||
"version": "Version",
|
"version": "Version",
|
||||||
"@version": {},
|
"@version": {},
|
||||||
|
|
||||||
|
113
lib/widget/bom_list.dart
Normal file
113
lib/widget/bom_list.dart
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import "package:flutter/material.dart";
|
||||||
|
|
||||||
|
import "package:inventree/api.dart";
|
||||||
|
import "package:inventree/helpers.dart";
|
||||||
|
import "package:inventree/inventree/bom.dart";
|
||||||
|
import "package:inventree/l10.dart";
|
||||||
|
|
||||||
|
import "package:inventree/inventree/model.dart";
|
||||||
|
import "package:inventree/inventree/part.dart";
|
||||||
|
|
||||||
|
import "package:inventree/widget/paginator.dart";
|
||||||
|
import "package:inventree/widget/part_detail.dart";
|
||||||
|
import "package:inventree/widget/refreshable_state.dart";
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Widget for displaying a list of BomItems for the specified 'parent' Part instance
|
||||||
|
*/
|
||||||
|
class BomList extends StatefulWidget {
|
||||||
|
|
||||||
|
const BomList(this.parent);
|
||||||
|
|
||||||
|
final InvenTreePart parent;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_BomListState createState() => _BomListState(parent);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class _BomListState extends RefreshableState<BomList> {
|
||||||
|
|
||||||
|
_BomListState(this.parent);
|
||||||
|
|
||||||
|
final InvenTreePart parent;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String getAppBarTitle(BuildContext context) => L10().billOfMaterials;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget getBody(BuildContext context) {
|
||||||
|
return PaginatedBomList({
|
||||||
|
"part": parent.pk.toString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create a paginated widget displaying a list of BomItem objects
|
||||||
|
*/
|
||||||
|
class PaginatedBomList extends StatefulWidget {
|
||||||
|
|
||||||
|
const PaginatedBomList(this.filters, {this.onTotalChanged});
|
||||||
|
|
||||||
|
final Map<String, String> filters;
|
||||||
|
|
||||||
|
final Function(int)? onTotalChanged;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_PaginatedBomListState createState() => _PaginatedBomListState(filters, onTotalChanged);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class _PaginatedBomListState extends PaginatedSearchState<PaginatedBomList> {
|
||||||
|
|
||||||
|
_PaginatedBomListState(Map<String, String> filters, this.onTotalChanged) : super(filters);
|
||||||
|
|
||||||
|
Function(int)? onTotalChanged;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<InvenTreePageResponse?> requestPage(int limit, int offset, Map<String, String> params) async {
|
||||||
|
|
||||||
|
final page = await InvenTreeBomItem().listPaginated(limit, offset, filters: params);
|
||||||
|
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget buildItem(BuildContext context, InvenTreeModel model) {
|
||||||
|
|
||||||
|
InvenTreeBomItem bomItem = model as InvenTreeBomItem;
|
||||||
|
|
||||||
|
InvenTreePart? subPart = bomItem.subPart;
|
||||||
|
|
||||||
|
String title = subPart?.fullname ?? "error - no name";
|
||||||
|
String description = subPart?.description ?? "error - no description";
|
||||||
|
|
||||||
|
return ListTile(
|
||||||
|
title: Text(title),
|
||||||
|
subtitle: Text(description),
|
||||||
|
trailing: Text(
|
||||||
|
simpleNumberString(bomItem.quantity),
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
leading: InvenTreeAPI().getImage(
|
||||||
|
subPart?.thumbnail ?? "",
|
||||||
|
width: 40,
|
||||||
|
height: 40,
|
||||||
|
),
|
||||||
|
onTap: subPart == null ? null : () async {
|
||||||
|
InvenTreePart().get(bomItem.subPartId).then((var part) {
|
||||||
|
if (part is InvenTreePart) {
|
||||||
|
Navigator.push(context, MaterialPageRoute(builder: (context) => PartDetailWidget(part)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -2,16 +2,19 @@ import "package:flutter/material.dart";
|
|||||||
|
|
||||||
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||||
|
|
||||||
|
import "package:inventree/api.dart";
|
||||||
import "package:inventree/app_colors.dart";
|
import "package:inventree/app_colors.dart";
|
||||||
import "package:inventree/inventree/stock.dart";
|
import "package:inventree/inventree/stock.dart";
|
||||||
import "package:inventree/l10.dart";
|
import "package:inventree/l10.dart";
|
||||||
import "package:inventree/helpers.dart";
|
import "package:inventree/helpers.dart";
|
||||||
|
import "package:inventree/inventree/part.dart";
|
||||||
|
|
||||||
import "package:inventree/widget/attachment_widget.dart";
|
import "package:inventree/widget/attachment_widget.dart";
|
||||||
|
import "package:inventree/widget/bom_list.dart";
|
||||||
|
import "package:inventree/widget/part_list.dart";
|
||||||
import "package:inventree/widget/part_notes.dart";
|
import "package:inventree/widget/part_notes.dart";
|
||||||
import "package:inventree/widget/progress.dart";
|
import "package:inventree/widget/progress.dart";
|
||||||
import "package:inventree/inventree/part.dart";
|
|
||||||
import "package:inventree/widget/category_display.dart";
|
import "package:inventree/widget/category_display.dart";
|
||||||
import "package:inventree/api.dart";
|
|
||||||
import "package:inventree/widget/refreshable_state.dart";
|
import "package:inventree/widget/refreshable_state.dart";
|
||||||
import "package:inventree/widget/part_image_widget.dart";
|
import "package:inventree/widget/part_image_widget.dart";
|
||||||
import "package:inventree/widget/snacks.dart";
|
import "package:inventree/widget/snacks.dart";
|
||||||
@ -41,6 +44,10 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
|||||||
|
|
||||||
int attachmentCount = 0;
|
int attachmentCount = 0;
|
||||||
|
|
||||||
|
int bomCount = 0;
|
||||||
|
|
||||||
|
int variantCount = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getAppBarTitle(BuildContext context) => L10().partDetails;
|
String getAppBarTitle(BuildContext context) => L10().partDetails;
|
||||||
|
|
||||||
@ -118,6 +125,18 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
|||||||
"part": part.pk.toString()
|
"part": part.pk.toString()
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
bomCount = await InvenTreePart().count(
|
||||||
|
filters: {
|
||||||
|
"in_bom_for": part.pk.toString(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
variantCount = await InvenTreePart().count(
|
||||||
|
filters: {
|
||||||
|
"variant_of": part.pk.toString(),
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future <void> _toggleStar() async {
|
Future <void> _toggleStar() async {
|
||||||
@ -261,12 +280,41 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display number of "variant" parts if any exist
|
||||||
|
if (variantCount > 0) {
|
||||||
|
tiles.add(
|
||||||
|
ListTile(
|
||||||
|
title: Text(L10().variants),
|
||||||
|
leading: FaIcon(FontAwesomeIcons.shapes, color: COLOR_CLICK),
|
||||||
|
trailing: Text(variantCount.toString()),
|
||||||
|
onTap: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => PartList(
|
||||||
|
{
|
||||||
|
"variant_of": part.pk.toString(),
|
||||||
|
},
|
||||||
|
title: L10().variants
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
tiles.add(
|
tiles.add(
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text(L10().availableStock),
|
title: Text(L10().availableStock),
|
||||||
subtitle: Text(L10().stockDetails),
|
subtitle: Text(L10().stockDetails),
|
||||||
leading: FaIcon(FontAwesomeIcons.boxes, color: COLOR_CLICK),
|
leading: FaIcon(FontAwesomeIcons.boxes, color: COLOR_CLICK),
|
||||||
trailing: Text(part.stockString()),
|
trailing: Text(
|
||||||
|
part.stockString(),
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
tabIndex = 1;
|
tabIndex = 1;
|
||||||
@ -296,14 +344,19 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
|||||||
// Tiles for an "assembly" part
|
// Tiles for an "assembly" part
|
||||||
if (part.isAssembly) {
|
if (part.isAssembly) {
|
||||||
|
|
||||||
if (part.bomItemCount > 0) {
|
if (bomCount > 0) {
|
||||||
tiles.add(
|
tiles.add(
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text(L10().billOfMaterials),
|
title: Text(L10().billOfMaterials),
|
||||||
leading: FaIcon(FontAwesomeIcons.thList),
|
leading: FaIcon(FontAwesomeIcons.thList, color: COLOR_CLICK),
|
||||||
trailing: Text("${part.bomItemCount}"),
|
trailing: Text(bomCount.toString()),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
// TODO
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => BomList(part)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -583,7 +636,6 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
|||||||
icon: FaIcon(FontAwesomeIcons.boxes),
|
icon: FaIcon(FontAwesomeIcons.boxes),
|
||||||
label: L10().stock
|
label: L10().stock
|
||||||
),
|
),
|
||||||
// TODO - Add part actions
|
|
||||||
BottomNavigationBarItem(
|
BottomNavigationBarItem(
|
||||||
icon: FaIcon(FontAwesomeIcons.wrench),
|
icon: FaIcon(FontAwesomeIcons.wrench),
|
||||||
label: L10().actions,
|
label: L10().actions,
|
||||||
|
@ -12,23 +12,27 @@ import "package:inventree/l10.dart";
|
|||||||
|
|
||||||
class PartList extends StatefulWidget {
|
class PartList extends StatefulWidget {
|
||||||
|
|
||||||
const PartList(this.filters);
|
const PartList(this.filters, {this.title = ""});
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
|
||||||
final Map<String, String> filters;
|
final Map<String, String> filters;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_PartListState createState() => _PartListState(filters);
|
_PartListState createState() => _PartListState(filters, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class _PartListState extends RefreshableState<PartList> {
|
class _PartListState extends RefreshableState<PartList> {
|
||||||
|
|
||||||
_PartListState(this.filters);
|
_PartListState(this.filters, this.title);
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
|
||||||
final Map<String, String> filters;
|
final Map<String, String> filters;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String getAppBarTitle(BuildContext context) => L10().parts;
|
String getAppBarTitle(BuildContext context) => title.isNotEmpty ? title : L10().parts;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget getBody(BuildContext context) {
|
Widget getBody(BuildContext context) {
|
||||||
|
@ -513,7 +513,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
|
|||||||
} else {
|
} else {
|
||||||
tiles.add(
|
tiles.add(
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text(L10().quantity),
|
title: item.allocated > 0 ? Text(L10().quantityAvailable) : Text(L10().quantity),
|
||||||
leading: FaIcon(FontAwesomeIcons.cubes),
|
leading: FaIcon(FontAwesomeIcons.cubes),
|
||||||
trailing: Text("${item.quantityString()}"),
|
trailing: Text("${item.quantityString()}"),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user