Generate multiple records in Mysql

Asked

Viewed 181 times

0

In the Mysql have two tables:

  • CUSTOMERS

    | codigo-cliente | nome | grupo | valor |
    |----------------|------|-------|-------|
    
  • FINANCIAL

    | codigo-cliente | codigo-boleto | grupo | valor |
    |----------------|---------------|-------|-------|
    

On the page generate boletos, I have a field where I select for which group of customers I want to generate the boletos.

When clicking to generate, I need the system to generate in the FINANCIAL table a ticket for each client of the CLIENT table that is part of the selected group.

Example:

I selected group 10 and had it generated

CLIENT TABLE

| codigo-cliente | nome   | grupo | valor |
|----------------|--------|-------|-------|
| 1              | teste1 | 10    | 100   |
| 2              | teste2 | 12    | 60    |
| 3              | teste3 | 10    | 30    |

In the FINANCIAL table will register a ticket for each client who is in group 10.

FINANCIAL SCHEDULE

| codigo-cliente | codigo-boleto | grupo | valor |
|----------------|---------------|-------|-------|
| 1              | 200           | 10    | 100   |
| 3              | 201           | 10    | 30    |
  • If possible post the code you have tried.

  • You did not inform where the information comes from for the "code-billet"

  • Pagotti, the "codigo-boleto" is automatically generated by Mysql.

  • Valdeir Psr, I couldn’t find the right logic, so I don’t have the code.

2 answers

1


As we have the post we received from the form, with the grupo, we’ll do the following:

// Seleciono tudo que envolve o grupo = 10.
$this->db->where('grupo', $this->input->post('grupo'));
$consulta = $this->db->get('clientes')->result();

// Com base nisso, faço o laço de repetição, para que eu insira no banco de dados, todos os boletos gerados com grupo = 10
foreach($consulta as $valor){
    $dados['codigo-boleto'] = '200';
    $dados['grupo'] = $valor->grupo;
    $dados['codigo-cliente'] = $valor->codigo-cliente;
    $dados['valor'] = '100'; 
    $this->db->insert('financeiro', $dados);
}

This way you enter all boletos, according to the selected group. However, it should be noted that you do not have any information coming via post, the value and code of the boleto.

  • Andre, unfortunately it didn’t work. I don’t know if I did something wrong besides what you gave me...I’ll update the question with the code used;

0

Follows the code used:

VIEW Has the form and ajax code:

<script>
$("#frmCobrancas").submit(function(e){
    e.preventDefault();
    var base_url = "<?php echo base_url() ?>";

    $.ajax({
        url : base_url+'financeiro/gerar_cobrancas',
        type: 'POST',
        dataType: 'html',
        data: {
            'rota_cobranca': $('#cRotacobranca').val(),
            'data_emissao': $('#cDataemissao').val(),
            'mes_referencia': $('#cMesreferencia').val(),
            'data_vencimento': $('#cDatavencimento').val()

        },success:function(data){
            console.log(data);
        }

    });
});

CONTROLLER

public function gerar_cobrancas() {
    if ($this->input->post('rota_cobranca') == ""){
        echo 0;
    }else{

        $data = array(
            'rota_cobranca'     => $this->input->post('rota_cobranca'),
            'data_emissao'      => FormatarDataUS($this->input->post('data_emissao')),
            'mes_referencia'    => FormatarDataUS($this->input->post('mes_referencia')),
            'data_vencimento'   => FormatarDataUS($this->input->post('data_vencimento')),               
        );

        $query = $this->financeiro_model->set_GerarCobrancas($data);

    }
}

MODEL

public function set_GerarCobrancas($data){

    $consulta = $this->db
            ->where('rota_cobranca', $data['rota_cobranca'])
            ->where('vinculo', 'A')
            ->get('tb_clientes')->result();


    // Com base nisso, faço o laço de repetição, para que eu insira no banco de dados, todos os boletos gerados com grupo = 10
    foreach($consulta as $valor){

        $boleto['codigo-boleto'] = ''; //AUTO_INCREMENT
        $boleto['codigo_cliente'] = $valor->matricula;
        $boleto['nosso_numero'] = '0'; //SEMPRE SERÁ "0"
        $boleto['mes_referencia'] = $data['mes_referencia']; 
        $boleto['data_emissao'] = $data['data_emissao']; 
        $boleto['data_vencimento'] = $data['data_vencimento']; 
        $boleto['data_pagamento'] = "0000-00-00"; //SEMPRE SERÁ 0000-00-00
        $boleto['valor'] = $valor->mensalidade;
        $boleto['valor_pago'] = 0; //SEMPRE SERÁ "0"
        $boleto['situacao'] = 0; //SEMPRE SERÁ "0"
        $boleto['rota_cobranca'] = $valor->rota_cobranca;
        $this->db->insert('tb_historico_financeiro', $boleto);
    }

    return;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.