38 require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
39 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
51 public $element =
'product';
56 public $table_element =
'product';
61 public $fk_element =
'fk_product';
66 protected $childtables = array(
67 'supplier_proposaldet' => array(
'name' =>
'SupplierProposal',
'parent' =>
'supplier_proposal',
'parentkey' =>
'fk_supplier_proposal'),
68 'propaldet' => array(
'name' =>
'Proposal',
'parent' =>
'propal',
'parentkey' =>
'fk_propal'),
69 'commandedet' => array(
'name' =>
'Order',
'parent' =>
'commande',
'parentkey' =>
'fk_commande'),
70 'facturedet' => array(
'name' =>
'Invoice',
'parent' =>
'facture',
'parentkey' =>
'fk_facture'),
71 'contratdet' => array(
'name' =>
'Contract',
'parent' =>
'contrat',
'parentkey' =>
'fk_contrat'),
72 'facture_fourn_det' => array(
'name' =>
'SupplierInvoice',
'parent' =>
'facture_fourn',
'parentkey' =>
'fk_facture_fourn'),
73 'commande_fournisseurdet' => array(
'name' =>
'SupplierOrder',
'parent' =>
'commande_fournisseur',
'parentkey' =>
'fk_commande')
81 public $ismultientitymanaged = 1;
86 public $isextrafieldmanaged = 1;
91 public $picto =
'product';
98 public $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
141 public $price_formated;
150 public $price_ttc_formated;
164 public $price_min_ttc;
170 public $price_base_type;
174 public $multiprices_ttc = array();
175 public $multiprices_base_type = array();
176 public $multiprices_min = array();
177 public $multiprices_min_ttc = array();
178 public $multiprices_tva_tx = array();
179 public $multiprices_recuperableonly = array();
183 public $prices_by_qty = array();
184 public $prices_by_qty_id = array();
185 public $prices_by_qty_list = array();
210 public $localtax2_tx;
211 public $localtax1_type;
212 public $localtax2_type;
216 public $desc_supplier;
217 public $vatrate_supplier;
218 public $default_vat_code_supplier;
219 public $fourn_multicurrency_price;
220 public $fourn_multicurrency_unitprice;
221 public $fourn_multicurrency_tx;
222 public $fourn_multicurrency_id;
223 public $fourn_multicurrency_code;
229 public $qc_frequency;
236 public $stock_reel = 0;
243 public $stock_theorique;
260 public $seuil_stock_alerte = 0;
305 public $status_buy = 0;
327 public $fk_default_bom;
334 public $status_batch = 0;
341 public $batch_mask =
'';
359 public $weight_units;
361 public $length_units;
365 public $height_units;
367 public $surface_units;
369 public $volume_units;
372 public $net_measure_units;
374 public $accountancy_code_sell;
375 public $accountancy_code_sell_intra;
376 public $accountancy_code_sell_export;
377 public $accountancy_code_buy;
378 public $accountancy_code_buy_intra;
379 public $accountancy_code_buy_export;
393 public $barcode_type;
400 public $barcode_type_code;
402 public $stats_propale = array();
403 public $stats_commande = array();
404 public $stats_contrat = array();
405 public $stats_facture = array();
406 public $stats_proposal_supplier = array();
407 public $stats_commande_fournisseur = array();
408 public $stats_expedition = array();
409 public $stats_reception = array();
410 public $stats_mo = array();
411 public $stats_bom = array();
412 public $stats_mrptoconsume = array();
413 public $stats_mrptoproduce = array();
414 public $stats_facturerec = array();
415 public $stats_facture_fournisseur = array();
424 public $date_creation;
429 public $date_modification;
447 public $fk_default_warehouse;
451 public $fk_price_expression;
456 public $fourn_price_base_type;
468 public $ref_supplier;
482 public $price_autogen = 0;
489 public $supplierprices;
509 public $is_object_used;
515 public $mandatory_period;
545 public $fields = array(
546 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
547 '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'),
548 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>5),
549 'label' =>array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>2,
'position'=>15,
'csslist'=>
'tdoverflowmax250'),
550 'barcode' =>array(
'type'=>
'varchar(255)',
'label'=>
'Barcode',
'enabled'=>
'isModEnabled("barcode")',
'position'=>20,
'visible'=>-1,
'showoncombobox'=>3),
551 'fk_barcode_type' => array(
'type'=>
'integer',
'label'=>
'BarcodeType',
'enabled'=>
'1',
'position'=>21,
'notnull'=>0,
'visible'=>-1,),
552 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61),
553 'note' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62),
554 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>500),
555 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>501),
557 'fk_user_author'=>array(
'type'=>
'integer',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>510,
'foreignkey'=>
'llx_user.rowid'),
558 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>511),
560 'localtax1_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax1tx',
'enabled'=>
'1',
'position'=>150,
'notnull'=>0,
'visible'=>-1,),
561 'localtax1_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax1type',
'enabled'=>
'1',
'position'=>155,
'notnull'=>1,
'visible'=>-1,),
562 'localtax2_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax2tx',
'enabled'=>
'1',
'position'=>160,
'notnull'=>0,
'visible'=>-1,),
563 'localtax2_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax2type',
'enabled'=>
'1',
'position'=>165,
'notnull'=>1,
'visible'=>-1,),
564 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'index'=>0,
'position'=>1000),
567 'mandatory_period' => array(
'type'=>
'integer',
'label'=>
'mandatory_period',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'default'=>0,
'index'=>1,
'position'=>1000),
607 $this->
ref = trim($this->
ref);
635 public function create($user, $notrigger = 0)
637 global $conf, $langs;
643 $this->
ref = trim($this->
ref);
647 $this->label = trim($this->label);
648 $this->price_ttc =
price2num($this->price_ttc);
650 $this->price_min_ttc =
price2num($this->price_min_ttc);
651 $this->price_min =
price2num($this->price_min);
652 if (empty($this->tva_tx)) {
655 if (empty($this->tva_npr)) {
659 if (empty($this->localtax1_tx)) {
660 $this->localtax1_tx = 0;
662 if (empty($this->localtax2_tx)) {
663 $this->localtax2_tx = 0;
665 if (empty($this->localtax1_type)) {
666 $this->localtax1_type =
'0';
668 if (empty($this->localtax2_type)) {
669 $this->localtax2_type =
'0';
671 if (empty($this->
price)) {
674 if (empty($this->price_min)) {
675 $this->price_min = 0;
678 if (empty($this->price_by_qty)) {
679 $this->price_by_qty = 0;
682 if (empty($this->status)) {
685 if (empty($this->status_buy)) {
686 $this->status_buy = 0;
695 if ($this->price_base_type ==
'TTC' && $this->price_ttc > 0) {
696 $price_ttc =
price2num($this->price_ttc,
'MU');
697 $price_ht =
price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),
'MU');
701 if ($this->price_base_type !=
'TTC' && $this->
price > 0) {
703 $price_ttc =
price2num($this->
price * (1 + ($this->tva_tx / 100)),
'MU');
707 if (($this->price_min_ttc > 0) && ($this->price_base_type ==
'TTC')) {
708 $price_min_ttc =
price2num($this->price_min_ttc,
'MU');
709 $price_min_ht =
price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),
'MU');
713 if (($this->price_min > 0) && ($this->price_base_type !=
'TTC')) {
714 $price_min_ht =
price2num($this->price_min,
'MU');
715 $price_min_ttc =
price2num($this->price_min * (1 + ($this->tva_tx / 100)),
'MU');
718 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
719 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
720 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
721 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
722 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
723 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
726 $this->barcode = trim($this->barcode);
727 $this->mandatory_period = empty($this->mandatory_period) ? 0 : $this->mandatory_period;
729 if (empty($this->label)) {
730 $this->error =
'ErrorMandatoryParametersNotProvided';
734 if (empty($this->
ref) || $this->
ref ==
'auto') {
737 if ($module !=
'mod_codeproduct_leopard') {
738 if (substr($module, 0, 16) ==
'mod_codeproduct_' && substr($module, -3) ==
'php') {
739 $module = substr($module, 0,
dol_strlen($module) - 4);
742 $modCodeProduct =
new $module;
743 if (!empty($modCodeProduct->code_auto)) {
744 $this->
ref = $modCodeProduct->getNextValue($this, $this->
type);
746 unset($modCodeProduct);
749 if (empty($this->
ref)) {
750 $this->error =
'ProductModuleNotSetupForAutoRef';
755 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);
762 if ($this->barcode == -1) {
763 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
768 $result = $this->
verify();
771 $sql =
"SELECT count(*) as nb";
772 $sql .=
" FROM ".$this->db->prefix().
"product";
773 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
774 $sql .=
" AND ref = '".$this->db->escape($this->
ref).
"'";
776 $result = $this->db->query(
$sql);
778 $obj = $this->db->fetch_object($result);
781 $sql =
"INSERT INTO ".$this->db->prefix().
"product (";
786 $sql .=
", price_min";
787 $sql .=
", price_min_ttc";
789 $sql .=
", fk_user_author";
790 $sql .=
", fk_product_type";
792 $sql .=
", price_ttc";
793 $sql .=
", price_base_type";
796 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
797 $sql .=
", accountancy_code_buy";
798 $sql .=
", accountancy_code_buy_intra";
799 $sql .=
", accountancy_code_buy_export";
800 $sql .=
", accountancy_code_sell";
801 $sql .=
", accountancy_code_sell_intra";
802 $sql .=
", accountancy_code_sell_export";
805 $sql .=
", finished";
807 $sql .=
", batch_mask";
809 $sql .=
", mandatory_period";
810 $sql .=
") VALUES (";
811 $sql .=
"'".$this->db->idate($now).
"'";
812 $sql .=
", ".((int) $conf->entity);
813 $sql .=
", '".$this->db->escape($this->
ref).
"'";
814 $sql .=
", ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
815 $sql .=
", ".price2num($price_min_ht);
816 $sql .=
", ".price2num($price_min_ttc);
817 $sql .=
", ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
818 $sql .=
", ".((int) $user->id);
820 $sql .=
", ".price2num($price_ht,
'MT');
821 $sql .=
", ".price2num($price_ttc,
'MT');
822 $sql .=
", '".$this->db->escape($this->price_base_type).
"'";
823 $sql .=
", ".((int) $this->status);
824 $sql .=
", ".((int) $this->status_buy);
825 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
826 $sql .=
", '".$this->db->escape($this->accountancy_code_buy).
"'";
827 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_intra).
"'";
828 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_export).
"'";
829 $sql .=
", '".$this->db->escape($this->accountancy_code_sell).
"'";
830 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_intra).
"'";
831 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_export).
"'";
833 $sql .=
", '".$this->db->escape($this->canvas).
"'";
834 $sql .=
", ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
'NULL' : (
int) $this->finished);
835 $sql .=
", ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : ((
int) $this->status_batch));
836 $sql .=
", '".$this->db->escape($this->batch_mask).
"'";
837 $sql .=
", ".($this->fk_unit > 0 ? ((int) $this->fk_unit) :
'NULL');
838 $sql .=
", '".$this->db->escape($this->mandatory_period).
"'";
841 dol_syslog(get_class($this).
"::Create", LOG_DEBUG);
842 $result = $this->db->query(
$sql);
844 $id = $this->db->last_insert_id($this->db->prefix().
"product");
848 $this->
price = $price_ht;
849 $this->price_ttc = $price_ttc;
850 $this->price_min = $price_min_ht;
851 $this->price_min_ttc = $price_min_ttc;
855 if ($this->
update($id, $user,
true,
'add') <= 0) {
860 $this->error = $this->db->lasterror();
864 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
865 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " .((int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
867 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
868 $sql .=
" fk_product";
870 $sql .=
", accountancy_code_buy";
871 $sql .=
", accountancy_code_buy_intra";
872 $sql .=
", accountancy_code_buy_export";
873 $sql .=
", accountancy_code_sell";
874 $sql .=
", accountancy_code_sell_intra";
875 $sql .=
", accountancy_code_sell_export";
876 $sql .=
") VALUES (";
878 $sql .=
", " . $conf->entity;
879 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
880 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
881 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
882 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
883 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
884 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
886 $result = $this->db->query(
$sql);
889 $this->error =
'ErrorFailedToInsertAccountancyForEntity';
894 $this->error =
'ErrorFailedToGetInsertedId';
898 $this->error = $this->db->lasterror();
902 $langs->load(
"products");
904 $this->error =
"ErrorProductAlreadyExists";
905 dol_syslog(get_class($this).
"::Create fails, ref ".$this->
ref.
" already exists");
909 $this->error = $this->db->lasterror();
912 if (!$error && !$notrigger) {
925 $this->db->rollback();
929 $this->db->rollback();
930 dol_syslog(get_class($this).
"::Create fails verify ".join(
',', $this->errors), LOG_WARNING);
946 $this->errors = array();
949 $this->
ref = trim($this->
ref);
952 $this->errors[] =
'ErrorBadRef';
956 $arrayofnonnegativevalue = array(
'weight'=>
'Weight',
'width'=>
'Width',
'height'=>
'Height',
'length'=>
'Length',
'surface'=>
'Surface',
'volume'=>
'Volume');
957 foreach ($arrayofnonnegativevalue as $key => $value) {
958 if (property_exists($this, $key) && !empty($this->$key) && ($this->$key < 0)) {
959 $langs->loadLangs(array(
"main",
"other"));
960 $this->error = $langs->trans(
"FieldCannotBeNegative", $langs->transnoentitiesnoconv($value));
961 $this->errors[] = $this->error;
966 $rescode = $this->
check_barcode($this->barcode, $this->barcode_type_code);
968 if ($rescode == -1) {
969 $this->errors[] =
'ErrorBadBarCodeSyntax';
970 } elseif ($rescode == -2) {
971 $this->errors[] =
'ErrorBarCodeRequired';
972 } elseif ($rescode == -3) {
974 $this->errors[] =
'ErrorBarCodeAlreadyUsed';
998 if (
isModEnabled(
'barcode') && !empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
999 $module = strtolower($conf->global->BARCODE_PRODUCT_ADDON_NUM);
1001 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
1002 foreach ($dirsociete as $dirroot) {
1009 $mod =
new $module();
1011 dol_syslog(get_class($this).
"::check_barcode value=".$valuetotest.
" type=".$typefortest.
" module=".$module);
1012 $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest);
1030 public function update($id, $user, $notrigger =
false, $action =
'update', $updatetype =
false)
1032 global $langs, $conf, $hookmanager;
1037 if (!$this->label) {
1038 $this->label =
'MISSING LABEL';
1043 $this->
ref = trim($this->
ref);
1047 $this->label = trim($this->label);
1049 $this->note_private = (isset($this->note_private) ? trim($this->note_private) :
null);
1050 $this->note_public = (isset($this->note_public) ? trim($this->note_public) :
null);
1051 $this->net_measure =
price2num($this->net_measure);
1052 $this->net_measure_units = trim($this->net_measure_units);
1053 $this->weight =
price2num($this->weight);
1054 $this->weight_units = trim($this->weight_units);
1055 $this->length =
price2num($this->length);
1056 $this->length_units = trim($this->length_units);
1058 $this->width_units = trim($this->width_units);
1059 $this->height =
price2num($this->height);
1060 $this->height_units = trim($this->height_units);
1061 $this->surface =
price2num($this->surface);
1062 $this->surface_units = trim($this->surface_units);
1063 $this->volume =
price2num($this->volume);
1064 $this->volume_units = trim($this->volume_units);
1067 if (is_numeric($this->length_units)) {
1068 $this->width_units = $this->length_units;
1070 if (is_numeric($this->length_units)) {
1071 $this->height_units = $this->length_units;
1075 if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units) {
1076 $this->surface = $this->length * $this->width;
1079 if (empty($this->volume) && !empty($this->surface) && !empty($this->height) && $this->length_units == $this->height_units) {
1080 $this->volume = $this->surface * $this->height;
1084 if (empty($this->tva_tx)) {
1087 if (empty($this->tva_npr)) {
1090 if (empty($this->localtax1_tx)) {
1091 $this->localtax1_tx = 0;
1093 if (empty($this->localtax2_tx)) {
1094 $this->localtax2_tx = 0;
1096 if (empty($this->localtax1_type)) {
1097 $this->localtax1_type =
'0';
1099 if (empty($this->localtax2_type)) {
1100 $this->localtax2_type =
'0';
1102 if (empty($this->status)) {
1105 if (empty($this->status_buy)) {
1106 $this->status_buy = 0;
1109 if (empty($this->country_id)) {
1110 $this->country_id = 0;
1113 if (empty($this->state_id)) {
1114 $this->state_id = 0;
1118 $this->barcode = trim($this->barcode);
1120 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
1121 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
1122 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
1123 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
1124 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
1125 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
1132 if ($action !=
'add') {
1133 $result = $this->
verify();
1141 if (empty($this->oldcopy)) {
1147 if ($this->
hasbatch() && !$this->oldcopy->hasbatch()) {
1149 $valueforundefinedlot =
'000000';
1150 if (!empty($conf->global->STOCK_DEFAULT_BATCH)) {
1151 $valueforundefinedlot = $conf->global->STOCK_DEFAULT_BATCH;
1154 dol_syslog(
"Flag batch of product id=".$this->
id.
" is set to ON, so we will create missing records into product_batch");
1157 foreach ($this->stock_warehouse as $idW => $ObjW) {
1159 foreach ($ObjW->detail_batch as $detail) {
1160 if ($detail->batch == $valueforundefinedlot || $detail->batch ==
'Undefined') {
1162 $sqlclean =
"DELETE FROM ".$this->db->prefix().
"product_batch WHERE batch in('Undefined', '".$this->db->escape($valueforundefinedlot).
"') AND fk_product_stock = ".((int) $ObjW->id);
1163 $result = $this->db->query($sqlclean);
1171 $qty_batch += $detail->qty;
1175 if ($ObjW->real <> $qty_batch) {
1177 $ObjBatch->batch = $valueforundefinedlot;
1178 $ObjBatch->qty = ($ObjW->real - $qty_batch);
1179 $ObjBatch->fk_product_stock = $ObjW->id;
1181 if ($ObjBatch->create($user, 1) < 0) {
1183 $this->errors = $ObjBatch->errors;
1190 if ($this->barcode == -1) {
1191 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
1194 $sql =
"UPDATE ".$this->db->prefix().
"product";
1195 $sql .=
" SET label = '".$this->db->escape($this->label).
"'";
1198 $sql .=
", fk_product_type = ".((int) $this->
type);
1201 $sql .=
", ref = '".$this->db->escape($this->
ref).
"'";
1202 $sql .=
", ref_ext = ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1203 $sql .=
", default_vat_code = ".($this->default_vat_code ?
"'".$this->db->escape($this->default_vat_code).
"'" :
"null");
1204 $sql .=
", tva_tx = ".((float) $this->tva_tx);
1205 $sql .=
", recuperableonly = ".((int) $this->tva_npr);
1206 $sql .=
", localtax1_tx = ".((float) $this->localtax1_tx);
1207 $sql .=
", localtax2_tx = ".((float) $this->localtax2_tx);
1208 $sql .=
", localtax1_type = ".($this->localtax1_type !=
'' ?
"'".$this->db->escape($this->localtax1_type).
"'" :
"'0'");
1209 $sql .=
", localtax2_type = ".($this->localtax2_type !=
'' ?
"'".$this->db->escape($this->localtax2_type).
"'" :
"'0'");
1211 $sql .=
", barcode = ".(empty($this->barcode) ?
"null" :
"'".$this->db->escape($this->barcode).
"'");
1212 $sql .=
", fk_barcode_type = ".(empty($this->barcode_type) ?
"null" : $this->db->escape($this->barcode_type));
1214 $sql .=
", tosell = ".(int) $this->status;
1215 $sql .=
", tobuy = ".(int) $this->status_buy;
1216 $sql .=
", tobatch = ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : (
int) $this->status_batch);
1217 $sql .=
", batch_mask = '".$this->db->escape($this->batch_mask).
"'";
1219 $sql .=
", finished = ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
"null" : (
int) $this->finished);
1220 $sql .=
", fk_default_bom = ".((!isset($this->fk_default_bom) || $this->fk_default_bom < 0 || $this->fk_default_bom ==
'') ?
"null" : (
int) $this->fk_default_bom);
1221 $sql .=
", net_measure = ".($this->net_measure !=
'' ?
"'".$this->db->escape($this->net_measure).
"'" :
'null');
1222 $sql .=
", net_measure_units = ".($this->net_measure_units !=
'' ?
"'".$this->db->escape($this->net_measure_units).
"'" :
'null');
1223 $sql .=
", weight = ".($this->weight !=
'' ?
"'".$this->db->escape($this->weight).
"'" :
'null');
1224 $sql .=
", weight_units = ".($this->weight_units !=
'' ?
"'".$this->db->escape($this->weight_units).
"'" :
'null');
1225 $sql .=
", length = ".($this->length !=
'' ?
"'".$this->db->escape($this->length).
"'" :
'null');
1226 $sql .=
", length_units = ".($this->length_units !=
'' ?
"'".$this->db->escape($this->length_units).
"'" :
'null');
1227 $sql .=
", width= ".($this->width !=
'' ?
"'".$this->db->escape($this->width).
"'" :
'null');
1228 $sql .=
", width_units = ".($this->width_units !=
'' ?
"'".$this->db->escape($this->width_units).
"'" :
'null');
1229 $sql .=
", height = ".($this->height !=
'' ?
"'".$this->db->escape($this->height).
"'" :
'null');
1230 $sql .=
", height_units = ".($this->height_units !=
'' ?
"'".$this->db->escape($this->height_units).
"'" :
'null');
1231 $sql .=
", surface = ".($this->surface !=
'' ?
"'".$this->db->escape($this->surface).
"'" :
'null');
1232 $sql .=
", surface_units = ".($this->surface_units !=
'' ?
"'".$this->db->escape($this->surface_units).
"'" :
'null');
1233 $sql .=
", volume = ".($this->volume !=
'' ?
"'".$this->db->escape($this->volume).
"'" :
'null');
1234 $sql .=
", volume_units = ".($this->volume_units !=
'' ?
"'".$this->db->escape($this->volume_units).
"'" :
'null');
1235 $sql .=
", fk_default_warehouse = ".($this->fk_default_warehouse > 0 ? ((int) $this->fk_default_warehouse) :
'null');
1236 $sql .=
", fk_default_workstation = ".($this->fk_default_workstation > 0 ? ((int) $this->fk_default_workstation) :
'null');
1237 $sql .=
", seuil_stock_alerte = ".((isset($this->seuil_stock_alerte) && is_numeric($this->seuil_stock_alerte)) ? (
float) $this->seuil_stock_alerte :
'null');
1238 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
1239 $sql .=
", url = ".($this->url ?
"'".$this->db->escape($this->url).
"'" :
'null');
1240 $sql .=
", customcode = '".$this->db->escape($this->customcode).
"'";
1241 $sql .=
", fk_country = ".($this->country_id > 0 ? (int) $this->country_id :
'null');
1242 $sql .=
", fk_state = ".($this->state_id > 0 ? (int) $this->state_id :
'null');
1243 $sql .=
", lifetime = ".($this->lifetime > 0 ? (int) $this->lifetime :
'null');
1244 $sql .=
", qc_frequency = ".($this->qc_frequency > 0 ? (int) $this->qc_frequency :
'null');
1245 $sql .=
", note = ".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
'null');
1246 $sql .=
", note_public = ".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
'null');
1247 $sql .=
", duration = '".$this->db->escape($this->duration_value.$this->duration_unit).
"'";
1248 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1249 $sql .=
", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1250 $sql .=
", accountancy_code_buy_intra = '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1251 $sql .=
", accountancy_code_buy_export = '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1252 $sql .=
", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1253 $sql .=
", accountancy_code_sell_intra= '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1254 $sql .=
", accountancy_code_sell_export= '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1256 $sql .=
", desiredstock = ".((isset($this->desiredstock) && is_numeric($this->desiredstock)) ? (
float) $this->desiredstock :
"null");
1257 $sql .=
", cost_price = ".($this->cost_price !=
'' ? $this->db->escape($this->cost_price) :
'null');
1258 $sql .=
", fk_unit= ".(!$this->fk_unit ?
'NULL' : (int) $this->fk_unit);
1259 $sql .=
", price_autogen = ".(!$this->price_autogen ? 0 : 1);
1260 $sql .=
", fk_price_expression = ".($this->fk_price_expression != 0 ? (int) $this->fk_price_expression :
'NULL');
1261 $sql .=
", fk_user_modif = ".($user->id > 0 ? $user->id :
'NULL');
1262 $sql .=
", mandatory_period = ".($this->mandatory_period );
1264 $sql .=
" WHERE rowid = ".((int) $id);
1266 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1268 $resql = $this->db->query(
$sql);
1275 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".
$sql;
1283 if (!$error && !empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
1284 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " . ((
int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
1286 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
1287 $sql .=
" fk_product";
1289 $sql .=
", accountancy_code_buy";
1290 $sql .=
", accountancy_code_buy_intra";
1291 $sql .=
", accountancy_code_buy_export";
1292 $sql .=
", accountancy_code_sell";
1293 $sql .=
", accountancy_code_sell_intra";
1294 $sql .=
", accountancy_code_sell_export";
1295 $sql .=
") VALUES (";
1297 $sql .=
", " . $conf->entity;
1298 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1299 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1300 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1301 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1302 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1303 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1305 $result = $this->db->query(
$sql);
1308 $this->error =
'ErrorFailedToUpdateAccountancyForEntity';
1320 if (!$error && !$notrigger) {
1322 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1329 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
1331 if ($conf->product->dir_output) {
1334 if (file_exists($olddir)) {
1338 $res = @rename($olddir, $newdir);
1340 $langs->load(
"errors");
1341 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
1350 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1354 foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
1355 $currcomb->updateProperties($this, $user);
1359 $this->db->commit();
1362 $this->db->rollback();
1366 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1367 $langs->load(
"errors");
1368 if (empty($conf->barcode->enabled) || empty($this->barcode)) {
1369 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductAlreadyExists", $this->
ref);
1371 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductBarCodeAlreadyExists", $this->barcode);
1373 $this->errors[] = $this->error;
1374 $this->db->rollback();
1377 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".
$sql;
1378 $this->errors[] = $this->error;
1379 $this->db->rollback();
1384 $this->db->rollback();
1385 dol_syslog(get_class($this).
"::Update fails verify ".join(
',', $this->errors), LOG_WARNING);
1397 public function delete(
User $user, $notrigger = 0)
1399 global $conf, $langs;
1400 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1405 if (empty($this->
id)) {
1406 $this->error =
"Object must be fetched before calling delete";
1410 $this->error =
"ErrorForbidden";
1415 if (empty($objectisused)) {
1418 if (!$error && empty($notrigger)) {
1420 $result = $this->
call_trigger(
'PRODUCT_DELETE', $user);
1429 $sql =
"DELETE FROM ".$this->db->prefix().
'product_batch';
1430 $sql .=
" WHERE fk_product_stock IN (";
1431 $sql .=
"SELECT rowid FROM ".$this->db->prefix().
'product_stock';
1432 $sql .=
" WHERE fk_product = ".((int) $this->
id).
")";
1434 $result = $this->db->query(
$sql);
1437 $this->errors[] = $this->db->lasterror();
1443 $elements = array(
'product_fournisseur_price',
'product_price',
'product_lang',
'categorie_product',
'product_stock',
'product_customer_price',
'product_lot');
1444 foreach ($elements as $table) {
1446 $sql =
"DELETE FROM ".$this->db->prefix().$table;
1447 $sql .=
" WHERE fk_product = ".(int) $this->
id;
1449 $result = $this->db->query(
$sql);
1452 $this->errors[] = $this->db->lasterror();
1459 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1460 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
1465 if ($prodcomb->deleteByFkProductParent($user, $this->id) < 0) {
1467 $this->errors[] =
'Error deleting combinations';
1471 if (!$error && ($prodcomb->fetchByFkProductChild($this->id) > 0) && ($prodcomb->delete($user) < 0)) {
1473 $this->errors[] =
'Error deleting child combination';
1479 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
1480 $sql .=
" WHERE fk_product_pere = ".(int) $this->
id.
" OR fk_product_fils = ".(
int) $this->id;
1482 $result = $this->db->query(
$sql);
1485 $this->errors[] = $this->db->lasterror();
1494 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1500 $sqlz =
"DELETE FROM ".$this->db->prefix().
"product";
1501 $sqlz .=
" WHERE rowid = ".(int) $this->
id;
1503 $resultz = $this->db->query($sqlz);
1506 $this->errors[] = $this->db->lasterror();
1521 if ($conf->product->dir_output) {
1522 $dir = $conf->product->dir_output.
"/".$ref;
1523 if (file_exists($dir)) {
1526 $this->errors[] =
'ErrorFailToDeleteDir';
1534 $this->db->commit();
1537 foreach ($this->errors as $errmsg) {
1538 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1539 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1541 $this->db->rollback();
1545 $this->error =
"ErrorRecordIsUsedCantDelete";
1558 global $conf, $langs;
1560 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
1561 $current_lang = $langs->getDefaultLang();
1563 foreach ($langs_available as $key => $value) {
1564 if ($key == $current_lang) {
1565 $sql =
"SELECT rowid";
1566 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1567 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1568 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1570 $result = $this->db->query(
$sql);
1572 if ($this->db->num_rows($result)) {
1573 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1575 $sql2 .=
" label='".$this->db->escape($this->label).
"',";
1576 $sql2 .=
" description='".$this->db->escape($this->
description).
"'";
1577 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1578 $sql2 .=
", note='".$this->db->escape($this->other).
"'";
1580 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1582 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1583 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1587 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->label).
"',";
1588 $sql2 .=
" '".$this->db->escape($this->
description).
"'";
1589 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1590 $sql2 .=
", '".$this->db->escape($this->other).
"'";
1594 dol_syslog(get_class($this).
'::setMultiLangs key = current_lang = '.$key);
1595 if (!$this->db->query($sql2)) {
1596 $this->error = $this->db->lasterror();
1599 } elseif (isset($this->multilangs[$key])) {
1600 if (empty($this->multilangs[
"$key"][
"label"])) {
1601 $this->errors[] = $key .
' : ' . $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Label"));
1605 $sql =
"SELECT rowid";
1606 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1607 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1608 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1610 $result = $this->db->query(
$sql);
1612 if ($this->db->num_rows($result)) {
1613 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1615 $sql2 .=
" label = '".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1616 $sql2 .=
" description = '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1617 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1618 $sql2 .=
", note = '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1620 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1622 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1623 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1627 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1628 $sql2 .=
" '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1629 if (!empty($conf->global->PRODUCT_USE_OTHER_FIELD_IN_TRANSLATION)) {
1630 $sql2 .=
", '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1636 if ($this->multilangs[
"$key"][
"label"] || $this->multilangs[
"$key"][
"description"]) {
1637 if (!$this->db->query($sql2)) {
1638 $this->error = $this->db->lasterror();
1648 $result = $this->
call_trigger(
'PRODUCT_SET_MULTILANGS', $user);
1650 $this->error = $this->db->lasterror();
1668 $sql =
"DELETE FROM ".$this->db->prefix().
"product_lang";
1669 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($langtodelete).
"'";
1671 dol_syslog(get_class($this).
'::delMultiLangs', LOG_DEBUG);
1672 $result = $this->db->query(
$sql);
1675 $result = $this->
call_trigger(
'PRODUCT_DEL_MULTILANGS', $user);
1677 $this->error = $this->db->lasterror();
1678 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1684 $this->error = $this->db->lasterror();
1685 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1700 global $user, $langs, $conf;
1706 if ($type ==
'buy') {
1707 $field =
'accountancy_code_buy';
1708 } elseif ($type ==
'buy_intra') {
1709 $field =
'accountancy_code_buy_intra';
1710 } elseif ($type ==
'buy_export') {
1711 $field =
'accountancy_code_buy_export';
1712 } elseif ($type ==
'sell') {
1713 $field =
'accountancy_code_sell';
1714 } elseif ($type ==
'sell_intra') {
1715 $field =
'accountancy_code_sell_intra';
1716 } elseif ($type ==
'sell_export') {
1717 $field =
'accountancy_code_sell_export';
1722 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET ";
1723 $sql .=
"$field = '".$this->db->escape($value).
"'";
1724 $sql .=
" WHERE rowid = ".((int) $this->
id);
1727 $resql = $this->db->query(
$sql);
1731 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1738 $this->db->rollback();
1742 $this->$field = $value;
1744 $this->db->commit();
1747 $this->error = $this->db->lasterror();
1748 $this->db->rollback();
1762 $current_lang = $langs->getDefaultLang();
1764 $sql =
"SELECT lang, label, description, note as other";
1765 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1766 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1768 $result = $this->db->query(
$sql);
1770 while ($obj = $this->db->fetch_object($result)) {
1772 if ($obj->lang == $current_lang) {
1773 $this->label = $obj->label;
1775 $this->other = $obj->other;
1777 $this->multilangs[
"$obj->lang"][
"label"] = $obj->label;
1778 $this->multilangs[
"$obj->lang"][
"description"] = $obj->description;
1779 $this->multilangs[
"$obj->lang"][
"other"] = $obj->other;
1783 $this->error =
"Error: ".$this->db->lasterror().
" - ".
$sql;
1797 $testExit = array(
'multiprices',
'multiprices_ttc',
'multiprices_base_type',
'multiprices_min',
'multiprices_min_ttc',
'multiprices_tva_tx',
'multiprices_recuperableonly');
1799 foreach ($testExit as $field) {
1800 if (!isset($this->$field)) {
1803 $tmparray = $this->$field;
1804 if (!isset($tmparray[$level])) {
1810 'level' => $level ? $level : 1,
1811 'multiprices' => doubleval($this->multiprices[$level]),
1812 'multiprices_ttc' => doubleval($this->multiprices_ttc[$level]),
1813 'multiprices_base_type' => $this->multiprices_base_type[$level],
1814 'multiprices_min' => doubleval($this->multiprices_min[$level]),
1815 'multiprices_min_ttc' => doubleval($this->multiprices_min_ttc[$level]),
1816 'multiprices_tva_tx' => doubleval($this->multiprices_tva_tx[$level]),
1817 'multiprices_recuperableonly' => doubleval($this->multiprices_recuperableonly[$level]),
1840 if (empty($this->price_by_qty)) {
1841 $this->price_by_qty = 0;
1845 $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,";
1846 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
1847 $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).
",";
1848 $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');
1851 dol_syslog(get_class($this).
"::_log_price", LOG_DEBUG);
1852 $resql = $this->db->query(
$sql);
1854 $this->error = $this->db->lasterror();
1874 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price_by_qty";
1875 $sql .=
" WHERE fk_product_price = ".((int) $rowid);
1876 $resql = $this->db->query(
$sql);
1878 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price";
1879 $sql .=
" WHERE rowid=".((int) $rowid);
1880 $resql = $this->db->query(
$sql);
1884 $this->error = $this->db->lasterror();
1899 public function getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp = 0)
1901 global $conf, $db, $hookmanager, $action;
1904 if (is_object($hookmanager)) {
1905 $parameters = array(
'thirdparty_seller'=>$thirdparty_seller,
'thirdparty_buyer' => $thirdparty_buyer,
'pqp' => $pqp);
1907 $reshook = $hookmanager->executeHooks(
'getSellPrice', $parameters, $this, $action);
1909 return $hookmanager->resArray;
1920 $pu_ht = $this->price;
1921 $pu_ttc = $this->price_ttc;
1922 $price_min = $this->price_min;
1923 $price_base_type = $this->price_base_type;
1926 if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($thirdparty_buyer->price_level)) {
1927 $pu_ht = $this->multiprices[$thirdparty_buyer->price_level];
1928 $pu_ttc = $this->multiprices_ttc[$thirdparty_buyer->price_level];
1929 $price_min = $this->multiprices_min[$thirdparty_buyer->price_level];
1930 $price_base_type = $this->multiprices_base_type[$thirdparty_buyer->price_level];
1931 if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
1932 if (isset($this->multiprices_tva_tx[$thirdparty_buyer->price_level])) {
1933 $tva_tx = $this->multiprices_tva_tx[$thirdparty_buyer->price_level];
1935 if (isset($this->multiprices_recuperableonly[$thirdparty_buyer->price_level])) {
1936 $tva_npr = $this->multiprices_recuperableonly[$thirdparty_buyer->price_level];
1942 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1944 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
1948 $filter = array(
't.fk_product' => $this->
id,
't.fk_soc' => $thirdparty_buyer->id);
1950 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
1952 if (count($prodcustprice->lines) > 0) {
1953 $pu_ht =
price($prodcustprice->lines[0]->price);
1954 $price_min =
price($prodcustprice->lines[0]->price_min);
1955 $pu_ttc =
price($prodcustprice->lines[0]->price_ttc);
1956 $price_base_type = $prodcustprice->lines[0]->price_base_type;
1957 $tva_tx = $prodcustprice->lines[0]->tva_tx;
1958 if ($prodcustprice->lines[0]->default_vat_code && !preg_match(
'/\(.*\)/',
$tva_tx)) {
1959 $tva_tx .=
' ('.$prodcustprice->lines[0]->default_vat_code.
')';
1961 $tva_npr = $prodcustprice->lines[0]->recuperableonly;
1967 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
1969 if ($this->prices_by_qty[0]) {
1972 foreach ($this->prices_by_qty_list[0] as $priceforthequantityarray) {
1973 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1977 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1978 $pu_ht = $priceforthequantityarray[
'unitprice'];
1980 $pu_ttc = $priceforthequantityarray[
'unitprice'];
1985 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1987 if ($this->prices_by_qty[$thirdparty_buyer->price_level]) {
1990 foreach ($this->prices_by_qty_list[$thirdparty_buyer->price_level] as $priceforthequantityarray) {
1991 if ($priceforthequantityarray[
'rowid'] != $pqp) {
1995 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
1996 $pu_ht = $priceforthequantityarray[
'unitprice'];
1998 $pu_ttc = $priceforthequantityarray[
'unitprice'];
2005 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);
2022 public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref =
'', $fk_soc = 0)
2029 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2030 $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,";
2031 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2032 $sql .=
" pfp.packaging";
2033 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2034 $sql .=
" WHERE pfp.rowid = ".((int) $prodfournprice);
2036 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2038 $sql .=
" ORDER BY pfp.quantity DESC";
2040 dol_syslog(get_class($this).
"::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
2041 $resql = $this->db->query(
$sql);
2043 $obj = $this->db->fetch_object($resql);
2044 if ($obj && $obj->quantity > 0) {
2045 if (
isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2047 $prod_supplier->product_fourn_price_id = $obj->rowid;
2048 $prod_supplier->id = $obj->fk_product;
2049 $prod_supplier->fourn_qty = $obj->quantity;
2050 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2051 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2053 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2055 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2056 if ($price_result >= 0) {
2057 $obj->price = $price_result;
2060 $this->product_fourn_price_id = $obj->rowid;
2061 $this->buyprice = $obj->price;
2062 $this->fourn_pu = $obj->price / $obj->quantity;
2063 $this->fourn_price_base_type =
'HT';
2064 $this->fourn_socid = $obj->fk_soc;
2065 $this->ref_fourn = $obj->ref_supplier;
2066 $this->ref_supplier = $obj->ref_supplier;
2067 $this->desc_supplier = $obj->desc_supplier;
2068 $this->remise_percent = $obj->remise_percent;
2069 $this->vatrate_supplier = $obj->tva_tx;
2070 $this->default_vat_code_supplier = $obj->default_vat_code;
2071 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2072 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2073 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2074 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2075 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2076 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2077 $this->packaging = $obj->packaging;
2079 $result = $obj->fk_product;
2083 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2084 $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,";
2085 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2086 $sql .=
" pfp.packaging";
2087 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2088 $sql .=
" WHERE 1 = 1";
2089 if ($product_id > 0) {
2090 $sql .=
" AND pfp.fk_product = ".((int) $product_id);
2092 if ($fourn_ref !=
'none') {
2093 $sql .=
" AND pfp.ref_fourn = '".$this->db->escape($fourn_ref).
"'";
2096 $sql .=
" AND pfp.fk_soc = ".((int) $fk_soc);
2099 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2101 $sql .=
" ORDER BY pfp.quantity DESC";
2104 dol_syslog(get_class($this).
"::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
2105 $resql = $this->db->query(
$sql);
2107 $obj = $this->db->fetch_object($resql);
2108 if ($obj && $obj->quantity > 0) {
2109 if (
isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2111 $prod_supplier->product_fourn_price_id = $obj->rowid;
2112 $prod_supplier->id = $obj->fk_product;
2113 $prod_supplier->fourn_qty = $obj->quantity;
2114 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2115 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2117 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2119 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2121 $obj->price = $price_result;
2124 $this->product_fourn_price_id = $obj->rowid;
2125 $this->buyprice = $obj->price;
2126 $this->fourn_qty = $obj->quantity;
2127 $this->fourn_pu = $obj->price / $obj->quantity;
2128 $this->fourn_price_base_type =
'HT';
2129 $this->fourn_socid = $obj->fk_soc;
2130 $this->ref_fourn = $obj->ref_supplier;
2131 $this->ref_supplier = $obj->ref_supplier;
2132 $this->desc_supplier = $obj->desc_supplier;
2133 $this->remise_percent = $obj->remise_percent;
2134 $this->vatrate_supplier = $obj->tva_tx;
2135 $this->default_vat_code_supplier = $obj->default_vat_code;
2136 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2137 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2138 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2139 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2140 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2141 if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
2142 $this->packaging = $obj->packaging;
2144 $result = $obj->fk_product;
2150 $this->error = $this->db->lasterror();
2155 $this->error = $this->db->lasterror();
2177 public function updatePrice($newprice, $newpricebase, $user, $newvat =
'', $newminprice = 0, $level = 0, $newnpr = 0, $newpbq = 0, $ignore_autogen = 0, $localtaxes_array = array(), $newdefaultvatcode =
'')
2179 global $conf, $langs;
2185 dol_syslog(get_class($this).
"::update_price id=".$id.
" newprice=".$newprice.
" newpricebase=".$newpricebase.
" newminprice=".$newminprice.
" level=".$level.
" npr=".$newnpr.
" newdefaultvatcode=".$newdefaultvatcode);
2188 if (empty($this->tva_tx)) {
2191 if (empty($newnpr)) {
2194 if (empty($newminprice)) {
2197 if (empty($newminprice)) {
2202 if ($newvat ==
'') {
2208 if ((!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && !$ignore_autogen && $this->price_autogen && ($level == 1)) {
2209 return $this->
generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpbq);
2212 if (!empty($newminprice) && ($newminprice > $newprice)) {
2213 $this->error =
'ErrorPriceCantBeLowerThanMinPrice';
2217 if ($newprice !==
'' || $newprice === 0) {
2218 if ($newpricebase ==
'TTC') {
2219 $price_ttc =
price2num($newprice,
'MU');
2220 $price =
price2num($newprice) / (1 + ($newvat / 100));
2223 if ($newminprice !=
'' || $newminprice == 0) {
2224 $price_min_ttc =
price2num($newminprice,
'MU');
2225 $price_min =
price2num($newminprice) / (1 + ($newvat / 100));
2226 $price_min =
price2num($price_min,
'MU');
2233 $price_ttc = ($newnpr != 1) ? (
float)
price2num($newprice) * (1 + ($newvat / 100)) : $price;
2234 $price_ttc =
price2num($price_ttc,
'MU');
2236 if ($newminprice !==
'' || $newminprice === 0) {
2237 $price_min =
price2num($newminprice,
'MU');
2238 $price_min_ttc =
price2num($newminprice) * (1 + ($newvat / 100));
2239 $price_min_ttc =
price2num($price_min_ttc,
'MU');
2248 if (count($localtaxes_array) > 0) {
2249 $localtaxtype1 = $localtaxes_array[
'0'];
2250 $localtax1 = $localtaxes_array[
'1'];
2251 $localtaxtype2 = $localtaxes_array[
'2'];
2252 $localtax2 = $localtaxes_array[
'3'];
2255 if (!empty($newdefaultvatcode)) {
2258 $sql =
"SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
2259 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as t, ".MAIN_DB_PREFIX.
"c_country as c";
2260 $sql .=
" WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($mysoc->country_code).
"'";
2261 $sql .=
" AND t.taux = ".((float) $newdefaultvatcode).
" AND t.active = 1";
2262 $sql .=
" AND t.code = '".$this->db->escape($newdefaultvatcode).
"'";
2263 $resql = $this->db->query(
$sql);
2265 $obj = $this->db->fetch_object($resql);
2267 $npr = $obj->recuperableonly;
2268 $localtax1 = $obj->localtax1;
2269 $localtax2 = $obj->localtax2;
2270 $localtaxtype1 = $obj->localtax1_type;
2271 $localtaxtype2 = $obj->localtax2_type;
2276 $localtaxtype1 =
'0';
2278 $localtaxtype2 =
'0';
2282 if (empty($localtax1)) {
2285 if (empty($localtax2)) {
2293 $sql =
"UPDATE ".$this->db->prefix().
"product SET";
2294 $sql .=
" price_base_type='".$this->db->escape($newpricebase).
"',";
2295 $sql .=
" price=".$price.
",";
2296 $sql .=
" price_ttc=".$price_ttc.
",";
2297 $sql .=
" price_min=".$price_min.
",";
2298 $sql .=
" price_min_ttc=".$price_min_ttc.
",";
2299 $sql .=
" localtax1_tx=".($localtax1 >= 0 ? $localtax1 :
'NULL').
",";
2300 $sql .=
" localtax2_tx=".($localtax2 >= 0 ? $localtax2 :
'NULL').
",";
2301 $sql .=
" localtax1_type=".($localtaxtype1 !=
'' ?
"'".$this->db->escape($localtaxtype1).
"'" :
"'0'").
",";
2302 $sql .=
" localtax2_type=".($localtaxtype2 !=
'' ?
"'".$this->db->escape($localtaxtype2).
"'" :
"'0'").
",";
2303 $sql .=
" default_vat_code=".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
2304 $sql .=
" tva_tx='".price2num($newvat).
"',";
2305 $sql .=
" recuperableonly='".$this->db->escape($newnpr).
"'";
2306 $sql .=
" WHERE rowid = ".((int) $id);
2308 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
2309 $resql = $this->db->query(
$sql);
2311 $this->multiprices[$level] = $price;
2312 $this->multiprices_ttc[$level] = $price_ttc;
2313 $this->multiprices_min[$level] = $price_min;
2314 $this->multiprices_min_ttc[$level] = $price_min_ttc;
2315 $this->multiprices_base_type[$level] = $newpricebase;
2316 $this->multiprices_default_vat_code[$level] = $newdefaultvatcode;
2317 $this->multiprices_tva_tx[$level] = $newvat;
2318 $this->multiprices_recuperableonly[$level] = $newnpr;
2320 $this->
price = $price;
2321 $this->price_ttc = $price_ttc;
2322 $this->price_min = $price_min;
2323 $this->price_min_ttc = $price_min_ttc;
2324 $this->price_base_type = $newpricebase;
2325 $this->default_vat_code = $newdefaultvatcode;
2326 $this->tva_tx = $newvat;
2327 $this->tva_npr = $newnpr;
2329 $this->localtax1_tx = $localtax1;
2330 $this->localtax2_tx = $localtax2;
2331 $this->localtax1_type = $localtaxtype1;
2332 $this->localtax2_type = $localtaxtype2;
2335 $this->price_by_qty = $newpbq;
2339 if (!empty(array_diff_assoc($newPriceData, $lastPriceData)) || empty($conf->global->PRODUIT_MULTIPRICES)) {
2343 $this->level = $level;
2346 $result = $this->
call_trigger(
'PRODUCT_PRICE_MODIFY', $user);
2348 $this->db->rollback();
2353 $this->db->commit();
2355 $this->db->rollback();
2356 $this->error = $this->db->lasterror();
2375 $this->fk_price_expression = $expression_id;
2377 return $this->
update($this->
id, $user);
2392 public function fetch($id =
'', $ref =
'', $ref_ext =
'', $barcode =
'', $ignore_expression = 0, $ignore_price_load = 0, $ignore_lang_load = 0)
2394 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
2396 global $langs, $conf;
2398 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
2401 if (!$id && !$ref && !$ref_ext && !$barcode) {
2402 $this->error =
'ErrorWrongParameters';
2403 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
2407 $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,";
2408 $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,";
2409 $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,";
2410 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2411 $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,";
2412 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2413 $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,";
2415 $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,";
2420 $separatedEntityPMP =
false;
2421 $separatedStock =
false;
2422 $visibleWarehousesEntities = $conf->entity;
2423 if (!empty($conf->global->MULTICOMPANY_PRODUCT_SHARING_ENABLED)) {
2424 if (!empty($conf->global->MULTICOMPANY_PMP_PER_ENTITY_ENABLED)) {
2425 $checkPMPPerEntity = $this->db->query(
"SELECT pmp FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = ".((int) $id).
" AND entity = ".(
int) $conf->entity);
2426 if ($this->db->num_rows($checkPMPPerEntity)>0) {
2427 $separatedEntityPMP =
true;
2431 $separatedStock =
true;
2432 if (isset($mc->sharings[
'stock']) && !empty($mc->sharings[
'stock'])) {
2433 $visibleWarehousesEntities .=
"," . implode(
",", $mc->sharings[
'stock']);
2436 if ($separatedEntityPMP) {
2437 $sql .=
" ppe.pmp,";
2441 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2442 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf,";
2443 if ($separatedStock) {
2444 $sql .=
" SUM(sp.reel) as stock";
2448 $sql .=
" FROM ".$this->db->prefix().
"product as p";
2449 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) || $separatedEntityPMP) {
2450 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
2452 if ($separatedStock) {
2453 $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).
"))";
2457 $sql .=
" WHERE p.rowid = ".((int) $id);
2459 $sql .=
" WHERE p.entity IN (".getEntity($this->element).
")";
2461 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
2462 } elseif ($ref_ext) {
2463 $sql .=
" AND p.ref_ext = '".$this->db->escape($ref_ext).
"'";
2464 } elseif ($barcode) {
2465 $sql .=
" AND p.barcode = '".$this->db->escape($barcode).
"'";
2468 if ($separatedStock) {
2469 $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,";
2470 $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,";
2471 $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,";
2472 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2473 $sql .=
" p.surface, p.surface_units, p.volume, p.volume_units, p.barcode, p.fk_barcode_type, p.finished,";
2474 if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
2475 $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,";
2477 $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,";
2479 if ($separatedEntityPMP) {
2480 $sql .=
" ppe.pmp,";
2484 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2485 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf";
2486 if (!$separatedStock) {
2487 $sql .=
", p.stock";
2491 $resql = $this->db->query(
$sql);
2493 unset($this->oldcopy);
2495 if ($this->db->num_rows($resql) > 0) {
2496 $obj = $this->db->fetch_object($resql);
2498 $this->
id = $obj->rowid;
2499 $this->
ref = $obj->ref;
2500 $this->ref_ext = $obj->ref_ext;
2501 $this->label = $obj->label;
2503 $this->url = $obj->url;
2504 $this->note_public = $obj->note_public;
2505 $this->note_private = $obj->note_private;
2506 $this->note = $obj->note_private;
2508 $this->
type = $obj->fk_product_type;
2509 $this->status = $obj->tosell;
2510 $this->status_buy = $obj->tobuy;
2511 $this->status_batch = $obj->tobatch;
2512 $this->batch_mask = $obj->batch_mask;
2514 $this->customcode = $obj->customcode;
2515 $this->country_id = $obj->fk_country;
2516 $this->country_code =
getCountry($this->country_id, 2, $this->db);
2517 $this->state_id = $obj->fk_state;
2518 $this->lifetime = $obj->lifetime;
2519 $this->qc_frequency = $obj->qc_frequency;
2520 $this->
price = $obj->price;
2521 $this->price_ttc = $obj->price_ttc;
2522 $this->price_min = $obj->price_min;
2523 $this->price_min_ttc = $obj->price_min_ttc;
2524 $this->price_base_type = $obj->price_base_type;
2525 $this->cost_price = $obj->cost_price;
2526 $this->default_vat_code = $obj->default_vat_code;
2527 $this->tva_tx = $obj->tva_tx;
2529 $this->tva_npr = $obj->tva_npr;
2530 $this->recuperableonly = $obj->tva_npr;
2532 $this->localtax1_tx = $obj->localtax1_tx;
2533 $this->localtax2_tx = $obj->localtax2_tx;
2534 $this->localtax1_type = $obj->localtax1_type;
2535 $this->localtax2_type = $obj->localtax2_type;
2537 $this->finished = $obj->finished;
2538 $this->fk_default_bom = $obj->fk_default_bom;
2540 $this->duration = $obj->duration;
2541 $this->duration_value = $obj->duration ? substr($obj->duration, 0,
dol_strlen($obj->duration) - 1) :
null;
2542 $this->duration_unit = $obj->duration ? substr($obj->duration, -1) :
null;
2543 $this->canvas = $obj->canvas;
2544 $this->net_measure = $obj->net_measure;
2545 $this->net_measure_units = $obj->net_measure_units;
2546 $this->weight = $obj->weight;
2547 $this->weight_units = $obj->weight_units;
2548 $this->length = $obj->length;
2549 $this->length_units = $obj->length_units;
2550 $this->width = $obj->width;
2551 $this->width_units = $obj->width_units;
2552 $this->height = $obj->height;
2553 $this->height_units = $obj->height_units;
2555 $this->surface = $obj->surface;
2556 $this->surface_units = $obj->surface_units;
2557 $this->volume = $obj->volume;
2558 $this->volume_units = $obj->volume_units;
2559 $this->barcode = $obj->barcode;
2560 $this->barcode_type = $obj->fk_barcode_type;
2562 $this->accountancy_code_buy = $obj->accountancy_code_buy;
2563 $this->accountancy_code_buy_intra = $obj->accountancy_code_buy_intra;
2564 $this->accountancy_code_buy_export = $obj->accountancy_code_buy_export;
2565 $this->accountancy_code_sell = $obj->accountancy_code_sell;
2566 $this->accountancy_code_sell_intra = $obj->accountancy_code_sell_intra;
2567 $this->accountancy_code_sell_export = $obj->accountancy_code_sell_export;
2569 $this->fk_default_warehouse = $obj->fk_default_warehouse;
2570 $this->fk_default_workstation = $obj->fk_default_workstation;
2571 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
2572 $this->desiredstock = $obj->desiredstock;
2573 $this->stock_reel = $obj->stock;
2574 $this->pmp = $obj->pmp;
2576 $this->date_creation = $obj->datec;
2577 $this->date_modification = $obj->tms;
2578 $this->import_key = $obj->import_key;
2579 $this->entity = $obj->entity;
2581 $this->ref_ext = $obj->ref_ext;
2582 $this->fk_price_expression = $obj->fk_price_expression;
2583 $this->fk_unit = $obj->fk_unit;
2584 $this->price_autogen = $obj->price_autogen;
2585 $this->model_pdf = $obj->model_pdf;
2587 $this->mandatory_period = $obj->mandatory_period;
2589 $this->db->free($resql);
2600 if (!empty($conf->global->PRODUIT_MULTIPRICES) && empty($ignore_price_load)) {
2601 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2602 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2603 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2604 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2605 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2606 $sql .=
" AND price_level=".((int) $i);
2607 $sql .=
" AND fk_product = ".((int) $this->
id);
2608 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2610 $resql = $this->db->query(
$sql);
2612 $result = $this->db->fetch_array($resql);
2614 $this->multiprices[$i] = $result ? $result[
"price"] :
null;
2615 $this->multiprices_ttc[$i] = $result ? $result[
"price_ttc"] :
null;
2616 $this->multiprices_min[$i] = $result ? $result[
"price_min"] :
null;
2617 $this->multiprices_min_ttc[$i] = $result ? $result[
"price_min_ttc"] :
null;
2618 $this->multiprices_base_type[$i] = $result ? $result[
"price_base_type"] :
null;
2620 $this->multiprices_tva_tx[$i] = $result ? $result[
"tva_tx"].($result ?
' ('.$result[
'default_vat_code'].
')' :
'') :
null;
2621 $this->multiprices_recuperableonly[$i] = $result ? $result[
"recuperableonly"] :
null;
2659 $this->error = $this->db->lasterror;
2663 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES) && empty($ignore_price_load)) {
2665 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) && empty($ignore_price_load)) {
2666 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2667 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
2668 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2669 $sql .=
" WHERE fk_product = ".((int) $this->
id);
2670 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2672 $resql = $this->db->query(
$sql);
2674 $result = $this->db->fetch_array($resql);
2677 $this->prices_by_qty[0] = $result[
"price_by_qty"];
2678 $this->prices_by_qty_id[0] = $result[
"rowid"];
2680 if ($this->prices_by_qty[0] == 1) {
2681 $sql =
"SELECT rowid,price, unitprice, quantity, remise_percent, remise, remise, price_base_type";
2682 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2683 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[0]);
2684 $sql .=
" ORDER BY quantity ASC";
2685 $resultat = array();
2686 $resql = $this->db->query(
$sql);
2689 while ($result = $this->db->fetch_array($resql)) {
2690 $resultat[$ii] = array();
2691 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2692 $resultat[$ii][
"price"] = $result[
"price"];
2693 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2694 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2695 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2697 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2700 $this->prices_by_qty_list[0] = $resultat;
2702 $this->error = $this->db->lasterror;
2707 $this->error = $this->db->lasterror;
2710 } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES) && empty($ignore_price_load)) {
2711 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2712 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2713 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2714 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2715 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2716 $sql .=
" AND price_level=".((int) $i);
2717 $sql .=
" AND fk_product = ".((int) $this->
id);
2718 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2720 $resql = $this->db->query(
$sql);
2722 $result = $this->db->fetch_array($resql);
2724 $this->multiprices[$i] = $result[
"price"];
2725 $this->multiprices_ttc[$i] = $result[
"price_ttc"];
2726 $this->multiprices_min[$i] = $result[
"price_min"];
2727 $this->multiprices_min_ttc[$i] = $result[
"price_min_ttc"];
2728 $this->multiprices_base_type[$i] = $result[
"price_base_type"];
2730 $this->multiprices_tva_tx[$i] = $result[
"tva_tx"];
2731 $this->multiprices_recuperableonly[$i] = $result[
"recuperableonly"];
2734 $this->prices_by_qty[$i] = $result[
"price_by_qty"];
2735 $this->prices_by_qty_id[$i] = $result[
"rowid"];
2737 if ($this->prices_by_qty[$i] == 1) {
2738 $sql =
"SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type";
2739 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2740 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[$i]);
2741 $sql .=
" ORDER BY quantity ASC";
2742 $resultat = array();
2743 $resql = $this->db->query(
$sql);
2746 while ($result = $this->db->fetch_array($resql)) {
2747 $resultat[$ii] = array();
2748 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2749 $resultat[$ii][
"price"] = $result[
"price"];
2750 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2751 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2752 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2753 $resultat[$ii][
"remise"] = $result[
"remise"];
2754 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2757 $this->prices_by_qty_list[$i] = $resultat;
2759 $this->error = $this->db->lasterror;
2764 $this->error = $this->db->lasterror;
2770 if (
isModEnabled(
'dynamicprices') && !empty($this->fk_price_expression) && empty($ignore_expression)) {
2771 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2773 $price_result = $priceparser->parseProduct($this);
2774 if ($price_result >= 0) {
2775 $this->
price = $price_result;
2777 $this->price_ttc =
price2num($this->
price) * (1 + ($this->tva_tx / 100));
2778 $this->price_ttc =
price2num($this->price_ttc,
'MU');
2784 $this->stock_warehouse = array();
2791 $this->error = $this->db->lasterror();
2806 global $user, $hookmanager, $action;
2810 foreach (array(
'toconsume',
'consumed',
'toproduce',
'produced') as $role) {
2811 $this->stats_mo[
'customers_'.$role] = 0;
2812 $this->stats_mo[
'nb_'.$role] = 0;
2813 $this->stats_mo[
'qty_'.$role] = 0;
2815 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
2816 $sql .=
" SUM(mp.qty) as qty";
2817 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as c";
2818 $sql .=
" INNER JOIN ".$this->db->prefix().
"mrp_production as mp ON mp.fk_mo=c.rowid";
2819 if (empty($user->rights->societe->client->voir) && !$socid) {
2820 $sql .=
"INNER JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON sc.fk_soc=c.fk_soc AND sc.fk_user = ".((int) $user->id);
2823 $sql .=
" c.entity IN (".getEntity(
'mo').
")";
2825 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
2826 $sql .=
" AND mp.role ='".$this->db->escape($role).
"'";
2828 $sql .=
" AND c.fk_soc = ".((int) $socid);
2831 $result = $this->db->query(
$sql);
2833 $obj = $this->db->fetch_object($result);
2834 $this->stats_mo[
'customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
2835 $this->stats_mo[
'nb_'.$role] = $obj->nb ? $obj->nb : 0;
2836 $this->stats_mo[
'qty_'.$role] = $obj->qty ?
price2num($obj->qty,
'MS') : 0;
2838 $this->error = $this->db->error();
2843 if (!empty($error)) {
2847 $parameters = array(
'socid' => $socid);
2848 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2850 $this->stats_mo = $hookmanager->resArray[
'stats_mo'];
2866 global $user, $hookmanager, $action;
2870 $this->stats_bom[
'nb_toproduce'] = 0;
2871 $this->stats_bom[
'nb_toconsume'] = 0;
2872 $this->stats_bom[
'qty_toproduce'] = 0;
2873 $this->stats_bom[
'qty_toconsume'] = 0;
2875 $sql =
"SELECT COUNT(DISTINCT b.rowid) as nb_toproduce,";
2876 $sql .=
" SUM(b.qty) as qty_toproduce";
2877 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2878 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2880 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2881 $sql .=
" AND b.fk_product =".((int) $this->
id);
2882 $sql .=
" GROUP BY b.rowid";
2884 $result = $this->db->query(
$sql);
2886 $obj = $this->db->fetch_object($result);
2887 $this->stats_bom[
'nb_toproduce'] = !empty($obj->nb_toproduce) ? $obj->nb_toproduce : 0;
2888 $this->stats_bom[
'qty_toproduce'] = !empty($obj->qty_toproduce) ?
price2num($obj->qty_toproduce) : 0;
2890 $this->error = $this->db->error();
2894 $sql =
"SELECT COUNT(DISTINCT bl.rowid) as nb_toconsume,";
2895 $sql .=
" SUM(bl.qty) as qty_toconsume";
2896 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2897 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2899 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2900 $sql .=
" AND bl.fk_product =".((int) $this->
id);
2902 $result = $this->db->query(
$sql);
2904 $obj = $this->db->fetch_object($result);
2905 $this->stats_bom[
'nb_toconsume'] = !empty($obj->nb_toconsume) ? $obj->nb_toconsume : 0;
2906 $this->stats_bom[
'qty_toconsume'] = !empty($obj->qty_toconsume) ?
price2num($obj->qty_toconsume) : 0;
2908 $this->error = $this->db->error();
2912 if (!empty($error)) {
2916 $parameters = array(
'socid' => $socid);
2917 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2919 $this->stats_bom = $hookmanager->resArray[
'stats_bom'];
2935 global $conf, $user, $hookmanager, $action;
2937 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb,";
2938 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
2939 $sql .=
" FROM ".$this->db->prefix().
"propaldet as pd";
2940 $sql .=
", ".$this->db->prefix().
"propal as p";
2941 $sql .=
", ".$this->db->prefix().
"societe as s";
2942 if (empty($user->rights->societe->client->voir) && !$socid) {
2943 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
2945 $sql .=
" WHERE p.rowid = pd.fk_propal";
2946 $sql .=
" AND p.fk_soc = s.rowid";
2947 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
2948 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
2949 if (empty($user->rights->societe->client->voir) && !$socid) {
2950 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2954 $sql .=
" AND p.fk_soc = ".((int) $socid);
2957 $result = $this->db->query(
$sql);
2959 $obj = $this->db->fetch_object($result);
2960 $this->stats_propale[
'customers'] = $obj->nb_customers;
2961 $this->stats_propale[
'nb'] = $obj->nb;
2962 $this->stats_propale[
'rows'] = $obj->nb_rows;
2963 $this->stats_propale[
'qty'] = $obj->qty ? $obj->qty : 0;
2966 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
2968 if (is_array($TFather) && !empty($TFather)) {
2969 foreach ($TFather as &$fatherData) {
2970 $pFather =
new Product($this->db);
2971 $pFather->id = $fatherData[
'id'];
2972 $qtyCoef = $fatherData[
'qty'];
2974 if ($fatherData[
'incdec']) {
2975 $pFather->load_stats_propale($socid);
2977 $this->stats_propale[
'customers'] += $pFather->stats_propale[
'customers'];
2978 $this->stats_propale[
'nb'] += $pFather->stats_propale[
'nb'];
2979 $this->stats_propale[
'rows'] += $pFather->stats_propale[
'rows'];
2980 $this->stats_propale[
'qty'] += $pFather->stats_propale[
'qty'] * $qtyCoef;
2986 $parameters = array(
'socid' => $socid);
2987 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerProposal', $parameters, $this, $action);
2989 $this->stats_propale = $hookmanager->resArray[
'stats_propale'];
2994 $this->error = $this->db->error();
3010 global $conf, $user, $hookmanager, $action;
3012 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_suppliers, COUNT(DISTINCT p.rowid) as nb,";
3013 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
3014 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as pd";
3015 $sql .=
", ".$this->db->prefix().
"supplier_proposal as p";
3016 $sql .=
", ".$this->db->prefix().
"societe as s";
3017 if (empty($user->rights->societe->client->voir) && !$socid) {
3018 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3020 $sql .=
" WHERE p.rowid = pd.fk_supplier_proposal";
3021 $sql .=
" AND p.fk_soc = s.rowid";
3022 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
3023 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
3024 if (empty($user->rights->societe->client->voir) && !$socid) {
3025 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3029 $sql .=
" AND p.fk_soc = ".((int) $socid);
3032 $result = $this->db->query(
$sql);
3034 $obj = $this->db->fetch_object($result);
3035 $this->stats_proposal_supplier[
'suppliers'] = $obj->nb_suppliers;
3036 $this->stats_proposal_supplier[
'nb'] = $obj->nb;
3037 $this->stats_proposal_supplier[
'rows'] = $obj->nb_rows;
3038 $this->stats_proposal_supplier[
'qty'] = $obj->qty ? $obj->qty : 0;
3040 $parameters = array(
'socid' => $socid);
3041 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierProposal', $parameters, $this, $action);
3043 $this->stats_proposal_supplier = $hookmanager->resArray[
'stats_proposal_supplier'];
3048 $this->error = $this->db->error();
3066 global $conf, $user, $hookmanager, $action;
3068 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3069 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3070 $sql .=
" FROM ".$this->db->prefix().
"commandedet as cd";
3071 $sql .=
", ".$this->db->prefix().
"commande as c";
3072 $sql .=
", ".$this->db->prefix().
"societe as s";
3073 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3074 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3076 $sql .=
" WHERE c.rowid = cd.fk_commande";
3077 $sql .=
" AND c.fk_soc = s.rowid";
3078 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'commande').
")";
3079 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3080 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3081 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3084 $sql .=
" AND c.fk_soc = ".((int) $socid);
3086 if ($filtrestatut <>
'') {
3087 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3090 $result = $this->db->query(
$sql);
3092 $obj = $this->db->fetch_object($result);
3093 $this->stats_commande[
'customers'] = $obj->nb_customers;
3094 $this->stats_commande[
'nb'] = $obj->nb;
3095 $this->stats_commande[
'rows'] = $obj->nb_rows;
3096 $this->stats_commande[
'qty'] = $obj->qty ? $obj->qty : 0;
3099 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3101 if (is_array($TFather) && !empty($TFather)) {
3102 foreach ($TFather as &$fatherData) {
3103 $pFather =
new Product($this->db);
3104 $pFather->id = $fatherData[
'id'];
3105 $qtyCoef = $fatherData[
'qty'];
3107 if ($fatherData[
'incdec']) {
3108 $pFather->load_stats_commande($socid, $filtrestatut);
3110 $this->stats_commande[
'customers'] += $pFather->stats_commande[
'customers'];
3111 $this->stats_commande[
'nb'] += $pFather->stats_commande[
'nb'];
3112 $this->stats_commande[
'rows'] += $pFather->stats_commande[
'rows'];
3113 $this->stats_commande[
'qty'] += $pFather->stats_commande[
'qty'] * $qtyCoef;
3121 if ($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
3122 if (!empty($conf->global->DECREASE_ONLY_UNINVOICEDPRODUCTS)) {
3125 $sql =
"SELECT SUM(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".$this->db->prefix().
"facturedet as fd ";
3126 $sql .=
" JOIN ".$this->db->prefix().
"facture as f ON fd.fk_facture = f.rowid";
3127 $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'))";
3128 $sql .=
" JOIN ".$this->db->prefix().
"commande as c ON el.fk_source = c.rowid";
3129 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND c.facture = 0 AND fd.fk_product = ".((int) $this->
id);
3131 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3132 $resql = $this->db->query(
$sql);
3134 if ($this->db->num_rows($resql) > 0) {
3135 $obj = $this->db->fetch_object($resql);
3136 $adeduire += $obj->count;
3140 $this->stats_commande[
'qty'] -= $adeduire;
3143 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3147 $sql =
"SELECT sum(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".MAIN_DB_PREFIX.
"facturedet as fd ";
3148 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture as f ON fd.fk_facture = f.rowid";
3149 $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'))";
3150 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c ON el.fk_source = c.rowid";
3151 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND f.fk_statut > ".
Facture::STATUS_DRAFT.
" AND fd.fk_product = ".((int) $this->
id);
3153 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3154 $resql = $this->db->query(
$sql);
3156 if ($this->db->num_rows($resql) > 0) {
3157 $obj = $this->db->fetch_object($resql);
3158 $adeduire += $obj->count;
3161 $this->error = $this->db->error();
3165 $this->stats_commande[
'qty'] -= $adeduire;
3169 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3170 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerOrder', $parameters, $this, $action);
3172 $this->stats_commande = $hookmanager->resArray[
'stats_commande'];
3176 $this->error = $this->db->error();
3194 global $conf, $user, $hookmanager, $action;
3196 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb,";
3197 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3198 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as cd";
3199 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as c";
3200 $sql .=
", ".$this->db->prefix().
"societe as s";
3201 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3202 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3204 $sql .=
" WHERE c.rowid = cd.fk_commande";
3205 $sql .=
" AND c.fk_soc = s.rowid";
3206 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3207 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3208 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3209 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3212 $sql .=
" AND c.fk_soc = ".((int) $socid);
3214 if ($filtrestatut !=
'') {
3215 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3217 if (!empty($dateofvirtualstock)) {
3218 $sql .=
" AND c.date_livraison <= '".$this->db->idate($dateofvirtualstock).
"'";
3221 $result = $this->db->query(
$sql);
3223 $obj = $this->db->fetch_object($result);
3224 $this->stats_commande_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3225 $this->stats_commande_fournisseur[
'nb'] = $obj->nb;
3226 $this->stats_commande_fournisseur[
'rows'] = $obj->nb_rows;
3227 $this->stats_commande_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3229 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3230 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierOrder', $parameters, $this, $action);
3232 $this->stats_commande_fournisseur = $hookmanager->resArray[
'stats_commande_fournisseur'];
3237 $this->error = $this->db->error().
' sql='.
$sql;
3252 public function load_stats_sending($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $filterShipmentStatus =
'')
3255 global $conf, $user, $hookmanager, $action;
3257 $sql =
"SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb,";
3258 $sql .=
" COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
3259 $sql .=
" FROM ".$this->db->prefix().
"expeditiondet as ed";
3260 $sql .=
", ".$this->db->prefix().
"commandedet as cd";
3261 $sql .=
", ".$this->db->prefix().
"commande as c";
3262 $sql .=
", ".$this->db->prefix().
"expedition as e";
3263 $sql .=
", ".$this->db->prefix().
"societe as s";
3264 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3265 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3267 $sql .=
" WHERE e.rowid = ed.fk_expedition";
3268 $sql .=
" AND c.rowid = cd.fk_commande";
3269 $sql .=
" AND e.fk_soc = s.rowid";
3270 $sql .=
" AND e.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'expedition').
")";
3271 $sql .=
" AND ed.fk_origin_line = cd.rowid";
3272 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3273 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3274 $sql .=
" AND e.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3277 $sql .=
" AND e.fk_soc = ".((int) $socid);
3279 if ($filtrestatut <>
'') {
3280 $sql .=
" AND c.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3282 if (!empty($filterShipmentStatus)) {
3283 $sql .=
" AND e.fk_statut IN (".$this->db->sanitize($filterShipmentStatus).
")";
3286 $result = $this->db->query(
$sql);
3288 $obj = $this->db->fetch_object($result);
3289 $this->stats_expedition[
'customers'] = $obj->nb_customers;
3290 $this->stats_expedition[
'nb'] = $obj->nb;
3291 $this->stats_expedition[
'rows'] = $obj->nb_rows;
3292 $this->stats_expedition[
'qty'] = $obj->qty ? $obj->qty : 0;
3295 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3297 if (is_array($TFather) && !empty($TFather)) {
3298 foreach ($TFather as &$fatherData) {
3299 $pFather =
new Product($this->db);
3300 $pFather->id = $fatherData[
'id'];
3301 $qtyCoef = $fatherData[
'qty'];
3303 if ($fatherData[
'incdec']) {
3304 $pFather->load_stats_sending($socid, $filtrestatut, $forVirtualStock);
3306 $this->stats_expedition[
'customers'] += $pFather->stats_expedition[
'customers'];
3307 $this->stats_expedition[
'nb'] += $pFather->stats_expedition[
'nb'];
3308 $this->stats_expedition[
'rows'] += $pFather->stats_expedition[
'rows'];
3309 $this->stats_expedition[
'qty'] += $pFather->stats_expedition[
'qty'] * $qtyCoef;
3315 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock,
'filterShipmentStatus' => $filterShipmentStatus);
3316 $reshook = $hookmanager->executeHooks(
'loadStatsSending', $parameters, $this, $action);
3318 $this->stats_expedition = $hookmanager->resArray[
'stats_expedition'];
3323 $this->error = $this->db->error();
3338 public function load_stats_reception($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $dateofvirtualstock =
null)
3341 global $conf, $user, $hookmanager, $action;
3343 $sql =
"SELECT COUNT(DISTINCT cf.fk_soc) as nb_suppliers, COUNT(DISTINCT cf.rowid) as nb,";
3344 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3345 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur_dispatch as fd";
3346 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as cf";
3347 $sql .=
", ".$this->db->prefix().
"societe as s";
3348 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3349 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3351 $sql .=
" WHERE cf.rowid = fd.fk_commande";
3352 $sql .=
" AND cf.fk_soc = s.rowid";
3353 $sql .=
" AND cf.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'supplier_order').
")";
3354 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3355 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3356 $sql .=
" AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3359 $sql .=
" AND cf.fk_soc = ".((int) $socid);
3361 if ($filtrestatut <>
'') {
3362 $sql .=
" AND cf.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3364 if (!empty($dateofvirtualstock)) {
3365 $sql .=
" AND fd.datec <= '".$this->db->idate($dateofvirtualstock).
"'";
3368 $result = $this->db->query(
$sql);
3370 $obj = $this->db->fetch_object($result);
3371 $this->stats_reception[
'suppliers'] = $obj->nb_suppliers;
3372 $this->stats_reception[
'nb'] = $obj->nb;
3373 $this->stats_reception[
'rows'] = $obj->nb_rows;
3374 $this->stats_reception[
'qty'] = $obj->qty ? $obj->qty : 0;
3376 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3377 $reshook = $hookmanager->executeHooks(
'loadStatsReception', $parameters, $this, $action);
3379 $this->stats_reception = $hookmanager->resArray[
'stats_reception'];
3384 $this->error = $this->db->error();
3402 global $conf, $user, $hookmanager, $action;
3404 $sql =
"SELECT COUNT(DISTINCT m.fk_soc) as nb_customers, COUNT(DISTINCT m.rowid) as nb,";
3405 $sql .=
" COUNT(mp.rowid) as nb_rows, SUM(mp.qty) as qty, role";
3406 $sql .=
" FROM ".$this->db->prefix().
"mrp_production as mp";
3407 $sql .=
", ".$this->db->prefix().
"mrp_mo as m";
3408 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON s.rowid = m.fk_soc";
3409 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3410 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3412 $sql .=
" WHERE m.rowid = mp.fk_mo";
3413 $sql .=
" AND m.entity IN (".getEntity($forVirtualStock && !empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ?
'stock' :
'mrp').
")";
3414 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
3415 if (empty($user->rights->societe->client->voir) && !$socid && !$forVirtualStock) {
3416 $sql .=
" AND m.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3419 $sql .=
" AND m.fk_soc = ".((int) $socid);
3421 if ($filtrestatut <>
'') {
3422 $sql .=
" AND m.status IN (".$this->db->sanitize($filtrestatut).
")";
3424 if (!empty($dateofvirtualstock)) {
3425 $sql .=
" AND m.date_valid <= '".$this->db->idate($dateofvirtualstock).
"'";
3427 $sql .=
" GROUP BY role";
3429 $this->stats_mrptoconsume[
'customers'] = 0;
3430 $this->stats_mrptoconsume[
'nb'] = 0;
3431 $this->stats_mrptoconsume[
'rows'] = 0;
3432 $this->stats_mrptoconsume[
'qty'] = 0;
3433 $this->stats_mrptoproduce[
'customers'] = 0;
3434 $this->stats_mrptoproduce[
'nb'] = 0;
3435 $this->stats_mrptoproduce[
'rows'] = 0;
3436 $this->stats_mrptoproduce[
'qty'] = 0;
3438 $result = $this->db->query(
$sql);
3440 while ($obj = $this->db->fetch_object($result)) {
3441 if ($obj->role ==
'toconsume') {
3442 $this->stats_mrptoconsume[
'customers'] += $obj->nb_customers;
3443 $this->stats_mrptoconsume[
'nb'] += $obj->nb;
3444 $this->stats_mrptoconsume[
'rows'] += $obj->nb_rows;
3445 $this->stats_mrptoconsume[
'qty'] += ($obj->qty ? $obj->qty : 0);
3447 if ($obj->role ==
'consumed') {
3451 $this->stats_mrptoconsume[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3453 if ($obj->role ==
'toproduce') {
3454 $this->stats_mrptoproduce[
'customers'] += $obj->nb_customers;
3455 $this->stats_mrptoproduce[
'nb'] += $obj->nb;
3456 $this->stats_mrptoproduce[
'rows'] += $obj->nb_rows;
3457 $this->stats_mrptoproduce[
'qty'] += ($obj->qty ? $obj->qty : 0);
3459 if ($obj->role ==
'produced') {
3463 $this->stats_mrptoproduce[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3468 if ($this->stats_mrptoconsume[
'qty'] < 0) {
3469 $this->stats_mrptoconsume[
'qty'] = 0;
3471 if ($this->stats_mrptoproduce[
'qty'] < 0) {
3472 $this->stats_mrptoproduce[
'qty'] = 0;
3475 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3476 $reshook = $hookmanager->executeHooks(
'loadStatsInProduction', $parameters, $this, $action);
3478 $this->stats_mrptoproduce = $hookmanager->resArray[
'stats_mrptoproduce'];
3483 $this->error = $this->db->error();
3498 global $conf, $user, $hookmanager, $action;
3500 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3501 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3502 $sql .=
" FROM ".$this->db->prefix().
"contratdet as cd";
3503 $sql .=
", ".$this->db->prefix().
"contrat as c";
3504 $sql .=
", ".$this->db->prefix().
"societe as s";
3505 if (empty($user->rights->societe->client->voir) && !$socid) {
3506 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3508 $sql .=
" WHERE c.rowid = cd.fk_contrat";
3509 $sql .=
" AND c.fk_soc = s.rowid";
3510 $sql .=
" AND c.entity IN (".getEntity(
'contract').
")";
3511 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3512 if (empty($user->rights->societe->client->voir) && !$socid) {
3513 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3517 $sql .=
" AND c.fk_soc = ".((int) $socid);
3520 $result = $this->db->query(
$sql);
3522 $obj = $this->db->fetch_object($result);
3523 $this->stats_contrat[
'customers'] = $obj->nb_customers;
3524 $this->stats_contrat[
'nb'] = $obj->nb;
3525 $this->stats_contrat[
'rows'] = $obj->nb_rows;
3526 $this->stats_contrat[
'qty'] = $obj->qty ? $obj->qty : 0;
3529 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3531 if (is_array($TFather) && !empty($TFather)) {
3532 foreach ($TFather as &$fatherData) {
3533 $pFather =
new Product($this->db);
3534 $pFather->id = $fatherData[
'id'];
3535 $qtyCoef = $fatherData[
'qty'];
3537 if ($fatherData[
'incdec']) {
3538 $pFather->load_stats_contrat($socid);
3540 $this->stats_contrat[
'customers'] += $pFather->stats_contrat[
'customers'];
3541 $this->stats_contrat[
'nb'] += $pFather->stats_contrat[
'nb'];
3542 $this->stats_contrat[
'rows'] += $pFather->stats_contrat[
'rows'];
3543 $this->stats_contrat[
'qty'] += $pFather->stats_contrat[
'qty'] * $qtyCoef;
3549 $parameters = array(
'socid' => $socid);
3550 $reshook = $hookmanager->executeHooks(
'loadStatsContract', $parameters, $this, $action);
3552 $this->stats_contrat = $hookmanager->resArray[
'stats_contrat'];
3557 $this->error = $this->db->error().
' sql='.
$sql;
3572 global $db, $conf, $user, $hookmanager, $action;
3574 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3575 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(".$this->db->ifsql(
'f.type != 2',
'fd.qty',
'fd.qty * -1').
") as qty";
3576 $sql .=
" FROM ".$this->db->prefix().
"facturedet as fd";
3577 $sql .=
", ".$this->db->prefix().
"facture as f";
3578 $sql .=
", ".$this->db->prefix().
"societe as s";
3579 if (empty($user->rights->societe->client->voir) && !$socid) {
3580 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3582 $sql .=
" WHERE f.rowid = fd.fk_facture";
3583 $sql .=
" AND f.fk_soc = s.rowid";
3584 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3585 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3586 if (empty($user->rights->societe->client->voir) && !$socid) {
3587 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3591 $sql .=
" AND f.fk_soc = ".((int) $socid);
3594 $result = $this->db->query(
$sql);
3596 $obj = $this->db->fetch_object($result);
3597 $this->stats_facture[
'customers'] = $obj->nb_customers;
3598 $this->stats_facture[
'nb'] = $obj->nb;
3599 $this->stats_facture[
'rows'] = $obj->nb_rows;
3600 $this->stats_facture[
'qty'] = $obj->qty ? $obj->qty : 0;
3603 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3605 if (is_array($TFather) && !empty($TFather)) {
3606 foreach ($TFather as &$fatherData) {
3607 $pFather =
new Product($this->db);
3608 $pFather->id = $fatherData[
'id'];
3609 $qtyCoef = $fatherData[
'qty'];
3611 if ($fatherData[
'incdec']) {
3612 $pFather->load_stats_facture($socid);
3614 $this->stats_facture[
'customers'] += $pFather->stats_facture[
'customers'];
3615 $this->stats_facture[
'nb'] += $pFather->stats_facture[
'nb'];
3616 $this->stats_facture[
'rows'] += $pFather->stats_facture[
'rows'];
3617 $this->stats_facture[
'qty'] += $pFather->stats_facture[
'qty'] * $qtyCoef;
3623 $parameters = array(
'socid' => $socid);
3624 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoice', $parameters, $this, $action);
3626 $this->stats_facture = $hookmanager->resArray[
'stats_facture'];
3631 $this->error = $this->db->error();
3647 global $db, $conf, $user, $hookmanager;
3649 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3650 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3651 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet_rec as fd";
3652 $sql .=
", ".MAIN_DB_PREFIX.
"facture_rec as f";
3653 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
3654 if (empty($user->rights->societe->client->voir) && !$socid) {
3655 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
3657 $sql .=
" WHERE f.rowid = fd.fk_facture";
3658 $sql .=
" AND f.fk_soc = s.rowid";
3659 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3660 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3661 if (empty($user->rights->societe->client->voir) && !$socid) {
3662 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3666 $sql .=
" AND f.fk_soc = ".((int) $socid);
3669 $result = $this->db->query(
$sql);
3671 $obj = $this->db->fetch_object($result);
3672 $this->stats_facturerec[
'customers'] = $obj->nb_customers;
3673 $this->stats_facturerec[
'nb'] = $obj->nb;
3674 $this->stats_facturerec[
'rows'] = $obj->nb_rows;
3675 $this->stats_facturerec[
'qty'] = $obj->qty ? $obj->qty : 0;
3678 if (!empty($conf->global->PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC)) {
3680 if (is_array($TFather) && !empty($TFather)) {
3681 foreach ($TFather as &$fatherData) {
3682 $pFather =
new Product($this->db);
3683 $pFather->id = $fatherData[
'id'];
3684 $qtyCoef = $fatherData[
'qty'];
3686 if ($fatherData[
'incdec']) {
3687 $pFather->load_stats_facture($socid);
3689 $this->stats_facturerec[
'customers'] += $pFather->stats_facturerec[
'customers'];
3690 $this->stats_facturerec[
'nb'] += $pFather->stats_facturerec[
'nb'];
3691 $this->stats_facturerec[
'rows'] += $pFather->stats_facturerec[
'rows'];
3692 $this->stats_facturerec[
'qty'] += $pFather->stats_facturerec[
'qty'] * $qtyCoef;
3698 $parameters = array(
'socid' => $socid);
3699 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoiceRec', $parameters, $this, $action);
3701 $this->stats_facturerec = $hookmanager->resArray[
'stats_facturerec'];
3706 $this->error = $this->db->error();
3721 global $conf, $user, $hookmanager, $action;
3723 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,";
3724 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3725 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as fd";
3726 $sql .=
", ".$this->db->prefix().
"facture_fourn as f";
3727 $sql .=
", ".$this->db->prefix().
"societe as s";
3728 if (empty($user->rights->societe->client->voir) && !$socid) {
3729 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3731 $sql .=
" WHERE f.rowid = fd.fk_facture_fourn";
3732 $sql .=
" AND f.fk_soc = s.rowid";
3733 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3734 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3735 if (empty($user->rights->societe->client->voir) && !$socid) {
3736 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3740 $sql .=
" AND f.fk_soc = ".((int) $socid);
3743 $result = $this->db->query(
$sql);
3745 $obj = $this->db->fetch_object($result);
3746 $this->stats_facture_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3747 $this->stats_facture_fournisseur[
'nb'] = $obj->nb;
3748 $this->stats_facture_fournisseur[
'rows'] = $obj->nb_rows;
3749 $this->stats_facture_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3751 $parameters = array(
'socid' => $socid);
3752 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierInvoice', $parameters, $this, $action);
3754 $this->stats_facture_fournisseur = $hookmanager->resArray[
'stats_facture_fournisseur'];
3759 $this->error = $this->db->error();
3778 $resql = $this->db->query(
$sql);
3780 $num = $this->db->num_rows($resql);
3783 $arr = $this->db->fetch_array($resql);
3784 $keyfortab = (string) $arr[1];
3786 $keyfortab = substr($keyfortab, -2);
3789 if ($mode ==
'byunit') {
3790 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[0];
3791 } elseif ($mode ==
'bynumber') {
3792 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3793 } elseif ($mode ==
'byamount') {
3794 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3802 $this->error = $this->db->error().
' sql='.
$sql;
3807 $year = strftime(
'%Y', time());
3808 $month = strftime(
'%m', time());
3809 } elseif ($year == -1) {
3818 for ($j = 0; $j < 12; $j++) {
3820 $idx = ucfirst(
dol_trunc(
dol_print_date(
dol_mktime(12, 0, 0, $month, 1, 1970),
"%b"), 1,
'right',
'UTF-8', 1));
3823 $result[$j] = array($idx, isset($tab[$year.$month]) ? $tab[$year.$month] : 0);
3826 $month =
"0".($month - 1);
3828 $month = substr($month, 1);
3836 return array_reverse($result);
3851 public function get_nb_vente($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3857 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3858 if ($mode ==
'bynumber') {
3859 $sql .=
", count(DISTINCT f.rowid)";
3861 $sql .=
", sum(d.total_ht) as total_ht";
3862 $sql .=
" FROM ".$this->db->prefix().
"facturedet as d, ".$this->db->prefix().
"facture as f, ".$this->db->prefix().
"societe as s";
3863 if ($filteronproducttype >= 0) {
3864 $sql .=
", ".$this->db->prefix().
"product as p";
3866 if (empty($user->rights->societe->client->voir) && !$socid) {
3867 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3869 $sql .=
" WHERE f.rowid = d.fk_facture";
3870 if ($this->
id > 0) {
3871 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3873 $sql .=
" AND d.fk_product > 0";
3875 if ($filteronproducttype >= 0) {
3876 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3878 $sql .=
" AND f.fk_soc = s.rowid";
3879 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3880 if (empty($user->rights->societe->client->voir) && !$socid) {
3881 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3884 $sql .=
" AND f.fk_soc = $socid";
3886 $sql .= $morefilter;
3887 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3888 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3905 public function get_nb_achat($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3911 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3912 if ($mode ==
'bynumber') {
3913 $sql .=
", count(DISTINCT f.rowid)";
3915 $sql .=
", sum(d.total_ht) as total_ht";
3916 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as d, ".$this->db->prefix().
"facture_fourn as f, ".$this->db->prefix().
"societe as s";
3917 if ($filteronproducttype >= 0) {
3918 $sql .=
", ".$this->db->prefix().
"product as p";
3920 if (empty($user->rights->societe->client->voir) && !$socid) {
3921 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3923 $sql .=
" WHERE f.rowid = d.fk_facture_fourn";
3924 if ($this->
id > 0) {
3925 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3927 $sql .=
" AND d.fk_product > 0";
3929 if ($filteronproducttype >= 0) {
3930 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3932 $sql .=
" AND f.fk_soc = s.rowid";
3933 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3934 if (empty($user->rights->societe->client->voir) && !$socid) {
3935 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3938 $sql .=
" AND f.fk_soc = $socid";
3940 $sql .= $morefilter;
3941 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3942 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3958 public function get_nb_propal($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3961 global $conf, $user;
3963 $sql =
"SELECT sum(d.qty) as qty, date_format(p.datep, '%Y%m')";
3964 if ($mode ==
'bynumber') {
3965 $sql .=
", count(DISTINCT p.rowid)";
3967 $sql .=
", sum(d.total_ht) as total_ht";
3968 $sql .=
" FROM ".$this->db->prefix().
"propaldet as d, ".$this->db->prefix().
"propal as p, ".$this->db->prefix().
"societe as s";
3969 if ($filteronproducttype >= 0) {
3970 $sql .=
", ".$this->db->prefix().
"product as prod";
3972 if (empty($user->rights->societe->client->voir) && !$socid) {
3973 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3975 $sql .=
" WHERE p.rowid = d.fk_propal";
3976 if ($this->
id > 0) {
3977 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3979 $sql .=
" AND d.fk_product > 0";
3981 if ($filteronproducttype >= 0) {
3982 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
3984 $sql .=
" AND p.fk_soc = s.rowid";
3985 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
3986 if (empty($user->rights->societe->client->voir) && !$socid) {
3987 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3990 $sql .=
" AND p.fk_soc = ".((int) $socid);
3992 $sql .= $morefilter;
3993 $sql .=
" GROUP BY date_format(p.datep,'%Y%m')";
3994 $sql .=
" ORDER BY date_format(p.datep,'%Y%m') DESC";
4016 $sql =
"SELECT sum(d.qty) as qty, date_format(p.date_valid, '%Y%m')";
4017 if ($mode ==
'bynumber') {
4018 $sql .=
", count(DISTINCT p.rowid)";
4020 $sql .=
", sum(d.total_ht) as total_ht";
4021 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as d, ".$this->db->prefix().
"supplier_proposal as p, ".$this->db->prefix().
"societe as s";
4022 if ($filteronproducttype >= 0) {
4023 $sql .=
", ".$this->db->prefix().
"product as prod";
4025 if (empty($user->rights->societe->client->voir) && !$socid) {
4026 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4028 $sql .=
" WHERE p.rowid = d.fk_supplier_proposal";
4029 if ($this->
id > 0) {
4030 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4032 $sql .=
" AND d.fk_product > 0";
4034 if ($filteronproducttype >= 0) {
4035 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
4037 $sql .=
" AND p.fk_soc = s.rowid";
4038 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
4039 if (empty($user->rights->societe->client->voir) && !$socid) {
4040 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4043 $sql .=
" AND p.fk_soc = ".((int) $socid);
4045 $sql .= $morefilter;
4046 $sql .=
" GROUP BY date_format(p.date_valid,'%Y%m')";
4047 $sql .=
" ORDER BY date_format(p.date_valid,'%Y%m') DESC";
4063 public function get_nb_order($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4066 global $conf, $user;
4068 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4069 if ($mode ==
'bynumber') {
4070 $sql .=
", count(DISTINCT c.rowid)";
4072 $sql .=
", sum(d.total_ht) as total_ht";
4073 $sql .=
" FROM ".$this->db->prefix().
"commandedet as d, ".$this->db->prefix().
"commande as c, ".$this->db->prefix().
"societe as s";
4074 if ($filteronproducttype >= 0) {
4075 $sql .=
", ".$this->db->prefix().
"product as p";
4077 if (empty($user->rights->societe->client->voir) && !$socid) {
4078 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4080 $sql .=
" WHERE c.rowid = d.fk_commande";
4081 if ($this->
id > 0) {
4082 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4084 $sql .=
" AND d.fk_product > 0";
4086 if ($filteronproducttype >= 0) {
4087 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4089 $sql .=
" AND c.fk_soc = s.rowid";
4090 $sql .=
" AND c.entity IN (".getEntity(
'commande').
")";
4091 if (empty($user->rights->societe->client->voir) && !$socid) {
4092 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4095 $sql .=
" AND c.fk_soc = ".((int) $socid);
4097 $sql .= $morefilter;
4098 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4099 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4118 global $conf, $user;
4120 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4121 if ($mode ==
'bynumber') {
4122 $sql .=
", count(DISTINCT c.rowid)";
4124 $sql .=
", sum(d.total_ht) as total_ht";
4125 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as d, ".$this->db->prefix().
"commande_fournisseur as c, ".$this->db->prefix().
"societe as s";
4126 if ($filteronproducttype >= 0) {
4127 $sql .=
", ".$this->db->prefix().
"product as p";
4129 if (empty($user->rights->societe->client->voir) && !$socid) {
4130 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4132 $sql .=
" WHERE c.rowid = d.fk_commande";
4133 if ($this->
id > 0) {
4134 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4136 $sql .=
" AND d.fk_product > 0";
4138 if ($filteronproducttype >= 0) {
4139 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4141 $sql .=
" AND c.fk_soc = s.rowid";
4142 $sql .=
" AND c.entity IN (".getEntity(
'supplier_order').
")";
4143 if (empty($user->rights->societe->client->voir) && !$socid) {
4144 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4147 $sql .=
" AND c.fk_soc = ".((int) $socid);
4149 $sql .= $morefilter;
4150 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4151 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4167 public function get_nb_contract($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4170 global $conf, $user;
4172 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_contrat, '%Y%m')";
4173 if ($mode ==
'bynumber') {
4174 $sql .=
", count(DISTINCT c.rowid)";
4176 $sql .=
", sum(d.total_ht) as total_ht";
4177 $sql .=
" FROM ".$this->db->prefix().
"contratdet as d, ".$this->db->prefix().
"contrat as c, ".$this->db->prefix().
"societe as s";
4178 if ($filteronproducttype >= 0) {
4179 $sql .=
", ".$this->db->prefix().
"product as p";
4181 if (empty($user->rights->societe->client->voir) && !$socid) {
4182 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4185 $sql .=
" WHERE c.entity IN (".getEntity(
'contract').
")";
4186 $sql .=
" AND c.rowid = d.fk_contrat";
4188 if ($this->
id > 0) {
4189 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4191 $sql .=
" AND d.fk_product > 0";
4193 if ($filteronproducttype >= 0) {
4194 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4196 $sql .=
" AND c.fk_soc = s.rowid";
4198 if (empty($user->rights->societe->client->voir) && !$socid) {
4199 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4202 $sql .=
" AND c.fk_soc = ".((int) $socid);
4204 $sql .= $morefilter;
4205 $sql .=
" GROUP BY date_format(c.date_contrat,'%Y%m')";
4206 $sql .=
" ORDER BY date_format(c.date_contrat,'%Y%m') DESC";
4222 public function get_nb_mos($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4225 global $conf, $user;
4227 $sql =
"SELECT sum(d.qty), date_format(d.date_valid, '%Y%m')";
4228 if ($mode ==
'bynumber') {
4229 $sql .=
", count(DISTINCT d.rowid)";
4231 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as d LEFT JOIN ".$this->db->prefix().
"societe as s ON d.fk_soc = s.rowid";
4232 if ($filteronproducttype >= 0) {
4233 $sql .=
", ".$this->db->prefix().
"product as p";
4235 if (empty($user->rights->societe->client->voir) && !$socid) {
4236 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4239 $sql .=
" WHERE d.entity IN (".getEntity(
'mo').
")";
4240 $sql .=
" AND d.status > 0";
4242 if ($this->
id > 0) {
4243 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4245 $sql .=
" AND d.fk_product > 0";
4247 if ($filteronproducttype >= 0) {
4248 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4251 if (empty($user->rights->societe->client->voir) && !$socid) {
4252 $sql .=
" AND d.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4255 $sql .=
" AND d.fk_soc = ".((int) $socid);
4257 $sql .= $morefilter;
4258 $sql .=
" GROUP BY date_format(d.date_valid,'%Y%m')";
4259 $sql .=
" ORDER BY date_format(d.date_valid,'%Y%m') DESC";
4278 if (!is_numeric($id_pere)) {
4281 if (!is_numeric($id_fils)) {
4284 if (!is_numeric($incdec)) {
4294 $sql =
"SELECT fk_product_pere from ".$this->db->prefix().
"product_association";
4295 $sql .=
" WHERE fk_product_pere = ".((int) $id_fils).
" AND fk_product_fils = ".((int) $id_pere);
4296 if (!$this->db->query(
$sql)) {
4301 $sql =
"SELECT MAX(rang) as max_rank FROM ".$this->db->prefix().
"product_association";
4302 $sql .=
" WHERE fk_product_pere = ".((int) $id_pere);
4303 $resql = $this->db->query(
$sql);
4305 $obj = $this->db->fetch_object($resql);
4306 $rank = $obj->max_rank + 1;
4308 $sql =
"INSERT INTO ".$this->db->prefix().
"product_association(fk_product_pere,fk_product_fils,qty,incdec,rang)";
4309 $sql .=
" VALUES (".((int) $id_pere).
", ".((int) $id_fils).
", ".
price2num($qty,
'MS').
", ".
price2num($incdec,
'MS').
", ".((int) $rank).
")";
4310 if (! $this->db->query(
$sql)) {
4337 if (!is_numeric($id_pere)) {
4340 if (!is_numeric($id_fils)) {
4343 if (!is_numeric($incdec)) {
4346 if (!is_numeric($qty)) {
4350 $sql =
'UPDATE '.$this->db->prefix().
'product_association SET ';
4351 $sql .=
'qty = '.price2num($qty,
'MS');
4352 $sql .=
',incdec = '.price2num($incdec,
'MS');
4353 $sql .=
' WHERE fk_product_pere = '.((int) $id_pere).
' AND fk_product_fils = '.((int) $id_fils);
4355 if (!$this->db->query(
$sql)) {
4374 if (!is_numeric($fk_parent)) {
4377 if (!is_numeric($fk_child)) {
4381 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
4382 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4383 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4385 dol_syslog(get_class($this).
'::del_sousproduit', LOG_DEBUG);
4386 if (!$this->db->query(
$sql)) {
4392 $sqlrank =
"SELECT rowid, rang FROM ".$this->db->prefix().
"product_association";
4393 $sqlrank.=
" WHERE fk_product_pere = ".((int) $fk_parent);
4394 $sqlrank.=
" ORDER BY rang";
4395 $resqlrank = $this->db->query($sqlrank);
4398 while ($objrank = $this->db->fetch_object($resqlrank)) {
4400 $sql =
"UPDATE ".$this->db->prefix().
"product_association";
4401 $sql.=
" SET rang = ".((int) $cpt);
4402 $sql.=
" WHERE rowid = ".((int) $objrank->rowid);
4403 if (! $this->db->query(
$sql)) {
4423 $sql =
"SELECT fk_product_pere, qty, incdec";
4424 $sql .=
" FROM ".$this->db->prefix().
"product_association";
4425 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4426 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4428 $result = $this->db->query(
$sql);
4430 $num = $this->db->num_rows($result);
4433 $obj = $this->db->fetch_object($result);
4435 $this->is_sousproduit_qty = $obj->qty;
4436 $this->is_sousproduit_incdec = $obj->incdec;
4467 dol_syslog(get_class($this).
"::add_fournisseur id_fourn = ".$id_fourn.
" ref_fourn=".
$ref_fourn.
" quantity=".$quantity, LOG_DEBUG);
4473 $sql =
"SELECT rowid, fk_product";
4474 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4475 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4477 $sql .=
" AND fk_product <> ".((int) $this->
id);
4478 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4480 $resql = $this->db->query(
$sql);
4482 $obj = $this->db->fetch_object($resql);
4485 $this->product_id_already_linked = $obj->fk_product;
4488 $this->db->free($resql);
4492 $sql =
"SELECT rowid";
4493 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4494 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4498 $sql .=
" AND (ref_fourn = '' OR ref_fourn IS NULL)";
4500 $sql .=
" AND quantity = ".((float) $quantity);
4501 $sql .=
" AND fk_product = ".((int) $this->
id);
4502 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4504 $resql = $this->db->query(
$sql);
4506 $obj = $this->db->fetch_object($resql);
4510 $sql =
"INSERT INTO ".$this->db->prefix().
"product_fournisseur_price(";
4513 $sql .=
", fk_product";
4515 $sql .=
", ref_fourn";
4516 $sql .=
", quantity";
4517 $sql .=
", fk_user";
4519 $sql .=
") VALUES (";
4520 $sql .=
"'".$this->db->idate($now).
"'";
4521 $sql .=
", ".$conf->entity;
4522 $sql .=
", ".$this->id;
4523 $sql .=
", ".$id_fourn;
4525 $sql .=
", ".$quantity;
4526 $sql .=
", ".$user->id;
4530 if ($this->db->query(
$sql)) {
4531 $this->product_fourn_price_id = $this->db->last_insert_id($this->db->prefix().
"product_fournisseur_price");
4534 $this->error = $this->db->lasterror();
4539 $this->product_fourn_price_id = $obj->rowid;
4543 $this->error = $this->db->lasterror();
4562 $sql =
"SELECT DISTINCT p.fk_soc";
4563 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as p";
4564 $sql .=
" WHERE p.fk_product = ".((int) $this->
id);
4565 $sql .=
" AND p.entity = ".((int) $conf->entity);
4567 $result = $this->db->query(
$sql);
4569 $num = $this->db->num_rows($result);
4572 $obj = $this->db->fetch_object($result);
4573 $list[$i] = $obj->fk_soc;
4591 global $conf, $user;
4598 $sql =
"INSERT INTO ".$this->db->prefix().
"product_price (";
4600 $sql .=
", fk_product";
4601 $sql .=
", date_price";
4602 $sql .=
", price_level";
4604 $sql .=
", price_ttc";
4605 $sql .=
", price_min";
4606 $sql .=
", price_min_ttc";
4607 $sql .=
", price_base_type";
4608 $sql .=
", default_vat_code";
4610 $sql .=
", recuperableonly";
4611 $sql .=
", localtax1_tx";
4612 $sql .=
", localtax1_type";
4613 $sql .=
", localtax2_tx";
4614 $sql .=
", localtax2_type";
4615 $sql .=
", fk_user_author";
4617 $sql .=
", price_by_qty";
4618 $sql .=
", fk_price_expression";
4619 $sql .=
", fk_multicurrency";
4620 $sql .=
", multicurrency_code";
4621 $sql .=
", multicurrency_tx";
4622 $sql .=
", multicurrency_price";
4623 $sql .=
", multicurrency_price_ttc";
4628 $sql .=
", '".$this->db->idate($now).
"'";
4629 $sql .=
", price_level";
4631 $sql .=
", price_ttc";
4632 $sql .=
", price_min";
4633 $sql .=
", price_min_ttc";
4634 $sql .=
", price_base_type";
4635 $sql .=
", default_vat_code";
4637 $sql .=
", recuperableonly";
4638 $sql .=
", localtax1_tx";
4639 $sql .=
", localtax1_type";
4640 $sql .=
", localtax2_tx";
4641 $sql .=
", localtax2_type";
4642 $sql .=
", ".$user->id;
4644 $sql .=
", price_by_qty";
4645 $sql .=
", fk_price_expression";
4646 $sql .=
", fk_multicurrency";
4647 $sql .=
", multicurrency_code";
4648 $sql .=
", multicurrency_tx";
4649 $sql .=
", multicurrency_price";
4650 $sql .=
", multicurrency_price_ttc";
4651 $sql .=
" FROM ".$this->db->prefix().
"product_price";
4652 $sql .=
" WHERE fk_product = ".((int) $fromId);
4653 $sql .=
" ORDER BY date_price DESC";
4654 if ($conf->global->PRODUIT_MULTIPRICES_LIMIT > 0) {
4655 $sql .=
" LIMIT ".$conf->global->PRODUIT_MULTIPRICES_LIMIT;
4659 $resql = $this->db->query(
$sql);
4661 $this->db->rollback();
4665 $this->db->commit();
4682 $sql =
'INSERT INTO '.$this->db->prefix().
'product_association (fk_product_pere, fk_product_fils, qty)';
4683 $sql .=
" SELECT ".$toId.
", fk_product_fils, qty FROM ".$this->db->prefix().
"product_association";
4684 $sql .=
" WHERE fk_product_pere = ".((int) $fromId);
4686 dol_syslog(get_class($this).
'::clone_association', LOG_DEBUG);
4687 if (!$this->db->query(
$sql)) {
4688 $this->db->rollback();
4692 $this->db->commit();
4725 $sql =
"INSERT ".$this->db->prefix().
"product_fournisseur_price (";
4726 $sql .=
" datec, fk_product, fk_soc, price, quantity, fk_user)";
4727 $sql .=
" SELECT '".$this->db->idate($now).
"', ".((int) $toId).
", fk_soc, price, quantity, fk_user";
4728 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4729 $sql .=
" WHERE fk_product = ".((int) $fromId);
4731 dol_syslog(get_class($this).
'::clone_fournisseurs', LOG_DEBUG);
4732 $resql = $this->db->query(
$sql);
4734 $this->db->rollback();
4737 $this->db->commit();
4755 public function fetch_prod_arbo($prod, $compl_path =
'', $multiply = 1, $level = 1, $id_parent = 0, $ignore_stock_load = 0)
4758 global $conf, $langs;
4762 foreach ($prod as $id_product => $desc_pere) {
4763 if (is_array($desc_pere)) {
4764 $id = (!empty($desc_pere[0]) ? $desc_pere[0] :
'');
4765 $nb = (!empty($desc_pere[1]) ? $desc_pere[1] :
'');
4766 $type = (!empty($desc_pere[2]) ? $desc_pere[2] :
'');
4767 $label = (!empty($desc_pere[3]) ? $desc_pere[3] :
'');
4768 $incdec = (!empty($desc_pere[4]) ? $desc_pere[4] : 0);
4770 if ($multiply < 1) {
4775 if (is_null($tmpproduct)) {
4776 $tmpproduct =
new Product($this->db);
4778 $tmpproduct->fetch($id);
4780 if (empty($ignore_stock_load) && ($tmpproduct->isProduct() || !empty($conf->global->STOCK_SUPPORTS_SERVICES))) {
4781 $tmpproduct->load_stock(
'nobatch,novirtual');
4784 $this->res[] = array(
4786 'id_parent'=>$id_parent,
4787 'ref'=>$tmpproduct->ref,
4789 'nb_total'=>$nb * $multiply,
4790 'stock'=>$tmpproduct->stock_reel,
4791 'stock_alert'=>$tmpproduct->seuil_stock_alerte,
4793 'fullpath'=>$compl_path.$label,
4795 'desiredstock'=>$tmpproduct->desiredstock,
4798 'entity'=>$tmpproduct->entity
4802 if (isset($desc_pere[
'childs']) && is_array($desc_pere[
'childs'])) {
4804 $this->
fetch_prod_arbo($desc_pere[
'childs'], $compl_path.$desc_pere[3].
" -> ", $desc_pere[1] * $multiply, $level + 1, $id, $ignore_stock_load);
4822 $this->res = array();
4823 if (isset($this->sousprods) && is_array($this->sousprods)) {
4824 foreach ($this->sousprods as $prod_name => $desc_product) {
4825 if (is_array($desc_product)) {
4826 $this->
fetch_prod_arbo($desc_product,
"", $multiply, 1, $this->
id, $ignore_stock_load);
4845 $sql =
"SELECT COUNT(pa.rowid) as nb";
4846 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa";
4848 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id).
" OR pa.fk_product_pere = ".((int) $this->
id);
4849 } elseif ($mode == -1) {
4850 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id);
4851 } elseif ($mode == 1) {
4852 $sql .=
" WHERE pa.fk_product_pere = ".((int) $this->
id);
4855 $resql = $this->db->query(
$sql);
4857 $obj = $this->db->fetch_object($resql);
4876 $sql =
"SELECT count(rowid) as nb FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_parent = ".((int) $this->
id);
4877 $sql .=
" AND entity IN (".getEntity(
'product').
")";
4879 $resql = $this->db->query(
$sql);
4881 $obj = $this->db->fetch_object($resql);
4900 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_child = ".((int) $this->
id).
" AND entity IN (".
getEntity(
'product').
")";
4902 $query = $this->db->query(
$sql);
4905 if (!$this->db->num_rows($query)) {
4926 $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";
4927 $sql .=
", p.tosell as status, p.tobuy as status_buy";
4928 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa,";
4929 $sql .=
" ".$this->db->prefix().
"product as p";
4930 $sql .=
" WHERE p.rowid = pa.fk_product_pere";
4931 $sql .=
" AND pa.fk_product_fils = ".((int) $this->
id);
4936 while ($record = $this->db->fetch_array(
$res)) {
4938 $prods[$record[
'id']][
'id'] = $record[
'rowid'];
4939 $prods[$record[
'id']][
'ref'] = $record[
'ref'];
4940 $prods[$record[
'id']][
'label'] = $record[
'label'];
4941 $prods[$record[
'id']][
'qty'] = $record[
'qty'];
4942 $prods[$record[
'id']][
'incdec'] = $record[
'incdec'];
4943 $prods[$record[
'id']][
'fk_product_type'] = $record[
'fk_product_type'];
4944 $prods[$record[
'id']][
'entity'] = $record[
'entity'];
4945 $prods[$record[
'id']][
'status'] = $record[
'status'];
4946 $prods[$record[
'id']][
'status_buy'] = $record[
'status_buy'];
4965 public function getChildsArbo($id, $firstlevelonly = 0, $level = 1, $parents = array())
4967 global $alreadyfound;
4973 $sql =
"SELECT p.rowid, p.ref, p.label as label, p.fk_product_type,";
4974 $sql .=
" pa.qty as qty, pa.fk_product_fils as id, pa.incdec,";
4975 $sql .=
" pa.rowid as fk_association, pa.rang";
4976 $sql .=
" FROM ".$this->db->prefix().
"product as p,";
4977 $sql .=
" ".$this->db->prefix().
"product_association as pa";
4978 $sql .=
" WHERE p.rowid = pa.fk_product_fils";
4979 $sql .=
" AND pa.fk_product_pere = ".((int) $id);
4980 $sql .=
" AND pa.fk_product_fils <> ".((int) $id);
4981 $sql.=
" ORDER BY pa.rang";
4983 dol_syslog(get_class($this).
'::getChildsArbo id='.$id.
' level='.$level.
' parents='.(is_array($parents)?implode(
',', $parents):$parents), LOG_DEBUG);
4986 $alreadyfound = array($id=>1);
4996 while ($rec = $this->db->fetch_array(
$res)) {
4997 if (!empty($alreadyfound[$rec[
'rowid']])) {
4998 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);
4999 if (in_array($rec[
'id'], $parents)) {
5003 $alreadyfound[$rec[
'rowid']] = 1;
5004 $prods[$rec[
'rowid']] = array(
5007 2=>$rec[
'fk_product_type'],
5008 3=>$this->db->escape($rec[
'label']),
5011 6=>$rec[
'fk_association'],
5016 if (empty($firstlevelonly)) {
5017 $listofchilds = $this->
getChildsArbo($rec[
'rowid'], 0, $level + 1, array_push($parents, $rec[
'rowid']));
5018 foreach ($listofchilds as $keyChild => $valueChild) {
5019 $prods[$rec[
'rowid']][
'childs'][$keyChild] = $valueChild;
5043 foreach ($this->
getChildsArbo($this->
id) as $keyChild => $valueChild) {
5044 $parent[$this->label][$keyChild] = $valueChild;
5046 foreach ($parent as $key => $value) {
5047 $this->sousprods[$key] = $value;
5059 global $conf, $langs;
5061 $langs->load(
'products',
'other');
5064 $nofetch = !empty($params[
'nofetch']);
5066 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5067 return [
'optimize' => $langs->trans(
"ShowProduct")];
5070 if (!empty($this->entity)) {
5071 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80, 0, 0, 0, 0, 1);
5072 if ($this->nbphoto > 0) {
5073 $datas[
'photo'] =
'<div class="photointooltip floatright">'.
"\n" . $tmpphoto .
'</div>';
5078 $datas[
'picto'] =
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
5080 $datas[
'picto']=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
5082 if (isset($this->status) && isset($this->status_buy)) {
5083 $datas[
'status']=
' '.$this->getLibStatut(5, 0) .
' '.$this->getLibStatut(5, 1);
5086 if (!empty($this->
ref)) {
5087 $datas[
'ref']=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.$this->ref;
5089 if (!empty($this->label)) {
5090 $datas[
'label']=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
5094 $langs->load(
"productbatch");
5095 $datas[
'batchstatus']=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
5099 $datas[
'barcode']=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
5103 if ($this->weight) {
5104 $datas[
'weight']=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
5107 if ($this->length) {
5108 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
5111 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
5113 if ($this->height) {
5114 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
5117 $datas[
'size']=
"<br>".$labelsize;
5120 $labelsurfacevolume =
"";
5121 if ($this->surface) {
5122 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
5124 if ($this->volume) {
5125 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
5127 if ($labelsurfacevolume) {
5128 $datas[
'surface']=
"<br>" . $labelsurfacevolume;
5131 if (!empty($this->pmp) && $this->pmp) {
5132 $datas[
'pmp'] =
"<br><b>".$langs->trans(
"PMPValue").
'</b>: '.
price($this->pmp, 0,
'', 1, -1, -1, $conf->currency);
5136 if ($this->status && isset($this->accountancy_code_sell)) {
5137 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5138 $selllabel =
'<br>';
5139 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
5140 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
5141 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
5142 $datas[
'accountancysell'] = $selllabel;
5144 if ($this->status_buy && isset($this->accountancy_code_buy)) {
5145 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5147 if (empty($this->status)) {
5148 $buylabel .=
'<br>';
5150 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
5151 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
5152 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
5153 $datas[
'accountancybuy'] = $buylabel;
5158 require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
5160 $datas[
'categories'] =
'<br>' .
$form->showCategories($this->
id, Categorie::TYPE_PRODUCT, 1);
5179 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
5181 global $conf, $langs, $hookmanager;
5182 include_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
5186 $newref = $this->ref;
5188 $newref =
dol_trunc($newref, $maxlength,
'middle');
5192 'objecttype' => $this->element,
5193 'option' => $option,
5196 $classfortooltip =
'classfortooltip';
5199 $classfortooltip =
'classforajaxtooltip';
5200 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
5207 if (empty($notooltip)) {
5208 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
5209 $label = $langs->trans(
"ShowProduct");
5210 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1, 1).
'"';
5212 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1, 1).
'"' :
' title="tocomplete"');
5213 $linkclose .= $dataparams.
' class="nowraponall '.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
5215 $linkclose =
' class="nowraponall'.($morecss ?
' '.$morecss :
'').
'"';
5218 if ($option ==
'supplier' || $option ==
'category') {
5219 $url = DOL_URL_ROOT.
'/product/fournisseurs.php?id='.$this->id;
5220 } elseif ($option ==
'stock') {
5221 $url = DOL_URL_ROOT.
'/product/stock/product.php?id='.$this->id;
5222 } elseif ($option ==
'composition') {
5223 $url = DOL_URL_ROOT.
'/product/composition/card.php?id='.$this->id;
5225 $url = DOL_URL_ROOT.
'/product/card.php?id='.$this->id;
5228 if ($option !==
'nolink') {
5230 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
5231 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
5232 $add_save_lastsearch_values = 1;
5234 if ($add_save_lastsearch_values) {
5235 $url .=
'&save_lastsearch_values=1';
5239 $linkstart =
'<a href="'.$url.
'"';
5240 $linkstart .= $linkclose.
'>';
5243 $result .= $linkstart;
5246 $result .= (
img_object(($notooltip ?
'' : $label),
'product',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5249 $result .= (
img_object(($notooltip ?
'' : $label),
'service',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5253 $result .= $linkend;
5254 if ($withpicto != 2) {
5255 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
5259 $hookmanager->initHooks(array(
'productdao'));
5260 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'label' => &$label);
5261 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
5263 $result = $hookmanager->resPrint;
5265 $result .= $hookmanager->resPrint;
5282 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
5284 global $conf, $user, $langs;
5286 $langs->load(
"products");
5287 $outputlangs->load(
"products");
5294 $modelpath =
"core/modules/product/doc/";
5296 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
5310 return $this->
LibStatut($this->status, $mode, $type);
5312 return $this->
LibStatut($this->status_buy, $mode, $type);
5314 return $this->
LibStatut($this->status_batch, $mode, $type);
5317 return $this->
LibStatut($this->status_buy, $mode, $type);
5333 global $conf, $langs;
5335 $labelStatus = $labelStatusShort =
'';
5337 $langs->load(
'products');
5339 $langs->load(
"productbatch");
5345 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial')));
5348 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort')));
5353 return dolGetStatus($langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch'),
'',
'', empty($status) ?
'status5' :
'status4', 3,
'dot');
5359 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'));
5363 $statuttrans = empty($status) ?
'status5' :
'status4';
5368 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSellShort');
5369 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSell');
5370 } elseif ($type == 1) {
5371 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuyShort');
5372 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuy');
5373 } elseif ($type == 2) {
5374 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch');
5375 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort');
5377 } elseif ($status == 1) {
5380 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSellShort');
5381 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSell');
5382 } elseif ($type == 1) {
5383 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnBuyShort');
5384 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnBuy');
5385 } elseif ($type == 2) {
5386 $labelStatus = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial'));
5387 $labelStatusShort = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort'));
5389 } elseif ( $type == 2 && $status == 2 ) {
5390 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSerial');
5391 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort');
5395 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'),
'',
'',
'status0', 0);
5397 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statuttrans, $mode);
5410 $langs->load(
'products');
5412 if (isset($this->finished) && $this->finished >= 0) {
5413 $sql =
"SELECT label, code FROM ".$this->db->prefix().
"c_product_nature where code = ".((int) $this->finished).
" AND active=1";
5414 $resql = $this->db->query(
$sql);
5415 if ($resql && $this->db->num_rows($resql) > 0) {
5416 $res = $this->db->fetch_array($resql);
5417 $label = $langs->trans(
$res[
'label']);
5418 $this->db->free($resql);
5421 $this->error = $this->db->error().
' sql='.
$sql;
5422 dol_syslog(__METHOD__.
' Error '.$this->error, LOG_ERR);
5448 public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label =
'', $price = 0, $inventorycode =
'', $origin_element =
'', $origin_id =
null, $disablestockchangeforsubproduct = 0, $extrafields =
null)
5454 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5460 $nbpiece = abs($nbpiece);
5463 $op[0] =
"+".trim($nbpiece);
5464 $op[1] =
"-".trim($nbpiece);
5467 $movementstock->setOrigin($origin_element, $origin_id);
5468 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'',
'',
'',
'',
false, 0, $disablestockchangeforsubproduct);
5472 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5473 $movementstock->array_options = $array_options;
5474 $movementstock->insertExtraFields();
5476 $this->db->commit();
5479 $this->error = $movementstock->error;
5480 $this->errors = $movementstock->errors;
5482 $this->db->rollback();
5510 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)
5516 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5522 $nbpiece = abs($nbpiece);
5525 $op[0] =
"+".trim($nbpiece);
5526 $op[1] =
"-".trim($nbpiece);
5529 $movementstock->setOrigin($origin_element, $origin_id);
5530 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'', $dlc, $dluo, $lot,
false, 0, $disablestockchangeforsubproduct);
5534 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5535 $movementstock->array_options = $array_options;
5536 $movementstock->insertExtraFields();
5538 $this->db->commit();
5541 $this->error = $movementstock->error;
5542 $this->errors = $movementstock->errors;
5544 $this->db->rollback();
5564 public function load_stock($option =
'', $includedraftpoforvirtual =
null, $dateofvirtualstock =
null)
5569 $this->stock_reel = 0;
5570 $this->stock_warehouse = array();
5571 $this->stock_theorique = 0;
5574 $warehouseStatus = array();
5575 if (preg_match(
'/warehouseclosed/', $option)) {
5578 if (preg_match(
'/warehouseopen/', $option)) {
5581 if (preg_match(
'/warehouseinternal/', $option)) {
5582 if (!empty($conf->global->ENTREPOT_EXTRA_STATUS)) {
5589 $sql =
"SELECT ps.rowid, ps.reel, ps.fk_entrepot";
5590 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
5591 $sql .=
", ".$this->db->prefix().
"entrepot as w";
5592 $sql .=
" WHERE w.entity IN (".getEntity(
'stock').
")";
5593 $sql .=
" AND w.rowid = ps.fk_entrepot";
5594 $sql .=
" AND ps.fk_product = ".((int) $this->
id);
5595 if (count($warehouseStatus)) {
5596 $sql .=
" AND w.statut IN (".$this->db->sanitize(implode(
',', $warehouseStatus)).
")";
5599 $sql .=
" ORDER BY ps.reel ".(!empty($conf->global->DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE)?
'DESC':
'ASC');
5601 dol_syslog(get_class($this).
"::load_stock", LOG_DEBUG);
5602 $result = $this->db->query(
$sql);
5604 $num = $this->db->num_rows($result);
5608 $row = $this->db->fetch_object($result);
5609 $this->stock_warehouse[$row->fk_entrepot] =
new stdClass();
5610 $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
5611 $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
5612 if ((!preg_match(
'/nobatch/', $option)) && $this->
hasbatch()) {
5613 $this->stock_warehouse[$row->fk_entrepot]->detail_batch =
Productbatch::findAll($this->db, $row->rowid, 1, $this->id);
5615 $this->stock_reel += $row->reel;
5619 $this->db->free($result);
5621 if (!preg_match(
'/novirtual/', $option)) {
5627 $this->error = $this->db->lasterror();
5646 global $conf, $hookmanager, $action;
5648 $stock_commande_client = 0;
5649 $stock_commande_fournisseur = 0;
5650 $stock_sending_client = 0;
5651 $stock_reception_fournisseur = 0;
5652 $stock_inproduction = 0;
5661 $stock_commande_client = $this->stats_commande[
'qty'];
5664 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
5665 $filterShipmentStatus =
'';
5666 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)) {
5668 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5675 $stock_sending_client = $this->stats_expedition[
'qty'];
5678 $filterStatus = empty($conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK) ?
'3,4' : $conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK;
5679 if (isset($includedraftpoforvirtual)) {
5680 $filterStatus =
'0,1,2,'.$filterStatus;
5686 $stock_commande_fournisseur = $this->stats_commande_fournisseur[
'qty'];
5690 $filterStatus =
'4';
5691 if (isset($includedraftpoforvirtual)) {
5692 $filterStatus =
'0,'.$filterStatus;
5698 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5702 $filterStatus =
'4';
5703 if (isset($includedraftpoforvirtual)) {
5704 $filterStatus =
'0,'.$filterStatus;
5710 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5717 $stock_inproduction = $this->stats_mrptoproduce[
'qty'] - $this->stats_mrptoconsume[
'qty'];
5720 $this->stock_theorique = $this->stock_reel + $stock_inproduction;
5723 if (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE)) {
5724 $this->stock_theorique -= ($stock_commande_client - $stock_sending_client);
5725 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER)) {
5726 $this->stock_theorique += 0;
5727 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
5728 $this->stock_theorique -= $stock_commande_client;
5731 if (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE)) {
5732 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5733 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)) {
5734 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5735 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)) {
5736 $this->stock_theorique -= $stock_reception_fournisseur;
5737 } elseif (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
5738 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5741 $hookmanager->initHooks(array(
'productdao'));
5742 $parameters = array(
'id'=>$this->
id,
'includedraftpoforvirtual' => $includedraftpoforvirtual);
5744 $reshook = $hookmanager->executeHooks(
'loadvirtualstock', $parameters, $this, $action);
5746 $this->stock_theorique = $hookmanager->resArray[
'stock_theorique'];
5764 $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";
5765 $sql .=
" WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = ".((int) $this->
id).
" AND pb.batch = '".$this->db->escape($batch).
"'";
5766 $sql .=
" GROUP BY pb.batch, pb.eatby, pb.sellby";
5767 dol_syslog(get_class($this).
"::loadBatchInfo load first entry found for lot/serial = ".$batch, LOG_DEBUG);
5768 $resql = $this->db->query(
$sql);
5770 $num = $this->db->num_rows($resql);
5773 $obj = $this->db->fetch_object($resql);
5774 $result[] = array(
'batch'=>$batch,
'eatby'=>$this->db->jdate($obj->eatby),
'sellby'=>$this->db->jdate($obj->sellby),
'qty'=>$obj->qty);
5780 $this->db->rollback();
5798 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5804 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos";
5806 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product').dol_sanitizeFileName($this->
ref);
5811 $dir_osencoded = $dir;
5813 if (is_dir($dir_osencoded)) {
5814 $originImage = $dir.
'/'.$file[
'name'];
5825 if (is_numeric($result) && $result > 0) {
5842 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5843 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5849 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos/";
5851 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product');
5857 if (file_exists($dir_osencoded)) {
5858 $handle = opendir($dir_osencoded);
5859 if (is_resource($handle)) {
5860 while (($file = readdir($handle)) !==
false) {
5862 $file = utf8_encode($file);
5884 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5885 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5891 $handle = @opendir($dir_osencoded);
5892 if (is_resource($handle)) {
5893 while (($file = readdir($handle)) !==
false) {
5895 $file = utf8_encode($file);
5902 $photo_vignette =
'';
5904 if (preg_match(
'/('.$this->regeximgext.
')$/i', $photo, $regs)) {
5905 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $photo).
'_small'.$regs[0];
5908 $dirthumb = $dir.
'thumbs/';
5912 $obj[
'photo'] = $photo;
5913 if ($photo_vignette &&
dol_is_file($dirthumb.$photo_vignette)) {
5914 $obj[
'photo_vignette'] =
'thumbs/'.$photo_vignette;
5916 $obj[
'photo_vignette'] =
"";
5919 $tabobj[$nbphoto - 1] = $obj;
5922 if ($nbmax && $nbphoto >= $nbmax) {
5944 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5945 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5947 $dir = dirname($file).
'/';
5948 $dirthumb = $dir.
'/thumbs/';
5949 $filename = preg_replace(
'/'.preg_quote($dir,
'/').
'/i',
'', $file);
5955 if (preg_match(
'/('.$this->regeximgext.
')$/i', $filename, $regs)) {
5956 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_small'.$regs[0];
5957 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5961 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_mini'.$regs[0];
5962 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
5979 $infoImg = getimagesize($file_osencoded);
5980 $this->imgWidth = $infoImg[0];
5981 $this->imgHeight = $infoImg[1];
5993 global $hookmanager;
5995 $this->nb = array();
5997 $sql =
"SELECT count(p.rowid) as nb, fk_product_type";
5998 $sql .=
" FROM ".$this->db->prefix().
"product as p";
5999 $sql .=
' WHERE p.entity IN ('.getEntity($this->element, 1).
')';
6001 if (is_object($hookmanager)) {
6002 $parameters = array();
6003 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
6004 $sql .= $hookmanager->resPrint;
6006 $sql .=
' GROUP BY fk_product_type';
6008 $resql = $this->db->query(
$sql);
6010 while ($obj = $this->db->fetch_object($resql)) {
6011 if ($obj->fk_product_type == 1) {
6012 $this->nb[
"services"] = $obj->nb;
6014 $this->nb[
"products"] = $obj->nb;
6017 $this->db->free($resql);
6021 $this->error = $this->db->error();
6054 return ($this->mandatory_period == 1 ?
true :
false);
6071 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
6072 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
6073 foreach ($dirsociete as $dirroot) {
6079 $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
6082 $result = $mod->getNextValue($object, $type);
6084 dol_syslog(get_class($this).
"::get_barcode barcode=".$result.
" module=".$var);
6098 global $user, $langs, $conf, $mysoc;
6103 $this->specimen = 1;
6105 $this->
ref =
'PRODUCT_SPEC';
6106 $this->label =
'PRODUCT SPECIMEN';
6107 $this->
description =
'This is description of this product specimen that was created the '.dol_print_date($now,
'dayhourlog').
'.';
6108 $this->specimen = 1;
6109 $this->country_id = 1;
6111 $this->status_buy = 1;
6113 $this->note_private =
'This is a comment (private)';
6114 $this->note_public =
'This is a comment (public)';
6115 $this->date_creation = $now;
6116 $this->date_modification = $now;
6119 $this->weight_units = 3;
6122 $this->length_units = 1;
6124 $this->width_units = 0;
6125 $this->height =
null;
6126 $this->height_units =
null;
6128 $this->surface = 30;
6129 $this->surface_units = 0;
6130 $this->volume = 300;
6131 $this->volume_units = 0;
6133 $this->barcode = -1;
6146 if (!$this->fk_unit) {
6150 $langs->load(
'products');
6152 $label_type =
'label';
6153 if ($type ==
'short') {
6154 $label_type =
'short_label';
6157 $sql =
"SELECT ".$label_type.
", code from ".$this->db->prefix().
"c_units where rowid = ".((int) $this->fk_unit);
6159 $resql = $this->db->query(
$sql);
6160 if ($resql && $this->db->num_rows($resql) > 0) {
6161 $res = $this->db->fetch_array($resql);
6162 $label = ($label_type ==
'short_label' ?
$res[$label_type] :
'unit'.$res[
'code']);
6163 $this->db->free($resql);
6166 $this->error = $this->db->error();
6167 dol_syslog(get_class($this).
"::getLabelOfUnit Error ".$this->error, LOG_ERR);
6179 return ($this->status_batch > 0 ?
true :
false);
6194 $maxpricesupplier = 0;
6196 if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE)) {
6197 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
6199 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->
id,
'',
'');
6201 if (is_array($product_fourn_list) && count($product_fourn_list) > 0) {
6202 foreach ($product_fourn_list as $productfourn) {
6203 if ($productfourn->fourn_unitprice > $maxpricesupplier) {
6204 $maxpricesupplier = $productfourn->fourn_unitprice;
6208 $maxpricesupplier *= $conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE;
6212 return $maxpricesupplier;
6228 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
6229 return parent::setCategoriesCommon($categories, Categorie::TYPE_PRODUCT);
6243 'product_customer_price',
6244 'product_customer_price_log'
6265 $sql =
"SELECT rowid, level, fk_level, var_percent, var_min_percent FROM ".$this->db->prefix().
"product_pricerules";
6266 $query = $this->db->query(
$sql);
6270 while ($result = $this->db->fetch_object($query)) {
6271 $rules[$result->level] = $result;
6279 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
6280 $price = $baseprice;
6281 $price_min = $baseprice;
6285 if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
6286 $price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent / 100));
6289 $prices[$i] = $price;
6292 if (isset($rules[$i]->var_min_percent) && $rules[$i]->var_min_percent) {
6293 $price_min = $price * (1 - ($rules[$i]->var_min_percent / 100));
6297 $check_amount = (($price == $this->multiprices[$i]) && ($price_min == $this->multiprices_min[$i]));
6298 $check_type = ($baseprice == $this->multiprices_base_type[$i]);
6300 if ($check_amount && $check_type) {
6304 if ($this->
updatePrice($price, $price_type, $user, $price_vat, $price_min, $i, $npr, $psq,
true) < 0) {
6322 return $user->rights->produit;
6324 return $user->rights->service;
6336 $sql =
"SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification,";
6337 $sql .=
" p.fk_user_author, p.fk_user_modif";
6338 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as p";
6339 $sql .=
" WHERE p.rowid = ".((int) $id);
6341 $result = $this->db->query(
$sql);
6343 if ($this->db->num_rows($result)) {
6344 $obj = $this->db->fetch_object($result);
6346 $this->
id = $obj->rowid;
6348 if ($obj->fk_user_author) {
6349 $cuser =
new User($this->db);
6350 $cuser->fetch($obj->fk_user_author);
6351 $this->user_creation = $cuser;
6354 if ($obj->fk_user_modif) {
6355 $muser =
new User($this->db);
6356 $muser->fetch($obj->fk_user_modif);
6357 $this->user_modification = $muser;
6360 $this->
ref = $obj->ref;
6361 $this->date_creation = $this->db->jdate($obj->date_creation);
6362 $this->date_modification = $this->db->jdate($obj->date_modification);
6365 $this->db->free($result);
6380 if (empty($this->duration_value)) {
6381 $this->errors[]=
'ErrorDurationForServiceNotDefinedCantCalculateHourlyPrice';
6385 if ($this->duration_unit ==
'i') {
6386 $prodDurationHours = 1. / 60;
6388 if ($this->duration_unit ==
'h') {
6389 $prodDurationHours = 1.;
6391 if ($this->duration_unit ==
'd') {
6392 $prodDurationHours = 24.;
6394 if ($this->duration_unit ==
'w') {
6395 $prodDurationHours = 24. * 7;
6397 if ($this->duration_unit ==
'm') {
6398 $prodDurationHours = 24. * 30;
6400 if ($this->duration_unit ==
'y') {
6401 $prodDurationHours = 24. * 365;
6405 return $prodDurationHours;
6418 global $langs,$conf;
6420 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
6422 $return =
'<div class="box-flex-item box-flex-grow-zero">';
6423 $return .=
'<div class="info-box info-box-sm">';
6424 $return .=
'<div class="info-box-img">';
6427 $label .= $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 120, 160, 0, 0, 0,
'',
'photoref photokanban');
6437 $return .=
'</div>';
6438 $return .=
'<div class="info-box-content">';
6439 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
6440 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
6441 if (property_exists($this,
'label')) {
6442 $return .=
'<br><span class="info-box-label opacitymedium">'.$this->label.
'</span>';
6444 if (property_exists($this,
'price') && property_exists($this,
'price_ttc')) {
6445 if ($this->price_base_type ==
'TTC') {
6446 $return .=
'<br><span class="info-box-status amount">'.price($this->price_ttc).
' '.$langs->trans(
"TTC").
'</span>';
6448 if ($this->status) {
6449 $return .=
'<br><span class="info-box-status amount">'.price($this->
price).
' '.$langs->trans(
"HT").
'</span>';
6453 if (property_exists($this,
'stock_reel')) {
6454 $return .=
'<br><span class="info-box-status opacitymedium">'.$langs->trans(
'PhysicalStock').
' : <span class="bold">'.$this->stock_reel.
'</span></span>';
6456 if (method_exists($this,
'getLibStatut')) {
6457 $return .=
'<br><span class="info-box-status margintoponly">'.$this->getLibStatut(3, 1).
' '.$this->
getLibStatut(3, 0).
'</span>';
6459 $return .=
'</div>';
6460 $return .=
'</div>';
6461 $return .=
'</div>';
6472 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.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
print *****$script_file(".$version.") pid 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).
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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)
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.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.
div float
Buy price without taxes.
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