I'm trying to make an expense calculation on the system but I'm not having success. The system works as follows, the User makes a request involving 1 or more products, these requests are stored in a table. Each Product registered in the system is stored in another table, having the standard fields, in stock, unit cost, already sent and pending. The User has two tables, one for the Branch that he manages and the other is the users' account.
The problem is, having the order value for each branch. That is, add up how many product items have been shipped to the affiliate and then multiply by the cost of it, and finally add up the entire result to see the total affiliate spend at the moment.
I tried using while
but it displayed several times the same product in different amounts for the same branch.
LEGEND
branches = table where all affiliated branches are located;
stationery_itens = table where the products are;
stationery_solicitations = table where orders are placed;
stationery_solicited = table where the products requested are in the order;
qnt_enviada = Quantity Sent;
cost = Unit Cost per product;
chns = Unique code for each data entered in the system, it works as a protocol number.
PHP I'm using the PDO for connection
$procura_filial = $site->query("SELECT * FROM filiais ORDER BY codigo ASC");
while ($filiais = $procura_filial->fetch(PDO::FETCH_OBJ)) {
$procura_solicitacoes = $site->query("SELECT * FROM papelaria_solicitacoes WHERE filial = '$filiais->chns'");
if ($procura_solicitacoes->rowCount() <= 0) {
}else {
while($solicitacoes = $procura_solicitacoes->fetch(PDO::FETCH_OBJ)){
$procura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE chns = '$solicitacoes->chns'");
while ($solicitados = $procura_solicitados->fetch(PDO::FETCH_OBJ)) {
$reprocura_solicitados = $site->query("SELECT * FROM papelaria_solicitados WHERE filial = '$solicitados->filial' and produtos = '$solicitados->produtos'");
//SOMA QUANTIDADE ENVIADA DE CADA PRODUTO
while($soma_qnt = $reprocura_solicitados->fetch(PDO::FETCH_OBJ)){
$val1 = $soma_qnt->qnt_enviada;
$total_cont += $val1;
$total_produto_env = $total_cont;
}
}
}
}
echo "Filial: ".$soma_qnt->filial." Produto: ".$soma_qnt->produtos." Enviados: ".$total_produto_env."<br>";
}