36 |
|
|
37 |
class Product |
class Product |
38 |
{ |
{ |
39 |
var $db ; |
var $db ; |
40 |
|
|
41 |
var $id ; |
var $id ; |
42 |
var $ref; |
var $ref; |
43 |
var $libelle; |
var $libelle; |
44 |
var $description; |
var $description; |
45 |
var $price; |
var $price; |
46 |
var $tva_tx; |
var $tva_tx; |
47 |
var $type; |
var $type; |
48 |
var $seuil_stock_alerte; |
var $seuil_stock_alerte; |
49 |
var $duration_value; |
var $duration_value; |
50 |
var $duration_unit; |
var $duration_unit; |
51 |
|
|
52 |
|
var $stats_propale=array(); |
53 |
|
var $stats_commande=array(); |
54 |
|
var $stats_contrat=array(); |
55 |
|
var $stats_facture=array(); |
56 |
|
|
57 |
|
|
58 |
/** |
/** |
59 |
* \brief Constructeur de la classe |
* \brief Constructeur de la classe |
60 |
* \param DB handler accès base de données |
* \param DB Handler accès base de données |
61 |
* \param id id produit (0 par defaut) |
* \param id Id produit (0 par defaut) |
62 |
*/ |
*/ |
|
|
|
63 |
function Product($DB, $id=0) |
function Product($DB, $id=0) |
64 |
{ |
{ |
65 |
$this->db = $DB; |
$this->db = $DB; |
70 |
|
|
71 |
|
|
72 |
/** |
/** |
73 |
* \brief Vérifie que la référence et libellé du produit est non null |
* \brief Vérifie que la référence et libellé du produit est non null |
74 |
* \return int 1 si ok, 0 sinon |
* \return int 1 si ok, 0 sinon |
75 |
*/ |
*/ |
76 |
|
|
77 |
function check() |
function check() |
359 |
} |
} |
360 |
|
|
361 |
|
|
362 |
/** |
/** |
363 |
* \brief Modifie le prix d'un produit/service |
* \brief Modifie le prix d'un produit/service |
364 |
* \param id id du produit/service à modifier |
* \param id id du produit/service à modifier |
365 |
* \param user utilisateur qui modifie le prix |
* \param user utilisateur qui modifie le prix |
366 |
*/ |
*/ |
367 |
|
function update_price($id, $user) |
|
function update_price($id, $user) |
|
|
{ |
|
|
if (strlen(trim($this->price)) > 0 ) |
|
|
{ |
|
|
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."product "; |
|
|
$sql .= " SET price = " . ereg_replace(",",".",$this->price); |
|
|
$sql .= " WHERE rowid = " . $id; |
|
|
|
|
|
if ( $this->db->query($sql) ) |
|
|
{ |
|
|
$this->_log_price($user); |
|
|
return 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
dolibarr_print_error($this->db); |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
$this->error = "Prix saisi invalide."; |
|
|
return -2; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* \brief Charge le produit/service en mémoire |
|
|
* \param id id du produit/service à charger |
|
|
*/ |
|
|
|
|
|
function fetch ($id) |
|
|
{ |
|
|
$sql = "SELECT rowid, ref, label, description, price, tva_tx, envente, nbvente, fk_product_type, duration, seuil_stock_alerte"; |
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."product WHERE rowid = $id"; |
|
|
|
|
|
$result = $this->db->query($sql) ; |
|
|
|
|
|
if ( $result ) |
|
|
{ |
|
|
$result = $this->db->fetch_array(); |
|
|
|
|
|
$this->id = $result["rowid"]; |
|
|
$this->ref = $result["ref"]; |
|
|
$this->libelle = stripslashes($result["label"]); |
|
|
$this->description = stripslashes($result["description"]); |
|
|
$this->price = $result["price"]; |
|
|
$this->tva_tx = $result["tva_tx"]; |
|
|
$this->type = $result["fk_product_type"]; |
|
|
$this->nbvente = $result["nbvente"]; |
|
|
$this->envente = $result["envente"]; |
|
|
$this->duration = $result["duration"]; |
|
|
$this->duration_value = substr($result["duration"],0,strlen($result["duration"])-1); |
|
|
$this->duration_unit = substr($result["duration"],-1); |
|
|
$this->seuil_stock_alerte = $result["seuil_stock_alerte"]; |
|
|
|
|
|
$this->label_url = '<a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$this->id.'">'.$this->libelle.'</a>'; |
|
|
|
|
|
if ($this->type == 0) |
|
|
{ |
|
|
$this->isproduct = 1; |
|
|
$this->isservice = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
$this->isproduct = 0; |
|
|
$this->isservice = 1; |
|
|
} |
|
|
|
|
|
$this->db->free(); |
|
|
|
|
|
$sql = "SELECT reel, fk_entrepot"; |
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."product_stock WHERE fk_product = $id"; |
|
|
$result = $this->db->query($sql) ; |
|
|
if ( $result ) |
|
|
{ |
|
|
$num = $this->db->num_rows(); |
|
|
$i=0; |
|
|
if ($num > 0) |
|
|
{ |
|
|
while ($i < $num ) |
|
|
{ |
|
|
$row = $this->db->fetch_row($i); |
|
|
$this->stock_entrepot[$row[1]] = $row[0]; |
|
|
|
|
|
$this->stock_reel = $this->stock_reel + $row[0]; |
|
|
$i++; |
|
|
} |
|
|
|
|
|
$this->no_stock = 0; |
|
|
} |
|
|
else |
|
|
{ |
|
|
$this->no_stock = 1; |
|
|
} |
|
|
$this->db->free(); |
|
|
} |
|
|
return 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
dolibarr_print_error($this->db); |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* \brief Renvoie le nombre de propale incluant le produit/service |
|
|
* \param socid id societe |
|
|
* \return int nombre d'inclusion |
|
|
*/ |
|
|
|
|
|
function count_propale($socid=0) |
|
368 |
{ |
{ |
369 |
$sql = "SELECT pd.fk_propal"; |
if (strlen(trim($this->price)) > 0 ) |
370 |
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."propal as pr"; |
{ |
371 |
$sql .= " WHERE pr.rowid = pd.fk_propal AND p.rowid = pd.fk_product AND p.rowid = ".$this->id; |
$sql = "UPDATE ".MAIN_DB_PREFIX."product "; |
372 |
if ($socid > 0) |
$sql .= " SET price = " . ereg_replace(",",".",$this->price); |
373 |
{ |
$sql .= " WHERE rowid = " . $id; |
374 |
$sql .= " AND pr.fk_soc = $socid"; |
|
375 |
} |
if ( $this->db->query($sql) ) |
376 |
$sql .= " GROUP BY pd.fk_propal"; |
{ |
377 |
|
$this->_log_price($user); |
378 |
$result = $this->db->query($sql) ; |
return 1; |
379 |
|
} |
380 |
if ( $result ) |
else |
381 |
{ |
{ |
382 |
return $this->db->num_rows(); |
dolibarr_print_error($this->db); |
383 |
} |
return -1; |
384 |
else |
} |
385 |
{ |
} |
386 |
return 0; |
else |
387 |
} |
{ |
388 |
|
$this->error = "Prix saisi invalide."; |
389 |
|
return -2; |
390 |
|
} |
391 |
} |
} |
392 |
|
|
393 |
|
|
394 |
/** |
/** |
395 |
* \brief Renvoie le nombre de client avec propale incluant le produit/service |
* \brief Charge le produit/service en mémoire |
396 |
* \param socid id societe |
* \param id id du produit/service à charger |
397 |
* \return int nombre d'inclusion |
*/ |
398 |
*/ |
function fetch ($id) |
|
|
|
|
function count_propale_client($socid=0) |
|
399 |
{ |
{ |
400 |
$sql = "SELECT pr.fk_soc"; |
$sql = "SELECT rowid, ref, label, description, price, tva_tx, envente, nbvente, fk_product_type, duration, seuil_stock_alerte"; |
401 |
$sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."propal as pr"; |
$sql .= " FROM ".MAIN_DB_PREFIX."product WHERE rowid = $id"; |
402 |
$sql .= " WHERE p.rowid = pd.fk_product AND pd.fk_propal = pr.rowid AND p.rowid = ".$this->id; |
|
403 |
if ($socid > 0) |
$result = $this->db->query($sql) ; |
404 |
{ |
|
405 |
$sql .= " AND pr.fk_soc = $socid"; |
if ( $result ) |
406 |
} |
{ |
407 |
$sql .= " GROUP BY pr.fk_soc"; |
$result = $this->db->fetch_array(); |
408 |
|
|
409 |
$result = $this->db->query($sql) ; |
$this->id = $result["rowid"]; |
410 |
|
$this->ref = $result["ref"]; |
411 |
if ( $result ) |
$this->libelle = stripslashes($result["label"]); |
412 |
{ |
$this->description = stripslashes($result["description"]); |
413 |
return $this->db->num_rows(); |
$this->price = $result["price"]; |
414 |
} |
$this->tva_tx = $result["tva_tx"]; |
415 |
else |
$this->type = $result["fk_product_type"]; |
416 |
{ |
$this->nbvente = $result["nbvente"]; |
417 |
return 0; |
$this->envente = $result["envente"]; |
418 |
} |
$this->duration = $result["duration"]; |
419 |
|
$this->duration_value = substr($result["duration"],0,strlen($result["duration"])-1); |
420 |
|
$this->duration_unit = substr($result["duration"],-1); |
421 |
|
$this->seuil_stock_alerte = $result["seuil_stock_alerte"]; |
422 |
|
|
423 |
|
$this->label_url = '<a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$this->id.'">'.$this->libelle.'</a>'; |
424 |
|
|
425 |
|
if ($this->type == 0) |
426 |
|
{ |
427 |
|
$this->isproduct = 1; |
428 |
|
$this->isservice = 0; |
429 |
|
} |
430 |
|
else |
431 |
|
{ |
432 |
|
$this->isproduct = 0; |
433 |
|
$this->isservice = 1; |
434 |
|
} |
435 |
|
|
436 |
|
$this->db->free(); |
437 |
|
|
438 |
|
$sql = "SELECT reel, fk_entrepot"; |
439 |
|
$sql .= " FROM ".MAIN_DB_PREFIX."product_stock WHERE fk_product = $id"; |
440 |
|
$result = $this->db->query($sql) ; |
441 |
|
if ( $result ) |
442 |
|
{ |
443 |
|
$num = $this->db->num_rows(); |
444 |
|
$i=0; |
445 |
|
if ($num > 0) |
446 |
|
{ |
447 |
|
while ($i < $num ) |
448 |
|
{ |
449 |
|
$row = $this->db->fetch_row($i); |
450 |
|
$this->stock_entrepot[$row[1]] = $row[0]; |
451 |
|
|
452 |
|
$this->stock_reel = $this->stock_reel + $row[0]; |
453 |
|
$i++; |
454 |
|
} |
455 |
|
|
456 |
|
$this->no_stock = 0; |
457 |
|
} |
458 |
|
else |
459 |
|
{ |
460 |
|
$this->no_stock = 1; |
461 |
|
} |
462 |
|
$this->db->free(); |
463 |
|
} |
464 |
|
return 1; |
465 |
|
} |
466 |
|
else |
467 |
|
{ |
468 |
|
dolibarr_print_error($this->db); |
469 |
|
return -1; |
470 |
|
} |
471 |
} |
} |
472 |
|
|
473 |
|
|
474 |
/** |
/** |
475 |
* \brief Renvoie le nombre de facture incluant le produit/service |
* \brief Charge tableau des stats propale pour le produit/service |
476 |
* \param socid id societe |
* \param socid Id societe |
477 |
* \return int nombre d'inclusion |
* \return array Tableau des stats |
478 |
*/ |
*/ |
479 |
|
function load_stats_propale($socid=0) |
|
function count_facture($socid=0) |
|
480 |
{ |
{ |
481 |
$sql = "SELECT pd.fk_facture"; |
$sql = "SELECT COUNT(DISTINCT pr.fk_soc) as nb_customers, COUNT(DISTINCT pr.rowid) as nb,"; |
482 |
$sql .= " FROM ".MAIN_DB_PREFIX."facturedet as pd, ".MAIN_DB_PREFIX."product as p"; |
$sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; |
483 |
$sql .= ", ".MAIN_DB_PREFIX."facture as f"; |
$sql.= " FROM ".MAIN_DB_PREFIX."propaldet as pd, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."propal as pr"; |
484 |
$sql .= " WHERE f.rowid = pd.fk_facture AND p.rowid = pd.fk_product AND p.rowid = ".$this->id; |
$sql.= " WHERE p.rowid = pd.fk_product AND pd.fk_propal = pr.rowid AND p.rowid = ".$this->id; |
485 |
if ($socid > 0) |
//$sql.= " AND pr.fk_statut != 0"; |
486 |
{ |
if ($socid > 0) |
487 |
$sql .= " AND f.fk_soc = $socid"; |
{ |
488 |
} |
$sql .= " AND pr.fk_soc = $socid"; |
489 |
$sql .= " GROUP BY pd.fk_facture"; |
} |
490 |
|
|
491 |
|
$result = $this->db->query($sql) ; |
492 |
|
if ( $result ) |
493 |
|
{ |
494 |
|
$obj=$this->db->fetch_object($result); |
495 |
|
$this->stats_propale['customers']=$obj->nb_customers; |
496 |
|
$this->stats_propale['nb']=$obj->nb; |
497 |
|
$this->stats_propale['rows']=$obj->nb_rows; |
498 |
|
$this->stats_propale['qty']=$obj->qty?$obj->qty:0; |
499 |
|
return 1; |
500 |
|
} |
501 |
|
else |
502 |
|
{ |
503 |
|
$this->error=$this->db->error(); |
504 |
|
return -1; |
505 |
|
} |
506 |
|
} |
507 |
|
|
|
$result = $this->db->query($sql) ; |
|
508 |
|
|
509 |
if ( $result ) |
/** |
510 |
{ |
* \brief Charge tableau des stats commande pour le produit/service |
511 |
return $this->db->num_rows(); |
* \param socid Id societe |
512 |
} |
* \return array Tableau des stats |
513 |
else |
*/ |
514 |
{ |
function load_stats_commande($socid=0) |
515 |
return 0; |
{ |
516 |
} |
$sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,"; |
517 |
|
$sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty"; |
518 |
|
$sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd, ".MAIN_DB_PREFIX."product as p,"; |
519 |
|
$sql.= " ".MAIN_DB_PREFIX."commande as c"; |
520 |
|
$sql.= " WHERE c.rowid = cd.fk_commande AND p.rowid = cd.fk_product AND p.rowid = ".$this->id; |
521 |
|
//$sql.= " AND c.fk_statut != 0"; |
522 |
|
if ($socid > 0) |
523 |
|
{ |
524 |
|
$sql .= " AND c.fk_soc = $socid"; |
525 |
|
} |
526 |
|
|
527 |
|
$result = $this->db->query($sql) ; |
528 |
|
if ( $result ) |
529 |
|
{ |
530 |
|
$obj=$this->db->fetch_object($result); |
531 |
|
$this->stats_commande['customers']=$obj->nb_customers; |
532 |
|
$this->stats_commande['nb']=$obj->nb; |
533 |
|
$this->stats_commande['rows']=$obj->nb_rows; |
534 |
|
$this->stats_commande['qty']=$obj->qty?$obj->qty:0; |
535 |
|
return 1; |
536 |
|
} |
537 |
|
else |
538 |
|
{ |
539 |
|
$this->error=$this->db->error(); |
540 |
|
return -1; |
541 |
|
} |
542 |
|
} |
543 |
|
|
544 |
|
/** |
545 |
|
* \brief Charge tableau des stats contrat pour le produit/service |
546 |
|
* \param socid Id societe |
547 |
|
* \return array Tableau des stats |
548 |
|
*/ |
549 |
|
function load_stats_contrat($socid=0) |
550 |
|
{ |
551 |
|
$sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,"; |
552 |
|
$sql.= " COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty"; |
553 |
|
$sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p,"; |
554 |
|
$sql.= " ".MAIN_DB_PREFIX."contrat as c"; |
555 |
|
$sql.= " WHERE c.rowid = cd.fk_contrat AND p.rowid = cd.fk_product AND p.rowid = ".$this->id; |
556 |
|
//$sql.= " AND c.statut != 0"; |
557 |
|
if ($socid > 0) |
558 |
|
{ |
559 |
|
$sql .= " AND c.fk_soc = $socid"; |
560 |
|
} |
561 |
|
|
562 |
|
$result = $this->db->query($sql) ; |
563 |
|
if ( $result ) |
564 |
|
{ |
565 |
|
$obj=$this->db->fetch_object($result); |
566 |
|
$this->stats_contrat['customers']=$obj->nb_customers; |
567 |
|
$this->stats_contrat['nb']=$obj->nb; |
568 |
|
$this->stats_contrat['rows']=$obj->nb_rows; |
569 |
|
$this->stats_contrat['qty']=$obj->qty?$obj->qty:0; |
570 |
|
return 1; |
571 |
|
} |
572 |
|
else |
573 |
|
{ |
574 |
|
$this->error=$this->db->error(); |
575 |
|
return -1; |
576 |
|
} |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
|
* \brief Charge tableau des stats facture pour le produit/service |
581 |
|
* \param socid Id societe |
582 |
|
* \return array Tableau des stats |
583 |
|
*/ |
584 |
|
function load_stats_facture($socid=0) |
585 |
|
{ |
586 |
|
$sql = "SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,"; |
587 |
|
$sql.= " COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty"; |
588 |
|
$sql.= " FROM ".MAIN_DB_PREFIX."facturedet as pd, ".MAIN_DB_PREFIX."product as p"; |
589 |
|
$sql.= ", ".MAIN_DB_PREFIX."facture as f"; |
590 |
|
$sql.= " WHERE f.rowid = pd.fk_facture AND p.rowid = pd.fk_product AND p.rowid = ".$this->id; |
591 |
|
//$sql.= " AND f.fk_statut != 0"; |
592 |
|
if ($socid > 0) |
593 |
|
{ |
594 |
|
$sql .= " AND f.fk_soc = $socid"; |
595 |
|
} |
596 |
|
|
597 |
|
$result = $this->db->query($sql) ; |
598 |
|
if ( $result ) |
599 |
|
{ |
600 |
|
$obj=$this->db->fetch_object($result); |
601 |
|
$this->stats_facture['customers']=$obj->nb_customers; |
602 |
|
$this->stats_facture['nb']=$obj->nb; |
603 |
|
$this->stats_facture['rows']=$obj->nb_rows; |
604 |
|
$this->stats_facture['qty']=$obj->qty?$obj->qty:0; |
605 |
|
return 1; |
606 |
|
} |
607 |
|
else |
608 |
|
{ |
609 |
|
$this->error=$this->db->error(); |
610 |
|
return -1; |
611 |
|
} |
612 |
} |
} |
613 |
|
|
614 |
|
|