2017-10-04 23:22:05 -05:00
|
|
|
var query = []
|
|
|
|
var grid = null
|
2017-10-06 00:10:27 -05:00
|
|
|
var msg = ''
|
2017-10-04 23:22:05 -05:00
|
|
|
|
|
|
|
|
2017-11-02 23:24:31 -06:00
|
|
|
function get_condicion_pago(){
|
|
|
|
webix.ajax().get('/values/condicionespago', {
|
|
|
|
error: function(text, data, xhr) {
|
|
|
|
},
|
|
|
|
success: function(text, data, xhr) {
|
|
|
|
var values = data.json();
|
|
|
|
$$('txt_condicion_pago').define('suggest', values)
|
|
|
|
$$('txt_condicion_pago').refresh()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 23:22:05 -05:00
|
|
|
function get_series(){
|
|
|
|
webix.ajax().get('/values/series', function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
table_series.clear()
|
|
|
|
table_series.insert(values)
|
|
|
|
pre = values[0]
|
|
|
|
$$('lst_serie').getList().parse(values)
|
|
|
|
$$('lst_serie').setValue(pre.id)
|
2017-10-15 02:30:55 -05:00
|
|
|
if(pre.usarcon){
|
|
|
|
$$('lst_tipo_comprobante').setValue(pre.usarcon)
|
2017-10-04 23:22:05 -05:00
|
|
|
$$('lst_tipo_comprobante').config.readonly = true
|
|
|
|
$$('lst_tipo_comprobante').refresh()
|
|
|
|
}
|
2017-10-06 00:10:27 -05:00
|
|
|
if(values.length == 1){
|
|
|
|
$$('lst_serie').config.readonly = true
|
|
|
|
$$('lst_serie').refresh()
|
|
|
|
}
|
2017-10-04 23:22:05 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_forma_pago(){
|
2017-10-07 00:16:58 -05:00
|
|
|
webix.ajax().get('/values/formapago', {key: true}, function(text, data){
|
2017-10-04 23:22:05 -05:00
|
|
|
var values = data.json()
|
|
|
|
$$('lst_forma_pago').getList().parse(values)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_monedas(){
|
|
|
|
webix.ajax().get('/values/monedas', function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
pre = values[0]
|
|
|
|
$$('lst_moneda').getList().parse(values)
|
|
|
|
$$('lst_moneda').setValue(pre.id)
|
2017-10-06 00:10:27 -05:00
|
|
|
if(values.length == 1){
|
|
|
|
$$('fs_moneda').hide()
|
2017-10-18 23:06:07 -05:00
|
|
|
//~ $$('fs_moneda').refresh()
|
2017-10-06 00:10:27 -05:00
|
|
|
}
|
2017-10-04 23:22:05 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_uso_cfdi(){
|
2017-10-07 00:16:58 -05:00
|
|
|
get_uso_cfdi_to_table({key: true})
|
|
|
|
query = table_usocfdi.chain().data()
|
2017-10-06 00:10:27 -05:00
|
|
|
$$('lst_uso_cfdi').getList().parse(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_regimen_fiscal(){
|
|
|
|
webix.ajax().get('/values/regimenes', function(text, data){
|
2017-10-04 23:22:05 -05:00
|
|
|
var values = data.json()
|
|
|
|
pre = values[0]
|
2017-10-06 00:10:27 -05:00
|
|
|
$$('lst_regimen_fiscal').getList().parse(values)
|
|
|
|
$$('lst_regimen_fiscal').setValue(pre.id)
|
|
|
|
if(values.length == 1){
|
|
|
|
$$('fs_regimen_fiscal').hide()
|
2017-10-18 23:06:07 -05:00
|
|
|
//~ $$('fs_regimen_fiscal').refresh()
|
2017-10-06 00:10:27 -05:00
|
|
|
}
|
2017-10-04 23:22:05 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function default_config(){
|
2017-10-05 15:33:25 -05:00
|
|
|
webix.ajax().sync().get('/values/taxes', function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
table_taxes.clear()
|
|
|
|
table_taxes.insert(values)
|
|
|
|
})
|
2017-10-04 23:22:05 -05:00
|
|
|
get_series()
|
|
|
|
get_forma_pago()
|
|
|
|
get_monedas()
|
|
|
|
get_uso_cfdi()
|
2017-10-06 00:10:27 -05:00
|
|
|
get_regimen_fiscal()
|
2017-10-04 23:22:05 -05:00
|
|
|
table_pt.clear()
|
|
|
|
table_totals.clear()
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
function cmd_new_invoice_click(id, e, node){
|
2017-10-04 23:22:05 -05:00
|
|
|
var form = $$('form_invoice')
|
|
|
|
var grid_totals = $$('grid_totals')
|
|
|
|
grid = $$('grid_details')
|
2017-06-27 15:43:02 -05:00
|
|
|
|
2017-10-04 23:22:05 -05:00
|
|
|
default_config()
|
|
|
|
form.adjust()
|
2017-10-05 15:33:25 -05:00
|
|
|
form.setValues({id: 0, id_partner: 0, lbl_client: 'Ninguno'})
|
2017-11-02 23:24:31 -06:00
|
|
|
get_condicion_pago()
|
2017-10-04 23:22:05 -05:00
|
|
|
grid.clearAll()
|
|
|
|
grid_totals.clearAll()
|
|
|
|
grid_totals.add({id: 1, concepto: 'SubTotal', importe: 0})
|
|
|
|
$$('multi_invoices').setValue('invoices_new')
|
2017-11-01 10:36:27 -06:00
|
|
|
form.focus('search_client_name')
|
2017-10-04 23:22:05 -05:00
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
function cmd_edit_invoice_click(id, e, node){
|
|
|
|
|
|
|
|
$$("multi_invoices").setValue("invoices_new")
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function delete_invoice(id){
|
|
|
|
webix.ajax().del('/invoices', {id: id}, function(text, xml, xhr){
|
|
|
|
if(xhr.status == 200){
|
2017-10-10 18:49:05 -05:00
|
|
|
gi.remove(id)
|
2017-10-06 00:10:27 -05:00
|
|
|
msg_sucess('Factura eliminada correctamente')
|
|
|
|
}else{
|
|
|
|
msg_error('No se pudo eliminar')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
function cmd_delete_invoice_click(id, e, node){
|
2017-10-16 23:36:10 -05:00
|
|
|
if(gi.count() == 0){
|
|
|
|
return
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
2017-10-10 18:49:05 -05:00
|
|
|
var row = gi.getSelectedItem()
|
2017-10-06 00:10:27 -05:00
|
|
|
if (row == undefined){
|
|
|
|
msg_error('Selecciona una factura')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-10 18:49:05 -05:00
|
|
|
if(row.uuid){
|
2017-10-06 00:10:27 -05:00
|
|
|
msg_error('Solo se pueden eliminar facturas sin timbrar')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = '¿Estás seguro de eliminar la siguiente Factura?<BR><BR>'
|
|
|
|
msg += '(' + row['folio'] + ') ' + row['cliente']
|
|
|
|
msg += '<BR><BR>ESTA ACCIÓN NO SE PUEDE DESHACER'
|
|
|
|
webix.confirm({
|
|
|
|
title:'Eliminar Factura',
|
|
|
|
ok:'Si',
|
|
|
|
cancel:'No',
|
|
|
|
type:'confirm-error',
|
|
|
|
text:msg,
|
|
|
|
callback:function(result){
|
|
|
|
if (result){
|
|
|
|
delete_invoice(row['id'])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function validate_invoice(values){
|
|
|
|
|
|
|
|
if(values.id_partner == 0){
|
|
|
|
webix.UIManager.setFocus('search_client_name')
|
|
|
|
msg = 'Selecciona un cliente'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!grid.count()){
|
|
|
|
webix.UIManager.setFocus('search_product_id')
|
|
|
|
msg = 'Agrega al menos un producto o servicio'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:58 -05:00
|
|
|
var uso_cfdi = $$('lst_uso_cfdi').getValue()
|
|
|
|
if(uso_cfdi.trim() == ""){
|
|
|
|
webix.UIManager.setFocus('lst_uso_cfdi')
|
|
|
|
msg = 'El Uso del CFDI es requerido'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var forma_pago = $$('lst_forma_pago').getValue()
|
|
|
|
if(forma_pago.trim() == ""){
|
|
|
|
webix.UIManager.setFocus('lst_forma_pago')
|
|
|
|
msg = 'La Forma de pago es requerida'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
var tipo_cambio = $$('txt_tipo_cambio').getValue()
|
|
|
|
if(tipo_cambio.trim() == ""){
|
|
|
|
webix.UIManager.setFocus('txt_tipo_cambio')
|
|
|
|
msg = 'El Tipo de Cambio es requerido'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isNaN(tipo_cambio * 1)){
|
|
|
|
webix.UIManager.setFocus('txt_tipo_cambio')
|
|
|
|
msg = 'El Tipo de Cambio debe ser un valor númerico'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var moneda = $$('lst_moneda').getValue()
|
|
|
|
if(moneda == 'MXN' && tipo_cambio != 1){
|
|
|
|
webix.UIManager.setFocus('txt_tipo_cambio')
|
|
|
|
msg = 'Si la moneda es MXN, el Tipo de Cambio debe ser 1.00'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(moneda != 'MXN' && tipo_cambio == 1){
|
|
|
|
webix.UIManager.setFocus('txt_tipo_cambio')
|
|
|
|
msg = 'Si la moneda no es MXN, el Tipo de Cambio debe ser diferente de 1.00'
|
|
|
|
msg_error(msg)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:58 -05:00
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function update_grid_invoices(values){
|
|
|
|
if(values.new){
|
2017-10-10 18:49:05 -05:00
|
|
|
gi.add(values.row)
|
2017-10-06 00:10:27 -05:00
|
|
|
}else{
|
2017-10-10 18:49:05 -05:00
|
|
|
gi.updateItem(values.row['id'], values.row)
|
2017-10-06 00:10:27 -05:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
2017-10-07 00:16:58 -05:00
|
|
|
function send_timbrar(id){
|
|
|
|
webix.ajax().get('/values/timbrar', {id: id}, function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
if(values.ok){
|
|
|
|
msg_sucess(values.msg)
|
2017-10-10 18:49:05 -05:00
|
|
|
gi.updateItem(id, values.row)
|
2017-10-07 00:16:58 -05:00
|
|
|
}else{
|
|
|
|
webix.alert({
|
|
|
|
title: 'Error al Timbrar',
|
|
|
|
text: values.msg,
|
|
|
|
type: 'alert-error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
|
|
|
|
function save_invoice(data){
|
|
|
|
var result = false
|
|
|
|
var values = NaN
|
|
|
|
|
|
|
|
webix.ajax().sync().post('invoices', data, {
|
|
|
|
error:function(text, data, XmlHttpRequest){
|
|
|
|
msg = 'Ocurrio un error, consulta a soporte técnico'
|
|
|
|
msg_error(msg)
|
|
|
|
},
|
|
|
|
success:function(text, data, XmlHttpRequest){
|
|
|
|
values = data.json();
|
|
|
|
if(values.ok){
|
2017-10-10 18:49:05 -05:00
|
|
|
msg_sucess('Factura guardada correctamente. Enviando a timbrar')
|
2017-10-06 00:10:27 -05:00
|
|
|
update_grid_invoices(values)
|
2017-10-07 00:16:58 -05:00
|
|
|
send_timbrar(values.row['id'])
|
2017-10-06 00:10:27 -05:00
|
|
|
result = true
|
|
|
|
}else{
|
2017-10-10 18:49:05 -05:00
|
|
|
msg_error(values.msg)
|
2017-10-06 00:10:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if(result){
|
|
|
|
table_pt.clear()
|
|
|
|
table_totals.clear()
|
|
|
|
grid.clearAll()
|
|
|
|
$$('grid_totals').clearAll()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
|
2017-10-04 23:22:05 -05:00
|
|
|
function cmd_timbrar_click(id, e, node){
|
2017-10-06 00:10:27 -05:00
|
|
|
var form = this.getFormView();
|
|
|
|
|
|
|
|
if(!form.validate()) {
|
|
|
|
webix.message({type:'error', text:'Valores inválidos'})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var values = form.getValues();
|
|
|
|
if(!validate_invoice(values)){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rows = grid.data.getRange()
|
|
|
|
for (i = 0; i < rows.length; i++) {
|
|
|
|
delete rows[i]['delete']
|
|
|
|
delete rows[i]['clave']
|
|
|
|
delete rows[i]['unidad']
|
|
|
|
delete rows[i]['importe']
|
|
|
|
rows[i]['valor_unitario'] = parseFloat(rows[i]['valor_unitario'])
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
var data = new Object()
|
|
|
|
data['id'] = values.id
|
|
|
|
data['cliente'] = values.id_partner
|
|
|
|
data['productos'] = rows
|
|
|
|
data['serie'] = $$('lst_serie').getText()
|
|
|
|
data['forma_pago'] = $$('lst_forma_pago').getValue()
|
|
|
|
data['condiciones_pago'] = $$('txt_condicion_pago').getValue().trim()
|
|
|
|
data['moneda'] = $$('lst_moneda').getValue()
|
|
|
|
data['tipo_cambio'] = $$('txt_tipo_cambio').getValue()
|
|
|
|
data['tipo_comprobante'] = $$('lst_tipo_comprobante').getValue()
|
|
|
|
data['metodo_pago'] = $$('lst_metodo_pago').getValue()
|
|
|
|
data['uso_cfdi'] = $$('lst_uso_cfdi').getValue()
|
|
|
|
data['regimen_fiscal'] = $$('lst_regimen_fiscal').getValue()
|
|
|
|
|
|
|
|
if(!save_invoice(data)){
|
2017-06-27 15:43:02 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
form.setValues({id_partner: 0, lbl_partner: 'Ninguno'})
|
2017-10-04 23:22:05 -05:00
|
|
|
$$('multi_invoices').setValue('invoices_home')
|
|
|
|
}
|
2017-06-27 15:43:02 -05:00
|
|
|
|
|
|
|
|
2017-10-04 23:22:05 -05:00
|
|
|
function cmd_close_invoice_click(id, e, node){
|
|
|
|
$$('multi_invoices').setValue('invoices_home')
|
|
|
|
}
|
2017-10-05 12:16:20 -05:00
|
|
|
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
function search_client_by_id(id){
|
2017-10-05 12:16:20 -05:00
|
|
|
var msg = ''
|
2017-10-05 15:33:25 -05:00
|
|
|
webix.ajax().get('/values/client', {'id': id}, {
|
2017-10-05 12:16:20 -05:00
|
|
|
error: function(text, data, xhr) {
|
|
|
|
webix.message({type: 'error', text: 'Error al consultar'})
|
|
|
|
},
|
|
|
|
success: function(text, data, xhr){
|
|
|
|
var values = data.json()
|
|
|
|
if (values.ok){
|
2017-10-05 15:33:25 -05:00
|
|
|
set_client(values.row)
|
2017-10-05 12:16:20 -05:00
|
|
|
}else{
|
|
|
|
msg = 'No se encontró un cliente con la clave: ' + id
|
|
|
|
webix.message({type:'error', text: msg})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
function set_client(row){
|
2017-10-05 12:16:20 -05:00
|
|
|
var form = $$('form_invoice')
|
|
|
|
var html = '<span class="webix_icon fa-user"></span><span class="lbl_partner">'
|
|
|
|
form.setValues({
|
2017-10-07 00:16:58 -05:00
|
|
|
id_partner:row.id, search_client_id:'', search_client_name:'',
|
|
|
|
forma_pago: row.forma_pago, uso_cfdi: row.uso_cfdi}, true)
|
2017-10-05 12:16:20 -05:00
|
|
|
html += row.nombre + ' (' + row.rfc + ')</span>'
|
2017-10-05 15:33:25 -05:00
|
|
|
$$('lbl_client').setValue(html)
|
2017-10-05 12:16:20 -05:00
|
|
|
form.focus('search_product_id')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
function grid_clients_found_click(obj){
|
|
|
|
set_client(obj)
|
2017-10-05 12:16:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
function search_client_id_key_press(code, e){
|
2017-10-05 12:16:20 -05:00
|
|
|
var value = this.getValue()
|
|
|
|
if(code == 13 && value.length > 0){
|
|
|
|
var id = parseInt(value, 10)
|
|
|
|
if (isNaN(id)){
|
|
|
|
webix.message({type:'error', text:'Captura una clave válida'});
|
|
|
|
}else{
|
2017-10-05 15:33:25 -05:00
|
|
|
search_client_by_id(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function calculate_taxes(){
|
|
|
|
var tmp = null
|
|
|
|
table_totals.clear()
|
|
|
|
var subtotal = 0
|
|
|
|
var total_iva = 0
|
|
|
|
var id = 2
|
|
|
|
var grid_totals = $$('grid_totals')
|
|
|
|
|
|
|
|
grid_totals.clearAll()
|
|
|
|
grid_totals.add({id: 1, concepto: 'SubTotal', importe: 0})
|
|
|
|
|
|
|
|
grid.eachRow(function(row){
|
|
|
|
var product = grid.getItem(row)
|
|
|
|
subtotal += parseFloat(product.importe)
|
|
|
|
query = table_pt.chain().find({'product': product.id}).data()
|
|
|
|
for(var tax of query){
|
|
|
|
tmp = table_totals.findOne({'tax': tax.tax})
|
|
|
|
if(tmp === null){
|
|
|
|
table_totals.insert(
|
|
|
|
{'tax': tax.tax, 'importe': parseFloat(product.importe)})
|
|
|
|
tmp = table_totals.findOne({'tax': tax.tax})
|
|
|
|
}else{
|
|
|
|
tmp.importe += parseFloat(product.importe)
|
|
|
|
table_totals.update(tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var tax = null
|
|
|
|
var tipo = 'Traslado '
|
|
|
|
var concepto = ''
|
|
|
|
var total_tax = 0
|
|
|
|
query = table_totals.chain().data()
|
|
|
|
for(var t of query){
|
|
|
|
tax = table_taxes.findOne({'id': t.tax})
|
|
|
|
if(tax.tipo == 'E' || tax.tipo == 'R'){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
concepto = tipo + tax.name + ' (' + tax.tasa + ')'
|
2017-10-06 00:10:27 -05:00
|
|
|
total_tax = (tax.tasa * t.importe).round(DECIMALES)
|
2017-10-05 15:33:25 -05:00
|
|
|
grid_totals.add({id: id, concepto: concepto, importe: total_tax})
|
|
|
|
id += 1
|
|
|
|
if(tax.name == 'IVA'){
|
|
|
|
total_iva += total_tax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tipo = 'Retención '
|
|
|
|
for(var t of query){
|
|
|
|
tax = table_taxes.findOne({'id': t.tax})
|
|
|
|
if(tax.tipo == 'E' || tax.tipo == 'T'){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
concepto = tipo + tax.name + ' (' + tax.tasa + ')'
|
|
|
|
if(tax.tasa == (2/3).round(6)){
|
2017-10-06 00:10:27 -05:00
|
|
|
total_tax = (tax.tasa * total_iva * -1).round(DECIMALES)
|
2017-10-05 15:33:25 -05:00
|
|
|
concepto = tipo + tax.name + ' (2/3)'
|
|
|
|
}else{
|
2017-10-06 00:10:27 -05:00
|
|
|
total_tax = (tax.tasa * t.importe * -1).round(DECIMALES)
|
2017-10-05 15:33:25 -05:00
|
|
|
}
|
|
|
|
grid_totals.add({id: id, concepto: concepto, importe: total_tax})
|
|
|
|
id += 1
|
|
|
|
}
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
var row = {importe: subtotal}
|
2017-10-05 15:33:25 -05:00
|
|
|
grid_totals.updateItem(1, row)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function set_product(values){
|
|
|
|
var taxes = values.taxes
|
|
|
|
var values = values.row
|
|
|
|
var form = $$('form_invoice')
|
|
|
|
var row = grid.getItem(values.id)
|
|
|
|
|
|
|
|
values['delete'] = '-'
|
|
|
|
if (row == undefined){
|
|
|
|
values['cantidad'] = 1
|
|
|
|
values['importe'] = values['valor_unitario']
|
|
|
|
grid.add(values)
|
|
|
|
} else {
|
|
|
|
values['cantidad'] = parseFloat(row.cantidad) + 1
|
|
|
|
values['importe'] = values['valor_unitario'] * values['cantidad']
|
|
|
|
grid.updateItem(row.id, values)
|
|
|
|
}
|
|
|
|
form.setValues({search_product_id:'', search_product_name:''}, true)
|
|
|
|
|
|
|
|
for(var v of taxes){
|
|
|
|
var pt = table_pt.findOne(v)
|
|
|
|
if(pt === null){
|
|
|
|
table_pt.insert(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
calculate_taxes()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function grid_products_found_click(obj){
|
|
|
|
search_product_by_id(obj.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function search_product_by_id(id){
|
|
|
|
var msg = ''
|
|
|
|
|
|
|
|
webix.ajax().get('/values/product', {'id': id}, {
|
|
|
|
error: function(text, data, xhr) {
|
|
|
|
webix.message({type: 'error', text: 'Error al consultar'})
|
|
|
|
},
|
|
|
|
success: function(text, data, xhr){
|
|
|
|
var values = data.json()
|
|
|
|
if (values.ok){
|
|
|
|
set_product(values)
|
|
|
|
} else {
|
|
|
|
msg = 'No se encontró un producto con la clave: ' + id
|
|
|
|
webix.message({type: 'error', text: msg})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function search_product_id_key_press(code, e){
|
|
|
|
var value = this.getValue()
|
|
|
|
if(code == 13 && value.length > 0){
|
|
|
|
var id = parseInt(value, 10)
|
|
|
|
if (isNaN(id)){
|
|
|
|
webix.message({type: 'error', text: 'Captura una clave válida'});
|
|
|
|
}else{
|
|
|
|
search_product_by_id(id)
|
2017-10-05 12:16:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 15:33:25 -05:00
|
|
|
|
|
|
|
|
2017-10-06 00:10:27 -05:00
|
|
|
function grid_details_before_edit_start(id){
|
|
|
|
var columns = ['', 'descripcion', 'cantidad', 'valor_unitario']
|
|
|
|
if(!columns.indexOf(id.column)){
|
|
|
|
return !this.getItem(id.row)[id.column]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function grid_details_before_edit_stop(state, editor){
|
|
|
|
var row = grid.getItem(editor.row)
|
|
|
|
|
|
|
|
if(editor.column == 'descripcion'){
|
|
|
|
if(!state.value.trim()){
|
|
|
|
msg = 'La descripción no puede estar vacía'
|
|
|
|
webix.message({type:'error', text: msg})
|
|
|
|
grid.blockEvent()
|
|
|
|
state.value = state.old
|
|
|
|
grid.editCancel()
|
|
|
|
grid.unblockEvent()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
state.value = state.value.trim()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if(editor.column == 'cantidad'){
|
|
|
|
var cantidad = parseFloat(state.value)
|
|
|
|
if(isNaN(cantidad)){
|
|
|
|
msg = 'La cantidad debe ser un número'
|
|
|
|
webix.message({type:'error', text: msg})
|
|
|
|
grid.blockEvent()
|
|
|
|
state.value = state.old
|
|
|
|
grid.editCancel()
|
|
|
|
grid.unblockEvent()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var valor_unitario = row['valor_unitario']
|
|
|
|
}
|
|
|
|
|
|
|
|
if(editor.column == 'valor_unitario'){
|
|
|
|
var valor_unitario = parseFloat(state.value)
|
|
|
|
if(isNaN(valor_unitario)){
|
|
|
|
msg = 'El valor unitario debe ser un número'
|
|
|
|
webix.message({type:'error', text: msg})
|
|
|
|
grid.blockEvent()
|
|
|
|
state.value = state.old
|
|
|
|
grid.editCancel()
|
|
|
|
grid.unblockEvent()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var cantidad = row['cantidad']
|
|
|
|
}
|
|
|
|
|
|
|
|
row['importe'] = (cantidad * valor_unitario).round(DECIMALES)
|
|
|
|
grid.refresh()
|
|
|
|
calculate_taxes()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 15:33:25 -05:00
|
|
|
function grid_details_click(id, e, node){
|
|
|
|
if(id.column != 'delete'){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
grid.remove(id.row)
|
|
|
|
calculate_taxes()
|
|
|
|
}
|
2017-10-06 00:10:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
function grid_details_header_click(id){
|
|
|
|
if(id.column != 'delete'){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = '¿Estás seguro de quitar todos los productos?'
|
|
|
|
webix.confirm({
|
|
|
|
title: 'Quitar todos',
|
|
|
|
ok: 'Si',
|
|
|
|
cancel: 'No',
|
|
|
|
type: 'confirm-error',
|
|
|
|
text: msg,
|
|
|
|
callback:function(result){
|
|
|
|
if (result){
|
|
|
|
grid.clearAll()
|
|
|
|
calculate_taxes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-10 18:49:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
function cmd_refacturar_click(){
|
|
|
|
show('Refacturar')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cmd_invoice_timbrar_click(){
|
|
|
|
if(gi.count() == 0){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = gi.getSelectedItem()
|
|
|
|
if (row == undefined){
|
|
|
|
msg_error('Selecciona una factura')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(row.uuid){
|
|
|
|
msg_error('La factura ya esta timbrada')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = '¿Estás seguro de enviar a timbrar esta factura?'
|
|
|
|
webix.confirm({
|
|
|
|
title: 'Timbrar Factura',
|
|
|
|
ok: 'Si',
|
|
|
|
cancel: 'No',
|
|
|
|
type: 'confirm-error',
|
|
|
|
text: msg,
|
|
|
|
callback:function(result){
|
|
|
|
if(result){
|
|
|
|
send_timbrar(row.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-10 22:42:11 -05:00
|
|
|
|
|
|
|
|
2017-10-16 00:02:51 -05:00
|
|
|
function enviar_correo(row){
|
|
|
|
if(!row.uuid){
|
|
|
|
msg_error('La factura no esta timbrada')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = '¿Estás seguro de enviar por correo esta factura?'
|
|
|
|
webix.confirm({
|
|
|
|
title: 'Enviar Factura',
|
|
|
|
ok: 'Si',
|
|
|
|
cancel: 'No',
|
|
|
|
type: 'confirm-error',
|
|
|
|
text: msg,
|
|
|
|
callback:function(result){
|
|
|
|
if(result){
|
|
|
|
webix.ajax().post('/values/sendmail', {'id': row.id}, {
|
|
|
|
error:function(text, data, XmlHttpRequest){
|
|
|
|
msg = 'Ocurrio un error, consulta a soporte técnico'
|
|
|
|
msg_error(msg)
|
|
|
|
},
|
|
|
|
success:function(text, data, XmlHttpRequest){
|
|
|
|
values = data.json();
|
|
|
|
if(values.ok){
|
|
|
|
msg_sucess(values.msg)
|
|
|
|
}else{
|
|
|
|
msg_error(values.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-10 22:42:11 -05:00
|
|
|
function grid_invoices_click(id, e, node){
|
|
|
|
var row = this.getItem(id)
|
2017-10-15 02:30:55 -05:00
|
|
|
|
2017-10-10 22:42:11 -05:00
|
|
|
if(id.column == 'xml'){
|
|
|
|
location = '/doc/xml/' + row.id
|
2017-10-15 02:30:55 -05:00
|
|
|
}else if(id.column == 'pdf'){
|
|
|
|
location = '/doc/pdf/' + row.id
|
|
|
|
}else if(id.column == 'zip'){
|
|
|
|
location = '/doc/zip/' + row.id
|
|
|
|
}else if(id.column == 'email'){
|
2017-10-16 00:02:51 -05:00
|
|
|
enviar_correo(row)
|
2017-10-10 22:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-16 23:36:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
function send_cancel(id){
|
2017-10-29 16:53:10 -06:00
|
|
|
webix.ajax().get('/values/cancelinvoice', {id: id}, function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
if(values.ok){
|
|
|
|
msg_sucess(values.msg)
|
|
|
|
gi.updateItem(id, values.row)
|
|
|
|
}else{
|
|
|
|
webix.alert({
|
|
|
|
title: 'Error al Cancelar',
|
|
|
|
text: values.msg,
|
|
|
|
type: 'alert-error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2017-10-16 23:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function cmd_invoice_cancelar_click(){
|
|
|
|
if(gi.count() == 0){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = gi.getSelectedItem()
|
|
|
|
if (row == undefined){
|
|
|
|
msg_error('Selecciona una factura')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!row.uuid){
|
|
|
|
msg_error('La factura no esta timbrada, solo es posible cancelar \
|
|
|
|
facturas timbradas')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-29 16:53:10 -06:00
|
|
|
if(row.estatus == 'Cancelada'){
|
|
|
|
msg_error('La factura ya esta cancelada')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-16 23:36:10 -05:00
|
|
|
msg = '¿Estás seguro de enviar a cancelar esta factura?<BR><BR> \
|
|
|
|
ESTA ACCIÓN NO SE PUEDE DESHACER'
|
|
|
|
webix.confirm({
|
|
|
|
title: 'Cancelar Factura',
|
|
|
|
ok: 'Si',
|
|
|
|
cancel: 'No',
|
|
|
|
type: 'confirm-error',
|
|
|
|
text: msg,
|
|
|
|
callback:function(result){
|
|
|
|
if(result){
|
|
|
|
send_cancel(row.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-28 00:30:42 -05:00
|
|
|
|
|
|
|
|
2017-10-28 21:58:18 -05:00
|
|
|
function get_invoices(rango){
|
|
|
|
if(rango == undefined){
|
|
|
|
var fy = $$('filter_year')
|
|
|
|
var fm = $$('filter_month')
|
|
|
|
|
|
|
|
var y = fy.getValue()
|
|
|
|
var m = fm.getValue()
|
|
|
|
rango = {'year': y, 'month': m}
|
|
|
|
}
|
|
|
|
|
|
|
|
var grid = $$('grid_invoices')
|
|
|
|
webix.ajax().get('/invoices', rango, {
|
|
|
|
error: function(text, data, xhr) {
|
|
|
|
webix.message({type: 'error', text: 'Error al consultar'})
|
|
|
|
},
|
|
|
|
success: function(text, data, xhr) {
|
|
|
|
var values = data.json();
|
|
|
|
grid.clearAll();
|
|
|
|
if (values.ok){
|
|
|
|
grid.parse(values.rows, 'json');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-28 00:30:42 -05:00
|
|
|
function filter_year_change(nv, ov){
|
2017-10-28 21:58:18 -05:00
|
|
|
get_invoices()
|
2017-10-28 00:30:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function filter_month_change(nv, ov){
|
2017-10-28 21:58:18 -05:00
|
|
|
get_invoices()
|
2017-10-28 00:30:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function filter_dates_change(range){
|
2017-10-28 22:21:39 -05:00
|
|
|
if(range.start != null && range.end != null){
|
|
|
|
get_invoices(range)
|
|
|
|
}
|
2017-10-28 00:30:42 -05:00
|
|
|
}
|
2017-10-28 23:37:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
function cmd_invoice_sat_click(){
|
|
|
|
if(gi.count() == 0){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = gi.getSelectedItem()
|
|
|
|
if (row == undefined){
|
|
|
|
msg_error('Selecciona una factura')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!row.uuid){
|
|
|
|
msg_error('La factura no esta timbrada, solo es posible consultar \
|
|
|
|
el estatus en el SAT de facturas timbradas')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
webix.ajax().get('/values/statussat', {id: row.id}, function(text, data){
|
|
|
|
var values = data.json()
|
|
|
|
show(values)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2017-10-30 00:03:02 -06:00
|
|
|
|
|
|
|
|
|
|
|
function cmd_prefactura_click(){
|
|
|
|
var form = this.getFormView()
|
|
|
|
|
|
|
|
if(!form.validate()) {
|
|
|
|
msg_error('Valores inválidos')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var values = form.getValues()
|
|
|
|
if(!validate_invoice(values)){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
show('PreFactura')
|
|
|
|
}
|
2017-11-02 23:24:31 -06:00
|
|
|
|
|
|
|
|
|
|
|
function lst_metodo_pago_change(nv, ov){
|
|
|
|
if(nv == 'PPD'){
|
|
|
|
$$('lst_forma_pago').setValue('99')
|
|
|
|
}
|
|
|
|
}
|