2

I am creating an invoice with increment numbers start from #1 #2 #3 ...., but I have no idea how to start with. Here is the structure I want to have:

Invoice#1
|- info...
Invoice#2
|- info...
Invoice#3
|- info...
DatabaseReference ref 
 = FirebaseDatabase.getInstance().getReference().child("Invoice#" {increment}); //how to put increment here? if I declare i=1 in myActivity.java, it only runs in one

and this is part of the Invoice model I have

public class Invoice {
    String buyer, seller, typeOfPymt;
    int total;
    String billdate;
    int invoice_num;
    int discount;
    int tax;
    int grand_total;


    public Invoice(String buyer, String seller, int grand_total, String billdate, int invoice_num, int discount) {
        this.buyer = buyer;
        this.seller = seller;
        this.billdate = billdate;
        this.invoice_num = invoice_num;
        this.discount = discount;
        this.grand_total = grand_total;
    }

How can I add increments inside the Model?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Noizy Z
  • 81
  • 11
  • You can add the last increment Integer in sharedPreference & add it by +1 when doing your work, save it.. – Darshan Nov 04 '19 at 07:16

1 Answers1

1

There is no way you can pass an "increment" argument to the child() method that can help you read the last invoice number and generate a new incremented one.

In your database, a more appropriate approach would be to use as a key of your invoice a random key that is generated by the push() method and not those incrementing voice numbers. The main reason is scalability, as it is explained by @FrankvanPuffelen in the following post:

Using the above approach, the number of your invoice will become a property in your invoice object.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193