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(){ webix.ajax().sync().get('/values/taxes', function(text, data){ var values = data.json() table_taxes.clear() table_taxes.insert(values) }) get_series() get_forma_pago() get_monedas() get_uso_cfdi() table_pt.clear() table_totals.clear() } function cmd_new_invoice_click(id, e, node){ var form = $$('form_invoice') var grid_totals = $$('grid_totals') grid = $$('grid_details') default_config() 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}) $$('multi_invoices').setValue('invoices_new') form.focus('search_client_id') } 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"}); }; function cmd_timbrar_click(id, e, node){ var form = this.getFormView() if (!form.validate()) { webix.message({type: 'error', text: 'Valores inválidos'}) return } $$('form_invoice').clear(); $$('multi_invoices').setValue('invoices_home') webix.message({type:"success", text: "Factura guardada correctamente"}) } function cmd_close_invoice_click(id, e, node){ $$('multi_invoices').setValue('invoices_home') } function search_client_by_id(id){ var msg = '' webix.ajax().get('/values/client', {'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_client(values.row) }else{ msg = 'No se encontró un cliente con la clave: ' + id webix.message({type:'error', text: msg}) } } }) } function set_client(row){ var form = $$('form_invoice') var html = '' form.setValues({ id_partner:row.id, search_client_id:'', search_client_name:''}, true) html += row.nombre + ' (' + row.rfc + ')' $$('lbl_client').setValue(html) form.focus('search_product_id') } function grid_clients_found_click(obj){ set_client(obj) } function search_client_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_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) } } } function grid_details_click(id, e, node){ if(id.column != 'delete'){ return } grid.remove(id.row) calculate_taxes() }