empresa-libre/source/static/js/controller/invoices.js

1413 lines
35 KiB
JavaScript
Raw Normal View History

2017-10-04 23:22:05 -05:00
var query = []
var grid = null
2017-10-06 00:10:27 -05:00
var msg = ''
2017-11-08 23:47:15 -06:00
var result = false
var tipo_relacion = ''
2017-11-12 18:50:41 -06:00
var anticipo = false
2017-10-04 23:22:05 -05: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()
webix.ajax().sync().get('/values/validartimbrar', function(text, data){
var values = data.json()
if(!values.ok){
msg_error(values.msg)
$$('cmd_timbrar').disable()
}else{
if(values.msg){
msg_error(values.msg)
}
$$('cmd_timbrar').enable()
}
})
2017-10-04 23:22:05 -05:00
}
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'})
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})
2017-11-08 23:47:15 -06:00
$$('cmd_cfdi_relacionados').disable()
2017-10-04 23:22:05 -05:00
$$('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
}
2017-11-08 23:47:15 -06:00
msg = '¿Estás seguro de eliminar la siguiente Factura?<BR><BR>'
2017-10-06 00:10:27 -05:00
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-11-12 18:50:41 -06:00
anticipo = $$('chk_cfdi_anticipo').getValue()
if(anticipo){
var mp = $$('lst_metodo_pago').getValue()
if(mp != 'PUE'){
msg = 'En anticipos, el método de pago debe ser: Pago en una sola exhibición'
msg_error(msg)
return false
}
if(grid.count() != 1){
msg = 'Los anticipos solo llevan un concepto'
msg_error(msg)
return false
}
query = table_relaciones.chain().data()
if(query.length > 0){
msg = 'Los anticipos no deben llevar CFDI relacionados'
msg_error(msg)
return false
}
}
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-11-12 21:56:45 -06:00
function send_anticipo_egreso(id){
webix.ajax().get('/values/anticipoegreso', {id: id}, function(text, data){
var values = data.json()
if(values.ok){
msg_sucess(values.msg)
gi.add(values.row)
}else{
webix.alert({
title: 'Error al Timbrar',
text: values.msg,
type: 'alert-error'
})
}
})
}
function generar_anticipo_egreso(id){
msg = 'La factura tiene un CFDI de anticipo relacionado<BR><BR>'
msg += '¿Deseas generar la factura de egreso correspondiente?'
webix.confirm({
title: 'Generar Egreso',
ok: 'Si',
cancel: 'No',
type: 'confirm-error',
text: msg,
callback:function(result){
if(result){
send_anticipo_egreso(id)
}
}
})
}
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-11-12 21:56:45 -06:00
if(values.anticipo){
//~ generar_anticipo_egreso(id)
//~ show('Generar egreso de anticipo')
}
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-11-05 00:13:48 -06:00
function save_preinvoice(data){
var result = false
var values = NaN
webix.ajax().sync().post('preinvoices', 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){
msg_sucess('Pre Factura generada correctamente')
result = true
}else{
msg_error(values.msg)
}
}
})
if(result){
table_pt.clear()
table_totals.clear()
grid.clearAll()
$$('grid_totals').clearAll()
}
return result
}
2017-11-08 23:47:15 -06:00
function guardar_y_timbrar(values){
query = table_relaciones.chain().data()
var ids = []
if(query.length > 0){
for (i = 0; i < query.length; i++) {
ids.push(query[i]['id'])
}
2017-10-06 00:10:27 -05:00
}
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()
2017-11-08 23:47:15 -06:00
data['relacionados'] = ids
data['tipo_relacion'] = tipo_relacion
2017-11-12 18:50:41 -06:00
data['anticipo'] = anticipo
2017-10-06 00:10:27 -05:00
if(!save_invoice(data)){
2017-06-27 15:43:02 -05:00
return
}
2017-11-08 23:47:15 -06:00
table_relaciones.clear()
tipo_relacion = ''
2017-11-12 18:50:41 -06:00
anticipo = false
2017-11-12 21:56:45 -06:00
$$('chk_cfdi_anticipo').setValue(0)
2017-11-08 23:47:15 -06:00
$$('form_invoice').setValues({id_partner: 0, lbl_partner: 'Ninguno'})
2017-10-04 23:22:05 -05:00
$$('multi_invoices').setValue('invoices_home')
2017-11-08 23:47:15 -06:00
}
function cmd_timbrar_click(id, e, node){
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
}
query = table_relaciones.chain().data()
msg = '¿Todos los datos son correctos?<BR><BR>'
if(query.length > 0){
msg += 'La factura tiene CFDI relacionados<BR><BR>'
}
2017-11-12 18:50:41 -06:00
if(anticipo){
msg += 'La factura es Anticipo<BR><BR>'
}
2017-11-08 23:47:15 -06:00
msg += '¿Estás seguro de timbrar esta factura?'
webix.confirm({
title: 'Timbrar Factura',
ok: 'Si',
cancel: 'No',
type: 'confirm-error',
text: msg,
callback:function(result){
if(result){
guardar_y_timbrar(values)
}
}
})
2017-10-04 23:22:05 -05:00
}
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-11-08 23:47:15 -06:00
$$('cmd_cfdi_relacionados').enable()
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
2017-11-05 00:13:48 -06:00
function reset_invoice(){
var form = $$('form_invoice')
var grid_totals = $$('grid_totals')
form.adjust()
form.setValues({id: 0, id_partner: 0, lbl_client: 'Ninguno'})
grid.clearAll()
grid_totals.clearAll()
grid_totals.add({id: 1, concepto: 'SubTotal', importe: 0})
table_pt.clear()
table_totals.clear()
2017-11-08 23:47:15 -06:00
$$('cmd_cfdi_relacionados').disable()
2017-11-05 00:13:48 -06:00
form.focus('search_client_name')
}
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
}
2017-11-05 00:13:48 -06:00
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'])
}
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_preinvoice(data)){
return
}
reset_invoice()
$$('tv_invoice').getTabbar().setValue('PreFacturas')
2017-10-30 00:03:02 -06:00
}
function lst_metodo_pago_change(nv, ov){
if(nv == 'PPD'){
$$('lst_forma_pago').setValue('99')
}
}
2017-11-05 00:13:48 -06:00
function get_prefacturas(){
var fy = $$('prefilter_year')
var fm = $$('prefilter_month')
var y = fy.getValue()
var m = fm.getValue()
rango = {'year': y, 'month': m}
var grid = $$('grid_preinvoices')
webix.ajax().get('/preinvoices', 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')
}
}
})
}
function tb_invoice_change(nv, ov){
if(nv == 'PreFacturas'){
get_prefacturas()
}
}
function prefilter_year_change(nv, ov){
get_prefacturas()
}
function prefilter_month_change(nv, ov){
get_prefacturas()
}
function delete_preinvoice(id){
webix.ajax().del('/preinvoices', {id: id}, function(text, xml, xhr){
if(xhr.status == 200){
$$('grid_preinvoices').remove(id)
msg_sucess('PreFactura eliminada correctamente')
}else{
msg_error('No se pudo eliminar')
}
})
}
function cmd_delete_preinvoice_click(id, e, node){
var grid = $$('grid_preinvoices')
if(grid.count() == 0){
return
}
var row = grid.getSelectedItem()
if (row == undefined){
msg_error('Selecciona una prefactura')
return
}
var msg = '¿Estás seguro de eliminar la siguiente PREFactura?<BR><BR>'
msg += '(' + row['folio'] + ') ' + row['cliente']
msg += '<BR><BR>ESTA ACCIÓN NO SE PUEDE DESHACER'
webix.confirm({
title:'Eliminar Pre Factura',
ok:'Si',
cancel:'No',
type:'confirm-error',
text:msg,
callback:function(result){
if (result){
delete_preinvoice(row['id'])
}
}
})
}
2017-11-05 21:23:37 -06:00
function agregar_preproducto(values){
var taxes = values.taxes
var values = values.row
var form = $$('form_invoice')
var row = grid.getItem(values.id)
values['delete'] = '-'
if (row == undefined){
grid.add(values)
} else {
values['cantidad'] = parseFloat(row.cantidad) + parseFloat(values['cantidad'])
values['importe'] = values['valor_unitario'] * values['cantidad']
grid.updateItem(row.id, values)
}
for(var v of taxes){
var pt = table_pt.findOne(v)
if(pt === null){
table_pt.insert(v)
}
}
}
2017-11-05 00:13:48 -06:00
function refacturar_preinvoice(id){
2017-11-05 21:23:37 -06:00
webix.ajax().get('/values/preproductos', {'id': id}, {
error: function(text, data, xhr) {
msg_error('Error al consultar')
},
success: function(text, data, xhr){
var values = data.json()
set_client(values.receptor)
for(var p of values.rows){
agregar_preproducto(p)
}
calculate_taxes()
$$('tv_invoice').getTabbar().setValue('Generar')
}
})
2017-11-05 00:13:48 -06:00
}
function cmd_facturar_preinvoice_click(id, e, node){
var grid = $$('grid_preinvoices')
if(grid.count() == 0){
return
}
var row = grid.getSelectedItem()
if (row == undefined){
msg_error('Selecciona una prefactura')
return
}
var msg = '¿Estás seguro de facturar la siguiente PREFactura?<BR><BR>'
msg += '(' + row['folio'] + ') ' + row['cliente']
webix.confirm({
title: 'Generar Factura',
ok: 'Si',
cancel: 'No',
type: 'confirm-error',
text: msg,
callback:function(result){
if (result){
refacturar_preinvoice(row['id'])
}
}
})
}
2017-11-05 19:53:27 -06:00
function enviar_prefactura(id){
msg = '¿Estás seguro de enviar por correo esta prefactura?'
webix.confirm({
title: 'Enviar Factura',
ok: 'Si',
cancel: 'No',
type: 'confirm-error',
text: msg,
callback:function(result){
if(result){
webix.ajax().post('/values/enviarprefac', {'id': 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)
}
}
})
}
}
})
}
function grid_preinvoices_click(id, e, node){
var row = this.getItem(id)
if(id.column == 'pdf'){
location = '/doc/pdf2/' + row.id
}else if(id.column == 'email'){
enviar_prefactura(row.id)
}
}
2017-11-06 23:34:43 -06:00
2017-11-08 23:47:15 -06:00
function get_facturas_por_cliente(){
var values = $$('form_invoice').getValues()
var id = values.id_partner
var y = $$('filter_cfdi_year').getValue()
var m = $$('filter_cfdi_month').getValue()
var ids = []
var rows = $$('grid_relacionados').data.getRange()
for (i = 0; i < rows.length; i++) {
ids.push(rows[i]['id'])
}
filters = {
'year': y,
'month': m,
'id_cliente': id,
'cfdis': ids,
2017-11-12 18:50:41 -06:00
'anticipo': $$('chk_relacionados_anticipo').getValue(),
2017-11-08 23:47:15 -06:00
'folio': $$('filter_cfdi_folio').getValue(),
'uuid': $$('filter_cfdi_uuid').getValue(),
'opt': 'relacionados'
}
var grid = $$('grid_cfdi_cliente')
webix.ajax().get('/invoices', filters, {
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');
};
}
})
}
function get_info_cfdi_relacionados(){
webix.ajax().get('/values/tiporelacion', {key: true}, function(text, data){
var values = data.json()
$$('lst_tipo_relacion').getList().parse(values)
$$('lst_tipo_relacion').setValue(tipo_relacion)
})
query = table_relaciones.chain().data()
$$('grid_relacionados').parse(query)
get_facturas_por_cliente()
}
2017-11-06 23:34:43 -06:00
function cmd_cfdi_relacionados_click(){
2017-11-08 23:47:15 -06:00
var d = new Date()
ui_invoice.init()
var fy = $$('filter_cfdi_year')
var fm = $$('filter_cfdi_month')
fy.blockEvent()
fm.blockEvent()
$$('lbl_cfdi_cliente').setValue($$('lbl_client').getValue())
data = $$('filter_year').getList().data
fy.getList().data.sync(data)
fy.setValue(d.getFullYear())
fm.setValue(d.getMonth() + 1)
fy.unblockEvent()
fm.unblockEvent()
get_info_cfdi_relacionados()
$$('win_cfdi_relacionados').show()
2017-11-06 23:34:43 -06:00
}
2017-11-08 23:47:15 -06:00
function cmd_limpiar_relacionados_click(){
msg = '¿Estás seguro de quitar todas las relaciones<BR><BR>'
msg += 'ESTA ACCION NO SE PUEDE DESHACER'
webix.confirm({
title: 'Limpiar relaciones',
ok: 'Si',
cancel: 'No',
type: 'confirm-error',
text: msg,
callback:function(result){
if(result){
$$('lst_tipo_relacion').setValue('')
$$('grid_relacionados').clearAll()
table_relaciones.clear()
tipo_relacion = ''
msg_sucess('Las relaciones han sido eliminadas')
}
}
})
}
function cmd_guardar_relacionados_click(){
var grid = $$('grid_relacionados')
var value = $$('lst_tipo_relacion').getValue()
if(value == '' || value == '-'){
msg_error('Selecciona el tipo de relación')
return
}
if(grid.count() == 0){
msg_error('Agrega al menos un CFDI a relacionar')
return
}
var data = grid.data.getRange()
table_relaciones.clear()
table_relaciones.insert(data)
tipo_relacion = value
msg_sucess('Relaciones guardadas correctamente')
}
function cmd_filter_relacionados_click(){
get_facturas_por_cliente()
}
function filter_cfdi_year_change(nv, ov){
cmd_filter_relacionados_click()
}
function filter_cfdi_month_change(nv, ov){
cmd_filter_relacionados_click()
}
2017-11-12 18:50:41 -06:00
function lst_tipo_relacion_change(nv, ov){
$$('chk_relacionados_anticipo').setValue(0)
$$('chk_relacionados_anticipo').disable()
if(nv=='07'){
$$('chk_relacionados_anticipo').enable()
$$('chk_relacionados_anticipo').setValue(1)
cmd_filter_relacionados_click()
}
}
2017-11-12 21:56:45 -06:00
function lst_serie_change(nv, ov){
show(nv)
}