mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-30 21:05:42 +00:00 
			
		
		
		
	Add barcode handler to scan stock item into location
This commit is contained in:
		| @@ -339,6 +339,40 @@ class _QRViewState extends State<InvenTreeQRView> { | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class StockItemScanIntoLocationHandler extends BarcodeHandler { | ||||||
|  |   /** | ||||||
|  |    * Barcode handler for scanning a provided StockItem into a scanned StockLocation | ||||||
|  |    */ | ||||||
|  |  | ||||||
|  |   final InvenTreeStockItem item; | ||||||
|  |  | ||||||
|  |   StockItemScanIntoLocationHandler(this.item); | ||||||
|  |  | ||||||
|  |   @override | ||||||
|  |   Future<void> onBarcodeMatched(Map<String, dynamic> data) { | ||||||
|  |     // If the barcode points to a 'stocklocation', great! | ||||||
|  |     if (!data.containsKey('stocklocation')) { | ||||||
|  |       showErrorDialog( | ||||||
|  |           _context, | ||||||
|  |           "Invalid Barcode", | ||||||
|  |           "Barcode does not match a Stock Location", | ||||||
|  |           onDismissed: _controller.resumeCamera, | ||||||
|  |       ); | ||||||
|  |      } else { | ||||||
|  |       // Extract location information | ||||||
|  |       int location = data['stocklocation']['pk'] as int; | ||||||
|  |  | ||||||
|  |       // Transfer stock to specified location | ||||||
|  |       item.transferStock(location).then((response) { | ||||||
|  |         print("Response: ${response.statusCode}"); | ||||||
|  |         _controller.dispose(); | ||||||
|  |         Navigator.of(_context).pop(); | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Future<void> scanQrCode(BuildContext context) async { | Future<void> scanQrCode(BuildContext context) async { | ||||||
|  |  | ||||||
|   Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeQRView(BarcodeScanHandler()))); |   Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeQRView(BarcodeScanHandler()))); | ||||||
|   | |||||||
| @@ -379,20 +379,26 @@ class InvenTreeStockItem extends InvenTreeModel { | |||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Future<http.Response> transferStock(double q, int location, {String notes}) async { |   Future<http.Response> transferStock(int location, {double quantity, String notes}) async { | ||||||
|  |     if (quantity == null) {} else | ||||||
|  |     if ((quantity < 0) || (quantity > this.quantity)) { | ||||||
|  |       quantity = this.quantity; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     if ((q == null) || (q > quantity)) q = quantity; |     Map<String, dynamic> data = { | ||||||
|  |  | ||||||
|     return api.post("/stock/transfer/", body: { |  | ||||||
|       "item": { |       "item": { | ||||||
|         "pk": "${pk}", |         "pk": "${pk}", | ||||||
|         "quantity": "${q}", |       }, | ||||||
|         }, |  | ||||||
|       "location": "${location}", |       "location": "${location}", | ||||||
|       "notes": notes ?? '', |       "notes": notes ?? '', | ||||||
|     }); |     }; | ||||||
|   } |  | ||||||
|  |  | ||||||
|  |     if (quantity != null) { | ||||||
|  |       data["item"]["quantity"] = "${quantity}"; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return api.post("/stock/transfer/", body: data); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -253,7 +253,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|     _quantityController.clear(); |     _quantityController.clear(); | ||||||
|     _notesController.clear(); |     _notesController.clear(); | ||||||
|  |  | ||||||
|     var response = await item.transferStock(quantity, location.pk, notes: notes); |     var response = await item.transferStock(location.pk, quantity: quantity, notes: notes); | ||||||
|  |  | ||||||
|     // TODO - Error handling (potentially return false?) |     // TODO - Error handling (potentially return false?) | ||||||
|     refresh(); |     refresh(); | ||||||
| @@ -525,6 +525,10 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|         leading: FaIcon(FontAwesomeIcons.exchangeAlt), |         leading: FaIcon(FontAwesomeIcons.exchangeAlt), | ||||||
|         trailing: FaIcon(FontAwesomeIcons.qrcode), |         trailing: FaIcon(FontAwesomeIcons.qrcode), | ||||||
|         onTap: () { |         onTap: () { | ||||||
|  |           Navigator.push( | ||||||
|  |             context, | ||||||
|  |             MaterialPageRoute(builder: (context) => InvenTreeQRView(StockItemScanIntoLocationHandler(item))) | ||||||
|  |           ); | ||||||
|         }, |         }, | ||||||
|       ) |       ) | ||||||
|     ); |     ); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user