function cmd_new_partner_click(id, e, node){
$$('form_partner').setValues({
id:0, country:'México', opt_person_type: 1, is_active: true})
$$('grid_partners').clearSelection()
$$('multi_partners').setValue('partners_new')
};
function cmd_new_contact_click(id, e, node){
$$('grid_contacts').clearSelection()
$$('multi_contacts').setValue('contacts_new')
};
function cmd_edit_partner_click(id, e, node){
var row = $$('grid_partners').getSelectedItem();
if (row == undefined){
webix.message({ type:'error', text:'Selecciona un Cliente' });
return
};
webix.ajax().get("/partners", {id:row['id']}, {
error: function(text, data, xhr) {
webix.message({ type:"error", text: "Error al consultar"});
},
success: function(text, data, xhr){
var values = data.json();
$$('form_partner').setValues(values);
}
});
$$("multi_partners").setValue("partners_new")
};
function cmd_delete_partner_click(id, e, node){
var row = $$('grid_partners').getSelectedItem();
if (row == undefined){
webix.message({ type:'error', text:'Selecciona un Cliente' });
return
};
var msg = '¿Estás seguro de eliminar al cliente?
'
msg += row['name'] + ' (' + row['rfc'] + ')'
msg += '
ESTA ACCIÓN NO SE PUEDE DESHACER'
webix.confirm({
title:'Eliminar Cliente',
ok:'Si',
cancel:'No',
type:'confirm-error',
text:msg,
callback:function(result){
if (result){
delete_partner(row['id'])
}
}
})
};
function delete_partner(id){
webix.ajax().del('/partners', {id:id}, function(text, xml, xhr){
var msg = 'Cliente eliminado correctamente'
if (xhr.status == 200){
$$('grid_partners').remove(id);
webix.message({type:'success', text:msg});
} else {
msg = 'No se pudo eliminar'
webix.message({type:'error', text:msg});
}
});
}
function cmd_save_partner_click(id, e, node){
var form = this.getFormView();
if (!form.validate()) {
webix.message({ type:'error', text:'Valores inválidos'})
return
}
var values = form.getValues();
webix.ajax().post("partners", values, {
error:function(text, data, XmlHttpRequest){
msg = 'Ocurrio un error, consulta a soporte técnico';
webix.message({type:'error', text:msg});
},
success:function(text, data, XmlHttpRequest){
var values = data.json();
if (values.ok) {
update_grid_partner(values)
} else {
webix.message({type:'error', text:values.msg});
}
}
});
};
function update_grid_partner(values){
var msg = 'Cliente agregado correctamente'
if (values.new){
$$('form_partner').clear()
$$('grid_partners').add(values.row)
}else{
msg = 'Cliente actualizado correctamente'
$$("grid_partners").updateItem(values.row['id'], values.row)
}
$$('multi_partners').setValue('partners_home')
webix.message({type:'success', text:msg})
}
function cmd_cancel_partner_click(id, e, node){
$$('multi_partners').setValue('partners_home')
}
function cmd_cancel_contact_click(id, e, node){
$$('multi_contacts').setValue('contacts_home')
}
function postal_code_key_up(){
var value = this.getValue()
var msg = ''
if( value.length == 5 ){
webix.ajax().get('/cp', {cp:value}, {
error: function(text, data, xhr) {
webix.message({type:'error', text:'Error al consultar el C.P.'})
},
success: function(text, data, xhr) {
var values = data.json();
if (values.estado == undefined){
msg = 'No se encontró el C.P., asegurate de que sea correcto'
webix.message({type:'error', text:msg})
} else {
$$('form_partner').setValues({
state:values.estado,
municipio: values.municipio,
colonia:''}, true)
$$('colonia').define('suggest', [])
if (webix.isArray(values.colonia)){
$$('colonia').define('suggest', values.colonia)
}else{
$$('form_partner').setValues({colonia:values.colonia}, true)
}
$$('colonia').refresh()
$$('form_partner').focus('colonia')
}
}
})
}
}
function postal_code_key_press(code, e){
var data = [8, 9, 37, 39, 46]
if ( data.indexOf(code) >= 0 ){
return true;
}
if ( code < 48 || code > 57){
return false;
}
}
function colonia_on_focus(){
if ($$(this.config.suggest).getList().config.height > 2){
$$(this.config.suggest).show(this.getInputNode())
}
}