2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 05:26:47 +00:00
Oliver Walters a7d11faec8 Add "QuantityField"
Juicy juicy refactoring
2020-04-18 22:07:02 +10:00

266 lines
5.3 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'model.dart';
import 'package:InvenTree/api.dart';
class InvenTreeStockItem extends InvenTreeModel {
@override
String NAME = "StockItem";
@override
String URL = "stock/";
@override
Map<String, String> defaultGetFilters() {
var headers = new Map<String, String>();
headers["part_detail"] = "true";
headers["location_detail"] = "true";
headers["supplier_detail"] = "true";
return headers;
}
InvenTreeStockItem() : super();
InvenTreeStockItem.fromJson(Map<String, dynamic> json) : super.fromJson(json) {
// TODO
}
int get partId => jsondata['part'] ?? -1;
int get trackingItemCount => jsondata['tracking_items'] as int ?? 0;
String get partName {
String nm = '';
// Use the detailed part information as priority
if (jsondata.containsKey('part_detail')) {
nm = jsondata['part_detail']['full_name'] ?? '';
}
if (nm.isEmpty) {
nm = jsondata['part__name'] ?? '';
}
return nm;
}
String get partDescription {
String desc = '';
// Use the detailed part description as priority
if (jsondata.containsKey('part_detail')) {
desc = jsondata['part_detail']['description'] ?? '';
}
if (desc.isEmpty) {
desc = jsondata['part__description'] ?? '';
}
return desc;
}
String get partImage {
String img = '';
if (jsondata.containsKey('part_detail')) {
img = jsondata['part_detail']['thumbnail'] ?? '';
}
if (img.isEmpty) {
img = jsondata['part__thumbnail'] ?? '';
}
return img;
}
String get partThumbnail {
String thumb = jsondata['part__thumbnail'] as String ?? '';
if (thumb.isEmpty) thumb = InvenTreeAPI.staticThumb;
return thumb;
}
int get supplierPartId => jsondata['supplier_part'] as int ?? -1;
String get supplierImage {
String thumb = '';
if (jsondata.containsKey("supplier_detail")) {
thumb = jsondata['supplier_detail']['supplier_logo'] ?? '';
}
return thumb;
}
String get supplierName {
String sname = '';
if (jsondata.containsKey("supplier_detail")) {
sname = jsondata["supplier_detail"]["supplier_name"] ?? '';
}
return sname;
}
String get supplierSKU {
String sku = '';
if (jsondata.containsKey("supplier_detail")) {
sku = jsondata["supplier_detail"]["SKU"] ?? '';
}
return sku;
}
int get serialNumber => jsondata['serial'] as int ?? null;
double get quantity => double.tryParse(jsondata['quantity'].toString() ?? '0');
int get locationId => jsondata['location'] as int ?? -1;
bool isSerialized() => serialNumber != null && quantity.toInt() == 1;
String get locationName {
String loc = '';
if (jsondata.containsKey('location_detail')) {
loc = jsondata['location_detail']['name'] ?? '';
}
if (loc.isEmpty) {
loc = jsondata['location__name'] ?? '';
}
return loc;
}
String get locationPathString {
String path = '';
if (jsondata.containsKey('location_detail')) {
path = jsondata['location_detail']['pathstring'] ?? '';
}
return path;
}
String get displayQuantity {
// Display either quantity or serial number!
if (serialNumber != null) {
return "SN: $serialNumber";
} else {
return quantity.toString().trim();
}
}
@override
InvenTreeModel createFromJson(Map<String, dynamic> json) {
var item = InvenTreeStockItem.fromJson(json);
// TODO?
return item;
}
Future<http.Response> countStock(double quan, {String notes}) async {
// Cannot 'count' a serialized StockItem
if (isSerialized()) {
return null;
}
// Cannot count negative stock
if (quan < 0) {
return null;
}
return api.post("/stock/count/", body: {
"item": {
"pk": "${pk}",
"quantity": "${quan}",
},
"notes": notes ?? '',
});
}
Future<http.Response> addStock(double quan, {String notes}) async {
if (isSerialized() || quan <= 0) return null;
return api.post("/stock/add/", body: {
"item": {
"pk": "${pk}",
"quantity": "${quan}",
},
"notes": notes ?? '',
});
}
Future<http.Response> removeStock(double quan, {String notes}) async {
if (isSerialized() || quan <= 0) return null;
return api.post("/stock/remove/", body: {
"item": {
"pk": "${pk}",
"quantity": "${quan}",
},
"notes": notes ?? '',
});
}
}
class InvenTreeStockLocation extends InvenTreeModel {
@override
String NAME = "StockLocation";
@override
String URL = "stock/location/";
String get pathstring => jsondata['pathstring'] ?? '';
String get parentpathstring {
// TODO - Drive the refactor tractor through this
List<String> psplit = pathstring.split('/');
if (psplit.length > 0) {
psplit.removeLast();
}
String p = psplit.join('/');
if (p.isEmpty) {
p = "Top level stock location";
}
return p;
}
int get itemcount => jsondata['items'] ?? 0;
InvenTreeStockLocation() : super();
InvenTreeStockLocation.fromJson(Map<String, dynamic> json) : super.fromJson(json) {
// TODO
}
@override
InvenTreeModel createFromJson(Map<String, dynamic> json) {
var loc = InvenTreeStockLocation.fromJson(json);
return loc;
}
}