Created
September 2, 2020 19:11
-
-
Save davidchc/952efd793fc2729f4cb0b348e9402aa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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