通过在Swiftui中使用核心数据将两个值乘以两个值

发布于 2025-02-12 04:08:52 字数 4590 浏览 1 评论 0原文

我尝试通过使用核心数据来制作发票应用程序,我有4个属性:文章字符串,价格字符串,数量字符串和税收字符串,我试图做一个乘以乘价格 *数量的量,我想做另一个的结果计算每篇文章的税收的功能 像这样 - >

文章: 价格$: 数量x: 税务%: 总税百分比: 总价税含$:

然后最后我希望用户在Textfield在2个不同的TextView中创建的列表的总价格和总税收

,例如

总税百分比: 总计含税$:

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()
            }
        }
        
    }

}

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()
            }
        }
        
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2025-02-19 04:08:52

您可以使用扩展

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
    }
}

然后可以使用

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

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)$ ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文