38require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
39require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
41require_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'),
74 'mrp_production' => array(
'name' =>
'Mo',
'parent' =>
'mrp_mo',
'parentkey' =>
'fk_mo' ),
75 'bom_bom' => array(
'name' =>
'BOM'),
76 'bom_bomline' => array(
'name' =>
'BOMLine',
'parent' =>
'bom_bom',
'parentkey' =>
'fk_bom'),
84 public $ismultientitymanaged = 1;
89 public $isextrafieldmanaged = 1;
94 public $picto =
'product';
101 public $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
144 public $price_formated;
153 public $price_ttc_formated;
167 public $price_min_ttc;
173 public $price_base_type;
177 public $multiprices_ttc = array();
178 public $multiprices_base_type = array();
179 public $multiprices_min = array();
180 public $multiprices_min_ttc = array();
181 public $multiprices_tva_tx = array();
182 public $multiprices_recuperableonly = array();
186 public $prices_by_qty = array();
187 public $prices_by_qty_id = array();
188 public $prices_by_qty_list = array();
213 public $localtax2_tx;
214 public $localtax1_type;
215 public $localtax2_type;
219 public $desc_supplier;
220 public $vatrate_supplier;
221 public $default_vat_code_supplier;
222 public $fourn_multicurrency_price;
223 public $fourn_multicurrency_unitprice;
224 public $fourn_multicurrency_tx;
225 public $fourn_multicurrency_id;
226 public $fourn_multicurrency_code;
232 public $qc_frequency;
239 public $stock_reel = 0;
246 public $stock_theorique;
263 public $seuil_stock_alerte = 0;
308 public $status_buy = 0;
330 public $fk_default_bom;
337 public $status_batch = 0;
344 public $batch_mask =
'';
362 public $weight_units;
364 public $length_units;
368 public $height_units;
370 public $surface_units;
372 public $volume_units;
375 public $net_measure_units;
377 public $accountancy_code_sell;
378 public $accountancy_code_sell_intra;
379 public $accountancy_code_sell_export;
380 public $accountancy_code_buy;
381 public $accountancy_code_buy_intra;
382 public $accountancy_code_buy_export;
396 public $barcode_type;
403 public $barcode_type_code;
405 public $stats_propale = array();
406 public $stats_commande = array();
407 public $stats_contrat = array();
408 public $stats_facture = array();
409 public $stats_proposal_supplier = array();
410 public $stats_commande_fournisseur = array();
411 public $stats_expedition = array();
412 public $stats_reception = array();
413 public $stats_mo = array();
414 public $stats_bom = array();
415 public $stats_mrptoconsume = array();
416 public $stats_mrptoproduce = array();
417 public $stats_facturerec = array();
418 public $stats_facture_fournisseur = array();
427 public $date_creation;
432 public $date_modification;
450 public $fk_default_warehouse;
454 public $fk_price_expression;
459 public $fourn_price_base_type;
471 public $ref_supplier;
485 public $price_autogen = 0;
492 public $supplierprices;
512 public $is_object_used;
518 public $mandatory_period;
548 public $fields = array(
549 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
550 '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'),
551 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>5),
552 'label' =>array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>2,
'position'=>15,
'csslist'=>
'tdoverflowmax250'),
553 'barcode' =>array(
'type'=>
'varchar(255)',
'label'=>
'Barcode',
'enabled'=>
'isModEnabled("barcode")',
'position'=>20,
'visible'=>-1,
'showoncombobox'=>3),
554 'fk_barcode_type' => array(
'type'=>
'integer',
'label'=>
'BarcodeType',
'enabled'=>
'1',
'position'=>21,
'notnull'=>0,
'visible'=>-1,),
555 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61),
556 'note' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62),
557 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>500),
558 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>501),
560 'fk_user_author'=>array(
'type'=>
'integer',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>510,
'foreignkey'=>
'llx_user.rowid'),
561 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>511),
563 'localtax1_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax1tx',
'enabled'=>
'1',
'position'=>150,
'notnull'=>0,
'visible'=>-1,),
564 'localtax1_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax1type',
'enabled'=>
'1',
'position'=>155,
'notnull'=>1,
'visible'=>-1,),
565 'localtax2_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax2tx',
'enabled'=>
'1',
'position'=>160,
'notnull'=>0,
'visible'=>-1,),
566 'localtax2_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax2type',
'enabled'=>
'1',
'position'=>165,
'notnull'=>1,
'visible'=>-1,),
567 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'index'=>0,
'position'=>1000),
570 'mandatory_period' => array(
'type'=>
'integer',
'label'=>
'mandatoryperiod',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'default'=>0,
'index'=>1,
'position'=>1000),
610 $this->
ref = trim($this->
ref);
638 public function create($user, $notrigger = 0)
640 global $conf, $langs;
646 $this->
ref = trim($this->
ref);
650 $this->label = trim($this->label);
651 $this->price_ttc =
price2num($this->price_ttc);
653 $this->price_min_ttc =
price2num($this->price_min_ttc);
654 $this->price_min =
price2num($this->price_min);
655 if (empty($this->tva_tx)) {
658 if (empty($this->tva_npr)) {
662 if (empty($this->localtax1_tx)) {
663 $this->localtax1_tx = 0;
665 if (empty($this->localtax2_tx)) {
666 $this->localtax2_tx = 0;
668 if (empty($this->localtax1_type)) {
669 $this->localtax1_type =
'0';
671 if (empty($this->localtax2_type)) {
672 $this->localtax2_type =
'0';
674 if (empty($this->
price)) {
677 if (empty($this->price_min)) {
678 $this->price_min = 0;
681 if (empty($this->price_by_qty)) {
682 $this->price_by_qty = 0;
685 if (empty($this->status)) {
688 if (empty($this->status_buy)) {
689 $this->status_buy = 0;
698 if ($this->price_base_type ==
'TTC' && $this->price_ttc > 0) {
699 $price_ttc =
price2num($this->price_ttc,
'MU');
700 $price_ht =
price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),
'MU');
704 if ($this->price_base_type !=
'TTC' && $this->
price > 0) {
706 $price_ttc =
price2num($this->
price * (1 + ($this->tva_tx / 100)),
'MU');
710 if (($this->price_min_ttc > 0) && ($this->price_base_type ==
'TTC')) {
711 $price_min_ttc =
price2num($this->price_min_ttc,
'MU');
712 $price_min_ht =
price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),
'MU');
716 if (($this->price_min > 0) && ($this->price_base_type !=
'TTC')) {
717 $price_min_ht =
price2num($this->price_min,
'MU');
718 $price_min_ttc =
price2num($this->price_min * (1 + ($this->tva_tx / 100)),
'MU');
721 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
722 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
723 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
724 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
725 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
726 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
729 $this->barcode = trim($this->barcode);
730 $this->mandatory_period = empty($this->mandatory_period) ? 0 : $this->mandatory_period;
732 if (empty($this->label)) {
733 $this->error =
'ErrorMandatoryParametersNotProvided';
737 if (empty($this->
ref) || $this->
ref ==
'auto') {
740 if ($module !=
'mod_codeproduct_leopard') {
741 if (substr($module, 0, 16) ==
'mod_codeproduct_' && substr($module, -3) ==
'php') {
742 $module = substr($module, 0,
dol_strlen($module) - 4);
745 $modCodeProduct =
new $module;
746 if (!empty($modCodeProduct->code_auto)) {
747 $this->
ref = $modCodeProduct->getNextValue($this, $this->
type);
749 unset($modCodeProduct);
752 if (empty($this->
ref)) {
753 $this->error =
'ProductModuleNotSetupForAutoRef';
758 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);
765 if ($this->barcode == -1) {
766 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
771 $result = $this->
verify();
774 $sql =
"SELECT count(*) as nb";
775 $sql .=
" FROM ".$this->db->prefix().
"product";
776 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
777 $sql .=
" AND ref = '".$this->db->escape($this->
ref).
"'";
779 $result = $this->db->query($sql);
781 $obj = $this->db->fetch_object($result);
784 $sql =
"INSERT INTO ".$this->db->prefix().
"product (";
789 $sql .=
", price_min";
790 $sql .=
", price_min_ttc";
792 $sql .=
", fk_user_author";
793 $sql .=
", fk_product_type";
795 $sql .=
", price_ttc";
796 $sql .=
", price_base_type";
799 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
800 $sql .=
", accountancy_code_buy";
801 $sql .=
", accountancy_code_buy_intra";
802 $sql .=
", accountancy_code_buy_export";
803 $sql .=
", accountancy_code_sell";
804 $sql .=
", accountancy_code_sell_intra";
805 $sql .=
", accountancy_code_sell_export";
808 $sql .=
", finished";
810 $sql .=
", batch_mask";
812 $sql .=
", mandatory_period";
813 $sql .=
") VALUES (";
814 $sql .=
"'".$this->db->idate($now).
"'";
815 $sql .=
", ".(!empty($this->entity) ? (int) $this->entity : (int) $conf->entity);
816 $sql .=
", '".$this->db->escape($this->
ref).
"'";
817 $sql .=
", ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
818 $sql .=
", ".price2num($price_min_ht);
819 $sql .=
", ".price2num($price_min_ttc);
820 $sql .=
", ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
821 $sql .=
", ".((int) $user->id);
822 $sql .=
", ".((int) $this->
type);
823 $sql .=
", ".price2num($price_ht,
'MT');
824 $sql .=
", ".price2num($price_ttc,
'MT');
825 $sql .=
", '".$this->db->escape($this->price_base_type).
"'";
826 $sql .=
", ".((int) $this->status);
827 $sql .=
", ".((int) $this->status_buy);
828 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
829 $sql .=
", '".$this->db->escape($this->accountancy_code_buy).
"'";
830 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_intra).
"'";
831 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_export).
"'";
832 $sql .=
", '".$this->db->escape($this->accountancy_code_sell).
"'";
833 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_intra).
"'";
834 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_export).
"'";
836 $sql .=
", '".$this->db->escape($this->canvas).
"'";
837 $sql .=
", ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
'NULL' : (int) $this->finished);
838 $sql .=
", ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : ((int) $this->status_batch));
839 $sql .=
", '".$this->db->escape($this->batch_mask).
"'";
840 $sql .=
", ".($this->fk_unit > 0 ? ((int) $this->fk_unit) :
'NULL');
841 $sql .=
", '".$this->db->escape($this->mandatory_period).
"'";
844 dol_syslog(get_class($this).
"::Create", LOG_DEBUG);
845 $result = $this->db->query($sql);
847 $id = $this->db->last_insert_id($this->db->prefix().
"product");
851 $this->
price = $price_ht;
852 $this->price_ttc = $price_ttc;
853 $this->price_min = $price_min_ht;
854 $this->price_min_ttc = $price_min_ttc;
858 if ($this->
update($id, $user,
true,
'add') <= 0) {
863 $this->error = $this->db->lasterror();
867 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
868 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " .((int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
870 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
871 $sql .=
" fk_product";
873 $sql .=
", accountancy_code_buy";
874 $sql .=
", accountancy_code_buy_intra";
875 $sql .=
", accountancy_code_buy_export";
876 $sql .=
", accountancy_code_sell";
877 $sql .=
", accountancy_code_sell_intra";
878 $sql .=
", accountancy_code_sell_export";
879 $sql .=
") VALUES (";
881 $sql .=
", " . $conf->entity;
882 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
883 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
884 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
885 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
886 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
887 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
889 $result = $this->db->query($sql);
892 $this->error =
'ErrorFailedToInsertAccountancyForEntity';
897 $this->error =
'ErrorFailedToGetInsertedId';
901 $this->error = $this->db->lasterror();
905 $langs->load(
"products");
907 $this->error =
"ErrorProductAlreadyExists";
908 dol_syslog(get_class($this).
"::Create fails, ref ".$this->
ref.
" already exists");
912 $this->error = $this->db->lasterror();
915 if (!$error && !$notrigger) {
928 $this->db->rollback();
932 $this->db->rollback();
933 dol_syslog(get_class($this).
"::Create fails verify ".join(
',', $this->errors), LOG_WARNING);
949 $this->errors = array();
952 $this->
ref = trim($this->
ref);
955 $this->errors[] =
'ErrorBadRef';
959 $arrayofnonnegativevalue = array(
'weight'=>
'Weight',
'width'=>
'Width',
'height'=>
'Height',
'length'=>
'Length',
'surface'=>
'Surface',
'volume'=>
'Volume');
960 foreach ($arrayofnonnegativevalue as $key => $value) {
961 if (property_exists($this, $key) && !empty($this->$key) && ($this->$key < 0)) {
962 $langs->loadLangs(array(
"main",
"other"));
963 $this->error = $langs->trans(
"FieldCannotBeNegative", $langs->transnoentitiesnoconv($value));
964 $this->errors[] = $this->error;
969 $rescode = $this->
check_barcode($this->barcode, $this->barcode_type_code);
971 if ($rescode == -1) {
972 $this->errors[] =
'ErrorBadBarCodeSyntax';
973 } elseif ($rescode == -2) {
974 $this->errors[] =
'ErrorBarCodeRequired';
975 } elseif ($rescode == -3) {
977 $this->errors[] =
'ErrorBarCodeAlreadyUsed';
1001 if (isModEnabled(
'barcode') && !empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
1002 $module = strtolower($conf->global->BARCODE_PRODUCT_ADDON_NUM);
1004 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
1005 foreach ($dirsociete as $dirroot) {
1012 $mod =
new $module();
1014 dol_syslog(get_class($this).
"::check_barcode value=".$valuetotest.
" type=".$typefortest.
" module=".$module);
1015 $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest);
1033 public function update($id, $user, $notrigger =
false, $action =
'update', $updatetype =
false)
1035 global $langs, $conf, $hookmanager;
1040 if (!$this->label) {
1041 $this->label =
'MISSING LABEL';
1046 $this->
ref = trim($this->
ref);
1050 $this->label = trim($this->label);
1052 $this->note_private = (isset($this->note_private) ? trim($this->note_private) :
null);
1053 $this->note_public = (isset($this->note_public) ? trim($this->note_public) :
null);
1054 $this->net_measure =
price2num($this->net_measure);
1055 $this->net_measure_units = trim($this->net_measure_units);
1056 $this->weight =
price2num($this->weight);
1057 $this->weight_units = trim($this->weight_units);
1058 $this->length =
price2num($this->length);
1059 $this->length_units = trim($this->length_units);
1061 $this->width_units = trim($this->width_units);
1062 $this->height =
price2num($this->height);
1063 $this->height_units = trim($this->height_units);
1064 $this->surface =
price2num($this->surface);
1065 $this->surface_units = trim($this->surface_units);
1066 $this->volume =
price2num($this->volume);
1067 $this->volume_units = trim($this->volume_units);
1070 if (is_numeric($this->length_units)) {
1071 $this->width_units = $this->length_units;
1073 if (is_numeric($this->length_units)) {
1074 $this->height_units = $this->length_units;
1078 if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units) {
1079 $this->surface = $this->length * $this->width;
1082 if (empty($this->volume) && !empty($this->surface) && !empty($this->height) && $this->length_units == $this->height_units) {
1083 $this->volume = $this->surface * $this->height;
1087 if (empty($this->tva_tx)) {
1090 if (empty($this->tva_npr)) {
1093 if (empty($this->localtax1_tx)) {
1094 $this->localtax1_tx = 0;
1096 if (empty($this->localtax2_tx)) {
1097 $this->localtax2_tx = 0;
1099 if (empty($this->localtax1_type)) {
1100 $this->localtax1_type =
'0';
1102 if (empty($this->localtax2_type)) {
1103 $this->localtax2_type =
'0';
1105 if (empty($this->status)) {
1108 if (empty($this->status_buy)) {
1109 $this->status_buy = 0;
1112 if (empty($this->country_id)) {
1113 $this->country_id = 0;
1116 if (empty($this->state_id)) {
1117 $this->state_id = 0;
1121 $this->barcode = trim($this->barcode);
1123 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
1124 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
1125 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
1126 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
1127 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
1128 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
1135 if ($action !=
'add') {
1136 $result = $this->
verify();
1144 if (empty($this->oldcopy)) {
1150 if ($this->
hasbatch() && !$this->oldcopy->hasbatch()) {
1152 $valueforundefinedlot =
'000000';
1153 if (!empty($conf->global->STOCK_DEFAULT_BATCH)) {
1154 $valueforundefinedlot = $conf->global->STOCK_DEFAULT_BATCH;
1157 dol_syslog(
"Flag batch of product id=".$this->
id.
" is set to ON, so we will create missing records into product_batch");
1160 foreach ($this->stock_warehouse as $idW => $ObjW) {
1162 foreach ($ObjW->detail_batch as $detail) {
1163 if ($detail->batch == $valueforundefinedlot || $detail->batch ==
'Undefined') {
1165 $sqlclean =
"DELETE FROM ".$this->db->prefix().
"product_batch WHERE batch in('Undefined', '".$this->db->escape($valueforundefinedlot).
"') AND fk_product_stock = ".((int) $ObjW->id);
1166 $result = $this->db->query($sqlclean);
1174 $qty_batch += $detail->qty;
1178 if ($ObjW->real <> $qty_batch) {
1180 $ObjBatch->batch = $valueforundefinedlot;
1181 $ObjBatch->qty = ($ObjW->real - $qty_batch);
1182 $ObjBatch->fk_product_stock = $ObjW->id;
1184 if ($ObjBatch->create($user, 1) < 0) {
1186 $this->errors = $ObjBatch->errors;
1193 if ($this->barcode == -1) {
1194 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
1197 $sql =
"UPDATE ".$this->db->prefix().
"product";
1198 $sql .=
" SET label = '".$this->db->escape($this->label).
"'";
1201 $sql .=
", fk_product_type = ".((int) $this->
type);
1204 $sql .=
", ref = '".$this->db->escape($this->
ref).
"'";
1205 $sql .=
", ref_ext = ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1206 $sql .=
", default_vat_code = ".($this->default_vat_code ?
"'".$this->db->escape($this->default_vat_code).
"'" :
"null");
1207 $sql .=
", tva_tx = ".((float) $this->tva_tx);
1208 $sql .=
", recuperableonly = ".((int) $this->tva_npr);
1209 $sql .=
", localtax1_tx = ".((float) $this->localtax1_tx);
1210 $sql .=
", localtax2_tx = ".((float) $this->localtax2_tx);
1211 $sql .=
", localtax1_type = ".($this->localtax1_type !=
'' ?
"'".$this->db->escape($this->localtax1_type).
"'" :
"'0'");
1212 $sql .=
", localtax2_type = ".($this->localtax2_type !=
'' ?
"'".$this->db->escape($this->localtax2_type).
"'" :
"'0'");
1214 $sql .=
", barcode = ".(empty($this->barcode) ?
"null" :
"'".$this->db->escape($this->barcode).
"'");
1215 $sql .=
", fk_barcode_type = ".(empty($this->barcode_type) ?
"null" : $this->db->escape($this->barcode_type));
1217 $sql .=
", tosell = ".(int) $this->status;
1218 $sql .=
", tobuy = ".(int) $this->status_buy;
1219 $sql .=
", tobatch = ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : (int) $this->status_batch);
1220 $sql .=
", batch_mask = '".$this->db->escape($this->batch_mask).
"'";
1222 $sql .=
", finished = ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
"null" : (int) $this->finished);
1223 $sql .=
", fk_default_bom = ".((!isset($this->fk_default_bom) || $this->fk_default_bom < 0 || $this->fk_default_bom ==
'') ?
"null" : (int) $this->fk_default_bom);
1224 $sql .=
", net_measure = ".($this->net_measure !=
'' ?
"'".$this->db->escape($this->net_measure).
"'" :
'null');
1225 $sql .=
", net_measure_units = ".($this->net_measure_units !=
'' ?
"'".$this->db->escape($this->net_measure_units).
"'" :
'null');
1226 $sql .=
", weight = ".($this->weight !=
'' ?
"'".$this->db->escape($this->weight).
"'" :
'null');
1227 $sql .=
", weight_units = ".($this->weight_units !=
'' ?
"'".$this->db->escape($this->weight_units).
"'" :
'null');
1228 $sql .=
", length = ".($this->length !=
'' ?
"'".$this->db->escape($this->length).
"'" :
'null');
1229 $sql .=
", length_units = ".($this->length_units !=
'' ?
"'".$this->db->escape($this->length_units).
"'" :
'null');
1230 $sql .=
", width= ".($this->width !=
'' ?
"'".$this->db->escape($this->width).
"'" :
'null');
1231 $sql .=
", width_units = ".($this->width_units !=
'' ?
"'".$this->db->escape($this->width_units).
"'" :
'null');
1232 $sql .=
", height = ".($this->height !=
'' ?
"'".$this->db->escape($this->height).
"'" :
'null');
1233 $sql .=
", height_units = ".($this->height_units !=
'' ?
"'".$this->db->escape($this->height_units).
"'" :
'null');
1234 $sql .=
", surface = ".($this->surface !=
'' ?
"'".$this->db->escape($this->surface).
"'" :
'null');
1235 $sql .=
", surface_units = ".($this->surface_units !=
'' ?
"'".$this->db->escape($this->surface_units).
"'" :
'null');
1236 $sql .=
", volume = ".($this->volume !=
'' ?
"'".$this->db->escape($this->volume).
"'" :
'null');
1237 $sql .=
", volume_units = ".($this->volume_units !=
'' ?
"'".$this->db->escape($this->volume_units).
"'" :
'null');
1238 $sql .=
", fk_default_warehouse = ".($this->fk_default_warehouse > 0 ? ((int) $this->fk_default_warehouse) :
'null');
1239 $sql .=
", fk_default_workstation = ".($this->fk_default_workstation > 0 ? ((int) $this->fk_default_workstation) :
'null');
1240 $sql .=
", seuil_stock_alerte = ".((isset($this->seuil_stock_alerte) && is_numeric($this->seuil_stock_alerte)) ? (
float) $this->seuil_stock_alerte :
'null');
1241 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
1242 $sql .=
", url = ".($this->url ?
"'".$this->db->escape($this->url).
"'" :
'null');
1243 $sql .=
", customcode = '".$this->db->escape($this->customcode).
"'";
1244 $sql .=
", fk_country = ".($this->country_id > 0 ? (int) $this->country_id :
'null');
1245 $sql .=
", fk_state = ".($this->state_id > 0 ? (int) $this->state_id :
'null');
1246 $sql .=
", lifetime = ".($this->lifetime > 0 ? (int) $this->lifetime :
'null');
1247 $sql .=
", qc_frequency = ".($this->qc_frequency > 0 ? (int) $this->qc_frequency :
'null');
1248 $sql .=
", note = ".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
'null');
1249 $sql .=
", note_public = ".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
'null');
1250 $sql .=
", duration = '".$this->db->escape($this->duration_value.$this->duration_unit).
"'";
1251 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1252 $sql .=
", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1253 $sql .=
", accountancy_code_buy_intra = '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1254 $sql .=
", accountancy_code_buy_export = '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1255 $sql .=
", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1256 $sql .=
", accountancy_code_sell_intra= '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1257 $sql .=
", accountancy_code_sell_export= '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1259 $sql .=
", desiredstock = ".((isset($this->desiredstock) && is_numeric($this->desiredstock)) ? (
float) $this->desiredstock :
"null");
1260 $sql .=
", cost_price = ".($this->cost_price !=
'' ? $this->db->escape($this->cost_price) :
'null');
1261 $sql .=
", fk_unit= ".(!$this->fk_unit ?
'NULL' : (int) $this->fk_unit);
1262 $sql .=
", price_autogen = ".(!$this->price_autogen ? 0 : 1);
1263 $sql .=
", fk_price_expression = ".($this->fk_price_expression != 0 ? (int) $this->fk_price_expression :
'NULL');
1264 $sql .=
", fk_user_modif = ".($user->id > 0 ? $user->id :
'NULL');
1265 $sql .=
", mandatory_period = ".($this->mandatory_period );
1267 $sql .=
" WHERE rowid = ".((int) $id);
1269 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1271 $resql = $this->db->query($sql);
1285 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1286 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " . ((
int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
1288 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
1289 $sql .=
" fk_product";
1291 $sql .=
", accountancy_code_buy";
1292 $sql .=
", accountancy_code_buy_intra";
1293 $sql .=
", accountancy_code_buy_export";
1294 $sql .=
", accountancy_code_sell";
1295 $sql .=
", accountancy_code_sell_intra";
1296 $sql .=
", accountancy_code_sell_export";
1297 $sql .=
") VALUES (";
1299 $sql .=
", " . $conf->entity;
1300 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1301 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1302 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1303 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1304 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1305 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1307 $result = $this->db->query($sql);
1310 $this->error =
'ErrorFailedToUpdateAccountancyForEntity';
1322 if (!$error && !$notrigger) {
1324 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1331 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
1333 if ($conf->product->dir_output) {
1336 if (file_exists($olddir)) {
1340 $res = @rename($olddir, $newdir);
1342 $langs->load(
"errors");
1343 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
1351 if (isModEnabled(
'variants')) {
1352 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1356 foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
1357 $currcomb->updateProperties($this, $user);
1361 $this->db->commit();
1364 $this->db->rollback();
1368 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1369 $langs->load(
"errors");
1370 if (empty($conf->barcode->enabled) || empty($this->barcode)) {
1371 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductAlreadyExists", $this->
ref);
1373 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductBarCodeAlreadyExists", $this->barcode);
1375 $this->errors[] = $this->error;
1376 $this->db->rollback();
1379 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".$sql;
1380 $this->errors[] = $this->error;
1381 $this->db->rollback();
1386 $this->db->rollback();
1387 dol_syslog(get_class($this).
"::Update fails verify ".join(
',', $this->errors), LOG_WARNING);
1399 public function delete(
User $user, $notrigger = 0)
1401 global $conf, $langs;
1402 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1407 if (empty($this->
id)) {
1408 $this->error =
"Object must be fetched before calling delete";
1412 $this->error =
"ErrorForbidden";
1417 if (empty($objectisused)) {
1420 if (!$error && empty($notrigger)) {
1422 $result = $this->
call_trigger(
'PRODUCT_DELETE', $user);
1431 $sql =
"DELETE FROM ".$this->db->prefix().
'product_batch';
1432 $sql .=
" WHERE fk_product_stock IN (";
1433 $sql .=
"SELECT rowid FROM ".$this->db->prefix().
'product_stock';
1434 $sql .=
" WHERE fk_product = ".((int) $this->
id).
")";
1436 $result = $this->db->query($sql);
1439 $this->errors[] = $this->db->lasterror();
1445 $elements = array(
'product_fournisseur_price',
'product_price',
'product_lang',
'categorie_product',
'product_stock',
'product_customer_price',
'product_lot');
1446 foreach ($elements as $table) {
1448 $sql =
"DELETE FROM ".$this->db->prefix().$table;
1449 $sql .=
" WHERE fk_product = ".(int) $this->
id;
1451 $result = $this->db->query($sql);
1454 $this->errors[] = $this->db->lasterror();
1461 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1462 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
1467 if ($prodcomb->deleteByFkProductParent($user, $this->id) < 0) {
1469 $this->errors[] =
'Error deleting combinations';
1473 if (!$error && ($prodcomb->fetchByFkProductChild($this->id) > 0) && ($prodcomb->delete($user) < 0)) {
1475 $this->errors[] =
'Error deleting child combination';
1481 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
1482 $sql .=
" WHERE fk_product_pere = ".(int) $this->
id.
" OR fk_product_fils = ".(
int) $this->id;
1484 $result = $this->db->query($sql);
1487 $this->errors[] = $this->db->lasterror();
1496 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1502 $sqlz =
"DELETE FROM ".$this->db->prefix().
"product";
1503 $sqlz .=
" WHERE rowid = ".(int) $this->
id;
1505 $resultz = $this->db->query($sqlz);
1508 $this->errors[] = $this->db->lasterror();
1523 if ($conf->product->dir_output) {
1524 $dir = $conf->product->dir_output.
"/".$ref;
1525 if (file_exists($dir)) {
1528 $this->errors[] =
'ErrorFailToDeleteDir';
1536 $this->db->commit();
1539 foreach ($this->errors as $errmsg) {
1540 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1541 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1543 $this->db->rollback();
1547 $this->error =
"ErrorRecordIsUsedCantDelete";
1560 global $conf, $langs;
1562 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
1563 $current_lang = $langs->getDefaultLang();
1565 foreach ($langs_available as $key => $value) {
1566 if ($key == $current_lang) {
1567 $sql =
"SELECT rowid";
1568 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1569 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1570 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1572 $result = $this->db->query($sql);
1574 if ($this->db->num_rows($result)) {
1575 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1577 $sql2 .=
" label='".$this->db->escape($this->label).
"',";
1578 $sql2 .=
" description='".$this->db->escape($this->
description).
"'";
1579 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1580 $sql2 .=
", note='".$this->db->escape($this->other).
"'";
1582 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1584 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1585 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1589 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->label).
"',";
1590 $sql2 .=
" '".$this->db->escape($this->
description).
"'";
1591 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1592 $sql2 .=
", '".$this->db->escape($this->other).
"'";
1596 dol_syslog(get_class($this).
'::setMultiLangs key = current_lang = '.$key);
1597 if (!$this->db->query($sql2)) {
1598 $this->error = $this->db->lasterror();
1601 } elseif (isset($this->multilangs[$key])) {
1602 if (empty($this->multilangs[
"$key"][
"label"])) {
1603 $this->errors[] = $key .
' : ' . $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Label"));
1607 $sql =
"SELECT rowid";
1608 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1609 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1610 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1612 $result = $this->db->query($sql);
1614 if ($this->db->num_rows($result)) {
1615 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1617 $sql2 .=
" label = '".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1618 $sql2 .=
" description = '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1619 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1620 $sql2 .=
", note = '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1622 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1624 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1625 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1629 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1630 $sql2 .=
" '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1631 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1632 $sql2 .=
", '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1638 if ($this->multilangs[
"$key"][
"label"] || $this->multilangs[
"$key"][
"description"]) {
1639 if (!$this->db->query($sql2)) {
1640 $this->error = $this->db->lasterror();
1650 $result = $this->
call_trigger(
'PRODUCT_SET_MULTILANGS', $user);
1652 $this->error = $this->db->lasterror();
1670 $sql =
"DELETE FROM ".$this->db->prefix().
"product_lang";
1671 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($langtodelete).
"'";
1673 dol_syslog(get_class($this).
'::delMultiLangs', LOG_DEBUG);
1674 $result = $this->db->query($sql);
1677 $result = $this->
call_trigger(
'PRODUCT_DEL_MULTILANGS', $user);
1679 $this->error = $this->db->lasterror();
1680 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1686 $this->error = $this->db->lasterror();
1687 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1702 global $user, $langs, $conf;
1708 if ($type ==
'buy') {
1709 $field =
'accountancy_code_buy';
1710 } elseif ($type ==
'buy_intra') {
1711 $field =
'accountancy_code_buy_intra';
1712 } elseif ($type ==
'buy_export') {
1713 $field =
'accountancy_code_buy_export';
1714 } elseif ($type ==
'sell') {
1715 $field =
'accountancy_code_sell';
1716 } elseif ($type ==
'sell_intra') {
1717 $field =
'accountancy_code_sell_intra';
1718 } elseif ($type ==
'sell_export') {
1719 $field =
'accountancy_code_sell_export';
1724 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET ";
1725 $sql .=
"$field = '".$this->db->escape($value).
"'";
1726 $sql .=
" WHERE rowid = ".((int) $this->
id);
1729 $resql = $this->db->query($sql);
1733 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1740 $this->db->rollback();
1744 $this->$field = $value;
1746 $this->db->commit();
1749 $this->error = $this->db->lasterror();
1750 $this->db->rollback();
1764 $current_lang = $langs->getDefaultLang();
1766 $sql =
"SELECT lang, label, description, note as other";
1767 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1768 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1770 $result = $this->db->query($sql);
1772 while ($obj = $this->db->fetch_object($result)) {
1774 if ($obj->lang == $current_lang) {
1775 $this->label = $obj->label;
1777 $this->other = $obj->other;
1779 $this->multilangs[
"$obj->lang"][
"label"] = $obj->label;
1780 $this->multilangs[
"$obj->lang"][
"description"] = $obj->description;
1781 $this->multilangs[
"$obj->lang"][
"other"] = $obj->other;
1785 $this->error =
"Error: ".$this->db->lasterror().
" - ".$sql;
1799 $testExit = array(
'multiprices',
'multiprices_ttc',
'multiprices_base_type',
'multiprices_min',
'multiprices_min_ttc',
'multiprices_tva_tx',
'multiprices_recuperableonly');
1801 foreach ($testExit as $field) {
1802 if (!isset($this->$field)) {
1805 $tmparray = $this->$field;
1806 if (!isset($tmparray[$level])) {
1812 'level' => $level ? $level : 1,
1813 'multiprices' => doubleval($this->multiprices[$level]),
1814 'multiprices_ttc' => doubleval($this->multiprices_ttc[$level]),
1815 'multiprices_base_type' => $this->multiprices_base_type[$level],
1816 'multiprices_min' => doubleval($this->multiprices_min[$level]),
1817 'multiprices_min_ttc' => doubleval($this->multiprices_min_ttc[$level]),
1818 'multiprices_tva_tx' => doubleval($this->multiprices_tva_tx[$level]),
1819 'multiprices_recuperableonly' => doubleval($this->multiprices_recuperableonly[$level]),
1842 if (empty($this->price_by_qty)) {
1843 $this->price_by_qty = 0;
1847 $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,";
1848 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
1849 $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).
",";
1850 $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');
1853 dol_syslog(get_class($this).
"::_log_price", LOG_DEBUG);
1854 $resql = $this->db->query($sql);
1856 $this->error = $this->db->lasterror();
1876 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price_by_qty";
1877 $sql .=
" WHERE fk_product_price = ".((int) $rowid);
1878 $resql = $this->db->query($sql);
1880 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price";
1881 $sql .=
" WHERE rowid=".((int) $rowid);
1882 $resql = $this->db->query($sql);
1886 $this->error = $this->db->lasterror();
1901 public function getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp = 0)
1903 global $conf, $db, $hookmanager, $action;
1906 if (is_object($hookmanager)) {
1907 $parameters = array(
'thirdparty_seller'=>$thirdparty_seller,
'thirdparty_buyer' => $thirdparty_buyer,
'pqp' => $pqp);
1909 $reshook = $hookmanager->executeHooks(
'getSellPrice', $parameters, $this, $action);
1911 return $hookmanager->resArray;
1922 $pu_ht = $this->price;
1923 $pu_ttc = $this->price_ttc;
1924 $price_min = $this->price_min;
1925 $price_base_type = $this->price_base_type;
1928 if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($thirdparty_buyer->price_level)) {
1929 $pu_ht = $this->multiprices[$thirdparty_buyer->price_level];
1930 $pu_ttc = $this->multiprices_ttc[$thirdparty_buyer->price_level];
1931 $price_min = $this->multiprices_min[$thirdparty_buyer->price_level];
1932 $price_base_type = $this->multiprices_base_type[$thirdparty_buyer->price_level];
1933 if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
1934 if (isset($this->multiprices_tva_tx[$thirdparty_buyer->price_level])) {
1935 $tva_tx = $this->multiprices_tva_tx[$thirdparty_buyer->price_level];
1937 if (isset($this->multiprices_recuperableonly[$thirdparty_buyer->price_level])) {
1938 $tva_npr = $this->multiprices_recuperableonly[$thirdparty_buyer->price_level];
1944 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1946 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
1950 $filter = array(
't.fk_product' => $this->
id,
't.fk_soc' => $thirdparty_buyer->id);
1952 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
1954 if (count($prodcustprice->lines) > 0) {
1955 $pu_ht =
price($prodcustprice->lines[0]->price);
1956 $price_min =
price($prodcustprice->lines[0]->price_min);
1957 $pu_ttc =
price($prodcustprice->lines[0]->price_ttc);
1958 $price_base_type = $prodcustprice->lines[0]->price_base_type;
1959 $tva_tx = $prodcustprice->lines[0]->tva_tx;
1960 if ($prodcustprice->lines[0]->default_vat_code && !preg_match(
'/\(.*\)/',
$tva_tx)) {
1961 $tva_tx .=
' ('.$prodcustprice->lines[0]->default_vat_code.
')';
1963 $tva_npr = $prodcustprice->lines[0]->recuperableonly;
1969 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
1971 if ($this->prices_by_qty[0]) {
1974 foreach ($this->prices_by_qty_list[0] as $priceforthequantityarray) {
1975 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1979 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1980 $pu_ht = $priceforthequantityarray[
'unitprice'];
1982 $pu_ttc = $priceforthequantityarray[
'unitprice'];
1987 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1989 if ($this->prices_by_qty[$thirdparty_buyer->price_level]) {
1992 foreach ($this->prices_by_qty_list[$thirdparty_buyer->price_level] as $priceforthequantityarray) {
1993 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1997 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1998 $pu_ht = $priceforthequantityarray[
'unitprice'];
2000 $pu_ttc = $priceforthequantityarray[
'unitprice'];
2007 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);
2024 public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref =
'', $fk_soc = 0)
2031 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2032 $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,";
2033 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2034 $sql .=
" pfp.packaging";
2035 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2036 $sql .=
" WHERE pfp.rowid = ".((int) $prodfournprice);
2038 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2040 $sql .=
" ORDER BY pfp.quantity DESC";
2042 dol_syslog(get_class($this).
"::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
2043 $resql = $this->db->query($sql);
2045 $obj = $this->db->fetch_object($resql);
2046 if ($obj && $obj->quantity > 0) {
2047 if (isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2049 $prod_supplier->product_fourn_price_id = $obj->rowid;
2050 $prod_supplier->id = $obj->fk_product;
2051 $prod_supplier->fourn_qty = $obj->quantity;
2052 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2053 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2055 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2057 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2058 if ($price_result >= 0) {
2059 $obj->price = $price_result;
2062 $this->product_fourn_price_id = $obj->rowid;
2063 $this->buyprice = $obj->price;
2064 $this->fourn_pu = $obj->price / $obj->quantity;
2065 $this->fourn_price_base_type =
'HT';
2066 $this->fourn_socid = $obj->fk_soc;
2067 $this->ref_fourn = $obj->ref_supplier;
2068 $this->ref_supplier = $obj->ref_supplier;
2069 $this->desc_supplier = $obj->desc_supplier;
2070 $this->remise_percent = $obj->remise_percent;
2071 $this->vatrate_supplier = $obj->tva_tx;
2072 $this->default_vat_code_supplier = $obj->default_vat_code;
2073 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2074 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2075 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2076 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2077 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2078 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2079 $this->packaging = $obj->packaging;
2081 $result = $obj->fk_product;
2085 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2086 $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,";
2087 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2088 $sql .=
" pfp.packaging";
2089 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2090 $sql .=
" WHERE 1 = 1";
2091 if ($product_id > 0) {
2092 $sql .=
" AND pfp.fk_product = ".((int) $product_id);
2094 if ($fourn_ref !=
'none') {
2095 $sql .=
" AND pfp.ref_fourn = '".$this->db->escape($fourn_ref).
"'";
2098 $sql .=
" AND pfp.fk_soc = ".((int) $fk_soc);
2101 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2103 $sql .=
" ORDER BY pfp.quantity DESC";
2106 dol_syslog(get_class($this).
"::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
2107 $resql = $this->db->query($sql);
2109 $obj = $this->db->fetch_object($resql);
2110 if ($obj && $obj->quantity > 0) {
2111 if (isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2113 $prod_supplier->product_fourn_price_id = $obj->rowid;
2114 $prod_supplier->id = $obj->fk_product;
2115 $prod_supplier->fourn_qty = $obj->quantity;
2116 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2117 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2119 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2121 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2123 $obj->price = $price_result;
2126 $this->product_fourn_price_id = $obj->rowid;
2127 $this->buyprice = $obj->price;
2128 $this->fourn_qty = $obj->quantity;
2129 $this->fourn_pu = $obj->price / $obj->quantity;
2130 $this->fourn_price_base_type =
'HT';
2131 $this->fourn_socid = $obj->fk_soc;
2132 $this->ref_fourn = $obj->ref_supplier;
2133 $this->ref_supplier = $obj->ref_supplier;
2134 $this->desc_supplier = $obj->desc_supplier;
2135 $this->remise_percent = $obj->remise_percent;
2136 $this->vatrate_supplier = $obj->tva_tx;
2137 $this->default_vat_code_supplier = $obj->default_vat_code;
2138 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2139 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2140 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2141 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2142 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2143 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2144 $this->packaging = $obj->packaging;
2146 $result = $obj->fk_product;
2152 $this->error = $this->db->lasterror();
2157 $this->error = $this->db->lasterror();
2179 public function updatePrice($newprice, $newpricebase, $user, $newvat =
'', $newminprice = 0, $level = 0, $newnpr = 0, $newpbq = 0, $ignore_autogen = 0, $localtaxes_array = array(), $newdefaultvatcode =
'')
2181 global $conf, $langs;
2187 dol_syslog(get_class($this).
"::update_price id=".$id.
" newprice=".$newprice.
" newpricebase=".$newpricebase.
" newminprice=".$newminprice.
" level=".$level.
" npr=".$newnpr.
" newdefaultvatcode=".$newdefaultvatcode);
2190 if (empty($this->tva_tx)) {
2193 if (empty($newnpr)) {
2196 if (empty($newminprice)) {
2199 if (empty($newminprice)) {
2204 if ($newvat ==
'') {
2210 if ((!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && !$ignore_autogen && $this->price_autogen && ($level == 1)) {
2211 return $this->
generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpbq);
2214 if (!empty($newminprice) && ($newminprice > $newprice)) {
2215 $this->error =
'ErrorPriceCantBeLowerThanMinPrice';
2219 if ($newprice !==
'' || $newprice === 0) {
2220 if ($newpricebase ==
'TTC') {
2221 $price_ttc =
price2num($newprice,
'MU');
2222 $price =
price2num($newprice) / (1 + ($newvat / 100));
2225 if ($newminprice !=
'' || $newminprice == 0) {
2226 $price_min_ttc =
price2num($newminprice,
'MU');
2227 $price_min =
price2num($newminprice) / (1 + ($newvat / 100));
2228 $price_min =
price2num($price_min,
'MU');
2235 $price_ttc = ($newnpr != 1) ? (
float)
price2num($newprice) * (1 + ($newvat / 100)) : $price;
2236 $price_ttc =
price2num($price_ttc,
'MU');
2238 if ($newminprice !==
'' || $newminprice === 0) {
2239 $price_min =
price2num($newminprice,
'MU');
2240 $price_min_ttc =
price2num($newminprice) * (1 + ($newvat / 100));
2241 $price_min_ttc =
price2num($price_min_ttc,
'MU');
2250 if (count($localtaxes_array) > 0) {
2251 $localtaxtype1 = $localtaxes_array[
'0'];
2252 $localtax1 = $localtaxes_array[
'1'];
2253 $localtaxtype2 = $localtaxes_array[
'2'];
2254 $localtax2 = $localtaxes_array[
'3'];
2257 if (!empty($newdefaultvatcode)) {
2260 $sql =
"SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
2261 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as t, ".MAIN_DB_PREFIX.
"c_country as c";
2262 $sql .=
" WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($mysoc->country_code).
"'";
2263 $sql .=
" AND t.taux = ".((float) $newdefaultvatcode).
" AND t.active = 1";
2264 $sql .=
" AND t.code = '".$this->db->escape($newdefaultvatcode).
"'";
2265 $resql = $this->db->query($sql);
2267 $obj = $this->db->fetch_object($resql);
2269 $npr = $obj->recuperableonly;
2270 $localtax1 = $obj->localtax1;
2271 $localtax2 = $obj->localtax2;
2272 $localtaxtype1 = $obj->localtax1_type;
2273 $localtaxtype2 = $obj->localtax2_type;
2278 $localtaxtype1 =
'0';
2280 $localtaxtype2 =
'0';
2284 if (empty($localtax1)) {
2287 if (empty($localtax2)) {
2295 $sql =
"UPDATE ".$this->db->prefix().
"product SET";
2296 $sql .=
" price_base_type='".$this->db->escape($newpricebase).
"',";
2297 $sql .=
" price=".$price.
",";
2298 $sql .=
" price_ttc=".$price_ttc.
",";
2299 $sql .=
" price_min=".$price_min.
",";
2300 $sql .=
" price_min_ttc=".$price_min_ttc.
",";
2301 $sql .=
" localtax1_tx=".($localtax1 >= 0 ? $localtax1 :
'NULL').
",";
2302 $sql .=
" localtax2_tx=".($localtax2 >= 0 ? $localtax2 :
'NULL').
",";
2303 $sql .=
" localtax1_type=".($localtaxtype1 !=
'' ?
"'".$this->db->escape($localtaxtype1).
"'" :
"'0'").
",";
2304 $sql .=
" localtax2_type=".($localtaxtype2 !=
'' ?
"'".$this->db->escape($localtaxtype2).
"'" :
"'0'").
",";
2305 $sql .=
" default_vat_code=".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
2306 $sql .=
" tva_tx='".price2num($newvat).
"',";
2307 $sql .=
" recuperableonly='".$this->db->escape($newnpr).
"'";
2308 $sql .=
" WHERE rowid = ".((int) $id);
2310 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
2311 $resql = $this->db->query($sql);
2313 $this->multiprices[$level] = $price;
2314 $this->multiprices_ttc[$level] = $price_ttc;
2315 $this->multiprices_min[$level] = $price_min;
2316 $this->multiprices_min_ttc[$level] = $price_min_ttc;
2317 $this->multiprices_base_type[$level] = $newpricebase;
2318 $this->multiprices_default_vat_code[$level] = $newdefaultvatcode;
2319 $this->multiprices_tva_tx[$level] = $newvat;
2320 $this->multiprices_recuperableonly[$level] = $newnpr;
2322 $this->
price = $price;
2323 $this->price_ttc = $price_ttc;
2324 $this->price_min = $price_min;
2325 $this->price_min_ttc = $price_min_ttc;
2326 $this->price_base_type = $newpricebase;
2327 $this->default_vat_code = $newdefaultvatcode;
2328 $this->tva_tx = $newvat;
2329 $this->tva_npr = $newnpr;
2331 $this->localtax1_tx = $localtax1;
2332 $this->localtax2_tx = $localtax2;
2333 $this->localtax1_type = $localtaxtype1;
2334 $this->localtax2_type = $localtaxtype2;
2337 $this->price_by_qty = $newpbq;
2341 if (!empty(array_diff_assoc($newPriceData, $lastPriceData)) || empty($conf->global->PRODUIT_MULTIPRICES)) {
2345 $this->level = $level;
2348 $result = $this->
call_trigger(
'PRODUCT_PRICE_MODIFY', $user);
2350 $this->db->rollback();
2355 $this->db->commit();
2357 $this->db->rollback();
2358 $this->error = $this->db->lasterror();
2377 $this->fk_price_expression = $expression_id;
2379 return $this->
update($this->
id, $user);
2394 public function fetch($id =
'', $ref =
'', $ref_ext =
'', $barcode =
'', $ignore_expression = 0, $ignore_price_load = 0, $ignore_lang_load = 0)
2396 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
2398 global $langs, $conf;
2400 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
2403 if (!$id && !$ref && !$ref_ext && !$barcode) {
2404 $this->error =
'ErrorWrongParameters';
2405 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
2409 $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,";
2410 $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,";
2411 $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,";
2412 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2413 $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,";
2414 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2415 $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,";
2417 $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,";
2422 $separatedEntityPMP =
false;
2423 $separatedStock =
false;
2424 $visibleWarehousesEntities = $conf->entity;
2425 if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) {
2426 if (!empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
2427 $checkPMPPerEntity = $this->db->query(
"SELECT pmp FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = ".((int) $id).
" AND entity = ".(
int) $conf->entity);
2428 if ($this->db->num_rows($checkPMPPerEntity)>0) {
2429 $separatedEntityPMP =
true;
2433 $separatedStock =
true;
2434 if (isset($mc->sharings[
'stock']) && !empty($mc->sharings[
'stock'])) {
2435 $visibleWarehousesEntities .=
"," . implode(
",", $mc->sharings[
'stock']);
2438 if ($separatedEntityPMP) {
2439 $sql .=
" ppe.pmp,";
2443 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2444 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf,";
2445 if ($separatedStock) {
2446 $sql .=
" SUM(sp.reel) as stock";
2450 $sql .=
" FROM ".$this->db->prefix().
"product as p";
2451 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) || $separatedEntityPMP) {
2452 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
2454 if ($separatedStock) {
2455 $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).
"))";
2459 $sql .=
" WHERE p.rowid = ".((int) $id);
2461 $sql .=
" WHERE p.entity IN (".getEntity($this->element).
")";
2463 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
2464 } elseif ($ref_ext) {
2465 $sql .=
" AND p.ref_ext = '".$this->db->escape($ref_ext).
"'";
2466 } elseif ($barcode) {
2467 $sql .=
" AND p.barcode = '".$this->db->escape($barcode).
"'";
2470 if ($separatedStock) {
2471 $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,";
2472 $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,";
2473 $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,";
2474 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2475 $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,";
2476 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2477 $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,";
2479 $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,";
2481 if ($separatedEntityPMP) {
2482 $sql .=
" ppe.pmp,";
2486 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2487 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf";
2488 if (!$separatedStock) {
2489 $sql .=
", p.stock";
2493 $resql = $this->db->query($sql);
2495 unset($this->oldcopy);
2497 if ($this->db->num_rows($resql) > 0) {
2498 $obj = $this->db->fetch_object($resql);
2500 $this->
id = $obj->rowid;
2501 $this->
ref = $obj->ref;
2502 $this->ref_ext = $obj->ref_ext;
2503 $this->label = $obj->label;
2505 $this->url = $obj->url;
2506 $this->note_public = $obj->note_public;
2507 $this->note_private = $obj->note_private;
2508 $this->note = $obj->note_private;
2510 $this->
type = $obj->fk_product_type;
2511 $this->status = $obj->tosell;
2512 $this->status_buy = $obj->tobuy;
2513 $this->status_batch = $obj->tobatch;
2514 $this->batch_mask = $obj->batch_mask;
2516 $this->customcode = $obj->customcode;
2517 $this->country_id = $obj->fk_country;
2518 $this->country_code =
getCountry($this->country_id, 2, $this->db);
2519 $this->state_id = $obj->fk_state;
2520 $this->lifetime = $obj->lifetime;
2521 $this->qc_frequency = $obj->qc_frequency;
2522 $this->
price = $obj->price;
2523 $this->price_ttc = $obj->price_ttc;
2524 $this->price_min = $obj->price_min;
2525 $this->price_min_ttc = $obj->price_min_ttc;
2526 $this->price_base_type = $obj->price_base_type;
2527 $this->cost_price = $obj->cost_price;
2528 $this->default_vat_code = $obj->default_vat_code;
2529 $this->tva_tx = $obj->tva_tx;
2531 $this->tva_npr = $obj->tva_npr;
2532 $this->recuperableonly = $obj->tva_npr;
2534 $this->localtax1_tx = $obj->localtax1_tx;
2535 $this->localtax2_tx = $obj->localtax2_tx;
2536 $this->localtax1_type = $obj->localtax1_type;
2537 $this->localtax2_type = $obj->localtax2_type;
2539 $this->finished = $obj->finished;
2540 $this->fk_default_bom = $obj->fk_default_bom;
2542 $this->duration = $obj->duration;
2543 $this->duration_value = $obj->duration ? (int) (substr($obj->duration, 0,
dol_strlen($obj->duration) - 1)) : 0;
2544 $this->duration_unit = $obj->duration ? substr($obj->duration, -1) :
null;
2545 $this->canvas = $obj->canvas;
2546 $this->net_measure = $obj->net_measure;
2547 $this->net_measure_units = $obj->net_measure_units;
2548 $this->weight = $obj->weight;
2549 $this->weight_units = $obj->weight_units;
2550 $this->length = $obj->length;
2551 $this->length_units = $obj->length_units;
2552 $this->width = $obj->width;
2553 $this->width_units = $obj->width_units;
2554 $this->height = $obj->height;
2555 $this->height_units = $obj->height_units;
2557 $this->surface = $obj->surface;
2558 $this->surface_units = $obj->surface_units;
2559 $this->volume = $obj->volume;
2560 $this->volume_units = $obj->volume_units;
2561 $this->barcode = $obj->barcode;
2562 $this->barcode_type = $obj->fk_barcode_type;
2564 $this->accountancy_code_buy = $obj->accountancy_code_buy;
2565 $this->accountancy_code_buy_intra = $obj->accountancy_code_buy_intra;
2566 $this->accountancy_code_buy_export = $obj->accountancy_code_buy_export;
2567 $this->accountancy_code_sell = $obj->accountancy_code_sell;
2568 $this->accountancy_code_sell_intra = $obj->accountancy_code_sell_intra;
2569 $this->accountancy_code_sell_export = $obj->accountancy_code_sell_export;
2571 $this->fk_default_warehouse = $obj->fk_default_warehouse;
2572 $this->fk_default_workstation = $obj->fk_default_workstation;
2573 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
2574 $this->desiredstock = $obj->desiredstock;
2575 $this->stock_reel = $obj->stock;
2576 $this->pmp = $obj->pmp;
2578 $this->date_creation = $obj->datec;
2579 $this->date_modification = $obj->tms;
2580 $this->import_key = $obj->import_key;
2581 $this->entity = $obj->entity;
2583 $this->ref_ext = $obj->ref_ext;
2584 $this->fk_price_expression = $obj->fk_price_expression;
2585 $this->fk_unit = $obj->fk_unit;
2586 $this->price_autogen = $obj->price_autogen;
2587 $this->model_pdf = $obj->model_pdf;
2589 $this->mandatory_period = $obj->mandatory_period;
2591 $this->db->free($resql);
2602 if (!empty($conf->global->PRODUIT_MULTIPRICES) && empty($ignore_price_load)) {
2603 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2604 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2605 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2606 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2607 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2608 $sql .=
" AND price_level=".((int) $i);
2609 $sql .=
" AND fk_product = ".((int) $this->
id);
2610 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2612 $resql = $this->db->query($sql);
2614 $result = $this->db->fetch_array($resql);
2616 $this->multiprices[$i] = $result ? $result[
"price"] :
null;
2617 $this->multiprices_ttc[$i] = $result ? $result[
"price_ttc"] :
null;
2618 $this->multiprices_min[$i] = $result ? $result[
"price_min"] :
null;
2619 $this->multiprices_min_ttc[$i] = $result ? $result[
"price_min_ttc"] :
null;
2620 $this->multiprices_base_type[$i] = $result ? $result[
"price_base_type"] :
null;
2622 $this->multiprices_tva_tx[$i] = $result ? $result[
"tva_tx"].($result ?
' ('.$result[
'default_vat_code'].
')' :
'') :
null;
2623 $this->multiprices_recuperableonly[$i] = $result ? $result[
"recuperableonly"] :
null;
2661 $this->error = $this->db->lasterror;
2665 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && empty($ignore_price_load)) {
2667 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) && empty($ignore_price_load)) {
2668 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2669 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
2670 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2671 $sql .=
" WHERE fk_product = ".((int) $this->
id);
2672 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2674 $resql = $this->db->query($sql);
2676 $result = $this->db->fetch_array($resql);
2679 $this->prices_by_qty[0] = $result[
"price_by_qty"];
2680 $this->prices_by_qty_id[0] = $result[
"rowid"];
2682 if ($this->prices_by_qty[0] == 1) {
2683 $sql =
"SELECT rowid,price, unitprice, quantity, remise_percent, remise, remise, price_base_type";
2684 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2685 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[0]);
2686 $sql .=
" ORDER BY quantity ASC";
2687 $resultat = array();
2688 $resql = $this->db->query($sql);
2691 while ($result = $this->db->fetch_array($resql)) {
2692 $resultat[$ii] = array();
2693 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2694 $resultat[$ii][
"price"] = $result[
"price"];
2695 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2696 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2697 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2699 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2702 $this->prices_by_qty_list[0] = $resultat;
2704 $this->error = $this->db->lasterror;
2709 $this->error = $this->db->lasterror;
2712 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES) && empty($ignore_price_load)) {
2713 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2714 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2715 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2716 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2717 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2718 $sql .=
" AND price_level=".((int) $i);
2719 $sql .=
" AND fk_product = ".((int) $this->
id);
2720 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2722 $resql = $this->db->query($sql);
2724 $result = $this->db->fetch_array($resql);
2726 $this->multiprices[$i] = (!empty($result[
"price"]) ? $result[
"price"] : 0);
2727 $this->multiprices_ttc[$i] = (!empty($result[
"price_ttc"]) ? $result[
"price_ttc"] : 0);
2728 $this->multiprices_min[$i] = (!empty($result[
"price_min"]) ? $result[
"price_min"] : 0);
2729 $this->multiprices_min_ttc[$i] = (!empty($result[
"price_min_ttc"]) ? $result[
"price_min_ttc"] : 0);
2730 $this->multiprices_base_type[$i] = (!empty($result[
"price_base_type"]) ? $result[
"price_base_type"] :
'');
2732 $this->multiprices_tva_tx[$i] = (!empty($result[
"tva_tx"]) ? $result[
"tva_tx"] : 0);
2733 $this->multiprices_recuperableonly[$i] = (!empty($result[
"recuperableonly"]) ? $result[
"recuperableonly"] : 0);
2736 $this->prices_by_qty[$i] = (!empty($result[
"price_by_qty"]) ? $result[
"price_by_qty"] : 0);
2737 $this->prices_by_qty_id[$i] = (!empty($result[
"rowid"]) ? $result[
"rowid"] : 0);
2739 if ($this->prices_by_qty[$i] == 1) {
2740 $sql =
"SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type";
2741 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2742 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[$i]);
2743 $sql .=
" ORDER BY quantity ASC";
2744 $resultat = array();
2745 $resql = $this->db->query($sql);
2748 while ($result = $this->db->fetch_array($resql)) {
2749 $resultat[$ii] = array();
2750 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2751 $resultat[$ii][
"price"] = $result[
"price"];
2752 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2753 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2754 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2755 $resultat[$ii][
"remise"] = $result[
"remise"];
2756 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2759 $this->prices_by_qty_list[$i] = $resultat;
2761 $this->error = $this->db->lasterror;
2766 $this->error = $this->db->lasterror;
2772 if (isModEnabled(
'dynamicprices') && !empty($this->fk_price_expression) && empty($ignore_expression)) {
2773 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2775 $price_result = $priceparser->parseProduct($this);
2776 if ($price_result >= 0) {
2777 $this->
price = $price_result;
2779 $this->price_ttc =
price2num($this->
price) * (1 + ($this->tva_tx / 100));
2780 $this->price_ttc =
price2num($this->price_ttc,
'MU');
2786 $this->stock_warehouse = array();
2793 $this->error = $this->db->lasterror();
2808 global $user, $hookmanager, $action;
2812 foreach (array(
'toconsume',
'consumed',
'toproduce',
'produced') as $role) {
2813 $this->stats_mo[
'customers_'.$role] = 0;
2814 $this->stats_mo[
'nb_'.$role] = 0;
2815 $this->stats_mo[
'qty_'.$role] = 0;
2817 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
2818 $sql .=
" SUM(mp.qty) as qty";
2819 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as c";
2820 $sql .=
" INNER JOIN ".$this->db->prefix().
"mrp_production as mp ON mp.fk_mo=c.rowid";
2821 if (empty($user->rights->societe->client->voir) && !$socid) {
2822 $sql .=
"INNER JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON sc.fk_soc=c.fk_soc AND sc.fk_user = ".((int) $user->id);
2825 $sql .=
" c.entity IN (".getEntity(
'mo').
")";
2827 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
2828 $sql .=
" AND mp.role ='".$this->db->escape($role).
"'";
2830 $sql .=
" AND c.fk_soc = ".((int) $socid);
2833 $result = $this->db->query($sql);
2835 $obj = $this->db->fetch_object($result);
2836 $this->stats_mo[
'customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
2837 $this->stats_mo[
'nb_'.$role] = $obj->nb ? $obj->nb : 0;
2838 $this->stats_mo[
'qty_'.$role] = $obj->qty ?
price2num($obj->qty,
'MS') : 0;
2840 $this->error = $this->db->error();
2845 if (!empty($error)) {
2849 $parameters = array(
'socid' => $socid);
2850 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2852 $this->stats_mo = $hookmanager->resArray[
'stats_mo'];
2868 global $user, $hookmanager, $action;
2872 $this->stats_bom[
'nb_toproduce'] = 0;
2873 $this->stats_bom[
'nb_toconsume'] = 0;
2874 $this->stats_bom[
'qty_toproduce'] = 0;
2875 $this->stats_bom[
'qty_toconsume'] = 0;
2877 $sql =
"SELECT COUNT(DISTINCT b.rowid) as nb_toproduce,";
2878 $sql .=
" SUM(b.qty) as qty_toproduce";
2879 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2880 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2882 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2883 $sql .=
" AND b.fk_product =".((int) $this->
id);
2884 $sql .=
" GROUP BY b.rowid";
2886 $result = $this->db->query($sql);
2888 $obj = $this->db->fetch_object($result);
2889 $this->stats_bom[
'nb_toproduce'] = !empty($obj->nb_toproduce) ? $obj->nb_toproduce : 0;
2890 $this->stats_bom[
'qty_toproduce'] = !empty($obj->qty_toproduce) ?
price2num($obj->qty_toproduce) : 0;
2892 $this->error = $this->db->error();
2896 $sql =
"SELECT COUNT(DISTINCT bl.rowid) as nb_toconsume,";
2897 $sql .=
" SUM(bl.qty) as qty_toconsume";
2898 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2899 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2901 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2902 $sql .=
" AND bl.fk_product =".((int) $this->
id);
2904 $result = $this->db->query($sql);
2906 $obj = $this->db->fetch_object($result);
2907 $this->stats_bom[
'nb_toconsume'] = !empty($obj->nb_toconsume) ? $obj->nb_toconsume : 0;
2908 $this->stats_bom[
'qty_toconsume'] = !empty($obj->qty_toconsume) ?
price2num($obj->qty_toconsume) : 0;
2910 $this->error = $this->db->error();
2914 if (!empty($error)) {
2918 $parameters = array(
'socid' => $socid);
2919 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2921 $this->stats_bom = $hookmanager->resArray[
'stats_bom'];
2937 global $conf, $user, $hookmanager, $action;
2939 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb,";
2940 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
2941 $sql .=
" FROM ".$this->db->prefix().
"propaldet as pd";
2942 $sql .=
", ".$this->db->prefix().
"propal as p";
2943 $sql .=
", ".$this->db->prefix().
"societe as s";
2944 if (empty($user->rights->societe->client->voir) && !$socid) {
2945 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
2947 $sql .=
" WHERE p.rowid = pd.fk_propal";
2948 $sql .=
" AND p.fk_soc = s.rowid";
2949 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
2950 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
2951 if (empty($user->rights->societe->client->voir) && !$socid) {
2952 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2956 $sql .=
" AND p.fk_soc = ".((int) $socid);
2959 $result = $this->db->query($sql);
2961 $obj = $this->db->fetch_object($result);
2962 $this->stats_propale[
'customers'] = $obj->nb_customers;
2963 $this->stats_propale[
'nb'] = $obj->nb;
2964 $this->stats_propale[
'rows'] = $obj->nb_rows;
2965 $this->stats_propale[
'qty'] = $obj->qty ? $obj->qty : 0;
2968 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
2970 if (is_array($TFather) && !empty($TFather)) {
2971 foreach ($TFather as &$fatherData) {
2972 $pFather =
new Product($this->db);
2973 $pFather->id = $fatherData[
'id'];
2974 $qtyCoef = $fatherData[
'qty'];
2976 if ($fatherData[
'incdec']) {
2977 $pFather->load_stats_propale($socid);
2979 $this->stats_propale[
'customers'] += $pFather->stats_propale[
'customers'];
2980 $this->stats_propale[
'nb'] += $pFather->stats_propale[
'nb'];
2981 $this->stats_propale[
'rows'] += $pFather->stats_propale[
'rows'];
2982 $this->stats_propale[
'qty'] += $pFather->stats_propale[
'qty'] * $qtyCoef;
2988 $parameters = array(
'socid' => $socid);
2989 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerProposal', $parameters, $this, $action);
2991 $this->stats_propale = $hookmanager->resArray[
'stats_propale'];
2996 $this->error = $this->db->error();
3012 global $conf, $user, $hookmanager, $action;
3014 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_suppliers, COUNT(DISTINCT p.rowid) as nb,";
3015 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
3016 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as pd";
3017 $sql .=
", ".$this->db->prefix().
"supplier_proposal as p";
3018 $sql .=
", ".$this->db->prefix().
"societe as s";
3019 if (empty($user->rights->societe->client->voir) && !$socid) {
3020 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3022 $sql .=
" WHERE p.rowid = pd.fk_supplier_proposal";
3023 $sql .=
" AND p.fk_soc = s.rowid";
3024 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
3025 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
3026 if (empty($user->rights->societe->client->voir) && !$socid) {
3027 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3031 $sql .=
" AND p.fk_soc = ".((int) $socid);
3034 $result = $this->db->query($sql);
3036 $obj = $this->db->fetch_object($result);
3037 $this->stats_proposal_supplier[
'suppliers'] = $obj->nb_suppliers;
3038 $this->stats_proposal_supplier[
'nb'] = $obj->nb;
3039 $this->stats_proposal_supplier[
'rows'] = $obj->nb_rows;
3040 $this->stats_proposal_supplier[
'qty'] = $obj->qty ? $obj->qty : 0;
3042 $parameters = array(
'socid' => $socid);
3043 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierProposal', $parameters, $this, $action);
3045 $this->stats_proposal_supplier = $hookmanager->resArray[
'stats_proposal_supplier'];
3050 $this->error = $this->db->error();
3068 global $conf, $user, $hookmanager, $action;
3070 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3071 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3072 $sql .=
" FROM ".$this->db->prefix().
"commandedet as cd";
3073 $sql .=
", ".$this->db->prefix().
"commande as c";
3074 $sql .=
", ".$this->db->prefix().
"societe as s";
3075 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3076 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3078 $sql .=
" WHERE c.rowid = cd.fk_commande";
3079 $sql .=
" AND c.fk_soc = s.rowid";
3080 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'commande').
")";
3081 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3082 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3083 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3086 $sql .=
" AND c.fk_soc = ".((int) $socid);
3088 if ($filtrestatut <>
'') {
3089 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3092 $result = $this->db->query($sql);
3094 $obj = $this->db->fetch_object($result);
3095 $this->stats_commande[
'customers'] = $obj->nb_customers;
3096 $this->stats_commande[
'nb'] = $obj->nb;
3097 $this->stats_commande[
'rows'] = $obj->nb_rows;
3098 $this->stats_commande[
'qty'] = $obj->qty ? $obj->qty : 0;
3101 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3103 if (is_array($TFather) && !empty($TFather)) {
3104 foreach ($TFather as &$fatherData) {
3105 $pFather =
new Product($this->db);
3106 $pFather->id = $fatherData[
'id'];
3107 $qtyCoef = $fatherData[
'qty'];
3109 if ($fatherData[
'incdec']) {
3110 $pFather->load_stats_commande($socid, $filtrestatut);
3112 $this->stats_commande[
'customers'] += $pFather->stats_commande[
'customers'];
3113 $this->stats_commande[
'nb'] += $pFather->stats_commande[
'nb'];
3114 $this->stats_commande[
'rows'] += $pFather->stats_commande[
'rows'];
3115 $this->stats_commande[
'qty'] += $pFather->stats_commande[
'qty'] * $qtyCoef;
3123 if ($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
3124 if (!empty($conf->global->DECREASE_ONLY_UNINVOICEDPRODUCTS)) {
3127 $sql =
"SELECT SUM(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".$this->db->prefix().
"facturedet as fd ";
3128 $sql .=
" JOIN ".$this->db->prefix().
"facture as f ON fd.fk_facture = f.rowid";
3129 $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'))";
3130 $sql .=
" JOIN ".$this->db->prefix().
"commande as c ON el.fk_source = c.rowid";
3131 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND c.facture = 0 AND fd.fk_product = ".((int) $this->
id);
3133 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3134 $resql = $this->db->query($sql);
3136 if ($this->db->num_rows($resql) > 0) {
3137 $obj = $this->db->fetch_object($resql);
3138 $adeduire += $obj->count;
3142 $this->stats_commande[
'qty'] -= $adeduire;
3145 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3149 $sql =
"SELECT sum(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".MAIN_DB_PREFIX.
"facturedet as fd ";
3150 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture as f ON fd.fk_facture = f.rowid";
3151 $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'))";
3152 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c ON el.fk_source = c.rowid";
3153 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND f.fk_statut > ".
Facture::STATUS_DRAFT.
" AND fd.fk_product = ".((int) $this->
id);
3155 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3156 $resql = $this->db->query($sql);
3158 if ($this->db->num_rows($resql) > 0) {
3159 $obj = $this->db->fetch_object($resql);
3160 $adeduire += $obj->count;
3163 $this->error = $this->db->error();
3167 $this->stats_commande[
'qty'] -= $adeduire;
3171 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3172 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerOrder', $parameters, $this, $action);
3174 $this->stats_commande = $hookmanager->resArray[
'stats_commande'];
3178 $this->error = $this->db->error();
3196 global $conf, $user, $hookmanager, $action;
3198 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb,";
3199 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3200 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as cd";
3201 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as c";
3202 $sql .=
", ".$this->db->prefix().
"societe as s";
3203 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3204 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3206 $sql .=
" WHERE c.rowid = cd.fk_commande";
3207 $sql .=
" AND c.fk_soc = s.rowid";
3208 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3209 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3210 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3211 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3214 $sql .=
" AND c.fk_soc = ".((int) $socid);
3216 if ($filtrestatut !=
'') {
3217 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3219 if (!empty($dateofvirtualstock)) {
3220 $sql .=
" AND c.date_livraison <= '".$this->db->idate($dateofvirtualstock).
"'";
3223 $result = $this->db->query($sql);
3225 $obj = $this->db->fetch_object($result);
3226 $this->stats_commande_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3227 $this->stats_commande_fournisseur[
'nb'] = $obj->nb;
3228 $this->stats_commande_fournisseur[
'rows'] = $obj->nb_rows;
3229 $this->stats_commande_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3231 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3232 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierOrder', $parameters, $this, $action);
3234 $this->stats_commande_fournisseur = $hookmanager->resArray[
'stats_commande_fournisseur'];
3239 $this->error = $this->db->error().
' sql='.$sql;
3254 public function load_stats_sending($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $filterShipmentStatus =
'')
3257 global $conf, $user, $hookmanager, $action;
3259 $sql =
"SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb,";
3260 $sql .=
" COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
3261 $sql .=
" FROM ".$this->db->prefix().
"expeditiondet as ed";
3262 $sql .=
", ".$this->db->prefix().
"commandedet as cd";
3263 $sql .=
", ".$this->db->prefix().
"commande as c";
3264 $sql .=
", ".$this->db->prefix().
"expedition as e";
3265 $sql .=
", ".$this->db->prefix().
"societe as s";
3266 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3267 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3269 $sql .=
" WHERE e.rowid = ed.fk_expedition";
3270 $sql .=
" AND c.rowid = cd.fk_commande";
3271 $sql .=
" AND e.fk_soc = s.rowid";
3272 $sql .=
" AND e.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'expedition').
")";
3273 $sql .=
" AND ed.fk_origin_line = cd.rowid";
3274 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3275 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3276 $sql .=
" AND e.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3279 $sql .=
" AND e.fk_soc = ".((int) $socid);
3281 if ($filtrestatut <>
'') {
3282 $sql .=
" AND c.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3284 if (!empty($filterShipmentStatus)) {
3285 $sql .=
" AND e.fk_statut IN (".$this->db->sanitize($filterShipmentStatus).
")";
3288 $result = $this->db->query($sql);
3290 $obj = $this->db->fetch_object($result);
3291 $this->stats_expedition[
'customers'] = $obj->nb_customers;
3292 $this->stats_expedition[
'nb'] = $obj->nb;
3293 $this->stats_expedition[
'rows'] = $obj->nb_rows;
3294 $this->stats_expedition[
'qty'] = $obj->qty ? $obj->qty : 0;
3297 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3299 if (is_array($TFather) && !empty($TFather)) {
3300 foreach ($TFather as &$fatherData) {
3301 $pFather =
new Product($this->db);
3302 $pFather->id = $fatherData[
'id'];
3303 $qtyCoef = $fatherData[
'qty'];
3305 if ($fatherData[
'incdec']) {
3306 $pFather->load_stats_sending($socid, $filtrestatut, $forVirtualStock);
3308 $this->stats_expedition[
'customers'] += $pFather->stats_expedition[
'customers'];
3309 $this->stats_expedition[
'nb'] += $pFather->stats_expedition[
'nb'];
3310 $this->stats_expedition[
'rows'] += $pFather->stats_expedition[
'rows'];
3311 $this->stats_expedition[
'qty'] += $pFather->stats_expedition[
'qty'] * $qtyCoef;
3317 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock,
'filterShipmentStatus' => $filterShipmentStatus);
3318 $reshook = $hookmanager->executeHooks(
'loadStatsSending', $parameters, $this, $action);
3320 $this->stats_expedition = $hookmanager->resArray[
'stats_expedition'];
3325 $this->error = $this->db->error();
3340 public function load_stats_reception($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $dateofvirtualstock =
null)
3343 global $conf, $user, $hookmanager, $action;
3345 $sql =
"SELECT COUNT(DISTINCT cf.fk_soc) as nb_suppliers, COUNT(DISTINCT cf.rowid) as nb,";
3346 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3347 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur_dispatch as fd";
3348 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as cf";
3349 $sql .=
", ".$this->db->prefix().
"societe as s";
3350 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3351 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3353 $sql .=
" WHERE cf.rowid = fd.fk_commande";
3354 $sql .=
" AND cf.fk_soc = s.rowid";
3355 $sql .=
" AND cf.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3356 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3357 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3358 $sql .=
" AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3361 $sql .=
" AND cf.fk_soc = ".((int) $socid);
3363 if ($filtrestatut <>
'') {
3364 $sql .=
" AND cf.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3366 if (!empty($dateofvirtualstock)) {
3367 $sql .=
" AND fd.datec <= '".$this->db->idate($dateofvirtualstock).
"'";
3370 $result = $this->db->query($sql);
3372 $obj = $this->db->fetch_object($result);
3373 $this->stats_reception[
'suppliers'] = $obj->nb_suppliers;
3374 $this->stats_reception[
'nb'] = $obj->nb;
3375 $this->stats_reception[
'rows'] = $obj->nb_rows;
3376 $this->stats_reception[
'qty'] = $obj->qty ? $obj->qty : 0;
3378 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3379 $reshook = $hookmanager->executeHooks(
'loadStatsReception', $parameters, $this, $action);
3381 $this->stats_reception = $hookmanager->resArray[
'stats_reception'];
3386 $this->error = $this->db->error();
3404 global $conf, $user, $hookmanager, $action;
3406 $sql =
"SELECT COUNT(DISTINCT m.fk_soc) as nb_customers, COUNT(DISTINCT m.rowid) as nb,";
3407 $sql .=
" COUNT(mp.rowid) as nb_rows, SUM(mp.qty) as qty, role";
3408 $sql .=
" FROM ".$this->db->prefix().
"mrp_production as mp";
3409 $sql .=
", ".$this->db->prefix().
"mrp_mo as m";
3410 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON s.rowid = m.fk_soc";
3411 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3412 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3414 $sql .=
" WHERE m.rowid = mp.fk_mo";
3415 $sql .=
" AND m.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'mrp').
")";
3416 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
3417 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3418 $sql .=
" AND m.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3421 $sql .=
" AND m.fk_soc = ".((int) $socid);
3423 if ($filtrestatut <>
'') {
3424 $sql .=
" AND m.status IN (".$this->db->sanitize($filtrestatut).
")";
3426 if (!empty($dateofvirtualstock)) {
3427 $sql .=
" AND m.date_valid <= '".$this->db->idate($dateofvirtualstock).
"'";
3429 $sql .=
" GROUP BY role";
3431 $this->stats_mrptoconsume[
'customers'] = 0;
3432 $this->stats_mrptoconsume[
'nb'] = 0;
3433 $this->stats_mrptoconsume[
'rows'] = 0;
3434 $this->stats_mrptoconsume[
'qty'] = 0;
3435 $this->stats_mrptoproduce[
'customers'] = 0;
3436 $this->stats_mrptoproduce[
'nb'] = 0;
3437 $this->stats_mrptoproduce[
'rows'] = 0;
3438 $this->stats_mrptoproduce[
'qty'] = 0;
3440 $result = $this->db->query($sql);
3442 while ($obj = $this->db->fetch_object($result)) {
3443 if ($obj->role ==
'toconsume') {
3444 $this->stats_mrptoconsume[
'customers'] += $obj->nb_customers;
3445 $this->stats_mrptoconsume[
'nb'] += $obj->nb;
3446 $this->stats_mrptoconsume[
'rows'] += $obj->nb_rows;
3447 $this->stats_mrptoconsume[
'qty'] += ($obj->qty ? $obj->qty : 0);
3449 if ($obj->role ==
'consumed') {
3453 $this->stats_mrptoconsume[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3455 if ($obj->role ==
'toproduce') {
3456 $this->stats_mrptoproduce[
'customers'] += $obj->nb_customers;
3457 $this->stats_mrptoproduce[
'nb'] += $obj->nb;
3458 $this->stats_mrptoproduce[
'rows'] += $obj->nb_rows;
3459 $this->stats_mrptoproduce[
'qty'] += ($obj->qty ? $obj->qty : 0);
3461 if ($obj->role ==
'produced') {
3465 $this->stats_mrptoproduce[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3470 if ($this->stats_mrptoconsume[
'qty'] < 0) {
3471 $this->stats_mrptoconsume[
'qty'] = 0;
3473 if ($this->stats_mrptoproduce[
'qty'] < 0) {
3474 $this->stats_mrptoproduce[
'qty'] = 0;
3477 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3478 $reshook = $hookmanager->executeHooks(
'loadStatsInProduction', $parameters, $this, $action);
3480 $this->stats_mrptoproduce = $hookmanager->resArray[
'stats_mrptoproduce'];
3485 $this->error = $this->db->error();
3500 global $conf, $user, $hookmanager, $action;
3502 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3503 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3504 $sql .=
" FROM ".$this->db->prefix().
"contratdet as cd";
3505 $sql .=
", ".$this->db->prefix().
"contrat as c";
3506 $sql .=
", ".$this->db->prefix().
"societe as s";
3507 if (empty($user->rights->societe->client->voir) && !$socid) {
3508 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3510 $sql .=
" WHERE c.rowid = cd.fk_contrat";
3511 $sql .=
" AND c.fk_soc = s.rowid";
3512 $sql .=
" AND c.entity IN (".getEntity(
'contract').
")";
3513 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3514 if (empty($user->rights->societe->client->voir) && !$socid) {
3515 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3519 $sql .=
" AND c.fk_soc = ".((int) $socid);
3522 $result = $this->db->query($sql);
3524 $obj = $this->db->fetch_object($result);
3525 $this->stats_contrat[
'customers'] = $obj->nb_customers;
3526 $this->stats_contrat[
'nb'] = $obj->nb;
3527 $this->stats_contrat[
'rows'] = $obj->nb_rows;
3528 $this->stats_contrat[
'qty'] = $obj->qty ? $obj->qty : 0;
3531 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3533 if (is_array($TFather) && !empty($TFather)) {
3534 foreach ($TFather as &$fatherData) {
3535 $pFather =
new Product($this->db);
3536 $pFather->id = $fatherData[
'id'];
3537 $qtyCoef = $fatherData[
'qty'];
3539 if ($fatherData[
'incdec']) {
3540 $pFather->load_stats_contrat($socid);
3542 $this->stats_contrat[
'customers'] += $pFather->stats_contrat[
'customers'];
3543 $this->stats_contrat[
'nb'] += $pFather->stats_contrat[
'nb'];
3544 $this->stats_contrat[
'rows'] += $pFather->stats_contrat[
'rows'];
3545 $this->stats_contrat[
'qty'] += $pFather->stats_contrat[
'qty'] * $qtyCoef;
3551 $parameters = array(
'socid' => $socid);
3552 $reshook = $hookmanager->executeHooks(
'loadStatsContract', $parameters, $this, $action);
3554 $this->stats_contrat = $hookmanager->resArray[
'stats_contrat'];
3559 $this->error = $this->db->error().
' sql='.$sql;
3574 global $db, $conf, $user, $hookmanager, $action;
3576 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3577 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(".$this->db->ifsql(
'f.type != 2',
'fd.qty',
'fd.qty * -1').
") as qty";
3578 $sql .=
" FROM ".$this->db->prefix().
"facturedet as fd";
3579 $sql .=
", ".$this->db->prefix().
"facture as f";
3580 $sql .=
", ".$this->db->prefix().
"societe as s";
3581 if (empty($user->rights->societe->client->voir) && !$socid) {
3582 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3584 $sql .=
" WHERE f.rowid = fd.fk_facture";
3585 $sql .=
" AND f.fk_soc = s.rowid";
3586 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3587 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3588 if (empty($user->rights->societe->client->voir) && !$socid) {
3589 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3593 $sql .=
" AND f.fk_soc = ".((int) $socid);
3596 $result = $this->db->query($sql);
3598 $obj = $this->db->fetch_object($result);
3599 $this->stats_facture[
'customers'] = $obj->nb_customers;
3600 $this->stats_facture[
'nb'] = $obj->nb;
3601 $this->stats_facture[
'rows'] = $obj->nb_rows;
3602 $this->stats_facture[
'qty'] = $obj->qty ? $obj->qty : 0;
3605 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3607 if (is_array($TFather) && !empty($TFather)) {
3608 foreach ($TFather as &$fatherData) {
3609 $pFather =
new Product($this->db);
3610 $pFather->id = $fatherData[
'id'];
3611 $qtyCoef = $fatherData[
'qty'];
3613 if ($fatherData[
'incdec']) {
3614 $pFather->load_stats_facture($socid);
3616 $this->stats_facture[
'customers'] += $pFather->stats_facture[
'customers'];
3617 $this->stats_facture[
'nb'] += $pFather->stats_facture[
'nb'];
3618 $this->stats_facture[
'rows'] += $pFather->stats_facture[
'rows'];
3619 $this->stats_facture[
'qty'] += $pFather->stats_facture[
'qty'] * $qtyCoef;
3625 $parameters = array(
'socid' => $socid);
3626 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoice', $parameters, $this, $action);
3628 $this->stats_facture = $hookmanager->resArray[
'stats_facture'];
3633 $this->error = $this->db->error();
3649 global $db, $conf, $user, $hookmanager;
3651 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3652 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3653 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet_rec as fd";
3654 $sql .=
", ".MAIN_DB_PREFIX.
"facture_rec as f";
3655 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
3656 if (empty($user->rights->societe->client->voir) && !$socid) {
3657 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
3659 $sql .=
" WHERE f.rowid = fd.fk_facture";
3660 $sql .=
" AND f.fk_soc = s.rowid";
3661 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3662 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3663 if (empty($user->rights->societe->client->voir) && !$socid) {
3664 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3668 $sql .=
" AND f.fk_soc = ".((int) $socid);
3671 $result = $this->db->query($sql);
3673 $obj = $this->db->fetch_object($result);
3674 $this->stats_facturerec[
'customers'] = $obj->nb_customers;
3675 $this->stats_facturerec[
'nb'] = $obj->nb;
3676 $this->stats_facturerec[
'rows'] = $obj->nb_rows;
3677 $this->stats_facturerec[
'qty'] = $obj->qty ? $obj->qty : 0;
3680 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3682 if (is_array($TFather) && !empty($TFather)) {
3683 foreach ($TFather as &$fatherData) {
3684 $pFather =
new Product($this->db);
3685 $pFather->id = $fatherData[
'id'];
3686 $qtyCoef = $fatherData[
'qty'];
3688 if ($fatherData[
'incdec']) {
3689 $pFather->load_stats_facture($socid);
3691 $this->stats_facturerec[
'customers'] += $pFather->stats_facturerec[
'customers'];
3692 $this->stats_facturerec[
'nb'] += $pFather->stats_facturerec[
'nb'];
3693 $this->stats_facturerec[
'rows'] += $pFather->stats_facturerec[
'rows'];
3694 $this->stats_facturerec[
'qty'] += $pFather->stats_facturerec[
'qty'] * $qtyCoef;
3700 $parameters = array(
'socid' => $socid);
3701 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoiceRec', $parameters, $this, $action);
3703 $this->stats_facturerec = $hookmanager->resArray[
'stats_facturerec'];
3708 $this->error = $this->db->error();
3723 global $conf, $user, $hookmanager, $action;
3725 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,";
3726 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3727 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as fd";
3728 $sql .=
", ".$this->db->prefix().
"facture_fourn as f";
3729 $sql .=
", ".$this->db->prefix().
"societe as s";
3730 if (empty($user->rights->societe->client->voir) && !$socid) {
3731 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3733 $sql .=
" WHERE f.rowid = fd.fk_facture_fourn";
3734 $sql .=
" AND f.fk_soc = s.rowid";
3735 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3736 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3737 if (empty($user->rights->societe->client->voir) && !$socid) {
3738 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3742 $sql .=
" AND f.fk_soc = ".((int) $socid);
3745 $result = $this->db->query($sql);
3747 $obj = $this->db->fetch_object($result);
3748 $this->stats_facture_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3749 $this->stats_facture_fournisseur[
'nb'] = $obj->nb;
3750 $this->stats_facture_fournisseur[
'rows'] = $obj->nb_rows;
3751 $this->stats_facture_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3753 $parameters = array(
'socid' => $socid);
3754 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierInvoice', $parameters, $this, $action);
3756 $this->stats_facture_fournisseur = $hookmanager->resArray[
'stats_facture_fournisseur'];
3761 $this->error = $this->db->error();
3780 $resql = $this->db->query($sql);
3782 $num = $this->db->num_rows($resql);
3785 $arr = $this->db->fetch_array($resql);
3786 $keyfortab = (string) $arr[1];
3788 $keyfortab = substr($keyfortab, -2);
3791 if ($mode ==
'byunit') {
3792 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[0];
3793 } elseif ($mode ==
'bynumber') {
3794 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3795 } elseif ($mode ==
'byamount') {
3796 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3804 $this->error = $this->db->error().
' sql='.$sql;
3809 $year = strftime(
'%Y', time());
3810 $month = strftime(
'%m', time());
3811 } elseif ($year == -1) {
3820 for ($j = 0; $j < 12; $j++) {
3822 $idx = ucfirst(
dol_trunc(
dol_print_date(
dol_mktime(12, 0, 0, $month, 1, 1970),
"%b"), 1,
'right',
'UTF-8', 1));
3825 $result[$j] = array($idx, isset($tab[$year.$month]) ? $tab[$year.$month] : 0);
3828 $month =
"0".($month - 1);
3830 $month = substr($month, 1);
3838 return array_reverse($result);
3853 public function get_nb_vente($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3859 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3860 if ($mode ==
'bynumber') {
3861 $sql .=
", count(DISTINCT f.rowid)";
3863 $sql .=
", sum(d.total_ht) as total_ht";
3864 $sql .=
" FROM ".$this->db->prefix().
"facturedet as d, ".$this->db->prefix().
"facture as f, ".$this->db->prefix().
"societe as s";
3865 if ($filteronproducttype >= 0) {
3866 $sql .=
", ".$this->db->prefix().
"product as p";
3868 if (empty($user->rights->societe->client->voir) && !$socid) {
3869 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3871 $sql .=
" WHERE f.rowid = d.fk_facture";
3872 if ($this->
id > 0) {
3873 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3875 $sql .=
" AND d.fk_product > 0";
3877 if ($filteronproducttype >= 0) {
3878 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3880 $sql .=
" AND f.fk_soc = s.rowid";
3881 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3882 if (empty($user->rights->societe->client->voir) && !$socid) {
3883 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3886 $sql .=
" AND f.fk_soc = $socid";
3888 $sql .= $morefilter;
3889 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3890 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3892 return $this->
_get_stats($sql, $mode, $year);
3907 public function get_nb_achat($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3913 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3914 if ($mode ==
'bynumber') {
3915 $sql .=
", count(DISTINCT f.rowid)";
3917 $sql .=
", sum(d.total_ht) as total_ht";
3918 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as d, ".$this->db->prefix().
"facture_fourn as f, ".$this->db->prefix().
"societe as s";
3919 if ($filteronproducttype >= 0) {
3920 $sql .=
", ".$this->db->prefix().
"product as p";
3922 if (empty($user->rights->societe->client->voir) && !$socid) {
3923 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3925 $sql .=
" WHERE f.rowid = d.fk_facture_fourn";
3926 if ($this->
id > 0) {
3927 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3929 $sql .=
" AND d.fk_product > 0";
3931 if ($filteronproducttype >= 0) {
3932 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3934 $sql .=
" AND f.fk_soc = s.rowid";
3935 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3936 if (empty($user->rights->societe->client->voir) && !$socid) {
3937 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3940 $sql .=
" AND f.fk_soc = $socid";
3942 $sql .= $morefilter;
3943 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3944 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3946 return $this->
_get_stats($sql, $mode, $year);
3960 public function get_nb_propal($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3963 global $conf, $user;
3965 $sql =
"SELECT sum(d.qty) as qty, date_format(p.datep, '%Y%m')";
3966 if ($mode ==
'bynumber') {
3967 $sql .=
", count(DISTINCT p.rowid)";
3969 $sql .=
", sum(d.total_ht) as total_ht";
3970 $sql .=
" FROM ".$this->db->prefix().
"propaldet as d, ".$this->db->prefix().
"propal as p, ".$this->db->prefix().
"societe as s";
3971 if ($filteronproducttype >= 0) {
3972 $sql .=
", ".$this->db->prefix().
"product as prod";
3974 if (empty($user->rights->societe->client->voir) && !$socid) {
3975 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3977 $sql .=
" WHERE p.rowid = d.fk_propal";
3978 if ($this->
id > 0) {
3979 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3981 $sql .=
" AND d.fk_product > 0";
3983 if ($filteronproducttype >= 0) {
3984 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
3986 $sql .=
" AND p.fk_soc = s.rowid";
3987 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
3988 if (empty($user->rights->societe->client->voir) && !$socid) {
3989 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3992 $sql .=
" AND p.fk_soc = ".((int) $socid);
3994 $sql .= $morefilter;
3995 $sql .=
" GROUP BY date_format(p.datep,'%Y%m')";
3996 $sql .=
" ORDER BY date_format(p.datep,'%Y%m') DESC";
3998 return $this->
_get_stats($sql, $mode, $year);
4018 $sql =
"SELECT sum(d.qty) as qty, date_format(p.date_valid, '%Y%m')";
4019 if ($mode ==
'bynumber') {
4020 $sql .=
", count(DISTINCT p.rowid)";
4022 $sql .=
", sum(d.total_ht) as total_ht";
4023 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as d, ".$this->db->prefix().
"supplier_proposal as p, ".$this->db->prefix().
"societe as s";
4024 if ($filteronproducttype >= 0) {
4025 $sql .=
", ".$this->db->prefix().
"product as prod";
4027 if (empty($user->rights->societe->client->voir) && !$socid) {
4028 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4030 $sql .=
" WHERE p.rowid = d.fk_supplier_proposal";
4031 if ($this->
id > 0) {
4032 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4034 $sql .=
" AND d.fk_product > 0";
4036 if ($filteronproducttype >= 0) {
4037 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
4039 $sql .=
" AND p.fk_soc = s.rowid";
4040 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
4041 if (empty($user->rights->societe->client->voir) && !$socid) {
4042 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4045 $sql .=
" AND p.fk_soc = ".((int) $socid);
4047 $sql .= $morefilter;
4048 $sql .=
" GROUP BY date_format(p.date_valid,'%Y%m')";
4049 $sql .=
" ORDER BY date_format(p.date_valid,'%Y%m') DESC";
4051 return $this->
_get_stats($sql, $mode, $year);
4065 public function get_nb_order($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4068 global $conf, $user;
4070 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4071 if ($mode ==
'bynumber') {
4072 $sql .=
", count(DISTINCT c.rowid)";
4074 $sql .=
", sum(d.total_ht) as total_ht";
4075 $sql .=
" FROM ".$this->db->prefix().
"commandedet as d, ".$this->db->prefix().
"commande as c, ".$this->db->prefix().
"societe as s";
4076 if ($filteronproducttype >= 0) {
4077 $sql .=
", ".$this->db->prefix().
"product as p";
4079 if (empty($user->rights->societe->client->voir) && !$socid) {
4080 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4082 $sql .=
" WHERE c.rowid = d.fk_commande";
4083 if ($this->
id > 0) {
4084 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4086 $sql .=
" AND d.fk_product > 0";
4088 if ($filteronproducttype >= 0) {
4089 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4091 $sql .=
" AND c.fk_soc = s.rowid";
4092 $sql .=
" AND c.entity IN (".getEntity(
'commande').
")";
4093 if (empty($user->rights->societe->client->voir) && !$socid) {
4094 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4097 $sql .=
" AND c.fk_soc = ".((int) $socid);
4099 $sql .= $morefilter;
4100 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4101 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4103 return $this->
_get_stats($sql, $mode, $year);
4120 global $conf, $user;
4122 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4123 if ($mode ==
'bynumber') {
4124 $sql .=
", count(DISTINCT c.rowid)";
4126 $sql .=
", sum(d.total_ht) as total_ht";
4127 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as d, ".$this->db->prefix().
"commande_fournisseur as c, ".$this->db->prefix().
"societe as s";
4128 if ($filteronproducttype >= 0) {
4129 $sql .=
", ".$this->db->prefix().
"product as p";
4131 if (empty($user->rights->societe->client->voir) && !$socid) {
4132 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4134 $sql .=
" WHERE c.rowid = d.fk_commande";
4135 if ($this->
id > 0) {
4136 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4138 $sql .=
" AND d.fk_product > 0";
4140 if ($filteronproducttype >= 0) {
4141 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4143 $sql .=
" AND c.fk_soc = s.rowid";
4144 $sql .=
" AND c.entity IN (".getEntity(
'supplier_order').
")";
4145 if (empty($user->rights->societe->client->voir) && !$socid) {
4146 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4149 $sql .=
" AND c.fk_soc = ".((int) $socid);
4151 $sql .= $morefilter;
4152 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4153 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4155 return $this->
_get_stats($sql, $mode, $year);
4169 public function get_nb_contract($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4172 global $conf, $user;
4174 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_contrat, '%Y%m')";
4175 if ($mode ==
'bynumber') {
4176 $sql .=
", count(DISTINCT c.rowid)";
4178 $sql .=
", sum(d.total_ht) as total_ht";
4179 $sql .=
" FROM ".$this->db->prefix().
"contratdet as d, ".$this->db->prefix().
"contrat as c, ".$this->db->prefix().
"societe as s";
4180 if ($filteronproducttype >= 0) {
4181 $sql .=
", ".$this->db->prefix().
"product as p";
4183 if (empty($user->rights->societe->client->voir) && !$socid) {
4184 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4187 $sql .=
" WHERE c.entity IN (".getEntity(
'contract').
")";
4188 $sql .=
" AND c.rowid = d.fk_contrat";
4190 if ($this->
id > 0) {
4191 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4193 $sql .=
" AND d.fk_product > 0";
4195 if ($filteronproducttype >= 0) {
4196 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4198 $sql .=
" AND c.fk_soc = s.rowid";
4200 if (empty($user->rights->societe->client->voir) && !$socid) {
4201 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4204 $sql .=
" AND c.fk_soc = ".((int) $socid);
4206 $sql .= $morefilter;
4207 $sql .=
" GROUP BY date_format(c.date_contrat,'%Y%m')";
4208 $sql .=
" ORDER BY date_format(c.date_contrat,'%Y%m') DESC";
4210 return $this->
_get_stats($sql, $mode, $year);
4224 public function get_nb_mos($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4227 global $conf, $user;
4229 $sql =
"SELECT sum(d.qty), date_format(d.date_valid, '%Y%m')";
4230 if ($mode ==
'bynumber') {
4231 $sql .=
", count(DISTINCT d.rowid)";
4233 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as d LEFT JOIN ".$this->db->prefix().
"societe as s ON d.fk_soc = s.rowid";
4234 if ($filteronproducttype >= 0) {
4235 $sql .=
", ".$this->db->prefix().
"product as p";
4237 if (empty($user->rights->societe->client->voir) && !$socid) {
4238 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4241 $sql .=
" WHERE d.entity IN (".getEntity(
'mo').
")";
4242 $sql .=
" AND d.status > 0";
4244 if ($this->
id > 0) {
4245 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4247 $sql .=
" AND d.fk_product > 0";
4249 if ($filteronproducttype >= 0) {
4250 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4253 if (empty($user->rights->societe->client->voir) && !$socid) {
4254 $sql .=
" AND d.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4257 $sql .=
" AND d.fk_soc = ".((int) $socid);
4259 $sql .= $morefilter;
4260 $sql .=
" GROUP BY date_format(d.date_valid,'%Y%m')";
4261 $sql .=
" ORDER BY date_format(d.date_valid,'%Y%m') DESC";
4263 return $this->
_get_stats($sql, $mode, $year);
4280 if (!is_numeric($id_pere)) {
4283 if (!is_numeric($id_fils)) {
4286 if (!is_numeric($incdec)) {
4296 $sql =
"SELECT fk_product_pere from ".$this->db->prefix().
"product_association";
4297 $sql .=
" WHERE fk_product_pere = ".((int) $id_fils).
" AND fk_product_fils = ".((int) $id_pere);
4298 if (!$this->db->query($sql)) {
4303 $sql =
"SELECT MAX(rang) as max_rank FROM ".$this->db->prefix().
"product_association";
4304 $sql .=
" WHERE fk_product_pere = ".((int) $id_pere);
4305 $resql = $this->db->query($sql);
4307 $obj = $this->db->fetch_object($resql);
4308 $rank = $obj->max_rank + 1;
4310 $sql =
"INSERT INTO ".$this->db->prefix().
"product_association(fk_product_pere,fk_product_fils,qty,incdec,rang)";
4311 $sql .=
" VALUES (".((int) $id_pere).
", ".((int) $id_fils).
", ".
price2num($qty,
'MS').
", ".
price2num($incdec,
'MS').
", ".((int) $rank).
")";
4312 if (! $this->db->query($sql)) {
4339 if (!is_numeric($id_pere)) {
4342 if (!is_numeric($id_fils)) {
4345 if (!is_numeric($incdec)) {
4348 if (!is_numeric($qty)) {
4352 $sql =
'UPDATE '.$this->db->prefix().
'product_association SET ';
4353 $sql .=
'qty = '.price2num($qty,
'MS');
4354 $sql .=
',incdec = '.price2num($incdec,
'MS');
4355 $sql .=
' WHERE fk_product_pere = '.((int) $id_pere).
' AND fk_product_fils = '.((int) $id_fils);
4357 if (!$this->db->query($sql)) {
4376 if (!is_numeric($fk_parent)) {
4379 if (!is_numeric($fk_child)) {
4383 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
4384 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4385 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4387 dol_syslog(get_class($this).
'::del_sousproduit', LOG_DEBUG);
4388 if (!$this->db->query($sql)) {
4394 $sqlrank =
"SELECT rowid, rang FROM ".$this->db->prefix().
"product_association";
4395 $sqlrank.=
" WHERE fk_product_pere = ".((int) $fk_parent);
4396 $sqlrank.=
" ORDER BY rang";
4397 $resqlrank = $this->db->query($sqlrank);
4400 while ($objrank = $this->db->fetch_object($resqlrank)) {
4402 $sql =
"UPDATE ".$this->db->prefix().
"product_association";
4403 $sql.=
" SET rang = ".((int) $cpt);
4404 $sql.=
" WHERE rowid = ".((int) $objrank->rowid);
4405 if (! $this->db->query($sql)) {
4425 $sql =
"SELECT fk_product_pere, qty, incdec";
4426 $sql .=
" FROM ".$this->db->prefix().
"product_association";
4427 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4428 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4430 $result = $this->db->query($sql);
4432 $num = $this->db->num_rows($result);
4435 $obj = $this->db->fetch_object($result);
4437 $this->is_sousproduit_qty = $obj->qty;
4438 $this->is_sousproduit_incdec = $obj->incdec;
4469 dol_syslog(get_class($this).
"::add_fournisseur id_fourn = ".$id_fourn.
" ref_fourn=".
$ref_fourn.
" quantity=".$quantity, LOG_DEBUG);
4475 $sql =
"SELECT rowid, fk_product";
4476 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4477 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4478 $sql .=
" AND ref_fourn = '".$this->db->escape(
$ref_fourn).
"'";
4479 $sql .=
" AND fk_product <> ".((int) $this->
id);
4480 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4482 $resql = $this->db->query($sql);
4484 $obj = $this->db->fetch_object($resql);
4487 $this->product_id_already_linked = $obj->fk_product;
4490 $this->db->free($resql);
4494 $sql =
"SELECT rowid";
4495 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4496 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4498 $sql .=
" AND ref_fourn = '".$this->db->escape(
$ref_fourn).
"'";
4500 $sql .=
" AND (ref_fourn = '' OR ref_fourn IS NULL)";
4502 $sql .=
" AND quantity = ".((float) $quantity);
4503 $sql .=
" AND fk_product = ".((int) $this->
id);
4504 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4506 $resql = $this->db->query($sql);
4508 $obj = $this->db->fetch_object($resql);
4512 $sql =
"INSERT INTO ".$this->db->prefix().
"product_fournisseur_price(";
4515 $sql .=
", fk_product";
4517 $sql .=
", ref_fourn";
4518 $sql .=
", quantity";
4519 $sql .=
", fk_user";
4521 $sql .=
") VALUES (";
4522 $sql .=
"'".$this->db->idate($now).
"'";
4523 $sql .=
", ".$conf->entity;
4524 $sql .=
", ".$this->id;
4525 $sql .=
", ".$id_fourn;
4526 $sql .=
", '".$this->db->escape(
$ref_fourn).
"'";
4527 $sql .=
", ".$quantity;
4528 $sql .=
", ".$user->id;
4532 if ($this->db->query($sql)) {
4533 $this->product_fourn_price_id = $this->db->last_insert_id($this->db->prefix().
"product_fournisseur_price");
4536 $this->error = $this->db->lasterror();
4541 $this->product_fourn_price_id = $obj->rowid;
4545 $this->error = $this->db->lasterror();
4564 $sql =
"SELECT DISTINCT p.fk_soc";
4565 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as p";
4566 $sql .=
" WHERE p.fk_product = ".((int) $this->
id);
4567 $sql .=
" AND p.entity = ".((int) $conf->entity);
4569 $result = $this->db->query($sql);
4571 $num = $this->db->num_rows($result);
4574 $obj = $this->db->fetch_object($result);
4575 $list[$i] = $obj->fk_soc;
4593 global $conf, $user;
4600 $sql =
"INSERT INTO ".$this->db->prefix().
"product_price (";
4602 $sql .=
", fk_product";
4603 $sql .=
", date_price";
4604 $sql .=
", price_level";
4606 $sql .=
", price_ttc";
4607 $sql .=
", price_min";
4608 $sql .=
", price_min_ttc";
4609 $sql .=
", price_base_type";
4610 $sql .=
", default_vat_code";
4612 $sql .=
", recuperableonly";
4613 $sql .=
", localtax1_tx";
4614 $sql .=
", localtax1_type";
4615 $sql .=
", localtax2_tx";
4616 $sql .=
", localtax2_type";
4617 $sql .=
", fk_user_author";
4619 $sql .=
", price_by_qty";
4620 $sql .=
", fk_price_expression";
4621 $sql .=
", fk_multicurrency";
4622 $sql .=
", multicurrency_code";
4623 $sql .=
", multicurrency_tx";
4624 $sql .=
", multicurrency_price";
4625 $sql .=
", multicurrency_price_ttc";
4630 $sql .=
", '".$this->db->idate($now).
"'";
4631 $sql .=
", price_level";
4633 $sql .=
", price_ttc";
4634 $sql .=
", price_min";
4635 $sql .=
", price_min_ttc";
4636 $sql .=
", price_base_type";
4637 $sql .=
", default_vat_code";
4639 $sql .=
", recuperableonly";
4640 $sql .=
", localtax1_tx";
4641 $sql .=
", localtax1_type";
4642 $sql .=
", localtax2_tx";
4643 $sql .=
", localtax2_type";
4644 $sql .=
", ".$user->id;
4646 $sql .=
", price_by_qty";
4647 $sql .=
", fk_price_expression";
4648 $sql .=
", fk_multicurrency";
4649 $sql .=
", multicurrency_code";
4650 $sql .=
", multicurrency_tx";
4651 $sql .=
", multicurrency_price";
4652 $sql .=
", multicurrency_price_ttc";
4653 $sql .=
" FROM ".$this->db->prefix().
"product_price";
4654 $sql .=
" WHERE fk_product = ".((int) $fromId);
4655 $sql .=
" ORDER BY date_price DESC";
4656 if ($conf->global->PRODUIT_MULTIPRICES_LIMIT > 0) {
4657 $sql .=
" LIMIT ".$conf->global->PRODUIT_MULTIPRICES_LIMIT;
4661 $resql = $this->db->query($sql);
4663 $this->db->rollback();
4667 $this->db->commit();
4684 $sql =
'INSERT INTO '.$this->db->prefix().
'product_association (fk_product_pere, fk_product_fils, qty)';
4685 $sql .=
" SELECT ".$toId.
", fk_product_fils, qty FROM ".$this->db->prefix().
"product_association";
4686 $sql .=
" WHERE fk_product_pere = ".((int) $fromId);
4688 dol_syslog(get_class($this).
'::clone_association', LOG_DEBUG);
4689 if (!$this->db->query($sql)) {
4690 $this->db->rollback();
4694 $this->db->commit();
4727 $sql =
"INSERT ".$this->db->prefix().
"product_fournisseur_price (";
4728 $sql .=
" datec, fk_product, fk_soc, price, quantity, fk_user, tva_tx)";
4729 $sql .=
" SELECT '".$this->db->idate($now).
"', ".((int) $toId).
", fk_soc, price, quantity, fk_user, tva_tx";
4730 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4731 $sql .=
" WHERE fk_product = ".((int) $fromId);
4733 dol_syslog(get_class($this).
'::clone_fournisseurs', LOG_DEBUG);
4734 $resql = $this->db->query($sql);
4736 $this->db->rollback();
4739 $this->db->commit();
4757 public function fetch_prod_arbo($prod, $compl_path =
'', $multiply = 1, $level = 1, $id_parent = 0, $ignore_stock_load = 0)
4760 global $conf, $langs;
4764 foreach ($prod as $id_product => $desc_pere) {
4765 if (is_array($desc_pere)) {
4766 $id = (!empty($desc_pere[0]) ? $desc_pere[0] :
'');
4767 $nb = (!empty($desc_pere[1]) ? $desc_pere[1] :
'');
4768 $type = (!empty($desc_pere[2]) ? $desc_pere[2] :
'');
4769 $label = (!empty($desc_pere[3]) ? $desc_pere[3] :
'');
4770 $incdec = (!empty($desc_pere[4]) ? $desc_pere[4] : 0);
4772 if ($multiply < 1) {
4777 if (is_null($tmpproduct)) {
4778 $tmpproduct =
new Product($this->db);
4780 $tmpproduct->fetch($id);
4782 if (empty($ignore_stock_load) && ($tmpproduct->isProduct() || !empty($conf->global->STOCK_SUPPORTS_SERVICES))) {
4783 $tmpproduct->load_stock(
'nobatch,novirtual');
4786 $this->res[] = array(
4788 'id_parent'=>$id_parent,
4789 'ref'=>$tmpproduct->ref,
4791 'nb_total'=>$nb * $multiply,
4792 'stock'=>$tmpproduct->stock_reel,
4793 'stock_alert'=>$tmpproduct->seuil_stock_alerte,
4795 'fullpath'=>$compl_path.$label,
4797 'desiredstock'=>$tmpproduct->desiredstock,
4800 'entity'=>$tmpproduct->entity
4804 if (isset($desc_pere[
'childs']) && is_array($desc_pere[
'childs'])) {
4806 $this->
fetch_prod_arbo($desc_pere[
'childs'], $compl_path.$desc_pere[3].
" -> ", $desc_pere[1] * $multiply, $level + 1, $id, $ignore_stock_load);
4824 $this->res = array();
4825 if (isset($this->sousprods) && is_array($this->sousprods)) {
4826 foreach ($this->sousprods as $prod_name => $desc_product) {
4827 if (is_array($desc_product)) {
4828 $this->
fetch_prod_arbo($desc_product,
"", $multiply, 1, $this->
id, $ignore_stock_load);
4847 $sql =
"SELECT COUNT(pa.rowid) as nb";
4848 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa";
4850 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id).
" OR pa.fk_product_pere = ".((int) $this->
id);
4851 } elseif ($mode == -1) {
4852 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id);
4853 } elseif ($mode == 1) {
4854 $sql .=
" WHERE pa.fk_product_pere = ".((int) $this->
id);
4857 $resql = $this->db->query($sql);
4859 $obj = $this->db->fetch_object($resql);
4878 $sql =
"SELECT count(rowid) as nb FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_parent = ".((int) $this->
id);
4879 $sql .=
" AND entity IN (".getEntity(
'product').
")";
4881 $resql = $this->db->query($sql);
4883 $obj = $this->db->fetch_object($resql);
4901 if (isModEnabled(
'variants')) {
4902 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_child = ".((int) $this->
id).
" AND entity IN (".
getEntity(
'product').
")";
4904 $query = $this->db->query($sql);
4907 if (!$this->db->num_rows($query)) {
4928 $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";
4929 $sql .=
", p.tosell as status, p.tobuy as status_buy";
4930 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa,";
4931 $sql .=
" ".$this->db->prefix().
"product as p";
4932 $sql .=
" WHERE p.rowid = pa.fk_product_pere";
4933 $sql .=
" AND pa.fk_product_fils = ".((int) $this->
id);
4935 $res = $this->db->query($sql);
4938 while ($record = $this->db->fetch_array(
$res)) {
4940 $prods[$record[
'id']][
'id'] = $record[
'rowid'];
4941 $prods[$record[
'id']][
'ref'] = $record[
'ref'];
4942 $prods[$record[
'id']][
'label'] = $record[
'label'];
4943 $prods[$record[
'id']][
'qty'] = $record[
'qty'];
4944 $prods[$record[
'id']][
'incdec'] = $record[
'incdec'];
4945 $prods[$record[
'id']][
'fk_product_type'] = $record[
'fk_product_type'];
4946 $prods[$record[
'id']][
'entity'] = $record[
'entity'];
4947 $prods[$record[
'id']][
'status'] = $record[
'status'];
4948 $prods[$record[
'id']][
'status_buy'] = $record[
'status_buy'];
4967 public function getChildsArbo($id, $firstlevelonly = 0, $level = 1, $parents = array())
4969 global $alreadyfound;
4975 $sql =
"SELECT p.rowid, p.ref, p.label as label, p.fk_product_type,";
4976 $sql .=
" pa.qty as qty, pa.fk_product_fils as id, pa.incdec,";
4977 $sql .=
" pa.rowid as fk_association, pa.rang";
4978 $sql .=
" FROM ".$this->db->prefix().
"product as p,";
4979 $sql .=
" ".$this->db->prefix().
"product_association as pa";
4980 $sql .=
" WHERE p.rowid = pa.fk_product_fils";
4981 $sql .=
" AND pa.fk_product_pere = ".((int) $id);
4982 $sql .=
" AND pa.fk_product_fils <> ".((int) $id);
4983 $sql.=
" ORDER BY pa.rang";
4985 dol_syslog(get_class($this).
'::getChildsArbo id='.$id.
' level='.$level.
' parents='.(is_array($parents)?implode(
',', $parents):$parents), LOG_DEBUG);
4988 $alreadyfound = array($id=>1);
4995 $res = $this->db->query($sql);
4998 while ($rec = $this->db->fetch_array(
$res)) {
4999 if (!empty($alreadyfound[$rec[
'rowid']])) {
5000 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);
5001 if (in_array($rec[
'id'], $parents)) {
5005 $alreadyfound[$rec[
'rowid']] = 1;
5006 $prods[$rec[
'rowid']] = array(
5009 2=>$rec[
'fk_product_type'],
5010 3=>$this->db->escape($rec[
'label']),
5013 6=>$rec[
'fk_association'],
5018 if (empty($firstlevelonly)) {
5019 $listofchilds = $this->
getChildsArbo($rec[
'rowid'], 0, $level + 1, array_push($parents, $rec[
'rowid']));
5020 foreach ($listofchilds as $keyChild => $valueChild) {
5021 $prods[$rec[
'rowid']][
'childs'][$keyChild] = $valueChild;
5045 foreach ($this->
getChildsArbo($this->
id) as $keyChild => $valueChild) {
5046 $parent[$this->label][$keyChild] = $valueChild;
5048 foreach ($parent as $key => $value) {
5049 $this->sousprods[$key] = $value;
5061 global $conf, $langs;
5063 $langs->loadLangs(array(
'products',
'other'));
5066 $nofetch = !empty($params[
'nofetch']);
5068 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5069 return [
'optimize' => $langs->trans(
"ShowProduct")];
5072 if (!empty($this->entity)) {
5073 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80, 0, 0, 0, 0, 1);
5074 if ($this->nbphoto > 0) {
5075 $datas[
'photo'] =
'<div class="photointooltip floatright">'.
"\n" . $tmpphoto .
'</div>';
5080 $datas[
'picto'] =
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
5082 $datas[
'picto']=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
5084 if (isset($this->status) && isset($this->status_buy)) {
5085 $datas[
'status']=
' '.$this->getLibStatut(5, 0) .
' '.$this->getLibStatut(5, 1);
5088 if (!empty($this->
ref)) {
5089 $datas[
'ref']=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.$this->ref;
5091 if (!empty($this->label)) {
5092 $datas[
'label']=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
5095 if (isModEnabled(
'productbatch')) {
5096 $langs->load(
"productbatch");
5097 $datas[
'batchstatus']=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
5100 if (isModEnabled(
'barcode')) {
5101 $datas[
'barcode']=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
5105 if ($this->weight) {
5106 $datas[
'weight']=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
5109 if ($this->length) {
5110 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
5113 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
5115 if ($this->height) {
5116 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
5119 $datas[
'size']=
"<br>".$labelsize;
5122 $labelsurfacevolume =
"";
5123 if ($this->surface) {
5124 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
5126 if ($this->volume) {
5127 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
5129 if ($labelsurfacevolume) {
5130 $datas[
'surface']=
"<br>" . $labelsurfacevolume;
5133 if (!empty($this->pmp) && $this->pmp) {
5134 $datas[
'pmp'] =
"<br><b>".$langs->trans(
"PMPValue").
'</b>: '.
price($this->pmp, 0,
'', 1, -1, -1, $conf->currency);
5137 if (isModEnabled(
'accounting')) {
5138 if ($this->status && isset($this->accountancy_code_sell)) {
5139 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5140 $selllabel =
'<br>';
5141 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
5142 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
5143 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
5144 $datas[
'accountancysell'] = $selllabel;
5146 if ($this->status_buy && isset($this->accountancy_code_buy)) {
5147 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5149 if (empty($this->status)) {
5150 $buylabel .=
'<br>';
5152 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
5153 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
5154 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
5155 $datas[
'accountancybuy'] = $buylabel;
5159 if (isModEnabled(
'categorie') && !$nofetch) {
5160 require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
5161 $form =
new Form($this->db);
5162 $datas[
'categories'] =
'<br>' . $form->showCategories($this->
id, Categorie::TYPE_PRODUCT, 1);
5181 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
5183 global $conf, $langs, $hookmanager;
5184 include_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
5188 $newref = $this->ref;
5190 $newref =
dol_trunc($newref, $maxlength,
'middle');
5194 'objecttype' => (isset($this->
type) ? ($this->
type == 1 ?
'service' :
'product') : $this->element),
5195 'option' => $option,
5198 $classfortooltip =
'classfortooltip';
5201 $classfortooltip =
'classforajaxtooltip';
5202 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
5209 if (empty($notooltip)) {
5210 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5211 $label = $langs->trans(
"ShowProduct");
5212 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1, 1).
'"';
5214 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1, 1).
'"' :
' title="tocomplete"');
5215 $linkclose .= $dataparams.
' class="nowraponall '.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
5217 $linkclose =
' class="nowraponall'.($morecss ?
' '.$morecss :
'').
'"';
5220 if ($option ==
'supplier' || $option ==
'category') {
5221 $url = DOL_URL_ROOT.
'/product/fournisseurs.php?id='.$this->id;
5222 } elseif ($option ==
'stock') {
5223 $url = DOL_URL_ROOT.
'/product/stock/product.php?id='.$this->id;
5224 } elseif ($option ==
'composition') {
5225 $url = DOL_URL_ROOT.
'/product/composition/card.php?id='.$this->id;
5227 $url = DOL_URL_ROOT.
'/product/card.php?id='.$this->id;
5230 if ($option !==
'nolink') {
5232 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
5233 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
5234 $add_save_lastsearch_values = 1;
5236 if ($add_save_lastsearch_values) {
5237 $url .=
'&save_lastsearch_values=1';
5241 $linkstart =
'<a href="'.$url.
'"';
5242 $linkstart .= $linkclose.
'>';
5245 $result .= $linkstart;
5248 $result .= (
img_object(($notooltip ?
'' : $label),
'product',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5251 $result .= (
img_object(($notooltip ?
'' : $label),
'service',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5255 $result .= $linkend;
5256 if ($withpicto != 2) {
5257 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
5261 $hookmanager->initHooks(array(
'productdao'));
5262 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'label' => &$label);
5263 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
5265 $result = $hookmanager->resPrint;
5267 $result .= $hookmanager->resPrint;
5284 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
5286 global $conf, $user, $langs;
5288 $langs->load(
"products");
5289 $outputlangs->load(
"products");
5296 $modelpath =
"core/modules/product/doc/";
5298 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
5312 return $this->
LibStatut($this->status, $mode, $type);
5314 return $this->
LibStatut($this->status_buy, $mode, $type);
5316 return $this->
LibStatut($this->status_batch, $mode, $type);
5319 return $this->
LibStatut($this->status_buy, $mode, $type);
5335 global $conf, $langs;
5337 $labelStatus = $labelStatusShort =
'';
5339 $langs->load(
'products');
5340 if (isModEnabled(
'productbatch')) {
5341 $langs->load(
"productbatch");
5347 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial')));
5348 return dolGetStatus($label);
5350 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort')));
5351 return dolGetStatus($label);
5355 return dolGetStatus($langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch'),
'',
'', empty($status) ?
'status5' :
'status4', 3,
'dot');
5361 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'));
5365 $statuttrans = empty($status) ?
'status5' :
'status4';
5370 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSellShort');
5371 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSell');
5372 } elseif ($type == 1) {
5373 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuyShort');
5374 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuy');
5375 } elseif ($type == 2) {
5376 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch');
5377 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort');
5379 } elseif ($status == 1) {
5382 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSellShort');
5383 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSell');
5384 } elseif ($type == 1) {
5385 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnBuyShort');
5386 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnBuy');
5387 } elseif ($type == 2) {
5388 $labelStatus = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial'));
5389 $labelStatusShort = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort'));
5391 } elseif ( $type == 2 && $status == 2 ) {
5392 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSerial');
5393 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort');
5397 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'),
'',
'',
'status0', 0);
5399 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statuttrans, $mode);
5412 $langs->load(
'products');
5414 if (isset($this->finished) && $this->finished >= 0) {
5415 $sql =
"SELECT label, code FROM ".$this->db->prefix().
"c_product_nature where code = ".((int) $this->finished).
" AND active=1";
5416 $resql = $this->db->query($sql);
5417 if ($resql && $this->db->num_rows($resql) > 0) {
5418 $res = $this->db->fetch_array($resql);
5419 $label = $langs->trans(
$res[
'label']);
5420 $this->db->free($resql);
5423 $this->error = $this->db->error().
' sql='.$sql;
5424 dol_syslog(__METHOD__.
' Error '.$this->error, LOG_ERR);
5450 public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label =
'', $price = 0, $inventorycode =
'', $origin_element =
'', $origin_id =
null, $disablestockchangeforsubproduct = 0, $extrafields =
null)
5456 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5462 $nbpiece = abs($nbpiece);
5465 $op[0] =
"+".trim($nbpiece);
5466 $op[1] =
"-".trim($nbpiece);
5469 $movementstock->setOrigin($origin_element, $origin_id);
5470 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'',
'',
'',
'',
false, 0, $disablestockchangeforsubproduct);
5474 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5475 $movementstock->array_options = $array_options;
5476 $movementstock->insertExtraFields();
5478 $this->db->commit();
5481 $this->error = $movementstock->error;
5482 $this->errors = $movementstock->errors;
5484 $this->db->rollback();
5512 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)
5518 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5524 $nbpiece = abs($nbpiece);
5527 $op[0] =
"+".trim($nbpiece);
5528 $op[1] =
"-".trim($nbpiece);
5531 $movementstock->setOrigin($origin_element, $origin_id);
5532 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'', $dlc, $dluo, $lot,
false, 0, $disablestockchangeforsubproduct);
5536 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5537 $movementstock->array_options = $array_options;
5538 $movementstock->insertExtraFields();
5540 $this->db->commit();
5543 $this->error = $movementstock->error;
5544 $this->errors = $movementstock->errors;
5546 $this->db->rollback();
5566 public function load_stock($option =
'', $includedraftpoforvirtual =
null, $dateofvirtualstock =
null)
5571 $this->stock_reel = 0;
5572 $this->stock_warehouse = array();
5573 $this->stock_theorique = 0;
5576 $warehouseStatus = array();
5577 if (preg_match(
'/warehouseclosed/', $option)) {
5580 if (preg_match(
'/warehouseopen/', $option)) {
5583 if (preg_match(
'/warehouseinternal/', $option)) {
5584 if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
5591 $sql =
"SELECT ps.rowid, ps.reel, ps.fk_entrepot";
5592 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
5593 $sql .=
", ".$this->db->prefix().
"entrepot as w";
5594 $sql .=
" WHERE w.entity IN (".getEntity(
'stock').
")";
5595 $sql .=
" AND w.rowid = ps.fk_entrepot";
5596 $sql .=
" AND ps.fk_product = ".((int) $this->
id);
5597 if (count($warehouseStatus)) {
5598 $sql .=
" AND w.statut IN (".$this->db->sanitize(implode(
',', $warehouseStatus)).
")";
5601 $sql .=
" ORDER BY ps.reel ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'DESC':
'ASC');
5603 dol_syslog(get_class($this).
"::load_stock", LOG_DEBUG);
5604 $result = $this->db->query($sql);
5606 $num = $this->db->num_rows($result);
5610 $row = $this->db->fetch_object($result);
5611 $this->stock_warehouse[$row->fk_entrepot] =
new stdClass();
5612 $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
5613 $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
5614 if ((!preg_match(
'/nobatch/', $option)) && $this->
hasbatch()) {
5615 $this->stock_warehouse[$row->fk_entrepot]->detail_batch =
Productbatch::findAll($this->db, $row->rowid, 1, $this->id);
5617 $this->stock_reel += $row->reel;
5621 $this->db->free($result);
5623 if (!preg_match(
'/novirtual/', $option)) {
5629 $this->error = $this->db->lasterror();
5648 global $conf, $hookmanager, $action;
5650 $stock_commande_client = 0;
5651 $stock_commande_fournisseur = 0;
5652 $stock_sending_client = 0;
5653 $stock_reception_fournisseur = 0;
5654 $stock_inproduction = 0;
5658 if (isModEnabled(
'commande')) {
5663 $stock_commande_client = $this->stats_commande[
'qty'];
5665 if (isModEnabled(
"expedition")) {
5666 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
5667 $filterShipmentStatus =
'';
5668 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
5670 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5677 $stock_sending_client = $this->stats_expedition[
'qty'];
5679 if (isModEnabled(
"supplier_order")) {
5680 $filterStatus = empty($conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK) ?
'3,4' : $conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK;
5681 if (isset($includedraftpoforvirtual)) {
5682 $filterStatus =
'0,1,2,'.$filterStatus;
5688 $stock_commande_fournisseur = $this->stats_commande_fournisseur[
'qty'];
5690 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) && empty($conf->reception->enabled)) {
5692 $filterStatus =
'4';
5693 if (isset($includedraftpoforvirtual)) {
5694 $filterStatus =
'0,'.$filterStatus;
5700 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5702 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) && isModEnabled(
"reception")) {
5704 $filterStatus =
'4';
5705 if (isset($includedraftpoforvirtual)) {
5706 $filterStatus =
'0,'.$filterStatus;
5712 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5714 if (isModEnabled(
'mrp')) {
5719 $stock_inproduction = $this->stats_mrptoproduce[
'qty'] - $this->stats_mrptoconsume[
'qty'];
5722 $this->stock_theorique = $this->stock_reel + $stock_inproduction;
5725 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5726 $this->stock_theorique -= ($stock_commande_client - $stock_sending_client);
5727 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER)) {
5728 $this->stock_theorique += 0;
5729 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
5730 $this->stock_theorique -= $stock_commande_client;
5733 if (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE)) {
5734 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5735 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
5736 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5737 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
5738 $this->stock_theorique -= $stock_reception_fournisseur;
5739 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
5740 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5743 $hookmanager->initHooks(array(
'productdao'));
5744 $parameters = array(
'id'=>$this->
id,
'includedraftpoforvirtual' => $includedraftpoforvirtual);
5746 $reshook = $hookmanager->executeHooks(
'loadvirtualstock', $parameters, $this, $action);
5748 $this->stock_theorique = $hookmanager->resArray[
'stock_theorique'];
5766 $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";
5767 $sql .=
" WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = ".((int) $this->
id).
" AND pb.batch = '".$this->db->escape($batch).
"'";
5768 $sql .=
" GROUP BY pb.batch, pb.eatby, pb.sellby";
5769 dol_syslog(get_class($this).
"::loadBatchInfo load first entry found for lot/serial = ".$batch, LOG_DEBUG);
5770 $resql = $this->db->query($sql);
5772 $num = $this->db->num_rows($resql);
5775 $obj = $this->db->fetch_object($resql);
5776 $result[] = array(
'batch'=>$batch,
'eatby'=>$this->db->jdate($obj->eatby),
'sellby'=>$this->db->jdate($obj->sellby),
'qty'=>$obj->qty);
5782 $this->db->rollback();
5800 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5806 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos";
5808 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product').dol_sanitizeFileName($this->
ref);
5813 $dir_osencoded = $dir;
5815 if (is_dir($dir_osencoded)) {
5816 $originImage = $dir.
'/'.$file[
'name'];
5827 if (is_numeric($result) && $result > 0) {
5844 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5845 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5851 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos/";
5853 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product');
5859 if (file_exists($dir_osencoded)) {
5860 $handle = opendir($dir_osencoded);
5861 if (is_resource($handle)) {
5862 while (($file = readdir($handle)) !==
false) {
5864 $file = utf8_encode($file);
5886 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5887 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5893 $handle = @opendir($dir_osencoded);
5894 if (is_resource($handle)) {
5895 while (($file = readdir($handle)) !==
false) {
5897 $file = utf8_encode($file);
5904 $photo_vignette =
'';
5906 if (preg_match(
'/('.$this->regeximgext.
')$/i', $photo, $regs)) {
5907 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $photo).
'_small'.$regs[0];
5910 $dirthumb = $dir.
'thumbs/';
5914 $obj[
'photo'] = $photo;
5915 if ($photo_vignette &&
dol_is_file($dirthumb.$photo_vignette)) {
5916 $obj[
'photo_vignette'] =
'thumbs/'.$photo_vignette;
5918 $obj[
'photo_vignette'] =
"";
5921 $tabobj[$nbphoto - 1] = $obj;
5924 if ($nbmax && $nbphoto >= $nbmax) {
5946 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5947 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5949 $dir = dirname($file).
'/';
5950 $dirthumb = $dir.
'/thumbs/';
5951 $filename = preg_replace(
'/'.preg_quote($dir,
'/').
'/i',
'', $file);
5957 if (preg_match(
'/('.$this->regeximgext.
')$/i', $filename, $regs)) {
5958 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_small'.$regs[0];
5959 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5963 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_mini'.$regs[0];
5964 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5981 $infoImg = getimagesize($file_osencoded);
5982 $this->imgWidth = $infoImg[0];
5983 $this->imgHeight = $infoImg[1];
5995 global $hookmanager;
5997 $this->nb = array();
5999 $sql =
"SELECT count(p.rowid) as nb, fk_product_type";
6000 $sql .=
" FROM ".$this->db->prefix().
"product as p";
6001 $sql .=
' WHERE p.entity IN ('.getEntity($this->element, 1).
')';
6003 if (is_object($hookmanager)) {
6004 $parameters = array();
6005 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
6006 $sql .= $hookmanager->resPrint;
6008 $sql .=
' GROUP BY fk_product_type';
6010 $resql = $this->db->query($sql);
6012 while ($obj = $this->db->fetch_object($resql)) {
6013 if ($obj->fk_product_type == 1) {
6014 $this->nb[
"services"] = $obj->nb;
6016 $this->nb[
"products"] = $obj->nb;
6019 $this->db->free($resql);
6023 $this->error = $this->db->error();
6056 return ($this->mandatory_period == 1 ?
true :
false);
6073 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
6074 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
6075 foreach ($dirsociete as $dirroot) {
6081 $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
6084 $result = $mod->getNextValue($object, $type);
6086 dol_syslog(get_class($this).
"::get_barcode barcode=".$result.
" module=".$var);
6100 global $user, $langs, $conf, $mysoc;
6105 $this->specimen = 1;
6107 $this->
ref =
'PRODUCT_SPEC';
6108 $this->label =
'PRODUCT SPECIMEN';
6109 $this->
description =
'This is description of this product specimen that was created the '.dol_print_date($now,
'dayhourlog').
'.';
6110 $this->specimen = 1;
6111 $this->country_id = 1;
6113 $this->status_buy = 1;
6115 $this->note_private =
'This is a comment (private)';
6116 $this->note_public =
'This is a comment (public)';
6117 $this->date_creation = $now;
6118 $this->date_modification = $now;
6121 $this->weight_units = 3;
6124 $this->length_units = 1;
6126 $this->width_units = 0;
6127 $this->height =
null;
6128 $this->height_units =
null;
6130 $this->surface = 30;
6131 $this->surface_units = 0;
6132 $this->volume = 300;
6133 $this->volume_units = 0;
6135 $this->barcode = -1;
6148 if (!$this->fk_unit) {
6152 $langs->load(
'products');
6154 $label_type =
'label';
6155 if ($type ==
'short') {
6156 $label_type =
'short_label';
6159 $sql =
"SELECT ".$label_type.
", code from ".$this->db->prefix().
"c_units where rowid = ".((int) $this->fk_unit);
6161 $resql = $this->db->query($sql);
6162 if ($resql && $this->db->num_rows($resql) > 0) {
6163 $res = $this->db->fetch_array($resql);
6164 $label = ($label_type ==
'short_label' ?
$res[$label_type] :
'unit'.$res[
'code']);
6165 $this->db->free($resql);
6168 $this->error = $this->db->error();
6169 dol_syslog(get_class($this).
"::getLabelOfUnit Error ".$this->error, LOG_ERR);
6181 return ($this->status_batch > 0 ?
true :
false);
6196 $maxpricesupplier = 0;
6198 if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE)) {
6199 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
6201 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->
id,
'',
'');
6203 if (is_array($product_fourn_list) && count($product_fourn_list) > 0) {
6204 foreach ($product_fourn_list as $productfourn) {
6205 if ($productfourn->fourn_unitprice > $maxpricesupplier) {
6206 $maxpricesupplier = $productfourn->fourn_unitprice;
6210 $maxpricesupplier *= $conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE;
6214 return $maxpricesupplier;
6230 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
6231 return parent::setCategoriesCommon($categories, Categorie::TYPE_PRODUCT);
6245 'product_customer_price',
6246 'product_customer_price_log'
6267 $sql =
"SELECT rowid, level, fk_level, var_percent, var_min_percent FROM ".$this->db->prefix().
"product_pricerules";
6268 $query = $this->db->query($sql);
6272 while ($result = $this->db->fetch_object($query)) {
6273 $rules[$result->level] = $result;
6281 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
6282 $price = $baseprice;
6283 $price_min = $baseprice;
6287 if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
6288 $price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent / 100));
6291 $prices[$i] = $price;
6294 if (isset($rules[$i]->var_min_percent) && $rules[$i]->var_min_percent) {
6295 $price_min = $price * (1 - ($rules[$i]->var_min_percent / 100));
6299 $check_amount = (($price == $this->multiprices[$i]) && ($price_min == $this->multiprices_min[$i]));
6300 $check_type = ($baseprice == $this->multiprices_base_type[$i]);
6302 if ($check_amount && $check_type) {
6306 if ($this->
updatePrice($price, $price_type, $user, $price_vat, $price_min, $i, $npr, $psq,
true) < 0) {
6324 return $user->rights->produit;
6326 return $user->rights->service;
6338 $sql =
"SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification,";
6339 $sql .=
" p.fk_user_author, p.fk_user_modif";
6340 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as p";
6341 $sql .=
" WHERE p.rowid = ".((int) $id);
6343 $result = $this->db->query($sql);
6345 if ($this->db->num_rows($result)) {
6346 $obj = $this->db->fetch_object($result);
6348 $this->
id = $obj->rowid;
6350 if ($obj->fk_user_author) {
6351 $cuser =
new User($this->db);
6352 $cuser->fetch($obj->fk_user_author);
6353 $this->user_creation = $cuser;
6356 if ($obj->fk_user_modif) {
6357 $muser =
new User($this->db);
6358 $muser->fetch($obj->fk_user_modif);
6359 $this->user_modification = $muser;
6362 $this->
ref = $obj->ref;
6363 $this->date_creation = $this->db->jdate($obj->date_creation);
6364 $this->date_modification = $this->db->jdate($obj->date_modification);
6367 $this->db->free($result);
6382 if (empty($this->duration_value)) {
6383 $this->errors[]=
'ErrorDurationForServiceNotDefinedCantCalculateHourlyPrice';
6387 if ($this->duration_unit ==
'i') {
6388 $prodDurationHours = 1. / 60;
6390 if ($this->duration_unit ==
'h') {
6391 $prodDurationHours = 1.;
6393 if ($this->duration_unit ==
'd') {
6394 $prodDurationHours = 24.;
6396 if ($this->duration_unit ==
'w') {
6397 $prodDurationHours = 24. * 7;
6399 if ($this->duration_unit ==
'm') {
6400 $prodDurationHours = 24. * 30;
6402 if ($this->duration_unit ==
'y') {
6403 $prodDurationHours = 24. * 365;
6407 return $prodDurationHours;
6420 global $langs,$conf;
6422 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
6424 $return =
'<div class="box-flex-item box-flex-grow-zero">';
6425 $return .=
'<div class="info-box info-box-sm">';
6426 $return .=
'<div class="info-box-img">';
6429 $label .= $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 120, 160, 0, 0, 0,
'',
'photoref photokanban');
6439 $return .=
'</div>';
6440 $return .=
'<div class="info-box-content">';
6441 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
6442 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
6443 if (property_exists($this,
'label')) {
6444 $return .=
'<br><span class="info-box-label opacitymedium">'.$this->label.
'</span>';
6446 if (property_exists($this,
'price') && property_exists($this,
'price_ttc')) {
6447 if ($this->price_base_type ==
'TTC') {
6448 $return .=
'<br><span class="info-box-status amount">'.price($this->price_ttc).
' '.$langs->trans(
"TTC").
'</span>';
6450 if ($this->status) {
6451 $return .=
'<br><span class="info-box-status amount">'.price($this->
price).
' '.$langs->trans(
"HT").
'</span>';
6455 if (property_exists($this,
'stock_reel')) {
6456 $return .=
'<br><span class="info-box-status opacitymedium">'.$langs->trans(
'PhysicalStock').
' : <span class="bold">'.$this->stock_reel.
'</span></span>';
6458 if (method_exists($this,
'getLibStatut')) {
6459 $return .=
'<br><span class="info-box-status margintoponly">'.$this->getLibStatut(3, 1).
' '.$this->
getLibStatut(3, 0).
'</span>';
6461 $return .=
'</div>';
6462 $return .=
'</div>';
6463 $return .=
'</div>';
6474 public $picto =
'service';
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteEcmFiles($mode=0)
Delete related files of object in database.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
isObjectUsed($id=0, $entity=0)
Function to check if an object is used by others (by children).
deleteExtraFields()
Delete all extra fields values for the current object.
addThumbs($file)
Build thumb.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
show_photos($modulepart, $sdir, $size=0, $nbmax=0, $nbbyrow=5, $showfilename=0, $showaction=0, $maxHeight=120, $maxWidth=160, $nolink=0, $overwritetitle=0, $usesharelink=0, $cache='', $addphotorefcss='photoref')
Show photos of an object (nbmax maximum), into several columns.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
const STATUS_OPEN_INTERNAL
Warehouse open and only operations for stock transfers/corrections allowed (not for customer shipping...
const STATUS_OPEN_ALL
Warehouse open and any operations are allowed (customer shipping, supplier dispatch,...
const STATUS_CLOSED
Warehouse closed, inactive.
const STATUS_CLOSED
Closed status.
const STATUS_VALIDATED
Validated status.
const STATUS_DRAFT
Draft status.
Class to manage stock movements.
Class to parse product price expressions.
Class ProductCombination Used to represent a product combination.
Class to manage predefined suppliers products.
Class to manage products or services.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
get_nb_achat($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units or supplier invoices in which product is included.
getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp=0)
Return price of sell of a product for a seller/buyer/product.
__construct($db)
Constructor.
$price_by_qty
Price by quantity arrays.
is_sousproduit($fk_parent, $fk_child)
Check if it is a sub-product into a kit.
$duration
Service expiration label (value + unit)
setPriceExpression($expression_id)
Sets the supplier price expression.
getArrayForPriceCompare($level=0)
used to check if price have really change to avoid log pollution
get_arbo_each_prod($multiply=1, $ignore_stock_load=0)
Build the tree of subproducts and return it.
check_barcode($valuetotest, $typefortest)
Check barcode.
correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $dlc='', $dluo='', $lot='', $inventorycode='', $origin_element='', $origin_id=null, $disablestockchangeforsubproduct=0, $extrafields=null)
Adjust stock in a warehouse for product with batch number.
list_suppliers()
Return list of suppliers providing the product or service.
load_stats_mo($socid=0)
Charge tableau des stats OF pour le produit/service.
isVariant()
Return if loaded product is a variant.
hasVariants()
Return if a product has variants or not.
delMultiLangs($langtodelete, $user)
Delete a language for this product.
getLabelOfUnit($type='long')
Returns the text label from units dictionary.
load_stats_proposal_supplier($socid=0)
Charge tableau des stats propale pour le produit/service.
getLibFinished()
Retour label of nature of product.
add_fournisseur($user, $id_fourn, $ref_fourn, $quantity)
Add a supplier price for the product.
hasFatherOrChild($mode=0)
Count all parent and children products for current product (first level only)
load_stats_facturerec($socid=0)
Charge tableau des stats facture recurrentes pour le produit/service.
$product_id_already_linked
Product ID already linked to a reference supplier.
get_nb_propalsupplier($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in proposals in which product is included.
get_nb_contract($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in orders in which product is included.
load_stats_facture_fournisseur($socid=0)
Charge tableau des stats facture pour le produit/service.
get_nb_ordersupplier($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in orders in which product is included.
getMultiLangs()
Load array this->multilangs.
get_nb_mos($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in orders in which product is included.
clone_associations($fromId, $toId)
Clone links between products.
create($user, $notrigger=0)
Insert product into database.
load_stats_contrat($socid=0)
Charge tableau des stats contrat pour le produit/service.
isService()
Return if object is a product.
getRights()
Returns the rights used for this class.
loadBatchInfo($batch)
Load existing information about a serial.
$pmp
Average price value for product entry into stock (PMP)
load_stock($option='', $includedraftpoforvirtual=null, $dateofvirtualstock=null)
Load information about stock of a product into ->stock_reel, ->stock_warehouse[] (including stock_war...
getProductDurationHours()
Return the duration in Hours of a service base on duration fields.
fetch($id='', $ref='', $ref_ext='', $barcode='', $ignore_expression=0, $ignore_price_load=0, $ignore_lang_load=0)
Load a product in memory from database.
$default_vat_code
Default VAT code for product (link to code into llx_c_tva but without foreign keys)
$duration_unit
Serivce expiration unit.
get_buyprice($prodfournprice, $qty, $product_id=0, $fourn_ref='', $fk_soc=0)
Read price used by a provider.
clone_fournisseurs($fromId, $toId)
Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre.
const TYPE_PRODUCT
Regular product.
$stock_warehouse
Contains detail of stock of product into each warehouse.
$fk_default_workstation
Service Workstation.
add_photo($sdir, $file)
Move an uploaded file described into $file array into target directory $sdir.
log_price_delete($user, $rowid)
Delete a price line.
update_sousproduit($id_pere, $id_fils, $qty, $incdec=1)
Modify composed product.
update($id, $user, $notrigger=false, $action='update', $updatetype=false)
Update a record into database.
info($id)
Load information for tab info.
const TYPE_STOCKKIT
Advanced feature: stock kit.
load_stats_inproduction($socid=0, $filtrestatut='', $forVirtualStock=0, $dateofvirtualstock=null)
Charge tableau des stats production pour le produit/service.
correct_stock($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $inventorycode='', $origin_element='', $origin_id=null, $disablestockchangeforsubproduct=0, $extrafields=null)
Adjust stock in a warehouse for product.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
$multiprices
Arrays for multiprices.
$localtax1_tx
Other local taxes.
getChildsArbo($id, $firstlevelonly=0, $level=1, $parents=array())
Return childs of product $id.
load_virtual_stock($includedraftpoforvirtual=null, $dateofvirtualstock=null)
Load value ->stock_theorique of a product.
load_stats_propale($socid=0)
Charge tableau des stats propale pour le produit/service.
get_barcode($object, $type='')
Get a barcode from the module to generate barcode values.
setAccountancyCode($type, $value)
Sets an accountancy code for a product.
load_stats_facture($socid=0)
Charge tableau des stats facture pour le produit/service.
$remise_percent
Default discount percent.
setCategories($categories)
Sets object to supplied categories.
load_stats_reception($socid=0, $filtrestatut='', $forVirtualStock=0, $dateofvirtualstock=null)
Charge tableau des stats réception fournisseur pour le produit/service.
get_nb_propal($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in proposals in which product is included.
setMultiLangs($user)
Update or add a translation for a product.
$tva_npr
int French VAT NPR (0 or 1)
$tva_tx
Default VAT rate of product.
load_stats_bom($socid=0)
Charge tableau des stats OF pour le produit/service.
hasbatch()
Return if object has a sell-by date or eat-by date.
$weight
Metric of products.
load_stats_commande($socid=0, $filtrestatut='', $forVirtualStock=0)
Charge tableau des stats commande client pour le produit/service.
delete_photo($file)
Delete a photo and its thumbs.
fetch_prod_arbo($prod, $compl_path='', $multiply=1, $level=1, $id_parent=0, $ignore_stock_load=0)
Function recursive, used only by get_arbo_each_prod(), to build tree of subproducts into ->res Define...
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
getLibStatut($mode=0, $type=0)
Return label of status of object.
load_stats_sending($socid=0, $filtrestatut='', $forVirtualStock=0, $filterShipmentStatus='')
Charge tableau des stats expedition client pour le produit/service.
clone_price($fromId, $toId)
Recopie les prix d'un produit/service sur un autre.
$duration_value
Service expiration.
check()
Check that ref and label are ok.
updatePrice($newprice, $newpricebase, $user, $newvat='', $newminprice=0, $level=0, $newnpr=0, $newpbq=0, $ignore_autogen=0, $localtaxes_array=array(), $newdefaultvatcode='')
Modify customer price of a product/Service for a given level.
initAsSpecimen()
Initialise an instance with random values.
liste_photos($dir, $nbmax=0)
Return an array with all photos of product found on disk.
getFather()
Return all parent products for current product (first level only)
getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0, $morecss='', $add_label=0, $sep=' - ')
Return clicable link of object (with eventually picto)
$product_fourn_id
Id du fournisseur.
$desiredstock
Ask for replenishment when $desiredstock < $stock_reel.
add_sousproduit($id_pere, $id_fils, $qty, $incdec=1)
Link a product/service to a parent product/service.
verify()
Check properties of product are ok (like name, barcode, ...).
get_nb_order($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units in orders in which product is included.
del_sousproduit($fk_parent, $fk_child)
Remove a link between a subproduct and a parent product/service.
load_state_board()
Load indicators this->nb for the dashboard.
min_recommended_price()
Return minimum product recommended price.
_log_price($user, $level=0)
Insert a track that we changed a customer price.
_get_stats($sql, $mode, $year=0)
Return an array formated for showing graphs.
$multilangs
Array for multilangs.
load_stats_commande_fournisseur($socid=0, $filtrestatut='', $forVirtualStock=0, $dateofvirtualstock=null)
Charge tableau des stats commande fournisseur pour le produit/service.
isMandatoryPeriod()
Return if object have a constraint on mandatory_period.
isProduct()
Return if object is a product.
generateMultiprices(User $user, $baseprice, $price_type, $price_vat, $npr, $psq)
Generates prices for a product based on product multiprice generation rules.
LibStatut($status, $mode=0, $type=0)
Return label of a given status.
const TYPE_SERVICE
Service.
is_photo_available($sdir)
Return if at least one photo is available.
get_image_size($file)
Load size of image file.
get_nb_vente($socid, $mode, $filteronproducttype=-1, $year=0, $morefilter='')
Return nb of units or customers invoices in which product is included.
getTooltipContentArray($params)
getTooltipContentArray
const TYPE_ASSEMBLYKIT
Advanced feature: assembly kit.
Class to manage products or services.
Manage record for batch number management.
static findAll($dbs, $fk_product_stock, $with_qty=0, $fk_product=0)
Return all batch detail records for a given product and warehouse.
File of class to manage predefined price products or services by customer.
Class to manage Dolibarr users.
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Make control on an uploaded file from an GUI page and move it to final destination.
dol_is_file($pathoffile)
Return if path is a file.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that returns whether VAT must be recoverable collected VAT (e.g.: VAT NPR in France)
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
measuring_units_squared($unit)
Transform a given unit scale into the square of that unit, if known.
measuring_units_cubed($unit)
Transform a given unit scale into the cube of that unit, if known.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type