2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-05-14 13:03:11 +00:00

Adds support for currency display (#277)

* Adds a helper function for rendering currency data

* Update helper functions for StockItem model

* Render purchasePrice correctly for stockitem

* Use currency_formatter library instead of money_formatter

* Add total price display for purchase order

* icon fix
This commit is contained in:
Oliver 2023-03-08 23:42:26 +11:00 committed by GitHub
parent 221920cbbe
commit 347e80d8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 311 additions and 236 deletions

View File

@ -4,6 +4,7 @@
### 0.10.2 - March 2023 ### 0.10.2 - March 2023
--- ---
- Adds support for proper currency rendering
- Fix icon for supplier part detail widget - Fix icon for supplier part detail widget
### 0.10.1 - February 2023 ### 0.10.1 - February 2023

View File

@ -8,6 +8,7 @@
*/ */
import "dart:io"; import "dart:io";
import "package:currency_formatter/currency_formatter.dart";
import "package:audioplayers/audioplayers.dart"; import "package:audioplayers/audioplayers.dart";
import "package:one_context/one_context.dart"; import "package:one_context/one_context.dart";
@ -77,3 +78,23 @@ Future<void> playAudioFile(String path) async {
final player = AudioPlayer(); final player = AudioPlayer();
player.play(AssetSource(path)); player.play(AssetSource(path));
} }
/*
* Helper function for rendering a money / currency object as a String
*/
String renderCurrency(double? amount, String currency, {int decimals = 2}) {
if (amount == null) return "-";
if (amount.isInfinite || amount.isNaN) return "-";
CurrencyFormatterSettings backupSettings = CurrencyFormatterSettings(
symbol: "\$",
symbolSide: SymbolSide.left,
);
return CurrencyFormatter.format(
amount,
CurrencyFormatter.majors[currency.toLowerCase()] ?? backupSettings
);
}

View File

@ -89,6 +89,18 @@ class InvenTreePurchaseOrder extends InvenTreeModel {
bool get isFailed => status == PO_STATUS_CANCELLED || status == PO_STATUS_LOST || status == PO_STATUS_RETURNED; bool get isFailed => status == PO_STATUS_CANCELLED || status == PO_STATUS_LOST || status == PO_STATUS_RETURNED;
double? get totalPrice {
String price = (jsondata["total_price"] ?? "") as String;
if (price.isEmpty) {
return null;
} else {
return double.tryParse(price);
}
}
String get totalPriceCurrency => (jsondata["total_price_currency"] ?? "") as String;
Future<List<InvenTreePOLineItem>> getLineItems() async { Future<List<InvenTreePOLineItem>> getLineItems() async {
final results = await InvenTreePOLineItem().list( final results = await InvenTreePOLineItem().list(

View File

@ -206,6 +206,8 @@ class InvenTreeStockItem extends InvenTreeModel {
}, },
"status": {}, "status": {},
"batch": {}, "batch": {},
"purchase_price": {},
"purchase_price_currency": {},
"packaging": {}, "packaging": {},
"link": {}, "link": {},
}; };
@ -284,13 +286,21 @@ class InvenTreeStockItem extends InvenTreeModel {
int get partId => (jsondata["part"] ?? -1) as int; int get partId => (jsondata["part"] ?? -1) as int;
String get purchasePrice => (jsondata["purchase_price"] ?? "") as String; double? get purchasePrice {
String pp = (jsondata["purchase_price"] ?? "") as String;
if (pp.isEmpty) {
return null;
} else {
return double.tryParse(pp);
}
}
String get purchasePriceCurrency => (jsondata["purchase_price_currency"] ?? "") as String;
bool get hasPurchasePrice { bool get hasPurchasePrice {
double? pp = purchasePrice;
String pp = purchasePrice; return pp != null && pp > 0;
return pp.isNotEmpty && pp.trim() != "-";
} }
int get purchaseOrderId => (jsondata["purchase_order"] ?? -1) as int; int get purchaseOrderId => (jsondata["purchase_order"] ?? -1) as int;
@ -299,321 +309,321 @@ class InvenTreeStockItem extends InvenTreeModel {
bool get isBuilding => (jsondata["is_building"] ?? false) as bool; bool get isBuilding => (jsondata["is_building"] ?? false) as bool;
// Date of last update // Date of last update
DateTime? get updatedDate { DateTime? get updatedDate {
if (jsondata.containsKey("updated")) { if (jsondata.containsKey("updated")) {
return DateTime.tryParse((jsondata["updated"] ?? "") as String); return DateTime.tryParse((jsondata["updated"] ?? "") as String);
} else { } else {
return null; return null;
} }
}
String get updatedDateString {
var _updated = updatedDate;
if (_updated == null) {
return "";
} }
final DateFormat _format = DateFormat("yyyy-MM-dd"); String get updatedDateString {
var _updated = updatedDate;
return _format.format(_updated); if (_updated == null) {
} return "";
}
DateTime? get stocktakeDate { final DateFormat _format = DateFormat("yyyy-MM-dd");
if (jsondata.containsKey("stocktake_date")) {
return DateTime.tryParse((jsondata["stocktake_date"] ?? "") as String);
} else {
return null;
}
}
String get stocktakeDateString { return _format.format(_updated);
var _stocktake = stocktakeDate;
if (_stocktake == null) {
return "";
} }
final DateFormat _format = DateFormat("yyyy-MM-dd"); DateTime? get stocktakeDate {
if (jsondata.containsKey("stocktake_date")) {
return _format.format(_stocktake); return DateTime.tryParse((jsondata["stocktake_date"] ?? "") as String);
} } else {
return null;
String get partName { }
String nm = "";
// Use the detailed part information as priority
if (jsondata.containsKey("part_detail")) {
nm = (jsondata["part_detail"]["full_name"] ?? "") as String;
} }
// Backup if first value fails String get stocktakeDateString {
if (nm.isEmpty) { var _stocktake = stocktakeDate;
nm = (jsondata["part__name"] ?? "") as String;
if (_stocktake == null) {
return "";
}
final DateFormat _format = DateFormat("yyyy-MM-dd");
return _format.format(_stocktake);
} }
return nm; String get partName {
}
String get partDescription { String nm = "";
String desc = "";
// Use the detailed part description as priority // Use the detailed part information as priority
if (jsondata.containsKey("part_detail")) { if (jsondata.containsKey("part_detail")) {
desc = (jsondata["part_detail"]["description"] ?? "") as String; nm = (jsondata["part_detail"]["full_name"] ?? "") as String;
}
// Backup if first value fails
if (nm.isEmpty) {
nm = (jsondata["part__name"] ?? "") as String;
}
return nm;
} }
if (desc.isEmpty) { String get partDescription {
desc = (jsondata["part__description"] ?? "") as String; String desc = "";
// Use the detailed part description as priority
if (jsondata.containsKey("part_detail")) {
desc = (jsondata["part_detail"]["description"] ?? "") as String;
}
if (desc.isEmpty) {
desc = (jsondata["part__description"] ?? "") as String;
}
return desc;
} }
return desc; String get partImage {
} String img = "";
String get partImage { if (jsondata.containsKey("part_detail")) {
String img = ""; img = (jsondata["part_detail"]["thumbnail"] ?? "") as String;
}
if (jsondata.containsKey("part_detail")) { if (img.isEmpty) {
img = (jsondata["part_detail"]["thumbnail"] ?? "") as String; img = (jsondata["part__thumbnail"] ?? "") as String;
}
return img;
} }
if (img.isEmpty) { /*
img = (jsondata["part__thumbnail"] ?? "") as String;
}
return img;
}
/*
* Return the Part thumbnail for this stock item. * Return the Part thumbnail for this stock item.
*/ */
String get partThumbnail { String get partThumbnail {
String thumb = ""; String thumb = "";
thumb = (jsondata["part_detail"]?["thumbnail"] ?? "") as String; thumb = (jsondata["part_detail"]?["thumbnail"] ?? "") as String;
// Use "image" as a backup // Use "image" as a backup
if (thumb.isEmpty) { if (thumb.isEmpty) {
thumb = (jsondata["part_detail"]?["image"] ?? "") as String; thumb = (jsondata["part_detail"]?["image"] ?? "") as String;
}
// Try a different approach
if (thumb.isEmpty) {
thumb = (jsondata["part__thumbnail"] ?? "") as String;
}
// Still no thumbnail? Use the "no image" image
if (thumb.isEmpty) thumb = InvenTreeAPI.staticThumb;
return thumb;
} }
// Try a different approach int get supplierPartId => (jsondata["supplier_part"] ?? -1) as int;
if (thumb.isEmpty) {
thumb = (jsondata["part__thumbnail"] ?? "") as String; String get supplierImage {
String thumb = "";
if (jsondata.containsKey("supplier_part_detail")) {
thumb = (jsondata["supplier_part_detail"]?["supplier_detail"]?["image"] ?? "") as String;
} else if (jsondata.containsKey("supplier_detail")) {
thumb = (jsondata["supplier_detail"]["image"] ?? "") as String;
}
return thumb;
} }
// Still no thumbnail? Use the "no image" image String get supplierName {
if (thumb.isEmpty) thumb = InvenTreeAPI.staticThumb; String sname = "";
return thumb; if (jsondata.containsKey("supplier_detail")) {
} sname = (jsondata["supplier_detail"]["supplier_name"] ?? "") as String;
}
int get supplierPartId => (jsondata["supplier_part"] ?? -1) as int; return sname;
String get supplierImage {
String thumb = "";
if (jsondata.containsKey("supplier_part_detail")) {
thumb = (jsondata["supplier_part_detail"]?["supplier_detail"]?["image"] ?? "") as String;
} else if (jsondata.containsKey("supplier_detail")) {
thumb = (jsondata["supplier_detail"]["image"] ?? "") as String;
} }
return thumb; String get units {
} return (jsondata["part_detail"]?["units"] ?? "") as String;
String get supplierName {
String sname = "";
if (jsondata.containsKey("supplier_detail")) {
sname = (jsondata["supplier_detail"]["supplier_name"] ?? "") as String;
} }
return sname; String get supplierSKU {
} String sku = "";
String get units { if (jsondata.containsKey("supplier_part_detail")) {
return (jsondata["part_detail"]?["units"] ?? "") as String; sku = (jsondata["supplier_part_detail"]["SKU"] ?? "") as String;
} }
String get supplierSKU { return sku;
String sku = "";
if (jsondata.containsKey("supplier_part_detail")) {
sku = (jsondata["supplier_part_detail"]["SKU"] ?? "") as String;
} }
return sku; String get serialNumber => (jsondata["serial"] ?? "") as String;
}
String get serialNumber => (jsondata["serial"] ?? "") as String; double get quantity => double.tryParse(jsondata["quantity"].toString()) ?? 0;
double get quantity => double.tryParse(jsondata["quantity"].toString()) ?? 0; String quantityString({bool includeUnits = false}){
String quantityString({bool includeUnits = false}){ String q = "";
String q = ""; if (allocated > 0) {
q += simpleNumberString(available);
q += " / ";
}
if (allocated > 0) { q += simpleNumberString(quantity);
q += simpleNumberString(available);
q += " / "; if (includeUnits && units.isNotEmpty) {
q += " ${units}";
}
return q;
} }
q += simpleNumberString(quantity); double get allocated => double.tryParse(jsondata["allocated"].toString()) ?? 0;
if (includeUnits && units.isNotEmpty) { double get available => quantity - allocated;
q += " ${units}";
int get locationId => (jsondata["location"] ?? -1) as int;
bool isSerialized() => serialNumber.isNotEmpty && quantity.toInt() == 1;
String serialOrQuantityDisplay() {
if (isSerialized()) {
return "SN ${serialNumber}";
} else if (allocated > 0) {
return "${available} / ${quantity}";
} else {
return simpleNumberString(quantity);
}
} }
return q; String get locationName {
} String loc = "";
double get allocated => double.tryParse(jsondata["allocated"].toString()) ?? 0; if (locationId == -1 || !jsondata.containsKey("location_detail")) return "Unknown Location";
double get available => quantity - allocated; loc = (jsondata["location_detail"]["name"] ?? "") as String;
int get locationId => (jsondata["location"] ?? -1) as int; // Old-style name
if (loc.isEmpty) {
loc = (jsondata["location__name"] ?? "") as String;
}
bool isSerialized() => serialNumber.isNotEmpty && quantity.toInt() == 1; return loc;
String serialOrQuantityDisplay() {
if (isSerialized()) {
return "SN ${serialNumber}";
} else if (allocated > 0) {
return "${available} / ${quantity}";
} else {
return simpleNumberString(quantity);
}
}
String get locationName {
String loc = "";
if (locationId == -1 || !jsondata.containsKey("location_detail")) return "Unknown Location";
loc = (jsondata["location_detail"]["name"] ?? "") as String;
// Old-style name
if (loc.isEmpty) {
loc = (jsondata["location__name"] ?? "") as String;
} }
return loc; String get locationPathString {
}
String get locationPathString { if (locationId == -1 || !jsondata.containsKey("location_detail")) return L10().locationNotSet;
if (locationId == -1 || !jsondata.containsKey("location_detail")) return L10().locationNotSet; String _loc = (jsondata["location_detail"]["pathstring"] ?? "") as String;
String _loc = (jsondata["location_detail"]["pathstring"] ?? "") as String; if (_loc.isNotEmpty) {
return _loc;
if (_loc.isNotEmpty) { } else {
return _loc; return locationName;
} else { }
return locationName;
} }
}
String get displayQuantity { String get displayQuantity {
// Display either quantity or serial number! // Display either quantity or serial number!
if (serialNumber.isNotEmpty) { if (serialNumber.isNotEmpty) {
return "SN: $serialNumber"; return "SN: $serialNumber";
} else { } else {
return simpleNumberString(quantity); return simpleNumberString(quantity);
}
} }
}
@override @override
InvenTreeModel createFromJson(Map<String, dynamic> json) { InvenTreeModel createFromJson(Map<String, dynamic> json) {
return InvenTreeStockItem.fromJson(json); return InvenTreeStockItem.fromJson(json);
} }
/* /*
* Perform stocktake action: * Perform stocktake action:
* *
* - Add * - Add
* - Remove * - Remove
* - Count * - Count
*/ */
Future<bool> adjustStock(String endpoint, double q, {String? notes, int? location}) async { Future<bool> adjustStock(String endpoint, double q, {String? notes, int? location}) async {
// Serialized stock cannot be adjusted (unless it is a "transfer") // Serialized stock cannot be adjusted (unless it is a "transfer")
if (isSerialized() && location == null) { if (isSerialized() && location == null) {
return false; return false;
}
// Cannot handle negative stock
if (q < 0) {
return false;
}
Map<String, dynamic> data = {};
data = {
"items": [
{
"pk": "${pk}",
"quantity": "${quantity}",
}
],
"notes": notes ?? "",
};
if (location != null) {
data["location"] = location;
}
var response = await api.post(
endpoint,
body: data,
);
return response.isValid() && (response.statusCode == 200 || response.statusCode == 201);
} }
// Cannot handle negative stock Future<bool> countStock(double q, {String? notes}) async {
if (q < 0) {
return false; final bool result = await adjustStock("/stock/count/", q, notes: notes);
return result;
} }
Map<String, dynamic> data = {}; Future<bool> addStock(double q, {String? notes}) async {
data = { final bool result = await adjustStock("/stock/add/", q, notes: notes);
"items": [
{
"pk": "${pk}",
"quantity": "${quantity}",
}
],
"notes": notes ?? "",
};
if (location != null) { return result;
data["location"] = location;
} }
var response = await api.post( Future<bool> removeStock(double q, {String? notes}) async {
endpoint,
body: data,
);
return response.isValid() && (response.statusCode == 200 || response.statusCode == 201); final bool result = await adjustStock("/stock/remove/", q, notes: notes);
}
Future<bool> countStock(double q, {String? notes}) async { return result;
final bool result = await adjustStock("/stock/count/", q, notes: notes);
return result;
}
Future<bool> addStock(double q, {String? notes}) async {
final bool result = await adjustStock("/stock/add/", q, notes: notes);
return result;
}
Future<bool> removeStock(double q, {String? notes}) async {
final bool result = await adjustStock("/stock/remove/", q, notes: notes);
return result;
}
Future<bool> transferStock(int location, {double? quantity, String? notes}) async {
double q = this.quantity;
if (quantity != null) {
q = quantity;
} }
final bool result = await adjustStock( Future<bool> transferStock(int location, {double? quantity, String? notes}) async {
"/stock/transfer/",
q,
notes: notes,
location: location,
);
return result; double q = this.quantity;
if (quantity != null) {
q = quantity;
}
final bool result = await adjustStock(
"/stock/transfer/",
q,
notes: notes,
location: location,
);
return result;
}
} }
}
/* /*

View File

@ -1180,6 +1180,9 @@
"tokenMissingFromResponse": "Access token missing from response", "tokenMissingFromResponse": "Access token missing from response",
"@tokenMissingFromResponse": {}, "@tokenMissingFromResponse": {},
"totalPrice": "Total Price",
"@totalPrice": {},
"transfer": "Transfer", "transfer": "Transfer",
"@transfer": { "@transfer": {
"description": "transfer" "description": "transfer"

View File

@ -158,6 +158,14 @@ class _PurchaseOrderDetailState extends RefreshableState<PurchaseOrderDetailWidg
}, },
)); ));
tiles.add(ListTile(
title: Text(L10().totalPrice),
leading: FaIcon(FontAwesomeIcons.dollarSign),
trailing: Text(
renderCurrency(widget.order.totalPrice, widget.order.totalPriceCurrency)
),
));
tiles.add(ListTile( tiles.add(ListTile(
title: Text(L10().received), title: Text(L10().received),
leading: FaIcon(FontAwesomeIcons.clipboardCheck, color: COLOR_CLICK), leading: FaIcon(FontAwesomeIcons.clipboardCheck, color: COLOR_CLICK),
@ -407,7 +415,7 @@ class _PurchaseOrderDetailState extends RefreshableState<PurchaseOrderDetailWidg
onTap: onTabSelectionChanged, onTap: onTabSelectionChanged,
items: [ items: [
BottomNavigationBarItem( BottomNavigationBarItem(
icon: FaIcon(FontAwesomeIcons.info), icon: FaIcon(FontAwesomeIcons.circleInfo),
label: L10().details label: L10().details
), ),
BottomNavigationBarItem( BottomNavigationBarItem(

View File

@ -4,6 +4,7 @@ import "package:font_awesome_flutter/font_awesome_flutter.dart";
import "package:inventree/app_colors.dart"; import "package:inventree/app_colors.dart";
import "package:inventree/barcode.dart"; import "package:inventree/barcode.dart";
import "package:inventree/helpers.dart";
import "package:inventree/l10.dart"; import "package:inventree/l10.dart";
import "package:inventree/api.dart"; import "package:inventree/api.dart";
import "package:inventree/api_form.dart"; import "package:inventree/api_form.dart";
@ -675,7 +676,9 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
ListTile( ListTile(
title: Text(L10().purchasePrice), title: Text(L10().purchasePrice),
leading: FaIcon(FontAwesomeIcons.dollarSign), leading: FaIcon(FontAwesomeIcons.dollarSign),
trailing: Text(widget.item.purchasePrice), trailing: Text(
renderCurrency(widget.item.purchasePrice, widget.item.purchasePriceCurrency)
)
) )
); );
} }

View File

@ -257,6 +257,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.5" version: "1.0.5"
currency_formatter:
dependency: "direct main"
description:
name: currency_formatter
sha256: "24034a969f21a55071b1cf835655c1fb1fd94e3acd498a77283e945002591fb6"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
datetime_picker_formfield: datetime_picker_formfield:
dependency: "direct main" dependency: "direct main"
description: description:
@ -1034,6 +1042,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.1" version: "1.3.1"
universal_io:
dependency: transitive
description:
name: universal_io
sha256: "06866290206d196064fd61df4c7aea1ffe9a4e7c4ccaa8fcded42dd41948005d"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
url_launcher: url_launcher:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -11,6 +11,7 @@ dependencies:
cached_network_image: ^3.2.0 # Download and cache remote images cached_network_image: ^3.2.0 # Download and cache remote images
camera: ^0.10.3 # Camera camera: ^0.10.3 # Camera
cupertino_icons: ^1.0.3 cupertino_icons: ^1.0.3
currency_formatter: ^2.0.0
datetime_picker_formfield: ^2.0.0 # Date / time picker datetime_picker_formfield: ^2.0.0 # Date / time picker
device_info_plus: ^8.0.0 # Information about the device device_info_plus: ^8.0.0 # Information about the device
dropdown_search: ^5.0.5 # Dropdown autocomplete form fields dropdown_search: ^5.0.5 # Dropdown autocomplete form fields