Erfan Alipour
Erfan Alipour

Reputation: 15

multiply two value from textfield by using core data in swiftui

i try to make an invoice app by using core data, i have 4 attribute: Article String, Price String , Quantity String and Taxe String and i try to make a fonction to multiply price * quantity and the result of them i want to make another function for calculate the taxe for each article Like this ->

Article: Price $: Quantity x: Taxe %: Total taxe %: Total price taxe incl $:

and then at the end i want sum total price and total taxe of my list created by user in textfield in 2 diffrent textView

like this ->

Total taxe %: Total prie incl taxe $:

struct Facturation: View {

    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(entity: FacturationCoreData.entity(), sortDescriptors: []) private var factures: FetchedResults<FacturationCoreData>

    @State private var article = ""
    @State private var prix = ""
    @State private var quantite = ""
    @State private var tvac = ""
    @State private var tva = [0, 6, 12, 21]
    @State private var tipIndex = 2
    @AppStorage("textChange") private var textChange = ""

    func prixQuantite() -> Double {
        let fact = FacturationCoreData(context: viewContext)
        let prix = fact.prix ?? ""
        let quan = fact.quantite ?? ""
        let sum = ((Double(prix) ?? 00) * (Double(quan) ?? 00))
        return sum
    }

    func calculeTvaC() ->Double {
        let fact = FacturationCoreData(context: viewContext)
        let prixEtQuantite = prixQuantite()
        let tva = fact.tva ?? ""
        let prixTTC = ((prixEtQuantite / 100 * (Double(tva ?? "") ?? 00)) + 
        prixEtQuantite)
        return prixTTC
        
    }


    var body: some View {
        
        NavigationView{

        VStack {
            Form {
                
                Section("Article"){
                    TextField("Article", text: $article)
                        .disableAutocorrection(true)
                }
                HStack{
                    Section("Q"){
                        TextField("Quantite", text: $quantite)
                            .keyboardType(.decimalPad)
                    }
                    Section("P") {
                        TextField("Prix", text: $prix)
                            .keyboardType(.decimalPad)
                    }
                    Section("T") {
                        TextField("TVA", text: $tvac)
                            .keyboardType(.decimalPad)
                    }

                    
                }

            Button {
                let facture = FacturationCoreData(context: viewContext)
                facture.article = article
                facture.quantite = quantite
                facture.prix = prix
                facture.tva = tvac
                do{
                    try viewContext.save()
                }catch{
                    print(error)
                }
            } label: {
                Image(systemName: "square.and.arrow.down.fill")
                    .font(.title)
            }
            List{
                ForEach(factures) { facture in
                    VStack(alignment: .leading){
                        Text("Artile: " + (facture.article ?? ""))
                        Text("Quantité: " + (facture.quantite ?? "") + "x")
                        Text("Prix: " + (facture.prix ?? "") + "€")
                        Text("Tva: " + (facture.tva ?? "") + "%")
                        Text("Total price:\(prixQuantite())$ ")
                        Text("Total taxe:\(calculeTvaC())$ ")
 
                    }.onTapGesture {
                        textChange = facture.article ?? ""
     
                        
                    }
             
            }.onDelete { indexSet in
                indexSet.forEach { index in
                    let deletfacture = factures[index]
                    viewContext.delete(deletfacture)
                    do{
                        try viewContext.save()
                    }catch{
                        print(error)
                    }
                }
            }
                
            }

            
            }
            Text(textChange)
   

        }
        .navigationTitle("Facturation")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                NavigationLink {
                    Client()
                } label: {
                    Image(systemName: "person.badge.plus")
                }

                

        }
            ToolbarItem(placement: .navigationBarLeading) {
                NavigationLink {
                    ClientList()
                } label: {
                    Image(systemName: "list.bullet")
                }

                

        }
            ToolbarItem(placement: .navigationBarTrailing) {
                EditButton()
            }
        }
        
    }

}

Upvotes: 0

Views: 123

Answers (1)

lorem ipsum
lorem ipsum

Reputation: 29251

You can use an extension

extension FacturationCoreData{
    var prixQuantite : Double {
        let prix = self.prix ?? ""
        let quan = self.quantite ?? ""
        let sum = ((Double(prix) ?? 00) * (Double(quan) ?? 00))
        return sum
    }
    var calculeTvaC : Double {
        let prixEtQuantite = prixQuantite
        let tva = self.tva ?? ""
        let prixTTC = ((prixEtQuantite / 100 * (Double(tva ?? "") ?? 00)) +
        prixEtQuantite)
        return prixTTC
    }
}

Then you can use

Text("Total price:\(facture.prixQuantite)$ ")
Text("Total taxe:\(facture.calculeTvaC)$ ")

Upvotes: 1

Related Questions