Skip to content

Instantly share code, notes, and snippets.

@davidchc
Created September 2, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidchc/952efd793fc2729f4cb0b348e9402aa1 to your computer and use it in GitHub Desktop.
Save davidchc/952efd793fc2729f4cb0b348e9402aa1 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models;
class StockProduct
{
private $pdo;
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
/*Dá baixa no estoque*/
public function lowStocks(array $products = [])
{
foreach($products as $product){
try{
$this->pdo->beginTransaction();
$this->decreaseQuantity($product['product_id'], 1);
$this->pdo->commit();
}catch(\Exception $e){
$this->pdo->rollback();
}
}
}
//Faz o descremento, se der error na excução dá exception
public function decreaseQuantity($product_id, $quantity)
{
$stmt = $this->pdo->prepare("UPDATE products SET quantity -= :quantity WHERE id = :id ");
$stmt->bindValue(':quantity', $quantity);
$stmt->bindValue(':id', $product_id);
if($stmt->execute()) return true;
throw new \Exception("Erro ao executar a baixar de estoque");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment