fun insertar (view: View){
val vdocumento = documento.text.toString();
val vnombre = nombre.text.toString();
val vtelefono = telefono.text.toString();
val vdireccion = direccion.text.toString();
if(vdocumento.isNotBlank() && vdocumento.isNotEmpty()
&& vnombre.isNotBlank() && vnombre.isNotEmpty()
&& vtelefono.isNotBlank() && vtelefono.isNotEmpty()
&& vdireccion.isNotBlank() && vdireccion.isNotEmpty()
){
val db = dbHelper.writableDatabase
var values = ContentValues().apply {
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_CODIGO, vdocumento)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_NOMBRE, vnombre)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_DIRECCION, vdireccion)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_TELEFONO, vtelefono)
}
val newRowId = db?.insert(FerreteriaContract.ClienteEntry.TABLE_NAME, null, values)
db.close()
if (newRowId == null) {
Toast.makeText(this, "No se creo el registro", Toast.LENGTH_LONG).show();
}
documento.text = ""
nombre.text= ""
telefono.text= ""
direccion.text = ""
Toast.makeText(this, "Nuevo cleinte creado exitosamente", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Todos los campos son obligaotrios", Toast.LENGTH_LONG).show();
}
}
fun buscar(view: View){
val vdocumento = documento.text.toString();
if(vdocumento.isNotBlank() && vdocumento.isNotEmpty()) {
val db = dbHelper.readableDatabase
var cursor = db.query(
FerreteriaContract.ClienteEntry.TABLE_NAME,
null,
FerreteriaContract.ClienteEntry.COLUMN_NAME_CODIGO + "=" + vdocumento,
null,
null,
null,
null
)
if (cursor.moveToNext()) {
nombre.text =
cursor.getString(cursor.getColumnIndexOrThrow(FerreteriaContract.ClienteEntry.COLUMN_NAME_NOMBRE))
telefono.text =
cursor.getString(cursor.getColumnIndexOrThrow(FerreteriaContract.ClienteEntry.COLUMN_NAME_TELEFONO))
direccion.text =
cursor.getString(cursor.getColumnIndexOrThrow(FerreteriaContract.ClienteEntry.COLUMN_NAME_DIRECCION))
} else {
Toast.makeText(this, "No se encontro el usuario", Toast.LENGTH_LONG).show();
}
cursor.close()
db.close()
}else{
Toast.makeText(this, "el documento es requierido", Toast.LENGTH_LONG).show();
}
}
fun actualizar(view: View){
val vdocumento = documento.text.toString();
val vnombre = nombre.text.toString();
val vtelefono = telefono.text.toString();
val vdireccion = direccion.text.toString();
if(vdocumento.isNotBlank() && vdocumento.isNotEmpty()
&& vnombre.isNotBlank() && vnombre.isNotEmpty()
&& vtelefono.isNotBlank() && vtelefono.isNotEmpty()
&& vdireccion.isNotBlank() && vdireccion.isNotEmpty()
){
val db = dbHelper.writableDatabase
var values = ContentValues().apply {
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_CODIGO, vdocumento)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_NOMBRE, vnombre)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_DIRECCION, vdireccion)
put(FerreteriaContract.ClienteEntry.COLUMN_NAME_TELEFONO, vtelefono)
}
val newRowId = db?.update(FerreteriaContract.ClienteEntry.TABLE_NAME, values,FerreteriaContract.ClienteEntry.COLUMN_NAME_CODIGO+"="+vdocumento,null )
db.close()
if(newRowId != 1){
Toast.makeText(this,"La persona no existe",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"Persona modificada correctamente",Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Todos los campos son obligatorios", Toast.LENGTH_LONG).show();
}
}
fun borrar(view: View){
val vdocumento = documento.text.toString();
if(vdocumento.isNotBlank() && vdocumento.isNotEmpty()) {
val db = dbHelper.writableDatabase
val newRowId = db?.delete(FerreteriaContract.ClienteEntry.TABLE_NAME,FerreteriaContract.ClienteEntry.COLUMN_NAME_CODIGO+"="+vdocumento,null)
db.close()
documento.text = ""
nombre.text= ""
telefono.text= ""
direccion.text = ""
if ( newRowId != 0 ){
Toast.makeText(this,"Se ha eliminado un registro",Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "el documento es requierido", Toast.LENGTH_LONG).show();
}
No hay comentarios:
Publicar un comentario