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();
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;
3921 $sqlfix .=
" SET ".$fieldtva.
" = ".
price2num((
float) $tmpcal[1]).
", total_ttc = ".
price2num((
float) $tmpcal[2]);
3922 $sqlfix .=
", multicurrency_total_tva = ".price2num((
float) $tmpcal[17]).
", multicurrency_total_ttc = ".
price2num((
float) $tmpcal[18]);
3923 $sqlfix .=
" WHERE rowid = ".((int) $obj->rowid);
3924 dol_syslog(
'Warn1: 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);
3925 $resqlfix = $this->db->query($sqlfix);
3929 $obj->total_tva = $tmpcal[1];
3930 $obj->total_ttc = $tmpcal[2];
3931 $obj->multicurrency_total_tva = $tmpcal[17];
3932 $obj->multicurrency_total_ttc = $tmpcal[18];
3933 } elseif ($diff_when_using_price_ht) {
3936 if ((
float) $tmpcal[0] == (
float) $obj->total_ht) {
3941 $sqlfix =
"UPDATE ".$this->db->prefix().$this->table_element_line;
3942 $sqlfix .=
" SET ".$fieldtva.
" = ".
price2num((
float) $tmpcal[1]).
", total_ttc = ".
price2num((
float) $tmpcal[2]);
3943 $sqlfix .=
", multicurrency_total_tva = ".price2num((
float) $tmpcal[17]).
", multicurrency_total_ttc = ".
price2num((
float) $tmpcal[18]);
3944 $sqlfix .=
" WHERE rowid = ".((int) $obj->rowid);
3945 dol_syslog(
'Warn2: 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);
3946 $resqlfix = $this->db->query($sqlfix);
3950 $obj->total_tva = $tmpcal[1];
3951 $obj->total_ttc = $tmpcal[2];
3952 $obj->multicurrency_total_tva = $tmpcal[17];
3953 $obj->multicurrency_total_ttc = $tmpcal[18];
3958 $this->total_ht += $obj->total_ht;
3959 $this->total_tva += $obj->total_tva;
3960 $this->total_localtax1 += $obj->total_localtax1;
3961 $this->total_localtax2 += $obj->total_localtax2;
3962 $this->total_ttc += $obj->total_ttc;
3963 $this->multicurrency_total_ht += $obj->multicurrency_total_ht;
3964 $this->multicurrency_total_tva += $obj->multicurrency_total_tva;
3965 $this->multicurrency_total_ttc += $obj->multicurrency_total_ttc;
3967 if (!isset($total_ht_by_vats[$obj->vatrate])) {
3968 $total_ht_by_vats[$obj->vatrate] = 0;
3970 if (!isset($total_tva_by_vats[$obj->vatrate])) {
3971 $total_tva_by_vats[$obj->vatrate] = 0;
3973 if (!isset($total_ttc_by_vats[$obj->vatrate])) {
3974 $total_ttc_by_vats[$obj->vatrate] = 0;
3976 $total_ht_by_vats[$obj->vatrate] += $obj->total_ht;
3977 $total_tva_by_vats[$obj->vatrate] += $obj->total_tva;
3978 $total_ttc_by_vats[$obj->vatrate] += $obj->total_ttc;
3980 if ($forcedroundingmode ==
'1') {
3981 $tmpvat =
price2num($total_ht_by_vats[$obj->vatrate] * $obj->vatrate / 100,
'MT', 1);
3982 $diff =
price2num($total_tva_by_vats[$obj->vatrate] - (
float) $tmpvat,
'MT', 1);
3985 $maxdiff = (10 * pow(10, -1 *
getDolGlobalInt(
'MAIN_MAX_DECIMALS_TOT', 0)));
3986 if (abs((
float) $diff) > $maxdiff) {
3988 $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.';
3990 $this->error = $errmsg;
3994 $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);
3995 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);
3997 $resqlfix = $this->db->query($sqlfix);
4003 $this->total_tva = (float)
price2num($this->total_tva - (
float) $diff,
'', 1);
4004 $this->total_ttc = (float)
price2num($this->total_ttc - (
float) $diff,
'', 1);
4005 $total_tva_by_vats[$obj->vatrate] = (float)
price2num($total_tva_by_vats[$obj->vatrate] - (
float) $diff,
'', 1);
4006 $total_ttc_by_vats[$obj->vatrate] = (float)
price2num($total_ttc_by_vats[$obj->vatrate] - (
float) $diff,
'', 1);
4014 $this->total_ttc += isset($this->revenuestamp) ? $this->revenuestamp : 0;
4015 $this->multicurrency_total_ttc += isset($this->revenuestamp) ? ($this->revenuestamp * $multicurrency_tx) : 0;
4018 if (!empty($this->situation_cycle_ref) && !empty($this->situation_counter) && $this->situation_counter > 1 && method_exists($this,
'get_prev_sits')) {
4019 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
4022 $prev_sits = $this->get_prev_sits();
4024 foreach ($prev_sits as $sit) {
4025 $this->total_ht -= $sit->total_ht;
4026 $this->total_tva -= $sit->total_tva;
4027 $this->total_localtax1 -= $sit->total_localtax1;
4028 $this->total_localtax2 -= $sit->total_localtax2;
4029 $this->total_ttc -= $sit->total_ttc;
4030 $this->multicurrency_total_ht -= $sit->multicurrency_total_ht;
4031 $this->multicurrency_total_tva -= $sit->multicurrency_total_tva;
4032 $this->multicurrency_total_ttc -= $sit->multicurrency_total_ttc;
4039 $this->total_ht = (float)
price2num($this->total_ht);
4040 $this->total_tva = (float)
price2num($this->total_tva);
4041 $this->total_localtax1 = (float)
price2num($this->total_localtax1);
4042 $this->total_localtax2 = (float)
price2num($this->total_localtax2);
4043 $this->total_ttc = (float)
price2num($this->total_ttc);
4045 $this->db->free($resql);
4048 $fieldht =
'total_ht';
4050 $fieldlocaltax1 =
'localtax1';
4051 $fieldlocaltax2 =
'localtax2';
4052 $fieldttc =
'total_ttc';
4054 if (in_array($this->element, array(
'propal',
'commande',
'facture',
'facturerec',
'supplier_proposal',
'order_supplier',
'facture_fourn',
'invoice_supplier',
'invoice_supplier_rec',
'expensereport'))) {
4055 $fieldtva =
'total_tva';
4058 if (!$error && empty($nodatabaseupdate)) {
4059 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
' SET';
4060 $sql .=
" ".$fieldht.
" = ".((float)
price2num($this->total_ht,
'MT', 1)).
",";
4061 $sql .=
" ".$fieldtva.
" = ".((float)
price2num($this->total_tva,
'MT', 1)).
",";
4062 $sql .=
" ".$fieldlocaltax1.
" = ".((float)
price2num($this->total_localtax1,
'MT', 1)).
",";
4063 $sql .=
" ".$fieldlocaltax2.
" = ".((float)
price2num($this->total_localtax2,
'MT', 1)).
",";
4064 $sql .=
" ".$fieldttc.
" = ".((float)
price2num($this->total_ttc,
'MT', 1));
4065 $sql .=
", multicurrency_total_ht = ".((float)
price2num($this->multicurrency_total_ht,
'MT', 1));
4066 $sql .=
", multicurrency_total_tva = ".((float)
price2num($this->multicurrency_total_tva,
'MT', 1));
4067 $sql .=
", multicurrency_total_ttc = ".((float)
price2num($this->multicurrency_total_ttc,
'MT', 1));
4068 $sql .=
" WHERE rowid = ".((int) $this->
id);
4070 dol_syslog(get_class($this).
"::update_price", LOG_DEBUG);
4071 $resql = $this->db->query($sql);
4075 $this->error = $this->db->lasterror();
4076 $this->errors[] = $this->db->lasterror();
4081 $this->db->commit();
4084 $this->db->rollback();
4216 public function fetchObjectLinked($sourceid =
null, $sourcetype =
'', $targetid =
null, $targettype =
'', $clause =
'OR', $alsosametype = 1, $orderby =
'sourcetype', $loadalsoobjects = 1)
4218 global $conf, $hookmanager, $action;
4223 if ($this->
id > 0 && !empty($this->linkedObjectsFullLoaded[$this->
id])) {
4227 $this->linkedObjectsIds = array();
4228 $this->linkedObjects = array();
4230 $justsource =
false;
4231 $justtarget =
false;
4232 $withtargettype =
false;
4233 $withsourcetype =
false;
4235 $parameters = array(
'sourcetype' => $sourcetype,
'sourceid' => $sourceid,
'targettype' => $targettype,
'targetid' => $targetid);
4237 $reshook = $hookmanager->executeHooks(
'setLinkedObjectSourceTargetType', $parameters, $this, $action);
4239 if (!empty($hookmanager->resArray[
'sourcetype'])) {
4240 $sourcetype = $hookmanager->resArray[
'sourcetype'];
4242 if (!empty($hookmanager->resArray[
'sourceid'])) {
4243 $sourceid = $hookmanager->resArray[
'sourceid'];
4245 if (!empty($hookmanager->resArray[
'targettype'])) {
4246 $targettype = $hookmanager->resArray[
'targettype'];
4248 if (!empty($hookmanager->resArray[
'targetid'])) {
4249 $targetid = $hookmanager->resArray[
'targetid'];
4253 if (!empty($sourceid) && !empty($sourcetype) && empty($targetid)) {
4255 if (!empty($targettype)) {
4256 $withtargettype =
true;
4259 if (!empty($targetid) && !empty($targettype) && empty($sourceid)) {
4261 if (!empty($sourcetype)) {
4262 $withsourcetype =
true;
4266 $sourceid = (!empty($sourceid) ? $sourceid : $this->id);
4267 $targetid = (!empty($targetid) ? $targetid : $this->id);
4268 $sourcetype = (!empty($sourcetype) ? $sourcetype : $this->getElementType());
4269 $targettype = (!empty($targettype) ? $targettype : $this->getElementType());
4278 $sql =
"SELECT rowid, fk_source, sourcetype, fk_target, targettype";
4279 $sql .=
" FROM ".$this->db->prefix().
"element_element";
4281 if ($justsource || $justtarget) {
4283 $sql .=
"fk_source = ".((int) $sourceid).
" AND sourcetype = '".$this->db->escape($sourcetype).
"'";
4284 if ($withtargettype) {
4285 $sql .=
" AND targettype = '".$this->db->escape($targettype).
"'";
4287 } elseif ($justtarget) {
4288 $sql .=
"fk_target = ".((int) $targetid).
" AND targettype = '".$this->db->escape($targettype).
"'";
4289 if ($withsourcetype) {
4290 $sql .=
" AND sourcetype = '".$this->db->escape($sourcetype).
"'";
4294 $sql .=
"(fk_source = ".((int) $sourceid).
" AND sourcetype = '".$this->db->escape($sourcetype).
"')";
4295 $sql .=
" ".$clause.
" (fk_target = ".((int) $targetid).
" AND targettype = '".$this->db->escape($targettype).
"')";
4296 if ($loadalsoobjects && $this->
id > 0 && $sourceid == $this->
id && $sourcetype == $this->element && $targetid == $this->
id && $targettype == $this->element && $clause ==
'OR') {
4297 $this->linkedObjectsFullLoaded[$this->id] =
true;
4300 $sql .=
" ORDER BY ".$orderby;
4302 dol_syslog(get_class($this).
"::fetchObjectLink", LOG_DEBUG);
4303 $resql = $this->db->query($sql);
4305 $num = $this->db->num_rows($resql);
4308 $obj = $this->db->fetch_object($resql);
4309 if ($justsource || $justtarget) {
4311 $this->linkedObjectsIds[$obj->targettype][$obj->rowid] = $obj->fk_target;
4312 } elseif ($justtarget) {
4313 $this->linkedObjectsIds[$obj->sourcetype][$obj->rowid] = $obj->fk_source;
4316 if ($obj->fk_source == $sourceid && $obj->sourcetype == $sourcetype) {
4317 $this->linkedObjectsIds[$obj->targettype][$obj->rowid] = $obj->fk_target;
4319 if ($obj->fk_target == $targetid && $obj->targettype == $targettype) {
4320 $this->linkedObjectsIds[$obj->sourcetype][$obj->rowid] = $obj->fk_source;
4326 if (!empty($this->linkedObjectsIds)) {
4327 $tmparray = $this->linkedObjectsIds;
4328 foreach ($tmparray as $objecttype => $objectids) {
4330 $element = $element_properties[
'element'];
4331 $classPath = $element_properties[
'classpath'];
4332 $classFile = $element_properties[
'classfile'];
4333 $className = $element_properties[
'classname'];
4334 $module = $element_properties[
'module'];
4337 if (isModEnabled($module) && (($element != $this->element) || $alsosametype)) {
4338 if ($loadalsoobjects && (is_numeric($loadalsoobjects) || ($loadalsoobjects === $objecttype))) {
4340 if (class_exists($className)) {
4341 foreach ($objectids as $i => $objectid) {
4342 $object =
new $className($this->db);
4343 $ret =
$object->fetch($objectid);
4345 $this->linkedObjects[$objecttype][$i] =
$object;
4351 unset($this->linkedObjectsIds[$objecttype]);
5004 if (empty($this->lines)) {
5005 return array(
'weight' => $totalWeight,
'volume' => $totalVolume,
'ordered' => $totalOrdered,
'toship' => $totalToShip);
5008 foreach ($this->lines as $line) {
5009 if (isset($line->qty_asked)) {
5010 $totalOrdered += $line->qty_asked;
5012 if (isset($line->qty_shipped)) {
5013 $totalToShip += $line->qty_shipped;
5014 } elseif ($line->element ==
'commandefournisseurdispatch' && isset($line->qty)) {
5015 if (empty($totalToShip)) {
5018 $totalToShip += $line->qty;
5022 if ($this->element ==
'shipping') {
5024 $qty = $line->qty_shipped ? $line->qty_shipped : 0;
5026 $qty = $line->qty ? $line->qty : 0;
5029 $weight = !empty($line->weight) ? $line->weight : 0;
5030 ($weight == 0 && !empty($line->product->weight)) ? $weight = $line->product->weight : 0;
5031 $volume = !empty($line->volume) ? $line->volume : 0;
5032 ($volume == 0 && !empty($line->product->volume)) ? $volume = $line->product->volume : 0;
5034 $weight_units = !empty($line->weight_units) ? $line->weight_units : 0;
5035 ($weight_units == 0 && !empty($line->product->weight_units)) ? $weight_units = $line->product->weight_units : 0;
5036 $volume_units = !empty($line->volume_units) ? $line->volume_units : 0;
5037 ($volume_units == 0 && !empty($line->product->volume_units)) ? $volume_units = $line->product->volume_units : 0;
5041 if (!empty($weight_units)) {
5042 $weightUnit = $weight_units;
5044 if (!empty($volume_units)) {
5045 $volumeUnit = $volume_units;
5048 if (empty($totalWeight)) {
5051 if (empty($totalVolume)) {
5056 if ($weight_units < 50) {
5057 $trueWeightUnit = pow(10, $weightUnit);
5058 $totalWeight += $weight * $qty * $trueWeightUnit;
5060 if ($weight_units == 99) {
5062 $trueWeightUnit = 0.45359237;
5063 $totalWeight += $weight * $qty * $trueWeightUnit;
5064 } elseif ($weight_units == 98) {
5066 $trueWeightUnit = 0.0283495;
5067 $totalWeight += $weight * $qty * $trueWeightUnit;
5069 $totalWeight += $weight * $qty;
5072 if ($volume_units < 50) {
5074 $trueVolumeUnit = pow(10, $volumeUnit);
5076 $totalVolume += $volume * $qty * $trueVolumeUnit;
5078 $totalVolume += $volume * $qty;
5082 return array(
'weight' => $totalWeight,
'volume' => $totalVolume,
'ordered' => $totalOrdered,
'toship' => $totalToShip);
5181 public function printObjectLines($action, $seller, $buyer, $selected = 0, $dateSelector = 0, $defaulttpldir =
'/core/tpl')
5183 global $conf, $hookmanager, $langs, $user, $form, $extrafields,
$object;
5185 global $inputalsopricewithtax, $usemargins, $disableedit, $disablemove, $disableremove, $outputalsopricetotalwithtax;
5189 if (isModEnabled(
'margin') && !empty($this->element) && in_array($this->element, array(
'facture',
'facturerec',
'propal',
'commande'))) {
5193 $num = count($this->lines);
5196 if (!is_object($extrafields)) {
5197 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
5200 $extrafields->fetch_name_optionals_label($this->table_element_line);
5202 $parameters = array(
'num' => $num,
'dateSelector' => $dateSelector,
'seller' => $seller,
'buyer' => $buyer,
'selected' => $selected,
'table_element_line' => $this->table_element_line);
5203 $reshook = $hookmanager->executeHooks(
'printObjectLineTitle', $parameters, $this, $action);
5204 if (empty($reshook)) {
5208 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5209 foreach ($dirtpls as $module => $reldir) {
5211 if (!empty($module)) {
5214 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_title.tpl.php';
5216 if (file_exists($tpl)) {
5217 if (empty($conf->file->strict_mode)) {
5218 $res = @include $tpl;
5220 $res = include $tpl;
5231 print
"<!-- begin printObjectLines() --><tbody>\n";
5232 foreach ($this->lines as $line) {
5234 $line->fetch_optionals();
5237 if (is_object($hookmanager)) {
5238 if (empty($line->fk_parent_line)) {
5239 $parameters = array(
'line' => $line,
'num' => $num,
'i' => $i,
'dateSelector' => $dateSelector,
'seller' => $seller,
'buyer' => $buyer,
'selected' => $selected,
'table_element_line' => $line->table_element,
'defaulttpldir' => $defaulttpldir);
5240 $reshook = $hookmanager->executeHooks(
'printObjectLine', $parameters, $this, $action);
5242 $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);
5243 $reshook = $hookmanager->executeHooks(
'printObjectSubLine', $parameters, $this, $action);
5246 if (empty($reshook)) {
5247 $this->printObjectLine($action, $line,
'', $num, $i, $dateSelector, $seller, $buyer, $selected, $extrafields, $defaulttpldir);
5252 print
"</tbody><!-- end printObjectLines() -->\n";
5272 public function printObjectLine($action, $line, $var, $num, $i, $dateSelector, $seller, $buyer, $selected = 0, $extrafields =
null, $defaulttpldir =
'/core/tpl')
5274 global $conf, $langs, $user,
$object, $hookmanager;
5276 global $object_rights, $disableedit, $disablemove, $disableremove;
5278 $object_rights = $this->getRights();
5285 if ($action !=
'editline' || $selected != $line->id) {
5287 if (!empty($line->fk_product) && $line->fk_product > 0) {
5288 $product_static =
new Product($this->db);
5289 $product_static->fetch($line->fk_product);
5291 $product_static->ref = $line->ref;
5292 $product_static->label = !empty($line->label) ? $line->label :
"";
5294 $text = $product_static->getNomUrl(1);
5298 if (property_exists($this,
'socid') && !is_object($this->thirdparty)) {
5299 dol_print_error(
null,
'Error: Method printObjectLine was called on an object and object->fetch_thirdparty was not done before');
5303 $prod =
new Product($this->db);
5304 $prod->fetch($line->fk_product);
5306 $outputlangs = $langs;
5308 if (empty($newlang) &&
GETPOST(
'lang_id',
'aZ09')) {
5309 $newlang =
GETPOST(
'lang_id',
'aZ09');
5311 if (
getDolGlobalString(
'PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE') && empty($newlang) && is_object($this->thirdparty)) {
5312 $newlang = $this->thirdparty->default_lang;
5314 if (!empty($newlang)) {
5315 $outputlangs =
new Translate(
"", $conf);
5316 $outputlangs->setDefaultLang($newlang);
5319 $label = (!empty($prod->multilangs[$outputlangs->defaultlang][
"label"])) ? $prod->multilangs[$outputlangs->defaultlang][
"label"] : $line->product_label;
5321 $label = $line->product_label;
5324 $text .=
' - '.(!empty($line->label) ? $line->label : $label);
5325 $description .= (
getDolGlobalInt(
'PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE') ?
'' : (!empty($line->description) ?
dol_htmlentitiesbr($line->description) :
''));
5328 $line->pu_ttc =
price2num((!empty($line->subprice) ? $line->subprice : 0) * (1 + ((!empty($line->tva_tx) ? $line->tva_tx : 0) / 100)),
'MU');
5333 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5334 foreach ($dirtpls as $module => $reldir) {
5336 if (!empty($module)) {
5339 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_view.tpl.php';
5342 if (file_exists($tpl)) {
5343 if (empty($conf->file->strict_mode)) {
5344 $res = @include $tpl;
5346 $res = include $tpl;
5356 if ($this->
status == 0 && $action ==
'editline' && $selected == $line->id) {
5357 $label = (!empty($line->label) ? $line->label : (($line->fk_product > 0) ? $line->product_label :
''));
5359 $line->pu_ttc =
price2num($line->subprice * (1 + ($line->tva_tx / 100)),
'MU');
5364 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5365 foreach ($dirtpls as $module => $reldir) {
5366 if (!empty($module)) {
5369 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/objectline_edit.tpl.php';
5372 if (empty($conf->file->strict_mode)) {
5373 $res = @include $tpl;
5375 $res = include $tpl;
5453 public function printOriginLine($line, $var, $restrictlist =
'', $defaulttpldir =
'/core/tpl', $selectedLines = array())
5455 global $langs, $conf;
5458 if (!empty($line->date_start)) {
5459 $date_start = $line->date_start;
5461 $date_start = $line->date_debut_prevue;
5462 if ($line->date_debut_reel) {
5463 $date_start = $line->date_debut_reel;
5466 if (!empty($line->date_end)) {
5467 $date_end = $line->date_end;
5469 $date_end = $line->date_fin_prevue;
5470 if ($line->date_fin_reel) {
5471 $date_end = $line->date_fin_reel;
5475 $this->tpl[
'id'] = $line->id;
5477 $this->tpl[
'label'] =
'';
5478 if (!empty($line->fk_parent_line)) {
5479 $this->tpl[
'label'] .=
img_picto(
'',
'rightarrow');
5482 if (($line->info_bits & 2) == 2) {
5484 if (property_exists($this,
'socid')) {
5485 $discount->fk_soc = $this->socid;
5486 $discount->socid = $this->socid;
5488 $this->tpl[
'label'] .= $discount->getNomUrl(0,
'discount');
5489 } elseif (!empty($line->fk_product)) {
5490 $productstatic =
new Product($this->db);
5491 $productstatic->id = $line->fk_product;
5492 $productstatic->ref = $line->ref;
5493 $productstatic->type = $line->fk_product_type;
5494 if (empty($productstatic->ref)) {
5495 $line->fetch_product();
5496 $productstatic = $line->product;
5499 $this->tpl[
'label'] .= $productstatic->getNomUrl(1);
5500 $this->tpl[
'label'] .=
' - '.(!empty($line->label) ? $line->label : $line->product_label);
5502 if ($line->product_type == 1 && ($date_start || $date_end)) {
5506 $this->tpl[
'label'] .= ($line->product_type == -1 ?
' ' : ($line->product_type == 1 ?
img_object($langs->trans(
''),
'service') :
img_object($langs->trans(
''),
'product')));
5507 if (!empty($line->desc)) {
5508 $this->tpl[
'label'] .= $line->desc;
5510 $this->tpl[
'label'] .= ($line->label ?
' '.$line->label :
'');
5514 if ($line->product_type == 1 && ($date_start || $date_end)) {
5519 if (!empty($line->desc)) {
5520 if ($line->desc ==
'(CREDIT_NOTE)') {
5522 $discount->fetch($line->fk_remise_except);
5523 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromCreditNote", $discount->getNomUrl(0));
5524 } elseif ($line->desc ==
'(DEPOSIT)') {
5526 $discount->fetch($line->fk_remise_except);
5527 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromDeposit", $discount->getNomUrl(0));
5528 } elseif ($line->desc ==
'(EXCESS RECEIVED)') {
5530 $discount->fetch($line->fk_remise_except);
5531 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromExcessReceived", $discount->getNomUrl(0));
5532 } elseif ($line->desc ==
'(EXCESS PAID)') {
5534 $discount->fetch($line->fk_remise_except);
5535 $this->tpl[
'description'] = $langs->transnoentities(
"DiscountFromExcessPaid", $discount->getNomUrl(0));
5537 $this->tpl[
'description'] =
dol_trunc($line->desc, 60);
5540 $this->tpl[
'description'] =
' ';
5544 $this->tpl[
'vat_rate'] =
vatrate($line->tva_tx,
true);
5545 $this->tpl[
'vat_rate'] .= (($line->info_bits & 1) == 1) ?
'*' :
'';
5546 if (!empty($line->vat_src_code) && !preg_match(
'/\(/', $this->tpl[
'vat_rate'])) {
5547 $this->tpl[
'vat_rate'] .=
' ('.$line->vat_src_code.
')';
5550 $this->tpl[
'price'] =
price($line->subprice);
5551 $this->tpl[
'total_ht'] =
price($line->total_ht);
5552 $this->tpl[
'multicurrency_price'] =
price($line->multicurrency_subprice);
5553 $this->tpl[
'qty'] = (($line->info_bits & 2) != 2) ? $line->qty :
' ';
5555 $this->tpl[
'unit'] = $langs->transnoentities($line->getLabelOfUnit(
'long'));
5557 $this->tpl[
'remise_percent'] = (($line->info_bits & 2) != 2) ?
vatrate($line->remise_percent,
true) :
' ';
5560 $this->tpl[
'strike'] = 0;
5562 $this->tpl[
'strike'] = 1;
5567 $dirtpls = array_merge($conf->modules_parts[
'tpl'], array($defaulttpldir));
5568 foreach ($dirtpls as $module => $reldir) {
5569 if (!empty($module)) {
5572 $tpl = DOL_DOCUMENT_ROOT.$reldir.
'/originproductline.tpl.php';
5575 if (empty($conf->file->strict_mode)) {
5576 $res = @include $tpl;
5578 $res = include $tpl;
5701 protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams =
null)
5703 global $conf, $langs, $user, $hookmanager, $action;
5705 $srctemplatepath =
'';
5707 $parameters = array(
'modelspath' => $modelspath,
'modele' => $modele,
'outputlangs' => $outputlangs,
'hidedetails' => $hidedetails,
'hidedesc' => $hidedesc,
'hideref' => $hideref,
'moreparams' => $moreparams);
5708 $reshook = $hookmanager->executeHooks(
'commonGenerateDocument', $parameters, $this, $action);
5710 if (!empty($reshook)) {
5714 dol_syslog(
"commonGenerateDocument modele=".$modele.
" outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
5716 if (empty($modele)) {
5717 $this->error =
'BadValueForParameterModele';
5722 $err = error_reporting();
5724 @set_time_limit(120);
5725 error_reporting($err);
5728 $tmp = explode(
':', $modele, 2);
5729 $saved_model = $modele;
5730 if (!empty($tmp[1])) {
5732 $srctemplatepath = $tmp[1];
5739 $dirmodels = array(
'/');
5740 if (is_array($conf->modules_parts[
'models'])) {
5741 $dirmodels = array_merge($dirmodels, $conf->modules_parts[
'models']);
5743 foreach ($dirmodels as $reldir) {
5744 foreach (array(
'doc',
'pdf') as $prefix) {
5745 if (in_array(get_class($this), array(
'Adherent'))) {
5747 $file = $prefix.
"_".$modele.
".class.php";
5750 $file = $prefix.
"_".$modele.
".modules.php";
5757 if (file_exists($file)) {
5759 $classname = $prefix.
'_'.$modele;
5768 if ($filefound ===
'' || $classname ===
'') {
5769 $this->error = $langs->trans(
"Error").
' Failed to load doc generator with modelpaths='.$modelspath.
' - modele='.$modele;
5770 $this->errors[] = $this->error;
5781 require_once $filefound;
5783 $obj =
new $classname($this->db);
5786 if ($obj->type ==
'odt' && empty($srctemplatepath)) {
5787 $varfortemplatedir = $obj->scandir;
5791 $listoffiles = array();
5794 $listofdir = explode(
',', $dirtoscan);
5795 foreach ($listofdir as $key => $tmpdir) {
5796 $tmpdir = trim($tmpdir);
5797 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
5799 unset($listofdir[$key]);
5802 if (is_dir($tmpdir)) {
5803 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.od(s|t)$',
'',
'name', SORT_ASC, 0);
5804 if (count($tmpfiles)) {
5805 $listoffiles = array_merge($listoffiles, $tmpfiles);
5810 if (count($listoffiles)) {
5811 foreach ($listoffiles as $record) {
5812 $srctemplatepath = $record[
'fullname'];
5818 if (empty($srctemplatepath)) {
5819 $this->error =
'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined';
5824 if ($obj->type ==
'odt' && !empty($srctemplatepath)) {
5826 dol_syslog(
"Failed to locate template file ".$srctemplatepath, LOG_WARNING);
5827 $this->error =
'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound';
5834 $sav_charset_output = empty($outputlangs->charset_output) ?
'' : $outputlangs->charset_output;
5837 $this->model_pdf = $saved_model;
5839 if (in_array(get_class($this), array(
'Adherent'))) {
5840 '@phan-var-force Adherent $this';
5841 $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath,
'member', 1,
'tmp_cards', $moreparams);
5843 $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
5847 if ($resultwritefile > 0) {
5848 $outputlangs->charset_output = $sav_charset_output;
5851 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
5855 if (!empty($obj->result[
'fullpath'])) {
5856 $destfull = $obj->result[
'fullpath'];
5859 $update_main_doc_field = 0;
5860 if (!empty($obj->update_main_doc_field)) {
5861 $update_main_doc_field = 1;
5867 $this->indexFile($destfull, $update_main_doc_field);
5870 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);
5878 $outputlangs->charset_output = $sav_charset_output;
5879 $this->error = $obj->error;
5880 $this->errors = $obj->errors;
5881 dol_syslog(
"Error generating document for ".__CLASS__.
". Error: ".$obj->error, LOG_ERR);
5895 public function indexFile($destfull, $update_main_doc_field)
5897 global $conf, $user;
5899 $upload_dir = dirname($destfull);
5900 $destfile = basename($destfull);
5901 $rel_dir = preg_replace(
'/^'.preg_quote(DOL_DATA_ROOT,
'/').
'/',
'', $upload_dir);
5903 if (!preg_match(
'/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) {
5904 $filename = basename($destfile);
5905 $rel_dir = preg_replace(
'/[\\/]$/',
'', $rel_dir);
5906 $rel_dir = preg_replace(
'/^[\\/]/',
'', $rel_dir);
5908 include_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmfiles.class.php';
5909 $ecmfile =
new EcmFiles($this->db);
5910 $result = $ecmfile->fetch(0,
'', ($rel_dir ? $rel_dir.
'/' :
'').$filename);
5913 $setsharekey =
false;
5914 if ($this->element ==
'propal' || $this->element ==
'proposal') {
5916 $setsharekey =
true;
5919 $setsharekey =
true;
5922 if ($this->element ==
'commande' &&
getDolGlobalInt(
"ORDER_ALLOW_EXTERNAL_DOWNLOAD")) {
5923 $setsharekey =
true;
5925 if ($this->element ==
'facture' &&
getDolGlobalInt(
"INVOICE_ALLOW_EXTERNAL_DOWNLOAD")) {
5926 $setsharekey =
true;
5928 if ($this->element ==
'bank_account' &&
getDolGlobalInt(
"BANK_ACCOUNT_ALLOW_EXTERNAL_DOWNLOAD")) {
5929 $setsharekey =
true;
5931 if ($this->element ==
'product' &&
getDolGlobalInt(
"PRODUCT_ALLOW_EXTERNAL_DOWNLOAD")) {
5932 $setsharekey =
true;
5934 if ($this->element ==
'contrat' &&
getDolGlobalInt(
"CONTRACT_ALLOW_EXTERNAL_DOWNLOAD")) {
5935 $setsharekey =
true;
5937 if ($this->element ==
'fichinter' &&
getDolGlobalInt(
"FICHINTER_ALLOW_EXTERNAL_DOWNLOAD")) {
5938 $setsharekey =
true;
5940 if ($this->element ==
'supplier_proposal' &&
getDolGlobalInt(
"SUPPLIER_PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD")) {
5941 $setsharekey =
true;
5943 if ($this->element ==
'societe_rib' &&
getDolGlobalInt(
"SOCIETE_RIB_ALLOW_ONLINESIGN")) {
5944 $setsharekey =
true;
5948 if (empty($ecmfile->share)) {
5949 require_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
5956 $ecmfile->fullpath_orig =
'';
5957 $ecmfile->gen_or_uploaded =
'generated';
5958 $ecmfile->description =
'';
5959 $ecmfile->keywords =
'';
5960 $result = $ecmfile->update($user);
5966 $ecmfile->entity = $conf->entity;
5967 $ecmfile->filepath = $rel_dir;
5968 $ecmfile->filename = $filename;
5970 $ecmfile->fullpath_orig =
'';
5971 $ecmfile->gen_or_uploaded =
'generated';
5972 $ecmfile->description =
'';
5973 $ecmfile->keywords =
'';
5974 $ecmfile->src_object_type = $this->table_element;
5975 $ecmfile->src_object_id = $this->id;
5977 $result = $ecmfile->create($user);
5989 if ($update_main_doc_field && !empty($this->table_element)) {
5990 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET last_main_doc = '".$this->db->escape($ecmfile->filepath.
"/".$ecmfile->filename).
"'";
5991 $sql .=
" WHERE rowid = ".((int) $this->
id);
5993 $resql = $this->db->query($sql);
5998 $this->last_main_doc = $ecmfile->filepath.
'/'.$ecmfile->filename;
6339 global $conf, $extrafields;
6341 if (empty($rowid)) {
6344 if (empty($rowid) && isset($this->
rowid)) {
6345 $rowid = $this->rowid;
6349 if (!$this->table_element) {
6353 $this->array_options = array();
6355 if (!is_array($optionsArray)) {
6357 if (!isset($extrafields) || !is_object($extrafields)) {
6358 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6363 if (empty($extrafields->attributes[$this->table_element][
'loaded'])) {
6364 $extrafields->fetch_name_optionals_label($this->table_element);
6366 $optionsArray = (!empty($extrafields->attributes[$this->table_element][
'label']) ? $extrafields->attributes[$this->table_element][
'label'] :
null);
6368 global $extrafields;
6369 dol_syslog(
"Warning: fetch_optionals was called with param optionsArray defined when you should pass null now", LOG_WARNING);
6372 $table_element = $this->table_element;
6373 if ($table_element ==
'categorie') {
6374 $table_element =
'categories';
6378 if (is_array($optionsArray) && count($optionsArray) > 0) {
6379 $sql =
"SELECT rowid";
6380 foreach ($optionsArray as $name => $label) {
6381 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || (!in_array($extrafields->attributes[$this->table_element][
'type'][$name], [
'separate',
'point',
'multipts',
'linestrg',
'polygon']))) {
6385 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'point') {
6386 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6388 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'multipts') {
6389 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6391 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'linestrg') {
6392 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6394 if (empty($extrafields->attributes[$this->table_element][
'type'][$name]) || $extrafields->attributes[$this->table_element][
'type'][$name] ==
'polygon') {
6395 $sql .=
", ST_AsWKT(".$name.
") as ".$name;
6398 $sql .=
" FROM ".$this->db->prefix().$table_element.
"_extrafields";
6399 $sql .=
" WHERE fk_object = ".((int) $rowid);
6402 $resql = $this->db->query($sql);
6404 $numrows = $this->db->num_rows($resql);
6406 $tab = $this->db->fetch_array($resql);
6408 foreach ($tab as $key => $value) {
6410 if ($key !=
'rowid' && $key !=
'tms' && $key !=
'fk_member' && !is_int($key)) {
6412 if (!empty($extrafields->attributes[$this->table_element]) && in_array($extrafields->attributes[$this->table_element][
'type'][$key], array(
'date',
'datetime'))) {
6414 $this->array_options[
"options_".$key] = $this->db->jdate($value);
6416 $this->array_options[
"options_".$key] = $value;
6421 if (!empty($extrafields->attributes[$this->table_element][
'type'][$key]) && $extrafields->attributes[$this->table_element][
'type'][$key] ==
'password') {
6422 if (!empty($value) && preg_match(
'/^dolcrypt:/', $value)) {
6423 $this->array_options[
"options_".$key] =
dolDecrypt($value);
6432 if (is_array($extrafields->attributes[$this->table_element][
'label'])) {
6433 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
6434 $this->array_options[
'options_' . $key] =
null;
6441 if (is_array($extrafields->attributes[$this->table_element][
'label'])) {
6442 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
6443 if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element][
'computed'][$key])) {
6445 if (empty($conf->disable_compute)) {
6446 global $objectoffield;
6447 $objectoffield = $this;
6448 $this->array_options[
'options_' . $key] =
dol_eval($extrafields->attributes[$this->table_element][
'computed'][$key], 1, 0,
'2');
6454 $this->db->free($resql);
6462 $this->errors[] = $this->db->lasterror;
6517 global $conf, $langs, $user;
6523 if (empty($userused)) {
6529 if (!empty($this->array_options)) {
6531 $langs->load(
'admin');
6532 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6534 $target_extrafields = $extrafields->fetch_name_optionals_label($this->table_element);
6537 $new_array_options = array();
6538 foreach ($this->array_options as $key => $value) {
6539 if (in_array(substr($key, 8), array_keys($target_extrafields))) {
6540 $new_array_options[$key] = $value;
6541 } elseif (in_array($key, array_keys($target_extrafields))) {
6542 $new_array_options[
'options_'.$key] = $value;
6546 foreach ($new_array_options as $key => $value) {
6547 $attributeKey = substr($key, 8);
6548 $attributeType = $extrafields->attributes[$this->table_element][
'type'][$attributeKey];
6549 $attributeLabel = $langs->transnoentities($extrafields->attributes[$this->table_element][
'label'][$attributeKey]);
6550 $attributeParam = $extrafields->attributes[$this->table_element][
'param'][$attributeKey];
6551 $attributeRequired = $extrafields->attributes[$this->table_element][
'required'][$attributeKey];
6552 $attributeUnique = $extrafields->attributes[$this->table_element][
'unique'][$attributeKey];
6553 $attrfieldcomputed = $extrafields->attributes[$this->table_element][
'computed'][$attributeKey];
6557 if (!empty($this->context[
'createfromclone']) && $this->context[
'createfromclone'] ==
'createfromclone' && !empty($attributeUnique)) {
6558 $new_array_options[$key] =
null;
6563 if (!empty($this->context[
'createproductcombination']) && $this->context[
'createproductcombination'] ==
'createproductcombination' && !empty($attributeUnique)) {
6564 $new_array_options[$key] =
null;
6568 if ($attributeRequired) {
6569 $v = $this->array_options[$key];
6571 $langs->load(
"errors");
6572 dol_syslog(
"Mandatory field '".$key.
"' is empty during create and set to required into definition of extrafields");
6573 $this->errors[] = $langs->trans(
'ErrorFieldRequired', $attributeLabel);
6581 if (!empty($attrfieldcomputed)) {
6583 $value =
dol_eval($attrfieldcomputed, 1, 0,
'2');
6584 dol_syslog($langs->trans(
"Extrafieldcomputed").
" on ".$attributeLabel.
"(".$value.
")", LOG_DEBUG);
6585 $new_array_options[$key] = $value;
6587 $new_array_options[$key] =
null;
6591 switch ($attributeType) {
6593 if (!is_numeric($value) && $value !=
'') {
6594 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
6596 } elseif ($value ==
'') {
6597 $new_array_options[$key] =
null;
6603 if (!is_numeric($value) && $value !=
'') {
6604 dol_syslog($langs->trans(
"ExtraFieldHasWrongValue").
" for ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
6605 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
6607 } elseif ($value ==
'') {
6611 $new_array_options[$key] = $value;
6621 if ($this->array_options[$key] !=
'' && is_array($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options'])) {
6623 $tmparrays = array_keys($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options']);
6624 $algo = reset($tmparrays);
6629 if (is_object($this->oldcopy)) {
6631 if (isset($this->oldcopy->array_options[$key]) && $this->array_options[$key] == $this->oldcopy->array_options[$key]) {
6633 if ($algo ==
'dolcrypt') {
6634 if (!preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6635 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6637 $new_array_options[$key] = $this->array_options[$key];
6640 $new_array_options[$key] = $this->array_options[$key];
6644 if ($algo ==
'dolcrypt') {
6645 if (!preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6646 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6648 $new_array_options[$key] = $this->array_options[$key];
6651 $new_array_options[$key] =
dol_hash($this->array_options[$key], $algo);
6657 if ($algo ==
'dolcrypt' && !preg_match(
'/^dolcrypt:/', $this->array_options[$key])) {
6658 $new_array_options[$key] =
dolEncrypt($this->array_options[$key]);
6660 $new_array_options[$key] = $this->array_options[$key];
6665 $new_array_options[$key] = $this->array_options[$key];
6668 $new_array_options[$key] = $this->array_options[$key];
6674 if (!is_numeric($this->array_options[$key]) || $this->array_options[$key] != intval($this->array_options[$key])) {
6675 $this->array_options[$key] = strtotime($this->array_options[$key]);
6677 $new_array_options[$key] = $this->db->idate($this->array_options[$key]);
6681 if (!is_numeric($this->array_options[$key]) || $this->array_options[$key] != intval($this->array_options[$key])) {
6682 $this->array_options[$key] = strtotime($this->array_options[$key]);
6684 $new_array_options[$key] = $this->db->idate($this->array_options[$key],
'gmt');
6687 $param_list = array_keys($attributeParam[
'options']);
6690 $InfoFieldList = explode(
":", $param_list[0]);
6692 if ($InfoFieldList[0] && class_exists($InfoFieldList[0])) {
6693 if ($value ==
'-1') {
6694 $new_array_options[$key] =
'';
6696 $object =
new $InfoFieldList[0]($this->db);
6697 if (is_numeric($value)) {
6698 $res =
$object->fetch($value);
6700 $res =
$object->fetch(
'', $value);
6704 $new_array_options[$key] =
$object->id;
6706 $this->error =
"Id/Ref '".$value.
"' for object '".
$object->element.
"' not found";
6711 dol_syslog(
'Error bad setup of extrafield', LOG_WARNING);
6716 if (is_array($this->array_options[$key])) {
6717 $new_array_options[$key] = implode(
',', $this->array_options[$key]);
6719 $new_array_options[$key] = $this->array_options[$key];
6727 $table_element = $this->table_element;
6728 if ($table_element ==
'categorie') {
6729 $table_element =
'categories';
6732 dol_syslog(get_class($this).
"::insertExtraFields delete then insert", LOG_DEBUG);
6734 $sql_del =
"DELETE FROM ".$this->db->prefix().$table_element.
"_extrafields WHERE fk_object = ".((int) $this->
id);
6735 $this->db->query($sql_del);
6737 $sql =
"INSERT INTO ".$this->db->prefix().$table_element.
"_extrafields (fk_object";
6738 foreach ($new_array_options as $key => $value) {
6739 $attributeKey = substr($key, 8);
6741 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] !=
'separate') {
6742 $sql .=
",".$attributeKey;
6746 if (!empty($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities']) && is_array($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'])) {
6747 foreach ($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) {
6748 if (!isset($extrafields->attributes[$this->table_element][
'type'][$tmpkey])) {
6749 $sql .=
",".$tmpkey;
6753 $sql .=
") VALUES (".$this->id;
6755 foreach ($new_array_options as $key => $value) {
6756 $attributeKey = substr($key, 8);
6758 if (!in_array($extrafields->attributes[$this->table_element][
'type'][$attributeKey], [
'separate',
'point',
'multipts',
'linestrg',
'polygon'])) {
6759 if ($new_array_options[$key] !=
'' || $new_array_options[$key] ==
'0') {
6760 $sql .=
",'".$this->db->escape($new_array_options[$key]).
"'";
6765 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'point') {
6766 if (!empty($new_array_options[$key])) {
6767 $sql .=
",ST_PointFromText('".$this->db->escape($new_array_options[$key]).
"')";
6772 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'multipts') {
6773 if (!empty($new_array_options[$key])) {
6774 $sql .=
",ST_MultiPointFromText('".$this->db->escape($new_array_options[$key]).
"')";
6779 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'linestrg') {
6780 if (!empty($new_array_options[$key])) {
6781 $sql .=
",ST_LineFromText('".$this->db->escape($new_array_options[$key]).
"')";
6786 if ($extrafields->attributes[$this->table_element][
'type'][$attributeKey] ==
'polygon') {
6787 if (!empty($new_array_options[$key])) {
6788 $sql .=
",ST_PolyFromText('".$this->db->escape($new_array_options[$key]).
"')";
6795 if (!empty($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities']) && is_array($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'])) {
6796 foreach ($extrafields->attributes[$this->table_element][
'mandatoryfieldsofotherentities'] as $tmpkey => $tmpval) {
6797 if (!isset($extrafields->attributes[$this->table_element][
'type'][$tmpkey])) {
6798 if (in_array($tmpval, array(
'int',
'double',
'price'))) {
6809 $resql = $this->db->query($sql);
6811 $this->error = $this->db->lasterror();
6815 if (!$error && $trigger) {
6817 $this->context = array(
'extrafieldaddupdate' => 1);
6818 $result = $this->call_trigger($trigger, $userused);
6826 $this->db->rollback();
6829 $this->db->commit();
6849 global $conf, $langs, $user;
6851 if (empty($userused)) {
6861 if (is_array($this->array_languages)) {
6862 $new_array_languages = $this->array_languages;
6864 foreach ($new_array_languages as $key => $value) {
6865 $attributeKey = $key;
6866 $attributeType = $this->fields[$attributeKey][
'type'];
6867 $attributeLabel = $this->fields[$attributeKey][
'label'];
6872 switch ($attributeType) {
6874 if (is_array($value) || (!is_numeric($value) && $value !=
'')) {
6875 $this->errors[] = $langs->trans(
"ExtraLanguageHasWrongValue", $attributeLabel);
6877 } elseif ($value ==
'') {
6878 $new_array_languages[$key] =
null;
6883 if (!is_numeric($value) && $value !=
'') {
6884 dol_syslog($langs->trans(
"ExtraLanguageHasWrongValue").
" on ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
6885 $this->errors[] = $langs->trans(
"ExtraLanguageHasWrongValue", $attributeLabel);
6887 } elseif ($value ==
'') {
6888 $new_array_languages[$key] =
null;
6890 $new_array_languages[$key] = $value;
6904 $table_element = $this->table_element;
6905 if ($table_element ==
'categorie') {
6906 $table_element =
'categories';
6909 dol_syslog(get_class($this).
"::insertExtraLanguages delete then insert", LOG_DEBUG);
6911 foreach ($new_array_languages as $key => $langcodearray) {
6912 foreach ($langcodearray as $langcode => $value) {
6913 $sql_del =
"DELETE FROM ".$this->db->prefix().
"object_lang";
6914 $sql_del .=
" WHERE fk_object = ".((int) $this->
id).
" AND property = '".$this->db->escape($key).
"' AND type_object = '".$this->db->escape($table_element).
"'";
6915 $sql_del .=
" AND lang = '".$this->db->escape($langcode).
"'";
6916 $this->db->query($sql_del);
6918 if ($value !==
'') {
6919 $sql =
"INSERT INTO ".$this->db->prefix().
"object_lang (fk_object, property, type_object, lang, value";
6920 $sql .=
") VALUES (".$this->id.
", '".$this->db->escape($key).
"', '".$this->db->escape($table_element).
"', '".$this->db->escape($langcode).
"', '".$this->db->escape($value).
"'";
6923 $resql = $this->db->query($sql);
6925 $this->error = $this->db->lasterror();
6933 if (!$error && $trigger) {
6935 $this->context = array(
'extralanguagesaddupdate' => 1);
6936 $result = $this->call_trigger($trigger, $userused);
6944 $this->db->rollback();
6947 $this->db->commit();
6967 global $conf, $langs, $user, $hookmanager;
6973 if (empty($userused)) {
6979 if (!empty($this->array_options) && isset($this->array_options[
"options_".$key])) {
6981 $langs->load(
'admin');
6982 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
6984 $extrafields->fetch_name_optionals_label($this->table_element);
6986 $value = $this->array_options[
"options_".$key];
6988 $attributeKey = $key;
6989 $attributeType = $extrafields->attributes[$this->table_element][
'type'][$key];
6990 $attributeLabel = $extrafields->attributes[$this->table_element][
'label'][$key];
6991 $attributeParam = $extrafields->attributes[$this->table_element][
'param'][$key];
6992 $attributeRequired = $extrafields->attributes[$this->table_element][
'required'][$key];
6993 $attributeUnique = $extrafields->attributes[$this->table_element][
'unique'][$attributeKey];
6994 $attrfieldcomputed = $extrafields->attributes[$this->table_element][
'computed'][$key];
6997 if ($attributeRequired) {
6998 $mandatorypb =
false;
6999 if ($attributeType ==
'link' && $this->array_options[
"options_".$key] ==
'-1') {
7000 $mandatorypb =
true;
7002 if ($this->array_options[
"options_".$key] ===
'') {
7003 $mandatorypb =
true;
7006 $langs->load(
"errors");
7007 dol_syslog(
"Mandatory field 'options_".$key.
"' is empty during update and set to required into definition of extrafields");
7008 $this->errors[] = $langs->trans(
'ErrorFieldRequired', $attributeLabel);
7014 $new_array_options = $this->array_options;
7018 if (!empty($attrfieldcomputed)) {
7020 $value =
dol_eval($attrfieldcomputed, 1, 0,
'2');
7021 dol_syslog($langs->trans(
"Extrafieldcomputed").
" on ".$attributeLabel.
"(".$value.
")", LOG_DEBUG);
7023 $new_array_options[
"options_".$key] = $value;
7025 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7027 $new_array_options[
"options_".$key] =
null;
7029 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7033 switch ($attributeType) {
7035 if (!is_numeric($value) && $value !=
'') {
7036 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
7038 } elseif ($value ===
'') {
7039 $new_array_options[
"options_".$key] =
null;
7041 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7047 if (!is_numeric($value) && $value !=
'') {
7048 dol_syslog($langs->trans(
"ExtraFieldHasWrongValue").
" on ".$attributeLabel.
"(".$value.
"is not '".$attributeType.
"')", LOG_DEBUG);
7049 $this->errors[] = $langs->trans(
"ExtraFieldHasWrongValue", $attributeLabel);
7051 } elseif ($value ===
'') {
7055 $new_array_options[
"options_".$key] = $value;
7057 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7069 if ($this->array_options[
"options_".$key] !=
'' && is_array($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options'])) {
7071 $tmparrays = array_keys($extrafields->attributes[$this->table_element][
'param'][$attributeKey][
'options']);
7072 $algo = reset($tmparrays);
7078 if (is_object($this->oldcopy)) {
7080 if (isset($this->oldcopy->array_options[
"options_".$key]) && $this->array_options[
"options_".$key] == $this->oldcopy->array_options[
"options_".$key]) {
7081 if ($algo ==
'dolcrypt') {
7082 if (!preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7083 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7085 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7088 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7091 if ($algo ==
'dolcrypt') {
7092 if (!preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7093 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7095 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7098 $new_array_options[
"options_".$key] =
dol_hash($this->array_options[
"options_".$key], $algo);
7102 if ($algo ==
'dolcrypt' && !preg_match(
'/^dolcrypt:/', $this->array_options[
"options_".$key])) {
7103 $new_array_options[
"options_".$key] =
dolEncrypt($this->array_options[
"options_".$key]);
7105 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7110 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7113 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7116 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7120 if (empty($this->array_options[
"options_".$key])) {
7121 $new_array_options[
"options_".$key] =
null;
7123 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7125 $new_array_options[
"options_".$key] = $this->db->idate($this->array_options[
"options_".$key]);
7129 if (empty($this->array_options[
"options_".$key])) {
7130 $new_array_options[
"options_".$key] =
null;
7132 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7134 $new_array_options[
"options_".$key] = $this->db->idate($this->array_options[
"options_".$key],
'gmt');
7138 if (empty($this->array_options[
"options_".$key])) {
7139 $new_array_options[
"options_".$key] =
null;
7141 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7145 if ($this->array_options[
"options_".$key] ===
'') {
7146 $new_array_options[
"options_".$key] =
null;
7148 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7182 $new_array_options = array();
7183 if (is_array($this->array_options[
"options_".$key])) {
7184 $new_array_options[
"options_".$key] = implode(
',', $this->array_options[
"options_".$key]);
7186 $new_array_options[
"options_".$key] = $this->array_options[
"options_".$key];
7189 $this->array_options[
"options_".$key] = $new_array_options[
"options_".$key];
7195 $linealreadyfound = 0;
7198 $table_element = $this->table_element;
7199 if ($table_element ==
'categorie') {
7200 $table_element =
'categories';
7203 $sql =
"SELECT COUNT(rowid) as nb FROM ".$this->db->prefix().$table_element.
"_extrafields WHERE fk_object = ".((int) $this->
id);
7204 $resql = $this->db->query($sql);
7206 $tmpobj = $this->db->fetch_object($resql);
7208 $linealreadyfound = $tmpobj->nb;
7213 if ($linealreadyfound) {
7214 if ($this->array_options[
"options_".$key] ===
null) {
7215 $sql =
"UPDATE ".$this->db->prefix().$table_element.
"_extrafields SET ".$key.
" = null";
7217 $sql =
"UPDATE ".$this->db->prefix().$table_element.
"_extrafields SET ".$key.
" = '".$this->db->escape($new_array_options[
"options_".$key]).
"'";
7219 $sql .=
" WHERE fk_object = ".((int) $this->
id);
7221 $resql = $this->db->query($sql);
7224 $this->error = $this->db->lasterror();
7227 $result = $this->insertExtraFields(
'', $user);
7234 $parameters = array(
'key' => $key);
7236 $reshook = $hookmanager->executeHooks(
'updateExtraFieldBeforeCommit', $parameters, $this, $action);
7242 if (!$error && $trigger) {
7244 $this->context = array(
'extrafieldupdate' => 1);
7245 $result = $this->call_trigger($trigger, $userused);
7253 dol_syslog(__METHOD__.$this->error, LOG_ERR);
7254 $this->db->rollback();
7257 $this->db->commit();
7331 public function showInputField($val, $key, $value, $moreparam =
'', $keysuffix =
'', $keyprefix =
'', $morecss = 0, $nonewbutton = 0)
7333 global $conf, $langs, $form;
7335 if (!is_object($form)) {
7336 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
7337 $form =
new Form($this->db);
7340 if (!empty($this->fields)) {
7341 $val = $this->fields[$key];
7345 $fieldValidationErrorMsg =
'';
7346 $validationClass =
'';
7347 $fieldValidationErrorMsg = $this->getFieldError($key);
7348 if (!empty($fieldValidationErrorMsg)) {
7349 $validationClass =
' --error';
7351 $validationClass =
' --success';
7354 $valuemultiselectinput = array();
7359 $param[
'options'] = array();
7362 $size = !empty($this->fields[$key][
'size']) ? $this->fields[$key][
'size'] : 0;
7364 if (preg_match(
'/^(integer|link):(.*):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7365 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4].
':'.$reg[5] =>
'N');
7367 } elseif (preg_match(
'/^(integer|link):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7368 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4] =>
'N');
7370 } elseif (preg_match(
'/^(integer|link):(.*):(.*)/i', $val[
'type'], $reg)) {
7371 $param[
'options'] = array($reg[2].
':'.$reg[3] =>
'N');
7373 } elseif (preg_match(
'/^(sellist):(.*):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7374 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4].
':'.$reg[5] =>
'N');
7376 } elseif (preg_match(
'/^(sellist):(.*):(.*):(.*)/i', $val[
'type'], $reg)) {
7377 $param[
'options'] = array($reg[2].
':'.$reg[3].
':'.$reg[4] =>
'N');
7379 } elseif (preg_match(
'/^(sellist):(.*):(.*)/i', $val[
'type'], $reg)) {
7380 $param[
'options'] = array($reg[2].
':'.$reg[3] =>
'N');
7382 } elseif (preg_match(
'/^chkbxlst:(.*)/i', $val[
'type'], $reg)) {
7383 $param[
'options'] = array($reg[1] =>
'N');
7385 } elseif (preg_match(
'/varchar\((\d+)\)/', $val[
'type'], $reg)) {
7386 $param[
'options'] = array();
7389 } elseif (preg_match(
'/varchar/', $val[
'type'])) {
7390 $param[
'options'] = array();
7393 $param[
'options'] = array();
7394 $type = $this->fields[$key][
'type'];
7399 if (!empty($this->fields[$key][
'arrayofkeyval']) && is_array($this->fields[$key][
'arrayofkeyval'])) {
7400 $param[
'options'] = $this->fields[$key][
'arrayofkeyval'];
7402 if (empty($this->fields[$key][
'multiinput'])) {
7403 $type = (($this->fields[$key][
'type'] ==
'checkbox') ? $this->fields[$key][
'type'] :
'select');
7407 $label = $this->fields[$key][
'label'];
7410 $default = (!empty($this->fields[$key][
'default']) ? $this->fields[$key][
'default'] :
'');
7412 $computed = (!empty($this->fields[$key][
'computed']) ? $this->fields[$key][
'computed'] :
'');
7414 $unique = (!empty($this->fields[$key][
'unique']) ? $this->fields[$key][
'unique'] : 0);
7416 $required = (!empty($this->fields[$key][
'required']) ? $this->fields[$key][
'required'] : 0);
7418 $autofocusoncreate = (!empty($this->fields[$key][
'autofocusoncreate']) ? $this->fields[$key][
'autofocusoncreate'] : 0);
7421 $langfile = (!empty($this->fields[$key][
'langfile']) ? $this->fields[$key][
'langfile'] :
'');
7423 $list = (!empty($this->fields[$key][
'list']) ? $this->fields[$key][
'list'] : 0);
7424 $hidden = (in_array(abs($this->fields[$key][
'visible']), array(0, 2)) ? 1 : 0);
7426 $objectid = $this->id;
7429 if (!preg_match(
'/^search_/', $keyprefix)) {
7430 return '<span class="opacitymedium">'.$langs->trans(
"AutomaticallyCalculated").
'</span>';
7437 if (empty($morecss) && !empty($val[
'css'])) {
7438 $morecss = $val[
'css'];
7439 } elseif (empty($morecss)) {
7440 if ($type ==
'date') {
7441 $morecss =
'minwidth100imp';
7442 } elseif ($type ==
'datetime' || $type ==
'link') {
7443 $morecss =
'minwidth200imp';
7444 } elseif (in_array($type, array(
'int',
'integer',
'price')) || preg_match(
'/^double(\([0-9],[0-9]\)){0,1}/', (
string) $type)) {
7445 $morecss =
'maxwidth75';
7446 } elseif ($type ==
'url') {
7447 $morecss =
'minwidth400';
7448 } elseif ($type ==
'boolean') {
7451 if (is_numeric($size) && round((
float) $size) < 12) {
7452 $morecss =
'minwidth100';
7453 } elseif (is_numeric($size) && round((
float) $size) <= 48) {
7454 $morecss =
'minwidth200';
7456 $morecss =
'minwidth400';
7462 if (!empty($validationClass)) {
7463 $morecss .= $validationClass;
7466 if (in_array($type, array(
'date'))) {
7467 $tmp = explode(
',', $size);
7472 if (!$required && $value ==
'') {
7477 $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $show