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

311 lines
7.9 KiB
JavaScript
Raw Normal View History

2017-10-04 23:22:05 -05:00
var query = []
var grid = null
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)
if(pre.usar_con){
$$('lst_tipo_comprobante').setValue(pre.usar_con)
$$('lst_tipo_comprobante').config.readonly = true
$$('lst_tipo_comprobante').refresh()
}
})
}
function get_forma_pago(){
webix.ajax().get('/values/formapago', function(text, data){
var values = data.json()
pre = values[0]
$$('lst_forma_pago').getList().parse(values)
$$('lst_forma_pago').setValue(pre.id)
})
}
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)
})
}
function get_uso_cfdi(){
webix.ajax().get('/values/usocfdi', function(text, data){
var values = data.json()
pre = values[0]
table_usocfdi.clear()
table_usocfdi.insert(values)
$$('lst_uso_cfdi').getList().parse(values)
$$('lst_uso_cfdi').setValue(pre.id)
})
}
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()
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-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-10-05 15:33:25 -05:00
form.focus('search_client_id')
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")
};
function cmd_delete_invoice_click(id, e, node){
webix.message({type:"success", text: "OK Delete"});
};
2017-10-04 23:22:05 -05:00
function cmd_timbrar_click(id, e, node){
var form = this.getFormView()
2017-06-27 15:43:02 -05:00
if (!form.validate()) {
2017-10-04 23:22:05 -05:00
webix.message({type: 'error', text: 'Valores inválidos'})
2017-06-27 15:43:02 -05:00
return
}
$$('form_invoice').clear();
2017-10-04 23:22:05 -05:00
$$('multi_invoices').setValue('invoices_home')
webix.message({type:"success", text: "Factura guardada correctamente"})
}
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-05 15:33:25 -05:00
id_partner:row.id, search_client_id:'', search_client_name:''}, 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 + ')'
total_tax = (tax.tasa * t.importe).round(2)
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)){
total_tax = (tax.tasa * total_iva * -1).round(2)
concepto = tipo + tax.name + ' (2/3)'
}else{
total_tax = (tax.tasa * t.importe * -1).round(2)
}
grid_totals.add({id: id, concepto: concepto, importe: total_tax})
id += 1
}
row = {importe: subtotal}
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
function grid_details_click(id, e, node){
if(id.column != 'delete'){
return
}
grid.remove(id.row)
calculate_taxes()
}