984 global $action, $extrafields, $langs, $hookmanager;
987 $MAX_EXTRAFIELDS_TO_SHOW_IN_TOOLTIP =
getDolGlobalInt(
'MAX_EXTRAFIELDS_TO_SHOW_IN_TOOLTIP', 3);
993 if (!empty($extrafields->attributes[$this->table_element][
'label'])) {
994 $data[
'opendivextra'] =
'<div class="centpercent wordbreak divtooltipextra">';
995 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
996 if ($extrafields->attributes[$this->table_element][
'type'][$key] ==
'separate') {
999 if ($count >= abs($MAX_EXTRAFIELDS_TO_SHOW_IN_TOOLTIP)) {
1000 $data[
'more_extrafields'] =
'<br>...';
1004 if ($enabled && isset($extrafields->attributes[$this->table_element][
'enabled'][$key])) {
1005 $enabled = (int)
dol_eval($extrafields->attributes[$this->table_element][
'enabled'][$key], 1, 1,
'2');
1007 if ($enabled && isset($extrafields->attributes[$this->table_element][
'list'][$key])) {
1008 $enabled = (int)
dol_eval($extrafields->attributes[$this->table_element][
'list'][$key], 1, 1,
'2');
1011 if ($perms && isset($extrafields->attributes[$this->table_element][
'perms'][$key])) {
1012 $perms = (int)
dol_eval($extrafields->attributes[$this->table_element][
'perms'][$key], 1, 1,
'2');
1014 if (empty($enabled)) {
1017 if (abs($enabled) != 1 && abs($enabled) != 3 && abs($enabled) != 5 && abs($enabled) != 4) {
1020 if (empty($perms)) {
1023 if (!empty($extrafields->attributes[$this->table_element][
'langfile'][$key])) {
1024 $langs->load($extrafields->attributes[$this->table_element][
'langfile'][$key]);
1026 $labelextra = $langs->trans((
string) $extrafields->attributes[$this->table_element][
'label'][$key]);
1027 if ($extrafields->attributes[$this->table_element][
'type'][$key] ==
'separate') {
1028 $data[$key] =
'<br><b><u>'. $labelextra .
'</u></b>';
1030 $value = (empty($this->array_options[
'options_' . $key]) ?
'' : $this->array_options[
'options_' . $key]);
1031 $data[$key] =
'<br><b>'. $labelextra .
':</b> ' . $extrafields->showOutputField($key, $value,
'', $this->table_element);
1035 $data[
'closedivextra'] =
'</div>';
1038 $hookmanager->initHooks(array($this->element .
'dao'));
1039 $parameters = array(
1040 'tooltipcontentarray' => &$data,
1041 'params' => $params,
1044 $hookmanager->executeHooks(
'getTooltipContent', $parameters, $this, $action);
1047 $label = implode($data);
1235 public function add_contact($fk_socpeople, $type_contact, $source =
'external', $notrigger = 0)
1238 global $user, $langs;
1241 dol_syslog(get_class($this).
"::add_contact $fk_socpeople, $type_contact, $source, $notrigger");
1244 if ($fk_socpeople <= 0) {
1245 $langs->load(
"errors");
1246 $this->error = $langs->trans(
"ErrorWrongValueForParameterX",
"1");
1247 dol_syslog(get_class($this).
"::add_contact ".$this->error, LOG_ERR);
1250 if (!$type_contact) {
1251 $langs->load(
"errors");
1252 $this->error = $langs->trans(
"ErrorWrongValueForParameterX",
"2");
1253 dol_syslog(get_class($this).
"::add_contact ".$this->error, LOG_ERR);
1257 $id_type_contact = 0;
1258 if (is_numeric($type_contact)) {
1259 $id_type_contact = $type_contact;
1262 $sql =
"SELECT tc.rowid";
1263 $sql .=
" FROM ".$this->db->prefix().
"c_type_contact as tc";
1264 $sql .=
" WHERE tc.element='".$this->db->escape($this->element).
"'";
1265 $sql .=
" AND tc.source='".$this->db->escape($source).
"'";
1266 $sql .=
" AND tc.code='".$this->db->escape($type_contact).
"' AND tc.active=1";
1268 $resql = $this->db->query($sql);
1270 $obj = $this->db->fetch_object($resql);
1272 $id_type_contact = $obj->rowid;
1277 if ($id_type_contact == 0) {
1278 dol_syslog(
"CODE_NOT_VALID_FOR_THIS_ELEMENT: Code type of contact '".$type_contact.
"' does not exists or is not active for element ".$this->element.
", we can ignore it");
1286 $already_added =
false;
1287 if (is_array($TListeContacts) && !empty($TListeContacts)) {
1288 foreach ($TListeContacts as $array_contact) {
1289 if ($array_contact[
'status'] == 4 && $array_contact[
'id'] == $fk_socpeople && $array_contact[
'fk_c_type_contact'] == $id_type_contact) {
1290 $already_added =
true;
1296 if (!$already_added) {
1300 $sql =
"INSERT INTO ".$this->db->prefix().
"element_contact";
1301 $sql .=
" (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) ";
1302 $sql .=
" VALUES (".$this->id.
", ".((int) $fk_socpeople).
" , ";
1303 $sql .=
"'".$this->db->idate($datecreate).
"'";
1304 $sql .=
", 4, ".((int) $id_type_contact);
1307 $resql = $this->db->query($sql);
1310 $result = $this->
call_trigger(strtoupper($this->element).
'_ADD_CONTACT', $user);
1312 $this->db->rollback();
1317 $this->db->commit();
1320 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
1321 $this->error = $this->db->errno();
1322 $this->db->rollback();
1325 $this->error = $this->db->lasterror();
1326 $this->db->rollback();
1689 public function listeTypeContacts($source =
'internal', $option = 0, $activeonly = 0, $code =
'', $element =
'', $excludeelement =
'')
1691 global $langs, $conf;
1693 $langs->loadLangs(array(
'bills',
'contracts',
'interventions',
'orders',
'projects',
'propal',
'ticket',
'agenda'));
1697 $sql =
"SELECT DISTINCT tc.rowid, tc.code, tc.libelle as type_label, tc.position, tc.element";
1698 $sql .=
" FROM ".$this->db->prefix().
"c_type_contact as tc";
1700 $sqlWhere = array();
1701 if (!empty($element)) {
1702 $sqlWhere[] =
" tc.element='".$this->db->escape($element).
"'";
1704 if (!empty($excludeelement)) {
1705 $sqlWhere[] =
" tc.element <> '".$this->db->escape($excludeelement).
"'";
1708 if ($activeonly == 1) {
1709 $sqlWhere[] =
" tc.active=1";
1712 if (!empty($source) && $source !=
'all') {
1713 $sqlWhere[] =
" tc.source='".$this->db->escape($source).
"'";
1716 if (!empty($code)) {
1717 $sqlWhere[] =
" tc.code='".$this->db->escape($code).
"'";
1720 if (count($sqlWhere) > 0) {
1721 $sql .=
" WHERE ".implode(
' AND ', $sqlWhere);
1724 $sql .= $this->db->order(
'tc.element, tc.position',
'ASC');
1727 $resql = $this->db->query($sql);
1729 $num = $this->db->num_rows($resql);
1731 $langs->loadLangs(array(
"propal",
"orders",
"bills",
"suppliers",
"contracts",
"supplier_proposal"));
1733 while ($obj = $this->db->fetch_object($resql)) {
1734 $modulename = $obj->element;
1735 if (strpos($obj->element,
'project') !==
false) {
1736 $modulename =
'projet';
1737 } elseif ($obj->element ==
'contrat') {
1738 $element =
'contract';
1739 } elseif ($obj->element ==
'action') {
1740 $modulename =
'agenda';
1741 } elseif (strpos($obj->element,
'supplier') !==
false && $obj->element !=
'supplier_proposal') {
1742 $modulename =
'fournisseur';
1744 if (!empty($conf->{$modulename}->enabled)) {
1745 $libelle_element = $langs->trans(
'ContactDefault_'.$obj->element);
1746 $tmpelement = $obj->element;
1747 $transkey =
"TypeContact_".$tmpelement.
"_".$source.
"_".$obj->code;
1748 $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->type_label);
1749 $tab[$obj->rowid] = $libelle_element.
' - '.$libelle_type;
1755 $this->error = $this->db->lasterror();
2174 public function setValueFrom($field, $value, $table =
'', $id =
null, $format =
'', $id_field =
'', $fuser =
null, $trigkey =
'', $fk_user_field =
'fk_user_modif')
2178 if (empty($table)) {
2179 $table = $this->table_element;
2184 if (empty($format)) {
2187 if (empty($id_field)) {
2188 $id_field =
'rowid';
2192 if ($table ==
'product' && $field ==
'note_private') {
2196 if (in_array($table, array(
'actioncomm',
'adherent',
'advtargetemailing',
'cronjob',
'establishment'))) {
2197 $fk_user_field =
'fk_user_mod';
2199 if (in_array($table, array(
'prelevement_bons'))) {
2200 $fk_user_field =
'';
2205 $sql =
"SELECT " . $field;
2206 $sql .=
" FROM " . MAIN_DB_PREFIX . $table;
2207 $sql .=
" WHERE " . $id_field .
" = " . ((int) $id);
2209 $resql = $this->db->query($sql);
2211 if ($obj = $this->db->fetch_object($resql)) {
2212 if ($format ==
'date') {
2213 $oldvalue = $this->db->jdate($obj->$field);
2215 $oldvalue = $obj->$field;
2219 $this->error = $this->db->lasterror();
2230 $sql =
"UPDATE ".$this->db->prefix().$table.
" SET ";
2232 if ($format ==
'text') {
2233 $sql .= $field.
" = '".$this->db->escape($value).
"'";
2234 } elseif ($format ==
'int') {
2235 $sql .= $field.
" = ".((int) $value);
2236 } elseif ($format ==
'date') {
2237 $sql .= $field.
" = ".($value ?
"'".$this->db->idate($value).
"'" :
"null");
2238 } elseif ($format ==
'dategmt') {
2239 $sql .= $field.
" = ".($value ?
"'".$this->db->idate($value,
'gmt').
"'" :
"null");
2242 if ($fk_user_field) {
2243 if (!empty($fuser) && is_object($fuser)) {
2244 $sql .=
", ".$fk_user_field.
" = ".((int) $fuser->id);
2245 } elseif (empty($fuser) || $fuser !=
'none') {
2246 $sql .=
", ".$fk_user_field.
" = ".((int) $user->id);
2250 $sql .=
" WHERE ".$id_field.
" = ".((int) $id);
2252 $resql = $this->db->query($sql);
2256 if (method_exists($this,
'fetch')) {
2257 $result = $this->fetch($id);
2259 $result = $this->fetchCommon($id);
2261 $this->oldcopy = clone $this;
2262 if (property_exists($this->oldcopy, $field)) {
2263 $this->oldcopy->$field = $oldvalue;
2267 $result = $this->call_trigger($trigkey, (!empty($fuser) && is_object($fuser)) ? $fuser : $user);
2275 if (property_exists($this, $field)) {
2276 $this->$field = $value;
2278 $this->db->commit();
2281 $this->db->rollback();
2285 if ($this->db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
2286 $this->error =
'DB_ERROR_RECORD_ALREADY_EXISTS';
2288 $this->error = $this->db->lasterror();
2290 $this->db->rollback();
2309 global $conf, $user;
2311 if (!$this->table_element) {
2312 dol_print_error(
null, get_class($this).
"::load_previous_next_ref was called on object with property table_element not defined");
2315 if ($fieldid ==
'none') {
2320 if (in_array($this->table_element, array(
'facture_rec',
'facture_fourn_rec')) && $fieldid ==
'title') {
2326 if ($user->socid > 0) {
2327 $socid = $user->socid;
2332 $aliastablesociete =
's';
2333 if ($this->element ==
'societe') {
2334 $aliastablesociete =
'te';
2336 $restrictiononfksoc = empty($this->restrictiononfksoc) ? 0 : $this->restrictiononfksoc;
2337 $sql =
"SELECT MAX(te.".$fieldid.
")";
2338 $sql .=
" FROM ".(empty($nodbprefix) ? $this->db->prefix() :
'').$this->table_element.
" as te";
2339 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) {
2340 $tmparray = explode(
'@', $this->ismultientitymanaged);
2341 $sql .=
", ".$this->db->prefix().$tmparray[1].
" as ".($tmparray[1] ==
'societe' ?
's' :
'parenttable');
2342 } elseif ($restrictiononfksoc == 1 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2343 $sql .=
", ".$this->db->prefix().
"societe as s";
2344 } elseif ($restrictiononfksoc == 2 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2345 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON te.fk_soc = s.rowid";
2347 if ($restrictiononfksoc && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2348 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON ".$aliastablesociete.
".rowid = sc.fk_soc";
2350 if ($fieldid ==
'rowid') {
2351 $sql .=
" WHERE te.".$fieldid.
" < ".((int) $this->
id);
2353 $sql .=
" WHERE te.".$fieldid.
" < '".$this->db->escape($this->
ref).
"'";
2355 if ($restrictiononfksoc == 1 && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2356 $sql .=
" AND sc.fk_user = ".((int) $user->id);
2358 if ($restrictiononfksoc == 2 && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2359 $sql .=
" AND (sc.fk_user = ".((int) $user->id).
' OR te.fk_soc IS NULL)';
2362 $filtermax = $filter;
2367 if ($errormessage) {
2368 if (!preg_match(
'/^\s*AND/i', $filtermax)) {
2376 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) {
2377 $tmparray = explode(
'@', $this->ismultientitymanaged);
2378 $sql .=
" AND te.".$tmparray[0].
" = ".($tmparray[1] ==
"societe" ?
"s" :
"parenttable").
".rowid";
2379 } elseif ($restrictiononfksoc == 1 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2380 $sql .=
' AND te.fk_soc = s.rowid';
2382 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
2383 if ($this->element ==
'user' &&
getDolGlobalInt(
'MULTICOMPANY_TRANSVERSE_MODE')) {
2384 if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) {
2385 $sql .=
" AND te.entity IS NOT NULL";
2387 $sql .=
" AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix().
"usergroup_user as ug WHERE ug.entity IN (".
getEntity(
'usergroup').
"))";
2390 $sql .=
' AND te.entity IN ('.getEntity($this->element).
')';
2393 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged) && $this->element !=
'societe') {
2394 $tmparray = explode(
'@', $this->ismultientitymanaged);
2395 $sql .=
' AND parenttable.entity IN ('.getEntity($tmparray[1]).
')';
2397 if ($restrictiononfksoc == 1 && $socid && $this->element !=
'societe') {
2398 $sql .=
' AND te.fk_soc = '.((int) $socid);
2400 if ($restrictiononfksoc == 2 && $socid && $this->element !=
'societe') {
2401 $sql .=
' AND (te.fk_soc = '.((int) $socid).
' OR te.fk_soc IS NULL)';
2403 if ($restrictiononfksoc && $socid && $this->element ==
'societe') {
2404 $sql .=
' AND te.rowid = '.((int) $socid);
2408 $result = $this->db->query($sql);
2410 $this->error = $this->db->lasterror();
2413 $row = $this->db->fetch_row($result);
2414 $this->ref_previous = $row[0];
2416 $sql =
"SELECT MIN(te.".$fieldid.
")";
2417 $sql .=
" FROM ".(empty($nodbprefix) ? $this->db->prefix() :
'').$this->table_element.
" as te";
2418 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) {
2419 $tmparray = explode(
'@', $this->ismultientitymanaged);
2420 $sql .=
", ".$this->db->prefix().$tmparray[1].
" as ".($tmparray[1] ==
'societe' ?
's' :
'parenttable');
2421 } elseif ($restrictiononfksoc == 1 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2422 $sql .=
", ".$this->db->prefix().
"societe as s";
2423 } elseif ($restrictiononfksoc == 2 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2424 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe as s ON te.fk_soc = s.rowid";
2426 if ($restrictiononfksoc && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2427 $sql .=
" LEFT JOIN ".$this->db->prefix().
"societe_commerciaux as sc ON ".$aliastablesociete.
".rowid = sc.fk_soc";
2429 if ($fieldid ==
'rowid') {
2430 $sql .=
" WHERE te.".$fieldid.
" > ".((int) $this->
id);
2432 $sql .=
" WHERE te.".$fieldid.
" > '".$this->db->escape($this->
ref).
"'";
2434 if ($restrictiononfksoc == 1 && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2435 $sql .=
" AND (sc.fk_user = ".((int) $user->id);
2437 $userschilds = $user->getAllChildIds();
2438 $sql .=
" OR sc.fk_user IN (".$this->db->sanitize(implode(
',', $userschilds)).
")";
2442 if ($restrictiononfksoc == 2 && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2443 $sql .=
" AND (sc.fk_user = ".((int) $user->id).
' OR te.fk_soc IS NULL)';
2446 $filtermin = $filter;
2451 if ($errormessage) {
2452 if (!preg_match(
'/^\s*AND/i', $filtermin)) {
2462 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) {
2463 $tmparray = explode(
'@', $this->ismultientitymanaged);
2464 $sql .=
" AND te.".$tmparray[0].
" = ".($tmparray[1] ==
"societe" ?
"s" :
"parenttable").
".rowid";
2465 } elseif ($restrictiononfksoc == 1 && $this->element !=
'societe' && !$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
2466 $sql .=
' AND te.fk_soc = s.rowid';
2468 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
2469 if ($this->element ==
'user' &&
getDolGlobalInt(
'MULTICOMPANY_TRANSVERSE_MODE')) {
2470 if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) {
2471 $sql .=
" AND te.entity IS NOT NULL";
2473 $sql .=
" AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix().
"usergroup_user as ug WHERE ug.entity IN (".
getEntity(
'usergroup').
"))";
2476 $sql .=
' AND te.entity IN ('.getEntity($this->element).
')';
2479 if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged) && $this->element !=
'societe') {
2480 $tmparray = explode(
'@', $this->ismultientitymanaged);
2481 $sql .=
' AND parenttable.entity IN ('.getEntity($tmparray[1]).
')';
2483 if ($restrictiononfksoc == 1 && $socid && $this->element !=
'societe') {
2484 $sql .=
' AND te.fk_soc = '.((int) $socid);
2486 if ($restrictiononfksoc == 2 && $socid && $this->element !=
'societe') {
2487 $sql .=
' AND (te.fk_soc = '.((int) $socid).
' OR te.fk_soc IS NULL)';
2489 if ($restrictiononfksoc && $socid && $this->element ==
'societe') {
2490 $sql .=
' AND te.rowid = '.((int) $socid);
2495 $result = $this->db->query($sql);
2497 $this->error = $this->db->lasterror();
2500 $row = $this->db->fetch_row($result);
2501 $this->ref_next = $row[0];
3783 public function update_price($exclspec = 0, $roundingadjust =
'auto', $nodatabaseupdate = 0, $seller =
null)
3786 global $conf, $hookmanager, $action;
3788 $parameters = array(
'exclspec' => $exclspec,
'roundingadjust' => $roundingadjust,
'nodatabaseupdate' => $nodatabaseupdate,
'seller' => $seller);
3789 $reshook = $hookmanager->executeHooks(
'updateTotalPrice', $parameters, $this, $action);
3792 } elseif ($reshook < 0) {
3797 $isElementForSupplier =
false;
3798 $roundTotalConstName =
'MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND';
3800 if ($this->element ==
'propal') {
3801 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_PROPOSAL";
3802 } elseif ($this->element ==
'commande' || $this->element ==
'order') {
3803 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_ORDER";
3804 } elseif ($this->element ==
'facture' || $this->element ==
'invoice') {
3805 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_INVOICE";
3806 } elseif ($this->element ==
'facture_fourn' || $this->element ==
'supplier_invoice' || $this->element ==
'invoice_supplier' || $this->element ==
'invoice_supplier_rec') {
3807 $isElementForSupplier =
true;
3808 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_INVOICE";
3809 } elseif ($this->element ==
'order_supplier' || $this->element ==
'supplier_order') {
3810 $isElementForSupplier =
true;
3811 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_ORDER";
3812 } elseif ($this->element ==
'supplier_proposal') {
3813 $isElementForSupplier =
true;
3814 $MODULE =
"MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_PROPOSAL";
3816 if ($isElementForSupplier) {
3817 $roundTotalConstName =
'MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND_SUPPLIER';
3820 if (!empty($MODULE)) {
3823 foreach ($modsactivated as $mod) {
3824 if (isModEnabled($mod)) {
3831 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3833 $forcedroundingmode = $roundingadjust;
3834 if ($forcedroundingmode ==
'auto' && isset($conf->global->{$roundTotalConstName})) {
3836 } elseif ($forcedroundingmode ==
'auto') {
3837 $forcedroundingmode =
'0';
3842 $multicurrency_tx = !empty($this->multicurrency_tx) ? $this->multicurrency_tx : 1;
3845 $fieldtva =
'total_tva';
3846 $fieldlocaltax1 =
'total_localtax1';
3847 $fieldlocaltax2 =
'total_localtax2';
3848 $fieldup =
'subprice';
3849 $base_price_type =
'HT';
3850 if ($this->element ==
'facture_fourn' || $this->element ==
'invoice_supplier') {
3854 if ($this->element ==
'invoice_supplier_rec') {
3857 if ($this->element ==
'expensereport') {
3858 $fieldup =
'value_unit';
3859 $base_price_type =
'TTC';
3862 $sql =
"SELECT rowid, qty, ".$fieldup.
" as up, remise_percent, total_ht, ".$fieldtva.
" as total_tva, total_ttc, ".$fieldlocaltax1.
" as total_localtax1, ".$fieldlocaltax2.
" as total_localtax2,";
3863 $sql .=
' tva_tx as vatrate, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, info_bits, product_type';
3864 if ($this->table_element_line ==
'facturedet') {
3865 $sql .=
', situation_percent';
3867 $sql .=
', multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
3868 $sql .=
" FROM ".$this->db->prefix().$this->table_element_line;
3869 $sql .=
" WHERE ".$this->fk_element.
" = ".((int) $this->
id);
3871 $product_field =
'product_type';
3872 if ($this->table_element_line ==
'contratdet') {
3873 $product_field =
'';
3875 if ($product_field) {
3876 $sql .=
" AND ".$product_field.
" <> 9";
3879 $sql .=
' ORDER by rowid';
3881 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
3883 $resql = $this->db->query($sql);
3885 $this->total_ht = 0;
3886 $this->total_tva = 0;
3887 $this->total_localtax1 = 0;
3888 $this->total_localtax2 = 0;
3889 $this->total_ttc = 0;
3890 $total_ht_by_vats = array();
3891 $total_tva_by_vats = array();
3892 $total_ttc_by_vats = array();
3893 $this->multicurrency_total_ht = 0;
3894 $this->multicurrency_total_tva = 0;
3895 $this->multicurrency_total_ttc = 0;
3899 $num = $this->db->num_rows($resql);
3902 $obj = $this->db->fetch_object($resql);
3905 $parameters = array(
'fk_element' => $obj->rowid);
3906 $reshook = $hookmanager->executeHooks(
'changeRoundingMode', $parameters, $this, $action);
3908 if (empty($reshook) && $forcedroundingmode ==
'0') {
3910 $localtax_array = array($obj->localtax1_type, $obj->localtax1_tx, $obj->localtax2_type, $obj->localtax2_tx);
3911 $tmpcal =
calcul_price_total($obj->qty, $obj->up, $obj->remise_percent, $obj->vatrate, $obj->localtax1_tx, $obj->localtax2_tx, 0, $base_price_type, $obj->info_bits, $obj->product_type, $seller, $localtax_array, (isset($obj->situation_percent) ? $obj->situation_percent : 100), $multicurrency_tx);
3913 $diff_when_using_price_ht =
price2num($tmpcal[1] - $obj->total_tva,
'MT', 1);
3914 $diff_on_current_total =
price2num($obj->total_ttc - $obj->total_ht - $obj->total_tva - $obj->total_localtax1 - $obj->total_localtax2,
'MT', 1);
3918 if ($diff_on_current_total) {
3920 $sqlfix =
"UPDATE ".$this->db->prefix().$this->table_element_line.
" SET ".$fieldtva.
" = ".
price2num((
float) $tmpcal[1]).
", total_ttc = ".
price2num((
float) $tmpcal[2]).
" WHERE rowid = ".((int) $obj->rowid);
3921 dol_syslog(
'We found inconsistent data into detailed line (diff_on_current_total = '.$diff_on_current_total.
') for line rowid = '.$obj->rowid.
" (ht=".$obj->total_ht.
" vat=".$obj->total_tva.
" tax1=".$obj->total_localtax1.
" tax2=".$obj->total_localtax2.
" ttc=".$obj->total_ttc.
"). We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix, LOG_WARNING);
3922 $resqlfix = $this->db->query($sqlfix);
3926 $obj->total_tva = $tmpcal[1];
3927 $obj->total_ttc = $tmpcal[2];
3928 } elseif ($diff_when_using_price_ht) {
3931 $sqlfix =
"UPDATE ".$this->db->prefix().$this->table_element_line.
" SET ".$fieldtva.
" = ".
price2num((
float) $tmpcal[1]).
", total_ttc = ".
price2num((
float) $tmpcal[2]).
" WHERE rowid = ".((int) $obj->rowid);
3932 dol_syslog(
'We found a line with different rounding data into detailed line (diff_when_using_price_ht = '.$diff_when_using_price_ht.
' and diff_on_current_total = '.$diff_on_current_total.
') for line rowid = '.$obj->rowid.
" (total vat of line calculated=".$tmpcal[1].
", database=".$obj->total_tva.
"). We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix);
3933 $resqlfix = $this->db->query($sqlfix);
3937 $obj->total_tva = $tmpcal[1];
3938 $obj->total_ttc = $tmpcal[2];
3942 $this->total_ht += $obj->total_ht;
3943 $this->total_tva += $obj->total_tva;
3944 $this->total_localtax1 += $obj->total_localtax1;
3945 $this->total_localtax2 += $obj->total_localtax2;
3946 $this->total_ttc += $obj->total_ttc;
3947 $this->multicurrency_total_ht += $obj->multicurrency_total_ht;
3948 $this->multicurrency_total_tva += $obj->multicurrency_total_tva;
3949 $this->multicurrency_total_ttc += $obj->multicurrency_total_ttc;
3951 if (!isset($total_ht_by_vats[$obj->vatrate])) {
3952 $total_ht_by_vats[$obj->vatrate] = 0;
3954 if (!isset($total_tva_by_vats[$obj->vatrate])) {
3955 $total_tva_by_vats[$obj->vatrate] = 0;
3957 if (!isset($total_ttc_by_vats[$obj->vatrate])) {
3958 $total_ttc_by_vats[$obj->vatrate] = 0;
3960 $total_ht_by_vats[$obj->vatrate] += $obj->total_ht;
3961 $total_tva_by_vats[$obj->vatrate] += $obj->total_tva;
3962 $total_ttc_by_vats[$obj->vatrate] += $obj->total_ttc;
3964 if ($forcedroundingmode ==
'1') {
3965 $tmpvat =
price2num($total_ht_by_vats[$obj->vatrate] * $obj->vatrate / 100,
'MT', 1);
3966 $diff =
price2num($total_tva_by_vats[$obj->vatrate] - (
float) $tmpvat,
'MT', 1);
3969 $maxdiff = (10 * pow(10, -1 *
getDolGlobalInt(
'MAIN_MAX_DECIMALS_TOT', 0)));
3970 if (abs((
float) $diff) > $maxdiff) {
3972 $errmsg =
'We found a rounding difference after line '.($obj->rowid).
' between HT*VAT='.$tmpvat.
' and total in database='.$total_tva_by_vats[$obj->vatrate].
' (calculated with UP*qty) but diff='.$diff.
' is too high (> '.$maxdiff.
') to be corrected. Some data in your lines may be corrupted. Try to edit each line manually to fix this before restarting.';
3974 $this->error = $errmsg;
3978 $sqlfix =
"UPDATE ".$this->db->prefix().$this->table_element_line.
" SET ".$fieldtva.
" = ".
price2num($obj->total_tva - (
float) $diff).
", total_ttc = ".
price2num($obj->total_ttc - (
float) $diff).
" WHERE rowid = ".((int) $obj->rowid);
3979 dol_syslog(
'We found a difference of '.$diff.
' for line rowid = '.$obj->rowid.
' between TotalHT('.$total_ht_by_vats[$obj->vatrate].
')*VATrate('.$obj->vatrate.
')='.$tmpvat.
' and total in database='.$total_tva_by_vats[$obj->vatrate].
" (calculated with UP*qty). We fix the total_vat and total_ttc of line by running sqlfix = ".$sqlfix);
3981 $resqlfix = $this->db->query($sqlfix);
3987 $this->total_tva = (float)
price2num($this->total_tva - (
float) $diff,
'', 1);
3988 $this->total_ttc = (float)
price2num($this->total_ttc - (
float) $diff,
'', 1);
3989 $total_tva_by_vats[$obj->vatrate] = (float)
price2num($total_tva_by_vats[$obj->vatrate] - (
float) $diff,
'', 1);
3990 $total_ttc_by_vats[$obj->vatrate] = (float)
price2num($total_ttc_by_vats[$obj->vatrate] - (
float) $diff,
'', 1);
3998 $this->total_ttc += isset($this->revenuestamp) ? $this->revenuestamp : 0;
3999 $this->multicurrency_total_ttc += isset($this->revenuestamp) ? ($this->revenuestamp * $multicurrency_tx) : 0;
4002 if (!empty($this->situation_cycle_ref) && !empty($this->situation_counter) && $this->situation_counter > 1 && method_exists($this,
'get_prev_sits')) {
4003 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
4006 $prev_sits = $this->get_prev_sits();
4008 foreach ($prev_sits as $sit) {
4009 $this->total_ht -= $sit->total_ht;
4010 $this->total_tva -= $sit->total_tva;
4011 $this->total_localtax1 -= $sit->total_localtax1;
4012 $this->total_localtax2 -= $sit->total_localtax2;
4013 $this->total_ttc -= $sit->total_ttc;
4014 $this->multicurrency_total_ht -= $sit->multicurrency_total_ht;
4015 $this->multicurrency_total_tva -= $sit->multicurrency_total_tva;
4016 $this->multicurrency_total_ttc -= $sit->multicurrency_total_ttc;
4023 $this->total_ht = (float)
price2num($this->total_ht);
4024 $this->total_tva = (float)
price2num($this->total_tva);
4025 $this->total_localtax1 = (float)
price2num($this->total_localtax1);
4026 $this->total_localtax2 = (float)
price2num($this->total_localtax2);
4027 $this->total_ttc = (float)
price2num($this->total_ttc);
4029 $this->db->free($resql);
4032 $fieldht =
'total_ht';
4034 $fieldlocaltax1 =
'localtax1';
4035 $fieldlocaltax2 =
'localtax2';
4036 $fieldttc =
'total_ttc';
4038 if (in_array($this->element, array(
'propal',
'commande',
'facture',
'facturerec',
'supplier_proposal',
'order_supplier',
'facture_fourn',
'invoice_supplier',
'invoice_supplier_rec',
'expensereport'))) {
4039 $fieldtva =
'total_tva';
4042 if (!$error && empty($nodatabaseupdate)) {
4043 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
4044 $sql .=
" ".$fieldht.
" = ".((float)
price2num($this->total_ht,
'MT', 1)).
",";
4045 $sql .=
" ".$fieldtva.
" = ".((float)
price2num($this->total_tva,
'MT', 1)).
",";
4046 $sql .=
" ".$fieldlocaltax1.
" = ".((float)
price2num($this->total_localtax1,
'MT', 1)).
",";
4047 $sql .=
" ".$fieldlocaltax2.
" = ".((float)
price2num($this->total_localtax2,
'MT', 1)).
",";
4048 $sql .=
" ".$fieldttc.
" = ".((float)
price2num($this->total_ttc,
'MT', 1));
4049 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht,
'MT', 1));
4050 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva,
'MT', 1));
4051 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc,
'MT', 1));
4052 $sql .=
" WHERE rowid = ".((int) $this->
id);
4054 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
4055 $resql = $this->db->query($sql);
4059 $this->error = $this->db->lasterror();
4060 $this->errors[] = $this->db->lasterror();
4065 $this->db->commit();
4068 $this->db->rollback();
4200 public function fetchObjectLinked($sourceid =
null, $sourcetype =
'', $targetid =
null, $targettype =
'', $clause =
'OR', $alsosametype = 1, $orderby =
'sourcetype', $loadalsoobjects = 1)
4202 global $conf, $hookmanager, $action;
4207 if ($this->
id > 0 && !empty($this->linkedObjectsFullLoaded[$this->
id])) {
4211 $this->linkedObjectsIds = array();
4212 $this->linkedObjects = array();
4214 $justsource =
false;
4215 $justtarget =
false;
4216 $withtargettype =
false;
4217 $withsourcetype =
false;
4219 $parameters = array(
'sourcetype' => $sourcetype,
'sourceid' => $sourceid,
'targettype' => $targettype,
'targetid' => $targetid);
4221 $reshook = $hookmanager->executeHooks(
'setLinkedObjectSourceTargetType', $parameters, $this, $action);
4223 if (!empty($hookmanager->resArray[
'sourcetype'])) {
4224 $sourcetype = $hookmanager->resArray[
'sourcetype'];
4226 if (!empty($hookmanager->resArray[
'sourceid'])) {
4227 $sourceid = $hookmanager->resArray[
'sourceid'];
4229 if (!empty($hookmanager->resArray[
'targettype'])) {
4230 $targettype = $hookmanager->resArray[
'targettype'];
4232 if (!empty($hookmanager->resArray[
'targetid'])) {
4233 $targetid = $hookmanager->resArray[
'targetid'];
4237 if (!empty($sourceid) && !empty($sourcetype) && empty($targetid)) {
4239 if (!empty($targettype)) {
4240 $withtargettype =
true;
4243 if (!empty($targetid) && !empty($targettype) && empty($sourceid)) {
4245 if (!empty($sourcetype)) {
4246 $withsourcetype =
true;
4250 $sourceid = (!empty($sourceid) ? $sourceid : $this->id);
4251 $targetid = (!empty($targetid) ? $targetid : $this->id);
4252 $sourcetype = (!empty($sourcetype) ? $sourcetype : $this->element);
4253 $targettype = (!empty($targettype) ? $targettype : $this->element);
4262 $sql =
"SELECT rowid, fk_source, sourcetype, fk_target, targettype";
4263 $sql .=
" FROM ".$this->db->prefix().
"element_element";
4265 if ($justsource || $justtarget) {
4267 $sql .=
"fk_source = ".((int) $sourceid).
" AND sourcetype = '".$this->db->escape($sourcetype).
"'";
4268 if ($withtargettype) {
4269 $sql .=
" AND targettype = '".$this->db->escape($targettype).
"'";
4271 } elseif ($justtarget) {
4272 $sql .=
"fk_target = ".((int) $targetid).
" AND targettype = '".$this->db->escape($targettype).
"'";
4273 if ($withsourcetype) {
4274 $sql .=
" AND sourcetype = '".$this->db->escape($sourcetype).
"'";
4278 $sql .=
"(fk_source = ".((int) $sourceid).
" AND sourcetype = '".$this->db->escape($sourcetype).
"')";
4279 $sql .=
" ".$clause.
" (fk_target = ".((int) $targetid).
" AND targettype = '".$this->db->escape($targettype).
"')";
4280 if ($loadalsoobjects && $this->
id > 0 && $sourceid == $this->
id && $sourcetype == $this->element && $targetid == $this->
id && $targettype == $this->element && $clause ==
'OR') {
4281 $this->linkedObjectsFullLoaded[$this->id] =
true;
4284 $sql .=
" ORDER BY ".$orderby;
4286 dol_syslog(get_class($this).
"::fetchObjectLink", LOG_DEBUG);
4287 $resql = $this->db->query($sql);
4289 $num = $this->db->num_rows($resql);
4292 $obj = $this->db->fetch_object($resql);
4293 if ($justsource || $justtarget) {
4295 $this->linkedObjectsIds[$obj->targettype][$obj->rowid] = $obj->fk_target;
4296 } elseif ($justtarget) {
4297 $this->linkedObjectsIds[$obj->sourcetype][$obj->rowid] = $obj->fk_source;
4300 if ($obj->fk_source == $sourceid && $obj->sourcetype == $sourcetype) {
4301 $this->linkedObjectsIds[$obj->targettype][$obj->rowid] = $obj->fk_target;
4303 if ($obj->fk_target == $targetid && $obj->targettype == $targettype) {
4304 $this->linkedObjectsIds[$obj->sourcetype][$obj->rowid] = $obj->fk_source;
4310 if (!empty($this->linkedObjectsIds)) {
4311 $tmparray = $this->linkedObjectsIds;
4312 foreach ($tmparray as $objecttype => $objectids) {
4314 $element = $element_properties[
'element'];
4315 $classPath = $element_properties[
'classpath'];
4316 $classFile = $element_properties[
'classfile'];
4317 $className = $element_properties[
'classname'];
4318 $module = $element_properties[
'module'];
4321 if (isModEnabled($module) && (($element != $this->element) || $alsosametype)) {
4322 if ($loadalsoobjects && (is_numeric($loadalsoobjects) || ($loadalsoobjects === $objecttype))) {
4324 if (class_exists($className)) {
4325 foreach ($objectids as $i => $objectid) {
4326 $object =
new $className($this->db);
4327 $ret =
$object->fetch($objectid);
4329 $this->linkedObjects[$objecttype][$i] =
$object;
4335 unset($this->linkedObjectsIds[$objecttype]);
4979 if (empty($this->lines)) {
4980 return array(
'weight' => $totalWeight,
'volume' => $totalVolume,
'ordered' => $totalOrdered,
'toship' => $totalToShip);
4983 foreach ($this->lines as $line) {
4984 if (isset($line->qty_asked)) {
4985 $totalOrdered += $line->qty_asked;
4987 if (isset($line->qty_shipped)) {
4988 $totalToShip += $line->qty_shipped;
4989 } elseif ($line->element ==
'commandefournisseurdispatch' && isset($line->qty)) {
4990 if (empty($totalToShip)) {
4993 $totalToShip += $line->qty;
4997 if ($this->element ==
'shipping') {
4999 $qty = $line->qty_shipped ? $line->qty_shipped : 0;
5001 $qty = $line->qty ? $line->qty : 0;
5004 $weight = !empty($line->weight) ? $line->weight : 0;
5005 ($weight == 0 && !empty($line->product->weight)) ? $weight = $line->product->weight : 0;
5006 $volume = !empty($line->volume) ? $line->volume : 0;
5007 ($volume == 0 && !empty($line->product->volume)) ? $volume = $line->product->volume : 0;
5009 $weight_units = !empty($line->weight_units) ? $line->weight_units : 0;
5010 ($weight_units == 0 && !empty($line->product->weight_units)) ? $weight_units = $line->product->weight_units : 0;
5011 $volume_units = !empty($line->volume_units) ? $line->volume_units : 0;
5012 ($volume_units == 0 && !empty($line->product->volume_units)) ? $volume_units = $line->product->volume_units : 0;
5016 if (!empty($weight_units)) {
5017 $weightUnit = $weight_units;
5019 if (!empty($volume_units)) {
5020 $volumeUnit = $volume_units;
5023 if (empty($totalWeight)) {
5026 if (empty($totalVolume)) {
5031 if ($weight_units < 50) {
5032 $trueWeightUnit = pow(10, $weightUnit);
5033 $totalWeight += $weight * $qty * $trueWeightUnit;
5035 if ($weight_units == 99) {
5037 $trueWeightUnit = 0.45359237;
5038 $totalWeight += $weight * $qty * $trueWeightUnit;
5039 } elseif ($weight_units == 98) {
5041 $trueWeightUnit = 0.0283495;
5042 $totalWeight += $weight * $qty * $trueWeightUnit;
5044 $totalWeight += $weight * $qty;
5047 if ($volume_units < 50) {
5049 $trueVolumeUnit = pow(10, $volumeUnit);
5051 $totalVolume += $volume * $qty * $trueVolumeUnit;
5053 $totalVolume += $volume * $qty;
5057 return array(
'weight' => $totalWeight,
'volume' => $totalVolume,
'ordered' => $totalOrdered,
'toship' => $totalToShip);
5156 public function printObjectLines($action, $seller, $buyer, $selected = 0, $dateSelector = 0, $defaulttpldir =
'/core/tpl')
5158 global $conf, $hookmanager, $langs, $user, $form, $extrafields,
$object;
5160 global $inputalsopricewithtax, $usemargins, $disableedit, $disablemove, $disableremove, $outputalsopricetotalwithtax;
5164 if (isModEnabled(
'margin') && !empty($this->element) && in_array($this->element, array(
'facture',
'facturerec',
'propal',
'commande'))) {
5168 $num = count($this->lines);
5171 if (!is_object($extrafields)) {
5172 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
5175 $extrafields->fetch_name_optionals_label($this->table_element_line);
5177 $parameters = array(
'num' => $num,
'dateSelector' => $dateSelector,
'seller' => $seller,
'buyer' => $buyer,
'selected' => $selected,
'table_element_line' => $this->table_element_line);
5178 $reshook = $hookmanager->executeHooks(
'printObjectLineTitle', $parameters, $this, $action);
5179 if (empty($reshook)) {
5183 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5184 foreach ($dirtpls as $module => $reldir) {
5186 if (!empty($module)) {
5189 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_title.tpl.php';
5191 if (file_exists($tpl)) {
5192 if (empty($conf->file->strict_mode)) {
5193 $res = @include $tpl;
5195 $res = include $tpl;
5206 print
"<!-- begin printObjectLines() --><tbody>\n";
5207 foreach ($this->lines as $line) {
5209 $line->fetch_optionals();
5212 if (is_object($hookmanager)) {
5213 if (empty($line->fk_parent_line)) {
5214 $parameters = array(
'line' => $line,
'num' => $num,
'i' => $i,
'dateSelector' => $dateSelector,
'seller' => $seller,
'buyer' => $buyer,
'selected' => $selected,
'table_element_line' => $line->table_element,
'defaulttpldir' => $defaulttpldir);
5215 $reshook = $hookmanager->executeHooks(
'printObjectLine', $parameters, $this, $action);
5217 $parameters = array(
'line' => $line,
'num' => $num,
'i' => $i,
'dateSelector' => $dateSelector,
'seller' => $seller,
'buyer' => $buyer,
'selected' => $selected,
'table_element_line' => $line->table_element,
'fk_parent_line' => $line->fk_parent_line,
'defaulttpldir' => $defaulttpldir);
5218 $reshook = $hookmanager->executeHooks(
'printObjectSubLine', $parameters, $this, $action);
5221 if (empty($reshook)) {
5222 $this->printObjectLine($action, $line,
'', $num, $i, $dateSelector, $seller, $buyer, $selected, $extrafields, $defaulttpldir);
5227 print
"</tbody><!-- end printObjectLines() -->\n";
5247 public function printObjectLine($action, $line, $var, $num, $i, $dateSelector, $seller, $buyer, $selected = 0, $extrafields =
null, $defaulttpldir =
'/core/tpl')
5249 global $conf, $langs, $user,
$object, $hookmanager;
5251 global $object_rights, $disableedit, $disablemove, $disableremove;
5253 $object_rights = $this->getRights();
5260 if ($action !=
'editline' || $selected != $line->id) {
5262 if (!empty($line->fk_product) && $line->fk_product > 0) {
5263 $product_static =
new Product($this->db);
5264 $product_static->fetch($line->fk_product);
5266 $product_static->ref = $line->ref;
5267 $product_static->label = !empty($line->label) ? $line->label :
"";
5269 $text = $product_static->getNomUrl(1);
5273 if (property_exists($this,
'socid') && !is_object($this->thirdparty)) {
5274 dol_print_error(
null,
'Error: Method printObjectLine was called on an object and object->fetch_thirdparty was not done before');
5278 $prod =
new Product($this->db);
5279 $prod->fetch($line->fk_product);
5281 $outputlangs = $langs;
5283 if (empty($newlang) &&
GETPOST(
'lang_id',
'aZ09')) {
5284 $newlang =
GETPOST(
'lang_id',
'aZ09');
5286 if (
getDolGlobalString(
'PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE') && empty($newlang) && is_object($this->thirdparty)) {
5287 $newlang = $this->thirdparty->default_lang;
5289 if (!empty($newlang)) {
5290 $outputlangs =
new Translate(
"", $conf);
5291 $outputlangs->setDefaultLang($newlang);
5294 $label = (!empty($prod->multilangs[$outputlangs->defaultlang][
"label"])) ? $prod->multilangs[$outputlangs->defaultlang][
"label"] : $line->product_label;
5296 $label = $line->product_label;
5299 $text .=
' - '.(!empty($line->label) ? $line->label : $label);
5300 $description .= (
getDolGlobalInt(
'PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE') ?
'' : (!empty($line->description) ?
dol_htmlentitiesbr($line->description) :
''));
5303 $line->pu_ttc =
price2num((!empty($line->subprice) ? $line->subprice : 0) * (1 + ((!empty($line->tva_tx) ? $line->tva_tx : 0) / 100)),
'MU');
5308 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5309 foreach ($dirtpls as $module => $reldir) {
5311 if (!empty($module)) {
5314 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_view.tpl.php';
5317 if (file_exists($tpl)) {
5318 if (empty($conf->file->strict_mode)) {
5319 $res = @include $tpl;
5321 $res = include $tpl;
5331 if ($this->
status == 0 && $action ==
'editline' && $selected == $line->id) {
5332 $label = (!empty($line->label) ? $line->label : (($line->fk_product > 0) ? $line->product_label :
''));
5334 $line->pu_ttc =
price2num($line->subprice * (1 + ($line->tva_tx / 100)),
'MU');
5339 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5340 foreach ($dirtpls as $module => $reldir) {
5341 if (!empty($module)) {
5344 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_edit.tpl.php';
5347 if (empty($conf->file->strict_mode)) {
5348 $res = @include $tpl;
5350 $res = include $tpl;
5428 public function printOriginLine($line, $var, $restrictlist =
'', $defaulttpldir =
'/core/tpl', $selectedLines = array())
5430 global $langs, $conf;
5433 if (!empty($line->date_start)) {
5434 $date_start = $line->date_start;
5436 $date_start = $line->date_debut_prevue;
5437 if ($line->date_debut_reel) {
5438 $date_start = $line->date_debut_reel;
5441 if (!empty($line->date_end)) {
5442 $date_end = $line->date_end;
5444 $date_end = $line->date_fin_prevue;
5445 if ($line->date_fin_reel) {
5446 $date_end = $line->date_fin_reel;
5450 $this->tpl[
'id'] = $line->id;
5452 $this->tpl[
'label'] =
'';
5453 if (!empty($line->fk_parent_line)) {
5454 $this->tpl[
'label'] .=
img_picto(
'',
'rightarrow');
5457 if (($line->info_bits & 2) == 2) {
5459 if (property_exists($this,
'socid')) {
5460 $discount->fk_soc = $this->socid;
5461 $discount->socid = $this->socid;
5463 $this->tpl[
'label'] .= $discount->getNomUrl(0,
'discount');
5464 } elseif (!empty($line->fk_product)) {
5465 $productstatic =
new Product($this->db);
5466 $productstatic->id = $line->fk_product;
5467 $productstatic->ref = $line->ref;
5468 $productstatic->type = $line->fk_product_type;
5469 if (empty($productstatic->ref)) {
5470 $line->fetch_product();
5471 $productstatic = $line->product;
5474 $this->tpl[
'label'] .= $productstatic->getNomUrl(1);
5475 $this->tpl[
'label'] .=
' - '.(!empty($line->label) ? $line->label : $line->product_label);
5477 if ($line->product_type == 1 && ($date_start || $date_end)) {
5481 $this->tpl[
'label'] .= ($line->product_type == -1 ?
' ' : ($line->product_type == 1 ?
img_object($langs->trans(
''),
'service') :
img_object($langs->trans(
''),
'product')));
5482 if (!empty($line->desc)) {
5483 $this->tpl[
'label'] .= $line->desc;
5485 $this->tpl[
'label'] .= ($line->label ?
' '.$line->label :
'');
5489 if ($line->product_type == 1 && ($date_start || $date_end)) {
5494 if (!empty($line->desc)) {
5495 if ($line->desc ==
'(CREDIT_NOTE)') {
5497 $discount->fetch($line->fk_remise_except);
5498 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromCreditNote", $discount->getNomUrl(0));
5499 } elseif ($line->desc ==
'(DEPOSIT)') {
5501 $discount->fetch($line->fk_remise_except);
5502 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromDeposit", $discount->getNomUrl(0));
5503 } elseif ($line->desc ==
'(EXCESS RECEIVED)') {
5505 $discount->fetch($line->fk_remise_except);
5506 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromExcessReceived", $discount->getNomUrl(0));
5507 } elseif ($line->desc ==
'(EXCESS PAID)') {
5509 $discount->fetch($line->fk_remise_except);
5510 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromExcessPaid", $discount->getNomUrl(0));
5512 $this->tpl[
'description'] =
dol_trunc($line->desc, 60);
5515 $this->tpl[
'description'] =
' ';
5519 $this->tpl[
'vat_rate'] =
vatrate($line->tva_tx,
true);
5520 $this->tpl[
'vat_rate'] .= (($line->info_bits & 1) == 1) ?
'*' :
'';
5521 if (!empty($line->vat_src_code) && !preg_match(
'/\(/', $this->tpl[
'vat_rate'])) {
5522 $this->tpl[
'vat_rate'] .=
' ('.$line->vat_src_code.
')';
5525 $this->tpl[
'price'] =
price($line->subprice);
5526 $this->tpl[
'total_ht'] =
price($line->total_ht);
5527 $this->tpl[
'multicurrency_price'] =
price($line->multicurrency_subprice);
5528 $this->tpl[
'qty'] = (($line->info_bits & 2) != 2) ? $line->qty :
' ';
5530 $this->tpl[
'unit'] = $langs->transnoentities($line->getLabelOfUnit(
'long'));
5532 $this->tpl[
'remise_percent'] = (($line->info_bits & 2) != 2) ?
vatrate($line->remise_percent,
true) :
' ';
5535 $this->tpl[
'strike'] = 0;
5537 $this->tpl[
'strike'] = 1;
5542 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5543 foreach ($dirtpls as $module => $reldir) {
5544 if (!empty($module)) {
5547 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/originproductline.tpl.php';
5550 if (empty($conf->file->strict_mode)) {
5551 $res = @include $tpl;
5553 $res = include $tpl;
5676 protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams =
null)
5678 global $conf, $langs, $user, $hookmanager, $action;
5680 $srctemplatepath =
'';
5682 $parameters = array(
'modelspath' => $modelspath,
'modele' => $modele,
'outputlangs' => $outputlangs,
'hidedetails' => $hidedetails,
'hidedesc' => $hidedesc,
'hideref' => $hideref,
'moreparams' => $moreparams);
5683 $reshook = $hookmanager->executeHooks(
'commonGenerateDocument', $parameters, $this, $action);
5685 if (!empty($reshook)) {
5689 dol_syslog(
"commonGenerateDocument modele=".$modele.
" outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
5691 if (empty($modele)) {
5692 $this->error =
'BadValueForParameterModele';
5697 $err = error_reporting();
5699 @set_time_limit(120);
5700 error_reporting($err);
5703 $tmp = explode(
':', $modele, 2);
5704 $saved_model = $modele;
5705 if (!empty($tmp[1])) {
5707 $srctemplatepath = $tmp[1];
5714 $dirmodels = array(
'/');
5715 if (is_array($conf->modules_parts[
'models'])) {
5716 $dirmodels = array_merge($dirmodels, $conf->modules_parts[
'models']);
5718 foreach ($dirmodels as $reldir) {
5719 foreach (array(
'doc',
'pdf') as $prefix) {
5720 if (in_array(get_class($this), array(
'Adherent'))) {
5722 $file = $prefix.
"_".$modele.
".class.php";
5725 $file = $prefix.
"_".$modele.
".modules.php";
5732 if (file_exists($file)) {
5734 $classname = $prefix.
'_'.$modele;
5743 if ($filefound ===
'' || $classname ===
'') {
5744 $this->error = $langs->trans(
"Error").
' Failed to load doc generator with modelpaths='.$modelspath.
' - modele='.$modele;
5745 $this->errors[] = $this->error;
5756 require_once $filefound;
5758 $obj =
new $classname($this->db);
5761 if ($obj->type ==
'odt' && empty($srctemplatepath)) {
5762 $varfortemplatedir = $obj->scandir;
5766 $listoffiles = array();
5769 $listofdir = explode(
',', $dirtoscan);
5770 foreach ($listofdir as $key => $tmpdir) {
5771 $tmpdir = trim($tmpdir);
5772 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
5774 unset($listofdir[$key]);
5777 if (is_dir($tmpdir)) {
5778 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.od(s|t)$',
'',
'name', SORT_ASC, 0);
5779 if (count($tmpfiles)) {
5780 $listoffiles = array_merge($listoffiles, $tmpfiles);
5785 if (count($listoffiles)) {
5786 foreach ($listoffiles as $record) {
5787 $srctemplatepath = $record[
'fullname'];
5793 if (empty($srctemplatepath)) {
5794 $this->error =
'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined';
5799 if ($obj->type ==
'odt' && !empty($srctemplatepath)) {
5801 dol_syslog(
"Failed to locate template file ".$srctemplatepath, LOG_WARNING);
5802 $this->error =
'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound';
5809 $sav_charset_output = empty($outputlangs->charset_output) ?
'' : $outputlangs->charset_output;
5812 $this->model_pdf = $saved_model;
5814 if (in_array(get_class($this), array(
'Adherent'))) {
5815 '@phan-var-force Adherent $this';
5816 $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath,
'member', 1,
'tmp_cards', $moreparams);
5818 $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
5822 if ($resultwritefile > 0) {
5823 $outputlangs->charset_output = $sav_charset_output;
5826 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5830 if (!empty($obj->result[
'fullpath'])) {
5831 $destfull = $obj->result[
'fullpath'];
5834 $update_main_doc_field = 0;
5835 if (!empty($obj->update_main_doc_field)) {
5836 $update_main_doc_field = 1;
5842 $this->indexFile($destfull, $update_main_doc_field);
5845 dol_syslog(
'Method ->write_file was called on object '.get_class($obj).
' and return a success but the return array ->result["fullpath"] was not set.', LOG_WARNING);
5853 $outputlangs->charset_output = $sav_charset_output;
5854 $this->error = $obj->error;
5855 $this->errors = $obj->errors;
5856 dol_syslog(
"Error generating document for ".__CLASS__.
". Error: ".$obj->error, LOG_ERR);
5870 public function indexFile($destfull, $update_main_doc_field)
5872 global $conf, $user;
5874 $upload_dir = dirname($destfull);
5875 $destfile = basename($destfull);
5876 $rel_dir = preg_replace(
'/^'.preg_quote(DOL_DATA_ROOT,
'/').
'/',
'', $upload_dir);
5878 if (!preg_match(
'/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) {
5879 $filename = basename($destfile);
5880 $rel_dir = preg_replace(
'/[\\/]$/',
'', $rel_dir);
5881 $rel_dir = preg_replace(
'/^[\\/]/',
'', $rel_dir);
5883 include_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
5884 $ecmfile =
new EcmFiles($this->db);
5885 $result = $ecmfile->fetch(0,
'', ($rel_dir ? $rel_dir.
'/' :
'').$filename);
5888 $setsharekey =
false;
5889 if ($this->element ==
'propal' || $this->element ==
'proposal') {
5891 $setsharekey =
true;
5894 $setsharekey =
true;
5897 if ($this->element ==
'commande' &&
getDolGlobalInt(
"ORDER_ALLOW_EXTERNAL_DOWNLOAD")) {
5898 $setsharekey =
true;
5900 if ($this->element ==
'facture' &&
getDolGlobalInt(
"INVOICE_ALLOW_EXTERNAL_DOWNLOAD")) {
5901 $setsharekey =
true;
5903 if ($this->element ==
'bank_account' &&
getDolGlobalInt(
"BANK_ACCOUNT_ALLOW_EXTERNAL_DOWNLOAD")) {
5904 $setsharekey =
true;
5906 if ($this->element ==
'product' &&
getDolGlobalInt(
"PRODUCT_ALLOW_EXTERNAL_DOWNLOAD")) {
5907 $setsharekey =
true;
5909 if ($this->element ==
'contrat' &&
getDolGlobalInt(
"CONTRACT_ALLOW_EXTERNAL_DOWNLOAD")) {
5910 $setsharekey =
true;
5912 if ($this->element ==
'fichinter' &&
getDolGlobalInt(
"FICHINTER_ALLOW_EXTERNAL_DOWNLOAD")) {
5913 $setsharekey =
true;
5915 if ($this->element ==
'supplier_proposal' &&
getDolGlobalInt(
"SUPPLIER_PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD")) {
5916 $setsharekey =
true;
5918 if ($this->element ==
'societe_rib' &&
getDolGlobalInt(
"SOCIETE_RIB_ALLOW_ONLINESIGN")) {
5919 $setsharekey =
true;
5923 if (empty($ecmfile->share)) {
5924 require_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
5931 $ecmfile->fullpath_orig =
'';
5932 $ecmfile->gen_or_uploaded =
'generated';
5933 $ecmfile->description =
'';
5934 $ecmfile->keywords =
'';
5935 $result = $ecmfile->update($user);
5941 $ecmfile->entity = $conf->entity;
5942 $ecmfile->filepath = $rel_dir;
5943 $ecmfile->filename = $filename;
5945 $ecmfile->fullpath_orig =
'';
5946 $ecmfile->gen_or_uploaded =
'generated';
5947 $ecmfile->description =
'';
5948 $ecmfile->keywords =
'';
5949 $ecmfile->src_object_type = $this->table_element;
5950 $ecmfile->src_object_id = $this->id;
5952 $result = $ecmfile->create($user);
5964 if ($update_main_doc_field && !empty($this->table_element)) {
5965 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET last_main_doc = '".$this->db->escape($ecmfile->filepath.
"/".$ecmfile->filename).
"'";
5966 $sql .=
" WHERE rowid = ".((int) $this->
id);
5968 $resql = $this->db->query($sql);
5973 $this->last_main_doc = $ecmfile->filepath.
'/'.$ecmfile->filename;
6314 global $conf, $extrafields;
6316 if (empty($rowid)) {
6319 if (empty($rowid) && isset($this->
rowid)) {
6320 $rowid = $this->rowid;
6324 if (!$this->table_element) {
6328 $this->array_options = array();
6330 if (!is_array($optionsArray)) {
6332 if (!isset($extrafields) || !is_object($extrafields)) {
6333 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6338 if (empty($extrafields->attributes[$this->table_element][
'loaded'])) {
6339 $extrafields->fetch_name_optionals_label($this->table_element);
6341 $optionsArray = (!empty($extrafields->attributes[$this->table_element][
'label']) ? $extrafields->attributes[$this->table_element][
'label'] :
null);
6343 global $extrafields;
6344 dol_syslog(
"Warning: fetch_optionals was called with param optionsArray defined when you should pass null now", LOG_WARNING);
6347 $table_element = $this->table_element;
6348 if ($table_element ==
'categorie') {
6349 $table_element =
'categories';
6353 if (is_array($optionsArray) && count($optionsArray) > 0) {
6354 $sql =
"SELECT rowid";
6355 foreach ($optionsArray as $name => $label) {
6356 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || (!in_array($extrafields->attributes[$this->table_element][
'type'][$name], [
'separate',
'point',
'multipts',
'linestrg',
'polygon']))) {
6360 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'point') {
6361 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6363 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'multipts') {
6364 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6366 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'linestrg') {
6367 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6369 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'polygon') {
6370 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6373 $sql .=
" FROM ".$this->db->prefix().$table_element.
"_extrafields";
6374 $sql .=
" WHERE fk_object = ".((int) $rowid);
6377 $resql = $this->db->query($sql);
6379 $numrows = $this->db->num_rows($resql);
6381 $tab = $this->db->fetch_array($resql);
6383 foreach ($tab as $key => $value) {
6385 if ($key !=
'rowid' && $key !=
'tms' && $key !=
'fk_member' && !is_int($key)) {
6387 if (!empty($extrafields->attributes[$this->table_element]) && in_array($extrafields->attributes[$this->table_element][
'type'][$key], array(
'date',
'datetime'))) {
6389 $this->array_options[
"options_".$key] = $this->db->jdate($value);
6391 $this->array_options[
"options_".$key] = $value;
6396 if (!empty($extrafields->attributes[$this->table_element][
'type'][$key]) && $extrafields->attributes[$this->table_element][
'type'][$key] ==
'password') {
6397 if (!empty($value) && preg_match(
'/^dolcrypt:/', $value)) {
6398 $this->array_options[
"options_".$key] =
dolDecrypt($value);
6407 if (is_array($extrafields->attributes[$this->table_element][
'label'])) {
6408 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
6409 $this->array_options[
'options_' . $key] =
null;
6416 if (is_array($extrafields->attributes[$this->table_element][
'label'])) {
6417 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
6418 if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element][
'computed'][$key])) {
6420 if (empty($conf->disable_compute)) {
6421 global $objectoffield;
6422 $objectoffield = $this;
6423 $this->array_options[
'options_' . $key] =
dol_eval($extrafields->attributes[$this->table_element][
'computed'][$key], 1, 0,
'2');
6429 $this->db->free($resql);
6437 $this->errors[] = $this->db->lasterror;
6492 global $conf, $langs, $user;
6498 if (empty($userused)) {
6504 if (!empty($this->array_options)) {
6506 $langs->load(
'admin');
6507 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6509 $target_extrafields = $extrafields->fetch_name_optionals_label($this->table_element);
6512 $new_array_options = array();
6513 foreach ($this->array_options as $key => $value) {
6514 if (in_array(substr($key, 8), array_keys($target_extrafields))) {
6515 $new_array_options[$key] = $value;
6516 } elseif (in_array($key, array_keys($target_extrafields))) {
6517 $new_array_options[
'options_'.$key] = $value;
6521 foreach ($new_array_options as $key => $value) {
6522 $attributeKey = substr($key, 8);
6523 $attributeType = $extrafields->attributes[$this->table_element][
'type'][$attributeKey];
6524 $attributeLabel = $langs->transnoentities($extrafields->attributes[$this->table_element][
'label'][$attributeKey]);
6525 $attributeParam = $extrafields->attributes[$this->table_element][
'param'][$attributeKey];
6526 $attributeRequired = $extrafields->attributes[$this->table_element][
'required'][$attributeKey];
6527 $attributeUnique = $extrafields->attributes[$this->table_element][
'unique'][$attributeKey];
6528 $attrfieldcomputed = $extrafields->attributes[$this->table_element][
'computed'][$attributeKey];
6532 if (!empty($this->context[
'createfromclone']) && $this->context[
'createfromclone'] ==
'createfromclone' && !empty($attributeUnique)) {
6533 $new_array_options[$key] =
null;
6537 if ($attributeRequired) {
6538 $v = $this->array_options[$key];
6540 $langs->load(
"errors");
6541 dol_syslog(
"Mandatory field '".$key.
"' is empty during create and set to required into definition of extrafields");
6542 $this->errors[] = $langs->trans(
'ErrorFieldRequired', $attributeLabel);
6550 if (!empty($attrfieldcomputed)) {
6552 $value =
dol_eval($attrfieldcomputed, 1, 0,
'2');
6553 dol_syslog($langs->trans(
"Extrafieldcomputed").
" on ".$attributeLabel.
"(".$value.
")", LOG_DEBUG);
6554 $new_array_options[$key] = $value;
6556 $new_array_options[$key] =
null;
6560 switch ($attributeType) {
6562 if (!is_numeric($value) && $value !=
'') {
6563 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
6565 } elseif ($value ==
'') {
6566 $new_array_options[$key] =
null;
6572 if (!is_numeric($value) && $value !=
'') {
6573 dol_syslog($langs->trans(
"ExtraFieldHasWrongValue").
" for ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
6574 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
6576 } elseif ($value ==
'') {
6580 $new_array_options[$key] = $value;
6590 if ($this->array_options[$key] !=
'' && is_array($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options'])) {
6592 $tmparrays = array_keys($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options']);
6593 $algo = reset($tmparrays);
6598 if (is_object($this->oldcopy)) {
6600 if (isset($this->oldcopy->array_options[$key]) && $this->array_options[$key] == $this->oldcopy->array_options[$key]) {
6602 if ($algo ==
'dolcrypt') {
6603 if (!preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6604 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6606 $new_array_options[$key] = $this->array_options[$key];
6609 $new_array_options[$key] = $this->array_options[$key];
6613 if ($algo ==
'dolcrypt') {
6614 if (!preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6615 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6617 $new_array_options[$key] = $this->array_options[$key];
6620 $new_array_options[$key] =
dol_hash($this->array_options[$key], $algo);
6626 if ($algo ==
'dolcrypt' && !preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6627 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6629 $new_array_options[$key] = $this->array_options[$key];
6634 $new_array_options[$key] = $this->array_options[$key];
6637 $new_array_options[$key] = $this->array_options[$key];
6643 if (!is_numeric($this->array_options[$key]) || $this->array_options[$key] != intval($this->array_options[$key])) {
6644 $this->array_options[$key] = strtotime($this->array_options[$key]);
6646 $new_array_options[$key] = $this->db->idate($this->array_options[$key]);
6650 if (!is_numeric($this->array_options[$key]) || $this->array_options[$key] != intval($this->array_options[$key])) {
6651 $this->array_options[$key] = strtotime($this->array_options[$key]);
6653 $new_array_options[$key] = $this->db->idate($this->array_options[$key],
'gmt');
6656 $param_list = array_keys($attributeParam[
'options']);
6659 $InfoFieldList = explode(
":", $param_list[0]);
6661 if ($InfoFieldList[0] && class_exists($InfoFieldList[0])) {
6662 if ($value ==
'-1') {
6663 $new_array_options[$key] =
'';
6665 $object =
new $InfoFieldList[0]($this->db);
6666 if (is_numeric($value)) {
6667 $res =
$object->fetch($value);
6669 $res =
$object->fetch(
'', $value);
6673 $new_array_options[$key] =
$object->id;
6675 $this->error =
"Id/Ref '".$value.
"' for object '".
$object->element.
"' not found";
6680 dol_syslog(
'Error bad setup of extrafield', LOG_WARNING);
6685 if (is_array($this->array_options[$key])) {
6686 $new_array_options[$key] = implode(
',', $this->array_options[$key]);
6688 $new_array_options[$key] = $this->array_options[$key];
6696 $table_element = $this->table_element;
6697 if ($table_element ==
'categorie') {
6698 $table_element =
'categories';
6701 dol_syslog(get_class($this).
"::insertExtraFields delete then insert", LOG_DEBUG);
6703 $sql_del =
"DELETE FROM ".$this->db->prefix().$table_element.
"_extrafields WHERE fk_object = ".((int) $this->
id);
6704 $this->db->query($sql_del);
6706 $sql =
"INSERT INTO ".$this->db->prefix().$table_element.
"_extrafields (fk_object";
6707 foreach ($new_array_options as $key => $value) {
6708 $attributeKey = substr($key, 8);
6710 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] !=
'separate') {
6711 $sql .=
",".$attributeKey;
6715 if (!empty($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities']) && is_array($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'])) {
6716 foreach ($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) {
6717 if (!isset($extrafields->attributes[$this->table_element][
'type'][$tmpkey])) {
6718 $sql .=
",".$tmpkey;
6722 $sql .=
") VALUES (".$this->id;
6724 foreach ($new_array_options as $key => $value) {
6725 $attributeKey = substr($key, 8);
6727 if (!in_array($extrafields->attributes[$this->table_element][
'type'][$attributeKey], [
'separate',
'point',
'multipts',
'linestrg',
'polygon'])) {
6728 if ($new_array_options[$key] !=
'' || $new_array_options[$key] ==
'0') {
6729 $sql .=
",'".$this->db->escape($new_array_options[$key]).
"'";
6734 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'point') {
6735 if (!empty($new_array_options[$key])) {
6736 $sql .=
",ST_PointFromText('".$this->db->escape($new_array_options[$key]).
"')";
6741 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'multipts') {
6742 if (!empty($new_array_options[$key])) {
6743 $sql .=
",ST_MultiPointFromText('".$this->db->escape($new_array_options[$key]).
"')";
6748 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'linestrg') {
6749 if (!empty($new_array_options[$key])) {
6750 $sql .=
",ST_LineFromText('".$this->db->escape($new_array_options[$key]).
"')";
6755 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'polygon') {
6756 if (!empty($new_array_options[$key])) {
6757 $sql .=
",ST_PolyFromText('".$this->db->escape($new_array_options[$key]).
"')";
6764 if (!empty($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities']) && is_array($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'])) {
6765 foreach ($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) {
6766 if (!isset($extrafields->attributes[$this->table_element][
'type'][$tmpkey])) {
6767 if (in_array($tmpval, array(
'int',
'double',
'price'))) {
6778 $resql = $this->db->query($sql);
6780 $this->error = $this->db->lasterror();
6784 if (!$error && $trigger) {
6786 $this->context = array(
'extrafieldaddupdate' => 1);
6787 $result = $this->call_trigger($trigger, $userused);
6795 $this->db->rollback();
6798 $this->db->commit();
6818 global $conf, $langs, $user;
6820 if (empty($userused)) {
6830 if (is_array($this->array_languages)) {
6831 $new_array_languages = $this->array_languages;
6833 foreach ($new_array_languages as $key => $value) {
6834 $attributeKey = $key;
6835 $attributeType = $this->fields[$attributeKey][
'type'];
6836 $attributeLabel = $this->fields[$attributeKey][
'label'];
6841 switch ($attributeType) {
6843 if (is_array($value) || (!is_numeric($value) && $value !=
'')) {
6844 $this->errors[] = $langs->trans(
"ExtraLanguageHasWrongValue", $attributeLabel);
6846 } elseif ($value ==
'') {
6847 $new_array_languages[$key] =
null;
6852 if (!is_numeric($value) && $value !=
'') {
6853 dol_syslog($langs->trans(
"ExtraLanguageHasWrongValue").
" on ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
6854 $this->errors[] = $langs->trans(
"ExtraLanguageHasWrongValue", $attributeLabel);
6856 } elseif ($value ==
'') {
6857 $new_array_languages[$key] =
null;
6859 $new_array_languages[$key] = $value;
6873 $table_element = $this->table_element;
6874 if ($table_element ==
'categorie') {
6875 $table_element =
'categories';
6878 dol_syslog(get_class($this).
"::insertExtraLanguages delete then insert", LOG_DEBUG);
6880 foreach ($new_array_languages as $key => $langcodearray) {
6881 foreach ($langcodearray as $langcode => $value) {
6882 $sql_del =
"DELETE FROM ".$this->db->prefix().
"object_lang";
6883 $sql_del .=
" WHERE fk_object = ".((int) $this->
id).
" AND property = '".$this->db->escape($key).
"' AND type_object = '".$this->db->escape($table_element).
"'";
6884 $sql_del .=
" AND lang = '".$this->db->escape($langcode).
"'";
6885 $this->db->query($sql_del);
6887 if ($value !==
'') {
6888 $sql =
"INSERT INTO ".$this->db->prefix().
"object_lang (fk_object, property, type_object, lang, value";
6889 $sql .=
") VALUES (".$this->id.
", '".$this->db->escape($key).
"', '".$this->db->escape($table_element).
"', '".$this->db->escape($langcode).
"', '".$this->db->escape($value).
"'";
6892 $resql = $this->db->query($sql);
6894 $this->error = $this->db->lasterror();
6902 if (!$error && $trigger) {
6904 $this->context = array(
'extralanguagesaddupdate' => 1);
6905 $result = $this->call_trigger($trigger, $userused);
6913 $this->db->rollback();
6916 $this->db->commit();
6936 global $conf, $langs, $user, $hookmanager;
6942 if (empty($userused)) {
6948 if (!empty($this->array_options) && isset($this->array_options[
"options_".$key])) {
6950 $langs->load(
'admin');
6951 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6953 $extrafields->fetch_name_optionals_label($this->table_element);
6955 $value = $this->array_options[
"options_".$key];
6957 $attributeKey = $key;
6958 $attributeType = $extrafields->attributes[$this->table_element][
'type'][$key];
6959 $attributeLabel = $extrafields->attributes[$this->table_element][
'label'][$key];
6960 $attributeParam = $extrafields->attributes[$this->table_element][
'param'][$key];
6961 $attributeRequired = $extrafields->attributes[$this->table_element][
'required'][$key];
6962 $attributeUnique = $extrafields->attributes[$this->table_element][
'unique'][$attributeKey];
6963 $attrfieldcomputed = $extrafields->attributes[$this->table_element][
'computed'][$key];
6966 if ($attributeRequired) {
6967 $mandatorypb =
false;
6968 if ($attributeType ==
'link' && $this->array_options[
"options_".$key] ==
'-1') {
6969 $mandatorypb =
true;
6971 if ($this->array_options[
"options_".$key] ===
'') {
6972 $mandatorypb =
true;
6975 $langs->load(
"errors");
6976 dol_syslog(
"Mandatory field 'options_".$key.
"' is empty during update and set to required into definition of extrafields");
6977 $this->errors[] = $langs->trans(
'ErrorFieldRequired', $attributeLabel);
6983 $new_array_options = $this->array_options;
6987 if (!empty($attrfieldcomputed)) {
6989 $value =
dol_eval($attrfieldcomputed, 1, 0,
'2');
6990 dol_syslog($langs->trans(
"Extrafieldcomputed").
" on ".$attributeLabel.
"(".$value.
")", LOG_DEBUG);
6992 $new_array_options[
"options_".$key] = $value;
6994 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
6996 $new_array_options[
"options_".$key] =
null;
6998 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7002 switch ($attributeType) {
7004 if (!is_numeric($value) && $value !=
'') {
7005 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
7007 } elseif ($value ===
'') {
7008 $new_array_options[
"options_".$key] =
null;
7010 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7016 if (!is_numeric($value) && $value !=
'') {
7017 dol_syslog($langs->trans(
"ExtraFieldHasWrongValue").
" on ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
7018 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
7020 } elseif ($value ===
'') {
7024 $new_array_options[
"options_".$key] = $value;
7026 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7038 if ($this->array_options[
"options_".$key] !=
'' && is_array($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options'])) {
7040 $tmparrays = array_keys($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options']);
7041 $algo = reset($tmparrays);
7047 if (is_object($this->oldcopy)) {
7049 if (isset($this->oldcopy->array_options[
"options_".$key]) && $this->array_options[
"options_".$key] == $this->oldcopy->array_options[
"options_".$key]) {
7050 if ($algo ==
'dolcrypt') {
7051 if (!preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7052 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7054 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7057 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7060 if ($algo ==
'dolcrypt') {
7061 if (!preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7062 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7064 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7067 $new_array_options[
"options_".$key] =
dol_hash($this->array_options[
"options_".$key], $algo);
7071 if ($algo ==
'dolcrypt' && !preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7072 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7074 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7079 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7082 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7085 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7089 if (empty($this->array_options[
"options_".$key])) {
7090 $new_array_options[
"options_".$key] =
null;
7092 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7094 $new_array_options[
"options_".$key] = $this->db->idate($this->array_options[
"options_".$key]);
7098 if (empty($this->array_options[
"options_".$key])) {
7099 $new_array_options[
"options_".$key] =
null;
7101 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7103 $new_array_options[
"options_".$key] = $this->db->idate($this->array_options[
"options_".$key],
'gmt');
7107 if (empty($this->array_options[
"options_".$key])) {
7108 $new_array_options[
"options_".$key] =
null;
7110 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7114 if ($this->array_options[
"options_".$key] ===
'') {
7115 $new_array_options[
"options_".$key] =
null;
7117 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7151 $new_array_options = array();
7152 if (is_array($this->array_options[
"options_".$key])) {
7153 $new_array_options[
"options_".$key] = implode(
',', $this->array_options[
"options_".$key]);
7155 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7158 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7164 $linealreadyfound = 0;
7167 $table_element = $this->table_element;
7168 if ($table_element ==
'categorie') {
7169 $table_element =
'categories';
7172 $sql =
"SELECT COUNT(rowid) as nb FROM ".$this->db->prefix().$table_element.
"_extrafields WHERE fk_object = ".((int) $this->
id);
7173 $resql = $this->db->query($sql);
7175 $tmpobj = $this->db->fetch_object($resql);
7177 $linealreadyfound = $tmpobj->nb;
7182 if ($linealreadyfound) {
7183 if ($this->array_options[
"options_".$key] ===
null) {
7184 $sql =
"UPDATE ".$this->db->prefix().$table_element.
"_extrafields SET ".$key.
" = null";
7186 $sql =
"UPDATE ".$this->db->prefix().$table_element.
"_extrafields SET ".$key.
" = '".$this->db->escape($new_array_options[
"options_".$key]).
"'";
7188 $sql .=
" WHERE fk_object = ".((int) $this->
id);
7190 $resql = $this->db->query($sql);
7193 $this->error = $this->db->lasterror();
7196 $result = $this->insertExtraFields(
'', $user);
7203 $parameters = array(
'key' => $key);
7205 $reshook = $hookmanager->executeHooks(
'updateExtraFieldBeforeCommit', $parameters, $this, $action);
7211 if (!$error && $trigger) {
7213 $this->context = array(
'extrafieldupdate' => 1);
7214 $result = $this->call_trigger($trigger, $userused);
7222 dol_syslog(__METHOD__.$this->error, LOG_ERR);
7223 $this->db->rollback();
7226 $this->db->commit();
7300 public function showInputField($val, $key, $value, $moreparam =
'', $keysuffix =
'', $keyprefix =
'', $morecss = 0, $nonewbutton = 0)
7302 global $conf, $langs, $form;
7304 if (!is_object($form)) {
7305 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
7306 $form =
new Form($this->db);
7309 if (!empty($this->fields)) {
7310 $val = $this->fields[$key];
7314 $fieldValidationErrorMsg =
'';
7315 $validationClass =
'';
7316 $fieldValidationErrorMsg = $this->getFieldError($key);
7317 if (!empty($fieldValidationErrorMsg)) {
7318 $validationClass =
' --error';
7320 $validationClass =
' --success';
7323 $valuemultiselectinput = array();
7328 $param[
'options'] = array();
7331 $size = !empty($this->fields[$key][
'size']) ? $this->fields[$key][
'size'] : 0;
7333 if (preg_match(
'/^(integer|link):(.*):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7334 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4].
':'.$reg[5] =>
'N');
7336 } elseif (preg_match(
'/^(integer|link):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7337 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4] =>
'N');
7339 } elseif (preg_match(
'/^(integer|link):(.*):(.*)/i', $val[
'type'], $reg)) {
7340 $param[
'options'] = array($reg[2].
':'.$reg[3] =>
'N');
7342 } elseif (preg_match(
'/^(sellist):(.*):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7343 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4].
':'.$reg[5] =>
'N');
7345 } elseif (preg_match(
'/^(sellist):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7346 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4] =>
'N');
7348 } elseif (preg_match(
'/^(sellist):(.*):(.*)/i', $val[
'type'], $reg)) {
7349 $param[
'options'] = array($reg[2].
':'.$reg[3] =>
'N');
7351 } elseif (preg_match(
'/^chkbxlst:(.*)/i', $val[
'type'], $reg)) {
7352 $param[
'options'] = array($reg[1] =>
'N');
7354 } elseif (preg_match(
'/varchar\((\d+)\)/', $val[
'type'], $reg)) {
7355 $param[
'options'] = array();
7358 } elseif (preg_match(
'/varchar/', $val[
'type'])) {
7359 $param[
'options'] = array();
7362 $param[
'options'] = array();
7363 $type = $this->fields[$key][
'type'];
7368 if (!empty($this->fields[$key][
'arrayofkeyval']) && is_array($this->fields[$key][
'arrayofkeyval'])) {
7369 $param[
'options'] = $this->fields[$key][
'arrayofkeyval'];
7371 if (empty($this->fields[$key][
'multiinput'])) {
7372 $type = (($this->fields[$key][
'type'] ==
'checkbox') ? $this->fields[$key][
'type'] :
'select');
7376 $label = $this->fields[$key][
'label'];
7379 $default = (!empty($this->fields[$key][
'default']) ? $this->fields[$key][
'default'] :
'');
7381 $computed = (!empty($this->fields[$key][
'computed']) ? $this->fields[$key][
'computed'] :
'');
7383 $unique = (!empty($this->fields[$key][
'unique']) ? $this->fields[$key][
'unique'] : 0);
7385 $required = (!empty($this->fields[$key][
'required']) ? $this->fields[$key][
'required'] : 0);
7387 $autofocusoncreate = (!empty($this->fields[$key][
'autofocusoncreate']) ? $this->fields[$key][
'autofocusoncreate'] : 0);
7390 $langfile = (!empty($this->fields[$key][
'langfile']) ? $this->fields[$key][
'langfile'] :
'');
7392 $list = (!empty($this->fields[$key][
'list']) ? $this->fields[$key][
'list'] : 0);
7393 $hidden = (in_array(abs($this->fields[$key][
'visible']), array(0, 2)) ? 1 : 0);
7395 $objectid = $this->id;
7398 if (!preg_match(
'/^search_/', $keyprefix)) {
7399 return '<span class="opacitymedium">'.$langs->trans(
"AutomaticallyCalculated").
'</span>';
7406 if (empty($morecss) && !empty($val[
'css'])) {
7407 $morecss = $val[
'css'];
7408 } elseif (empty($morecss)) {
7409 if ($type ==
'date') {
7410 $morecss =
'minwidth100imp';
7411 } elseif ($type ==
'datetime' || $type ==
'link') {
7412 $morecss =
'minwidth200imp';
7413 } elseif (in_array($type, array(
'int',
'integer',
'price')) || preg_match(
'/^double(\([0-9],[0-9]\)){0,1}/', (
string) $type)) {
7414 $morecss =
'maxwidth75';
7415 } elseif ($type ==
'url') {
7416 $morecss =
'minwidth400';
7417 } elseif ($type ==
'boolean') {
7420 if (is_numeric($size) && round((
float) $size) < 12) {
7421 $morecss =
'minwidth100';
7422 } elseif (is_numeric($size) && round((
float) $size) <= 48) {
7423 $morecss =
'minwidth200';
7425 $morecss =
'minwidth400';
7431 if (!empty($validationClass)) {
7432 $morecss .= $validationClass;
7435 if (in_array($type, array(
'date'))) {
7436 $tmp = explode(
',', $size);
7441 if (!$required && $value ==
'') {
7446 $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required,
'', 1, (($keyprefix !=
'search_' && $keyprefix !=
'search_options_') ? 1 : 0), 0, 1);
7447 } elseif (in_array($type, array(
'datetime'))) {
7448 $tmp = explode(
',', $size);
7453 if (!$required && $value ==
'') {
7458 $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required,
'', 1, (($keyprefix !=
'search_' && $keyprefix !=
'search_options_') ? 1 : 0), 0, 1,
'',
'',
'', 1,
'',
'',
'tzuserrel');
7459 } elseif (in_array($type, array(
'duration'))) {
7460 $out = $form->select_duration($keyprefix.$key.$keysuffix, $value, 0,
'text', 0, 1);
7461 } elseif (in_array($type, array(
'int',
'integer'))) {
7462 $tmp = explode(
',', $size);
7464 $out =
'<input type="text" class="flat '.$morecss.
'" name="'.$keyprefix.$key.$keysuffix.
'" id="'.$keyprefix.$key.$keysuffix.
'"'.($newsize > 0 ?
' maxlength="'.$newsize.
'"' :
'').
' value="'.
dol_escape_htmltag($value).
'"'.($moreparam ? $moreparam :
'').($autofocusoncreate ?
' autofocus' :
'').
'>';
7465 } elseif (in_array($type, array(
'real'))) {
7466 $out =
'<input type="text" class="flat '.$morecss.
'" name="'.$keyprefix.$key.$keysuffix.
'" id="'.$keyprefix.$key.$keysuffix.
'" value="'.
dol_escape_htmltag($value).
'"'.($moreparam ? $moreparam :
'').($autofocusoncreate ?
' autofocus' :
'').
'>';
7467 } elseif (preg_match(
'/varchar/', (
string) $type)) {
7468 $out =
'<input type="text" class="flat '.$morecss.
'" name="'.$keyprefix.$key.$keysuffix.
'" id="'.$keyprefix.$key.$keysuffix.
'&quo