38 require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
39 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
51 public $element =
'product';
56 public $table_element =
'product';
61 public $fk_element =
'fk_product';
66 protected $childtables = array(
67 'supplier_proposaldet' => array(
'name' =>
'SupplierProposal',
'parent' =>
'supplier_proposal',
'parentkey' =>
'fk_supplier_proposal'),
68 'propaldet' => array(
'name' =>
'Proposal',
'parent' =>
'propal',
'parentkey' =>
'fk_propal'),
69 'commandedet' => array(
'name' =>
'Order',
'parent' =>
'commande',
'parentkey' =>
'fk_commande'),
70 'facturedet' => array(
'name' =>
'Invoice',
'parent' =>
'facture',
'parentkey' =>
'fk_facture'),
71 'contratdet' => array(
'name' =>
'Contract',
'parent' =>
'contrat',
'parentkey' =>
'fk_contrat'),
72 'facture_fourn_det' => array(
'name' =>
'SupplierInvoice',
'parent' =>
'facture_fourn',
'parentkey' =>
'fk_facture_fourn'),
73 'commande_fournisseurdet' => array(
'name' =>
'SupplierOrder',
'parent' =>
'commande_fournisseur',
'parentkey' =>
'fk_commande')
81 public $ismultientitymanaged = 1;
86 public $isextrafieldmanaged = 1;
91 public $picto =
'product';
98 public $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
141 public $price_formated;
150 public $price_ttc_formated;
164 public $price_min_ttc;
170 public $price_base_type;
174 public $multiprices_ttc = array();
175 public $multiprices_base_type = array();
176 public $multiprices_min = array();
177 public $multiprices_min_ttc = array();
178 public $multiprices_tva_tx = array();
179 public $multiprices_recuperableonly = array();
183 public $prices_by_qty = array();
184 public $prices_by_qty_id = array();
185 public $prices_by_qty_list = array();
204 public $localtax2_tx;
205 public $localtax1_type;
206 public $localtax2_type;
210 public $desc_supplier;
211 public $vatrate_supplier;
212 public $default_vat_code_supplier;
213 public $fourn_multicurrency_price;
214 public $fourn_multicurrency_unitprice;
215 public $fourn_multicurrency_tx;
216 public $fourn_multicurrency_id;
217 public $fourn_multicurrency_code;
223 public $qc_frequency;
230 public $stock_reel = 0;
237 public $stock_theorique;
254 public $seuil_stock_alerte = 0;
264 public $duration_value;
269 public $fk_default_workstation;
296 public $status_buy = 0;
318 public $fk_default_bom;
325 public $status_batch = 0;
332 public $batch_mask =
'';
350 public $weight_units;
352 public $length_units;
356 public $height_units;
358 public $surface_units;
360 public $volume_units;
363 public $net_measure_units;
365 public $accountancy_code_sell;
366 public $accountancy_code_sell_intra;
367 public $accountancy_code_sell_export;
368 public $accountancy_code_buy;
369 public $accountancy_code_buy_intra;
370 public $accountancy_code_buy_export;
384 public $barcode_type;
391 public $barcode_type_code;
393 public $stats_propale = array();
394 public $stats_commande = array();
395 public $stats_contrat = array();
396 public $stats_facture = array();
397 public $stats_commande_fournisseur = array();
398 public $stats_expedition = array();
399 public $stats_reception = array();
400 public $stats_mo = array();
401 public $stats_bom = array();
402 public $stats_mrptoconsume = array();
403 public $stats_mrptoproduce = array();
404 public $stats_facturerec = array();
405 public $stats_facture_fournisseur = array();
414 public $date_creation;
419 public $date_modification;
437 public $fk_default_warehouse;
441 public $fk_price_expression;
446 public $fourn_price_base_type;
458 public $ref_supplier;
472 public $price_autogen = 0;
479 public $supplierprices;
486 public $is_object_used;
494 public $mandatory_period;
524 public $fields = array(
525 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
526 'ref' =>array(
'type'=>
'varchar(128)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>1,
'index'=>1,
'position'=>10,
'searchall'=>1,
'comment'=>
'Reference of object'),
527 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>5),
528 'label' =>array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>2,
'position'=>15,
'csslist'=>
'tdoverflowmax250'),
529 'barcode' =>array(
'type'=>
'varchar(255)',
'label'=>
'Barcode',
'enabled'=>
'isModEnabled("barcode")',
'position'=>20,
'visible'=>-1,
'showoncombobox'=>3),
530 'fk_barcode_type' => array(
'type'=>
'integer',
'label'=>
'BarcodeType',
'enabled'=>
'1',
'position'=>21,
'notnull'=>0,
'visible'=>-1,),
531 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61),
532 'note' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62),
533 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>500),
534 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>501),
536 'fk_user_author'=>array(
'type'=>
'integer',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>510,
'foreignkey'=>
'llx_user.rowid'),
537 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>511),
539 'localtax1_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax1tx',
'enabled'=>
'1',
'position'=>150,
'notnull'=>0,
'visible'=>-1,),
540 'localtax1_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax1type',
'enabled'=>
'1',
'position'=>155,
'notnull'=>1,
'visible'=>-1,),
541 'localtax2_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax2tx',
'enabled'=>
'1',
'position'=>160,
'notnull'=>0,
'visible'=>-1,),
542 'localtax2_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax2type',
'enabled'=>
'1',
'position'=>165,
'notnull'=>1,
'visible'=>-1,),
543 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'index'=>0,
'position'=>1000),
546 'mandatory_period' => array(
'type'=>
'integer',
'label'=>
'mandatory_period',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'default'=>0,
'index'=>1,
'position'=>1000),
586 $this->
ref = trim($this->
ref);
614 public function create($user, $notrigger = 0)
616 global $conf, $langs;
622 $this->
ref = trim($this->
ref);
626 $this->label = trim($this->label);
627 $this->price_ttc =
price2num($this->price_ttc);
629 $this->price_min_ttc =
price2num($this->price_min_ttc);
630 $this->price_min =
price2num($this->price_min);
631 if (empty($this->tva_tx)) {
634 if (empty($this->tva_npr)) {
638 if (empty($this->localtax1_tx)) {
639 $this->localtax1_tx = 0;
641 if (empty($this->localtax2_tx)) {
642 $this->localtax2_tx = 0;
644 if (empty($this->localtax1_type)) {
645 $this->localtax1_type =
'0';
647 if (empty($this->localtax2_type)) {
648 $this->localtax2_type =
'0';
650 if (empty($this->
price)) {
653 if (empty($this->price_min)) {
654 $this->price_min = 0;
657 if (empty($this->price_by_qty)) {
658 $this->price_by_qty = 0;
661 if (empty($this->status)) {
664 if (empty($this->status_buy)) {
665 $this->status_buy = 0;
674 if ($this->price_base_type ==
'TTC' && $this->price_ttc > 0) {
675 $price_ttc =
price2num($this->price_ttc,
'MU');
676 $price_ht =
price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),
'MU');
680 if ($this->price_base_type !=
'TTC' && $this->
price > 0) {
682 $price_ttc =
price2num($this->
price * (1 + ($this->tva_tx / 100)),
'MU');
686 if (($this->price_min_ttc > 0) && ($this->price_base_type ==
'TTC')) {
687 $price_min_ttc =
price2num($this->price_min_ttc,
'MU');
688 $price_min_ht =
price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),
'MU');
692 if (($this->price_min > 0) && ($this->price_base_type !=
'TTC')) {
693 $price_min_ht =
price2num($this->price_min,
'MU');
694 $price_min_ttc =
price2num($this->price_min * (1 + ($this->tva_tx / 100)),
'MU');
697 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
698 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
699 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
700 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
701 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
702 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
705 $this->barcode = trim($this->barcode);
706 $this->mandatory_period = empty($this->mandatory_period) ? 0 : $this->mandatory_period;
708 if (empty($this->label)) {
709 $this->error =
'ErrorMandatoryParametersNotProvided';
713 if (empty($this->
ref) || $this->
ref ==
'auto') {
716 if ($module !=
'mod_codeproduct_leopard') {
717 if (substr($module, 0, 16) ==
'mod_codeproduct_' && substr($module, -3) ==
'php') {
718 $module = substr($module, 0,
dol_strlen($module) - 4);
721 $modCodeProduct =
new $module;
722 if (!empty($modCodeProduct->code_auto)) {
723 $this->
ref = $modCodeProduct->getNextValue($this, $this->
type);
725 unset($modCodeProduct);
728 if (empty($this->
ref)) {
729 $this->error =
'ProductModuleNotSetupForAutoRef';
734 dol_syslog(get_class($this).
"::create ref=".$this->
ref.
" price=".$this->price.
" price_ttc=".$this->price_ttc.
" tva_tx=".$this->tva_tx.
" price_base_type=".$this->price_base_type, LOG_DEBUG);
741 if ($this->barcode == -1) {
742 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
747 $result = $this->
verify();
750 $sql =
"SELECT count(*) as nb";
751 $sql .=
" FROM ".$this->db->prefix().
"product";
752 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
753 $sql .=
" AND ref = '".$this->db->escape($this->
ref).
"'";
755 $result = $this->db->query(
$sql);
757 $obj = $this->db->fetch_object($result);
760 $sql =
"INSERT INTO ".$this->db->prefix().
"product (";
765 $sql .=
", price_min";
766 $sql .=
", price_min_ttc";
768 $sql .=
", fk_user_author";
769 $sql .=
", fk_product_type";
771 $sql .=
", price_ttc";
772 $sql .=
", price_base_type";
775 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
776 $sql .=
", accountancy_code_buy";
777 $sql .=
", accountancy_code_buy_intra";
778 $sql .=
", accountancy_code_buy_export";
779 $sql .=
", accountancy_code_sell";
780 $sql .=
", accountancy_code_sell_intra";
781 $sql .=
", accountancy_code_sell_export";
784 $sql .=
", finished";
786 $sql .=
", batch_mask";
788 $sql .=
", mandatory_period";
789 $sql .=
") VALUES (";
790 $sql .=
"'".$this->db->idate($now).
"'";
791 $sql .=
", ".((int) $conf->entity);
792 $sql .=
", '".$this->db->escape($this->
ref).
"'";
793 $sql .=
", ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
794 $sql .=
", ".price2num($price_min_ht);
795 $sql .=
", ".price2num($price_min_ttc);
796 $sql .=
", ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
797 $sql .=
", ".((int) $user->id);
799 $sql .=
", ".price2num($price_ht,
'MT');
800 $sql .=
", ".price2num($price_ttc,
'MT');
801 $sql .=
", '".$this->db->escape($this->price_base_type).
"'";
802 $sql .=
", ".((int) $this->status);
803 $sql .=
", ".((int) $this->status_buy);
804 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
805 $sql .=
", '".$this->db->escape($this->accountancy_code_buy).
"'";
806 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_intra).
"'";
807 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_export).
"'";
808 $sql .=
", '".$this->db->escape($this->accountancy_code_sell).
"'";
809 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_intra).
"'";
810 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_export).
"'";
812 $sql .=
", '".$this->db->escape($this->canvas).
"'";
813 $sql .=
", ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
'NULL' : (
int) $this->finished);
814 $sql .=
", ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : ((
int) $this->status_batch));
815 $sql .=
", '".$this->db->escape($this->batch_mask).
"'";
816 $sql .=
", ".($this->fk_unit > 0 ? ((int) $this->fk_unit) :
'NULL');
817 $sql .=
", '".$this->db->escape($this->mandatory_period).
"'";
820 dol_syslog(get_class($this).
"::Create", LOG_DEBUG);
821 $result = $this->db->query(
$sql);
823 $id = $this->db->last_insert_id($this->db->prefix().
"product");
827 $this->
price = $price_ht;
828 $this->price_ttc = $price_ttc;
829 $this->price_min = $price_min_ht;
830 $this->price_min_ttc = $price_min_ttc;
834 if ($this->
update($id, $user,
true,
'add') <= 0) {
839 $this->error = $this->db->lasterror();
843 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
844 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " .((int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
846 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
847 $sql .=
" fk_product";
849 $sql .=
", accountancy_code_buy";
850 $sql .=
", accountancy_code_buy_intra";
851 $sql .=
", accountancy_code_buy_export";
852 $sql .=
", accountancy_code_sell";
853 $sql .=
", accountancy_code_sell_intra";
854 $sql .=
", accountancy_code_sell_export";
855 $sql .=
") VALUES (";
857 $sql .=
", " . $conf->entity;
858 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
859 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
860 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
861 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
862 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
863 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
865 $result = $this->db->query(
$sql);
868 $this->error =
'ErrorFailedToInsertAccountancyForEntity';
873 $this->error =
'ErrorFailedToGetInsertedId';
877 $this->error = $this->db->lasterror();
881 $langs->load(
"products");
883 $this->error =
"ErrorProductAlreadyExists";
884 dol_syslog(get_class($this).
"::Create fails, ref ".$this->
ref.
" already exists");
888 $this->error = $this->db->lasterror();
891 if (!$error && !$notrigger) {
904 $this->db->rollback();
908 $this->db->rollback();
909 dol_syslog(get_class($this).
"::Create fails verify ".join(
',', $this->errors), LOG_WARNING);
925 $this->errors = array();
928 $this->
ref = trim($this->
ref);
931 $this->errors[] =
'ErrorBadRef';
935 $arrayofnonnegativevalue = array(
'weight'=>
'Weight',
'width'=>
'Width',
'height'=>
'Height',
'length'=>
'Length',
'surface'=>
'Surface',
'volume'=>
'Volume');
936 foreach ($arrayofnonnegativevalue as $key => $value) {
937 if (property_exists($this, $key) && !empty($this->$key) && ($this->$key < 0)) {
938 $langs->loadLangs(array(
"main",
"other"));
939 $this->error = $langs->trans(
"FieldCannotBeNegative", $langs->transnoentitiesnoconv($value));
940 $this->errors[] = $this->error;
945 $rescode = $this->
check_barcode($this->barcode, $this->barcode_type_code);
947 if ($rescode == -1) {
948 $this->errors[] =
'ErrorBadBarCodeSyntax';
949 } elseif ($rescode == -2) {
950 $this->errors[] =
'ErrorBarCodeRequired';
951 } elseif ($rescode == -3) {
953 $this->errors[] =
'ErrorBarCodeAlreadyUsed';
977 if (
isModEnabled(
'barcode') && !empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
978 $module = strtolower($conf->global->BARCODE_PRODUCT_ADDON_NUM);
980 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
981 foreach ($dirsociete as $dirroot) {
988 $mod =
new $module();
990 dol_syslog(get_class($this).
"::check_barcode value=".$valuetotest.
" type=".$typefortest.
" module=".$module);
991 $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest);
1009 public function update($id, $user, $notrigger =
false, $action =
'update', $updatetype =
false)
1011 global $langs, $conf, $hookmanager;
1016 if (!$this->label) {
1017 $this->label =
'MISSING LABEL';
1022 $this->
ref = trim($this->
ref);
1026 $this->label = trim($this->label);
1028 $this->note_private = (isset($this->note_private) ? trim($this->note_private) :
null);
1029 $this->note_public = (isset($this->note_public) ? trim($this->note_public) :
null);
1030 $this->net_measure =
price2num($this->net_measure);
1031 $this->net_measure_units = trim($this->net_measure_units);
1032 $this->weight =
price2num($this->weight);
1033 $this->weight_units = trim($this->weight_units);
1034 $this->length =
price2num($this->length);
1035 $this->length_units = trim($this->length_units);
1037 $this->width_units = trim($this->width_units);
1038 $this->height =
price2num($this->height);
1039 $this->height_units = trim($this->height_units);
1040 $this->surface =
price2num($this->surface);
1041 $this->surface_units = trim($this->surface_units);
1042 $this->volume =
price2num($this->volume);
1043 $this->volume_units = trim($this->volume_units);
1046 if (is_numeric($this->length_units)) {
1047 $this->width_units = $this->length_units;
1049 if (is_numeric($this->length_units)) {
1050 $this->height_units = $this->length_units;
1054 if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units) {
1055 $this->surface = $this->length * $this->width;
1058 if (empty($this->volume) && !empty($this->surface) && !empty($this->height) && $this->length_units == $this->height_units) {
1059 $this->volume = $this->surface * $this->height;
1063 if (empty($this->tva_tx)) {
1066 if (empty($this->tva_npr)) {
1069 if (empty($this->localtax1_tx)) {
1070 $this->localtax1_tx = 0;
1072 if (empty($this->localtax2_tx)) {
1073 $this->localtax2_tx = 0;
1075 if (empty($this->localtax1_type)) {
1076 $this->localtax1_type =
'0';
1078 if (empty($this->localtax2_type)) {
1079 $this->localtax2_type =
'0';
1081 if (empty($this->status)) {
1084 if (empty($this->status_buy)) {
1085 $this->status_buy = 0;
1088 if (empty($this->country_id)) {
1089 $this->country_id = 0;
1092 if (empty($this->state_id)) {
1093 $this->state_id = 0;
1097 $this->barcode = trim($this->barcode);
1099 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
1100 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
1101 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
1102 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
1103 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
1104 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
1111 if ($action !=
'add') {
1112 $result = $this->
verify();
1120 if (empty($this->oldcopy)) {
1126 if ($this->
hasbatch() && !$this->oldcopy->hasbatch()) {
1128 $valueforundefinedlot =
'000000';
1129 if (!empty($conf->global->STOCK_DEFAULT_BATCH)) {
1130 $valueforundefinedlot = $conf->global->STOCK_DEFAULT_BATCH;
1133 dol_syslog(
"Flag batch of product id=".$this->
id.
" is set to ON, so we will create missing records into product_batch");
1136 foreach ($this->stock_warehouse as $idW => $ObjW) {
1138 foreach ($ObjW->detail_batch as $detail) {
1139 if ($detail->batch == $valueforundefinedlot || $detail->batch ==
'Undefined') {
1141 $sqlclean =
"DELETE FROM ".$this->db->prefix().
"product_batch WHERE batch in('Undefined', '".$this->db->escape($valueforundefinedlot).
"') AND fk_product_stock = ".((int) $ObjW->id);
1142 $result = $this->db->query($sqlclean);
1150 $qty_batch += $detail->qty;
1154 if ($ObjW->real <> $qty_batch) {
1156 $ObjBatch->batch = $valueforundefinedlot;
1157 $ObjBatch->qty = ($ObjW->real - $qty_batch);
1158 $ObjBatch->fk_product_stock = $ObjW->id;
1160 if ($ObjBatch->create($user, 1) < 0) {
1162 $this->errors = $ObjBatch->errors;
1169 if ($this->barcode == -1) {
1170 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
1173 $sql =
"UPDATE ".$this->db->prefix().
"product";
1174 $sql .=
" SET label = '".$this->db->escape($this->label).
"'";
1177 $sql .=
", fk_product_type = ".((int) $this->
type);
1180 $sql .=
", ref = '".$this->db->escape($this->
ref).
"'";
1181 $sql .=
", ref_ext = ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1182 $sql .=
", default_vat_code = ".($this->default_vat_code ?
"'".$this->db->escape($this->default_vat_code).
"'" :
"null");
1183 $sql .=
", tva_tx = ".((float) $this->tva_tx);
1184 $sql .=
", recuperableonly = ".((int) $this->tva_npr);
1185 $sql .=
", localtax1_tx = ".((float) $this->localtax1_tx);
1186 $sql .=
", localtax2_tx = ".((float) $this->localtax2_tx);
1187 $sql .=
", localtax1_type = ".($this->localtax1_type !=
'' ?
"'".$this->db->escape($this->localtax1_type).
"'" :
"'0'");
1188 $sql .=
", localtax2_type = ".($this->localtax2_type !=
'' ?
"'".$this->db->escape($this->localtax2_type).
"'" :
"'0'");
1190 $sql .=
", barcode = ".(empty($this->barcode) ?
"null" :
"'".$this->db->escape($this->barcode).
"'");
1191 $sql .=
", fk_barcode_type = ".(empty($this->barcode_type) ?
"null" : $this->db->escape($this->barcode_type));
1193 $sql .=
", tosell = ".(int) $this->status;
1194 $sql .=
", tobuy = ".(int) $this->status_buy;
1195 $sql .=
", tobatch = ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : (
int) $this->status_batch);
1196 $sql .=
", batch_mask = '".$this->db->escape($this->batch_mask).
"'";
1198 $sql .=
", finished = ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
"null" : (
int) $this->finished);
1199 $sql .=
", fk_default_bom = ".((!isset($this->fk_default_bom) || $this->fk_default_bom < 0 || $this->fk_default_bom ==
'') ?
"null" : (
int) $this->fk_default_bom);
1200 $sql .=
", net_measure = ".($this->net_measure !=
'' ?
"'".$this->db->escape($this->net_measure).
"'" :
'null');
1201 $sql .=
", net_measure_units = ".($this->net_measure_units !=
'' ?
"'".$this->db->escape($this->net_measure_units).
"'" :
'null');
1202 $sql .=
", weight = ".($this->weight !=
'' ?
"'".$this->db->escape($this->weight).
"'" :
'null');
1203 $sql .=
", weight_units = ".($this->weight_units !=
'' ?
"'".$this->db->escape($this->weight_units).
"'" :
'null');
1204 $sql .=
", length = ".($this->length !=
'' ?
"'".$this->db->escape($this->length).
"'" :
'null');
1205 $sql .=
", length_units = ".($this->length_units !=
'' ?
"'".$this->db->escape($this->length_units).
"'" :
'null');
1206 $sql .=
", width= ".($this->width !=
'' ?
"'".$this->db->escape($this->width).
"'" :
'null');
1207 $sql .=
", width_units = ".($this->width_units !=
'' ?
"'".$this->db->escape($this->width_units).
"'" :
'null');
1208 $sql .=
", height = ".($this->height !=
'' ?
"'".$this->db->escape($this->height).
"'" :
'null');
1209 $sql .=
", height_units = ".($this->height_units !=
'' ?
"'".$this->db->escape($this->height_units).
"'" :
'null');
1210 $sql .=
", surface = ".($this->surface !=
'' ?
"'".$this->db->escape($this->surface).
"'" :
'null');
1211 $sql .=
", surface_units = ".($this->surface_units !=
'' ?
"'".$this->db->escape($this->surface_units).
"'" :
'null');
1212 $sql .=
", volume = ".($this->volume !=
'' ?
"'".$this->db->escape($this->volume).
"'" :
'null');
1213 $sql .=
", volume_units = ".($this->volume_units !=
'' ?
"'".$this->db->escape($this->volume_units).
"'" :
'null');
1214 $sql .=
", fk_default_warehouse = ".($this->fk_default_warehouse > 0 ? $this->db->escape($this->fk_default_warehouse) :
'null');
1215 $sql .=
", fk_default_workstation = ".($this->fk_default_workstation > 0 ? $this->db->escape($this->fk_default_workstation) :
'null');
1216 $sql .=
", seuil_stock_alerte = ".((isset($this->seuil_stock_alerte) && is_numeric($this->seuil_stock_alerte)) ? (
float) $this->seuil_stock_alerte :
'null');
1217 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
1218 $sql .=
", url = ".($this->url ?
"'".$this->db->escape($this->url).
"'" :
'null');
1219 $sql .=
", customcode = '".$this->db->escape($this->customcode).
"'";
1220 $sql .=
", fk_country = ".($this->country_id > 0 ? (int) $this->country_id :
'null');
1221 $sql .=
", fk_state = ".($this->state_id > 0 ? (int) $this->state_id :
'null');
1222 $sql .=
", lifetime = ".($this->lifetime > 0 ? (int) $this->lifetime :
'null');
1223 $sql .=
", qc_frequency = ".($this->qc_frequency > 0 ? (int) $this->qc_frequency :
'null');
1224 $sql .=
", note = ".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
'null');
1225 $sql .=
", note_public = ".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
'null');
1226 $sql .=
", duration = '".$this->db->escape($this->duration_value.$this->duration_unit).
"'";
1227 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1228 $sql .=
", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1229 $sql .=
", accountancy_code_buy_intra = '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1230 $sql .=
", accountancy_code_buy_export = '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1231 $sql .=
", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1232 $sql .=
", accountancy_code_sell_intra= '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1233 $sql .=
", accountancy_code_sell_export= '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1235 $sql .=
", desiredstock = ".((isset($this->desiredstock) && is_numeric($this->desiredstock)) ? (
float) $this->desiredstock :
"null");
1236 $sql .=
", cost_price = ".($this->cost_price !=
'' ? $this->db->escape($this->cost_price) :
'null');
1237 $sql .=
", fk_unit= ".(!$this->fk_unit ?
'NULL' : (int) $this->fk_unit);
1238 $sql .=
", price_autogen = ".(!$this->price_autogen ? 0 : 1);
1239 $sql .=
", fk_price_expression = ".($this->fk_price_expression != 0 ? (int) $this->fk_price_expression :
'NULL');
1240 $sql .=
", fk_user_modif = ".($user->id > 0 ? $user->id :
'NULL');
1241 $sql .=
", mandatory_period = ".($this->mandatory_period );
1243 $sql .=
" WHERE rowid = ".((int) $id);
1245 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1247 $resql = $this->db->query(
$sql);
1254 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".
$sql;
1262 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1263 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " . ((
int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
1265 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
1266 $sql .=
" fk_product";
1268 $sql .=
", accountancy_code_buy";
1269 $sql .=
", accountancy_code_buy_intra";
1270 $sql .=
", accountancy_code_buy_export";
1271 $sql .=
", accountancy_code_sell";
1272 $sql .=
", accountancy_code_sell_intra";
1273 $sql .=
", accountancy_code_sell_export";
1274 $sql .=
") VALUES (";
1276 $sql .=
", " . $conf->entity;
1277 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1278 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1279 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1280 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1281 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1282 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1284 $result = $this->db->query(
$sql);
1287 $this->error =
'ErrorFailedToUpdateAccountancyForEntity';
1299 if (!$error && !$notrigger) {
1301 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1308 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
1310 if ($conf->product->dir_output) {
1313 if (file_exists($olddir)) {
1317 $res = @rename($olddir, $newdir);
1319 $langs->load(
"errors");
1320 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
1329 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1333 foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
1334 $currcomb->updateProperties($this, $user);
1338 $this->db->commit();
1341 $this->db->rollback();
1345 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1346 $langs->load(
"errors");
1347 if (empty($conf->barcode->enabled) || empty($this->barcode)) {
1348 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductAlreadyExists", $this->
ref);
1350 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductBarCodeAlreadyExists", $this->barcode);
1352 $this->errors[] = $this->error;
1353 $this->db->rollback();
1356 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".
$sql;
1357 $this->errors[] = $this->error;
1358 $this->db->rollback();
1363 $this->db->rollback();
1364 dol_syslog(get_class($this).
"::Update fails verify ".join(
',', $this->errors), LOG_WARNING);
1376 public function delete(
User $user, $notrigger = 0)
1378 global $conf, $langs;
1379 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1384 if (empty($this->
id)) {
1385 $this->error =
"Object must be fetched before calling delete";
1389 $this->error =
"ErrorForbidden";
1394 if (empty($objectisused)) {
1397 if (!$error && empty($notrigger)) {
1399 $result = $this->
call_trigger(
'PRODUCT_DELETE', $user);
1408 $sql =
"DELETE FROM ".$this->db->prefix().
'product_batch';
1409 $sql .=
" WHERE fk_product_stock IN (";
1410 $sql .=
"SELECT rowid FROM ".$this->db->prefix().
'product_stock';
1411 $sql .=
" WHERE fk_product = ".((int) $this->
id).
")";
1413 $result = $this->db->query(
$sql);
1416 $this->errors[] = $this->db->lasterror();
1422 $elements = array(
'product_fournisseur_price',
'product_price',
'product_lang',
'categorie_product',
'product_stock',
'product_customer_price',
'product_lot');
1423 foreach ($elements as $table) {
1425 $sql =
"DELETE FROM ".$this->db->prefix().$table;
1426 $sql .=
" WHERE fk_product = ".(int) $this->
id;
1428 $result = $this->db->query(
$sql);
1431 $this->errors[] = $this->db->lasterror();
1438 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1439 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
1444 if ($prodcomb->deleteByFkProductParent($user, $this->id) < 0) {
1446 $this->errors[] =
'Error deleting combinations';
1450 if (!$error && ($prodcomb->fetchByFkProductChild($this->id) > 0) && ($prodcomb->delete($user) < 0)) {
1452 $this->errors[] =
'Error deleting child combination';
1458 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
1459 $sql .=
" WHERE fk_product_pere = ".(int) $this->
id.
" OR fk_product_fils = ".(
int) $this->id;
1461 $result = $this->db->query(
$sql);
1464 $this->errors[] = $this->db->lasterror();
1473 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1479 $sqlz =
"DELETE FROM ".$this->db->prefix().
"product";
1480 $sqlz .=
" WHERE rowid = ".(int) $this->
id;
1482 $resultz = $this->db->query($sqlz);
1485 $this->errors[] = $this->db->lasterror();
1500 if ($conf->product->dir_output) {
1501 $dir = $conf->product->dir_output.
"/".$ref;
1502 if (file_exists($dir)) {
1505 $this->errors[] =
'ErrorFailToDeleteDir';
1513 $this->db->commit();
1516 foreach ($this->errors as $errmsg) {
1517 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1518 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1520 $this->db->rollback();
1524 $this->error =
"ErrorRecordIsUsedCantDelete";
1537 global $conf, $langs;
1539 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
1540 $current_lang = $langs->getDefaultLang();
1542 foreach ($langs_available as $key => $value) {
1543 if ($key == $current_lang) {
1544 $sql =
"SELECT rowid";
1545 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1546 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1547 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1549 $result = $this->db->query(
$sql);
1551 if ($this->db->num_rows($result)) {
1552 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1554 $sql2 .=
" label='".$this->db->escape($this->label).
"',";
1555 $sql2 .=
" description='".$this->db->escape($this->
description).
"'";
1556 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1557 $sql2 .=
", note='".$this->db->escape($this->other).
"'";
1559 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1561 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1562 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1566 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->label).
"',";
1567 $sql2 .=
" '".$this->db->escape($this->
description).
"'";
1568 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1569 $sql2 .=
", '".$this->db->escape($this->other).
"'";
1573 dol_syslog(get_class($this).
'::setMultiLangs key = current_lang = '.$key);
1574 if (!$this->db->query($sql2)) {
1575 $this->error = $this->db->lasterror();
1578 } elseif (isset($this->multilangs[$key])) {
1579 if (empty($this->multilangs[
"$key"][
"label"])) {
1580 $this->errors[] = $key .
' : ' . $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Label"));
1584 $sql =
"SELECT rowid";
1585 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1586 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1587 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1589 $result = $this->db->query(
$sql);
1591 if ($this->db->num_rows($result)) {
1592 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1594 $sql2 .=
" label = '".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1595 $sql2 .=
" description = '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1596 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1597 $sql2 .=
", note = '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1599 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1601 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1602 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1606 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1607 $sql2 .=
" '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1608 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1609 $sql2 .=
", '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1615 if ($this->multilangs[
"$key"][
"label"] || $this->multilangs[
"$key"][
"description"]) {
1616 if (!$this->db->query($sql2)) {
1617 $this->error = $this->db->lasterror();
1627 $result = $this->
call_trigger(
'PRODUCT_SET_MULTILANGS', $user);
1629 $this->error = $this->db->lasterror();
1647 $sql =
"DELETE FROM ".$this->db->prefix().
"product_lang";
1648 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($langtodelete).
"'";
1650 dol_syslog(get_class($this).
'::delMultiLangs', LOG_DEBUG);
1651 $result = $this->db->query(
$sql);
1654 $result = $this->
call_trigger(
'PRODUCT_DEL_MULTILANGS', $user);
1656 $this->error = $this->db->lasterror();
1657 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1663 $this->error = $this->db->lasterror();
1664 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1679 global $user, $langs, $conf;
1685 if ($type ==
'buy') {
1686 $field =
'accountancy_code_buy';
1687 } elseif ($type ==
'buy_intra') {
1688 $field =
'accountancy_code_buy_intra';
1689 } elseif ($type ==
'buy_export') {
1690 $field =
'accountancy_code_buy_export';
1691 } elseif ($type ==
'sell') {
1692 $field =
'accountancy_code_sell';
1693 } elseif ($type ==
'sell_intra') {
1694 $field =
'accountancy_code_sell_intra';
1695 } elseif ($type ==
'sell_export') {
1696 $field =
'accountancy_code_sell_export';
1701 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET ";
1702 $sql .=
"$field = '".$this->db->escape($value).
"'";
1703 $sql .=
" WHERE rowid = ".((int) $this->
id);
1706 $resql = $this->db->query(
$sql);
1710 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1717 $this->db->rollback();
1721 $this->$field = $value;
1723 $this->db->commit();
1726 $this->error = $this->db->lasterror();
1727 $this->db->rollback();
1741 $current_lang = $langs->getDefaultLang();
1743 $sql =
"SELECT lang, label, description, note as other";
1744 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1745 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1747 $result = $this->db->query(
$sql);
1749 while ($obj = $this->db->fetch_object($result)) {
1751 if ($obj->lang == $current_lang) {
1752 $this->label = $obj->label;
1754 $this->other = $obj->other;
1756 $this->multilangs[
"$obj->lang"][
"label"] = $obj->label;
1757 $this->multilangs[
"$obj->lang"][
"description"] = $obj->description;
1758 $this->multilangs[
"$obj->lang"][
"other"] = $obj->other;
1762 $this->error =
"Error: ".$this->db->lasterror().
" - ".
$sql;
1776 $testExit = array(
'multiprices',
'multiprices_ttc',
'multiprices_base_type',
'multiprices_min',
'multiprices_min_ttc',
'multiprices_tva_tx',
'multiprices_recuperableonly');
1778 foreach ($testExit as $field) {
1779 if (!isset($this->$field)) {
1782 $tmparray = $this->$field;
1783 if (!isset($tmparray[$level])) {
1789 'level' => $level ? $level : 1,
1790 'multiprices' => doubleval($this->multiprices[$level]),
1791 'multiprices_ttc' => doubleval($this->multiprices_ttc[$level]),
1792 'multiprices_base_type' => $this->multiprices_base_type[$level],
1793 'multiprices_min' => doubleval($this->multiprices_min[$level]),
1794 'multiprices_min_ttc' => doubleval($this->multiprices_min_ttc[$level]),
1795 'multiprices_tva_tx' => doubleval($this->multiprices_tva_tx[$level]),
1796 'multiprices_recuperableonly' => doubleval($this->multiprices_recuperableonly[$level]),
1819 if (empty($this->price_by_qty)) {
1820 $this->price_by_qty = 0;
1824 $sql =
"INSERT INTO ".$this->db->prefix().
"product_price(price_level,date_price, fk_product, fk_user_author, price, price_ttc, price_base_type,tosell, tva_tx, default_vat_code, recuperableonly,";
1825 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
1826 $sql .=
" VALUES(".($level ? ((int) $level) : 1).
", '".$this->db->idate($now).
"', ".((int) $this->
id).
", ".((int) $user->id).
", ".((
float)
price2num($this->
price)).
", ".((
float)
price2num($this->price_ttc)).
",'".$this->db->escape($this->price_base_type).
"',".((int) $this->status).
", ".((
float)
price2num($this->tva_tx)).
", ".($this->default_vat_code ? (
"'".$this->db->escape($this->default_vat_code).
"'") :
"null").
", ".((
int)
$this->tva_npr).
",";
1827 $sql .=
" ".price2num($this->localtax1_tx).
", ".
price2num($this->localtax2_tx).
", '".$this->db->escape($this->localtax1_type).
"', '".$this->db->escape($this->localtax2_type).
"', ".
price2num($this->price_min).
", ".
price2num($this->price_min_ttc).
", ".
price2num($this->price_by_qty).
", ".((int) $conf->entity).
",".($this->fk_price_expression > 0 ? ((int) $this->fk_price_expression) :
'null');
1830 dol_syslog(get_class($this).
"::_log_price", LOG_DEBUG);
1831 $resql = $this->db->query(
$sql);
1833 $this->error = $this->db->lasterror();
1853 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price_by_qty";
1854 $sql .=
" WHERE fk_product_price = ".((int) $rowid);
1855 $resql = $this->db->query(
$sql);
1857 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price";
1858 $sql .=
" WHERE rowid=".((int) $rowid);
1859 $resql = $this->db->query(
$sql);
1863 $this->error = $this->db->lasterror();
1878 public function getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp = 0)
1880 global $conf, $db, $hookmanager, $action;
1883 if (is_object($hookmanager)) {
1884 $parameters = array(
'thirdparty_seller'=>$thirdparty_seller,
'thirdparty_buyer' => $thirdparty_buyer,
'pqp' => $pqp);
1886 $reshook = $hookmanager->executeHooks(
'getSellPrice', $parameters, $this, $action);
1888 return $hookmanager->resArray;
1899 $pu_ht = $this->price;
1900 $pu_ttc = $this->price_ttc;
1901 $price_min = $this->price_min;
1902 $price_base_type = $this->price_base_type;
1905 if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($thirdparty_buyer->price_level)) {
1906 $pu_ht = $this->multiprices[$thirdparty_buyer->price_level];
1907 $pu_ttc = $this->multiprices_ttc[$thirdparty_buyer->price_level];
1908 $price_min = $this->multiprices_min[$thirdparty_buyer->price_level];
1909 $price_base_type = $this->multiprices_base_type[$thirdparty_buyer->price_level];
1910 if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
1911 if (isset($this->multiprices_tva_tx[$thirdparty_buyer->price_level])) {
1912 $tva_tx = $this->multiprices_tva_tx[$thirdparty_buyer->price_level];
1914 if (isset($this->multiprices_recuperableonly[$thirdparty_buyer->price_level])) {
1915 $tva_npr = $this->multiprices_recuperableonly[$thirdparty_buyer->price_level];
1921 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1923 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
1927 $filter = array(
't.fk_product' => $this->
id,
't.fk_soc' => $thirdparty_buyer->id);
1929 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
1931 if (count($prodcustprice->lines) > 0) {
1932 $pu_ht =
price($prodcustprice->lines[0]->price);
1933 $price_min =
price($prodcustprice->lines[0]->price_min);
1934 $pu_ttc =
price($prodcustprice->lines[0]->price_ttc);
1935 $price_base_type = $prodcustprice->lines[0]->price_base_type;
1936 $tva_tx = $prodcustprice->lines[0]->tva_tx;
1937 if ($prodcustprice->lines[0]->default_vat_code && !preg_match(
'/\(.*\)/',
$tva_tx)) {
1938 $tva_tx .=
' ('.$prodcustprice->lines[0]->default_vat_code.
')';
1940 $tva_npr = $prodcustprice->lines[0]->recuperableonly;
1946 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
1948 if ($this->prices_by_qty[0]) {
1951 foreach ($this->prices_by_qty_list[0] as $priceforthequantityarray) {
1952 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1956 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1957 $pu_ht = $priceforthequantityarray[
'unitprice'];
1959 $pu_ttc = $priceforthequantityarray[
'unitprice'];
1964 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1966 if ($this->prices_by_qty[$thirdparty_buyer->price_level]) {
1969 foreach ($this->prices_by_qty_list[$thirdparty_buyer->price_level] as $priceforthequantityarray) {
1970 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1974 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1975 $pu_ht = $priceforthequantityarray[
'unitprice'];
1977 $pu_ttc = $priceforthequantityarray[
'unitprice'];
1984 return array(
'pu_ht'=>$pu_ht,
'pu_ttc'=>$pu_ttc,
'price_min'=>$price_min,
'price_base_type'=>$price_base_type,
'tva_tx'=>
$tva_tx,
'tva_npr'=>
$tva_npr);
2001 public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref =
'', $fk_soc = 0)
2008 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2009 $sql .=
" pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.desc_fourn as desc_supplier, pfp.tva_tx, pfp.default_vat_code, pfp.fk_supplier_price_expression,";
2010 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2011 $sql .=
" pfp.packaging";
2012 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2013 $sql .=
" WHERE pfp.rowid = ".((int) $prodfournprice);
2015 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2017 $sql .=
" ORDER BY pfp.quantity DESC";
2019 dol_syslog(get_class($this).
"::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
2020 $resql = $this->db->query(
$sql);
2022 $obj = $this->db->fetch_object($resql);
2023 if ($obj && $obj->quantity > 0) {
2024 if (
isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2026 $prod_supplier->product_fourn_price_id = $obj->rowid;
2027 $prod_supplier->id = $obj->fk_product;
2028 $prod_supplier->fourn_qty = $obj->quantity;
2029 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2030 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2032 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2034 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2035 if ($price_result >= 0) {
2036 $obj->price = $price_result;
2039 $this->product_fourn_price_id = $obj->rowid;
2040 $this->buyprice = $obj->price;
2041 $this->fourn_pu = $obj->price / $obj->quantity;
2042 $this->fourn_price_base_type =
'HT';
2043 $this->fourn_socid = $obj->fk_soc;
2044 $this->ref_fourn = $obj->ref_supplier;
2045 $this->ref_supplier = $obj->ref_supplier;
2046 $this->desc_supplier = $obj->desc_supplier;
2047 $this->remise_percent = $obj->remise_percent;
2048 $this->vatrate_supplier = $obj->tva_tx;
2049 $this->default_vat_code_supplier = $obj->default_vat_code;
2050 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2051 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2052 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2053 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2054 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2055 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2056 $this->packaging = $obj->packaging;
2058 $result = $obj->fk_product;
2062 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2063 $sql .=
" pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.desc_fourn as desc_supplier, pfp.tva_tx, pfp.default_vat_code, pfp.fk_supplier_price_expression,";
2064 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2065 $sql .=
" pfp.packaging";
2066 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2067 $sql .=
" WHERE 1 = 1";
2068 if ($product_id > 0) {
2069 $sql .=
" AND pfp.fk_product = ".((int) $product_id);
2071 if ($fourn_ref !=
'none') {
2072 $sql .=
" AND pfp.ref_fourn = '".$this->db->escape($fourn_ref).
"'";
2075 $sql .=
" AND pfp.fk_soc = ".((int) $fk_soc);
2078 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2080 $sql .=
" ORDER BY pfp.quantity DESC";
2083 dol_syslog(get_class($this).
"::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
2084 $resql = $this->db->query(
$sql);
2086 $obj = $this->db->fetch_object($resql);
2087 if ($obj && $obj->quantity > 0) {
2088 if (
isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2090 $prod_supplier->product_fourn_price_id = $obj->rowid;
2091 $prod_supplier->id = $obj->fk_product;
2092 $prod_supplier->fourn_qty = $obj->quantity;
2093 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2094 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2096 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2098 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2100 $obj->price = $price_result;
2103 $this->product_fourn_price_id = $obj->rowid;
2104 $this->buyprice = $obj->price;
2105 $this->fourn_qty = $obj->quantity;
2106 $this->fourn_pu = $obj->price / $obj->quantity;
2107 $this->fourn_price_base_type =
'HT';
2108 $this->fourn_socid = $obj->fk_soc;
2109 $this->ref_fourn = $obj->ref_supplier;
2110 $this->ref_supplier = $obj->ref_supplier;
2111 $this->desc_supplier = $obj->desc_supplier;
2112 $this->remise_percent = $obj->remise_percent;
2113 $this->vatrate_supplier = $obj->tva_tx;
2114 $this->default_vat_code_supplier = $obj->default_vat_code;
2115 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2116 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2117 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2118 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2119 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2120 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2121 $this->packaging = $obj->packaging;
2123 $result = $obj->fk_product;
2129 $this->error = $this->db->lasterror();
2134 $this->error = $this->db->lasterror();
2156 public function updatePrice($newprice, $newpricebase, $user, $newvat =
'', $newminprice = 0, $level = 0, $newnpr = 0, $newpbq = 0, $ignore_autogen = 0, $localtaxes_array = array(), $newdefaultvatcode =
'')
2158 global $conf, $langs;
2164 dol_syslog(get_class($this).
"::update_price id=".$id.
" newprice=".$newprice.
" newpricebase=".$newpricebase.
" newminprice=".$newminprice.
" level=".$level.
" npr=".$newnpr.
" newdefaultvatcode=".$newdefaultvatcode);
2167 if (empty($this->tva_tx)) {
2170 if (empty($newnpr)) {
2173 if (empty($newminprice)) {
2176 if (empty($newminprice)) {
2181 if ($newvat ==
'') {
2187 if ((!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && !$ignore_autogen && $this->price_autogen && ($level == 1)) {
2188 return $this->
generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpbq);
2191 if (!empty($newminprice) && ($newminprice > $newprice)) {
2192 $this->error =
'ErrorPriceCantBeLowerThanMinPrice';
2196 if ($newprice !==
'' || $newprice === 0) {
2197 if ($newpricebase ==
'TTC') {
2198 $price_ttc =
price2num($newprice,
'MU');
2199 $price =
price2num($newprice) / (1 + ($newvat / 100));
2202 if ($newminprice !=
'' || $newminprice == 0) {
2203 $price_min_ttc =
price2num($newminprice,
'MU');
2204 $price_min =
price2num($newminprice) / (1 + ($newvat / 100));
2205 $price_min =
price2num($price_min,
'MU');
2212 $price_ttc = ($newnpr != 1) ? (
float)
price2num($newprice) * (1 + ($newvat / 100)) : $price;
2213 $price_ttc =
price2num($price_ttc,
'MU');
2215 if ($newminprice !==
'' || $newminprice === 0) {
2216 $price_min =
price2num($newminprice,
'MU');
2217 $price_min_ttc =
price2num($newminprice) * (1 + ($newvat / 100));
2218 $price_min_ttc =
price2num($price_min_ttc,
'MU');
2227 if (count($localtaxes_array) > 0) {
2228 $localtaxtype1 = $localtaxes_array[
'0'];
2229 $localtax1 = $localtaxes_array[
'1'];
2230 $localtaxtype2 = $localtaxes_array[
'2'];
2231 $localtax2 = $localtaxes_array[
'3'];
2234 if (!empty($newdefaultvatcode)) {
2237 $sql =
"SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
2238 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as t, ".MAIN_DB_PREFIX.
"c_country as c";
2239 $sql .=
" WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($mysoc->country_code).
"'";
2240 $sql .=
" AND t.taux = ".((float) $newdefaultvatcode).
" AND t.active = 1";
2241 $sql .=
" AND t.code = '".$this->db->escape($newdefaultvatcode).
"'";
2242 $resql = $this->db->query(
$sql);
2244 $obj = $this->db->fetch_object($resql);
2246 $npr = $obj->recuperableonly;
2247 $localtax1 = $obj->localtax1;
2248 $localtax2 = $obj->localtax2;
2249 $localtaxtype1 = $obj->localtax1_type;
2250 $localtaxtype2 = $obj->localtax2_type;
2255 $localtaxtype1 =
'0';
2257 $localtaxtype2 =
'0';
2261 if (empty($localtax1)) {
2264 if (empty($localtax2)) {
2272 $sql =
"UPDATE ".$this->db->prefix().
"product SET";
2273 $sql .=
" price_base_type='".$this->db->escape($newpricebase).
"',";
2274 $sql .=
" price=".$price.
",";
2275 $sql .=
" price_ttc=".$price_ttc.
",";
2276 $sql .=
" price_min=".$price_min.
",";
2277 $sql .=
" price_min_ttc=".$price_min_ttc.
",";
2278 $sql .=
" localtax1_tx=".($localtax1 >= 0 ? $localtax1 :
'NULL').
",";
2279 $sql .=
" localtax2_tx=".($localtax2 >= 0 ? $localtax2 :
'NULL').
",";
2280 $sql .=
" localtax1_type=".($localtaxtype1 !=
'' ?
"'".$this->db->escape($localtaxtype1).
"'" :
"'0'").
",";
2281 $sql .=
" localtax2_type=".($localtaxtype2 !=
'' ?
"'".$this->db->escape($localtaxtype2).
"'" :
"'0'").
",";
2282 $sql .=
" default_vat_code=".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
2283 $sql .=
" tva_tx='".price2num($newvat).
"',";
2284 $sql .=
" recuperableonly='".$this->db->escape($newnpr).
"'";
2285 $sql .=
" WHERE rowid = ".((int) $id);
2287 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
2288 $resql = $this->db->query(
$sql);
2290 $this->multiprices[$level] = $price;
2291 $this->multiprices_ttc[$level] = $price_ttc;
2292 $this->multiprices_min[$level] = $price_min;
2293 $this->multiprices_min_ttc[$level] = $price_min_ttc;
2294 $this->multiprices_base_type[$level] = $newpricebase;
2295 $this->multiprices_default_vat_code[$level] = $newdefaultvatcode;
2296 $this->multiprices_tva_tx[$level] = $newvat;
2297 $this->multiprices_recuperableonly[$level] = $newnpr;
2299 $this->
price = $price;
2300 $this->price_ttc = $price_ttc;
2301 $this->price_min = $price_min;
2302 $this->price_min_ttc = $price_min_ttc;
2303 $this->price_base_type = $newpricebase;
2304 $this->default_vat_code = $newdefaultvatcode;
2305 $this->tva_tx = $newvat;
2306 $this->tva_npr = $newnpr;
2308 $this->localtax1_tx = $localtax1;
2309 $this->localtax2_tx = $localtax2;
2310 $this->localtax1_type = $localtaxtype1;
2311 $this->localtax2_type = $localtaxtype2;
2314 $this->price_by_qty = $newpbq;
2318 if (!empty(array_diff_assoc($newPriceData, $lastPriceData)) || empty($conf->global->PRODUIT_MULTIPRICES)) {
2322 $this->level = $level;
2325 $result = $this->
call_trigger(
'PRODUCT_PRICE_MODIFY', $user);
2327 $this->db->rollback();
2332 $this->db->commit();
2334 $this->db->rollback();
2335 $this->error = $this->db->lasterror();
2354 $this->fk_price_expression = $expression_id;
2356 return $this->
update($this->
id, $user);
2371 public function fetch($id =
'', $ref =
'', $ref_ext =
'', $barcode =
'', $ignore_expression = 0, $ignore_price_load = 0, $ignore_lang_load = 0)
2373 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
2375 global $langs, $conf;
2377 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
2380 if (!$id && !$ref && !$ref_ext && !$barcode) {
2381 $this->error =
'ErrorWrongParameters';
2382 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
2386 $sql =
"SELECT p.rowid, p.ref, p.ref_ext, p.label, p.description, p.url, p.note_public, p.note as note_private, p.customcode, p.fk_country, p.fk_state, p.lifetime, p.qc_frequency, p.price, p.price_ttc,";
2387 $sql .=
" p.price_min, p.price_min_ttc, p.price_base_type, p.cost_price, p.default_vat_code, p.tva_tx, p.recuperableonly as tva_npr, p.localtax1_tx, p.localtax2_tx, p.localtax1_type, p.localtax2_type, p.tosell,";
2388 $sql .=
" p.tobuy, p.fk_product_type, p.duration, p.fk_default_warehouse, p.fk_default_workstation, p.seuil_stock_alerte, p.canvas, p.net_measure, p.net_measure_units, p.weight, p.weight_units,";
2389 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2390 $sql .=
" p.surface, p.surface_units, p.volume, p.volume_units, p.barcode, p.fk_barcode_type, p.finished, p.fk_default_bom, p.mandatory_period,";
2391 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2392 $sql .=
" p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export, p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
2394 $sql .=
" ppe.accountancy_code_buy, ppe.accountancy_code_buy_intra, ppe.accountancy_code_buy_export, ppe.accountancy_code_sell, ppe.accountancy_code_sell_intra, ppe.accountancy_code_sell_export,";
2399 $separatedEntityPMP =
false;
2400 $separatedStock =
false;
2401 $visibleWarehousesEntities = $conf->entity;
2402 if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) {
2403 if (!empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
2404 $checkPMPPerEntity = $this->db->query(
"SELECT pmp FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = ".((int) $id).
" AND entity = ".(
int) $conf->entity);
2405 if ($this->db->num_rows($checkPMPPerEntity)>0) {
2406 $separatedEntityPMP =
true;
2410 $separatedStock =
true;
2411 if (isset($mc->sharings[
'stock']) && !empty($mc->sharings[
'stock'])) {
2412 $visibleWarehousesEntities .=
"," . implode(
",", $mc->sharings[
'stock']);
2415 if ($separatedEntityPMP) {
2416 $sql .=
" ppe.pmp,";
2420 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2421 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf,";
2422 if ($separatedStock) {
2423 $sql .=
" SUM(sp.reel) as stock";
2427 $sql .=
" FROM ".$this->db->prefix().
"product as p";
2428 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) || $separatedEntityPMP) {
2429 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
2431 if ($separatedStock) {
2432 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"product_stock as sp ON sp.fk_product = p.rowid AND sp.fk_entrepot IN (SELECT rowid FROM ".$this->db->prefix().
"entrepot WHERE entity IN (".$this->db->sanitize($visibleWarehousesEntities).
"))";
2436 $sql .=
" WHERE p.rowid = ".((int) $id);
2438 $sql .=
" WHERE p.entity IN (".getEntity($this->element).
")";
2440 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
2441 } elseif ($ref_ext) {
2442 $sql .=
" AND p.ref_ext = '".$this->db->escape($ref_ext).
"'";
2443 } elseif ($barcode) {
2444 $sql .=
" AND p.barcode = '".$this->db->escape($barcode).
"'";
2447 if ($separatedStock) {
2448 $sql .=
" GROUP BY p.rowid, p.ref, p.ref_ext, p.label, p.description, p.url, p.note_public, p.note, p.customcode, p.fk_country, p.fk_state, p.lifetime, p.qc_frequency, p.price, p.price_ttc,";
2449 $sql .=
" p.price_min, p.price_min_ttc, p.price_base_type, p.cost_price, p.default_vat_code, p.tva_tx, p.recuperableonly, p.localtax1_tx, p.localtax2_tx, p.localtax1_type, p.localtax2_type, p.tosell,";
2450 $sql .=
" p.tobuy, p.fk_product_type, p.duration, p.fk_default_warehouse, p.fk_default_workstation, p.seuil_stock_alerte, p.canvas, p.net_measure, p.net_measure_units, p.weight, p.weight_units,";
2451 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2452 $sql .=
" p.surface, p.surface_units, p.volume, p.volume_units, p.barcode, p.fk_barcode_type, p.finished,";
2453 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2454 $sql .=
" p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export, p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
2456 $sql .=
" ppe.accountancy_code_buy, ppe.accountancy_code_buy_intra, ppe.accountancy_code_buy_export, ppe.accountancy_code_sell, ppe.accountancy_code_sell_intra, ppe.accountancy_code_sell_export,";
2458 if ($separatedEntityPMP) {
2459 $sql .=
" ppe.pmp,";
2463 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2464 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf";
2465 if (!$separatedStock) {
2466 $sql .=
", p.stock";
2470 $resql = $this->db->query(
$sql);
2472 unset($this->oldcopy);
2474 if ($this->db->num_rows($resql) > 0) {
2475 $obj = $this->db->fetch_object($resql);
2477 $this->
id = $obj->rowid;
2478 $this->
ref = $obj->ref;
2479 $this->ref_ext = $obj->ref_ext;
2480 $this->label = $obj->label;
2482 $this->url = $obj->url;
2483 $this->note_public = $obj->note_public;
2484 $this->note_private = $obj->note_private;
2485 $this->note = $obj->note_private;
2487 $this->
type = $obj->fk_product_type;
2488 $this->status = $obj->tosell;
2489 $this->status_buy = $obj->tobuy;
2490 $this->status_batch = $obj->tobatch;
2491 $this->batch_mask = $obj->batch_mask;
2493 $this->customcode = $obj->customcode;
2494 $this->country_id = $obj->fk_country;
2495 $this->country_code =
getCountry($this->country_id, 2, $this->db);
2496 $this->state_id = $obj->fk_state;
2497 $this->lifetime = $obj->lifetime;
2498 $this->qc_frequency = $obj->qc_frequency;
2499 $this->
price = $obj->price;
2500 $this->price_ttc = $obj->price_ttc;
2501 $this->price_min = $obj->price_min;
2502 $this->price_min_ttc = $obj->price_min_ttc;
2503 $this->price_base_type = $obj->price_base_type;
2504 $this->cost_price = $obj->cost_price;
2505 $this->default_vat_code = $obj->default_vat_code;
2506 $this->tva_tx = $obj->tva_tx;
2508 $this->tva_npr = $obj->tva_npr;
2509 $this->recuperableonly = $obj->tva_npr;
2511 $this->localtax1_tx = $obj->localtax1_tx;
2512 $this->localtax2_tx = $obj->localtax2_tx;
2513 $this->localtax1_type = $obj->localtax1_type;
2514 $this->localtax2_type = $obj->localtax2_type;
2516 $this->finished = $obj->finished;
2517 $this->fk_default_bom = $obj->fk_default_bom;
2519 $this->duration = $obj->duration;
2520 $this->duration_value = substr($obj->duration, 0,
dol_strlen($obj->duration) - 1);
2521 $this->duration_unit = substr($obj->duration, -1);
2522 $this->canvas = $obj->canvas;
2523 $this->net_measure = $obj->net_measure;
2524 $this->net_measure_units = $obj->net_measure_units;
2525 $this->weight = $obj->weight;
2526 $this->weight_units = $obj->weight_units;
2527 $this->length = $obj->length;
2528 $this->length_units = $obj->length_units;
2529 $this->width = $obj->width;
2530 $this->width_units = $obj->width_units;
2531 $this->height = $obj->height;
2532 $this->height_units = $obj->height_units;
2534 $this->surface = $obj->surface;
2535 $this->surface_units = $obj->surface_units;
2536 $this->volume = $obj->volume;
2537 $this->volume_units = $obj->volume_units;
2538 $this->barcode = $obj->barcode;
2539 $this->barcode_type = $obj->fk_barcode_type;
2541 $this->accountancy_code_buy = $obj->accountancy_code_buy;
2542 $this->accountancy_code_buy_intra = $obj->accountancy_code_buy_intra;
2543 $this->accountancy_code_buy_export = $obj->accountancy_code_buy_export;
2544 $this->accountancy_code_sell = $obj->accountancy_code_sell;
2545 $this->accountancy_code_sell_intra = $obj->accountancy_code_sell_intra;
2546 $this->accountancy_code_sell_export = $obj->accountancy_code_sell_export;
2548 $this->fk_default_warehouse = $obj->fk_default_warehouse;
2549 $this->fk_default_workstation = $obj->fk_default_workstation;
2550 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
2551 $this->desiredstock = $obj->desiredstock;
2552 $this->stock_reel = $obj->stock;
2553 $this->pmp = $obj->pmp;
2555 $this->date_creation = $obj->datec;
2556 $this->date_modification = $obj->tms;
2557 $this->import_key = $obj->import_key;
2558 $this->entity = $obj->entity;
2560 $this->ref_ext = $obj->ref_ext;
2561 $this->fk_price_expression = $obj->fk_price_expression;
2562 $this->fk_unit = $obj->fk_unit;
2563 $this->price_autogen = $obj->price_autogen;
2564 $this->model_pdf = $obj->model_pdf;
2566 $this->mandatory_period = $obj->mandatory_period;
2568 $this->db->free($resql);
2579 if (!empty($conf->global->PRODUIT_MULTIPRICES) && empty($ignore_price_load)) {
2580 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2581 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2582 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2583 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2584 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2585 $sql .=
" AND price_level=".((int) $i);
2586 $sql .=
" AND fk_product = ".((int) $this->
id);
2587 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2589 $resql = $this->db->query(
$sql);
2591 $result = $this->db->fetch_array($resql);
2593 $this->multiprices[$i] = $result ? $result[
"price"] :
null;
2594 $this->multiprices_ttc[$i] = $result ? $result[
"price_ttc"] :
null;
2595 $this->multiprices_min[$i] = $result ? $result[
"price_min"] :
null;
2596 $this->multiprices_min_ttc[$i] = $result ? $result[
"price_min_ttc"] :
null;
2597 $this->multiprices_base_type[$i] = $result ? $result[
"price_base_type"] :
null;
2599 $this->multiprices_tva_tx[$i] = $result ? $result[
"tva_tx"].($result ?
' ('.$result[
'default_vat_code'].
')' :
'') :
null;
2600 $this->multiprices_recuperableonly[$i] = $result ? $result[
"recuperableonly"] :
null;
2638 $this->error = $this->db->lasterror;
2642 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && empty($ignore_price_load)) {
2644 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) && empty($ignore_price_load)) {
2645 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2646 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
2647 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2648 $sql .=
" WHERE fk_product = ".((int) $this->
id);
2649 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2651 $resql = $this->db->query(
$sql);
2653 $result = $this->db->fetch_array($resql);
2656 $this->prices_by_qty[0] = $result[
"price_by_qty"];
2657 $this->prices_by_qty_id[0] = $result[
"rowid"];
2659 if ($this->prices_by_qty[0] == 1) {
2660 $sql =
"SELECT rowid,price, unitprice, quantity, remise_percent, remise, remise, price_base_type";
2661 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2662 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[0]);
2663 $sql .=
" ORDER BY quantity ASC";
2664 $resultat = array();
2665 $resql = $this->db->query(
$sql);
2668 while ($result = $this->db->fetch_array($resql)) {
2669 $resultat[$ii] = array();
2670 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2671 $resultat[$ii][
"price"] = $result[
"price"];
2672 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2673 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2674 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2676 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2679 $this->prices_by_qty_list[0] = $resultat;
2681 $this->error = $this->db->lasterror;
2686 $this->error = $this->db->lasterror;
2689 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES) && empty($ignore_price_load)) {
2690 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2691 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2692 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2693 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2694 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2695 $sql .=
" AND price_level=".((int) $i);
2696 $sql .=
" AND fk_product = ".((int) $this->
id);
2697 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2699 $resql = $this->db->query(
$sql);
2701 $result = $this->db->fetch_array($resql);
2703 $this->multiprices[$i] = $result[
"price"];
2704 $this->multiprices_ttc[$i] = $result[
"price_ttc"];
2705 $this->multiprices_min[$i] = $result[
"price_min"];
2706 $this->multiprices_min_ttc[$i] = $result[
"price_min_ttc"];
2707 $this->multiprices_base_type[$i] = $result[
"price_base_type"];
2709 $this->multiprices_tva_tx[$i] = $result[
"tva_tx"];
2710 $this->multiprices_recuperableonly[$i] = $result[
"recuperableonly"];
2713 $this->prices_by_qty[$i] = $result[
"price_by_qty"];
2714 $this->prices_by_qty_id[$i] = $result[
"rowid"];
2716 if ($this->prices_by_qty[$i] == 1) {
2717 $sql =
"SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type";
2718 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2719 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[$i]);
2720 $sql .=
" ORDER BY quantity ASC";
2721 $resultat = array();
2722 $resql = $this->db->query(
$sql);
2725 while ($result = $this->db->fetch_array($resql)) {
2726 $resultat[$ii] = array();
2727 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2728 $resultat[$ii][
"price"] = $result[
"price"];
2729 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2730 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2731 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2732 $resultat[$ii][
"remise"] = $result[
"remise"];
2733 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2736 $this->prices_by_qty_list[$i] = $resultat;
2738 $this->error = $this->db->lasterror;
2743 $this->error = $this->db->lasterror;
2749 if (
isModEnabled(
'dynamicprices') && !empty($this->fk_price_expression) && empty($ignore_expression)) {
2750 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2752 $price_result = $priceparser->parseProduct($this);
2753 if ($price_result >= 0) {
2754 $this->
price = $price_result;
2756 $this->price_ttc =
price2num($this->
price) * (1 + ($this->tva_tx / 100));
2757 $this->price_ttc =
price2num($this->price_ttc,
'MU');
2763 $this->stock_warehouse = array();
2770 $this->error = $this->db->lasterror();
2785 global $user, $hookmanager, $action;
2789 foreach (array(
'toconsume',
'consumed',
'toproduce',
'produced') as $role) {
2790 $this->stats_mo[
'customers_'.$role] = 0;
2791 $this->stats_mo[
'nb_'.$role] = 0;
2792 $this->stats_mo[
'qty_'.$role] = 0;
2794 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
2795 $sql .=
" SUM(mp.qty) as qty";
2796 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as c";
2797 $sql .=
" INNER JOIN ".$this->db->prefix().
"mrp_production as mp ON mp.fk_mo=c.rowid";
2798 if (empty($user->rights->societe->client->voir) && !$socid) {
2799 $sql .=
"INNER JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON sc.fk_soc=c.fk_soc AND sc.fk_user = ".((int) $user->id);
2802 $sql .=
" c.entity IN (".getEntity(
'mo').
")";
2804 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
2805 $sql .=
" AND mp.role ='".$this->db->escape($role).
"'";
2807 $sql .=
" AND c.fk_soc = ".((int) $socid);
2810 $result = $this->db->query(
$sql);
2812 $obj = $this->db->fetch_object($result);
2813 $this->stats_mo[
'customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
2814 $this->stats_mo[
'nb_'.$role] = $obj->nb ? $obj->nb : 0;
2815 $this->stats_mo[
'qty_'.$role] = $obj->qty ?
price2num($obj->qty,
'MS') : 0;
2817 $this->error = $this->db->error();
2822 if (!empty($error)) {
2826 $parameters = array(
'socid' => $socid);
2827 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2829 $this->stats_mo = $hookmanager->resArray[
'stats_mo'];
2845 global $user, $hookmanager, $action;
2849 $this->stats_bom[
'nb_toproduce'] = 0;
2850 $this->stats_bom[
'nb_toconsume'] = 0;
2851 $this->stats_bom[
'qty_toproduce'] = 0;
2852 $this->stats_bom[
'qty_toconsume'] = 0;
2854 $sql =
"SELECT COUNT(DISTINCT b.rowid) as nb_toproduce,";
2855 $sql .=
" SUM(b.qty) as qty_toproduce";
2856 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2857 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2859 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2860 $sql .=
" AND b.fk_product =".((int) $this->
id);
2861 $sql .=
" GROUP BY b.rowid";
2863 $result = $this->db->query(
$sql);
2865 $obj = $this->db->fetch_object($result);
2866 $this->stats_bom[
'nb_toproduce'] = !empty($obj->nb_toproduce) ? $obj->nb_toproduce : 0;
2867 $this->stats_bom[
'qty_toproduce'] = !empty($obj->qty_toproduce) ?
price2num($obj->qty_toproduce) : 0;
2869 $this->error = $this->db->error();
2873 $sql =
"SELECT COUNT(DISTINCT bl.rowid) as nb_toconsume,";
2874 $sql .=
" SUM(bl.qty) as qty_toconsume";
2875 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2876 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2878 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2879 $sql .=
" AND bl.fk_product =".((int) $this->
id);
2881 $result = $this->db->query(
$sql);
2883 $obj = $this->db->fetch_object($result);
2884 $this->stats_bom[
'nb_toconsume'] = !empty($obj->nb_toconsume) ? $obj->nb_toconsume : 0;
2885 $this->stats_bom[
'qty_toconsume'] = !empty($obj->qty_toconsume) ?
price2num($obj->qty_toconsume) : 0;
2887 $this->error = $this->db->error();
2891 if (!empty($error)) {
2895 $parameters = array(
'socid' => $socid);
2896 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2898 $this->stats_bom = $hookmanager->resArray[
'stats_bom'];
2914 global $conf, $user, $hookmanager, $action;
2916 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb,";
2917 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
2918 $sql .=
" FROM ".$this->db->prefix().
"propaldet as pd";
2919 $sql .=
", ".$this->db->prefix().
"propal as p";
2920 $sql .=
", ".$this->db->prefix().
"societe as s";
2921 if (empty($user->rights->societe->client->voir) && !$socid) {
2922 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
2924 $sql .=
" WHERE p.rowid = pd.fk_propal";
2925 $sql .=
" AND p.fk_soc = s.rowid";
2926 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
2927 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
2928 if (empty($user->rights->societe->client->voir) && !$socid) {
2929 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2933 $sql .=
" AND p.fk_soc = ".((int) $socid);
2936 $result = $this->db->query(
$sql);
2938 $obj = $this->db->fetch_object($result);
2939 $this->stats_propale[
'customers'] = $obj->nb_customers;
2940 $this->stats_propale[
'nb'] = $obj->nb;
2941 $this->stats_propale[
'rows'] = $obj->nb_rows;
2942 $this->stats_propale[
'qty'] = $obj->qty ? $obj->qty : 0;
2945 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
2947 if (is_array($TFather) && !empty($TFather)) {
2948 foreach ($TFather as &$fatherData) {
2949 $pFather =
new Product($this->db);
2950 $pFather->id = $fatherData[
'id'];
2951 $qtyCoef = $fatherData[
'qty'];
2953 if ($fatherData[
'incdec']) {
2954 $pFather->load_stats_propale($socid);
2956 $this->stats_propale[
'customers'] += $pFather->stats_propale[
'customers'];
2957 $this->stats_propale[
'nb'] += $pFather->stats_propale[
'nb'];
2958 $this->stats_propale[
'rows'] += $pFather->stats_propale[
'rows'];
2959 $this->stats_propale[
'qty'] += $pFather->stats_propale[
'qty'] * $qtyCoef;
2965 $parameters = array(
'socid' => $socid);
2966 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerProposal', $parameters, $this, $action);
2968 $this->stats_propale = $hookmanager->resArray[
'stats_propale'];
2973 $this->error = $this->db->error();
2989 global $conf, $user, $hookmanager, $action;
2991 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_suppliers, COUNT(DISTINCT p.rowid) as nb,";
2992 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
2993 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as pd";
2994 $sql .=
", ".$this->db->prefix().
"supplier_proposal as p";
2995 $sql .=
", ".$this->db->prefix().
"societe as s";
2996 if (empty($user->rights->societe->client->voir) && !$socid) {
2997 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
2999 $sql .=
" WHERE p.rowid = pd.fk_supplier_proposal";
3000 $sql .=
" AND p.fk_soc = s.rowid";
3001 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
3002 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
3003 if (empty($user->rights->societe->client->voir) && !$socid) {
3004 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3008 $sql .=
" AND p.fk_soc = ".((int) $socid);
3011 $result = $this->db->query(
$sql);
3013 $obj = $this->db->fetch_object($result);
3014 $this->stats_proposal_supplier[
'suppliers'] = $obj->nb_suppliers;
3015 $this->stats_proposal_supplier[
'nb'] = $obj->nb;
3016 $this->stats_proposal_supplier[
'rows'] = $obj->nb_rows;
3017 $this->stats_proposal_supplier[
'qty'] = $obj->qty ? $obj->qty : 0;
3019 $parameters = array(
'socid' => $socid);
3020 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierProposal', $parameters, $this, $action);
3022 $this->stats_proposal_supplier = $hookmanager->resArray[
'stats_proposal_supplier'];
3027 $this->error = $this->db->error();
3045 global $conf, $user, $hookmanager, $action;
3047 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3048 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3049 $sql .=
" FROM ".$this->db->prefix().
"commandedet as cd";
3050 $sql .=
", ".$this->db->prefix().
"commande as c";
3051 $sql .=
", ".$this->db->prefix().
"societe as s";
3052 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3053 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3055 $sql .=
" WHERE c.rowid = cd.fk_commande";
3056 $sql .=
" AND c.fk_soc = s.rowid";
3057 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'commande').
")";
3058 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3059 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3060 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3063 $sql .=
" AND c.fk_soc = ".((int) $socid);
3065 if ($filtrestatut <>
'') {
3066 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3069 $result = $this->db->query(
$sql);
3071 $obj = $this->db->fetch_object($result);
3072 $this->stats_commande[
'customers'] = $obj->nb_customers;
3073 $this->stats_commande[
'nb'] = $obj->nb;
3074 $this->stats_commande[
'rows'] = $obj->nb_rows;
3075 $this->stats_commande[
'qty'] = $obj->qty ? $obj->qty : 0;
3078 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3080 if (is_array($TFather) && !empty($TFather)) {
3081 foreach ($TFather as &$fatherData) {
3082 $pFather =
new Product($this->db);
3083 $pFather->id = $fatherData[
'id'];
3084 $qtyCoef = $fatherData[
'qty'];
3086 if ($fatherData[
'incdec']) {
3087 $pFather->load_stats_commande($socid, $filtrestatut);
3089 $this->stats_commande[
'customers'] += $pFather->stats_commande[
'customers'];
3090 $this->stats_commande[
'nb'] += $pFather->stats_commande[
'nb'];
3091 $this->stats_commande[
'rows'] += $pFather->stats_commande[
'rows'];
3092 $this->stats_commande[
'qty'] += $pFather->stats_commande[
'qty'] * $qtyCoef;
3100 if ($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
3101 if (!empty($conf->global->DECREASE_ONLY_UNINVOICEDPRODUCTS)) {
3104 $sql =
"SELECT sum(fd.qty) as count FROM ".$this->db->prefix().
"facturedet as fd ";
3105 $sql .=
" JOIN ".$this->db->prefix().
"facture as f ON fd.fk_facture = f.rowid ";
3106 $sql .=
" JOIN ".$this->db->prefix().
"element_element as el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))";
3107 $sql .=
" JOIN ".$this->db->prefix().
"commande as c ON el.fk_source = c.rowid ";
3108 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND c.facture = 0 AND fd.fk_product = ".((int) $this->
id);
3109 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3110 $resql = $this->db->query(
$sql);
3112 if ($this->db->num_rows($resql) > 0) {
3113 $obj = $this->db->fetch_object($resql);
3114 $adeduire += $obj->count;
3118 $this->stats_commande[
'qty'] -= $adeduire;
3121 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3125 $sql =
'SELECT sum(fd.qty) as count FROM '.MAIN_DB_PREFIX.
'facturedet as fd ';
3126 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture as f ON fd.fk_facture = f.rowid ';
3127 $sql .=
' JOIN '.MAIN_DB_PREFIX.
"element_element as el ON ((el.fk_target = f.rowid AND el.targettype = 'facture' AND sourcetype = 'commande') OR (el.fk_source = f.rowid AND el.targettype = 'commande' AND sourcetype = 'facture'))";
3128 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'commande as c ON el.fk_source = c.rowid ';
3129 $sql .=
' WHERE c.fk_statut IN ('.$this->db->sanitize($filtrestatut).
') AND f.fk_statut > '.
Facture::STATUS_DRAFT.
' AND fd.fk_product = '.((int) $this->
id);
3130 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3131 $resql = $this->db->query(
$sql);
3133 if ($this->db->num_rows($resql) > 0) {
3134 $obj = $this->db->fetch_object($resql);
3135 $adeduire += $obj->count;
3139 $this->stats_commande[
'qty'] -= $adeduire;
3143 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3144 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerOrder', $parameters, $this, $action);
3146 $this->stats_commande = $hookmanager->resArray[
'stats_commande'];
3150 $this->error = $this->db->error();
3168 global $conf, $user, $hookmanager, $action;
3170 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb,";
3171 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3172 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as cd";
3173 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as c";
3174 $sql .=
", ".$this->db->prefix().
"societe as s";
3175 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3176 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3178 $sql .=
" WHERE c.rowid = cd.fk_commande";
3179 $sql .=
" AND c.fk_soc = s.rowid";
3180 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3181 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3182 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3183 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3186 $sql .=
" AND c.fk_soc = ".((int) $socid);
3188 if ($filtrestatut !=
'') {
3189 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3191 if (!empty($dateofvirtualstock)) {
3192 $sql .=
" AND c.date_livraison <= '".$this->db->idate($dateofvirtualstock).
"'";
3195 $result = $this->db->query(
$sql);
3197 $obj = $this->db->fetch_object($result);
3198 $this->stats_commande_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3199 $this->stats_commande_fournisseur[
'nb'] = $obj->nb;
3200 $this->stats_commande_fournisseur[
'rows'] = $obj->nb_rows;
3201 $this->stats_commande_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3203 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3204 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierOrder', $parameters, $this, $action);
3206 $this->stats_commande_fournisseur = $hookmanager->resArray[
'stats_commande_fournisseur'];
3211 $this->error = $this->db->error().
' sql='.
$sql;
3226 public function load_stats_sending($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $filterShipmentStatus =
'')
3229 global $conf, $user, $hookmanager, $action;
3231 $sql =
"SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb,";
3232 $sql .=
" COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
3233 $sql .=
" FROM ".$this->db->prefix().
"expeditiondet as ed";
3234 $sql .=
", ".$this->db->prefix().
"commandedet as cd";
3235 $sql .=
", ".$this->db->prefix().
"commande as c";
3236 $sql .=
", ".$this->db->prefix().
"expedition as e";
3237 $sql .=
", ".$this->db->prefix().
"societe as s";
3238 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3239 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3241 $sql .=
" WHERE e.rowid = ed.fk_expedition";
3242 $sql .=
" AND c.rowid = cd.fk_commande";
3243 $sql .=
" AND e.fk_soc = s.rowid";
3244 $sql .=
" AND e.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'expedition').
")";
3245 $sql .=
" AND ed.fk_origin_line = cd.rowid";
3246 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3247 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3248 $sql .=
" AND e.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3251 $sql .=
" AND e.fk_soc = ".((int) $socid);
3253 if ($filtrestatut <>
'') {
3254 $sql .=
" AND c.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3256 if (!empty($filterShipmentStatus)) {
3257 $sql .=
" AND e.fk_statut IN (".$this->db->sanitize($filterShipmentStatus).
")";
3260 $result = $this->db->query(
$sql);
3262 $obj = $this->db->fetch_object($result);
3263 $this->stats_expedition[
'customers'] = $obj->nb_customers;
3264 $this->stats_expedition[
'nb'] = $obj->nb;
3265 $this->stats_expedition[
'rows'] = $obj->nb_rows;
3266 $this->stats_expedition[
'qty'] = $obj->qty ? $obj->qty : 0;
3269 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3271 if (is_array($TFather) && !empty($TFather)) {
3272 foreach ($TFather as &$fatherData) {
3273 $pFather =
new Product($this->db);
3274 $pFather->id = $fatherData[
'id'];
3275 $qtyCoef = $fatherData[
'qty'];
3277 if ($fatherData[
'incdec']) {
3278 $pFather->load_stats_sending($socid, $filtrestatut, $forVirtualStock);
3280 $this->stats_expedition[
'customers'] += $pFather->stats_expedition[
'customers'];
3281 $this->stats_expedition[
'nb'] += $pFather->stats_expedition[
'nb'];
3282 $this->stats_expedition[
'rows'] += $pFather->stats_expedition[
'rows'];
3283 $this->stats_expedition[
'qty'] += $pFather->stats_expedition[
'qty'] * $qtyCoef;
3289 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock,
'filterShipmentStatus' => $filterShipmentStatus);
3290 $reshook = $hookmanager->executeHooks(
'loadStatsSending', $parameters, $this, $action);
3292 $this->stats_expedition = $hookmanager->resArray[
'stats_expedition'];
3297 $this->error = $this->db->error();
3312 public function load_stats_reception($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $dateofvirtualstock =
null)
3315 global $conf, $user, $hookmanager, $action;
3317 $sql =
"SELECT COUNT(DISTINCT cf.fk_soc) as nb_suppliers, COUNT(DISTINCT cf.rowid) as nb,";
3318 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3319 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur_dispatch as fd";
3320 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as cf";
3321 $sql .=
", ".$this->db->prefix().
"societe as s";
3322 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3323 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3325 $sql .=
" WHERE cf.rowid = fd.fk_commande";
3326 $sql .=
" AND cf.fk_soc = s.rowid";
3327 $sql .=
" AND cf.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3328 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3329 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3330 $sql .=
" AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3333 $sql .=
" AND cf.fk_soc = ".((int) $socid);
3335 if ($filtrestatut <>
'') {
3336 $sql .=
" AND cf.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3338 if (!empty($dateofvirtualstock)) {
3339 $sql .=
" AND fd.datec <= '".$this->db->idate($dateofvirtualstock).
"'";
3342 $result = $this->db->query(
$sql);
3344 $obj = $this->db->fetch_object($result);
3345 $this->stats_reception[
'suppliers'] = $obj->nb_suppliers;
3346 $this->stats_reception[
'nb'] = $obj->nb;
3347 $this->stats_reception[
'rows'] = $obj->nb_rows;
3348 $this->stats_reception[
'qty'] = $obj->qty ? $obj->qty : 0;
3350 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3351 $reshook = $hookmanager->executeHooks(
'loadStatsReception', $parameters, $this, $action);
3353 $this->stats_reception = $hookmanager->resArray[
'stats_reception'];
3358 $this->error = $this->db->error();
3376 global $conf, $user, $hookmanager, $action;
3378 $sql =
"SELECT COUNT(DISTINCT m.fk_soc) as nb_customers, COUNT(DISTINCT m.rowid) as nb,";
3379 $sql .=
" COUNT(mp.rowid) as nb_rows, SUM(mp.qty) as qty, role";
3380 $sql .=
" FROM ".$this->db->prefix().
"mrp_production as mp";
3381 $sql .=
", ".$this->db->prefix().
"mrp_mo as m";
3382 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON s.rowid = m.fk_soc";
3383 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3384 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3386 $sql .=
" WHERE m.rowid = mp.fk_mo";
3387 $sql .=
" AND m.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'mrp').
")";
3388 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
3389 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3390 $sql .=
" AND m.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3393 $sql .=
" AND m.fk_soc = ".((int) $socid);
3395 if ($filtrestatut <>
'') {
3396 $sql .=
" AND m.status IN (".$this->db->sanitize($filtrestatut).
")";
3398 if (!empty($dateofvirtualstock)) {
3399 $sql .=
" AND m.date_valid <= '".$this->db->idate($dateofvirtualstock).
"'";
3401 $sql .=
" GROUP BY role";
3403 $this->stats_mrptoconsume[
'customers'] = 0;
3404 $this->stats_mrptoconsume[
'nb'] = 0;
3405 $this->stats_mrptoconsume[
'rows'] = 0;
3406 $this->stats_mrptoconsume[
'qty'] = 0;
3407 $this->stats_mrptoproduce[
'customers'] = 0;
3408 $this->stats_mrptoproduce[
'nb'] = 0;
3409 $this->stats_mrptoproduce[
'rows'] = 0;
3410 $this->stats_mrptoproduce[
'qty'] = 0;
3412 $result = $this->db->query(
$sql);
3414 while ($obj = $this->db->fetch_object($result)) {
3415 if ($obj->role ==
'toconsume') {
3416 $this->stats_mrptoconsume[
'customers'] += $obj->nb_customers;
3417 $this->stats_mrptoconsume[
'nb'] += $obj->nb;
3418 $this->stats_mrptoconsume[
'rows'] += $obj->nb_rows;
3419 $this->stats_mrptoconsume[
'qty'] += ($obj->qty ? $obj->qty : 0);
3421 if ($obj->role ==
'consumed') {
3425 $this->stats_mrptoconsume[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3427 if ($obj->role ==
'toproduce') {
3428 $this->stats_mrptoproduce[
'customers'] += $obj->nb_customers;
3429 $this->stats_mrptoproduce[
'nb'] += $obj->nb;
3430 $this->stats_mrptoproduce[
'rows'] += $obj->nb_rows;
3431 $this->stats_mrptoproduce[
'qty'] += ($obj->qty ? $obj->qty : 0);
3433 if ($obj->role ==
'produced') {
3437 $this->stats_mrptoproduce[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3442 if ($this->stats_mrptoconsume[
'qty'] < 0) {
3443 $this->stats_mrptoconsume[
'qty'] = 0;
3445 if ($this->stats_mrptoproduce[
'qty'] < 0) {
3446 $this->stats_mrptoproduce[
'qty'] = 0;
3449 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3450 $reshook = $hookmanager->executeHooks(
'loadStatsInProduction', $parameters, $this, $action);
3452 $this->stats_mrptoproduce = $hookmanager->resArray[
'stats_mrptoproduce'];
3457 $this->error = $this->db->error();
3472 global $conf, $user, $hookmanager, $action;
3474 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3475 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3476 $sql .=
" FROM ".$this->db->prefix().
"contratdet as cd";
3477 $sql .=
", ".$this->db->prefix().
"contrat as c";
3478 $sql .=
", ".$this->db->prefix().
"societe as s";
3479 if (empty($user->rights->societe->client->voir) && !$socid) {
3480 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3482 $sql .=
" WHERE c.rowid = cd.fk_contrat";
3483 $sql .=
" AND c.fk_soc = s.rowid";
3484 $sql .=
" AND c.entity IN (".getEntity(
'contract').
")";
3485 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3486 if (empty($user->rights->societe->client->voir) && !$socid) {
3487 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3491 $sql .=
" AND c.fk_soc = ".((int) $socid);
3494 $result = $this->db->query(
$sql);
3496 $obj = $this->db->fetch_object($result);
3497 $this->stats_contrat[
'customers'] = $obj->nb_customers;
3498 $this->stats_contrat[
'nb'] = $obj->nb;
3499 $this->stats_contrat[
'rows'] = $obj->nb_rows;
3500 $this->stats_contrat[
'qty'] = $obj->qty ? $obj->qty : 0;
3503 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3505 if (is_array($TFather) && !empty($TFather)) {
3506 foreach ($TFather as &$fatherData) {
3507 $pFather =
new Product($this->db);
3508 $pFather->id = $fatherData[
'id'];
3509 $qtyCoef = $fatherData[
'qty'];
3511 if ($fatherData[
'incdec']) {
3512 $pFather->load_stats_contrat($socid);
3514 $this->stats_contrat[
'customers'] += $pFather->stats_contrat[
'customers'];
3515 $this->stats_contrat[
'nb'] += $pFather->stats_contrat[
'nb'];
3516 $this->stats_contrat[
'rows'] += $pFather->stats_contrat[
'rows'];
3517 $this->stats_contrat[
'qty'] += $pFather->stats_contrat[
'qty'] * $qtyCoef;
3523 $parameters = array(
'socid' => $socid);
3524 $reshook = $hookmanager->executeHooks(
'loadStatsContract', $parameters, $this, $action);
3526 $this->stats_contrat = $hookmanager->resArray[
'stats_contrat'];
3531 $this->error = $this->db->error().
' sql='.
$sql;
3546 global $db, $conf, $user, $hookmanager, $action;
3548 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3549 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(".$this->db->ifsql(
'f.type != 2',
'fd.qty',
'fd.qty * -1').
") as qty";
3550 $sql .=
" FROM ".$this->db->prefix().
"facturedet as fd";
3551 $sql .=
", ".$this->db->prefix().
"facture as f";
3552 $sql .=
", ".$this->db->prefix().
"societe as s";
3553 if (empty($user->rights->societe->client->voir) && !$socid) {
3554 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3556 $sql .=
" WHERE f.rowid = fd.fk_facture";
3557 $sql .=
" AND f.fk_soc = s.rowid";
3558 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3559 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3560 if (empty($user->rights->societe->client->voir) && !$socid) {
3561 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3565 $sql .=
" AND f.fk_soc = ".((int) $socid);
3568 $result = $this->db->query(
$sql);
3570 $obj = $this->db->fetch_object($result);
3571 $this->stats_facture[
'customers'] = $obj->nb_customers;
3572 $this->stats_facture[
'nb'] = $obj->nb;
3573 $this->stats_facture[
'rows'] = $obj->nb_rows;
3574 $this->stats_facture[
'qty'] = $obj->qty ? $obj->qty : 0;
3577 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3579 if (is_array($TFather) && !empty($TFather)) {
3580 foreach ($TFather as &$fatherData) {
3581 $pFather =
new Product($this->db);
3582 $pFather->id = $fatherData[
'id'];
3583 $qtyCoef = $fatherData[
'qty'];
3585 if ($fatherData[
'incdec']) {
3586 $pFather->load_stats_facture($socid);
3588 $this->stats_facture[
'customers'] += $pFather->stats_facture[
'customers'];
3589 $this->stats_facture[
'nb'] += $pFather->stats_facture[
'nb'];
3590 $this->stats_facture[
'rows'] += $pFather->stats_facture[
'rows'];
3591 $this->stats_facture[
'qty'] += $pFather->stats_facture[
'qty'] * $qtyCoef;
3597 $parameters = array(
'socid' => $socid);
3598 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoice', $parameters, $this, $action);
3600 $this->stats_facture = $hookmanager->resArray[
'stats_facture'];
3605 $this->error = $this->db->error();
3621 global $db, $conf, $user, $hookmanager;
3623 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3624 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3625 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet_rec as fd";
3626 $sql .=
", ".MAIN_DB_PREFIX.
"facture_rec as f";
3627 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
3628 if (empty($user->rights->societe->client->voir) && !$socid) {
3629 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
3631 $sql .=
" WHERE f.rowid = fd.fk_facture";
3632 $sql .=
" AND f.fk_soc = s.rowid";
3633 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3634 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3635 if (empty($user->rights->societe->client->voir) && !$socid) {
3636 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3640 $sql .=
" AND f.fk_soc = ".((int) $socid);
3643 $result = $this->db->query(
$sql);
3645 $obj = $this->db->fetch_object($result);
3646 $this->stats_facturerec[
'customers'] = $obj->nb_customers;
3647 $this->stats_facturerec[
'nb'] = $obj->nb;
3648 $this->stats_facturerec[
'rows'] = $obj->nb_rows;
3649 $this->stats_facturerec[
'qty'] = $obj->qty ? $obj->qty : 0;
3652 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3654 if (is_array($TFather) && !empty($TFather)) {
3655 foreach ($TFather as &$fatherData) {
3656 $pFather =
new Product($this->db);
3657 $pFather->id = $fatherData[
'id'];
3658 $qtyCoef = $fatherData[
'qty'];
3660 if ($fatherData[
'incdec']) {
3661 $pFather->load_stats_facture($socid);
3663 $this->stats_facturerec[
'customers'] += $pFather->stats_facturerec[
'customers'];
3664 $this->stats_facturerec[
'nb'] += $pFather->stats_facturerec[
'nb'];
3665 $this->stats_facturerec[
'rows'] += $pFather->stats_facturerec[
'rows'];
3666 $this->stats_facturerec[
'qty'] += $pFather->stats_facturerec[
'qty'] * $qtyCoef;
3672 $parameters = array(
'socid' => $socid);
3673 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoiceRec', $parameters, $this, $action);
3675 $this->stats_facturerec = $hookmanager->resArray[
'stats_facturerec'];
3680 $this->error = $this->db->error();
3695 global $conf, $user, $hookmanager, $action;
3697 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,";
3698 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3699 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as fd";
3700 $sql .=
", ".$this->db->prefix().
"facture_fourn as f";
3701 $sql .=
", ".$this->db->prefix().
"societe as s";
3702 if (empty($user->rights->societe->client->voir) && !$socid) {
3703 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3705 $sql .=
" WHERE f.rowid = fd.fk_facture_fourn";
3706 $sql .=
" AND f.fk_soc = s.rowid";
3707 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3708 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3709 if (empty($user->rights->societe->client->voir) && !$socid) {
3710 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3714 $sql .=
" AND f.fk_soc = ".((int) $socid);
3717 $result = $this->db->query(
$sql);
3719 $obj = $this->db->fetch_object($result);
3720 $this->stats_facture_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3721 $this->stats_facture_fournisseur[
'nb'] = $obj->nb;
3722 $this->stats_facture_fournisseur[
'rows'] = $obj->nb_rows;
3723 $this->stats_facture_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3725 $parameters = array(
'socid' => $socid);
3726 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierInvoice', $parameters, $this, $action);
3728 $this->stats_facture_fournisseur = $hookmanager->resArray[
'stats_facture_fournisseur'];
3733 $this->error = $this->db->error();
3752 $resql = $this->db->query(
$sql);
3754 $num = $this->db->num_rows($resql);
3757 $arr = $this->db->fetch_array($resql);
3758 $keyfortab = (string) $arr[1];
3760 $keyfortab = substr($keyfortab, -2);
3763 if ($mode ==
'byunit') {
3764 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[0];
3765 } elseif ($mode ==
'bynumber') {
3766 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3767 } elseif ($mode ==
'byamount') {
3768 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3776 $this->error = $this->db->error().
' sql='.
$sql;
3781 $year = strftime(
'%Y', time());
3782 $month = strftime(
'%m', time());
3783 } elseif ($year == -1) {
3792 for ($j = 0; $j < 12; $j++) {
3794 $idx = ucfirst(
dol_trunc(
dol_print_date(
dol_mktime(12, 0, 0, $month, 1, 1970),
"%b"), 1,
'right',
'UTF-8', 1));
3797 $result[$j] = array($idx, isset($tab[$year.$month]) ? $tab[$year.$month] : 0);
3800 $month =
"0".($month - 1);
3802 $month = substr($month, 1);
3810 return array_reverse($result);
3825 public function get_nb_vente($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3831 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3832 if ($mode ==
'bynumber') {
3833 $sql .=
", count(DISTINCT f.rowid)";
3835 $sql .=
", sum(d.total_ht) as total_ht";
3836 $sql .=
" FROM ".$this->db->prefix().
"facturedet as d, ".$this->db->prefix().
"facture as f, ".$this->db->prefix().
"societe as s";
3837 if ($filteronproducttype >= 0) {
3838 $sql .=
", ".$this->db->prefix().
"product as p";
3840 if (empty($user->rights->societe->client->voir) && !$socid) {
3841 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3843 $sql .=
" WHERE f.rowid = d.fk_facture";
3844 if ($this->
id > 0) {
3845 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3847 $sql .=
" AND d.fk_product > 0";
3849 if ($filteronproducttype >= 0) {
3850 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3852 $sql .=
" AND f.fk_soc = s.rowid";
3853 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3854 if (empty($user->rights->societe->client->voir) && !$socid) {
3855 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3858 $sql .=
" AND f.fk_soc = $socid";
3860 $sql .= $morefilter;
3861 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3862 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3879 public function get_nb_achat($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3885 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3886 if ($mode ==
'bynumber') {
3887 $sql .=
", count(DISTINCT f.rowid)";
3889 $sql .=
", sum(d.total_ht) as total_ht";
3890 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as d, ".$this->db->prefix().
"facture_fourn as f, ".$this->db->prefix().
"societe as s";
3891 if ($filteronproducttype >= 0) {
3892 $sql .=
", ".$this->db->prefix().
"product as p";
3894 if (empty($user->rights->societe->client->voir) && !$socid) {
3895 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3897 $sql .=
" WHERE f.rowid = d.fk_facture_fourn";
3898 if ($this->
id > 0) {
3899 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3901 $sql .=
" AND d.fk_product > 0";
3903 if ($filteronproducttype >= 0) {
3904 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3906 $sql .=
" AND f.fk_soc = s.rowid";
3907 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3908 if (empty($user->rights->societe->client->voir) && !$socid) {
3909 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3912 $sql .=
" AND f.fk_soc = $socid";
3914 $sql .= $morefilter;
3915 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3916 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3932 public function get_nb_propal($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3935 global $conf, $user;
3937 $sql =
"SELECT sum(d.qty) as qty, date_format(p.datep, '%Y%m')";
3938 if ($mode ==
'bynumber') {
3939 $sql .=
", count(DISTINCT p.rowid)";
3941 $sql .=
", sum(d.total_ht) as total_ht";
3942 $sql .=
" FROM ".$this->db->prefix().
"propaldet as d, ".$this->db->prefix().
"propal as p, ".$this->db->prefix().
"societe as s";
3943 if ($filteronproducttype >= 0) {
3944 $sql .=
", ".$this->db->prefix().
"product as prod";
3946 if (empty($user->rights->societe->client->voir) && !$socid) {
3947 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3949 $sql .=
" WHERE p.rowid = d.fk_propal";
3950 if ($this->
id > 0) {
3951 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3953 $sql .=
" AND d.fk_product > 0";
3955 if ($filteronproducttype >= 0) {
3956 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
3958 $sql .=
" AND p.fk_soc = s.rowid";
3959 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
3960 if (empty($user->rights->societe->client->voir) && !$socid) {
3961 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3964 $sql .=
" AND p.fk_soc = ".((int) $socid);
3966 $sql .= $morefilter;
3967 $sql .=
" GROUP BY date_format(p.datep,'%Y%m')";
3968 $sql .=
" ORDER BY date_format(p.datep,'%Y%m') DESC";
3990 $sql =
"SELECT sum(d.qty) as qty, date_format(p.date_valid, '%Y%m')";
3991 if ($mode ==
'bynumber') {
3992 $sql .=
", count(DISTINCT p.rowid)";
3994 $sql .=
", sum(d.total_ht) as total_ht";
3995 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as d, ".$this->db->prefix().
"supplier_proposal as p, ".$this->db->prefix().
"societe as s";
3996 if ($filteronproducttype >= 0) {
3997 $sql .=
", ".$this->db->prefix().
"product as prod";
3999 if (empty($user->rights->societe->client->voir) && !$socid) {
4000 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4002 $sql .=
" WHERE p.rowid = d.fk_supplier_proposal";
4003 if ($this->
id > 0) {
4004 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4006 $sql .=
" AND d.fk_product > 0";
4008 if ($filteronproducttype >= 0) {
4009 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
4011 $sql .=
" AND p.fk_soc = s.rowid";
4012 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
4013 if (empty($user->rights->societe->client->voir) && !$socid) {
4014 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4017 $sql .=
" AND p.fk_soc = ".((int) $socid);
4019 $sql .= $morefilter;
4020 $sql .=
" GROUP BY date_format(p.date_valid,'%Y%m')";
4021 $sql .=
" ORDER BY date_format(p.date_valid,'%Y%m') DESC";
4037 public function get_nb_order($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4040 global $conf, $user;
4042 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4043 if ($mode ==
'bynumber') {
4044 $sql .=
", count(DISTINCT c.rowid)";
4046 $sql .=
", sum(d.total_ht) as total_ht";
4047 $sql .=
" FROM ".$this->db->prefix().
"commandedet as d, ".$this->db->prefix().
"commande as c, ".$this->db->prefix().
"societe as s";
4048 if ($filteronproducttype >= 0) {
4049 $sql .=
", ".$this->db->prefix().
"product as p";
4051 if (empty($user->rights->societe->client->voir) && !$socid) {
4052 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4054 $sql .=
" WHERE c.rowid = d.fk_commande";
4055 if ($this->
id > 0) {
4056 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4058 $sql .=
" AND d.fk_product > 0";
4060 if ($filteronproducttype >= 0) {
4061 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4063 $sql .=
" AND c.fk_soc = s.rowid";
4064 $sql .=
" AND c.entity IN (".getEntity(
'commande').
")";
4065 if (empty($user->rights->societe->client->voir) && !$socid) {
4066 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4069 $sql .=
" AND c.fk_soc = ".((int) $socid);
4071 $sql .= $morefilter;
4072 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4073 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4092 global $conf, $user;
4094 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4095 if ($mode ==
'bynumber') {
4096 $sql .=
", count(DISTINCT c.rowid)";
4098 $sql .=
", sum(d.total_ht) as total_ht";
4099 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as d, ".$this->db->prefix().
"commande_fournisseur as c, ".$this->db->prefix().
"societe as s";
4100 if ($filteronproducttype >= 0) {
4101 $sql .=
", ".$this->db->prefix().
"product as p";
4103 if (empty($user->rights->societe->client->voir) && !$socid) {
4104 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4106 $sql .=
" WHERE c.rowid = d.fk_commande";
4107 if ($this->
id > 0) {
4108 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4110 $sql .=
" AND d.fk_product > 0";
4112 if ($filteronproducttype >= 0) {
4113 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4115 $sql .=
" AND c.fk_soc = s.rowid";
4116 $sql .=
" AND c.entity IN (".getEntity(
'supplier_order').
")";
4117 if (empty($user->rights->societe->client->voir) && !$socid) {
4118 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4121 $sql .=
" AND c.fk_soc = ".((int) $socid);
4123 $sql .= $morefilter;
4124 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4125 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4141 public function get_nb_contract($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4144 global $conf, $user;
4146 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_contrat, '%Y%m')";
4147 if ($mode ==
'bynumber') {
4148 $sql .=
", count(DISTINCT c.rowid)";
4150 $sql .=
", sum(d.total_ht) as total_ht";
4151 $sql .=
" FROM ".$this->db->prefix().
"contratdet as d, ".$this->db->prefix().
"contrat as c, ".$this->db->prefix().
"societe as s";
4152 if ($filteronproducttype >= 0) {
4153 $sql .=
", ".$this->db->prefix().
"product as p";
4155 if (empty($user->rights->societe->client->voir) && !$socid) {
4156 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4159 $sql .=
" WHERE c.entity IN (".getEntity(
'contract').
")";
4160 $sql .=
" AND c.rowid = d.fk_contrat";
4162 if ($this->
id > 0) {
4163 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4165 $sql .=
" AND d.fk_product > 0";
4167 if ($filteronproducttype >= 0) {
4168 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4170 $sql .=
" AND c.fk_soc = s.rowid";
4172 if (empty($user->rights->societe->client->voir) && !$socid) {
4173 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4176 $sql .=
" AND c.fk_soc = ".((int) $socid);
4178 $sql .= $morefilter;
4179 $sql .=
" GROUP BY date_format(c.date_contrat,'%Y%m')";
4180 $sql .=
" ORDER BY date_format(c.date_contrat,'%Y%m') DESC";
4196 public function get_nb_mos($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4199 global $conf, $user;
4201 $sql =
"SELECT sum(d.qty), date_format(d.date_valid, '%Y%m')";
4202 if ($mode ==
'bynumber') {
4203 $sql .=
", count(DISTINCT d.rowid)";
4205 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as d LEFT JOIN ".$this->db->prefix().
"societe as s ON d.fk_soc = s.rowid";
4206 if ($filteronproducttype >= 0) {
4207 $sql .=
", ".$this->db->prefix().
"product as p";
4209 if (empty($user->rights->societe->client->voir) && !$socid) {
4210 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4213 $sql .=
" WHERE d.entity IN (".getEntity(
'mo').
")";
4214 $sql .=
" AND d.status > 0";
4216 if ($this->
id > 0) {
4217 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4219 $sql .=
" AND d.fk_product > 0";
4221 if ($filteronproducttype >= 0) {
4222 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4225 if (empty($user->rights->societe->client->voir) && !$socid) {
4226 $sql .=
" AND d.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4229 $sql .=
" AND d.fk_soc = ".((int) $socid);
4231 $sql .= $morefilter;
4232 $sql .=
" GROUP BY date_format(d.date_valid,'%Y%m')";
4233 $sql .=
" ORDER BY date_format(d.date_valid,'%Y%m') DESC";
4252 if (!is_numeric($id_pere)) {
4255 if (!is_numeric($id_fils)) {
4258 if (!is_numeric($incdec)) {
4268 $sql =
"SELECT fk_product_pere from ".$this->db->prefix().
"product_association";
4269 $sql .=
" WHERE fk_product_pere = ".((int) $id_fils).
" AND fk_product_fils = ".((int) $id_pere);
4270 if (!$this->db->query(
$sql)) {
4275 $sql =
"SELECT MAX(rang) as max_rank FROM ".$this->db->prefix().
"product_association";
4276 $sql .=
" WHERE fk_product_pere = ".((int) $id_pere);
4277 $resql = $this->db->query(
$sql);
4279 $obj = $this->db->fetch_object($resql);
4280 $rank = $obj->max_rank + 1;
4282 $sql =
"INSERT INTO ".$this->db->prefix().
"product_association(fk_product_pere,fk_product_fils,qty,incdec,rang)";
4283 $sql .=
" VALUES (".((int) $id_pere).
", ".((int) $id_fils).
", ".
price2num($qty,
'MS').
", ".
price2num($incdec,
'MS').
", ".((int) $rank).
")";
4284 if (! $this->db->query(
$sql)) {
4311 if (!is_numeric($id_pere)) {
4314 if (!is_numeric($id_fils)) {
4317 if (!is_numeric($incdec)) {
4320 if (!is_numeric($qty)) {
4324 $sql =
'UPDATE '.$this->db->prefix().
'product_association SET ';
4325 $sql .=
'qty = '.price2num($qty,
'MS');
4326 $sql .=
',incdec = '.price2num($incdec,
'MS');
4327 $sql .=
' WHERE fk_product_pere = '.((int) $id_pere).
' AND fk_product_fils = '.((int) $id_fils);
4329 if (!$this->db->query(
$sql)) {
4348 if (!is_numeric($fk_parent)) {
4351 if (!is_numeric($fk_child)) {
4355 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
4356 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4357 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4359 dol_syslog(get_class($this).
'::del_sousproduit', LOG_DEBUG);
4360 if (!$this->db->query(
$sql)) {
4366 $sqlrank =
"SELECT rowid, rang FROM ".$this->db->prefix().
"product_association";
4367 $sqlrank.=
" WHERE fk_product_pere = ".((int) $fk_parent);
4368 $sqlrank.=
" ORDER BY rang";
4369 $resqlrank = $this->db->query($sqlrank);
4372 while ($objrank = $this->db->fetch_object($resqlrank)) {
4374 $sql =
"UPDATE ".$this->db->prefix().
"product_association";
4375 $sql.=
" SET rang = ".((int) $cpt);
4376 $sql.=
" WHERE rowid = ".((int) $objrank->rowid);
4377 if (! $this->db->query(
$sql)) {
4397 $sql =
"SELECT fk_product_pere, qty, incdec";
4398 $sql .=
" FROM ".$this->db->prefix().
"product_association";
4399 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4400 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4402 $result = $this->db->query(
$sql);
4404 $num = $this->db->num_rows($result);
4407 $obj = $this->db->fetch_object($result);
4409 $this->is_sousproduit_qty = $obj->qty;
4410 $this->is_sousproduit_incdec = $obj->incdec;
4441 dol_syslog(get_class($this).
"::add_fournisseur id_fourn = ".$id_fourn.
" ref_fourn=".
$ref_fourn.
" quantity=".$quantity, LOG_DEBUG);
4447 $sql =
"SELECT rowid, fk_product";
4448 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4449 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4451 $sql .=
" AND fk_product <> ".((int) $this->
id);
4452 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4454 $resql = $this->db->query(
$sql);
4456 $obj = $this->db->fetch_object($resql);
4459 $this->product_id_already_linked = $obj->fk_product;
4462 $this->db->free($resql);
4466 $sql =
"SELECT rowid";
4467 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4468 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4472 $sql .=
" AND (ref_fourn = '' OR ref_fourn IS NULL)";
4474 $sql .=
" AND quantity = ".((float) $quantity);
4475 $sql .=
" AND fk_product = ".((int) $this->
id);
4476 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4478 $resql = $this->db->query(
$sql);
4480 $obj = $this->db->fetch_object($resql);
4484 $sql =
"INSERT INTO ".$this->db->prefix().
"product_fournisseur_price(";
4487 $sql .=
", fk_product";
4489 $sql .=
", ref_fourn";
4490 $sql .=
", quantity";
4491 $sql .=
", fk_user";
4493 $sql .=
") VALUES (";
4494 $sql .=
"'".$this->db->idate($now).
"'";
4495 $sql .=
", ".$conf->entity;
4496 $sql .=
", ".$this->id;
4497 $sql .=
", ".$id_fourn;
4499 $sql .=
", ".$quantity;
4500 $sql .=
", ".$user->id;
4504 if ($this->db->query(
$sql)) {
4505 $this->product_fourn_price_id = $this->db->last_insert_id($this->db->prefix().
"product_fournisseur_price");
4508 $this->error = $this->db->lasterror();
4513 $this->product_fourn_price_id = $obj->rowid;
4517 $this->error = $this->db->lasterror();
4536 $sql =
"SELECT DISTINCT p.fk_soc";
4537 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as p";
4538 $sql .=
" WHERE p.fk_product = ".((int) $this->
id);
4539 $sql .=
" AND p.entity = ".((int) $conf->entity);
4541 $result = $this->db->query(
$sql);
4543 $num = $this->db->num_rows($result);
4546 $obj = $this->db->fetch_object($result);
4547 $list[$i] = $obj->fk_soc;
4565 global $conf, $user;
4572 $sql =
"INSERT INTO ".$this->db->prefix().
"product_price (";
4574 $sql .=
", fk_product";
4575 $sql .=
", date_price";
4576 $sql .=
", price_level";
4578 $sql .=
", price_ttc";
4579 $sql .=
", price_min";
4580 $sql .=
", price_min_ttc";
4581 $sql .=
", price_base_type";
4582 $sql .=
", default_vat_code";
4584 $sql .=
", recuperableonly";
4585 $sql .=
", localtax1_tx";
4586 $sql .=
", localtax1_type";
4587 $sql .=
", localtax2_tx";
4588 $sql .=
", localtax2_type";
4589 $sql .=
", fk_user_author";
4591 $sql .=
", price_by_qty";
4592 $sql .=
", fk_price_expression";
4593 $sql .=
", fk_multicurrency";
4594 $sql .=
", multicurrency_code";
4595 $sql .=
", multicurrency_tx";
4596 $sql .=
", multicurrency_price";
4597 $sql .=
", multicurrency_price_ttc";
4602 $sql .=
", '".$this->db->idate($now).
"'";
4603 $sql .=
", price_level";
4605 $sql .=
", price_ttc";
4606 $sql .=
", price_min";
4607 $sql .=
", price_min_ttc";
4608 $sql .=
", price_base_type";
4609 $sql .=
", default_vat_code";
4611 $sql .=
", recuperableonly";
4612 $sql .=
", localtax1_tx";
4613 $sql .=
", localtax1_type";
4614 $sql .=
", localtax2_tx";
4615 $sql .=
", localtax2_type";
4616 $sql .=
", ".$user->id;
4618 $sql .=
", price_by_qty";
4619 $sql .=
", fk_price_expression";
4620 $sql .=
", fk_multicurrency";
4621 $sql .=
", multicurrency_code";
4622 $sql .=
", multicurrency_tx";
4623 $sql .=
", multicurrency_price";
4624 $sql .=
", multicurrency_price_ttc";
4625 $sql .=
" FROM ".$this->db->prefix().
"product_price";
4626 $sql .=
" WHERE fk_product = ".((int) $fromId);
4627 $sql .=
" ORDER BY date_price DESC";
4628 if ($conf->global->PRODUIT_MULTIPRICES_LIMIT > 0) {
4629 $sql .=
" LIMIT ".$conf->global->PRODUIT_MULTIPRICES_LIMIT;
4633 $resql = $this->db->query(
$sql);
4635 $this->db->rollback();
4639 $this->db->commit();
4656 $sql =
'INSERT INTO '.$this->db->prefix().
'product_association (fk_product_pere, fk_product_fils, qty)';
4657 $sql .=
" SELECT ".$toId.
", fk_product_fils, qty FROM ".$this->db->prefix().
"product_association";
4658 $sql .=
" WHERE fk_product_pere = ".((int) $fromId);
4660 dol_syslog(get_class($this).
'::clone_association', LOG_DEBUG);
4661 if (!$this->db->query(
$sql)) {
4662 $this->db->rollback();
4666 $this->db->commit();
4699 $sql =
"INSERT ".$this->db->prefix().
"product_fournisseur_price (";
4700 $sql .=
" datec, fk_product, fk_soc, price, quantity, fk_user)";
4701 $sql .=
" SELECT '".$this->db->idate($now).
"', ".((int) $toId).
", fk_soc, price, quantity, fk_user";
4702 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4703 $sql .=
" WHERE fk_product = ".((int) $fromId);
4705 dol_syslog(get_class($this).
'::clone_fournisseurs', LOG_DEBUG);
4706 $resql = $this->db->query(
$sql);
4708 $this->db->rollback();
4711 $this->db->commit();
4729 public function fetch_prod_arbo($prod, $compl_path =
'', $multiply = 1, $level = 1, $id_parent = 0, $ignore_stock_load = 0)
4732 global $conf, $langs;
4736 foreach ($prod as $id_product => $desc_pere) {
4737 if (is_array($desc_pere)) {
4738 $id = (!empty($desc_pere[0]) ? $desc_pere[0] :
'');
4739 $nb = (!empty($desc_pere[1]) ? $desc_pere[1] :
'');
4740 $type = (!empty($desc_pere[2]) ? $desc_pere[2] :
'');
4741 $label = (!empty($desc_pere[3]) ? $desc_pere[3] :
'');
4742 $incdec = (!empty($desc_pere[4]) ? $desc_pere[4] : 0);
4744 if ($multiply < 1) {
4749 if (is_null($tmpproduct)) {
4750 $tmpproduct =
new Product($this->db);
4752 $tmpproduct->fetch($id);
4754 if (empty($ignore_stock_load) && ($tmpproduct->isProduct() || !empty($conf->global->STOCK_SUPPORTS_SERVICES))) {
4755 $tmpproduct->load_stock(
'nobatch,novirtual');
4758 $this->res[] = array(
4760 'id_parent'=>$id_parent,
4761 'ref'=>$tmpproduct->ref,
4763 'nb_total'=>$nb * $multiply,
4764 'stock'=>$tmpproduct->stock_reel,
4765 'stock_alert'=>$tmpproduct->seuil_stock_alerte,
4767 'fullpath'=>$compl_path.$label,
4769 'desiredstock'=>$tmpproduct->desiredstock,
4772 'entity'=>$tmpproduct->entity
4776 if (isset($desc_pere[
'childs']) && is_array($desc_pere[
'childs'])) {
4778 $this->
fetch_prod_arbo($desc_pere[
'childs'], $compl_path.$desc_pere[3].
" -> ", $desc_pere[1] * $multiply, $level + 1, $id, $ignore_stock_load);
4796 $this->res = array();
4797 if (isset($this->sousprods) && is_array($this->sousprods)) {
4798 foreach ($this->sousprods as $prod_name => $desc_product) {
4799 if (is_array($desc_product)) {
4800 $this->
fetch_prod_arbo($desc_product,
"", $multiply, 1, $this->
id, $ignore_stock_load);
4819 $sql =
"SELECT COUNT(pa.rowid) as nb";
4820 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa";
4822 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id).
" OR pa.fk_product_pere = ".((int) $this->
id);
4823 } elseif ($mode == -1) {
4824 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id);
4825 } elseif ($mode == 1) {
4826 $sql .=
" WHERE pa.fk_product_pere = ".((int) $this->
id);
4829 $resql = $this->db->query(
$sql);
4831 $obj = $this->db->fetch_object($resql);
4850 $sql =
"SELECT count(rowid) as nb FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_parent = ".((int) $this->
id);
4851 $sql .=
" AND entity IN (".getEntity(
'product').
")";
4853 $resql = $this->db->query(
$sql);
4855 $obj = $this->db->fetch_object($resql);
4874 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_child = ".((int) $this->
id).
" AND entity IN (".
getEntity(
'product').
")";
4876 $query = $this->db->query(
$sql);
4879 if (!$this->db->num_rows($query)) {
4900 $sql =
"SELECT p.rowid, p.label as label, p.ref as ref, pa.fk_product_pere as id, p.fk_product_type, pa.qty, pa.incdec, p.entity";
4901 $sql .=
", p.tosell as status, p.tobuy as status_buy";
4902 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa,";
4903 $sql .=
" ".$this->db->prefix().
"product as p";
4904 $sql .=
" WHERE p.rowid = pa.fk_product_pere";
4905 $sql .=
" AND pa.fk_product_fils = ".((int) $this->
id);
4907 $res = $this->db->query(
$sql);
4910 while ($record = $this->db->fetch_array($res)) {
4912 $prods[$record[
'id']][
'id'] = $record[
'rowid'];
4913 $prods[$record[
'id']][
'ref'] = $record[
'ref'];
4914 $prods[$record[
'id']][
'label'] = $record[
'label'];
4915 $prods[$record[
'id']][
'qty'] = $record[
'qty'];
4916 $prods[$record[
'id']][
'incdec'] = $record[
'incdec'];
4917 $prods[$record[
'id']][
'fk_product_type'] = $record[
'fk_product_type'];
4918 $prods[$record[
'id']][
'entity'] = $record[
'entity'];
4919 $prods[$record[
'id']][
'status'] = $record[
'status'];
4920 $prods[$record[
'id']][
'status_buy'] = $record[
'status_buy'];
4939 public function getChildsArbo($id, $firstlevelonly = 0, $level = 1, $parents = array())
4941 global $alreadyfound;
4947 $sql =
"SELECT p.rowid, p.ref, p.label as label, p.fk_product_type,";
4948 $sql .=
" pa.qty as qty, pa.fk_product_fils as id, pa.incdec,";
4949 $sql .=
" pa.rowid as fk_association, pa.rang";
4950 $sql .=
" FROM ".$this->db->prefix().
"product as p,";
4951 $sql .=
" ".$this->db->prefix().
"product_association as pa";
4952 $sql .=
" WHERE p.rowid = pa.fk_product_fils";
4953 $sql .=
" AND pa.fk_product_pere = ".((int) $id);
4954 $sql .=
" AND pa.fk_product_fils <> ".((int) $id);
4955 $sql.=
" ORDER BY pa.rang";
4957 dol_syslog(get_class($this).
'::getChildsArbo id='.$id.
' level='.$level.
' parents='.(is_array($parents)?implode(
',', $parents):$parents), LOG_DEBUG);
4960 $alreadyfound = array($id=>1);
4967 $res = $this->db->query(
$sql);
4970 while ($rec = $this->db->fetch_array($res)) {
4971 if (!empty($alreadyfound[$rec[
'rowid']])) {
4972 dol_syslog(get_class($this).
'::getChildsArbo the product id='.$rec[
'rowid'].
' was already found at a higher level in tree. We discard to avoid infinite loop', LOG_WARNING);
4973 if (in_array($rec[
'id'], $parents)) {
4977 $alreadyfound[$rec[
'rowid']] = 1;
4978 $prods[$rec[
'rowid']] = array(
4981 2=>$rec[
'fk_product_type'],
4982 3=>$this->db->escape($rec[
'label']),
4985 6=>$rec[
'fk_association'],
4990 if (empty($firstlevelonly)) {
4991 $listofchilds = $this->
getChildsArbo($rec[
'rowid'], 0, $level + 1, array_push($parents, $rec[
'rowid']));
4992 foreach ($listofchilds as $keyChild => $valueChild) {
4993 $prods[$rec[
'rowid']][
'childs'][$keyChild] = $valueChild;
5017 foreach ($this->
getChildsArbo($this->
id) as $keyChild => $valueChild) {
5018 $parent[$this->label][$keyChild] = $valueChild;
5020 foreach ($parent as $key => $value) {
5021 $this->sousprods[$key] = $value;
5033 global $conf, $langs;
5035 $langs->load(
'products',
'other');
5038 $nofetch = !empty($params[
'nofetch']);
5040 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5041 return [
'optimize' => $langs->trans(
"ShowProduct")];
5044 if (!empty($this->entity)) {
5045 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80, 0, 0, 0, 0, 1);
5046 if ($this->nbphoto > 0) {
5047 $datas[
'photo'] =
'<div class="photointooltip floatright">'.
"\n" . $tmpphoto .
'</div>';
5052 $datas[
'picto'] =
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
5054 $datas[
'picto']=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
5056 if (isset($this->status) && isset($this->status_buy)) {
5057 $datas[
'status']=
' '.$this->getLibStatut(5, 0) .
' '.$this->getLibStatut(5, 1);
5060 if (!empty($this->
ref)) {
5061 $datas[
'ref']=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.$this->ref;
5063 if (!empty($this->label)) {
5064 $datas[
'label']=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
5068 $langs->load(
"productbatch");
5069 $datas[
'batchstatus']=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
5073 $datas[
'barcode']=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
5077 if ($this->weight) {
5078 $datas[
'weight']=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
5081 if ($this->length) {
5082 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
5085 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
5087 if ($this->height) {
5088 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
5091 $datas[
'size']=
"<br>".$labelsize;
5094 $labelsurfacevolume =
"";
5095 if ($this->surface) {
5096 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
5098 if ($this->volume) {
5099 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
5101 if ($labelsurfacevolume) {
5102 $datas[
'surface']=
"<br>" . $labelsurfacevolume;
5105 if (!empty($this->pmp) && $this->pmp) {
5106 $datas[
'pmp'] =
"<br><b>".$langs->trans(
"PMPValue").
'</b>: '.
price($this->pmp, 0,
'', 1, -1, -1, $conf->currency);
5110 if ($this->status && isset($this->accountancy_code_sell)) {
5111 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5112 $selllabel =
'<br>';
5113 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
5114 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
5115 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
5116 $datas[
'accountancysell'] = $selllabel;
5118 if ($this->status_buy && isset($this->accountancy_code_buy)) {
5119 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5121 if (empty($this->status)) {
5122 $buylabel .=
'<br>';
5124 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
5125 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
5126 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
5127 $datas[
'accountancybuy'] = $buylabel;
5132 require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
5134 $datas[
'categories'] =
'<br>' .
$form->showCategories($this->
id, Categorie::TYPE_PRODUCT, 1);
5153 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
5155 global $conf, $langs, $hookmanager;
5156 include_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
5160 $newref = $this->ref;
5162 $newref =
dol_trunc($newref, $maxlength,
'middle');
5166 'objecttype' => $this->element,
5167 'option' => $option,
5170 $classfortooltip =
'classfortooltip';
5173 $classfortooltip =
'classforajaxtooltip';
5174 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
5181 if (empty($notooltip)) {
5182 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5183 $label = $langs->trans(
"ShowProduct");
5184 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1, 1).
'"';
5186 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1, 1).
'"' :
' title="tocomplete"');
5187 $linkclose .= $dataparams.
' class="nowraponall '.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
5189 $linkclose =
' class="nowraponall'.($morecss ?
' '.$morecss :
'').
'"';
5192 if ($option ==
'supplier' || $option ==
'category') {
5193 $url = DOL_URL_ROOT.
'/product/fournisseurs.php?id='.$this->id;
5194 } elseif ($option ==
'stock') {
5195 $url = DOL_URL_ROOT.
'/product/stock/product.php?id='.$this->id;
5196 } elseif ($option ==
'composition') {
5197 $url = DOL_URL_ROOT.
'/product/composition/card.php?id='.$this->id;
5199 $url = DOL_URL_ROOT.
'/product/card.php?id='.$this->id;
5202 if ($option !==
'nolink') {
5204 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
5205 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
5206 $add_save_lastsearch_values = 1;
5208 if ($add_save_lastsearch_values) {
5209 $url .=
'&save_lastsearch_values=1';
5213 $linkstart =
'<a href="'.$url.
'"';
5214 $linkstart .= $linkclose.
'>';
5217 $result .= $linkstart;
5220 $result .= (
img_object(($notooltip ?
'' : $label),
'product',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5223 $result .= (
img_object(($notooltip ?
'' : $label),
'service',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5227 $result .= $linkend;
5228 if ($withpicto != 2) {
5229 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
5233 $hookmanager->initHooks(array(
'productdao'));
5234 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'label' => &$label);
5235 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
5237 $result = $hookmanager->resPrint;
5239 $result .= $hookmanager->resPrint;
5256 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
5258 global $conf, $user, $langs;
5260 $langs->load(
"products");
5261 $outputlangs->load(
"products");
5268 $modelpath =
"core/modules/product/doc/";
5270 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
5284 return $this->
LibStatut($this->status, $mode, $type);
5286 return $this->
LibStatut($this->status_buy, $mode, $type);
5288 return $this->
LibStatut($this->status_batch, $mode, $type);
5291 return $this->
LibStatut($this->status_buy, $mode, $type);
5307 global $conf, $langs;
5309 $labelStatus = $labelStatusShort =
'';
5311 $langs->load(
'products');
5313 $langs->load(
"productbatch");
5319 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial')));
5322 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort')));
5327 return dolGetStatus($langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch'),
'',
'', empty($status) ?
'status5' :
'status4', 3,
'dot');
5333 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'));
5337 $statuttrans = empty($status) ?
'status5' :
'status4';
5342 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSellShort');
5343 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSell');
5344 } elseif ($type == 1) {
5345 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuyShort');
5346 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuy');
5347 } elseif ($type == 2) {
5348 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch');
5349 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort');
5351 } elseif ($status == 1) {
5354 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSellShort');
5355 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSell');
5356 } elseif ($type == 1) {
5357 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnBuyShort');
5358 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnBuy');
5359 } elseif ($type == 2) {
5360 $labelStatus = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial'));
5361 $labelStatusShort = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort'));
5363 } elseif ( $type == 2 && $status == 2 ) {
5364 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSerial');
5365 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort');
5369 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'),
'',
'',
'status0', 0);
5371 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statuttrans, $mode);
5384 $langs->load(
'products');
5386 if (isset($this->finished) && $this->finished >= 0) {
5387 $sql =
"SELECT label, code FROM ".$this->db->prefix().
"c_product_nature where code = ".((int) $this->finished).
" AND active=1";
5388 $resql = $this->db->query(
$sql);
5389 if ($resql && $this->db->num_rows($resql) > 0) {
5390 $res = $this->db->fetch_array($resql);
5391 $label = $langs->trans($res[
'label']);
5392 $this->db->free($resql);
5395 $this->error = $this->db->error().
' sql='.
$sql;
5396 dol_syslog(__METHOD__.
' Error '.$this->error, LOG_ERR);
5422 public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label =
'', $price = 0, $inventorycode =
'', $origin_element =
'', $origin_id =
null, $disablestockchangeforsubproduct = 0, $extrafields =
null)
5428 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5434 $nbpiece = abs($nbpiece);
5437 $op[0] =
"+".trim($nbpiece);
5438 $op[1] =
"-".trim($nbpiece);
5441 $movementstock->setOrigin($origin_element, $origin_id);
5442 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'',
'',
'',
'',
false, 0, $disablestockchangeforsubproduct);
5446 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5447 $movementstock->array_options = $array_options;
5448 $movementstock->insertExtraFields();
5450 $this->db->commit();
5453 $this->error = $movementstock->error;
5454 $this->errors = $movementstock->errors;
5456 $this->db->rollback();
5482 public function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label =
'', $price = 0, $dlc =
'', $dluo =
'', $lot =
'', $inventorycode =
'', $origin_element =
'', $origin_id =
null, $disablestockchangeforsubproduct = 0, $extrafields =
null)
5488 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5494 $nbpiece = abs($nbpiece);
5497 $op[0] =
"+".trim($nbpiece);
5498 $op[1] =
"-".trim($nbpiece);
5501 $movementstock->setOrigin($origin_element, $origin_id);
5502 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'', $dlc, $dluo, $lot,
false, 0, $disablestockchangeforsubproduct);
5506 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5507 $movementstock->array_options = $array_options;
5508 $movementstock->insertExtraFields();
5510 $this->db->commit();
5513 $this->error = $movementstock->error;
5514 $this->errors = $movementstock->errors;
5516 $this->db->rollback();
5535 public function load_stock($option =
'', $includedraftpoforvirtual =
null, $dateofvirtualstock =
null)
5540 $this->stock_reel = 0;
5541 $this->stock_warehouse = array();
5542 $this->stock_theorique = 0;
5545 $warehouseStatus = array();
5546 if (preg_match(
'/warehouseclosed/', $option)) {
5549 if (preg_match(
'/warehouseopen/', $option)) {
5552 if (preg_match(
'/warehouseinternal/', $option)) {
5553 if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
5560 $sql =
"SELECT ps.rowid, ps.reel, ps.fk_entrepot";
5561 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
5562 $sql .=
", ".$this->db->prefix().
"entrepot as w";
5563 $sql .=
" WHERE w.entity IN (".getEntity(
'stock').
")";
5564 $sql .=
" AND w.rowid = ps.fk_entrepot";
5565 $sql .=
" AND ps.fk_product = ".((int) $this->
id);
5566 if (count($warehouseStatus)) {
5567 $sql .=
" AND w.statut IN (".$this->db->sanitize(implode(
',', $warehouseStatus)).
")";
5570 $sql .=
" ORDER BY ps.reel ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'DESC':
'ASC');
5572 dol_syslog(get_class($this).
"::load_stock", LOG_DEBUG);
5573 $result = $this->db->query(
$sql);
5575 $num = $this->db->num_rows($result);
5579 $row = $this->db->fetch_object($result);
5580 $this->stock_warehouse[$row->fk_entrepot] =
new stdClass();
5581 $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
5582 $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
5583 if ((!preg_match(
'/nobatch/', $option)) && $this->
hasbatch()) {
5584 $this->stock_warehouse[$row->fk_entrepot]->detail_batch =
Productbatch::findAll($this->db, $row->rowid, 1, $this->id);
5586 $this->stock_reel += $row->reel;
5590 $this->db->free($result);
5592 if (!preg_match(
'/novirtual/', $option)) {
5598 $this->error = $this->db->lasterror();
5617 global $conf, $hookmanager, $action;
5619 $stock_commande_client = 0;
5620 $stock_commande_fournisseur = 0;
5621 $stock_sending_client = 0;
5622 $stock_reception_fournisseur = 0;
5623 $stock_inproduction = 0;
5632 $stock_commande_client = $this->stats_commande[
'qty'];
5635 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
5636 $filterShipmentStatus =
'';
5637 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
5639 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5646 $stock_sending_client = $this->stats_expedition[
'qty'];
5649 $filterStatus = empty($conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK) ?
'3,4' : $conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK;
5650 if (isset($includedraftpoforvirtual)) {
5651 $filterStatus =
'0,1,2,'.$filterStatus;
5657 $stock_commande_fournisseur = $this->stats_commande_fournisseur[
'qty'];
5661 $filterStatus =
'4';
5662 if (isset($includedraftpoforvirtual)) {
5663 $filterStatus =
'0,'.$filterStatus;
5669 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5673 $filterStatus =
'4';
5674 if (isset($includedraftpoforvirtual)) {
5675 $filterStatus =
'0,'.$filterStatus;
5681 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5688 $stock_inproduction = $this->stats_mrptoproduce[
'qty'] - $this->stats_mrptoconsume[
'qty'];
5691 $this->stock_theorique = $this->stock_reel + $stock_inproduction;
5694 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5695 $this->stock_theorique -= ($stock_commande_client - $stock_sending_client);
5696 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER)) {
5697 $this->stock_theorique += 0;
5698 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
5699 $this->stock_theorique -= $stock_commande_client;
5702 if (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE)) {
5703 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5704 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
5705 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5706 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
5707 $this->stock_theorique -= $stock_reception_fournisseur;
5708 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
5709 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5712 $hookmanager->initHooks(array(
'productdao'));
5713 $parameters = array(
'id'=>$this->
id,
'includedraftpoforvirtual' => $includedraftpoforvirtual);
5715 $reshook = $hookmanager->executeHooks(
'loadvirtualstock', $parameters, $this, $action);
5717 $this->stock_theorique = $hookmanager->resArray[
'stock_theorique'];
5735 $sql =
"SELECT pb.batch, pb.eatby, pb.sellby, SUM(pb.qty) AS qty FROM ".$this->db->prefix().
"product_batch as pb, ".$this->db->prefix().
"product_stock as ps";
5736 $sql .=
" WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = ".((int) $this->
id).
" AND pb.batch = '".$this->db->escape($batch).
"'";
5737 $sql .=
" GROUP BY pb.batch, pb.eatby, pb.sellby";
5738 dol_syslog(get_class($this).
"::loadBatchInfo load first entry found for lot/serial = ".$batch, LOG_DEBUG);
5739 $resql = $this->db->query(
$sql);
5741 $num = $this->db->num_rows($resql);
5744 $obj = $this->db->fetch_object($resql);
5745 $result[] = array(
'batch'=>$batch,
'eatby'=>$this->db->jdate($obj->eatby),
'sellby'=>$this->db->jdate($obj->sellby),
'qty'=>$obj->qty);
5751 $this->db->rollback();
5769 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5775 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos";
5777 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product').dol_sanitizeFileName($this->
ref);
5782 $dir_osencoded = $dir;
5784 if (is_dir($dir_osencoded)) {
5785 $originImage = $dir.
'/'.$file[
'name'];
5796 if (is_numeric($result) && $result > 0) {
5813 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5814 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5820 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos/";
5822 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product');
5828 if (file_exists($dir_osencoded)) {
5829 $handle = opendir($dir_osencoded);
5830 if (is_resource($handle)) {
5831 while (($file = readdir($handle)) !==
false) {
5833 $file = utf8_encode($file);
5855 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5856 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5862 $handle = @opendir($dir_osencoded);
5863 if (is_resource($handle)) {
5864 while (($file = readdir($handle)) !==
false) {
5866 $file = utf8_encode($file);
5873 $photo_vignette =
'';
5875 if (preg_match(
'/('.$this->regeximgext.
')$/i', $photo, $regs)) {
5876 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $photo).
'_small'.$regs[0];
5879 $dirthumb = $dir.
'thumbs/';
5883 $obj[
'photo'] = $photo;
5884 if ($photo_vignette &&
dol_is_file($dirthumb.$photo_vignette)) {
5885 $obj[
'photo_vignette'] =
'thumbs/'.$photo_vignette;
5887 $obj[
'photo_vignette'] =
"";
5890 $tabobj[$nbphoto - 1] = $obj;
5893 if ($nbmax && $nbphoto >= $nbmax) {
5915 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5916 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5918 $dir = dirname($file).
'/';
5919 $dirthumb = $dir.
'/thumbs/';
5920 $filename = preg_replace(
'/'.preg_quote($dir,
'/').
'/i',
'', $file);
5926 if (preg_match(
'/('.$this->regeximgext.
')$/i', $filename, $regs)) {
5927 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_small'.$regs[0];
5928 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5932 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_mini'.$regs[0];
5933 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5950 $infoImg = getimagesize($file_osencoded);
5951 $this->imgWidth = $infoImg[0];
5952 $this->imgHeight = $infoImg[1];
5964 global $hookmanager;
5966 $this->nb = array();
5968 $sql =
"SELECT count(p.rowid) as nb, fk_product_type";
5969 $sql .=
" FROM ".$this->db->prefix().
"product as p";
5970 $sql .=
' WHERE p.entity IN ('.getEntity($this->element, 1).
')';
5972 if (is_object($hookmanager)) {
5973 $parameters = array();
5974 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
5975 $sql .= $hookmanager->resPrint;
5977 $sql .=
' GROUP BY fk_product_type';
5979 $resql = $this->db->query(
$sql);
5981 while ($obj = $this->db->fetch_object($resql)) {
5982 if ($obj->fk_product_type == 1) {
5983 $this->nb[
"services"] = $obj->nb;
5985 $this->nb[
"products"] = $obj->nb;
5988 $this->db->free($resql);
5992 $this->error = $this->db->error();
6025 return ($this->mandatory_period == 1 ?
true :
false);
6042 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
6043 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
6044 foreach ($dirsociete as $dirroot) {
6045 $res =
dol_include_once($dirroot.$conf->global->BARCODE_PRODUCT_ADDON_NUM.
'.php');
6050 $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
6053 $result = $mod->getNextValue($object, $type);
6055 dol_syslog(get_class($this).
"::get_barcode barcode=".$result.
" module=".$var);
6069 global $user, $langs, $conf, $mysoc;
6074 $this->specimen = 1;
6076 $this->
ref =
'PRODUCT_SPEC';
6077 $this->label =
'PRODUCT SPECIMEN';
6078 $this->
description =
'This is description of this product specimen that was created the '.dol_print_date($now,
'dayhourlog').
'.';
6079 $this->specimen = 1;
6080 $this->country_id = 1;
6082 $this->status_buy = 1;
6084 $this->note_private =
'This is a comment (private)';
6085 $this->note_public =
'This is a comment (public)';
6086 $this->date_creation = $now;
6087 $this->date_modification = $now;
6090 $this->weight_units = 3;
6093 $this->length_units = 1;
6095 $this->width_units = 0;
6096 $this->height =
null;
6097 $this->height_units =
null;
6099 $this->surface = 30;
6100 $this->surface_units = 0;
6101 $this->volume = 300;
6102 $this->volume_units = 0;
6104 $this->barcode = -1;
6117 if (!$this->fk_unit) {
6121 $langs->load(
'products');
6123 $label_type =
'label';
6124 if ($type ==
'short') {
6125 $label_type =
'short_label';
6128 $sql =
"SELECT ".$label_type.
", code from ".$this->db->prefix().
"c_units where rowid = ".((int) $this->fk_unit);
6130 $resql = $this->db->query(
$sql);
6131 if ($resql && $this->db->num_rows($resql) > 0) {
6132 $res = $this->db->fetch_array($resql);
6133 $label = ($label_type ==
'short_label' ? $res[$label_type] :
'unit'.$res[
'code']);
6134 $this->db->free($resql);
6137 $this->error = $this->db->error();
6138 dol_syslog(get_class($this).
"::getLabelOfUnit Error ".$this->error, LOG_ERR);
6150 return ($this->status_batch > 0 ?
true :
false);
6165 $maxpricesupplier = 0;
6167 if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE)) {
6168 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
6170 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->
id,
'',
'');
6172 if (is_array($product_fourn_list) && count($product_fourn_list) > 0) {
6173 foreach ($product_fourn_list as $productfourn) {
6174 if ($productfourn->fourn_unitprice > $maxpricesupplier) {
6175 $maxpricesupplier = $productfourn->fourn_unitprice;
6179 $maxpricesupplier *= $conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE;
6183 return $maxpricesupplier;
6199 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
6200 return parent::setCategoriesCommon($categories, Categorie::TYPE_PRODUCT);
6214 'product_customer_price',
6215 'product_customer_price_log'
6236 $sql =
"SELECT rowid, level, fk_level, var_percent, var_min_percent FROM ".$this->db->prefix().
"product_pricerules";
6237 $query = $this->db->query(
$sql);
6241 while ($result = $this->db->fetch_object($query)) {
6242 $rules[$result->level] = $result;
6250 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
6251 $price = $baseprice;
6252 $price_min = $baseprice;
6256 if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
6257 $price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent / 100));
6260 $prices[$i] = $price;
6263 if (isset($rules[$i]->var_min_percent) && $rules[$i]->var_min_percent) {
6264 $price_min = $price * (1 - ($rules[$i]->var_min_percent / 100));
6268 $check_amount = (($price == $this->multiprices[$i]) && ($price_min == $this->multiprices_min[$i]));
6269 $check_type = ($baseprice == $this->multiprices_base_type[$i]);
6271 if ($check_amount && $check_type) {
6275 if ($this->
updatePrice($price, $price_type, $user, $price_vat, $price_min, $i, $npr, $psq,
true) < 0) {
6293 return $user->rights->produit;
6295 return $user->rights->service;
6307 $sql =
"SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification,";
6308 $sql .=
" p.fk_user_author, p.fk_user_modif";
6309 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as p";
6310 $sql .=
" WHERE p.rowid = ".((int) $id);
6312 $result = $this->db->query(
$sql);
6314 if ($this->db->num_rows($result)) {
6315 $obj = $this->db->fetch_object($result);
6317 $this->
id = $obj->rowid;
6319 if ($obj->fk_user_author) {
6320 $cuser =
new User($this->db);
6321 $cuser->fetch($obj->fk_user_author);
6322 $this->user_creation = $cuser;
6325 if ($obj->fk_user_modif) {
6326 $muser =
new User($this->db);
6327 $muser->fetch($obj->fk_user_modif);
6328 $this->user_modification = $muser;
6331 $this->
ref = $obj->ref;
6332 $this->date_creation = $this->db->jdate($obj->date_creation);
6333 $this->date_modification = $this->db->jdate($obj->date_modification);
6336 $this->db->free($result);
6351 if (empty($this->duration_value)) {
6352 $this->errors[]=
'ErrorDurationForServiceNotDefinedCantCalculateHourlyPrice';
6356 if ($this->duration_unit ==
'i') {
6357 $prodDurationHours = 1. / 60;
6359 if ($this->duration_unit ==
'h') {
6360 $prodDurationHours = 1.;
6362 if ($this->duration_unit ==
'd') {
6363 $prodDurationHours = 24.;
6365 if ($this->duration_unit ==
'w') {
6366 $prodDurationHours = 24. * 7;
6368 if ($this->duration_unit ==
'm') {
6369 $prodDurationHours = 24. * 30;
6371 if ($this->duration_unit ==
'y') {
6372 $prodDurationHours = 24. * 365;
6374 $prodDurationHours *= $this->duration_value;
6376 return $prodDurationHours;
6389 global $langs,$conf;
6391 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
6393 $return =
'<div class="box-flex-item box-flex-grow-zero">';
6394 $return .=
'<div class="info-box info-box-sm">';
6395 $return .=
'<div class="info-box-img">';
6398 $label .= $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 120, 160, 0, 0, 0,
'',
'photoref photokanban');
6408 $return .=
'</div>';
6409 $return .=
'<div class="info-box-content">';
6410 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
6411 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
6412 if (property_exists($this,
'label')) {
6413 $return .=
'<br><span class="info-box-label opacitymedium">'.$this->label.
'</span>';
6415 if (property_exists($this,
'price') && property_exists($this,
'price_ttc')) {
6416 if ($this->price_base_type ==
'TTC') {
6417 $return .=
'<br><span class="info-box-status amount">'.price($this->price_ttc).
' '.$langs->trans(
"TTC").
'</span>';
6419 if ($this->status) {
6420 $return .=
'<br><span class="info-box-status amount">'.price($this->
price).
' '.$langs->trans(
"HT").
'</span>';
6424 if (property_exists($this,
'stock_reel')) {
6425 $return .=
'<br><span class="info-box-status opacitymedium">'.$langs->trans(
'PhysicalStock').
' : <span class="bold">'.$this->stock_reel.
'</span></span>';
6427 if (method_exists($this,
'getLibStatut')) {
6428 $return .=
'<br><span class="info-box-status margintoponly">'.$this->getLibStatut(3, 1).
' '.$this->
getLibStatut(3, 0).
'</span>';
6430 $return .=
'</div>';
6431 $return .=
'</div>';
6432 $return .=
'</div>';
6443 public $picto =
'service';