Advertisement
Dwitio

Untitled

Dec 30th, 2024
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.84 KB | None | 0 0
  1. // ... existing code ...
  2.  
  3. class _WorkOrderDetailState extends State<WorkOrderDetail> {
  4.   // ... existing code ...
  5.  
  6.   Future<void> fetchData() async {
  7.     try {
  8.       final wo = await apiService.fetchWorkOrder(widget.workOrderId);
  9.       setState(() {
  10.         futureWorkOrder = Future.value(wo);
  11.         workOrder = wo;
  12.         woStatus = int.parse('${workOrder!.woStatus}');
  13.        
  14.         // ... existing controller assignments ...
  15.  
  16.         // Process items and auto-select spareparts
  17.         workOrder!.items.forEach((item) {
  18.           if (item.source == 1) { // Sparepart
  19.             // Add to selected items map
  20.             _selectedItemsSparepart[int.parse(item.itemId)] = {
  21.               'count': item.qtyJual,
  22.               'ws_stock_id': item.wsStockId,
  23.               'harga_offline': item.hargaJual,
  24.               'diskon': item.diskonJual,
  25.               'barcode': item.barcode,
  26.               // Add other needed fields
  27.             };
  28.  
  29.             // Update other sparepart related variables
  30.             itemDetailSparepartIdList = item.itemId;
  31.             sourceSparepartList = item.source;
  32.             wsStockIdSparepart = item.wsStockId;
  33.             barcodeListSparepart = item.barcode;
  34.             quantityListSparePart = item.qtyJual;
  35.             listSubSparepartController = item.hargaJual;
  36.             discountListSparepart = item.diskonJual;
  37.             totalListSparepart = item.totalJual;
  38.             branchSparepartIdList = item.branchServiceId;
  39.             totalHarusDibayar += item.totalJual;
  40.           } else {
  41.             // Existing service item processing
  42.             serviceIdList = item.itemId;
  43.             sourceServiceList = item.source;
  44.             // ... rest of service assignments
  45.           }
  46.         });
  47.  
  48.         // Fetch details for selected spareparts
  49.         _fetchDetailsAndUpdateAllSelectedItems();
  50.       });
  51.     } catch (e) {
  52.       print('Error fetching data: $e');
  53.     }
  54.   }
  55.  
  56.   // Update _fetchDetailsAndUpdateAllSelectedItems to handle existing selections
  57.   Future<void> _fetchDetailsAndUpdateAllSelectedItems() async {
  58.     for (var entry in _selectedItemsSparepart.entries) {
  59.       final item_detail_id = entry.key.toString();
  60.       final ws_stock_id = entry.value['ws_stock_id'];
  61.  
  62.       try {
  63.         final url = 'https://devapi.dyvolt.com/api/warehousestok/get-item-detail-byws';
  64.         final response = await http.post(
  65.           Uri.parse(url),
  66.           headers: {
  67.             'Authorization': 'Bearer $token',
  68.             'Accept': 'application/vnd.api+json',
  69.             'Content-Type': 'application/vnd.api+json',
  70.           },
  71.           body: jsonEncode({
  72.             'item_detail_id': item_detail_id,
  73.             'ws_stock_id': ws_stock_id,
  74.           }),
  75.         );
  76.  
  77.         if (response.statusCode == 200) {
  78.           final data = json.decode(response.body);
  79.           final itemData = data['data'];
  80.  
  81.           setState(() {
  82.             // Preserve existing count and other values while updating with new data
  83.             _selectedItemsSparepart[int.parse(item_detail_id)] = {
  84.               ..._selectedItemsSparepart[int.parse(item_detail_id)]!,
  85.               'harga_offline': itemData['harga_offline'],
  86.               'item_name': itemData['item_name'],
  87.               'itemcode': itemData['itemcode'],
  88.               'size': itemData['size'],
  89.               'stock': itemData['stock'],
  90.               'barcode': itemData['barcode'],
  91.               'diskon': itemData['diskon'],
  92.               'item_detail_id': itemData['item_detail_id'],
  93.               'warehouse_type': itemData['warehouse_type'],
  94.               'images': itemData['images'],
  95.               'url_images': itemData['url_images'],
  96.             };
  97.           });
  98.         }
  99.       } catch (e) {
  100.         print('Error fetching details for item $item_detail_id: $e');
  101.       }
  102.     }
  103.   }
  104.  
  105.   // ... rest of existing code ...
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement