218 lines
6.0 KiB
PHP
218 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use CodeIgniter\Model;
|
||
|
||
class Products extends Model
|
||
{
|
||
/**
|
||
* table products
|
||
* @var string
|
||
*/
|
||
protected $table = 'products';
|
||
protected $primaryKey = 'id';
|
||
protected $allowedFields = ['name', 'sku', 'price', 'product_sold', 'qty', 'image', 'description', 'numero_de_moteur', 'marque', 'chasis', 'store_id', 'availability', 'is_piece', 'prix_vente', 'date_arivage', 'puissance', 'cler', 'categorie_id', 'etats','type', 'infoManquekit', 'info', 'infoManque'];
|
||
|
||
/**
|
||
* get the brand data
|
||
* @param int $id
|
||
* @return array|object|null
|
||
*/
|
||
public function getProductData(int $id = null)
|
||
{
|
||
if ($id) {
|
||
|
||
return $this->where('id', $id)->first();
|
||
}
|
||
|
||
return $this->where([
|
||
'is_piece' => 0,
|
||
'product_sold' => 0
|
||
])->orderBy('id', 'DESC')->findAll();
|
||
}
|
||
|
||
|
||
public function getProductData2(int $id)
|
||
{
|
||
$builder = $this->where('is_piece', 0)
|
||
->where('product_sold', 0)
|
||
->where('store_id', $id);
|
||
|
||
// if ($id != 0) {
|
||
// $builder = $builder->where('store_id', $id);
|
||
// }
|
||
|
||
return $builder->join('brands', 'brands.id = products.marque')
|
||
->orderBy('products.id', 'DESC')
|
||
->select('brands.name as brand_name,COUNT( products.id) as total_product, products.store_id as store_id,products.*')
|
||
->groupBy('store_id')
|
||
->findAll();
|
||
}
|
||
|
||
|
||
|
||
public function getProductData3(int $id)
|
||
{
|
||
if ($id == 0) {
|
||
return $this->where('is_piece', 0)->orderBy('id', 'DESC')->findAll();
|
||
}
|
||
|
||
// Fetch all products, ordered by ID descending
|
||
return $this->where('is_piece', 0)->where('product_sold', 0)->where('store_id', $id)->orderBy('id', 'DESC')->findAll();
|
||
}
|
||
|
||
public function getProductDataStore(int $store_id, bool $excludeAvance = true, int $currentProductId = null)
|
||
{
|
||
$builder = $this->where('is_piece', 0)
|
||
->where('product_sold', 0)
|
||
->where('availability', 1)
|
||
->where('store_id', $store_id);
|
||
|
||
if ($excludeAvance) {
|
||
$db = \Config\Database::connect();
|
||
$subQuery = $db->table('avances')
|
||
->select('product_id')
|
||
->where('active', 1)
|
||
->where('is_order', 0)
|
||
->getCompiledSelect();
|
||
|
||
$builder->where("id NOT IN ($subQuery)", null, false);
|
||
}
|
||
|
||
// Si on modifie et qu'on veut inclure le produit actuel dans la liste
|
||
if ($currentProductId) {
|
||
$builder->orWhere('id', $currentProductId);
|
||
}
|
||
|
||
return $builder->orderBy('id', 'DESC')->findAll();
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Get active products (availability = 1)
|
||
* @return array
|
||
*/
|
||
public function getActiveProductData()
|
||
{
|
||
return $this->where('is_piece', 0)->orderBy('id', 'DESC')->findAll();
|
||
}
|
||
|
||
/**
|
||
* Assigner un utilisateur à un magasin
|
||
*
|
||
* @param int|null $productid ID de l'utilisateur
|
||
* @param int|null $storeid ID du magasin
|
||
* @return bool Résultat de l'opération (true si success, false sinon)
|
||
*/
|
||
public function assignToStore($productid = null, $storeid = null)
|
||
{
|
||
// Vérifie si l'utilisateur et le magasin sont fournis
|
||
if (!is_null($productid) && !is_null($storeid)) {
|
||
// Mise à jour du champ store_id pour l'utilisateur spécifié
|
||
$this->db->table('products')
|
||
->where('id', $productid)
|
||
->update(['store_id' => $storeid]);
|
||
|
||
return true;
|
||
}
|
||
|
||
// Si $userid ou $storeid est null, l'opération échoue
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* create new product
|
||
* @param array $data
|
||
* @return bool
|
||
*/
|
||
public function create(array $data)
|
||
{
|
||
return $this->insert($data) ? true : false;
|
||
}
|
||
|
||
/**
|
||
* update existing product
|
||
* @param array $data
|
||
* @param int $id
|
||
* @return bool
|
||
*/
|
||
public function updateProduct(array $data, int $id)
|
||
{
|
||
return $this->update($id, $data) ? true : false;
|
||
}
|
||
|
||
/**
|
||
* remove existing product
|
||
* @param int $id
|
||
* @return bool
|
||
*/
|
||
public function remove(int $id)
|
||
{
|
||
return $this->delete($id) ? true : false;
|
||
}
|
||
|
||
public function countTotalProducts()
|
||
{
|
||
$db = \Config\Database::connect();
|
||
|
||
// Sous-requête pour obtenir les product_id dans avances actives
|
||
$subQuery = $db->table('avances')
|
||
->select('product_id')
|
||
->where('active', 1)
|
||
->where('is_order', 0)
|
||
->getCompiledSelect();
|
||
|
||
// Compter les produits disponibles
|
||
return $this->where('is_piece', 0)
|
||
->where('product_sold', 0)
|
||
->where("id NOT IN ($subQuery)", null, false)
|
||
->countAllResults();
|
||
}
|
||
|
||
/**
|
||
* count all products including sold and reserved (méthode originale si besoin)
|
||
*/
|
||
public function countAllProductsIncludingSold()
|
||
{
|
||
return $this->countAll();
|
||
}
|
||
|
||
public function getTotalProductPriceByIds(array $productIds)
|
||
{
|
||
try {
|
||
$total = 0.0;
|
||
|
||
foreach ($productIds as $id) {
|
||
// Récupère le prix du produit courant
|
||
$row = $this->select('price')
|
||
->where('id', $id)
|
||
->first();
|
||
|
||
if ($row && isset($row['price'])) {
|
||
$total += (float) $row['price'];
|
||
}
|
||
}
|
||
|
||
return $total;
|
||
} catch (\Throwable $th) {
|
||
// Loger l’erreur ici si besoin : log_message('error', $th->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function getProductNameById(int $id): ?string
|
||
{
|
||
$product = $this->where('id', $id)->first();
|
||
|
||
if ($product && isset($product['name'])) {
|
||
return $product['name']; // ou un autre champ selon le vrai nom
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
|
||
|
||
} |