Search in sources :

Example 1 with Transaction

use of com.example.asus.onlinecanteen.model.Transaction in project OnlineCanteen by josephgunawan97.

the class MerchantOrderDetailActivity method updateOrder.

public void updateOrder() {
    DatabaseReference productDatabase = FirebaseDatabase.getInstance().getReference();
    Log.i(MerchantOrderDetailActivity.class.getSimpleName(), "BEFORE UPDATE TRANS " + value);
    productDatabase.child("transactions").orderByChild(value).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Transaction transaction = dataSnapshot.getValue(Transaction.class);
            HashMap<String, Object> result = new HashMap<>();
            // result.put("imageUrl", );
            result.put("deliveryStatus", 1);
            Log.i(MerchantOrderDetailActivity.class.getSimpleName(), "UPDATE TRANS1 " + reference.child(value).getKey());
            reference.child(value).updateChildren(result);
        // Log.i(MerchantOrderDetailActivity.class.getSimpleName(), "UPDATE TRANS "+ reference.child(value).ge);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    detailAdapter.notifyDataSetChanged();
    Log.i(MerchantOrderDetailActivity.class.getSimpleName(), "UPDATE TRANS " + transaction.getDeliveryStatus());
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 2 with Transaction

use of com.example.asus.onlinecanteen.model.Transaction in project OnlineCanteen by josephgunawan97.

the class QrActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mScannerView = new ZXingScannerView(this);
    setContentView(mScannerView);
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED)
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, MY_CAMERA_REQUEST_CODE);
    Intent intent = getIntent();
    value = intent.getStringExtra("Location");
    if (value.equals("order")) {
        pos = intent.getIntExtra("Position", 0);
        transactionHistory = (ArrayList<Transaction>) intent.getSerializableExtra("Transaction");
    }
}
Also used : Transaction(com.example.asus.onlinecanteen.model.Transaction) Intent(android.content.Intent) ZXingScannerView(me.dm7.barcodescanner.zxing.ZXingScannerView)

Example 3 with Transaction

use of com.example.asus.onlinecanteen.model.Transaction in project OnlineCanteen by josephgunawan97.

the class TransactionHistoryFragment method attachTransactionEventListener.

private void attachTransactionEventListener() {
    if (transactionEventListener == null) {
        transactionEventListener = new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Transaction newTransaction = dataSnapshot.getValue(Transaction.class);
                if (newTransaction.getDeliveryStatus() == 3)
                    adapter.addTransactionHistory(newTransaction);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };
        transactionQuery.addChildEventListener(transactionEventListener);
    }
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 4 with Transaction

use of com.example.asus.onlinecanteen.model.Transaction in project OnlineCanteen by josephgunawan97.

the class MerchantOrderDetailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_merchant_order_detail);
    firebaseDatabase = FirebaseDatabase.getInstance();
    reference = firebaseDatabase.getReference("transactions");
    intent = getIntent();
    pos = intent.getIntExtra("Position", 0);
    transactionHistory = (ArrayList<Transaction>) intent.getSerializableExtra("Transaction");
    transaction = transactionHistory.get(pos);
    mAuth = FirebaseAuth.getInstance();
    // Initialize views
    transactiondate = findViewById(R.id.transaction_date);
    username = findViewById(R.id.username);
    location = findViewById(R.id.user_location);
    grandTotal = findViewById(R.id.transaction_detail_amount);
    orderStatus = findViewById(R.id.order_status);
    acceptButton = findViewById(R.id.acceptOrder);
    declineButton = findViewById(R.id.declineOrder);
    scanQR = findViewById(R.id.scan_qr);
    if (transaction.getDeliveryStatus() == 0) {
        acceptButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                WalletUtil walletUtil = new WalletUtil();
                walletUtil.debitAmount(mAuth.getCurrentUser().getUid(), transaction.getTotalPrice());
                Toast.makeText(getApplicationContext(), "Order accepted", Toast.LENGTH_LONG).show();
                Query query = reference.orderByChild("purchaseDate").equalTo(transaction.getPurchaseDate());
                query.addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        for (DataSnapshot child : snapshot.getChildren()) {
                            child.getRef().child("deliveryStatus").setValue(1);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
                Intent intent = new Intent(MerchantOrderDetailActivity.this, MainActivityMerchant.class);
                startActivity(intent);
                finish();
            }
        });
        declineButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Order declined", Toast.LENGTH_LONG).show();
                Query query = reference.orderByChild("purchaseDate").equalTo(transaction.getPurchaseDate());
                query.addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        for (DataSnapshot child : snapshot.getChildren()) {
                            child.getRef().child("deliveryStatus").setValue(4);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
                Intent intent = new Intent(MerchantOrderDetailActivity.this, MainActivityMerchant.class);
                startActivity(intent);
                finish();
            }
        });
    } else {
        acceptButton.setClickable(false);
        acceptButton.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        declineButton.setClickable(false);
        declineButton.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
    }
    scanQR.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), QrActivity.class);
            i.putExtra("Location", "order");
            i.putExtra("Transaction", transactionHistory);
            i.putExtra("Position", pos);
            startActivityForResult(i, SECOND_ACTIVITY_REQUEST_CODE);
        }
    });
    // Set views
    transactiondate.setText(Transaction.getPurchasedDateString(transaction.getPurchaseDate()));
    username.setText(transaction.getName());
    location.setText(transaction.getLocation());
    grandTotal.setText("Rp " + String.valueOf(transaction.getTotalPrice()));
    // TO BE CHANGED LATER
    orderStatus.setText(statusString(transaction.getDeliveryStatus()));
    // Adapter for order items list
    detailAdapter = new OrderDetailAdapter(transaction.getItems());
    itemsRecyclerView = findViewById(R.id.transaction_detail_items);
    layoutManager = new LinearLayoutManager(getApplicationContext());
    itemsRecyclerView.setLayoutManager(layoutManager);
    itemsRecyclerView.setAdapter(detailAdapter);
}
Also used : Query(com.google.firebase.database.Query) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) OrderDetailAdapter(com.example.asus.onlinecanteen.adapter.OrderDetailAdapter) ValueEventListener(com.google.firebase.database.ValueEventListener) WalletUtil(com.example.asus.onlinecanteen.utils.WalletUtil)

Example 5 with Transaction

use of com.example.asus.onlinecanteen.model.Transaction in project OnlineCanteen by josephgunawan97.

the class CartActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    cart = (ArrayList<Cart>) getIntent().getSerializableExtra("Cart");
    cartActivityAdapter = new CartActivityAdapter(this, cart);
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
    walletUtil = new WalletUtil();
    notificationRef = FirebaseDatabase.getInstance().getReference().child("notifications");
    // Calculating the grand total
    for (Cart c : cart) {
        total += c.getTotalPrice(c);
    }
    // Initialize ListView
    cartList = findViewById(R.id.cartList);
    grandTotal = findViewById(R.id.grandTotal);
    deliveryFee = findViewById(R.id.deliveryFee);
    orderButton = findViewById(R.id.OrderButton);
    locationEditText = findViewById(R.id.userLocation);
    // Set views
    grandTotal.setText("TOTAL: Rp " + total);
    deliveryFee.setText("Delivery Fee : Rp ");
    cartList.setAdapter(cartActivityAdapter);
    orderButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!locationEditText.getText().toString().isEmpty()) {
                // Make new transaction
                walletRef = FirebaseDatabase.getInstance().getReference().child("wallet");
                walletRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            String valueString = dataSnapshot.getValue().toString();
                            int walletCash = Integer.parseInt(valueString);
                            // if wallet money >= total
                            if (walletCash >= total) {
                                ArrayList<PurchasedItem> items = new ArrayList<>();
                                for (Cart c : cart) {
                                    PurchasedItem item = new PurchasedItem(c.getProductName(), c.getProductPrice(), c.getQuantity());
                                    items.add(item);
                                }
                                Intent intent = getIntent();
                                Transaction transaction = new Transaction(intent.getStringExtra("Seller"), FirebaseAuth.getInstance().getUid(), items, locationEditText.getText().toString());
                                TransactionUtil.insert(transaction);
                                Toast.makeText(getApplicationContext(), "Transcation done", Toast.LENGTH_SHORT).show();
                                setResult(RESULT_OK);
                                // Make notification to the seller
                                HashMap<String, String> notificationData = new HashMap<>();
                                notificationData.put("from", transaction.getUid());
                                notificationData.put("type", "new order");
                                notificationRef.child(transaction.getSid()).push().setValue(notificationData);
                                // Go back to main menu
                                finish();
                            } else {
                                Toast.makeText(getApplicationContext(), "Not enough wallet cash, please top-up at our counter", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            } else {
                // Alert dialog if the location is not filled in
                AlertDialog.Builder builder = new AlertDialog.Builder(CartActivity.this);
                builder.setMessage("Please enter your location!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.setTitle("Error");
                alert.show();
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) HashMap(java.util.HashMap) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) CartActivityAdapter(com.example.asus.onlinecanteen.adapter.CartActivityAdapter) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.example.asus.onlinecanteen.model.Transaction) ValueEventListener(com.google.firebase.database.ValueEventListener) WalletUtil(com.example.asus.onlinecanteen.utils.WalletUtil) PurchasedItem(com.example.asus.onlinecanteen.model.PurchasedItem) Cart(com.example.asus.onlinecanteen.model.Cart)

Aggregations

Transaction (com.example.asus.onlinecanteen.model.Transaction)13 DataSnapshot (com.google.firebase.database.DataSnapshot)8 DatabaseError (com.google.firebase.database.DatabaseError)8 ValueEventListener (com.google.firebase.database.ValueEventListener)5 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)4 Intent (android.content.Intent)3 View (android.view.View)3 ChildEventListener (com.google.firebase.database.ChildEventListener)3 ArrayList (java.util.ArrayList)3 DividerItemDecoration (android.support.v7.widget.DividerItemDecoration)2 RecyclerView (android.support.v7.widget.RecyclerView)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 OrderAdapter (com.example.asus.onlinecanteen.adapter.OrderAdapter)2 TransactionHistoryAdapter (com.example.asus.onlinecanteen.adapter.TransactionHistoryAdapter)2 WalletUtil (com.example.asus.onlinecanteen.utils.WalletUtil)2 HashMap (java.util.HashMap)2 DialogInterface (android.content.DialogInterface)1 AlertDialog (android.support.v7.app.AlertDialog)1 CartActivityAdapter (com.example.asus.onlinecanteen.adapter.CartActivityAdapter)1