Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions application/controllers/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ private function devolucaoEstoque($id)
if ($produtos = $this->os_model->getProdutos($id)) {
$this->load->model('produtos_model');
if ($this->data['configuration']['control_estoque']) {
$this->produtos_model->updateEstoqueBatch($produtos, '+');
foreach ($produtos as $p) {
$this->produtos_model->updateEstoque($p->produtos_id, $p->quantidade, '+');
log_info('ESTOQUE: Produto id ' . $p->produtos_id . ' voltou ao estoque. Quantidade: ' . $p->quantidade . '. Motivo: Cancelamento/Exclusão');
}
Comment on lines 573 to 577
}
Expand All @@ -584,8 +584,8 @@ private function debitarEstoque($id)
if ($produtos = $this->os_model->getProdutos($id)) {
$this->load->model('produtos_model');
if ($this->data['configuration']['control_estoque']) {
$this->produtos_model->updateEstoqueBatch($produtos, '-');
foreach ($produtos as $p) {
$this->produtos_model->updateEstoque($p->produtos_id, $p->quantidade, '-');
log_info('ESTOQUE: Produto id ' . $p->produtos_id . ' baixa do estoque. Quantidade: ' . $p->quantidade . '. Motivo: Mudou status que já estava Cancelado para outro');
}
}
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/api/v1/OsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,8 @@ private function devolucaoEstoque($id)
if ($produtos = $this->os_model->getProdutos($id)) {
$this->load->model('produtos_model');
if ($this->getConfig('control_estoque')) {
$this->produtos_model->updateEstoqueBatch($produtos, '+');
foreach ($produtos as $p) {
$this->produtos_model->updateEstoque($p->produtos_id, $p->quantidade, '+');
log_info('ESTOQUE: Produto id ' . $p->produtos_id . ' voltou ao estoque. Quantidade: ' . $p->quantidade . '. Motivo: Cancelamento/Exclusão');
}
Comment on lines 1046 to 1050
}
Expand All @@ -1057,8 +1057,8 @@ private function debitarEstoque($id)
if ($produtos = $this->os_model->getProdutos($id)) {
$this->load->model('produtos_model');
if ($this->getConfig('control_estoque')) {
$this->produtos_model->updateEstoqueBatch($produtos, '-');
foreach ($produtos as $p) {
$this->produtos_model->updateEstoque($p->produtos_id, $p->quantidade, '-');
log_info('ESTOQUE: Produto id ' . $p->produtos_id . ' baixa do estoque. Quantidade: ' . $p->quantidade . '. Motivo: Mudou status que já estava Cancelado para outro');
}
}
Expand Down
43 changes: 43 additions & 0 deletions application/models/Produtos_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,47 @@ public function updateEstoque($produto, $quantidade, $operacao = '-')

return $this->db->query($sql, [$quantidade, $produto]);
}

public function updateEstoqueBatch($produtos, $operacao = '-')
{
Comment on lines 78 to +82
if (empty($produtos)) {
return true;
}

$aggregated = [];

foreach ($produtos as $p) {
$id = isset($p->produtos_id) ? $p->produtos_id : (isset($p['produtos_id']) ? $p['produtos_id'] : null);
$qtd = isset($p->quantidade) ? $p->quantidade : (isset($p['quantidade']) ? $p['quantidade'] : null);

if ($id === null || $qtd === null) {
continue;
}

if (!isset($aggregated[$id])) {
$aggregated[$id] = 0;
}
$aggregated[$id] += $qtd;
}

if (empty($aggregated)) {
return true;
}

$ids = [];
$cases = [];
$params = [];

foreach ($aggregated as $id => $qtd) {
$ids[] = $id;
$cases[] = "WHEN idProdutos = ? THEN estoque $operacao ?";
$params[] = $id;
$params[] = $qtd;
}

$sql = "UPDATE produtos SET estoque = CASE " . implode(" ", $cases) . " ELSE estoque END WHERE idProdutos IN (" . implode(",", array_fill(0, count($ids), "?")) . ")";
$params = array_merge($params, $ids);

return $this->db->query($sql, $params);
}
}