39require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
41require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
42require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
53 public $element =
'product';
58 public $table_element =
'product';
63 public $fk_element =
'fk_product';
68 protected $childtables = array(
69 'supplier_proposaldet' => array(
'name' =>
'SupplierProposal',
'parent' =>
'supplier_proposal',
'parentkey' =>
'fk_supplier_proposal'),
70 'propaldet' => array(
'name' =>
'Proposal',
'parent' =>
'propal',
'parentkey' =>
'fk_propal'),
71 'commandedet' => array(
'name' =>
'Order',
'parent' =>
'commande',
'parentkey' =>
'fk_commande'),
72 'facturedet' => array(
'name' =>
'Invoice',
'parent' =>
'facture',
'parentkey' =>
'fk_facture'),
73 'contratdet' => array(
'name' =>
'Contract',
'parent' =>
'contrat',
'parentkey' =>
'fk_contrat'),
74 'facture_fourn_det' => array(
'name' =>
'SupplierInvoice',
'parent' =>
'facture_fourn',
'parentkey' =>
'fk_facture_fourn'),
75 'commande_fournisseurdet' => array(
'name' =>
'SupplierOrder',
'parent' =>
'commande_fournisseur',
'parentkey' =>
'fk_commande'),
76 'mrp_production' => array(
'name' =>
'Mo',
'parent' =>
'mrp_mo',
'parentkey' =>
'fk_mo' ),
77 'bom_bom' => array(
'name' =>
'BOM'),
78 'bom_bomline' => array(
'name' =>
'BOMLine',
'parent' =>
'bom_bom',
'parentkey' =>
'fk_bom'),
86 public $ismultientitymanaged = 1;
91 public $isextrafieldmanaged = 1;
96 public $picto =
'product';
103 public $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
146 public $price_formated;
155 public $price_ttc_formated;
169 public $price_min_ttc;
175 public $price_base_type;
179 public $multiprices_ttc = array();
180 public $multiprices_base_type = array();
181 public $multiprices_default_vat_code = array();
182 public $multiprices_min = array();
183 public $multiprices_min_ttc = array();
184 public $multiprices_tva_tx = array();
185 public $multiprices_recuperableonly = array();
189 public $prices_by_qty = array();
190 public $prices_by_qty_id = array();
191 public $prices_by_qty_list = array();
217 public $localtax2_tx;
218 public $localtax1_type;
219 public $localtax2_type;
223 public $desc_supplier;
224 public $vatrate_supplier;
225 public $default_vat_code_supplier;
226 public $fourn_multicurrency_price;
227 public $fourn_multicurrency_unitprice;
228 public $fourn_multicurrency_tx;
229 public $fourn_multicurrency_id;
230 public $fourn_multicurrency_code;
236 public $qc_frequency;
243 public $stock_reel = 0;
250 public $stock_theorique;
267 public $seuil_stock_alerte = 0;
312 public $status_buy = 0;
334 public $fk_default_bom;
341 public $product_fourn_price_id;
363 public $status_batch = 0;
370 public $batch_mask =
'';
388 public $weight_units;
390 public $length_units;
394 public $height_units;
396 public $surface_units;
398 public $volume_units;
401 public $net_measure_units;
403 public $accountancy_code_sell;
404 public $accountancy_code_sell_intra;
405 public $accountancy_code_sell_export;
406 public $accountancy_code_buy;
407 public $accountancy_code_buy_intra;
408 public $accountancy_code_buy_export;
418 public $barcode_type;
423 public $barcode_type_code;
425 public $stats_propale = array();
426 public $stats_commande = array();
427 public $stats_contrat = array();
428 public $stats_facture = array();
429 public $stats_proposal_supplier = array();
430 public $stats_commande_fournisseur = array();
431 public $stats_expedition = array();
432 public $stats_reception = array();
433 public $stats_mo = array();
434 public $stats_bom = array();
435 public $stats_mrptoconsume = array();
436 public $stats_mrptoproduce = array();
437 public $stats_facturerec = array();
438 public $stats_facture_fournisseur = array();
447 public $date_creation;
452 public $date_modification;
468 public $fk_default_warehouse;
472 public $fk_price_expression;
477 public $fourn_price_base_type;
489 public $ref_supplier;
503 public $price_autogen = 0;
510 public $supplierprices;
530 public $is_object_used;
532 public $is_sousproduit_qty;
533 public $is_sousproduit_incdec;
535 public $mandatory_period;
566 public $fields = array(
567 'rowid' => array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'index'=>1,
'position'=>1,
'comment'=>
'Id'),
568 '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'),
569 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'enabled'=>1,
'visible'=>0,
'default'=>1,
'notnull'=>1,
'index'=>1,
'position'=>5),
570 'label' =>array(
'type'=>
'varchar(255)',
'label'=>
'Label',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>2,
'position'=>15,
'csslist'=>
'tdoverflowmax250'),
571 'barcode' =>array(
'type'=>
'varchar(255)',
'label'=>
'Barcode',
'enabled'=>
'isModEnabled("barcode")',
'position'=>20,
'visible'=>-1,
'showoncombobox'=>3),
572 'fk_barcode_type' => array(
'type'=>
'integer',
'label'=>
'BarcodeType',
'enabled'=>
'1',
'position'=>21,
'notnull'=>0,
'visible'=>-1,),
573 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>61),
574 'note' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>62),
575 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>500),
576 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>501),
578 'fk_user_author'=>array(
'type'=>
'integer',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>510,
'foreignkey'=>
'llx_user.rowid'),
579 'fk_user_modif' =>array(
'type'=>
'integer',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>511),
581 'localtax1_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax1tx',
'enabled'=>
'1',
'position'=>150,
'notnull'=>0,
'visible'=>-1,),
582 'localtax1_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax1type',
'enabled'=>
'1',
'position'=>155,
'notnull'=>1,
'visible'=>-1,),
583 'localtax2_tx' => array(
'type'=>
'double(6,3)',
'label'=>
'Localtax2tx',
'enabled'=>
'1',
'position'=>160,
'notnull'=>0,
'visible'=>-1,),
584 'localtax2_type' => array(
'type'=>
'varchar(10)',
'label'=>
'Localtax2type',
'enabled'=>
'1',
'position'=>165,
'notnull'=>1,
'visible'=>-1,),
585 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'index'=>0,
'position'=>1000),
588 'mandatory_period' => array(
'type'=>
'integer',
'label'=>
'mandatoryperiod',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'default'=>0,
'index'=>1,
'position'=>1000),
604 const SELL_OR_EAT_BY_MANDATORY_ID_SELL_BY = 1;
605 const SELL_OR_EAT_BY_MANDATORY_ID_EAT_BY = 2;
606 const SELL_OR_EAT_BY_MANDATORY_ID_SELL_AND_EAT = 3;
628 $this->
ref = trim($this->
ref);
656 public function create($user, $notrigger = 0)
658 global $conf, $langs;
664 $this->
ref = trim($this->
ref);
668 $this->label = trim($this->label);
669 $this->price_ttc =
price2num($this->price_ttc);
671 $this->price_min_ttc =
price2num($this->price_min_ttc);
672 $this->price_min =
price2num($this->price_min);
673 if (empty($this->tva_tx)) {
676 if (empty($this->tva_npr)) {
680 if (empty($this->localtax1_tx)) {
681 $this->localtax1_tx = 0;
683 if (empty($this->localtax2_tx)) {
684 $this->localtax2_tx = 0;
686 if (empty($this->localtax1_type)) {
687 $this->localtax1_type =
'0';
689 if (empty($this->localtax2_type)) {
690 $this->localtax2_type =
'0';
692 if (empty($this->
price)) {
695 if (empty($this->price_min)) {
696 $this->price_min = 0;
699 if (empty($this->price_by_qty)) {
700 $this->price_by_qty = 0;
703 if (empty($this->
status)) {
706 if (empty($this->status_buy)) {
707 $this->status_buy = 0;
716 if ($this->price_base_type ==
'TTC' && $this->price_ttc > 0) {
717 $price_ttc =
price2num($this->price_ttc,
'MU');
718 $price_ht =
price2num($this->price_ttc / (1 + ($this->tva_tx / 100)),
'MU');
722 if ($this->price_base_type !=
'TTC' && $this->
price > 0) {
724 $price_ttc =
price2num($this->
price * (1 + ($this->tva_tx / 100)),
'MU');
728 if (($this->price_min_ttc > 0) && ($this->price_base_type ==
'TTC')) {
729 $price_min_ttc =
price2num($this->price_min_ttc,
'MU');
730 $price_min_ht =
price2num($this->price_min_ttc / (1 + ($this->tva_tx / 100)),
'MU');
734 if (($this->price_min > 0) && ($this->price_base_type !=
'TTC')) {
735 $price_min_ht =
price2num($this->price_min,
'MU');
736 $price_min_ttc =
price2num($this->price_min * (1 + ($this->tva_tx / 100)),
'MU');
739 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
740 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
741 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
742 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
743 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
744 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
747 $this->barcode = trim($this->barcode);
748 $this->mandatory_period = empty($this->mandatory_period) ? 0 : $this->mandatory_period;
750 if (empty($this->label)) {
751 $this->error =
'ErrorMandatoryParametersNotProvided';
755 if (empty($this->
ref) || $this->
ref ==
'auto') {
758 if ($module !=
'mod_codeproduct_leopard') {
759 if (substr($module, 0, 16) ==
'mod_codeproduct_' && substr($module, -3) ==
'php') {
760 $module = substr($module, 0,
dol_strlen($module) - 4);
763 $modCodeProduct =
new $module();
764 if (!empty($modCodeProduct->code_auto)) {
765 $this->
ref = $modCodeProduct->getNextValue($this, $this->
type);
767 unset($modCodeProduct);
770 if (empty($this->
ref)) {
771 $this->error =
'ProductModuleNotSetupForAutoRef';
776 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);
783 if ($this->barcode == -1) {
784 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
789 $result = $this->
verify();
792 $sql =
"SELECT count(*) as nb";
793 $sql .=
" FROM ".$this->db->prefix().
"product";
794 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
795 $sql .=
" AND ref = '".$this->db->escape($this->
ref).
"'";
797 $result = $this->db->query($sql);
799 $obj = $this->db->fetch_object($result);
802 $sql =
"INSERT INTO ".$this->db->prefix().
"product (";
807 $sql .=
", price_min";
808 $sql .=
", price_min_ttc";
810 $sql .=
", fk_user_author";
811 $sql .=
", fk_product_type";
813 $sql .=
", price_ttc";
814 $sql .=
", price_base_type";
818 $sql .=
", accountancy_code_buy";
819 $sql .=
", accountancy_code_buy_intra";
820 $sql .=
", accountancy_code_buy_export";
821 $sql .=
", accountancy_code_sell";
822 $sql .=
", accountancy_code_sell_intra";
823 $sql .=
", accountancy_code_sell_export";
826 $sql .=
", finished";
828 $sql .=
", batch_mask";
830 $sql .=
", mandatory_period";
831 $sql .=
") VALUES (";
832 $sql .=
"'".$this->db->idate($now).
"'";
833 $sql .=
", ".(!empty($this->entity) ? (int) $this->entity : (int) $conf->entity);
834 $sql .=
", '".$this->db->escape($this->
ref).
"'";
835 $sql .=
", ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
836 $sql .=
", ".price2num($price_min_ht);
837 $sql .=
", ".price2num($price_min_ttc);
838 $sql .=
", ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
839 $sql .=
", ".((int) $user->id);
840 $sql .=
", ".((int) $this->
type);
841 $sql .=
", ".price2num($price_ht,
'MT');
842 $sql .=
", ".price2num($price_ttc,
'MT');
843 $sql .=
", '".$this->db->escape($this->price_base_type).
"'";
844 $sql .=
", ".((int) $this->
status);
845 $sql .=
", ".((int) $this->status_buy);
847 $sql .=
", '".$this->db->escape($this->accountancy_code_buy).
"'";
848 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_intra).
"'";
849 $sql .=
", '".$this->db->escape($this->accountancy_code_buy_export).
"'";
850 $sql .=
", '".$this->db->escape($this->accountancy_code_sell).
"'";
851 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_intra).
"'";
852 $sql .=
", '".$this->db->escape($this->accountancy_code_sell_export).
"'";
854 $sql .=
", '".$this->db->escape($this->canvas).
"'";
855 $sql .=
", ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
'NULL' : (int) $this->finished);
856 $sql .=
", ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : ((int) $this->status_batch));
857 $sql .=
", '".$this->db->escape($this->batch_mask).
"'";
858 $sql .=
", ".($this->fk_unit > 0 ? ((int) $this->fk_unit) :
'NULL');
859 $sql .=
", '".$this->db->escape($this->mandatory_period).
"'";
862 dol_syslog(get_class($this).
"::Create", LOG_DEBUG);
863 $result = $this->db->query($sql);
865 $id = $this->db->last_insert_id($this->db->prefix().
"product");
869 $this->
price = $price_ht;
870 $this->price_ttc = $price_ttc;
871 $this->price_min = $price_min_ht;
872 $this->price_min_ttc = $price_min_ttc;
876 if ($this->
update($id, $user,
true,
'add') <= 0) {
881 $this->error = $this->db->lasterror();
886 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " .((int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
888 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
889 $sql .=
" fk_product";
891 $sql .=
", accountancy_code_buy";
892 $sql .=
", accountancy_code_buy_intra";
893 $sql .=
", accountancy_code_buy_export";
894 $sql .=
", accountancy_code_sell";
895 $sql .=
", accountancy_code_sell_intra";
896 $sql .=
", accountancy_code_sell_export";
897 $sql .=
") VALUES (";
899 $sql .=
", " . $conf->entity;
900 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
901 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
902 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
903 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
904 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
905 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
907 $result = $this->db->query($sql);
910 $this->error =
'ErrorFailedToInsertAccountancyForEntity';
915 $this->error =
'ErrorFailedToGetInsertedId';
919 $this->error = $this->db->lasterror();
923 $langs->load(
"products");
925 $this->error =
"ErrorProductAlreadyExists";
926 dol_syslog(get_class($this).
"::Create fails, ref ".$this->
ref.
" already exists");
930 $this->error = $this->db->lasterror();
933 if (!$error && !$notrigger) {
946 $this->db->rollback();
950 $this->db->rollback();
951 dol_syslog(get_class($this).
"::Create fails verify ".join(
',', $this->errors), LOG_WARNING);
967 $this->errors = array();
970 $this->
ref = trim($this->
ref);
973 $this->errors[] =
'ErrorBadRef';
977 $arrayofnonnegativevalue = array(
'weight'=>
'Weight',
'width'=>
'Width',
'height'=>
'Height',
'length'=>
'Length',
'surface'=>
'Surface',
'volume'=>
'Volume');
978 foreach ($arrayofnonnegativevalue as $key => $value) {
979 if (property_exists($this, $key) && !empty($this->$key) && ($this->$key < 0)) {
980 $langs->loadLangs(array(
"main",
"other"));
981 $this->error = $langs->trans(
"FieldCannotBeNegative", $langs->transnoentitiesnoconv($value));
982 $this->errors[] = $this->error;
987 $rescode = $this->
check_barcode($this->barcode, $this->barcode_type_code);
989 if ($rescode == -1) {
990 $this->errors[] =
'ErrorBadBarCodeSyntax';
991 } elseif ($rescode == -2) {
992 $this->errors[] =
'ErrorBarCodeRequired';
993 } elseif ($rescode == -3) {
995 $this->errors[] =
'ErrorBarCodeAlreadyUsed';
1020 $module = strtolower($conf->global->BARCODE_PRODUCT_ADDON_NUM);
1022 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
1023 foreach ($dirsociete as $dirroot) {
1030 $mod =
new $module();
1032 dol_syslog(get_class($this).
"::check_barcode value=".$valuetotest.
" type=".$typefortest.
" module=".$module);
1033 $result = $mod->verif($this->db, $valuetotest, $this, 0, $typefortest);
1051 public function update($id, $user, $notrigger =
false, $action =
'update', $updatetype =
false)
1053 global $langs, $conf, $hookmanager;
1058 if (!$this->label) {
1059 $this->label =
'MISSING LABEL';
1064 $this->
ref = trim($this->
ref);
1068 $this->label = trim($this->label);
1070 $this->note_private = (isset($this->note_private) ? trim($this->note_private) :
null);
1071 $this->note_public = (isset($this->note_public) ? trim($this->note_public) :
null);
1072 $this->net_measure =
price2num($this->net_measure);
1073 $this->net_measure_units = trim($this->net_measure_units);
1074 $this->weight =
price2num($this->weight);
1075 $this->weight_units = trim($this->weight_units);
1076 $this->length =
price2num($this->length);
1077 $this->length_units = trim($this->length_units);
1079 $this->width_units = trim($this->width_units);
1080 $this->height =
price2num($this->height);
1081 $this->height_units = trim($this->height_units);
1082 $this->surface =
price2num($this->surface);
1083 $this->surface_units = trim($this->surface_units);
1084 $this->volume =
price2num($this->volume);
1085 $this->volume_units = trim($this->volume_units);
1088 if (is_numeric($this->length_units)) {
1089 $this->width_units = $this->length_units;
1091 if (is_numeric($this->length_units)) {
1092 $this->height_units = $this->length_units;
1096 if (empty($this->surface) && !empty($this->length) && !empty($this->width) && $this->length_units == $this->width_units) {
1097 $this->surface = $this->length * $this->width;
1100 if (empty($this->volume) && !empty($this->surface) && !empty($this->height) && $this->length_units == $this->height_units) {
1101 $this->volume = $this->surface * $this->height;
1105 if (empty($this->tva_tx)) {
1108 if (empty($this->tva_npr)) {
1111 if (empty($this->localtax1_tx)) {
1112 $this->localtax1_tx = 0;
1114 if (empty($this->localtax2_tx)) {
1115 $this->localtax2_tx = 0;
1117 if (empty($this->localtax1_type)) {
1118 $this->localtax1_type =
'0';
1120 if (empty($this->localtax2_type)) {
1121 $this->localtax2_type =
'0';
1123 if (empty($this->
status)) {
1126 if (empty($this->status_buy)) {
1127 $this->status_buy = 0;
1130 if (empty($this->country_id)) {
1131 $this->country_id = 0;
1134 if (empty($this->state_id)) {
1135 $this->state_id = 0;
1139 $this->barcode = trim($this->barcode);
1141 $this->accountancy_code_buy = trim($this->accountancy_code_buy);
1142 $this->accountancy_code_buy_intra = trim($this->accountancy_code_buy_intra);
1143 $this->accountancy_code_buy_export = trim($this->accountancy_code_buy_export);
1144 $this->accountancy_code_sell = trim($this->accountancy_code_sell);
1145 $this->accountancy_code_sell_intra = trim($this->accountancy_code_sell_intra);
1146 $this->accountancy_code_sell_export = trim($this->accountancy_code_sell_export);
1153 if ($action !=
'add') {
1154 $result = $this->
verify();
1162 if (empty($this->oldcopy)) {
1167 if ($this->
hasbatch() && !$this->oldcopy->hasbatch()) {
1169 $valueforundefinedlot =
'000000';
1171 $valueforundefinedlot = $conf->global->STOCK_DEFAULT_BATCH;
1174 dol_syslog(
"Flag batch of product id=".$this->
id.
" is set to ON, so we will create missing records into product_batch");
1177 foreach ($this->stock_warehouse as $idW => $ObjW) {
1179 foreach ($ObjW->detail_batch as $detail) {
1180 if ($detail->batch == $valueforundefinedlot || $detail->batch ==
'Undefined') {
1182 $sqlclean =
"DELETE FROM ".$this->db->prefix().
"product_batch WHERE batch in('Undefined', '".$this->db->escape($valueforundefinedlot).
"') AND fk_product_stock = ".((int) $ObjW->id);
1183 $result = $this->db->query($sqlclean);
1191 $qty_batch += $detail->qty;
1195 if ($ObjW->real != $qty_batch) {
1197 $ObjBatch->batch = $valueforundefinedlot;
1198 $ObjBatch->qty = ($ObjW->real - $qty_batch);
1199 $ObjBatch->fk_product_stock = $ObjW->id;
1201 if ($ObjBatch->create($user, 1) < 0) {
1203 $this->errors = $ObjBatch->errors;
1207 if ($ObjLot->fetch(0, $this->id, $valueforundefinedlot) == 0) {
1208 $ObjLot->fk_product = $this->id;
1209 $ObjLot->entity = $this->entity;
1210 $ObjLot->fk_user_creat = $user->id;
1211 $ObjLot->batch = $valueforundefinedlot;
1212 if ($ObjLot->create($user,
true) < 0) {
1214 $this->errors = $ObjLot->errors;
1223 if ($this->barcode == -1) {
1224 $this->barcode = $this->
get_barcode($this, $this->barcode_type_code);
1227 $sql =
"UPDATE ".$this->db->prefix().
"product";
1228 $sql .=
" SET label = '".$this->db->escape($this->label).
"'";
1231 $sql .=
", fk_product_type = ".((int) $this->
type);
1234 $sql .=
", ref = '".$this->db->escape($this->
ref).
"'";
1235 $sql .=
", ref_ext = ".(!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
1236 $sql .=
", default_vat_code = ".($this->default_vat_code ?
"'".$this->db->escape($this->default_vat_code).
"'" :
"null");
1237 $sql .=
", tva_tx = ".((float) $this->tva_tx);
1238 $sql .=
", recuperableonly = ".((int) $this->tva_npr);
1239 $sql .=
", localtax1_tx = ".((float) $this->localtax1_tx);
1240 $sql .=
", localtax2_tx = ".((float) $this->localtax2_tx);
1241 $sql .=
", localtax1_type = ".($this->localtax1_type !=
'' ?
"'".$this->db->escape($this->localtax1_type).
"'" :
"'0'");
1242 $sql .=
", localtax2_type = ".($this->localtax2_type !=
'' ?
"'".$this->db->escape($this->localtax2_type).
"'" :
"'0'");
1244 $sql .=
", barcode = ".(empty($this->barcode) ?
"null" :
"'".$this->db->escape($this->barcode).
"'");
1245 $sql .=
", fk_barcode_type = ".(empty($this->barcode_type) ?
"null" : $this->db->escape($this->barcode_type));
1247 $sql .=
", tosell = ".(int) $this->
status;
1248 $sql .=
", tobuy = ".(int) $this->status_buy;
1249 $sql .=
", tobatch = ".((empty($this->status_batch) || $this->status_batch < 0) ?
'0' : (int) $this->status_batch);
1250 $sql .=
", batch_mask = '".$this->db->escape($this->batch_mask).
"'";
1252 $sql .=
", finished = ".((!isset($this->finished) || $this->finished < 0 || $this->finished ==
'') ?
"null" : (int) $this->finished);
1253 $sql .=
", fk_default_bom = ".((!isset($this->fk_default_bom) || $this->fk_default_bom < 0 || $this->fk_default_bom ==
'') ?
"null" : (int) $this->fk_default_bom);
1254 $sql .=
", net_measure = ".($this->net_measure !=
'' ?
"'".$this->db->escape($this->net_measure).
"'" :
'null');
1255 $sql .=
", net_measure_units = ".($this->net_measure_units !=
'' ?
"'".$this->db->escape($this->net_measure_units).
"'" :
'null');
1256 $sql .=
", weight = ".($this->weight !=
'' ?
"'".$this->db->escape($this->weight).
"'" :
'null');
1257 $sql .=
", weight_units = ".($this->weight_units !=
'' ?
"'".$this->db->escape($this->weight_units).
"'" :
'null');
1258 $sql .=
", length = ".($this->length !=
'' ?
"'".$this->db->escape($this->length).
"'" :
'null');
1259 $sql .=
", length_units = ".($this->length_units !=
'' ?
"'".$this->db->escape($this->length_units).
"'" :
'null');
1260 $sql .=
", width= ".($this->width !=
'' ?
"'".$this->db->escape($this->width).
"'" :
'null');
1261 $sql .=
", width_units = ".($this->width_units !=
'' ?
"'".$this->db->escape($this->width_units).
"'" :
'null');
1262 $sql .=
", height = ".($this->height !=
'' ?
"'".$this->db->escape($this->height).
"'" :
'null');
1263 $sql .=
", height_units = ".($this->height_units !=
'' ?
"'".$this->db->escape($this->height_units).
"'" :
'null');
1264 $sql .=
", surface = ".($this->surface !=
'' ?
"'".$this->db->escape($this->surface).
"'" :
'null');
1265 $sql .=
", surface_units = ".($this->surface_units !=
'' ?
"'".$this->db->escape($this->surface_units).
"'" :
'null');
1266 $sql .=
", volume = ".($this->volume !=
'' ?
"'".$this->db->escape($this->volume).
"'" :
'null');
1267 $sql .=
", volume_units = ".($this->volume_units !=
'' ?
"'".$this->db->escape($this->volume_units).
"'" :
'null');
1268 $sql .=
", fk_default_warehouse = ".($this->fk_default_warehouse > 0 ? ((int) $this->fk_default_warehouse) :
'null');
1269 $sql .=
", fk_default_workstation = ".($this->fk_default_workstation > 0 ? ((int) $this->fk_default_workstation) :
'null');
1270 $sql .=
", seuil_stock_alerte = ".((isset($this->seuil_stock_alerte) && is_numeric($this->seuil_stock_alerte)) ? (
float) $this->seuil_stock_alerte :
'null');
1271 $sql .=
", description = '".$this->db->escape($this->
description).
"'";
1272 $sql .=
", url = ".($this->url ?
"'".$this->db->escape($this->url).
"'" :
'null');
1273 $sql .=
", customcode = '".$this->db->escape($this->customcode).
"'";
1274 $sql .=
", fk_country = ".($this->country_id > 0 ? (int) $this->country_id :
'null');
1275 $sql .=
", fk_state = ".($this->state_id > 0 ? (int) $this->state_id :
'null');
1276 $sql .=
", lifetime = ".($this->lifetime > 0 ? (int) $this->lifetime :
'null');
1277 $sql .=
", qc_frequency = ".($this->qc_frequency > 0 ? (int) $this->qc_frequency :
'null');
1278 $sql .=
", note = ".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
'null');
1279 $sql .=
", note_public = ".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
'null');
1280 $sql .=
", duration = '".$this->db->escape($this->duration_value.$this->duration_unit).
"'";
1282 $sql .=
", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1283 $sql .=
", accountancy_code_buy_intra = '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1284 $sql .=
", accountancy_code_buy_export = '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1285 $sql .=
", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1286 $sql .=
", accountancy_code_sell_intra= '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1287 $sql .=
", accountancy_code_sell_export= '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1289 $sql .=
", desiredstock = ".((isset($this->desiredstock) && is_numeric($this->desiredstock)) ? (
float) $this->desiredstock :
"null");
1290 $sql .=
", cost_price = ".($this->cost_price !=
'' ? $this->db->escape($this->cost_price) :
'null');
1291 $sql .=
", fk_unit= ".(!$this->fk_unit ?
'NULL' : (int) $this->fk_unit);
1292 $sql .=
", price_autogen = ".(!$this->price_autogen ? 0 : 1);
1293 $sql .=
", fk_price_expression = ".($this->fk_price_expression != 0 ? (int) $this->fk_price_expression :
'NULL');
1294 $sql .=
", fk_user_modif = ".($user->id > 0 ? $user->id :
'NULL');
1295 $sql .=
", mandatory_period = ".($this->mandatory_period);
1297 $sql .=
" WHERE rowid = ".((int) $id);
1299 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
1301 $resql = $this->db->query($sql);
1316 $this->db->query(
"DELETE FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = " . ((
int) $this->id) .
" AND entity = " . ((
int) $conf->entity));
1318 $sql =
"INSERT INTO " . $this->db->prefix() .
"product_perentity (";
1319 $sql .=
" fk_product";
1321 $sql .=
", accountancy_code_buy";
1322 $sql .=
", accountancy_code_buy_intra";
1323 $sql .=
", accountancy_code_buy_export";
1324 $sql .=
", accountancy_code_sell";
1325 $sql .=
", accountancy_code_sell_intra";
1326 $sql .=
", accountancy_code_sell_export";
1327 $sql .=
") VALUES (";
1329 $sql .=
", " . $conf->entity;
1330 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy) .
"'";
1331 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_intra) .
"'";
1332 $sql .=
", '" . $this->db->escape($this->accountancy_code_buy_export) .
"'";
1333 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell) .
"'";
1334 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_intra) .
"'";
1335 $sql .=
", '" . $this->db->escape($this->accountancy_code_sell_export) .
"'";
1337 $result = $this->db->query($sql);
1340 $this->error =
'ErrorFailedToUpdateAccountancyForEntity';
1344 if (!$this->
hasbatch() && $this->oldcopy->hasbatch()) {
1346 $sql =
'SELECT pb.qty, ps.fk_entrepot, pb.batch FROM '.MAIN_DB_PREFIX.
'product_batch as pb';
1347 $sql.=
' INNER JOIN '.MAIN_DB_PREFIX.
'product_stock as ps ON (ps.rowid = pb.fk_product_stock)';
1348 $sql.=
' WHERE ps.fk_product = '.(int) $this->
id;
1350 $resql = $this->db->query($sql);
1354 while ($obj = $this->db->fetch_object($resql)) {
1356 $fk_entrepot = $obj->fk_entrepot;
1360 $batch = $obj->batch;
1363 $addOremove = $value > 0 ? 1 : 0;
1364 $label = $langs->trans(
'BatchStockMouvementAddInGlobal');
1365 $res = $this->
correct_stock_batch($user, $fk_entrepot, abs($value), $addOremove, $label, $price, $dlc, $dluo, $batch, $inventorycode,
'',
null, 0,
null,
true);
1368 $label = $langs->trans(
'BatchStockMouvementAddInGlobal');
1369 $res = $this->
correct_stock($user, $fk_entrepot, abs($value), (
int) empty($addOremove), $label, $price, $inventorycode,
'',
null, 0);
1388 if (!$error && !$notrigger) {
1390 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1397 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
1399 if ($conf->product->dir_output) {
1402 if (file_exists($olddir)) {
1406 $res = @rename($olddir, $newdir);
1408 $langs->load(
"errors");
1409 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
1417 if (isModEnabled(
'variants')) {
1418 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1422 foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) {
1423 $currcomb->updateProperties($this, $user);
1427 $this->db->commit();
1430 $this->db->rollback();
1434 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1435 $langs->load(
"errors");
1436 if (empty($conf->barcode->enabled) || empty($this->barcode)) {
1437 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductAlreadyExists", $this->
ref);
1439 $this->error = $langs->trans(
"Error").
" : ".$langs->trans(
"ErrorProductBarCodeAlreadyExists", $this->barcode);
1441 $this->errors[] = $this->error;
1442 $this->db->rollback();
1445 $this->error = $langs->trans(
"Error").
" : ".$this->db->error().
" - ".$sql;
1446 $this->errors[] = $this->error;
1447 $this->db->rollback();
1452 $this->db->rollback();
1453 dol_syslog(get_class($this).
"::Update fails verify ".join(
',', $this->errors), LOG_WARNING);
1465 public function delete(
User $user, $notrigger = 0)
1467 global $conf, $langs;
1468 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1473 if (empty($this->
id)) {
1474 $this->error =
"Object must be fetched before calling delete";
1478 $this->error =
"ErrorForbidden";
1483 if (empty($objectisused)) {
1486 if (!$error && empty($notrigger)) {
1488 $result = $this->
call_trigger(
'PRODUCT_DELETE', $user);
1497 $sql =
"DELETE FROM ".$this->db->prefix().
'product_batch';
1498 $sql .=
" WHERE fk_product_stock IN (";
1499 $sql .=
"SELECT rowid FROM ".$this->db->prefix().
'product_stock';
1500 $sql .=
" WHERE fk_product = ".((int) $this->
id).
")";
1502 $result = $this->db->query($sql);
1505 $this->errors[] = $this->db->lasterror();
1511 $elements = array(
'product_fournisseur_price',
'product_price',
'product_lang',
'categorie_product',
'product_stock',
'product_customer_price',
'product_lot');
1512 foreach ($elements as $table) {
1514 $sql =
"DELETE FROM ".$this->db->prefix().$table;
1515 $sql .=
" WHERE fk_product = ".(int) $this->
id;
1517 $result = $this->db->query($sql);
1520 $this->errors[] = $this->db->lasterror();
1527 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
1528 include_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
1533 if ($prodcomb->deleteByFkProductParent($user, $this->id) < 0) {
1535 $this->errors[] =
'Error deleting combinations';
1539 if (!$error && ($prodcomb->fetchByFkProductChild($this->id) > 0) && ($prodcomb->delete($user) < 0)) {
1541 $this->errors[] =
'Error deleting child combination';
1547 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
1548 $sql .=
" WHERE fk_product_pere = ".(int) $this->
id.
" OR fk_product_fils = ".(
int) $this->id;
1550 $result = $this->db->query($sql);
1553 $this->errors[] = $this->db->lasterror();
1562 dol_syslog(get_class($this).
"::delete error -4 ".$this->error, LOG_ERR);
1568 $sqlz =
"DELETE FROM ".$this->db->prefix().
"product";
1569 $sqlz .=
" WHERE rowid = ".(int) $this->
id;
1571 $resultz = $this->db->query($sqlz);
1574 $this->errors[] = $this->db->lasterror();
1590 if ($conf->product->dir_output) {
1591 $dir = $conf->product->dir_output.
"/".$ref;
1592 if (file_exists($dir)) {
1595 $this->errors[] =
'ErrorFailToDeleteDir';
1603 $this->db->commit();
1606 foreach ($this->errors as $errmsg) {
1607 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
1608 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1610 $this->db->rollback();
1614 $this->error =
"ErrorRecordIsUsedCantDelete";
1627 global $conf, $langs;
1629 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
1630 $current_lang = $langs->getDefaultLang();
1632 foreach ($langs_available as $key => $value) {
1633 if ($key == $current_lang) {
1634 $sql =
"SELECT rowid";
1635 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1636 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1637 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1639 $result = $this->db->query($sql);
1641 if ($this->db->num_rows($result)) {
1642 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1644 $sql2 .=
" label='".$this->db->escape($this->label).
"',";
1645 $sql2 .=
" description='".$this->db->escape($this->
description).
"'";
1647 $sql2 .=
", note='".$this->db->escape($this->other).
"'";
1649 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1651 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1656 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->label).
"',";
1657 $sql2 .=
" '".$this->db->escape($this->
description).
"'";
1659 $sql2 .=
", '".$this->db->escape($this->other).
"'";
1663 dol_syslog(get_class($this).
'::setMultiLangs key = current_lang = '.$key);
1664 if (!$this->db->query($sql2)) {
1665 $this->error = $this->db->lasterror();
1668 } elseif (isset($this->multilangs[$key])) {
1669 if (empty($this->multilangs[
"$key"][
"label"])) {
1670 $this->errors[] = $key .
' : ' . $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Label"));
1674 $sql =
"SELECT rowid";
1675 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1676 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1677 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1679 $result = $this->db->query($sql);
1681 if ($this->db->num_rows($result)) {
1682 $sql2 =
"UPDATE ".$this->db->prefix().
"product_lang";
1684 $sql2 .=
" label = '".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1685 $sql2 .=
" description = '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1687 $sql2 .=
", note = '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1689 $sql2 .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($key).
"'";
1691 $sql2 =
"INSERT INTO ".$this->db->prefix().
"product_lang (fk_product, lang, label, description";
1696 $sql2 .=
" VALUES(".$this->id.
",'".$this->db->escape($key).
"','".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1697 $sql2 .=
" '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1699 $sql2 .=
", '".$this->db->escape($this->multilangs[
"$key"][
"other"]).
"'";
1705 if ($this->multilangs[
"$key"][
"label"] || $this->multilangs[
"$key"][
"description"]) {
1706 if (!$this->db->query($sql2)) {
1707 $this->error = $this->db->lasterror();
1717 $result = $this->
call_trigger(
'PRODUCT_SET_MULTILANGS', $user);
1719 $this->error = $this->db->lasterror();
1737 $sql =
"DELETE FROM ".$this->db->prefix().
"product_lang";
1738 $sql .=
" WHERE fk_product = ".((int) $this->
id).
" AND lang = '".$this->db->escape($langtodelete).
"'";
1740 dol_syslog(get_class($this).
'::delMultiLangs', LOG_DEBUG);
1741 $result = $this->db->query($sql);
1744 $result = $this->
call_trigger(
'PRODUCT_DEL_MULTILANGS', $user);
1746 $this->error = $this->db->lasterror();
1747 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1753 $this->error = $this->db->lasterror();
1754 dol_syslog(get_class($this).
'::delMultiLangs error='.$this->error, LOG_ERR);
1769 global $user, $langs, $conf;
1775 if ($type ==
'buy') {
1776 $field =
'accountancy_code_buy';
1777 } elseif ($type ==
'buy_intra') {
1778 $field =
'accountancy_code_buy_intra';
1779 } elseif ($type ==
'buy_export') {
1780 $field =
'accountancy_code_buy_export';
1781 } elseif ($type ==
'sell') {
1782 $field =
'accountancy_code_sell';
1783 } elseif ($type ==
'sell_intra') {
1784 $field =
'accountancy_code_sell_intra';
1785 } elseif ($type ==
'sell_export') {
1786 $field =
'accountancy_code_sell_export';
1791 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET ";
1792 $sql .=
"$field = '".$this->db->escape($value).
"'";
1793 $sql .=
" WHERE rowid = ".((int) $this->
id);
1796 $resql = $this->db->query($sql);
1800 $result = $this->
call_trigger(
'PRODUCT_MODIFY', $user);
1807 $this->db->rollback();
1811 $this->$field = $value;
1813 $this->db->commit();
1816 $this->error = $this->db->lasterror();
1817 $this->db->rollback();
1831 $current_lang = $langs->getDefaultLang();
1833 $sql =
"SELECT lang, label, description, note as other";
1834 $sql .=
" FROM ".$this->db->prefix().
"product_lang";
1835 $sql .=
" WHERE fk_product = ".((int) $this->
id);
1837 $result = $this->db->query($sql);
1839 while ($obj = $this->db->fetch_object($result)) {
1841 if ($obj->lang == $current_lang) {
1842 $this->label = $obj->label;
1844 $this->other = $obj->other;
1846 $this->multilangs[
"$obj->lang"][
"label"] = $obj->label;
1847 $this->multilangs[
"$obj->lang"][
"description"] = $obj->description;
1848 $this->multilangs[
"$obj->lang"][
"other"] = $obj->other;
1852 $this->error =
"Error: ".$this->db->lasterror().
" - ".$sql;
1865 $testExit = array(
'multiprices',
'multiprices_ttc',
'multiprices_base_type',
'multiprices_min',
'multiprices_min_ttc',
'multiprices_tva_tx',
'multiprices_recuperableonly');
1867 foreach ($testExit as $field) {
1868 if (!isset($this->$field)) {
1871 $tmparray = $this->$field;
1872 if (!isset($tmparray[$level])) {
1878 'level' => $level ? $level : 1,
1879 'multiprices' => (float) $this->multiprices[$level],
1880 'multiprices_ttc' => (
float) $this->multiprices_ttc[$level],
1881 'multiprices_base_type' => $this->multiprices_base_type[$level],
1882 'multiprices_min' => (float) $this->multiprices_min[$level],
1883 'multiprices_min_ttc' => (
float) $this->multiprices_min_ttc[$level],
1884 'multiprices_tva_tx' => (float) $this->multiprices_tva_tx[$level],
1885 'multiprices_recuperableonly' => (
float) $this->multiprices_recuperableonly[$level],
1908 if (empty($this->price_by_qty)) {
1909 $this->price_by_qty = 0;
1913 $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,";
1914 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
1915 $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).
",";
1916 $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');
1919 dol_syslog(get_class($this).
"::_log_price", LOG_DEBUG);
1920 $resql = $this->db->query($sql);
1922 $this->error = $this->db->lasterror();
1942 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price_by_qty";
1943 $sql .=
" WHERE fk_product_price = ".((int) $rowid);
1944 $resql = $this->db->query($sql);
1946 $sql =
"DELETE FROM ".$this->db->prefix().
"product_price";
1947 $sql .=
" WHERE rowid=".((int) $rowid);
1948 $resql = $this->db->query($sql);
1952 $this->error = $this->db->lasterror();
1967 public function getSellPrice($thirdparty_seller, $thirdparty_buyer, $pqp = 0)
1969 global $conf, $hookmanager, $action;
1972 if (is_object($hookmanager)) {
1973 $parameters = array(
'thirdparty_seller'=>$thirdparty_seller,
'thirdparty_buyer' => $thirdparty_buyer,
'pqp' => $pqp);
1975 $reshook = $hookmanager->executeHooks(
'getSellPrice', $parameters, $this, $action);
1977 return $hookmanager->resArray;
1988 $pu_ht = $this->price;
1989 $pu_ttc = $this->price_ttc;
1990 $price_min = $this->price_min;
1991 $price_base_type = $this->price_base_type;
1994 if (
getDolGlobalString(
'PRODUIT_MULTIPRICES') && !empty($thirdparty_buyer->price_level)) {
1995 $pu_ht = $this->multiprices[$thirdparty_buyer->price_level];
1996 $pu_ttc = $this->multiprices_ttc[$thirdparty_buyer->price_level];
1997 $price_min = $this->multiprices_min[$thirdparty_buyer->price_level];
1998 $price_base_type = $this->multiprices_base_type[$thirdparty_buyer->price_level];
2000 if (isset($this->multiprices_tva_tx[$thirdparty_buyer->price_level])) {
2001 $tva_tx = $this->multiprices_tva_tx[$thirdparty_buyer->price_level];
2003 if (isset($this->multiprices_recuperableonly[$thirdparty_buyer->price_level])) {
2004 $tva_npr = $this->multiprices_recuperableonly[$thirdparty_buyer->price_level];
2012 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
2016 $filter = array(
't.fk_product' => $this->
id,
't.fk_soc' => $thirdparty_buyer->id);
2018 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
2020 if (count($prodcustprice->lines) > 0) {
2021 $pu_ht =
price($prodcustprice->lines[0]->price);
2022 $price_min =
price($prodcustprice->lines[0]->price_min);
2023 $pu_ttc =
price($prodcustprice->lines[0]->price_ttc);
2024 $price_base_type = $prodcustprice->lines[0]->price_base_type;
2025 $tva_tx = $prodcustprice->lines[0]->tva_tx;
2026 if ($prodcustprice->lines[0]->default_vat_code && !preg_match(
'/\(.*\)/',
$tva_tx)) {
2027 $tva_tx .=
' ('.$prodcustprice->lines[0]->default_vat_code.
')';
2029 $tva_npr = $prodcustprice->lines[0]->recuperableonly;
2037 if ($this->prices_by_qty[0]) {
2040 foreach ($this->prices_by_qty_list[0] as $priceforthequantityarray) {
2041 if ($priceforthequantityarray[
'rowid'] != $pqp) {
2045 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
2046 $pu_ht = $priceforthequantityarray[
'unitprice'];
2048 $pu_ttc = $priceforthequantityarray[
'unitprice'];
2055 if ($this->prices_by_qty[$thirdparty_buyer->price_level]) {
2058 foreach ($this->prices_by_qty_list[$thirdparty_buyer->price_level] as $priceforthequantityarray) {
2059 if ($priceforthequantityarray[
'rowid'] != $pqp) {
2063 if ($priceforthequantityarray[
'price_base_type'] ==
'HT') {
2064 $pu_ht = $priceforthequantityarray[
'unitprice'];
2066 $pu_ttc = $priceforthequantityarray[
'unitprice'];
2073 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);
2090 public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref =
'', $fk_soc = 0)
2097 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2098 $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,";
2099 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2100 $sql .=
" pfp.packaging";
2101 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2102 $sql .=
" WHERE pfp.rowid = ".((int) $prodfournprice);
2104 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2106 $sql .=
" ORDER BY pfp.quantity DESC";
2108 dol_syslog(get_class($this).
"::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
2109 $resql = $this->db->query($sql);
2111 $obj = $this->db->fetch_object($resql);
2112 if ($obj && $obj->quantity > 0) {
2113 if (isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2115 $prod_supplier->product_fourn_price_id = $obj->rowid;
2116 $prod_supplier->id = $obj->fk_product;
2117 $prod_supplier->fourn_qty = $obj->quantity;
2118 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2119 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2121 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2123 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2124 if ($price_result >= 0) {
2125 $obj->price = $price_result;
2128 $this->product_fourn_price_id = $obj->rowid;
2129 $this->buyprice = $obj->price;
2130 $this->fourn_pu = $obj->price / $obj->quantity;
2131 $this->fourn_price_base_type =
'HT';
2132 $this->fourn_socid = $obj->fk_soc;
2133 $this->ref_fourn = $obj->ref_supplier;
2134 $this->ref_supplier = $obj->ref_supplier;
2135 $this->desc_supplier = $obj->desc_supplier;
2136 $this->remise_percent = $obj->remise_percent;
2137 $this->vatrate_supplier = $obj->tva_tx;
2138 $this->default_vat_code_supplier = $obj->default_vat_code;
2139 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2140 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2141 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2142 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2143 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2145 $this->packaging = $obj->packaging;
2147 $result = $obj->fk_product;
2151 $sql =
"SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.remise_percent, pfp.fk_soc,";
2152 $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,";
2153 $sql .=
" pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
2154 $sql .=
" pfp.packaging";
2155 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as pfp";
2156 $sql .=
" WHERE 1 = 1";
2157 if ($product_id > 0) {
2158 $sql .=
" AND pfp.fk_product = ".((int) $product_id);
2160 if ($fourn_ref !=
'none') {
2161 $sql .=
" AND pfp.ref_fourn = '".$this->db->escape($fourn_ref).
"'";
2164 $sql .=
" AND pfp.fk_soc = ".((int) $fk_soc);
2167 $sql .=
" AND pfp.quantity <= ".((float) $qty);
2169 $sql .=
" ORDER BY pfp.quantity DESC";
2172 dol_syslog(get_class($this).
"::get_buyprice second search from qty/ref/product_id", LOG_DEBUG);
2173 $resql = $this->db->query($sql);
2175 $obj = $this->db->fetch_object($resql);
2176 if ($obj && $obj->quantity > 0) {
2177 if (isModEnabled(
'dynamicprices') && !empty($obj->fk_supplier_price_expression)) {
2179 $prod_supplier->product_fourn_price_id = $obj->rowid;
2180 $prod_supplier->id = $obj->fk_product;
2181 $prod_supplier->fourn_qty = $obj->quantity;
2182 $prod_supplier->fourn_tva_tx = $obj->tva_tx;
2183 $prod_supplier->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
2185 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2187 $price_result = $priceparser->parseProductSupplier($prod_supplier);
2189 $obj->price = $price_result;
2192 $this->product_fourn_price_id = $obj->rowid;
2193 $this->buyprice = $obj->price;
2194 $this->fourn_qty = $obj->quantity;
2195 $this->fourn_pu = $obj->price / $obj->quantity;
2196 $this->fourn_price_base_type =
'HT';
2197 $this->fourn_socid = $obj->fk_soc;
2198 $this->ref_fourn = $obj->ref_supplier;
2199 $this->ref_supplier = $obj->ref_supplier;
2200 $this->desc_supplier = $obj->desc_supplier;
2201 $this->remise_percent = $obj->remise_percent;
2202 $this->vatrate_supplier = $obj->tva_tx;
2203 $this->default_vat_code_supplier = $obj->default_vat_code;
2204 $this->fourn_multicurrency_price = $obj->multicurrency_price;
2205 $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
2206 $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
2207 $this->fourn_multicurrency_id = $obj->fk_multicurrency;
2208 $this->fourn_multicurrency_code = $obj->multicurrency_code;
2210 $this->packaging = $obj->packaging;
2212 $result = $obj->fk_product;
2218 $this->error = $this->db->lasterror();
2223 $this->error = $this->db->lasterror();
2246 public function updatePrice($newprice, $newpricebase, $user, $newvat =
'', $newminprice = 0, $level = 0, $newnpr = 0, $newpbq = 0, $ignore_autogen = 0, $localtaxes_array = array(), $newdefaultvatcode =
'', $notrigger = 0)
2248 global $conf, $langs;
2254 dol_syslog(get_class($this).
"::update_price id=".$id.
" newprice=".$newprice.
" newpricebase=".$newpricebase.
" newminprice=".$newminprice.
" level=".$level.
" npr=".$newnpr.
" newdefaultvatcode=".$newdefaultvatcode);
2257 if (empty($this->tva_tx)) {
2260 if (empty($newnpr)) {
2263 if (empty($newminprice)) {
2266 if (empty($newminprice)) {
2271 if ($newvat ==
'') {
2277 if ((
getDolGlobalString(
'PRODUIT_MULTIPRICES') ||
getDolGlobalString(
'PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) && !$ignore_autogen && $this->price_autogen && ($level == 1)) {
2278 return $this->
generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpbq);
2281 if (!empty($newminprice) && ($newminprice > $newprice)) {
2282 $this->error =
'ErrorPriceCantBeLowerThanMinPrice';
2286 if ($newprice !==
'' || $newprice === 0) {
2287 if ($newpricebase ==
'TTC') {
2288 $price_ttc =
price2num($newprice,
'MU');
2289 $price =
price2num($newprice) / (1 + ($newvat / 100));
2292 if ($newminprice !=
'' || $newminprice == 0) {
2293 $price_min_ttc =
price2num($newminprice,
'MU');
2294 $price_min =
price2num($newminprice) / (1 + ($newvat / 100));
2295 $price_min =
price2num($price_min,
'MU');
2302 $price_ttc = ($newnpr != 1) ? (
float)
price2num($newprice) * (1 + ($newvat / 100)) : $price;
2303 $price_ttc =
price2num($price_ttc,
'MU');
2305 if ($newminprice !==
'' || $newminprice === 0) {
2306 $price_min =
price2num($newminprice,
'MU');
2307 $price_min_ttc =
price2num($newminprice) * (1 + ($newvat / 100));
2308 $price_min_ttc =
price2num($price_min_ttc,
'MU');
2317 if (count($localtaxes_array) > 0) {
2318 $localtaxtype1 = $localtaxes_array[
'0'];
2319 $localtax1 = $localtaxes_array[
'1'];
2320 $localtaxtype2 = $localtaxes_array[
'2'];
2321 $localtax2 = $localtaxes_array[
'3'];
2324 if (!empty($newdefaultvatcode)) {
2327 $sql =
"SELECT t.rowid, t.code, t.recuperableonly as tva_npr, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
2328 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_tva as t, ".MAIN_DB_PREFIX.
"c_country as c";
2329 $sql .=
" WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($mysoc->country_code).
"'";
2330 $sql .=
" AND t.taux = ".((float) $newdefaultvatcode).
" AND t.active = 1";
2331 $sql .=
" AND t.code = '".$this->db->escape($newdefaultvatcode).
"'";
2332 $resql = $this->db->query($sql);
2334 $obj = $this->db->fetch_object($resql);
2336 $npr = $obj->tva_npr;
2337 $localtax1 = $obj->localtax1;
2338 $localtax2 = $obj->localtax2;
2339 $localtaxtype1 = $obj->localtax1_type;
2340 $localtaxtype2 = $obj->localtax2_type;
2345 $localtaxtype1 =
'0';
2347 $localtaxtype2 =
'0';
2351 if (empty($localtax1)) {
2354 if (empty($localtax2)) {
2362 $sql =
"UPDATE ".$this->db->prefix().
"product SET";
2363 $sql .=
" price_base_type = '".$this->db->escape($newpricebase).
"',";
2364 $sql .=
" price = ".(float) $price.
",";
2365 $sql .=
" price_ttc = ".(float) $price_ttc.
",";
2366 $sql .=
" price_min = ".(float) $price_min.
",";
2367 $sql .=
" price_min_ttc = ".(float) $price_min_ttc.
",";
2368 $sql .=
" localtax1_tx = ".($localtax1 >= 0 ? (float) $localtax1 :
'NULL').
",";
2369 $sql .=
" localtax2_tx = ".($localtax2 >= 0 ? (float) $localtax2 :
'NULL').
",";
2370 $sql .=
" localtax1_type = ".($localtaxtype1 !=
'' ?
"'".$this->db->escape($localtaxtype1).
"'" :
"'0'").
",";
2371 $sql .=
" localtax2_type = ".($localtaxtype2 !=
'' ?
"'".$this->db->escape($localtaxtype2).
"'" :
"'0'").
",";
2372 $sql .=
" default_vat_code = ".($newdefaultvatcode ?
"'".$this->db->escape($newdefaultvatcode).
"'" :
"null").
",";
2373 $sql .=
" tva_tx = ".(float)
price2num($newvat).
",";
2374 $sql .=
" recuperableonly = '".$this->db->escape($newnpr).
"'";
2375 $sql .=
" WHERE rowid = ".((int) $id);
2377 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
2378 $resql = $this->db->query($sql);
2380 $this->multiprices[$level] = $price;
2381 $this->multiprices_ttc[$level] = $price_ttc;
2382 $this->multiprices_min[$level] = $price_min;
2383 $this->multiprices_min_ttc[$level] = $price_min_ttc;
2384 $this->multiprices_base_type[$level] = $newpricebase;
2385 $this->multiprices_default_vat_code[$level] = $newdefaultvatcode;
2386 $this->multiprices_tva_tx[$level] = $newvat;
2387 $this->multiprices_recuperableonly[$level] = $newnpr;
2389 $this->
price = $price;
2390 $this->price_ttc = $price_ttc;
2391 $this->price_min = $price_min;
2392 $this->price_min_ttc = $price_min_ttc;
2393 $this->price_base_type = $newpricebase;
2394 $this->default_vat_code = $newdefaultvatcode;
2395 $this->tva_tx = $newvat;
2396 $this->tva_npr = $newnpr;
2398 $this->localtax1_tx = $localtax1;
2399 $this->localtax2_tx = $localtax2;
2400 $this->localtax1_type = $localtaxtype1;
2401 $this->localtax2_type = $localtaxtype2;
2404 $this->price_by_qty = $newpbq;
2408 if (!empty(array_diff_assoc($newPriceData, $lastPriceData)) || !
getDolGlobalString(
'PRODUIT_MULTIPRICES')) {
2412 $this->level = $level;
2416 $result = $this->
call_trigger(
'PRODUCT_PRICE_MODIFY', $user);
2418 $this->db->rollback();
2424 $this->db->commit();
2426 $this->db->rollback();
2427 $this->error = $this->db->lasterror();
2446 $this->fk_price_expression = $expression_id;
2448 return $this->
update($this->
id, $user);
2463 public function fetch($id =
'', $ref =
'', $ref_ext =
'', $barcode =
'', $ignore_expression = 0, $ignore_price_load = 0, $ignore_lang_load = 0)
2465 include_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
2467 global $langs, $conf;
2469 dol_syslog(get_class($this).
"::fetch id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
2472 if (!$id && !$ref && !$ref_ext && !$barcode) {
2473 $this->error =
'ErrorWrongParameters';
2474 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
2478 $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,";
2479 $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,";
2480 $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,";
2481 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2482 $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,";
2484 $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,";
2486 $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,";
2491 $separatedEntityPMP =
false;
2492 $separatedStock =
false;
2493 $visibleWarehousesEntities = $conf->entity;
2496 $checkPMPPerEntity = $this->db->query(
"SELECT pmp FROM " . $this->db->prefix() .
"product_perentity WHERE fk_product = ".((int) $id).
" AND entity = ".(
int) $conf->entity);
2497 if ($this->db->num_rows($checkPMPPerEntity)>0) {
2498 $separatedEntityPMP =
true;
2502 $separatedStock =
true;
2503 if (isset($mc->sharings[
'stock']) && !empty($mc->sharings[
'stock'])) {
2504 $visibleWarehousesEntities .=
"," . implode(
",", $mc->sharings[
'stock']);
2507 if ($separatedEntityPMP) {
2508 $sql .=
" ppe.pmp,";
2512 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2513 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf,";
2514 if ($separatedStock) {
2515 $sql .=
" SUM(sp.reel) as stock";
2519 $sql .=
" FROM ".$this->db->prefix().
"product as p";
2521 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
2523 if ($separatedStock) {
2524 $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).
"))";
2528 $sql .=
" WHERE p.rowid = ".((int) $id);
2530 $sql .=
" WHERE p.entity IN (".getEntity($this->element).
")";
2532 $sql .=
" AND p.ref = '".$this->db->escape($ref).
"'";
2533 } elseif ($ref_ext) {
2534 $sql .=
" AND p.ref_ext = '".$this->db->escape($ref_ext).
"'";
2535 } elseif ($barcode) {
2536 $sql .=
" AND p.barcode = '".$this->db->escape($barcode).
"'";
2539 if ($separatedStock) {
2540 $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,";
2541 $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,";
2542 $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,";
2543 $sql .=
" p.length, p.length_units, p.width, p.width_units, p.height, p.height_units,";
2544 $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,";
2546 $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,";
2548 $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,";
2550 if ($separatedEntityPMP) {
2551 $sql .=
" ppe.pmp,";
2555 $sql .=
" p.datec, p.tms, p.import_key, p.entity, p.desiredstock, p.tobatch, p.batch_mask, p.fk_unit,";
2556 $sql .=
" p.fk_price_expression, p.price_autogen, p.model_pdf";
2557 if (!$separatedStock) {
2558 $sql .=
", p.stock";
2562 $resql = $this->db->query($sql);
2564 unset($this->oldcopy);
2566 if ($this->db->num_rows($resql) > 0) {
2567 $obj = $this->db->fetch_object($resql);
2569 $this->
id = $obj->rowid;
2570 $this->
ref = $obj->ref;
2571 $this->ref_ext = $obj->ref_ext;
2572 $this->label = $obj->label;
2574 $this->url = $obj->url;
2575 $this->note_public = $obj->note_public;
2576 $this->note_private = $obj->note_private;
2577 $this->note = $obj->note_private;
2579 $this->
type = $obj->fk_product_type;
2580 $this->
status = $obj->tosell;
2581 $this->status_buy = $obj->tobuy;
2582 $this->status_batch = $obj->tobatch;
2583 $this->batch_mask = $obj->batch_mask;
2585 $this->customcode = $obj->customcode;
2586 $this->country_id = $obj->fk_country;
2587 $this->country_code =
getCountry($this->country_id, 2, $this->db);
2588 $this->state_id = $obj->fk_state;
2589 $this->lifetime = $obj->lifetime;
2590 $this->qc_frequency = $obj->qc_frequency;
2591 $this->
price = $obj->price;
2592 $this->price_ttc = $obj->price_ttc;
2593 $this->price_min = $obj->price_min;
2594 $this->price_min_ttc = $obj->price_min_ttc;
2595 $this->price_base_type = $obj->price_base_type;
2596 $this->cost_price = $obj->cost_price;
2597 $this->default_vat_code = $obj->default_vat_code;
2598 $this->tva_tx = $obj->tva_tx;
2600 $this->tva_npr = $obj->tva_npr;
2602 $this->localtax1_tx = $obj->localtax1_tx;
2603 $this->localtax2_tx = $obj->localtax2_tx;
2604 $this->localtax1_type = $obj->localtax1_type;
2605 $this->localtax2_type = $obj->localtax2_type;
2607 $this->finished = $obj->finished;
2608 $this->fk_default_bom = $obj->fk_default_bom;
2610 $this->duration = $obj->duration;
2611 $this->duration_value = $obj->duration ? (int) (substr($obj->duration, 0,
dol_strlen($obj->duration) - 1)) : 0;
2612 $this->duration_unit = $obj->duration ? substr($obj->duration, -1) :
null;
2613 $this->canvas = $obj->canvas;
2614 $this->net_measure = $obj->net_measure;
2615 $this->net_measure_units = $obj->net_measure_units;
2616 $this->weight = $obj->weight;
2617 $this->weight_units = $obj->weight_units;
2618 $this->length = $obj->length;
2619 $this->length_units = $obj->length_units;
2620 $this->width = $obj->width;
2621 $this->width_units = $obj->width_units;
2622 $this->height = $obj->height;
2623 $this->height_units = $obj->height_units;
2625 $this->surface = $obj->surface;
2626 $this->surface_units = $obj->surface_units;
2627 $this->volume = $obj->volume;
2628 $this->volume_units = $obj->volume_units;
2629 $this->barcode = $obj->barcode;
2630 $this->barcode_type = $obj->fk_barcode_type;
2632 $this->accountancy_code_buy = $obj->accountancy_code_buy;
2633 $this->accountancy_code_buy_intra = $obj->accountancy_code_buy_intra;
2634 $this->accountancy_code_buy_export = $obj->accountancy_code_buy_export;
2635 $this->accountancy_code_sell = $obj->accountancy_code_sell;
2636 $this->accountancy_code_sell_intra = $obj->accountancy_code_sell_intra;
2637 $this->accountancy_code_sell_export = $obj->accountancy_code_sell_export;
2639 $this->fk_default_warehouse = $obj->fk_default_warehouse;
2640 $this->fk_default_workstation = $obj->fk_default_workstation;
2641 $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
2642 $this->desiredstock = $obj->desiredstock;
2643 $this->stock_reel = $obj->stock;
2644 $this->pmp = $obj->pmp;
2646 $this->date_creation = $obj->datec;
2647 $this->date_modification = $obj->tms;
2648 $this->import_key = $obj->import_key;
2649 $this->entity = $obj->entity;
2651 $this->ref_ext = $obj->ref_ext;
2652 $this->fk_price_expression = $obj->fk_price_expression;
2653 $this->fk_unit = $obj->fk_unit;
2654 $this->price_autogen = $obj->price_autogen;
2655 $this->model_pdf = $obj->model_pdf;
2657 $this->mandatory_period = $obj->mandatory_period;
2659 $this->db->free($resql);
2671 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2672 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2673 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2674 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2675 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2676 $sql .=
" AND price_level=".((int) $i);
2677 $sql .=
" AND fk_product = ".((int) $this->
id);
2678 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2680 $resql = $this->db->query($sql);
2682 $result = $this->db->fetch_array($resql);
2684 $this->multiprices[$i] = $result ? $result[
"price"] :
null;
2685 $this->multiprices_ttc[$i] = $result ? $result[
"price_ttc"] :
null;
2686 $this->multiprices_min[$i] = $result ? $result[
"price_min"] :
null;
2687 $this->multiprices_min_ttc[$i] = $result ? $result[
"price_min_ttc"] :
null;
2688 $this->multiprices_base_type[$i] = $result ? $result[
"price_base_type"] :
null;
2690 $this->multiprices_tva_tx[$i] = $result ? $result[
"tva_tx"].($result ?
' ('.$result[
'default_vat_code'].
')' :
'') :
null;
2691 $this->multiprices_recuperableonly[$i] = $result ? $result[
"recuperableonly"] :
null;
2729 $this->error = $this->db->lasterror;
2733 } elseif (
getDolGlobalString(
'PRODUIT_CUSTOMER_PRICES') && empty($ignore_price_load)) {
2735 } elseif (
getDolGlobalString(
'PRODUIT_CUSTOMER_PRICES_BY_QTY') && empty($ignore_price_load)) {
2736 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2737 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid";
2738 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2739 $sql .=
" WHERE fk_product = ".((int) $this->
id);
2740 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2742 $resql = $this->db->query($sql);
2744 $result = $this->db->fetch_array($resql);
2747 $this->prices_by_qty[0] = $result[
"price_by_qty"];
2748 $this->prices_by_qty_id[0] = $result[
"rowid"];
2750 if ($this->prices_by_qty[0] == 1) {
2751 $sql =
"SELECT rowid,price, unitprice, quantity, remise_percent, remise, remise, price_base_type";
2752 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2753 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[0]);
2754 $sql .=
" ORDER BY quantity ASC";
2755 $resultat = array();
2756 $resql = $this->db->query($sql);
2759 while ($result = $this->db->fetch_array($resql)) {
2760 $resultat[$ii] = array();
2761 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2762 $resultat[$ii][
"price"] = $result[
"price"];
2763 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2764 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2765 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2767 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2770 $this->prices_by_qty_list[0] = $resultat;
2772 $this->error = $this->db->lasterror;
2777 $this->error = $this->db->lasterror;
2780 } elseif (
getDolGlobalString(
'PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') && empty($ignore_price_load)) {
2781 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
2782 $sql =
"SELECT price, price_ttc, price_min, price_min_ttc,";
2783 $sql .=
" price_base_type, tva_tx, default_vat_code, tosell, price_by_qty, rowid, recuperableonly";
2784 $sql .=
" FROM ".$this->db->prefix().
"product_price";
2785 $sql .=
" WHERE entity IN (".getEntity(
'productprice').
")";
2786 $sql .=
" AND price_level=".((int) $i);
2787 $sql .=
" AND fk_product = ".((int) $this->
id);
2788 $sql .=
" ORDER BY date_price DESC, rowid DESC";
2790 $resql = $this->db->query($sql);
2792 $result = $this->db->fetch_array($resql);
2794 $this->multiprices[$i] = (!empty($result[
"price"]) ? $result[
"price"] : 0);
2795 $this->multiprices_ttc[$i] = (!empty($result[
"price_ttc"]) ? $result[
"price_ttc"] : 0);
2796 $this->multiprices_min[$i] = (!empty($result[
"price_min"]) ? $result[
"price_min"] : 0);
2797 $this->multiprices_min_ttc[$i] = (!empty($result[
"price_min_ttc"]) ? $result[
"price_min_ttc"] : 0);
2798 $this->multiprices_base_type[$i] = (!empty($result[
"price_base_type"]) ? $result[
"price_base_type"] :
'');
2800 $this->multiprices_tva_tx[$i] = (!empty($result[
"tva_tx"]) ? $result[
"tva_tx"] : 0);
2801 $this->multiprices_recuperableonly[$i] = (!empty($result[
"recuperableonly"]) ? $result[
"recuperableonly"] : 0);
2804 $this->prices_by_qty[$i] = (!empty($result[
"price_by_qty"]) ? $result[
"price_by_qty"] : 0);
2805 $this->prices_by_qty_id[$i] = (!empty($result[
"rowid"]) ? $result[
"rowid"] : 0);
2807 if ($this->prices_by_qty[$i] == 1) {
2808 $sql =
"SELECT rowid, price, unitprice, quantity, remise_percent, remise, price_base_type";
2809 $sql .=
" FROM ".$this->db->prefix().
"product_price_by_qty";
2810 $sql .=
" WHERE fk_product_price = ".((int) $this->prices_by_qty_id[$i]);
2811 $sql .=
" ORDER BY quantity ASC";
2812 $resultat = array();
2813 $resql = $this->db->query($sql);
2816 while ($result = $this->db->fetch_array($resql)) {
2817 $resultat[$ii] = array();
2818 $resultat[$ii][
"rowid"] = $result[
"rowid"];
2819 $resultat[$ii][
"price"] = $result[
"price"];
2820 $resultat[$ii][
"unitprice"] = $result[
"unitprice"];
2821 $resultat[$ii][
"quantity"] = $result[
"quantity"];
2822 $resultat[$ii][
"remise_percent"] = $result[
"remise_percent"];
2823 $resultat[$ii][
"remise"] = $result[
"remise"];
2824 $resultat[$ii][
"price_base_type"] = $result[
"price_base_type"];
2827 $this->prices_by_qty_list[$i] = $resultat;
2829 $this->error = $this->db->lasterror;
2834 $this->error = $this->db->lasterror;
2840 if (isModEnabled(
'dynamicprices') && !empty($this->fk_price_expression) && empty($ignore_expression)) {
2841 include_once DOL_DOCUMENT_ROOT.
'/product/dynamic_price/class/price_parser.class.php';
2843 $price_result = $priceparser->parseProduct($this);
2844 if ($price_result >= 0) {
2845 $this->
price = $price_result;
2847 $this->price_ttc =
price2num($this->
price) * (1 + ($this->tva_tx / 100));
2848 $this->price_ttc =
price2num($this->price_ttc,
'MU');
2854 $this->stock_warehouse = array();
2861 $this->error = $this->db->lasterror();
2876 global $user, $hookmanager, $action;
2880 foreach (array(
'toconsume',
'consumed',
'toproduce',
'produced') as $role) {
2881 $this->stats_mo[
'customers_'.$role] = 0;
2882 $this->stats_mo[
'nb_'.$role] = 0;
2883 $this->stats_mo[
'qty_'.$role] = 0;
2885 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
2886 $sql .=
" SUM(mp.qty) as qty";
2887 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as c";
2888 $sql .=
" INNER JOIN ".$this->db->prefix().
"mrp_production as mp ON mp.fk_mo=c.rowid";
2889 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2890 $sql .=
" INNER JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON sc.fk_soc=c.fk_soc AND sc.fk_user = ".((int) $user->id);
2893 $sql .=
" c.entity IN (".getEntity(
'mo').
")";
2895 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
2896 $sql .=
" AND mp.role ='".$this->db->escape($role).
"'";
2898 $sql .=
" AND c.fk_soc = ".((int) $socid);
2901 $result = $this->db->query($sql);
2903 $obj = $this->db->fetch_object($result);
2904 $this->stats_mo[
'customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
2905 $this->stats_mo[
'nb_'.$role] = $obj->nb ? $obj->nb : 0;
2906 $this->stats_mo[
'qty_'.$role] = $obj->qty ?
price2num($obj->qty,
'MS') : 0;
2908 $this->error = $this->db->error();
2913 if (!empty($error)) {
2917 $parameters = array(
'socid' => $socid);
2918 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2920 $this->stats_mo = $hookmanager->resArray[
'stats_mo'];
2936 global $user, $hookmanager, $action;
2940 $this->stats_bom[
'nb_toproduce'] = 0;
2941 $this->stats_bom[
'nb_toconsume'] = 0;
2942 $this->stats_bom[
'qty_toproduce'] = 0;
2943 $this->stats_bom[
'qty_toconsume'] = 0;
2945 $sql =
"SELECT COUNT(DISTINCT b.rowid) as nb_toproduce,";
2946 $sql .=
" SUM(b.qty) as qty_toproduce";
2947 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2948 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2950 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2951 $sql .=
" AND b.fk_product =".((int) $this->
id);
2952 $sql .=
" GROUP BY b.rowid";
2954 $result = $this->db->query($sql);
2956 $obj = $this->db->fetch_object($result);
2957 $this->stats_bom[
'nb_toproduce'] = !empty($obj->nb_toproduce) ? $obj->nb_toproduce : 0;
2958 $this->stats_bom[
'qty_toproduce'] = !empty($obj->qty_toproduce) ?
price2num($obj->qty_toproduce) : 0;
2960 $this->error = $this->db->error();
2964 $sql =
"SELECT COUNT(DISTINCT bl.rowid) as nb_toconsume,";
2965 $sql .=
" SUM(bl.qty) as qty_toconsume";
2966 $sql .=
" FROM ".$this->db->prefix().
"bom_bom as b";
2967 $sql .=
" INNER JOIN ".$this->db->prefix().
"bom_bomline as bl ON bl.fk_bom=b.rowid";
2969 $sql .=
" b.entity IN (".getEntity(
'bom').
")";
2970 $sql .=
" AND bl.fk_product =".((int) $this->
id);
2972 $result = $this->db->query($sql);
2974 $obj = $this->db->fetch_object($result);
2975 $this->stats_bom[
'nb_toconsume'] = !empty($obj->nb_toconsume) ? $obj->nb_toconsume : 0;
2976 $this->stats_bom[
'qty_toconsume'] = !empty($obj->qty_toconsume) ?
price2num($obj->qty_toconsume) : 0;
2978 $this->error = $this->db->error();
2982 if (!empty($error)) {
2986 $parameters = array(
'socid' => $socid);
2987 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerMO', $parameters, $this, $action);
2989 $this->stats_bom = $hookmanager->resArray[
'stats_bom'];
3005 global $conf, $user, $hookmanager, $action;
3007 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_customers, COUNT(DISTINCT p.rowid) as nb,";
3008 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
3009 $sql .=
" FROM ".$this->db->prefix().
"propaldet as pd";
3010 $sql .=
", ".$this->db->prefix().
"propal as p";
3011 $sql .=
", ".$this->db->prefix().
"societe as s";
3012 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3013 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3015 $sql .=
" WHERE p.rowid = pd.fk_propal";
3016 $sql .=
" AND p.fk_soc = s.rowid";
3017 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
3018 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
3019 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3020 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3024 $sql .=
" AND p.fk_soc = ".((int) $socid);
3027 $result = $this->db->query($sql);
3029 $obj = $this->db->fetch_object($result);
3030 $this->stats_propale[
'customers'] = $obj->nb_customers;
3031 $this->stats_propale[
'nb'] = $obj->nb;
3032 $this->stats_propale[
'rows'] = $obj->nb_rows;
3033 $this->stats_propale[
'qty'] = $obj->qty ? $obj->qty : 0;
3038 if (is_array($TFather) && !empty($TFather)) {
3039 foreach ($TFather as &$fatherData) {
3040 $pFather =
new Product($this->db);
3041 $pFather->id = $fatherData[
'id'];
3042 $qtyCoef = $fatherData[
'qty'];
3044 if ($fatherData[
'incdec']) {
3045 $pFather->load_stats_propale($socid);
3047 $this->stats_propale[
'customers'] += $pFather->stats_propale[
'customers'];
3048 $this->stats_propale[
'nb'] += $pFather->stats_propale[
'nb'];
3049 $this->stats_propale[
'rows'] += $pFather->stats_propale[
'rows'];
3050 $this->stats_propale[
'qty'] += $pFather->stats_propale[
'qty'] * $qtyCoef;
3056 $parameters = array(
'socid' => $socid);
3057 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerProposal', $parameters, $this, $action);
3059 $this->stats_propale = $hookmanager->resArray[
'stats_propale'];
3064 $this->error = $this->db->error();
3080 global $conf, $user, $hookmanager, $action;
3082 $sql =
"SELECT COUNT(DISTINCT p.fk_soc) as nb_suppliers, COUNT(DISTINCT p.rowid) as nb,";
3083 $sql .=
" COUNT(pd.rowid) as nb_rows, SUM(pd.qty) as qty";
3084 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as pd";
3085 $sql .=
", ".$this->db->prefix().
"supplier_proposal as p";
3086 $sql .=
", ".$this->db->prefix().
"societe as s";
3087 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3088 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3090 $sql .=
" WHERE p.rowid = pd.fk_supplier_proposal";
3091 $sql .=
" AND p.fk_soc = s.rowid";
3092 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
3093 $sql .=
" AND pd.fk_product = ".((int) $this->
id);
3094 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3095 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3099 $sql .=
" AND p.fk_soc = ".((int) $socid);
3102 $result = $this->db->query($sql);
3104 $obj = $this->db->fetch_object($result);
3105 $this->stats_proposal_supplier[
'suppliers'] = $obj->nb_suppliers;
3106 $this->stats_proposal_supplier[
'nb'] = $obj->nb;
3107 $this->stats_proposal_supplier[
'rows'] = $obj->nb_rows;
3108 $this->stats_proposal_supplier[
'qty'] = $obj->qty ? $obj->qty : 0;
3110 $parameters = array(
'socid' => $socid);
3111 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierProposal', $parameters, $this, $action);
3113 $this->stats_proposal_supplier = $hookmanager->resArray[
'stats_proposal_supplier'];
3118 $this->error = $this->db->error();
3136 global $conf, $user, $hookmanager, $action;
3138 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3139 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3140 $sql .=
" FROM ".$this->db->prefix().
"commandedet as cd";
3141 $sql .=
", ".$this->db->prefix().
"commande as c";
3142 $sql .=
", ".$this->db->prefix().
"societe as s";
3143 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3144 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3146 $sql .=
" WHERE c.rowid = cd.fk_commande";
3147 $sql .=
" AND c.fk_soc = s.rowid";
3148 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock &&
getDolGlobalString(
'STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE') ?
'stock' :
'commande').
")";
3149 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3150 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3151 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3154 $sql .=
" AND c.fk_soc = ".((int) $socid);
3156 if ($filtrestatut !=
'') {
3157 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3160 $result = $this->db->query($sql);
3162 $obj = $this->db->fetch_object($result);
3163 $this->stats_commande[
'customers'] = $obj->nb_customers;
3164 $this->stats_commande[
'nb'] = $obj->nb;
3165 $this->stats_commande[
'rows'] = $obj->nb_rows;
3166 $this->stats_commande[
'qty'] = $obj->qty ? $obj->qty : 0;
3171 if (is_array($TFather) && !empty($TFather)) {
3172 foreach ($TFather as &$fatherData) {
3173 $pFather =
new Product($this->db);
3174 $pFather->id = $fatherData[
'id'];
3175 $qtyCoef = $fatherData[
'qty'];
3177 if ($fatherData[
'incdec']) {
3178 $pFather->load_stats_commande($socid, $filtrestatut);
3180 $this->stats_commande[
'customers'] += $pFather->stats_commande[
'customers'];
3181 $this->stats_commande[
'nb'] += $pFather->stats_commande[
'nb'];
3182 $this->stats_commande[
'rows'] += $pFather->stats_commande[
'rows'];
3183 $this->stats_commande[
'qty'] += $pFather->stats_commande[
'qty'] * $qtyCoef;
3195 $sql =
"SELECT SUM(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".$this->db->prefix().
"facturedet as fd ";
3196 $sql .=
" JOIN ".$this->db->prefix().
"facture as f ON fd.fk_facture = f.rowid";
3197 $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'))";
3198 $sql .=
" JOIN ".$this->db->prefix().
"commande as c ON el.fk_source = c.rowid";
3199 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND c.facture = 0 AND fd.fk_product = ".((int) $this->
id);
3201 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3202 $resql = $this->db->query($sql);
3204 if ($this->db->num_rows($resql) > 0) {
3205 $obj = $this->db->fetch_object($resql);
3206 $adeduire += $obj->count;
3210 $this->stats_commande[
'qty'] -= $adeduire;
3213 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
3217 $sql =
"SELECT sum(".$this->db->ifsql(
'f.type=2', -1, 1).
" * fd.qty) as count FROM ".MAIN_DB_PREFIX.
"facturedet as fd ";
3218 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture as f ON fd.fk_facture = f.rowid";
3219 $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'))";
3220 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c ON el.fk_source = c.rowid";
3221 $sql .=
" WHERE c.fk_statut IN (".$this->db->sanitize($filtrestatut).
") AND f.fk_statut > ".
Facture::STATUS_DRAFT.
" AND fd.fk_product = ".((int) $this->
id);
3223 dol_syslog(__METHOD__.
":: sql $sql", LOG_NOTICE);
3224 $resql = $this->db->query($sql);
3226 if ($this->db->num_rows($resql) > 0) {
3227 $obj = $this->db->fetch_object($resql);
3228 $adeduire += $obj->count;
3231 $this->error = $this->db->error();
3235 $this->stats_commande[
'qty'] -= $adeduire;
3239 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3240 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerOrder', $parameters, $this, $action);
3242 $this->stats_commande = $hookmanager->resArray[
'stats_commande'];
3246 $this->error = $this->db->error();
3264 global $conf, $user, $hookmanager, $action;
3266 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_suppliers, COUNT(DISTINCT c.rowid) as nb,";
3267 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3268 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as cd";
3269 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as c";
3270 $sql .=
", ".$this->db->prefix().
"societe as s";
3271 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3272 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3274 $sql .=
" WHERE c.rowid = cd.fk_commande";
3275 $sql .=
" AND c.fk_soc = s.rowid";
3276 $sql .=
" AND c.entity IN (".getEntity($forVirtualStock &&
getDolGlobalString(
'STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE') ?
'stock' :
'supplier_order').
")";
3277 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3278 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3279 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3282 $sql .=
" AND c.fk_soc = ".((int) $socid);
3284 if ($filtrestatut !=
'') {
3285 $sql .=
" AND c.fk_statut in (".$this->db->sanitize($filtrestatut).
")";
3287 if (!empty($dateofvirtualstock)) {
3288 $sql .=
" AND c.date_livraison <= '".$this->db->idate($dateofvirtualstock).
"'";
3291 $result = $this->db->query($sql);
3293 $obj = $this->db->fetch_object($result);
3294 $this->stats_commande_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3295 $this->stats_commande_fournisseur[
'nb'] = $obj->nb;
3296 $this->stats_commande_fournisseur[
'rows'] = $obj->nb_rows;
3297 $this->stats_commande_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3299 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3300 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierOrder', $parameters, $this, $action);
3302 $this->stats_commande_fournisseur = $hookmanager->resArray[
'stats_commande_fournisseur'];
3307 $this->error = $this->db->error().
' sql='.$sql;
3322 public function load_stats_sending($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $filterShipmentStatus =
'')
3325 global $conf, $user, $hookmanager, $action;
3327 $sql =
"SELECT COUNT(DISTINCT e.fk_soc) as nb_customers, COUNT(DISTINCT e.rowid) as nb,";
3328 $sql .=
" COUNT(ed.rowid) as nb_rows, SUM(ed.qty) as qty";
3329 $sql .=
" FROM ".$this->db->prefix().
"expeditiondet as ed";
3330 $sql .=
", ".$this->db->prefix().
"commandedet as cd";
3331 $sql .=
", ".$this->db->prefix().
"commande as c";
3332 $sql .=
", ".$this->db->prefix().
"expedition as e";
3333 $sql .=
", ".$this->db->prefix().
"societe as s";
3334 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3335 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3337 $sql .=
" WHERE e.rowid = ed.fk_expedition";
3338 $sql .=
" AND c.rowid = cd.fk_commande";
3339 $sql .=
" AND e.fk_soc = s.rowid";
3340 $sql .=
" AND e.entity IN (".getEntity($forVirtualStock &&
getDolGlobalString(
'STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE') ?
'stock' :
'expedition').
")";
3341 $sql .=
" AND ed.fk_origin_line = cd.rowid";
3342 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3343 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3344 $sql .=
" AND e.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3347 $sql .=
" AND e.fk_soc = ".((int) $socid);
3349 if ($filtrestatut !=
'') {
3350 $sql .=
" AND c.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3352 if (!empty($filterShipmentStatus)) {
3353 $sql .=
" AND e.fk_statut IN (".$this->db->sanitize($filterShipmentStatus).
")";
3356 $result = $this->db->query($sql);
3358 $obj = $this->db->fetch_object($result);
3359 $this->stats_expedition[
'customers'] = $obj->nb_customers;
3360 $this->stats_expedition[
'nb'] = $obj->nb;
3361 $this->stats_expedition[
'rows'] = $obj->nb_rows;
3362 $this->stats_expedition[
'qty'] = $obj->qty ? $obj->qty : 0;
3367 if (is_array($TFather) && !empty($TFather)) {
3368 foreach ($TFather as &$fatherData) {
3369 $pFather =
new Product($this->db);
3370 $pFather->id = $fatherData[
'id'];
3371 $qtyCoef = $fatherData[
'qty'];
3373 if ($fatherData[
'incdec']) {
3374 $pFather->load_stats_sending($socid, $filtrestatut, $forVirtualStock);
3376 $this->stats_expedition[
'customers'] += $pFather->stats_expedition[
'customers'];
3377 $this->stats_expedition[
'nb'] += $pFather->stats_expedition[
'nb'];
3378 $this->stats_expedition[
'rows'] += $pFather->stats_expedition[
'rows'];
3379 $this->stats_expedition[
'qty'] += $pFather->stats_expedition[
'qty'] * $qtyCoef;
3385 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock,
'filterShipmentStatus' => $filterShipmentStatus);
3386 $reshook = $hookmanager->executeHooks(
'loadStatsSending', $parameters, $this, $action);
3388 $this->stats_expedition = $hookmanager->resArray[
'stats_expedition'];
3393 $this->error = $this->db->error();
3408 public function load_stats_reception($socid = 0, $filtrestatut =
'', $forVirtualStock = 0, $dateofvirtualstock =
null)
3411 global $conf, $user, $hookmanager, $action;
3413 $sql =
"SELECT COUNT(DISTINCT cf.fk_soc) as nb_suppliers, COUNT(DISTINCT cf.rowid) as nb,";
3414 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3415 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseur_dispatch as fd";
3416 $sql .=
", ".$this->db->prefix().
"commande_fournisseur as cf";
3417 $sql .=
", ".$this->db->prefix().
"societe as s";
3418 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3419 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3421 $sql .=
" WHERE cf.rowid = fd.fk_commande";
3422 $sql .=
" AND cf.fk_soc = s.rowid";
3423 $sql .=
" AND cf.entity IN (".getEntity($forVirtualStock &&
getDolGlobalString(
'STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE') ?
'stock' :
'supplier_order').
")";
3424 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3425 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3426 $sql .=
" AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3429 $sql .=
" AND cf.fk_soc = ".((int) $socid);
3431 if ($filtrestatut !=
'') {
3432 $sql .=
" AND cf.fk_statut IN (".$this->db->sanitize($filtrestatut).
")";
3434 if (!empty($dateofvirtualstock)) {
3435 $sql .=
" AND fd.datec <= '".$this->db->idate($dateofvirtualstock).
"'";
3438 $result = $this->db->query($sql);
3440 $obj = $this->db->fetch_object($result);
3441 $this->stats_reception[
'suppliers'] = $obj->nb_suppliers;
3442 $this->stats_reception[
'nb'] = $obj->nb;
3443 $this->stats_reception[
'rows'] = $obj->nb_rows;
3444 $this->stats_reception[
'qty'] = $obj->qty ? $obj->qty : 0;
3446 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3447 $reshook = $hookmanager->executeHooks(
'loadStatsReception', $parameters, $this, $action);
3449 $this->stats_reception = $hookmanager->resArray[
'stats_reception'];
3454 $this->error = $this->db->error();
3472 global $conf, $user, $hookmanager, $action;
3474 $serviceStockIsEnabled = isModEnabled(
"service") &&
getDolGlobalString(
'STOCK_SUPPORTS_SERVICES');
3476 $sql =
"SELECT COUNT(DISTINCT m.fk_soc) as nb_customers, COUNT(DISTINCT m.rowid) as nb,";
3477 $sql .=
" COUNT(mp.rowid) as nb_rows, SUM(mp.qty) as qty, role";
3478 $sql .=
" FROM ".$this->db->prefix().
"mrp_production as mp";
3479 $sql .=
", ".$this->db->prefix().
"mrp_mo as m";
3480 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON s.rowid = m.fk_soc";
3481 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3482 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3484 $sql .=
" WHERE m.rowid = mp.fk_mo";
3485 $sql .=
" AND m.entity IN (".getEntity($forVirtualStock &&
getDolGlobalString(
'STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE') ?
'stock' :
'mrp').
")";
3486 $sql .=
" AND mp.fk_product = ".((int) $this->
id);
3487 $sql .=
" AND (mp.disable_stock_change IN (0) OR mp.disable_stock_change IS NULL)";
3488 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid && !$forVirtualStock) {
3489 $sql .=
" AND m.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3492 $sql .=
" AND m.fk_soc = ".((int) $socid);
3494 if ($filtrestatut !=
'') {
3495 $sql .=
" AND m.status IN (".$this->db->sanitize($filtrestatut).
")";
3497 if (!empty($dateofvirtualstock)) {
3498 $sql .=
" AND m.date_valid <= '".$this->db->idate($dateofvirtualstock).
"'";
3500 if (!$serviceStockIsEnabled) {
3501 $sql .=
"AND EXISTS (SELECT p.rowid FROM ".$this->db->prefix().
"product AS p WHERE p.rowid = ".((int) $this->
id).
" AND p.fk_product_type IN (0))";
3503 $sql .=
" GROUP BY role";
3505 $this->stats_mrptoconsume[
'customers'] = 0;
3506 $this->stats_mrptoconsume[
'nb'] = 0;
3507 $this->stats_mrptoconsume[
'rows'] = 0;
3508 $this->stats_mrptoconsume[
'qty'] = 0;
3509 $this->stats_mrptoproduce[
'customers'] = 0;
3510 $this->stats_mrptoproduce[
'nb'] = 0;
3511 $this->stats_mrptoproduce[
'rows'] = 0;
3512 $this->stats_mrptoproduce[
'qty'] = 0;
3514 $result = $this->db->query($sql);
3516 while ($obj = $this->db->fetch_object($result)) {
3517 if ($obj->role ==
'toconsume') {
3518 $this->stats_mrptoconsume[
'customers'] += $obj->nb_customers;
3519 $this->stats_mrptoconsume[
'nb'] += $obj->nb;
3520 $this->stats_mrptoconsume[
'rows'] += $obj->nb_rows;
3521 $this->stats_mrptoconsume[
'qty'] += ($obj->qty ? $obj->qty : 0);
3523 if ($obj->role ==
'consumed') {
3527 $this->stats_mrptoconsume[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3529 if ($obj->role ==
'toproduce') {
3530 $this->stats_mrptoproduce[
'customers'] += $obj->nb_customers;
3531 $this->stats_mrptoproduce[
'nb'] += $obj->nb;
3532 $this->stats_mrptoproduce[
'rows'] += $obj->nb_rows;
3533 $this->stats_mrptoproduce[
'qty'] += ($obj->qty ? $obj->qty : 0);
3535 if ($obj->role ==
'produced') {
3539 $this->stats_mrptoproduce[
'qty'] -= ($obj->qty ? $obj->qty : 0);
3544 if ($this->stats_mrptoconsume[
'qty'] < 0) {
3545 $this->stats_mrptoconsume[
'qty'] = 0;
3547 if ($this->stats_mrptoproduce[
'qty'] < 0) {
3548 $this->stats_mrptoproduce[
'qty'] = 0;
3551 $parameters = array(
'socid' => $socid,
'filtrestatut' => $filtrestatut,
'forVirtualStock' => $forVirtualStock);
3552 $reshook = $hookmanager->executeHooks(
'loadStatsInProduction', $parameters, $this, $action);
3554 $this->stats_mrptoproduce = $hookmanager->resArray[
'stats_mrptoproduce'];
3559 $this->error = $this->db->error();
3574 global $conf, $user, $hookmanager, $action;
3576 $sql =
"SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
3577 $sql .=
" COUNT(cd.rowid) as nb_rows, SUM(cd.qty) as qty";
3578 $sql .=
" FROM ".$this->db->prefix().
"contratdet as cd";
3579 $sql .=
", ".$this->db->prefix().
"contrat as c";
3580 $sql .=
", ".$this->db->prefix().
"societe as s";
3581 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3582 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3584 $sql .=
" WHERE c.rowid = cd.fk_contrat";
3585 $sql .=
" AND c.fk_soc = s.rowid";
3586 $sql .=
" AND c.entity IN (".getEntity(
'contract').
")";
3587 $sql .=
" AND cd.fk_product = ".((int) $this->
id);
3588 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3589 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3593 $sql .=
" AND c.fk_soc = ".((int) $socid);
3596 $result = $this->db->query($sql);
3598 $obj = $this->db->fetch_object($result);
3599 $this->stats_contrat[
'customers'] = $obj->nb_customers;
3600 $this->stats_contrat[
'nb'] = $obj->nb;
3601 $this->stats_contrat[
'rows'] = $obj->nb_rows;
3602 $this->stats_contrat[
'qty'] = $obj->qty ? $obj->qty : 0;
3607 if (is_array($TFather) && !empty($TFather)) {
3608 foreach ($TFather as &$fatherData) {
3609 $pFather =
new Product($this->db);
3610 $pFather->id = $fatherData[
'id'];
3611 $qtyCoef = $fatherData[
'qty'];
3613 if ($fatherData[
'incdec']) {
3614 $pFather->load_stats_contrat($socid);
3616 $this->stats_contrat[
'customers'] += $pFather->stats_contrat[
'customers'];
3617 $this->stats_contrat[
'nb'] += $pFather->stats_contrat[
'nb'];
3618 $this->stats_contrat[
'rows'] += $pFather->stats_contrat[
'rows'];
3619 $this->stats_contrat[
'qty'] += $pFather->stats_contrat[
'qty'] * $qtyCoef;
3625 $parameters = array(
'socid' => $socid);
3626 $reshook = $hookmanager->executeHooks(
'loadStatsContract', $parameters, $this, $action);
3628 $this->stats_contrat = $hookmanager->resArray[
'stats_contrat'];
3633 $this->error = $this->db->error().
' sql='.$sql;
3648 global $conf, $user, $hookmanager, $action;
3650 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3651 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(".$this->db->ifsql(
'f.type != 2',
'fd.qty',
'fd.qty * -1').
") as qty";
3652 $sql .=
" FROM ".$this->db->prefix().
"facturedet as fd";
3653 $sql .=
", ".$this->db->prefix().
"facture as f";
3654 $sql .=
", ".$this->db->prefix().
"societe as s";
3655 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3656 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3658 $sql .=
" WHERE f.rowid = fd.fk_facture";
3659 $sql .=
" AND f.fk_soc = s.rowid";
3660 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3661 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3662 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3663 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3667 $sql .=
" AND f.fk_soc = ".((int) $socid);
3670 $result = $this->db->query($sql);
3672 $obj = $this->db->fetch_object($result);
3673 $this->stats_facture[
'customers'] = $obj->nb_customers;
3674 $this->stats_facture[
'nb'] = $obj->nb;
3675 $this->stats_facture[
'rows'] = $obj->nb_rows;
3676 $this->stats_facture[
'qty'] = $obj->qty ? $obj->qty : 0;
3681 if (is_array($TFather) && !empty($TFather)) {
3682 foreach ($TFather as &$fatherData) {
3683 $pFather =
new Product($this->db);
3684 $pFather->id = $fatherData[
'id'];
3685 $qtyCoef = $fatherData[
'qty'];
3687 if ($fatherData[
'incdec']) {
3688 $pFather->load_stats_facture($socid);
3690 $this->stats_facture[
'customers'] += $pFather->stats_facture[
'customers'];
3691 $this->stats_facture[
'nb'] += $pFather->stats_facture[
'nb'];
3692 $this->stats_facture[
'rows'] += $pFather->stats_facture[
'rows'];
3693 $this->stats_facture[
'qty'] += $pFather->stats_facture[
'qty'] * $qtyCoef;
3699 $parameters = array(
'socid' => $socid);
3700 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoice', $parameters, $this, $action);
3702 $this->stats_facture = $hookmanager->resArray[
'stats_facture'];
3707 $this->error = $this->db->error();
3723 global $conf, $user, $hookmanager, $action;
3725 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_customers, COUNT(DISTINCT f.rowid) as nb,";
3726 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3727 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet_rec as fd";
3728 $sql .=
", ".MAIN_DB_PREFIX.
"facture_rec as f";
3729 $sql .=
", ".MAIN_DB_PREFIX.
"societe as s";
3730 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3731 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
3733 $sql .=
" WHERE f.rowid = fd.fk_facture";
3734 $sql .=
" AND f.fk_soc = s.rowid";
3735 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3736 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3737 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3738 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3742 $sql .=
" AND f.fk_soc = ".((int) $socid);
3745 $result = $this->db->query($sql);
3747 $obj = $this->db->fetch_object($result);
3748 $this->stats_facturerec[
'customers'] = $obj->nb_customers;
3749 $this->stats_facturerec[
'nb'] = $obj->nb;
3750 $this->stats_facturerec[
'rows'] = $obj->nb_rows;
3751 $this->stats_facturerec[
'qty'] = $obj->qty ? $obj->qty : 0;
3756 if (is_array($TFather) && !empty($TFather)) {
3757 foreach ($TFather as &$fatherData) {
3758 $pFather =
new Product($this->db);
3759 $pFather->id = $fatherData[
'id'];
3760 $qtyCoef = $fatherData[
'qty'];
3762 if ($fatherData[
'incdec']) {
3763 $pFather->load_stats_facture($socid);
3765 $this->stats_facturerec[
'customers'] += $pFather->stats_facturerec[
'customers'];
3766 $this->stats_facturerec[
'nb'] += $pFather->stats_facturerec[
'nb'];
3767 $this->stats_facturerec[
'rows'] += $pFather->stats_facturerec[
'rows'];
3768 $this->stats_facturerec[
'qty'] += $pFather->stats_facturerec[
'qty'] * $qtyCoef;
3774 $parameters = array(
'socid' => $socid);
3775 $reshook = $hookmanager->executeHooks(
'loadStatsCustomerInvoiceRec', $parameters, $this, $action);
3777 $this->stats_facturerec = $hookmanager->resArray[
'stats_facturerec'];
3782 $this->error = $this->db->error();
3797 global $conf, $user, $hookmanager, $action;
3799 $sql =
"SELECT COUNT(DISTINCT f.fk_soc) as nb_suppliers, COUNT(DISTINCT f.rowid) as nb,";
3800 $sql .=
" COUNT(fd.rowid) as nb_rows, SUM(fd.qty) as qty";
3801 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as fd";
3802 $sql .=
", ".$this->db->prefix().
"facture_fourn as f";
3803 $sql .=
", ".$this->db->prefix().
"societe as s";
3804 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3805 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3807 $sql .=
" WHERE f.rowid = fd.fk_facture_fourn";
3808 $sql .=
" AND f.fk_soc = s.rowid";
3809 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
3810 $sql .=
" AND fd.fk_product = ".((int) $this->
id);
3811 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3812 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3816 $sql .=
" AND f.fk_soc = ".((int) $socid);
3819 $result = $this->db->query($sql);
3821 $obj = $this->db->fetch_object($result);
3822 $this->stats_facture_fournisseur[
'suppliers'] = $obj->nb_suppliers;
3823 $this->stats_facture_fournisseur[
'nb'] = $obj->nb;
3824 $this->stats_facture_fournisseur[
'rows'] = $obj->nb_rows;
3825 $this->stats_facture_fournisseur[
'qty'] = $obj->qty ? $obj->qty : 0;
3827 $parameters = array(
'socid' => $socid);
3828 $reshook = $hookmanager->executeHooks(
'loadStatsSupplierInvoice', $parameters, $this, $action);
3830 $this->stats_facture_fournisseur = $hookmanager->resArray[
'stats_facture_fournisseur'];
3835 $this->error = $this->db->error();
3854 $resql = $this->db->query($sql);
3856 $num = $this->db->num_rows($resql);
3859 $arr = $this->db->fetch_array($resql);
3860 $keyfortab = (string) $arr[1];
3862 $keyfortab = substr($keyfortab, -2);
3865 if ($mode ==
'byunit') {
3866 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[0];
3867 } elseif ($mode ==
'bynumber') {
3868 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3869 } elseif ($mode ==
'byamount') {
3870 $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2];
3878 $this->error = $this->db->error().
' sql='.$sql;
3883 $year = strftime(
'%Y', time());
3884 $month = strftime(
'%m', time());
3885 } elseif ($year == -1) {
3894 for ($j = 0; $j < 12; $j++) {
3896 $idx = ucfirst(
dol_trunc(
dol_print_date(
dol_mktime(12, 0, 0, $month, 1, 1970),
"%b"), 1,
'right',
'UTF-8', 1));
3899 $result[$j] = array($idx, isset($tab[$year.$month]) ? $tab[$year.$month] : 0);
3902 $month =
"0".($month - 1);
3904 $month = substr($month, 1);
3912 return array_reverse($result);
3927 public function get_nb_vente($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3933 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3934 if ($mode ==
'bynumber') {
3935 $sql .=
", count(DISTINCT f.rowid)";
3937 $sql .=
", sum(d.total_ht) as total_ht";
3938 $sql .=
" FROM ".$this->db->prefix().
"facturedet as d, ".$this->db->prefix().
"facture as f, ".$this->db->prefix().
"societe as s";
3939 if ($filteronproducttype >= 0) {
3940 $sql .=
", ".$this->db->prefix().
"product as p";
3942 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3943 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3945 $sql .=
" WHERE f.rowid = d.fk_facture";
3946 if ($this->
id > 0) {
3947 $sql .=
" AND d.fk_product = ".((int) $this->
id);
3949 $sql .=
" AND d.fk_product > 0";
3951 if ($filteronproducttype >= 0) {
3952 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
3954 $sql .=
" AND f.fk_soc = s.rowid";
3955 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
3956 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3957 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
3960 $sql .=
" AND f.fk_soc = $socid";
3962 $sql .= $morefilter;
3963 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
3964 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
3966 return $this->
_get_stats($sql, $mode, $year);
3981 public function get_nb_achat($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
3987 $sql =
"SELECT sum(d.qty) as qty, date_format(f.datef, '%Y%m')";
3988 if ($mode ==
'bynumber') {
3989 $sql .=
", count(DISTINCT f.rowid)";
3991 $sql .=
", sum(d.total_ht) as total_ht";
3992 $sql .=
" FROM ".$this->db->prefix().
"facture_fourn_det as d, ".$this->db->prefix().
"facture_fourn as f, ".$this->db->prefix().
"societe as s";
3993 if ($filteronproducttype >= 0) {
3994 $sql .=
", ".$this->db->prefix().
"product as p";
3996 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
3997 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
3999 $sql .=
" WHERE f.rowid = d.fk_facture_fourn";
4000 if ($this->
id > 0) {
4001 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4003 $sql .=
" AND d.fk_product > 0";
4005 if ($filteronproducttype >= 0) {
4006 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4008 $sql .=
" AND f.fk_soc = s.rowid";
4009 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn').
")";
4010 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4011 $sql .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4014 $sql .=
" AND f.fk_soc = $socid";
4016 $sql .= $morefilter;
4017 $sql .=
" GROUP BY date_format(f.datef,'%Y%m')";
4018 $sql .=
" ORDER BY date_format(f.datef,'%Y%m') DESC";
4020 return $this->
_get_stats($sql, $mode, $year);
4034 public function get_nb_propal($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4037 global $conf, $user;
4039 $sql =
"SELECT sum(d.qty) as qty, date_format(p.datep, '%Y%m')";
4040 if ($mode ==
'bynumber') {
4041 $sql .=
", count(DISTINCT p.rowid)";
4043 $sql .=
", sum(d.total_ht) as total_ht";
4044 $sql .=
" FROM ".$this->db->prefix().
"propaldet as d, ".$this->db->prefix().
"propal as p, ".$this->db->prefix().
"societe as s";
4045 if ($filteronproducttype >= 0) {
4046 $sql .=
", ".$this->db->prefix().
"product as prod";
4048 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4049 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4051 $sql .=
" WHERE p.rowid = d.fk_propal";
4052 if ($this->
id > 0) {
4053 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4055 $sql .=
" AND d.fk_product > 0";
4057 if ($filteronproducttype >= 0) {
4058 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
4060 $sql .=
" AND p.fk_soc = s.rowid";
4061 $sql .=
" AND p.entity IN (".getEntity(
'propal').
")";
4062 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4063 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4066 $sql .=
" AND p.fk_soc = ".((int) $socid);
4068 $sql .= $morefilter;
4069 $sql .=
" GROUP BY date_format(p.datep,'%Y%m')";
4070 $sql .=
" ORDER BY date_format(p.datep,'%Y%m') DESC";
4072 return $this->
_get_stats($sql, $mode, $year);
4092 $sql =
"SELECT sum(d.qty) as qty, date_format(p.date_valid, '%Y%m')";
4093 if ($mode ==
'bynumber') {
4094 $sql .=
", count(DISTINCT p.rowid)";
4096 $sql .=
", sum(d.total_ht) as total_ht";
4097 $sql .=
" FROM ".$this->db->prefix().
"supplier_proposaldet as d, ".$this->db->prefix().
"supplier_proposal as p, ".$this->db->prefix().
"societe as s";
4098 if ($filteronproducttype >= 0) {
4099 $sql .=
", ".$this->db->prefix().
"product as prod";
4101 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4102 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4104 $sql .=
" WHERE p.rowid = d.fk_supplier_proposal";
4105 if ($this->
id > 0) {
4106 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4108 $sql .=
" AND d.fk_product > 0";
4110 if ($filteronproducttype >= 0) {
4111 $sql .=
" AND prod.rowid = d.fk_product AND prod.fk_product_type = ".((int) $filteronproducttype);
4113 $sql .=
" AND p.fk_soc = s.rowid";
4114 $sql .=
" AND p.entity IN (".getEntity(
'supplier_proposal').
")";
4115 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4116 $sql .=
" AND p.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4119 $sql .=
" AND p.fk_soc = ".((int) $socid);
4121 $sql .= $morefilter;
4122 $sql .=
" GROUP BY date_format(p.date_valid,'%Y%m')";
4123 $sql .=
" ORDER BY date_format(p.date_valid,'%Y%m') DESC";
4125 return $this->
_get_stats($sql, $mode, $year);
4139 public function get_nb_order($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4142 global $conf, $user;
4144 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4145 if ($mode ==
'bynumber') {
4146 $sql .=
", count(DISTINCT c.rowid)";
4148 $sql .=
", sum(d.total_ht) as total_ht";
4149 $sql .=
" FROM ".$this->db->prefix().
"commandedet as d, ".$this->db->prefix().
"commande as c, ".$this->db->prefix().
"societe as s";
4150 if ($filteronproducttype >= 0) {
4151 $sql .=
", ".$this->db->prefix().
"product as p";
4153 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4154 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4156 $sql .=
" WHERE c.rowid = d.fk_commande";
4157 if ($this->
id > 0) {
4158 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4160 $sql .=
" AND d.fk_product > 0";
4162 if ($filteronproducttype >= 0) {
4163 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4165 $sql .=
" AND c.fk_soc = s.rowid";
4166 $sql .=
" AND c.entity IN (".getEntity(
'commande').
")";
4167 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4168 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4171 $sql .=
" AND c.fk_soc = ".((int) $socid);
4173 $sql .= $morefilter;
4174 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4175 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4177 return $this->
_get_stats($sql, $mode, $year);
4194 global $conf, $user;
4196 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_commande, '%Y%m')";
4197 if ($mode ==
'bynumber') {
4198 $sql .=
", count(DISTINCT c.rowid)";
4200 $sql .=
", sum(d.total_ht) as total_ht";
4201 $sql .=
" FROM ".$this->db->prefix().
"commande_fournisseurdet as d, ".$this->db->prefix().
"commande_fournisseur as c, ".$this->db->prefix().
"societe as s";
4202 if ($filteronproducttype >= 0) {
4203 $sql .=
", ".$this->db->prefix().
"product as p";
4205 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4206 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4208 $sql .=
" WHERE c.rowid = d.fk_commande";
4209 if ($this->
id > 0) {
4210 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4212 $sql .=
" AND d.fk_product > 0";
4214 if ($filteronproducttype >= 0) {
4215 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4217 $sql .=
" AND c.fk_soc = s.rowid";
4218 $sql .=
" AND c.entity IN (".getEntity(
'supplier_order').
")";
4219 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4220 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4223 $sql .=
" AND c.fk_soc = ".((int) $socid);
4225 $sql .= $morefilter;
4226 $sql .=
" GROUP BY date_format(c.date_commande,'%Y%m')";
4227 $sql .=
" ORDER BY date_format(c.date_commande,'%Y%m') DESC";
4229 return $this->
_get_stats($sql, $mode, $year);
4243 public function get_nb_contract($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4246 global $conf, $user;
4248 $sql =
"SELECT sum(d.qty) as qty, date_format(c.date_contrat, '%Y%m')";
4249 if ($mode ==
'bynumber') {
4250 $sql .=
", count(DISTINCT c.rowid)";
4252 $sql .=
", sum(d.total_ht) as total_ht";
4253 $sql .=
" FROM ".$this->db->prefix().
"contratdet as d, ".$this->db->prefix().
"contrat as c, ".$this->db->prefix().
"societe as s";
4254 if ($filteronproducttype >= 0) {
4255 $sql .=
", ".$this->db->prefix().
"product as p";
4257 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4258 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4261 $sql .=
" WHERE c.entity IN (".getEntity(
'contract').
")";
4262 $sql .=
" AND c.rowid = d.fk_contrat";
4264 if ($this->
id > 0) {
4265 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4267 $sql .=
" AND d.fk_product > 0";
4269 if ($filteronproducttype >= 0) {
4270 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4272 $sql .=
" AND c.fk_soc = s.rowid";
4274 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4275 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4278 $sql .=
" AND c.fk_soc = ".((int) $socid);
4280 $sql .= $morefilter;
4281 $sql .=
" GROUP BY date_format(c.date_contrat,'%Y%m')";
4282 $sql .=
" ORDER BY date_format(c.date_contrat,'%Y%m') DESC";
4284 return $this->
_get_stats($sql, $mode, $year);
4298 public function get_nb_mos($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter =
'')
4301 global $conf, $user;
4303 $sql =
"SELECT sum(d.qty), date_format(d.date_valid, '%Y%m')";
4304 if ($mode ==
'bynumber') {
4305 $sql .=
", count(DISTINCT d.rowid)";
4307 $sql .=
" FROM ".$this->db->prefix().
"mrp_mo as d LEFT JOIN ".$this->db->prefix().
"societe as s ON d.fk_soc = s.rowid";
4308 if ($filteronproducttype >= 0) {
4309 $sql .=
", ".$this->db->prefix().
"product as p";
4311 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4312 $sql .=
", ".$this->db->prefix().
"societe_commerciaux as sc";
4315 $sql .=
" WHERE d.entity IN (".getEntity(
'mo').
")";
4316 $sql .=
" AND d.status > 0";
4318 if ($this->
id > 0) {
4319 $sql .=
" AND d.fk_product = ".((int) $this->
id);
4321 $sql .=
" AND d.fk_product > 0";
4323 if ($filteronproducttype >= 0) {
4324 $sql .=
" AND p.rowid = d.fk_product AND p.fk_product_type = ".((int) $filteronproducttype);
4327 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
4328 $sql .=
" AND d.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4331 $sql .=
" AND d.fk_soc = ".((int) $socid);
4333 $sql .= $morefilter;
4334 $sql .=
" GROUP BY date_format(d.date_valid,'%Y%m')";
4335 $sql .=
" ORDER BY date_format(d.date_valid,'%Y%m') DESC";
4337 return $this->
_get_stats($sql, $mode, $year);
4357 if (!is_numeric($id_pere)) {
4360 if (!is_numeric($id_fils)) {
4363 if (!is_numeric($incdec)) {
4373 $sql =
"SELECT fk_product_pere from ".$this->db->prefix().
"product_association";
4374 $sql .=
" WHERE fk_product_pere = ".((int) $id_fils).
" AND fk_product_fils = ".((int) $id_pere);
4375 if (!$this->db->query($sql)) {
4380 $sql =
"SELECT MAX(rang) as max_rank FROM ".$this->db->prefix().
"product_association";
4381 $sql .=
" WHERE fk_product_pere = ".((int) $id_pere);
4382 $resql = $this->db->query($sql);
4384 $obj = $this->db->fetch_object($resql);
4385 $rank = $obj->max_rank + 1;
4387 $sql =
"INSERT INTO ".$this->db->prefix().
"product_association(fk_product_pere,fk_product_fils,qty,incdec,rang)";
4388 $sql .=
" VALUES (".((int) $id_pere).
", ".((int) $id_fils).
", ".
price2num($qty,
'MS').
", ".
price2num($incdec,
'MS').
", ".((int) $rank).
")";
4389 if (! $this->db->query($sql)) {
4395 $result = $this->
call_trigger(
'PRODUCT_SUBPRODUCT_ADD', $user);
4397 $this->error = $this->db->lasterror();
4398 dol_syslog(get_class($this).
'::addSubproduct error='.$this->error, LOG_ERR);
4430 if (!is_numeric($id_pere)) {
4433 if (!is_numeric($id_fils)) {
4436 if (!is_numeric($incdec)) {
4439 if (!is_numeric($qty)) {
4443 $sql =
'UPDATE '.$this->db->prefix().
'product_association SET ';
4444 $sql .=
'qty = '.price2num($qty,
'MS');
4445 $sql .=
',incdec = '.price2num($incdec,
'MS');
4446 $sql .=
' WHERE fk_product_pere = '.((int) $id_pere).
' AND fk_product_fils = '.((int) $id_fils);
4448 if (!$this->db->query($sql)) {
4454 $result = $this->
call_trigger(
'PRODUCT_SUBPRODUCT_UPDATE', $user);
4456 $this->error = $this->db->lasterror();
4457 dol_syslog(get_class($this).
'::updateSubproduct error='.$this->error, LOG_ERR);
4481 if (!is_numeric($fk_parent)) {
4484 if (!is_numeric($fk_child)) {
4488 $sql =
"DELETE FROM ".$this->db->prefix().
"product_association";
4489 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4490 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4492 dol_syslog(get_class($this).
'::del_sousproduit', LOG_DEBUG);
4493 if (!$this->db->query($sql)) {
4499 $sqlrank =
"SELECT rowid, rang FROM ".$this->db->prefix().
"product_association";
4500 $sqlrank.=
" WHERE fk_product_pere = ".((int) $fk_parent);
4501 $sqlrank.=
" ORDER BY rang";
4502 $resqlrank = $this->db->query($sqlrank);
4505 while ($objrank = $this->db->fetch_object($resqlrank)) {
4507 $sql =
"UPDATE ".$this->db->prefix().
"product_association";
4508 $sql.=
" SET rang = ".((int) $cpt);
4509 $sql.=
" WHERE rowid = ".((int) $objrank->rowid);
4510 if (! $this->db->query($sql)) {
4519 $result = $this->
call_trigger(
'PRODUCT_SUBPRODUCT_DELETE', $user);
4521 $this->error = $this->db->lasterror();
4522 dol_syslog(get_class($this).
'::delSubproduct error='.$this->error, LOG_ERR);
4542 $sql =
"SELECT fk_product_pere, qty, incdec";
4543 $sql .=
" FROM ".$this->db->prefix().
"product_association";
4544 $sql .=
" WHERE fk_product_pere = ".((int) $fk_parent);
4545 $sql .=
" AND fk_product_fils = ".((int) $fk_child);
4547 $result = $this->db->query($sql);
4549 $num = $this->db->num_rows($result);
4552 $obj = $this->db->fetch_object($result);
4554 $this->is_sousproduit_qty = $obj->qty;
4555 $this->is_sousproduit_incdec = $obj->incdec;
4586 dol_syslog(get_class($this).
"::add_fournisseur id_fourn = ".$id_fourn.
" ref_fourn=".
$ref_fourn.
" quantity=".$quantity, LOG_DEBUG);
4592 $sql =
"SELECT rowid, fk_product";
4593 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4594 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4595 $sql .=
" AND ref_fourn = '".$this->db->escape(
$ref_fourn).
"'";
4596 $sql .=
" AND fk_product <> ".((int) $this->
id);
4597 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4599 $resql = $this->db->query($sql);
4601 $obj = $this->db->fetch_object($resql);
4604 $this->product_id_already_linked = $obj->fk_product;
4607 $this->db->free($resql);
4611 $sql =
"SELECT rowid";
4612 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4613 $sql .=
" WHERE fk_soc = ".((int) $id_fourn);
4615 $sql .=
" AND ref_fourn = '".$this->db->escape(
$ref_fourn).
"'";
4617 $sql .=
" AND (ref_fourn = '' OR ref_fourn IS NULL)";
4619 $sql .=
" AND quantity = ".((float) $quantity);
4620 $sql .=
" AND fk_product = ".((int) $this->
id);
4621 $sql .=
" AND entity IN (".getEntity(
'productsupplierprice').
")";
4623 $resql = $this->db->query($sql);
4625 $obj = $this->db->fetch_object($resql);
4629 $sql =
"INSERT INTO ".$this->db->prefix().
"product_fournisseur_price(";
4632 $sql .=
", fk_product";
4634 $sql .=
", ref_fourn";
4635 $sql .=
", quantity";
4636 $sql .=
", fk_user";
4638 $sql .=
") VALUES (";
4639 $sql .=
"'".$this->db->idate($now).
"'";
4640 $sql .=
", ".$conf->entity;
4641 $sql .=
", ".$this->id;
4642 $sql .=
", ".$id_fourn;
4643 $sql .=
", '".$this->db->escape(
$ref_fourn).
"'";
4644 $sql .=
", ".$quantity;
4645 $sql .=
", ".$user->id;
4649 if ($this->db->query($sql)) {
4650 $this->product_fourn_price_id = $this->db->last_insert_id($this->db->prefix().
"product_fournisseur_price");
4653 $this->error = $this->db->lasterror();
4658 $this->product_fourn_price_id = $obj->rowid;
4662 $this->error = $this->db->lasterror();
4681 $sql =
"SELECT DISTINCT p.fk_soc";
4682 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price as p";
4683 $sql .=
" WHERE p.fk_product = ".((int) $this->
id);
4684 $sql .=
" AND p.entity = ".((int) $conf->entity);
4686 $result = $this->db->query($sql);
4688 $num = $this->db->num_rows($result);
4691 $obj = $this->db->fetch_object($result);
4692 $list[$i] = $obj->fk_soc;
4710 global $conf, $user;
4717 $sql =
"INSERT INTO ".$this->db->prefix().
"product_price (";
4719 $sql .=
", fk_product";
4720 $sql .=
", date_price";
4721 $sql .=
", price_level";
4723 $sql .=
", price_ttc";
4724 $sql .=
", price_min";
4725 $sql .=
", price_min_ttc";
4726 $sql .=
", price_base_type";
4727 $sql .=
", default_vat_code";
4729 $sql .=
", recuperableonly";
4730 $sql .=
", localtax1_tx";
4731 $sql .=
", localtax1_type";
4732 $sql .=
", localtax2_tx";
4733 $sql .=
", localtax2_type";
4734 $sql .=
", fk_user_author";
4736 $sql .=
", price_by_qty";
4737 $sql .=
", fk_price_expression";
4738 $sql .=
", fk_multicurrency";
4739 $sql .=
", multicurrency_code";
4740 $sql .=
", multicurrency_tx";
4741 $sql .=
", multicurrency_price";
4742 $sql .=
", multicurrency_price_ttc";
4747 $sql .=
", '".$this->db->idate($now).
"'";
4748 $sql .=
", price_level";
4750 $sql .=
", price_ttc";
4751 $sql .=
", price_min";
4752 $sql .=
", price_min_ttc";
4753 $sql .=
", price_base_type";
4754 $sql .=
", default_vat_code";
4756 $sql .=
", recuperableonly";
4757 $sql .=
", localtax1_tx";
4758 $sql .=
", localtax1_type";
4759 $sql .=
", localtax2_tx";
4760 $sql .=
", localtax2_type";
4761 $sql .=
", ".$user->id;
4763 $sql .=
", price_by_qty";
4764 $sql .=
", fk_price_expression";
4765 $sql .=
", fk_multicurrency";
4766 $sql .=
", multicurrency_code";
4767 $sql .=
", multicurrency_tx";
4768 $sql .=
", multicurrency_price";
4769 $sql .=
", multicurrency_price_ttc";
4770 $sql .=
" FROM ".$this->db->prefix().
"product_price ps";
4771 $sql .=
" WHERE fk_product = ".((int) $fromId);
4772 $sql .=
" AND date_price IN (SELECT MAX(pd.date_price) FROM ".$this->db->prefix().
"product_price pd WHERE pd.fk_product = ".((int) $fromId).
" AND pd.price_level = ps.price_level)";
4773 $sql .=
" ORDER BY date_price DESC";
4776 $resql = $this->db->query($sql);
4778 $this->db->rollback();
4782 $this->db->commit();
4799 $sql =
'INSERT INTO '.$this->db->prefix().
'product_association (fk_product_pere, fk_product_fils, qty, incdec)';
4800 $sql .=
" SELECT ".$toId.
", fk_product_fils, qty, incdec FROM ".$this->db->prefix().
"product_association";
4801 $sql .=
" WHERE fk_product_pere = ".((int) $fromId);
4803 dol_syslog(get_class($this).
'::clone_association', LOG_DEBUG);
4804 if (!$this->db->query($sql)) {
4805 $this->db->rollback();
4809 $this->db->commit();
4842 $sql =
"INSERT ".$this->db->prefix().
"product_fournisseur_price (";
4843 $sql .=
" datec, fk_product, fk_soc, price, quantity, fk_user, tva_tx)";
4844 $sql .=
" SELECT '".$this->db->idate($now).
"', ".((int) $toId).
", fk_soc, price, quantity, fk_user, tva_tx";
4845 $sql .=
" FROM ".$this->db->prefix().
"product_fournisseur_price";
4846 $sql .=
" WHERE fk_product = ".((int) $fromId);
4848 dol_syslog(get_class($this).
'::clone_fournisseurs', LOG_DEBUG);
4849 $resql = $this->db->query($sql);
4851 $this->db->rollback();
4854 $this->db->commit();
4872 public function fetch_prod_arbo($prod, $compl_path =
'', $multiply = 1, $level = 1, $id_parent = 0, $ignore_stock_load = 0)
4875 global $conf, $langs;
4879 foreach ($prod as $id_product => $desc_pere) {
4880 if (is_array($desc_pere)) {
4881 $id = (!empty($desc_pere[0]) ? $desc_pere[0] :
'');
4882 $nb = (!empty($desc_pere[1]) ? $desc_pere[1] :
'');
4883 $type = (!empty($desc_pere[2]) ? $desc_pere[2] :
'');
4884 $label = (!empty($desc_pere[3]) ? $desc_pere[3] :
'');
4885 $incdec = (!empty($desc_pere[4]) ? $desc_pere[4] : 0);
4887 if ($multiply < 1) {
4892 if (is_null($tmpproduct)) {
4893 $tmpproduct =
new Product($this->db);
4895 $tmpproduct->fetch($id);
4897 if (empty($ignore_stock_load) && ($tmpproduct->isProduct() ||
getDolGlobalString(
'STOCK_SUPPORTS_SERVICES'))) {
4898 $tmpproduct->load_stock(
'nobatch,novirtual');
4901 $this->res[] = array(
4903 'id_parent'=>$id_parent,
4904 'ref'=>$tmpproduct->ref,
4906 'nb_total'=>$nb * $multiply,
4907 'stock'=>$tmpproduct->stock_reel,
4908 'stock_alert'=>$tmpproduct->seuil_stock_alerte,
4910 'fullpath'=>$compl_path.$label,
4912 'desiredstock'=>$tmpproduct->desiredstock,
4915 'entity'=>$tmpproduct->entity
4919 if (isset($desc_pere[
'childs']) && is_array($desc_pere[
'childs'])) {
4921 $this->
fetch_prod_arbo($desc_pere[
'childs'], $compl_path.$desc_pere[3].
" -> ", $desc_pere[1] * $multiply, $level + 1, $id, $ignore_stock_load);
4939 $this->res = array();
4940 if (isset($this->sousprods) && is_array($this->sousprods)) {
4941 foreach ($this->sousprods as $prod_name => $desc_product) {
4942 if (is_array($desc_product)) {
4943 $this->
fetch_prod_arbo($desc_product,
"", $multiply, 1, $this->
id, $ignore_stock_load);
4962 $sql =
"SELECT COUNT(pa.rowid) as nb";
4963 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa";
4965 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id).
" OR pa.fk_product_pere = ".((int) $this->
id);
4966 } elseif ($mode == -1) {
4967 $sql .=
" WHERE pa.fk_product_fils = ".((int) $this->
id);
4968 } elseif ($mode == 1) {
4969 $sql .=
" WHERE pa.fk_product_pere = ".((int) $this->
id);
4972 $resql = $this->db->query($sql);
4974 $obj = $this->db->fetch_object($resql);
4993 $sql =
"SELECT count(rowid) as nb FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_parent = ".((int) $this->
id);
4994 $sql .=
" AND entity IN (".getEntity(
'product').
")";
4996 $resql = $this->db->query($sql);
4998 $obj = $this->db->fetch_object($resql);
5016 if (isModEnabled(
'variants')) {
5017 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_combination WHERE fk_product_child = ".((int) $this->
id).
" AND entity IN (".
getEntity(
'product').
")";
5019 $query = $this->db->query($sql);
5022 if (!$this->db->num_rows($query)) {
5043 $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";
5044 $sql .=
", p.tosell as status, p.tobuy as status_buy";
5045 $sql .=
" FROM ".$this->db->prefix().
"product_association as pa,";
5046 $sql .=
" ".$this->db->prefix().
"product as p";
5047 $sql .=
" WHERE p.rowid = pa.fk_product_pere";
5048 $sql .=
" AND pa.fk_product_fils = ".((int) $this->
id);
5050 $res = $this->db->query($sql);
5053 while ($record = $this->db->fetch_array(
$res)) {
5055 $prods[$record[
'id']][
'id'] = $record[
'rowid'];
5056 $prods[$record[
'id']][
'ref'] = $record[
'ref'];
5057 $prods[$record[
'id']][
'label'] = $record[
'label'];
5058 $prods[$record[
'id']][
'qty'] = $record[
'qty'];
5059 $prods[$record[
'id']][
'incdec'] = $record[
'incdec'];
5060 $prods[$record[
'id']][
'fk_product_type'] = $record[
'fk_product_type'];
5061 $prods[$record[
'id']][
'entity'] = $record[
'entity'];
5062 $prods[$record[
'id']][
'status'] = $record[
'status'];
5063 $prods[$record[
'id']][
'status_buy'] = $record[
'status_buy'];
5082 public function getChildsArbo($id, $firstlevelonly = 0, $level = 1, $parents = array())
5084 global $alreadyfound;
5090 $sql =
"SELECT p.rowid, p.ref, p.label as label, p.fk_product_type,";
5091 $sql .=
" pa.qty as qty, pa.fk_product_fils as id, pa.incdec,";
5092 $sql .=
" pa.rowid as fk_association, pa.rang";
5093 $sql .=
" FROM ".$this->db->prefix().
"product as p,";
5094 $sql .=
" ".$this->db->prefix().
"product_association as pa";
5095 $sql .=
" WHERE p.rowid = pa.fk_product_fils";
5096 $sql .=
" AND pa.fk_product_pere = ".((int) $id);
5097 $sql .=
" AND pa.fk_product_fils <> ".((int) $id);
5098 $sql.=
" ORDER BY pa.rang";
5100 dol_syslog(get_class($this).
'::getChildsArbo id='.$id.
' level='.$level.
' parents='.(is_array($parents) ? implode(
',', $parents) : $parents), LOG_DEBUG);
5103 $alreadyfound = array($id=>1);
5110 $res = $this->db->query($sql);
5113 while ($rec = $this->db->fetch_array(
$res)) {
5114 if (!empty($alreadyfound[$rec[
'rowid']])) {
5115 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);
5116 if (in_array($rec[
'id'], $parents)) {
5120 $alreadyfound[$rec[
'rowid']] = 1;
5121 $prods[$rec[
'rowid']] = array(
5124 2=>$rec[
'fk_product_type'],
5125 3=>$this->db->escape($rec[
'label']),
5128 6=>$rec[
'fk_association'],
5133 if (empty($firstlevelonly)) {
5134 $parents[] = $rec[
'rowid'];
5135 $listofchilds = $this->
getChildsArbo($rec[
'rowid'], 0, $level + 1, $parents);
5136 foreach ($listofchilds as $keyChild => $valueChild) {
5137 $prods[$rec[
'rowid']][
'childs'][$keyChild] = $valueChild;
5161 foreach ($this->
getChildsArbo($this->
id) as $keyChild => $valueChild) {
5162 $parent[$this->label][$keyChild] = $valueChild;
5164 foreach ($parent as $key => $value) {
5165 $this->sousprods[$key] = $value;
5177 global $conf, $langs;
5179 $langs->loadLangs(array(
'products',
'other'));
5182 $nofetch = !empty($params[
'nofetch']);
5185 return [
'optimize' => $langs->trans(
"ShowProduct")];
5188 if (!empty($this->entity)) {
5189 $tmpphoto = $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80, 0, 0, 0, 0, 1);
5190 if ($this->nbphoto > 0) {
5191 $datas[
'photo'] =
'<div class="photointooltip floatright">'.
"\n" . $tmpphoto .
'</div>';
5196 $datas[
'picto'] =
img_picto(
'',
'product').
' <u class="paddingrightonly">'.$langs->trans(
"Product").
'</u>';
5198 $datas[
'picto']=
img_picto(
'',
'service').
' <u class="paddingrightonly">'.$langs->trans(
"Service").
'</u>';
5200 if (isset($this->
status) && isset($this->status_buy)) {
5201 $datas[
'status']=
' '.$this->getLibStatut(5, 0) .
' '.$this->getLibStatut(5, 1);
5204 if (!empty($this->
ref)) {
5205 $datas[
'ref']=
'<br><b>'.$langs->trans(
'ProductRef').
':</b> '.$this->ref;
5207 if (!empty($this->label)) {
5208 $datas[
'label']=
'<br><b>'.$langs->trans(
'ProductLabel').
':</b> '.$this->label;
5211 $datas[
'description']=
'<br><b>'.$langs->trans(
'ProductDescription').
':</b> '.dolGetFirstLineofText($this->
description, 5);
5214 if (isModEnabled(
'productbatch')) {
5215 $langs->load(
"productbatch");
5216 $datas[
'batchstatus']=
"<br><b>".$langs->trans(
"ManageLotSerial").
'</b>: '.$this->
getLibStatut(0, 2);
5219 if (isModEnabled(
'barcode')) {
5220 $datas[
'barcode']=
'<br><b>'.$langs->trans(
'BarCode').
':</b> '.$this->barcode;
5224 if ($this->weight) {
5225 $datas[
'weight']=
"<br><b>".$langs->trans(
"Weight").
'</b>: '.$this->weight.
' '.
measuringUnitString(0,
"weight", $this->weight_units);
5228 if ($this->length) {
5229 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Length").
'</b>: '.$this->length.
' '.
measuringUnitString(0,
'size', $this->length_units);
5232 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Width").
'</b>: '.$this->width.
' '.
measuringUnitString(0,
'size', $this->width_units);
5234 if ($this->height) {
5235 $labelsize .= ($labelsize ?
" - " :
"").
"<b>".$langs->trans(
"Height").
'</b>: '.$this->height.
' '.
measuringUnitString(0,
'size', $this->height_units);
5238 $datas[
'size']=
"<br>".$labelsize;
5241 $labelsurfacevolume =
"";
5242 if ($this->surface) {
5243 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Surface").
'</b>: '.$this->surface.
' '.
measuringUnitString(0,
'surface', $this->surface_units);
5245 if ($this->volume) {
5246 $labelsurfacevolume .= ($labelsurfacevolume ?
" - " :
"").
"<b>".$langs->trans(
"Volume").
'</b>: '.$this->volume.
' '.
measuringUnitString(0,
'volume', $this->volume_units);
5248 if ($labelsurfacevolume) {
5249 $datas[
'surface']=
"<br>" . $labelsurfacevolume;
5252 if (!empty($this->pmp) && $this->pmp) {
5253 $datas[
'pmp'] =
"<br><b>".$langs->trans(
"PMPValue").
'</b>: '.
price($this->pmp, 0,
'', 1, -1, -1, $conf->currency);
5256 if (isModEnabled(
'accounting')) {
5257 if ($this->
status && isset($this->accountancy_code_sell)) {
5258 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5259 $selllabel =
'<br>';
5260 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellCode').
':</b> '.
length_accountg($this->accountancy_code_sell);
5261 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellIntraCode').
':</b> '.
length_accountg($this->accountancy_code_sell_intra);
5262 $selllabel .=
'<br><b>'.$langs->trans(
'ProductAccountancySellExportCode').
':</b> '.
length_accountg($this->accountancy_code_sell_export);
5263 $datas[
'accountancysell'] = $selllabel;
5265 if ($this->status_buy && isset($this->accountancy_code_buy)) {
5266 include_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
5268 if (empty($this->
status)) {
5269 $buylabel .=
'<br>';
5271 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyCode').
':</b> '.
length_accountg($this->accountancy_code_buy);
5272 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyIntraCode').
':</b> '.
length_accountg($this->accountancy_code_buy_intra);
5273 $buylabel .=
'<br><b>'.$langs->trans(
'ProductAccountancyBuyExportCode').
':</b> '.
length_accountg($this->accountancy_code_buy_export);
5274 $datas[
'accountancybuy'] = $buylabel;
5278 if (isModEnabled(
'categorie') && !$nofetch) {
5279 require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
5280 $form =
new Form($this->db);
5281 $datas[
'categories'] =
'<br>' . $form->showCategories($this->
id, Categorie::TYPE_PRODUCT, 1);
5300 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss =
'', $add_label = 0, $sep =
' - ')
5302 global $conf, $langs, $hookmanager;
5303 include_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
5307 $newref = $this->ref;
5309 $newref =
dol_trunc($newref, $maxlength,
'middle');
5313 'objecttype' => (isset($this->
type) ? ($this->
type == 1 ?
'service' :
'product') : $this->element),
5314 'option' => $option,
5317 $classfortooltip =
'classfortooltip';
5320 $classfortooltip =
'classforajaxtooltip';
5321 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
5328 if (empty($notooltip)) {
5330 $label = $langs->trans(
"ShowProduct");
5331 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1, 1).
'"';
5333 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1, 1).
'"' :
' title="tocomplete"');
5334 $linkclose .= $dataparams.
' class="nowraponall '.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
5336 $linkclose =
' class="nowraponall'.($morecss ?
' '.$morecss :
'').
'"';
5339 if ($option ==
'supplier' || $option ==
'category') {
5340 $url = DOL_URL_ROOT.
'/product/fournisseurs.php?id='.$this->id;
5341 } elseif ($option ==
'stock') {
5342 $url = DOL_URL_ROOT.
'/product/stock/product.php?id='.$this->id;
5343 } elseif ($option ==
'composition') {
5344 $url = DOL_URL_ROOT.
'/product/composition/card.php?id='.$this->id;
5346 $url = DOL_URL_ROOT.
'/product/card.php?id='.$this->id;
5349 if ($option !==
'nolink') {
5351 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
5352 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
5353 $add_save_lastsearch_values = 1;
5355 if ($add_save_lastsearch_values) {
5356 $url .=
'&save_lastsearch_values=1';
5360 $linkstart =
'<a href="'.$url.
'"';
5361 $linkstart .= $linkclose.
'>';
5364 $result .= $linkstart;
5367 $result .= (
img_object(($notooltip ?
'' : $label),
'product',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5370 $result .= (
img_object(($notooltip ?
'' : $label),
'service',
'class="paddingright"', 0, 0, $notooltip ? 0 : 1));
5373 $result .=
'<span class="aaa">'.dol_escape_htmltag($newref).
'</span>';
5374 $result .= $linkend;
5375 if ($withpicto != 2) {
5376 $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) :
'');
5380 $hookmanager->initHooks(array(
'productdao'));
5381 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'label' => &$label);
5382 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
5384 $result = $hookmanager->resPrint;
5386 $result .= $hookmanager->resPrint;
5403 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
5405 global $conf, $user, $langs;
5407 $langs->load(
"products");
5408 $outputlangs->load(
"products");
5415 $modelpath =
"core/modules/product/doc/";
5417 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
5433 return $this->
LibStatut($this->status_buy, $mode, $type);
5435 return $this->
LibStatut($this->status_batch, $mode, $type);
5438 return $this->
LibStatut($this->status_buy, $mode, $type);
5454 global $conf, $langs;
5456 $labelStatus = $labelStatusShort =
'';
5458 $langs->load(
'products');
5459 if (isModEnabled(
'productbatch')) {
5460 $langs->load(
"productbatch");
5466 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial')));
5467 return dolGetStatus($label);
5469 $label = ($status == 0 ? $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort') : ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort')));
5470 return dolGetStatus($label);
5474 return dolGetStatus($langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch'),
'',
'', empty($status) ?
'status5' :
'status4', 3,
'dot');
5480 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'));
5484 $statuttrans = empty($status) ?
'status5' :
'status4';
5489 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSellShort');
5490 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnSell');
5491 } elseif ($type == 1) {
5492 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuyShort');
5493 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBuy');
5494 } elseif ($type == 2) {
5495 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatch');
5496 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusNotOnBatchShort');
5498 } elseif ($status == 1) {
5501 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSellShort');
5502 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSell');
5503 } elseif ($type == 1) {
5504 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnBuyShort');
5505 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnBuy');
5506 } elseif ($type == 2) {
5507 $labelStatus = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatch') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerial'));
5508 $labelStatusShort = ($status == 1 ? $langs->transnoentitiesnoconv(
'ProductStatusOnBatchShort') : $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort'));
5510 } elseif ($type == 2 && $status == 2) {
5511 $labelStatus = $langs->transnoentitiesnoconv(
'ProductStatusOnSerial');
5512 $labelStatusShort = $langs->transnoentitiesnoconv(
'ProductStatusOnSerialShort');
5516 return dolGetStatus($langs->transnoentitiesnoconv(
'Unknown'),
'',
'',
'status0', 0);
5518 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statuttrans, $mode);
5531 $langs->load(
'products');
5533 if (isset($this->finished) && $this->finished >= 0) {
5534 $sql =
"SELECT label, code FROM ".$this->db->prefix().
"c_product_nature where code = ".((int) $this->finished).
" AND active=1";
5535 $resql = $this->db->query($sql);
5536 if ($resql && $this->db->num_rows($resql) > 0) {
5537 $res = $this->db->fetch_array($resql);
5538 $label = $langs->trans(
$res[
'label']);
5539 $this->db->free($resql);
5542 $this->error = $this->db->error().
' sql='.$sql;
5543 dol_syslog(__METHOD__.
' Error '.$this->error, LOG_ERR);
5569 public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label =
'', $price = 0, $inventorycode =
'', $origin_element =
'', $origin_id =
null, $disablestockchangeforsubproduct = 0, $extrafields =
null)
5575 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5581 $nbpiece = abs($nbpiece);
5584 $op[0] =
"+".trim($nbpiece);
5585 $op[1] =
"-".trim($nbpiece);
5588 $movementstock->setOrigin($origin_element, $origin_id);
5589 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'',
'',
'',
'',
false, 0, $disablestockchangeforsubproduct);
5593 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5594 $movementstock->array_options = $array_options;
5595 $movementstock->insertExtraFields();
5597 $this->db->commit();
5600 $this->error = $movementstock->error;
5601 $this->errors = $movementstock->errors;
5603 $this->db->rollback();
5632 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, $force_update_batch =
false)
5638 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
5644 $nbpiece = abs($nbpiece);
5647 $op[0] =
"+".trim($nbpiece);
5648 $op[1] =
"-".trim($nbpiece);
5651 $movementstock->setOrigin($origin_element, $origin_id);
5652 $result = $movementstock->_create($user, $this->
id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode,
'', $dlc, $dluo, $lot,
false, 0, $disablestockchangeforsubproduct, 0, $force_update_batch);
5656 $array_options = $extrafields->getOptionalsFromPost(
'stock_mouvement');
5657 $movementstock->array_options = $array_options;
5658 $movementstock->insertExtraFields();
5660 $this->db->commit();
5663 $this->error = $movementstock->error;
5664 $this->errors = $movementstock->errors;
5666 $this->db->rollback();
5686 public function load_stock($option =
'', $includedraftpoforvirtual =
null, $dateofvirtualstock =
null)
5691 $this->stock_reel = 0;
5692 $this->stock_warehouse = array();
5693 $this->stock_theorique = 0;
5696 $warehouseStatus = array();
5697 if (preg_match(
'/warehouseclosed/', $option)) {
5700 if (preg_match(
'/warehouseopen/', $option)) {
5703 if (preg_match(
'/warehouseinternal/', $option)) {
5711 $sql =
"SELECT ps.rowid, ps.reel, ps.fk_entrepot";
5712 $sql .=
" FROM ".$this->db->prefix().
"product_stock as ps";
5713 $sql .=
", ".$this->db->prefix().
"entrepot as w";
5714 $sql .=
" WHERE w.entity IN (".getEntity(
'stock').
")";
5715 $sql .=
" AND w.rowid = ps.fk_entrepot";
5716 $sql .=
" AND ps.fk_product = ".((int) $this->
id);
5717 if (count($warehouseStatus)) {
5718 $sql .=
" AND w.statut IN (".$this->db->sanitize(implode(
',', $warehouseStatus)).
")";
5721 $sql .=
" ORDER BY ps.reel ".(getDolGlobalString(
'DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ?
'DESC' :
'ASC');
5723 dol_syslog(get_class($this).
"::load_stock", LOG_DEBUG);
5724 $result = $this->db->query($sql);
5726 $num = $this->db->num_rows($result);
5730 $row = $this->db->fetch_object($result);
5731 $this->stock_warehouse[$row->fk_entrepot] =
new stdClass();
5732 $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel;
5733 $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid;
5734 if ((!preg_match(
'/nobatch/', $option)) && $this->
hasbatch()) {
5735 $this->stock_warehouse[$row->fk_entrepot]->detail_batch =
Productbatch::findAll($this->db, $row->rowid, 1, $this->id);
5737 $this->stock_reel += $row->reel;
5741 $this->db->free($result);
5743 if (!preg_match(
'/novirtual/', $option)) {
5749 $this->error = $this->db->lasterror();
5768 global $conf, $hookmanager, $action;
5770 $stock_commande_client = 0;
5771 $stock_commande_fournisseur = 0;
5772 $stock_sending_client = 0;
5773 $stock_reception_fournisseur = 0;
5774 $stock_inproduction = 0;
5778 if (isModEnabled(
'commande')) {
5783 $stock_commande_client = $this->stats_commande[
'qty'];
5785 if (isModEnabled(
"expedition")) {
5786 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
5787 $filterShipmentStatus =
'';
5797 $stock_sending_client = $this->stats_expedition[
'qty'];
5799 if (isModEnabled(
"supplier_order")) {
5800 $filterStatus = !
getDolGlobalString(
'SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK') ?
'3,4' : $conf->global->SUPPLIER_ORDER_STATUS_FOR_VIRTUAL_STOCK;
5801 if (isset($includedraftpoforvirtual)) {
5802 $filterStatus =
'0,1,2,'.$filterStatus;
5808 $stock_commande_fournisseur = $this->stats_commande_fournisseur[
'qty'];
5810 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) && empty($conf->reception->enabled)) {
5812 $filterStatus =
'4';
5813 if (isset($includedraftpoforvirtual)) {
5814 $filterStatus =
'0,'.$filterStatus;
5820 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5822 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) && isModEnabled(
"reception")) {
5824 $filterStatus =
'4';
5825 if (isset($includedraftpoforvirtual)) {
5826 $filterStatus =
'0,'.$filterStatus;
5832 $stock_reception_fournisseur = $this->stats_reception[
'qty'];
5834 if (isModEnabled(
'mrp')) {
5839 $stock_inproduction = $this->stats_mrptoproduce[
'qty'] - $this->stats_mrptoconsume[
'qty'];
5842 $this->stock_theorique = $this->stock_reel + $stock_inproduction;
5846 $this->stock_theorique -= ($stock_commande_client - $stock_sending_client);
5848 $this->stock_theorique += 0;
5850 $this->stock_theorique -= $stock_commande_client;
5854 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5856 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5858 $this->stock_theorique -= $stock_reception_fournisseur;
5860 $this->stock_theorique += ($stock_commande_fournisseur - $stock_reception_fournisseur);
5863 $parameters = array(
'id'=>$this->
id,
'includedraftpoforvirtual' => $includedraftpoforvirtual);
5865 $reshook = $hookmanager->executeHooks(
'loadvirtualstock', $parameters, $this, $action);
5867 $this->stock_theorique = $hookmanager->resArray[
'stock_theorique'];
5868 } elseif ($reshook == 0 && isset($hookmanager->resArray[
'stock_stats_hook'])) {
5869 $this->stock_theorique += $hookmanager->resArray[
'stock_stats_hook'];
5887 $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";
5888 $sql .=
" WHERE pb.fk_product_stock = ps.rowid AND ps.fk_product = ".((int) $this->
id).
" AND pb.batch = '".$this->db->escape($batch).
"'";
5889 $sql .=
" GROUP BY pb.batch, pb.eatby, pb.sellby";
5890 dol_syslog(get_class($this).
"::loadBatchInfo load first entry found for lot/serial = ".$batch, LOG_DEBUG);
5891 $resql = $this->db->query($sql);
5893 $num = $this->db->num_rows($resql);
5896 $obj = $this->db->fetch_object($resql);
5897 $result[] = array(
'batch'=>$batch,
'eatby'=>$this->db->jdate($obj->eatby),
'sellby'=>$this->db->jdate($obj->sellby),
'qty'=>$obj->qty);
5903 $this->db->rollback();
5921 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5927 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos";
5929 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product').dol_sanitizeFileName($this->
ref);
5934 $dir_osencoded = $dir;
5936 if (is_dir($dir_osencoded)) {
5937 $originImage = $dir.
'/'.$file[
'name'];
5948 if (is_numeric($result) && $result > 0) {
5965 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5966 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
5972 $dir .=
'/'.get_exdir($this->
id, 2, 0, 0, $this,
'product').$this->id.
"/photos/";
5974 $dir .=
'/'.get_exdir(0, 0, 0, 0, $this,
'product');
5980 if (file_exists($dir_osencoded)) {
5981 $handle = opendir($dir_osencoded);
5982 if (is_resource($handle)) {
5983 while (($file = readdir($handle)) !==
false) {
5985 $file = mb_convert_encoding($file,
'UTF-8',
'ISO-8859-1');
6007 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
6008 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
6014 $handle = @opendir($dir_osencoded);
6015 if (is_resource($handle)) {
6016 while (($file = readdir($handle)) !==
false) {
6018 $file = mb_convert_encoding($file,
'UTF-8',
'ISO-8859-1');
6025 $photo_vignette =
'';
6027 if (preg_match(
'/('.$this->regeximgext.
')$/i', $photo, $regs)) {
6028 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $photo).
'_small'.$regs[0];
6031 $dirthumb = $dir.
'thumbs/';
6035 $obj[
'photo'] = $photo;
6036 if ($photo_vignette &&
dol_is_file($dirthumb.$photo_vignette)) {
6037 $obj[
'photo_vignette'] =
'thumbs/'.$photo_vignette;
6039 $obj[
'photo_vignette'] =
"";
6042 $tabobj[$nbphoto - 1] = $obj;
6045 if ($nbmax && $nbphoto >= $nbmax) {
6067 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
6068 include_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
6070 $dir = dirname($file).
'/';
6071 $dirthumb = $dir.
'/thumbs/';
6072 $filename = preg_replace(
'/'.preg_quote($dir,
'/').
'/i',
'', $file);
6078 if (preg_match(
'/('.$this->regeximgext.
')$/i', $filename, $regs)) {
6079 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_small'.$regs[0];
6080 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
6084 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_mini'.$regs[0];
6085 if (file_exists(
dol_osencode($dirthumb.$photo_vignette))) {
6102 $infoImg = getimagesize($file_osencoded);
6103 $this->imgWidth = $infoImg[0];
6104 $this->imgHeight = $infoImg[1];
6116 global $hookmanager;
6118 $this->nb = array();
6120 $sql =
"SELECT count(p.rowid) as nb, fk_product_type";
6121 $sql .=
" FROM ".$this->db->prefix().
"product as p";
6122 $sql .=
' WHERE p.entity IN ('.getEntity($this->element, 1).
')';
6124 if (is_object($hookmanager)) {
6125 $parameters = array();
6126 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this);
6127 $sql .= $hookmanager->resPrint;
6129 $sql .=
' GROUP BY fk_product_type';
6131 $resql = $this->db->query($sql);
6133 while ($obj = $this->db->fetch_object($resql)) {
6134 if ($obj->fk_product_type == 1) {
6135 $this->nb[
"services"] = $obj->nb;
6137 $this->nb[
"products"] = $obj->nb;
6140 $this->db->free($resql);
6144 $this->error = $this->db->error();
6186 return ($this->mandatory_period == 1 ?
true :
false);
6196 return ($this->status_batch > 0 ?
true :
false);
6216 $dirsociete = array_merge(array(
'/core/modules/barcode/'), $conf->modules_parts[
'barcode']);
6217 foreach ($dirsociete as $dirroot) {
6223 $var = $conf->global->BARCODE_PRODUCT_ADDON_NUM;
6226 $result = $mod->getNextValue($object, $type);
6228 dol_syslog(get_class($this).
"::get_barcode barcode=".$result.
" module=".$var);
6245 $this->specimen = 1;
6247 $this->
ref =
'PRODUCT_SPEC';
6248 $this->label =
'PRODUCT SPECIMEN';
6249 $this->
description =
'This is description of this product specimen that was created the '.dol_print_date($now,
'dayhourlog').
'.';
6250 $this->specimen = 1;
6251 $this->country_id = 1;
6253 $this->status_buy = 1;
6255 $this->note_private =
'This is a comment (private)';
6256 $this->note_public =
'This is a comment (public)';
6257 $this->date_creation = $now;
6258 $this->date_modification = $now;
6261 $this->weight_units = 3;
6264 $this->length_units = 1;
6266 $this->width_units = 0;
6267 $this->height =
null;
6268 $this->height_units =
null;
6270 $this->surface = 30;
6271 $this->surface_units = 0;
6272 $this->volume = 300;
6273 $this->volume_units = 0;
6275 $this->barcode = -1;
6288 if (!$this->fk_unit) {
6292 $langs->load(
'products');
6294 $label_type =
'label';
6295 if ($type ==
'short') {
6296 $label_type =
'short_label';
6299 $sql =
"SELECT ".$label_type.
", code from ".$this->db->prefix().
"c_units where rowid = ".((int) $this->fk_unit);
6301 $resql = $this->db->query($sql);
6302 if ($resql && $this->db->num_rows($resql) > 0) {
6303 $res = $this->db->fetch_array($resql);
6304 $label = ($label_type ==
'short_label' ?
$res[$label_type] :
'unit'.$res[
'code']);
6305 $this->db->free($resql);
6308 $this->error = $this->db->error();
6309 dol_syslog(get_class($this).
"::getLabelOfUnit Error ".$this->error, LOG_ERR);
6325 $maxpricesupplier = 0;
6328 include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
6330 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->
id,
'',
'');
6332 if (is_array($product_fourn_list) && count($product_fourn_list) > 0) {
6333 foreach ($product_fourn_list as $productfourn) {
6334 if ($productfourn->fourn_unitprice > $maxpricesupplier) {
6335 $maxpricesupplier = $productfourn->fourn_unitprice;
6339 $maxpricesupplier *= $conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE;
6343 return $maxpricesupplier;
6359 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
6360 return parent::setCategoriesCommon($categories, Categorie::TYPE_PRODUCT);
6374 'product_customer_price',
6375 'product_customer_price_log'
6396 $sql =
"SELECT rowid, level, fk_level, var_percent, var_min_percent FROM ".$this->db->prefix().
"product_pricerules";
6397 $query = $this->db->query($sql);
6401 while ($result = $this->db->fetch_object($query)) {
6402 $rules[$result->level] = $result;
6411 for ($i = 1; $i <= $nbofproducts; $i++) {
6412 $price = $baseprice;
6413 $price_min = $baseprice;
6417 if ($i > 1 && isset($rules[$i]->var_percent) && $rules[$i]->var_percent) {
6418 $price = $prices[$rules[$i]->fk_level] * (1 + ($rules[$i]->var_percent / 100));
6421 $prices[$i] = $price;
6424 if (isset($rules[$i]->var_min_percent) && $rules[$i]->var_min_percent) {
6425 $price_min = $price * (1 - ($rules[$i]->var_min_percent / 100));
6429 $check_amount = (($price == $this->multiprices[$i]) && ($price_min == $this->multiprices_min[$i]));
6430 $check_type = ($baseprice == $this->multiprices_base_type[$i]);
6432 if ($check_amount && $check_type) {
6436 if ($this->
updatePrice($price, $price_type, $user, $price_vat, $price_min, $i, $npr, $psq,
true) < 0) {
6454 return $user->rights->produit;
6456 return $user->rights->service;
6468 $sql =
"SELECT p.rowid, p.ref, p.datec as date_creation, p.tms as date_modification,";
6469 $sql .=
" p.fk_user_author, p.fk_user_modif";
6470 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as p";
6471 $sql .=
" WHERE p.rowid = ".((int) $id);
6473 $result = $this->db->query($sql);
6475 if ($this->db->num_rows($result)) {
6476 $obj = $this->db->fetch_object($result);
6478 $this->
id = $obj->rowid;
6479 $this->
ref = $obj->ref;
6481 $this->user_creation_id = $obj->fk_user_author;
6482 $this->user_modification_id = $obj->fk_user_modif;
6484 $this->date_creation = $this->db->jdate($obj->date_creation);
6485 $this->date_modification = $this->db->jdate($obj->date_modification);
6488 $this->db->free($result);
6503 if (empty($this->duration_value)) {
6504 $this->errors[]=
'ErrorDurationForServiceNotDefinedCantCalculateHourlyPrice';
6508 if ($this->duration_unit ==
'i') {
6509 $prodDurationHours = 1. / 60;
6511 if ($this->duration_unit ==
'h') {
6512 $prodDurationHours = 1.;
6514 if ($this->duration_unit ==
'd') {
6515 $prodDurationHours = 24.;
6517 if ($this->duration_unit ==
'w') {
6518 $prodDurationHours = 24. * 7;
6520 if ($this->duration_unit ==
'm') {
6521 $prodDurationHours = 24. * 30;
6523 if ($this->duration_unit ==
'y') {
6524 $prodDurationHours = 24. * 365;
6528 return $prodDurationHours;
6541 global $langs,$conf;
6543 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
6545 $return =
'<div class="box-flex-item box-flex-grow-zero">';
6546 $return .=
'<div class="info-box info-box-sm">';
6547 $return .=
'<div class="info-box-img">';
6550 $label .= $this->
show_photos(
'product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 120, 160, 0, 0, 0,
'',
'photoref photokanban');
6560 $return .=
'</div>';
6561 $return .=
'<div class="info-box-content">';
6562 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
6563 if ($selected >= 0) {
6564 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
6566 if (property_exists($this,
'label')) {
6567 $return .=
'<br><span class="info-box-label opacitymedium">'.$this->label.
'</span>';
6569 if (property_exists($this,
'price') && property_exists($this,
'price_ttc')) {
6570 if ($this->price_base_type ==
'TTC') {
6571 $return .=
'<br><span class="info-box-status amount">'.price($this->price_ttc).
' '.$langs->trans(
"TTC").
'</span>';
6574 $return .=
'<br><span class="info-box-status amount">'.price($this->
price).
' '.$langs->trans(
"HT").
'</span>';
6579 if (property_exists($this,
'stock_reel') && $this->
isProduct()) {
6580 $return .=
'<br><div class="info-box-status opacitymedium inline-block valignmiddle">'.img_picto($langs->trans(
'PhysicalStock'),
'stock').
'</div><div class="inline-block valignmiddle paddingleft" title="'.$langs->trans(
'PhysicalStock').
'">'.$this->stock_reel.
'</div>';
6583 if (method_exists($this,
'getLibStatut')) {
6585 $return .=
'<br><div class="info-box-status inline-block valignmiddle">'.$this->getLibStatut(3, 1).
' '.$this->
getLibStatut(3, 0).
'</div>';
6587 $return .=
'<div class="info-box-status inline-block valignmiddle marginleftonly paddingleft">'.$this->getLibStatut(3, 1).
' '.$this->
getLibStatut(3, 0).
'</div>';
6590 $return .=
'</div>';
6591 $return .=
'</div>';
6592 $return .=
'</div>';
6603 public $picto =
'service';
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
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.
File of class to manage predefined price products or services by customer.
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.
const SELL_OR_EAT_BY_MANDATORY_ID_NONE
Const sell or eat by mandatory id.
isStockManaged()
Return if object need to have its stock managed.
$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.
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_sousproduit($id_pere, $id_fils, $qty, $incdec=1, $notrigger=0)
Link a product/service to a parent product/service.
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.
updatePrice($newprice, $newpricebase, $user, $newvat='', $newminprice=0, $level=0, $newnpr=0, $newpbq=0, $ignore_autogen=0, $localtaxes_array=array(), $newdefaultvatcode='', $notrigger=0)
Modify customer price of a product/Service for a given level.
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($id, $user, $notrigger=false, $action='update', $updatetype=false)
Update a record into database.
info($id)
Load information for tab info.
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.
correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $dlc='', $dluo='', $lot='', $inventorycode='', $origin_element='', $origin_id=null, $disablestockchangeforsubproduct=0, $extrafields=null, $force_update_batch=false)
Adjust stock in a warehouse for product with batch number.
$tva_npr
int French VAT NPR is used (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.
del_sousproduit($fk_parent, $fk_child, $notrigger=0)
Remove a link between a subproduct and a parent product/service.
update_sousproduit($id_pere, $id_fils, $qty, $incdec=1, $notrigger=0)
Modify composed product.
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.
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 clickable link of object (with eventually picto)
$product_fourn_id
Id du fournisseur.
$desiredstock
Ask for replenishment when $desiredstock < $stock_reel.
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.
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
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.
Class with list of lots and properties.
Class to manage Dolibarr users.
hasRight($module, $permlevel1, $permlevel2='')
Return if a user has a permission.
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Make control on an uploaded file from an GUI page and move it to final destination.
dol_is_file($pathoffile)
Return if path is a file.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that returns whether VAT must be recoverable collected VAT (e.g.: VAT NPR in France)
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
measuringUnitString($unit, $measuring_style='', $scale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
measuring_units_squared($unit)
Transform a given unit scale into the square of that unit, if known.
measuring_units_cubed($unit)
Transform a given unit scale into the cube of that unit, if known.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type