I have the following the following result-set brought from the query that is described in the code below. What the php code does is to run the result-set by filtering two flag variables: source and destination . Then, during the iteration for each source and destination found, compare the records in the fields: amount and format_soft if it is greater than 1 and the second empty field. The record is counted as a sold ticket, otherwise, if it is less than 0 or with a negative number, it is counted as canceled. If you no longer find records with the same origin and destination: do subtraction sold - canceled = net. Both in amount and tickets. And print those calculated values corresponding to each origin and destination. Before closing the while loop the flags take the following value found from: source and destination.
SELECT Area_de_Venta, Clave_Area, FContable, Importe, Formato_Salto, Tipo, Corrida, Origen, Destino FROM db_ventas.tb_vtas WHERE Empresa_Corrida <> '' and FContable between '$fecha_in_A' and '$fecha_fn_A' and Area_de_Venta = '$a_venta' order by Area_de_Venta, Origen, Destino, Importe, FContable
while ( $rowA = $sqlA->fetch_assoc() ) { //bucle para recorrer array asociativo de A
//COMPARANDO BANDERAS DE A
if ( ($flag_origenA == $rowA['Origen']) && ($flag_destinoA == $rowA['Destino']) ) {
if ( ($rowA['Importe'] > 1) && ($rowA['Formato_Salto'] == '') ) { //SI SON BOLETOS VENDIDOS
$importe_fila = $rowA['Importe']; //extrayendo el importe de cada fila
$suma = $suma + $importe_fila;
//$rsuma = floor($suma * 10) /10; //redondeo
$contador_vendido++;
} elseif ( ($rowA['Importe'] <= 0 ) && ($rowA['Formato_Salto'] == '') ) { //SI SON CANCELADOS
$importe_cancel = $rowA['Importe'];
$importe_positivo = $importe_cancel * -1; //conversión a positivo
$suma_cancel = $suma_cancel + $importe_positivo;
//$rsuma_cancel = floor($suma_cancel * 10)/10;
$contador_cancel++;
}
$contador_neto = $contador_vendido - $contador_cancel;
$imp_neto = $suma - $suma_cancel;
$a_venta = $rowA['Area_de_Venta'];
$origen = $rowA['Origen'];
$destino = $rowA['Destino'];
// } // cierre del if de: año y mes
} else { //else de BANDERAS: ORIGEN Y DESTINO
<tr>
<td><?php echo $a_venta; ?></td>
<td><?php echo $contador_neto; ?></td>
<td><?php echo $imp_neto; ?></td>
<td><?php echo $origen; ?></td>
<td><?php echo $destino; ?></td>
<td><?php echo $contador_neto; ?></td>
</tr>
// reinician contadores
$contador_vendido = 0;
$suma = 0;
$rsuma = 0;
$contador_cancel = 0;
$suma_cancel = 0;
$rsuma_cancel = 0;
$contador_neto = 0;
$imp_neto = 0;
if ( ($rowA['Importe'] > 1) && ($rowA['Formato_Salto'] == '') ) { //SI SON BOLETOS VENDIDOS
$importe_fila = $rowA['Importe']; //extrayendo el importe de cada fila correspondiente a un area_venta
$suma = $suma + $importe_fila;
//$rsuma = floor($suma * 10) /10; //redondeo
$contador_vendido++;
} elseif ( ($rowA['Importe'] <= 0 ) && ($rowA['Formato_Salto'] == '') ) {
$importe_cancel = $rowA['Importe'];
$importe_positivo = $importe_cancel * -1; //conversión a positivo
$suma_cancel = $suma_cancel + $importe_positivo;
//$suma_cancel = floor($suma_cancel * 10)/10;
$contador_cancel++;
}
$contador_neto = $contador_vendido - $contador_cancel;
$imp_neto = $suma - $suma_cancel;
$a_venta = $rowA['Area_de_Venta'];
$origen = $rowA['Origen'];
$destino = $rowA['Destino'];
} // SI NO ES EL PRIMER REGISTRO
//FLAGS TOMAN EL VALOR DE LA SIGUENTE FILA, RECORRIDA EN EL BUCLE
$flag_origenA = $rowA['Origen'];
$flag_destinoA = $rowA['Destino'];
} //while A
<tr>
<td><?php echo $a_venta; ?></td>
<td><?php echo $contador_neto; ?></td>
<td><?php echo $imp_neto; ?></td>
<td><?php echo $origen; ?></td>
<td><?php echo $destino; ?></td>
<td><?php echo $contador_neto; ?></td>
</tr>
As an output, I get the following print in table:
This goes well, giving as entry a sales_ area, and two dates (initial and final). Now what I can not do is give another pair of dates (initial and final). Calculate the same as in the previous process and print a comparative of amount_net and tickets_net between: range_of_date_A and date_date_of_B: And have something like this: output 0 in the columns where there are no records of amounts and tickets. I was trying another nested while loop, but I could not make the connection. Is it possible to enter another loop and in between make the comparison to print the corresponding differences? Or there would be another better way to do it.
And have something like that in the table, (with blue the results of a range of Dates A and with green the results of a range of Dates B):