How to make the fields in a table fill by default with values from another table?

1

This is my billing table

db.define_table('facturacion',
                Field('numero_comprobante', 'string'),
                Field('numero_cotizacion', 'reference cotizaciones'),
                Field('cliente', 'reference clientes'),
                Field('marca_gg', 'reference marcas_definiciones'),
                Field('contacto_cliente', 'reference contactos'),
                Field('razon_social_gg', 'reference razones_zociales_gg'),
                Field('fecha_facturacion', 'date'),
                Field('estado_facturacion', 'reference estados_facturacion'),
                Field('tipo_comprobante', 'reference tipos_comprobantes'),
                Field('moneda', 'reference monedas'),
                Field('razon_social_nombre_completo_cliente', 'string'),
                Field('ruc_dni_ce_cliente', 'string'),
                Field('direccion_fiscal_cliente', 'string'),
                Field('detraccion', 'boolean'),
                Field('monto_detraccion'),
                Field('fecha_recepcion', 'date'),
                Field('sub_total', 'float'),
                Field('descuento', 'float'),
                Field('sub_total_venta', 'float'),
                Field('impuesto_igv', 'float'),
                Field('total_venta', 'float'),
                Field('nota', 'text'),
                format='%(numero_comprobante)s')

and this is my clients table

define_table('clientes',
                Field('cliente', 'string'),
                Field('creacion', 'date'),  # , default=now, writable=False
                Field('tipo_cliente', 'reference tipos_clientes'),
                Field('razon_social', 'string'),
                Field('tipo_doc_id', 'string'),
                Field('direccion_fiscal_cliente'),
                Field('fecha_aniversario', 'date'),
                Field('area_negocio', 'reference areas_negocios'),
                Field('estado_cliente', 'reference estados_clientes'),
                Field('medio_captacion', 'reference medios_captacion'),
                #Field('direccion_principal', 'string'),
                Field('telefono_contacto', 'string'),
                Field('email_contacto', 'string'),
                Field('url', 'string'),
                Field('nota', 'text'),
                format='%(cliente)s')

The problem is that when you select a customer the invoicing table ...

The fields of the invoicing table that are of clients must be filled in automatically, in other words when selecting a customer in invoicing, some fields must be filled or appear automatically.

    
asked by Fernando M. Goycochea 07.01.2016 в 22:04
source

1 answer

4

Here you have to use AJAX because the functionality you want to have will run once the site is already in the client's browser.

In your controller you must create a method that will be executed every time you select a new client, and this method will return a new HTML Helper SELECT that you are going to build, and this new select will replace the select that is currently being displayed on the site.

Now, since the AJAX function receives only one target as a parameter, it would be ideal if this method is parameterized and, depending on the parameter, returns the new select you want.

Finally in your HTML, where you show your form, you must add a JavaScript function.

For example:

<script type='text/javascript'>
function onChangeCliente(){
    eval("ajax('{{=URL('default', 'metodoControlador', vars=dict(nuevoSelect='razonSocial'))}}', ['cliente'], 'formulario_razonSocial')");
    eval("ajax('{{=URL('default', 'metodoControlador', vars=dict(nuevoSelect='dni'))}}', ['cliente'], 'formulario_dni')");
    eval("ajax('{{=URL('default', 'metodoControlador', vars=dict(nuevoSelect='direccionFiscal'))}}', ['cliente'], 'formulario_direccionFiscal')");
}

Where ['cliente'] is the attribute name of the select of clients, and 'formulario_razonSocial' , 'formulario_dni' and 'forulario_direccionFiscal' are the attributes id of the selects target that will change each time you choose a new customer.

To obtain these names use Firebug or the tools for developers that are included in Google Chrome.

In the controller you should only create the following method:

def metodoControlador(nuevoSelect):
    cliente = db(db.clientes.id == request.vars.cliente).select().first()
    if nuevoSelect == 'razonSocial':
        return SELECT([OPTION(cliente.razon_social)])
    elif nuevoSelect == 'dni':
        . . .
        . . .
        . . .

And, smart, the selects would change every time you change clients.

    
answered by 19.10.2016 / 01:23
source