Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ... existing code ...
- class _WorkOrderDetailState extends State<WorkOrderDetail> {
- // ... existing code ...
- Future<void> fetchData() async {
- try {
- final wo = await apiService.fetchWorkOrder(widget.workOrderId);
- setState(() {
- futureWorkOrder = Future.value(wo);
- workOrder = wo;
- woStatus = int.parse('${workOrder!.woStatus}');
- // ... existing controller assignments ...
- // Process items and auto-select spareparts
- workOrder!.items.forEach((item) {
- if (item.source == 1) { // Sparepart
- // Add to selected items map
- _selectedItemsSparepart[int.parse(item.itemId)] = {
- 'count': item.qtyJual,
- 'ws_stock_id': item.wsStockId,
- 'harga_offline': item.hargaJual,
- 'diskon': item.diskonJual,
- 'barcode': item.barcode,
- // Add other needed fields
- };
- // Update other sparepart related variables
- itemDetailSparepartIdList = item.itemId;
- sourceSparepartList = item.source;
- wsStockIdSparepart = item.wsStockId;
- barcodeListSparepart = item.barcode;
- quantityListSparePart = item.qtyJual;
- listSubSparepartController = item.hargaJual;
- discountListSparepart = item.diskonJual;
- totalListSparepart = item.totalJual;
- branchSparepartIdList = item.branchServiceId;
- totalHarusDibayar += item.totalJual;
- } else {
- // Existing service item processing
- serviceIdList = item.itemId;
- sourceServiceList = item.source;
- // ... rest of service assignments
- }
- });
- // Fetch details for selected spareparts
- _fetchDetailsAndUpdateAllSelectedItems();
- });
- } catch (e) {
- print('Error fetching data: $e');
- }
- }
- // Update _fetchDetailsAndUpdateAllSelectedItems to handle existing selections
- Future<void> _fetchDetailsAndUpdateAllSelectedItems() async {
- for (var entry in _selectedItemsSparepart.entries) {
- final item_detail_id = entry.key.toString();
- final ws_stock_id = entry.value['ws_stock_id'];
- try {
- final url = 'https://devapi.dyvolt.com/api/warehousestok/get-item-detail-byws';
- final response = await http.post(
- Uri.parse(url),
- headers: {
- 'Authorization': 'Bearer $token',
- 'Accept': 'application/vnd.api+json',
- 'Content-Type': 'application/vnd.api+json',
- },
- body: jsonEncode({
- 'item_detail_id': item_detail_id,
- 'ws_stock_id': ws_stock_id,
- }),
- );
- if (response.statusCode == 200) {
- final data = json.decode(response.body);
- final itemData = data['data'];
- setState(() {
- // Preserve existing count and other values while updating with new data
- _selectedItemsSparepart[int.parse(item_detail_id)] = {
- ..._selectedItemsSparepart[int.parse(item_detail_id)]!,
- 'harga_offline': itemData['harga_offline'],
- 'item_name': itemData['item_name'],
- 'itemcode': itemData['itemcode'],
- 'size': itemData['size'],
- 'stock': itemData['stock'],
- 'barcode': itemData['barcode'],
- 'diskon': itemData['diskon'],
- 'item_detail_id': itemData['item_detail_id'],
- 'warehouse_type': itemData['warehouse_type'],
- 'images': itemData['images'],
- 'url_images': itemData['url_images'],
- };
- });
- }
- } catch (e) {
- print('Error fetching details for item $item_detail_id: $e');
- }
- }
- }
- // ... rest of existing code ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement