ios – My utility features as a degree of sale / stock administration system. My drawback is its not depreciating the inventory quantity by the deprecated worth


My SwiftUI utility features as follows I add customized merchandise in content material view with a inventory title & depreciated worth & I add inventory names and ranges in StockInput view. My drawback happens within the pay operate that it does not minus the depreciated worth from the inventory quantity if the inventory names are equivalent.. Right here is snippets of my code (No errors happen its only a case the place nothing occurs.

Contentview the place I add customized merchandise

  TextField("Identify", textual content: $title)
                                       TextField("Value", textual content: $value)
                                       TextField("Inventory Identify", textual content: $stockName)
                                       TextField("Depreciated Quantity", textual content: $depreciatedAmount)
                                       Button(motion: {
                                           self.showingImagePicker = true
                                       }) {
                                           Picture(systemName: "photograph")
                                       }
                                   }
                                   .padding()
                                   Button(motion: {
                                       if let value = Double(self.value), let depreciatedAmount = Int(self.depreciatedAmount) {
                                           let product = ProductCustom(title: self.title, value: value, imageUrl: self.imageUrl, stockName: self.stockName, depreciatedAmount: depreciatedAmount)
                                           self.productData.customproducts.append(product)
                                       }
                                   }) {
                                       Textual content("Add Product")
                                           .font(.headline)
                                           .foregroundColor(.white)
                                           .padding()
                                           .background(Colour.orange)
                                           .cornerRadius(30)
                                   }``

StockView the place I add inventory

Textual content("Add Present Inventory & Quantity")
                    .font(.largeTitle)
                    .foregroundColor(.orange)
                    .padding()
                HStack {
                    ForEach(stocksData.shares, id: .title) { inventory in
                        ZStack {
                            RoundedRectangle(cornerRadius: 10)
                                .fill(Colour.orange)
                                .shadow(radius: 5)
                            VStack(alignment: .main) {
                                Textual content("(inventory.title)")
                                    .font(.headline)
                                Textual content("(inventory.quantity) (inventory.unit)")
                                    .font(.subheadline)
                                

                                
                                
                            }.padding()
                        }
                        .body(width: 100, peak: 100)
                    }
                }
                
                
                .padding()
                TextField("Inventory title", textual content: $stockName)
                    .padding()
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                
                
                    .cornerRadius(5.0)
                
                TextField("Inventory quantity", worth: $stockAmount, formatter: NumberFormatter())
                    .padding()
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .cornerRadius(5.0)
                
                Picker(choice: $unitSelection, label: Textual content("Unit")) {
                    ForEach(items.indices) {
                        Textual content(self.items[$0])
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                Button(motion: {
                    self.stocksData.shares.append((title: self.stockName, quantity: self.stockAmount, unit: self.items[self.unitSelection]))

                  
                }, label: {
                    Textual content("Add Inventory")
                })`

Detailed Statistics the place I show inventory ranges


`            HStack {
                ForEach(stocksData.shares, id: .title) { inventory in
                    VStack {
                        Textual content("(inventory.title)")
                        ZStack {
                            Circle()
                                .stroke(Colour.grey, lineWidth: 20)
                            Circle()
                                .trim(from: 0, to: CGFloat(inventory.quantity) / CGFloat(inventory.quantity))
                                .stroke(Colour.orange, lineWidth: 20)
                                .rotationEffect(Angle(levels: -90))
                            Textual content("(inventory.quantity)")
                                .font(.largeTitle)
                        }
                        .body(width: 100, peak: 200)
                    }
                    .padding()
                }
            }`
`

And eventually my pay operate

func pay() {
        
        for product in customproducts {
                if let productStockName = product.stockName, let index = stocksData.shares.firstIndex(the place: { $0.title == productStockName }) {
                    var inventory = stocksData.shares[index]
                    if let depreciatedAmount = product.depreciatedAmount {
                        inventory.quantity -= depreciatedAmount
                        stocksData.shares[index] = inventory
                    }
                }
            }

I anticipate the inventory degree to lower, I’ve tried many various variations of the pay operate with no success.. the inventory degree stays the identical even tho I’ve matching inventory names in content material view and inventory enter view

Leave a Reply