Exemple #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rutas);

    listView = (ListView) findViewById(R.id.listView1);
    SQLiteQuery sql = new SQLiteQuery(this);
    List<String> items = new ArrayList<String>();
    List<Ruta> rutas = sql.getRutas();
    if (rutas.size() == 0) {
      Toast.makeText(this, "No existen Rutas registradas.", Toast.LENGTH_LONG).show();
    } else {
      for (Ruta r : rutas) {
        items.add(r.getRuta());
        this.ids.add(r.getIdruta());
      }
      ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
      listView.setAdapter(adapter);
      listView.setAdapter(adapter);
    }

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    listView.setOnItemClickListener(
        new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(Rutas.this, Detalle_Ruta.class);
            intent.putExtra("idruta", "" + ids.get(position));
            startActivity(intent);
          }
        });
  }
  @SuppressWarnings("unchecked")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select__producto__venta);

    Intent startingIntent = getIntent();
    if (startingIntent != null) {
      // Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
      Bundle reicieveParams = getIntent().getExtras();
      idvendedor = Integer.parseInt(reicieveParams.getString("idvendedor"));
      idruta = Integer.parseInt(reicieveParams.getString("idruta"));
      idcliente = Integer.parseInt(reicieveParams.getString("idcliente"));
      this.detalleventas = (List<Detalleventa>) getIntent().getSerializableExtra("detalleventas");
    }

    listView = (ListView) findViewById(R.id.listView1);
    SQLiteQuery sql = new SQLiteQuery(this);
    List<Producto> productos = sql.getProductos();
    ArrayAdapter<Detalleventa> adapter =
        new ListArrayAdapterCantidad(this, detalleventas, productos);
    listView.setAdapter(adapter);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    listView.setOnItemClickListener(
        new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ViewGroup row = (ViewGroup) listView.getChildAt(position);
            TextView textView = (TextView) row.findViewById(R.id.rowTextView2);
            int cantidad = Integer.parseInt(textView.getText().toString());
            Intent intent = new Intent(Select_Producto_Venta.this, Select_Cantidad_Venta.class);
            intent.putExtra("idproducto", "" + detalleventas.get(position).getIdproducto());
            intent.putExtra("cantidad", "" + cantidad);
            intent.putExtra("idvendedor", "" + idvendedor);
            intent.putExtra("idruta", "" + idruta);
            intent.putExtra("idcliente", "" + idcliente);
            intent.putExtra("detalleventas", (Serializable) detalleventas);
            startActivity(intent);
          }
        });
  }
  @SuppressWarnings("deprecation")
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        Intent intentHome = new Intent(Select_Producto_Venta.this, Select_Cliente_Venta.class);
        intentHome.putExtra("idvendedor", "" + idvendedor);
        intentHome.putExtra("idruta", "" + idruta);
        startActivity(intentHome);
        return true;
      case MENU1:
        boolean hayCompra = false;
        double subtotal = 0;
        SQLiteQuery sql = new SQLiteQuery(Select_Producto_Venta.this);
        List<Detalleventa> detalles = new ArrayList<Detalleventa>();
        for (int i = 0; i < listView.getAdapter().getCount(); i++) {
          ViewGroup row = (ViewGroup) listView.getChildAt(i);
          TextView tvCantidad = (TextView) row.findViewById(R.id.rowTextView2);
          if (!tvCantidad.getText().toString().equals("0")) {
            int cantidad = Integer.parseInt(tvCantidad.getText().toString());
            Detalleventa dv = detalleventas.get(i);
            Producto p = sql.getProductoPorId(dv.getIdproducto());
            detalles.add(dv);
            subtotal += (p.getPventa() * cantidad);
            p.setCantidad(p.getCantidad() - cantidad);
            sql.updateProducto(p);
            hayCompra = true;
          }
        }
        if (hayCompra) {
          Venta v = new Venta();
          v.setFecha(new Date());
          v.setIdcliente(idcliente);
          v.setIdruta(idruta);
          v.setIdvendedor(idvendedor);
          v.setIva(subtotal * 0.16);
          v.setSubtotal(subtotal);
          v.setTotal(subtotal * 1.16);
          sql.insertVenta(v);
          int idventa = sql.getId("venta", "idventa") - 1;
          for (Detalleventa dv : detalles) {
            dv.setIdventa(idventa);
            sql.insertDetalleVenta(dv);
          }
          AlertDialog alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("Detalles venta...");
          alertDialog.setMessage(
              "Subtotal:     "
                  + subtotal
                  + "\n"
                  + "I.V.A:     "
                  + v.getIva()
                  + "\n"
                  + "TOTAL:     "
                  + v.getTotal());
          alertDialog.setButton(
              "OK",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(
                          Select_Producto_Venta.this, "Se ha guardado la venta.", Toast.LENGTH_LONG)
                      .show();
                  Intent intent = new Intent(Select_Producto_Venta.this, Mis_Ventas.class);
                  intent.putExtra("idvendedor", "" + idvendedor);
                  startActivity(intent);
                }
              });
          alertDialog.setIcon(R.drawable.ic_launcher);
          alertDialog.show();
        } else {
          Toast.makeText(this, "No hay cantidades válidas.", Toast.LENGTH_LONG).show();
        }

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }