From fb6677010dabfc4e2f79654dec146e8aa93bb399 Mon Sep 17 00:00:00 2001 From: William Mead Date: Tue, 10 Sep 2024 03:04:49 +0200 Subject: [PATCH 1/3] NEW contract signed status update (#30779) * Moved signed status constants to array dictionary * Added getLibSignedStatus & getSignedStatusLocalisedArray methods * Updated fetch & create methods for signed_status * Added signed status to status badge * Updated signed_status module builder field * Added object context signed status for action trigger * Added signed status to contract list * Fixed signed status for contract list * Added sign, unsign, confirm_sign, confirm_unsign actions. Added sign button. * Fixed old contract without signed status * Added contract modify notification * Added contract signed status update * Updated EN localizations * Fixed contract notification administration * Fixed notification message * Fixed contract status badge * Fixed langs * Fixed langs key * Fixed duplicate localization --- htdocs/admin/notification.php | 11 ++- htdocs/contrat/card.php | 52 ++++++++++++- htdocs/contrat/class/contrat.class.php | 101 +++++++++++++++++++------ htdocs/contrat/list.php | 30 +++++++- htdocs/core/ajax/onlineSign.php | 2 + htdocs/core/class/notify.class.php | 17 ++++- htdocs/langs/en_US/contracts.lang | 8 ++ htdocs/langs/en_US/other.lang | 2 + 8 files changed, 195 insertions(+), 28 deletions(-) diff --git a/htdocs/admin/notification.php b/htdocs/admin/notification.php index bc1c95aa688..ff47db34030 100644 --- a/htdocs/admin/notification.php +++ b/htdocs/admin/notification.php @@ -33,7 +33,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/triggers/interface_50_modNotification_Notification.class.php'; // Load translation files required by the page -$langs->loadLangs(array('admin', 'other', 'orders', 'propal', 'bills', 'errors', 'mails')); +$langs->loadLangs(array('admin', 'other', 'orders', 'propal', 'bills', 'errors', 'mails', 'contracts')); // Security check if (!$user->admin) { @@ -295,6 +295,8 @@ foreach ($listofnotifiedevents as $notifiedevent) { $elementLabel = $langs->trans('Shipping'); } elseif ($notifiedevent['elementtype'] == 'expensereport' || $notifiedevent['elementtype'] == 'expense_report') { $elementLabel = $langs->trans('ExpenseReport'); + } elseif ($notifiedevent['elementtype'] == 'contrat') { + $elementLabel = $langs->trans('Contract'); } if ($notifiedevent['elementtype'] == 'propal') { @@ -315,6 +317,8 @@ foreach ($listofnotifiedevents as $notifiedevent) { $model = 'invoice_supplier_send'; } elseif ($notifiedevent['elementtype'] == 'member') { $model = 'member'; + } elseif ($notifiedevent['elementtype'] == 'contrat') { + $model = 'contract_send'; } $constantes[$notifiedevent['code'].'_TEMPLATE'] = array('type'=>'emailtemplate:'.$model, 'label'=>$label); @@ -435,11 +439,14 @@ foreach ($listofnotifiedevents as $notifiedevent) { } elseif ($notifiedevent['elementtype'] == 'expensereport' || $notifiedevent['elementtype'] == 'expense_report') { $elementPicto = 'expensereport'; $elementLabel = $langs->trans('ExpenseReport'); + } elseif ($notifiedevent['elementtype'] == 'contrat') { + $elementPicto = 'contract'; + $elementLabel = $langs->trans('Contract'); } elseif ($notifiedevent['elementtype'] == 'agenda') { $elementPicto = 'action'; } - $labelfortrigger = 'AmountHT'; + $labelfortrigger = 'AmountHT'; $codehasnotrigger = 0; if (preg_match('/^(ACTION|HOLIDAY)/', $notifiedevent['code'])) { $codehasnotrigger++; diff --git a/htdocs/contrat/card.php b/htdocs/contrat/card.php index d79817ff836..f18e5daa63f 100644 --- a/htdocs/contrat/card.php +++ b/htdocs/contrat/card.php @@ -56,7 +56,7 @@ if (isModEnabled('project')) { require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; // Load translation files required by the page -$langs->loadLangs(array("contracts", "orders", "companies", "bills", "products", 'compta')); +$langs->loadLangs(array("contracts", "orders", "companies", "bills", "products", 'compta', 'propal')); $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); @@ -175,6 +175,22 @@ if (empty($reshook)) { } else { setEventMessages($object->error, $object->errors, 'errors'); } + } elseif ($action == 'confirm_sign' && $confirm == 'yes' && $user->hasRight('contract', 'creer')) { + $result = $object->setSignedStatus($user, GETPOSTINT('signed_status'), 0, 'CONTRACT_MODIFY'); + if ($result >= 0) { + header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit; + } else { + $mesg = $object->error; + } + } elseif ($action == 'confirm_unsign' && $confirm == 'yes' && $user->hasRight('contract', 'creer')) { + $result = $object->setSignedStatus($user, $object::SIGNED_STATUSES['STATUS_NO_SIGNATURE'], 0, 'CONTRACT_MODIFY'); + if ($result >= 0) { + header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit; + } else { + $mesg = $object->error; + } } elseif ($action == 'confirm_closeline' && $confirm == 'yes' && $permissiontoactivate) { $date_end = ''; if (GETPOST('endmonth') && GETPOST('endday') && GETPOST('endyear')) { @@ -1363,6 +1379,31 @@ if ($action == 'create') { // Clone confirmation $formquestion = array(array('type' => 'other', 'name' => 'socid', 'label' => $langs->trans("SelectThirdParty"), 'value' => $form->select_company(GETPOSTINT('socid'), 'socid', $filter))); $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneContract', $object->ref), 'confirm_clone', $formquestion, 'yes', 1); + } elseif ($action == 'sign') { + $text = $langs->trans('ConfirmSignContract'); + if (isModEnabled('notification')) { + require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php'; + $notify = new Notify($db); + $text .= '
'; + $text .= $notify->confirmMessage('CONTRACT_MODIFY', $object->socid, $object); + } + $formquestion = []; + $formquestion[] = [ + 'type' => 'select', + 'name' => 'signed_status', + 'label' => ''.$langs->trans('SignStatus').'', + 'values' => $object->getSignedStatusLocalisedArray() + ]; + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('SignContract'), $text, 'confirm_sign', $formquestion, 0, 1); + } elseif ($action == 'unsign') { + $text = $langs->trans('ConfirmUnsignContract'); + if (isModEnabled('notification')) { + require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php'; + $notify = new Notify($db); + $text .= '
'; + $text .= $notify->confirmMessage('CONTRACT_MODIFY', $object->socid, $object); + } + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('UnsignContract'), $text, 'confirm_unsign', '', 0, 1); } @@ -2246,6 +2287,15 @@ if ($action == 'create') { } } + // Sign + if ($object->status > Contrat::STATUS_DRAFT) { + if ($object->signed_status != Contrat::SIGNED_STATUSES['STATUS_SIGNED_ALL']) { + print '
' . $langs->trans("ContractSign") . '
'; + } else { + print '
' . $langs->trans("ContractUnsign") . '
'; + } + } + // Clone if ($user->hasRight('contrat', 'creer')) { unset($params['attr']['title']); diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php index ba6a803d36e..741ee9f7197 100644 --- a/htdocs/contrat/class/contrat.class.php +++ b/htdocs/contrat/class/contrat.class.php @@ -238,7 +238,7 @@ class Contrat extends CommonObject 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35), 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 40), 'date_contrat' => array('type' => 'datetime', 'label' => 'Date contrat', 'enabled' => 1, 'visible' => -1, 'position' => 45), - 'signed_status' => array('type' => 'smallint(6)', 'label' => 'SignedStatus', 'enabled' => 1, 'visible' => -1, 'position' => 50, 'arrayofkeyval' => array(0 => 'NoSignature', 1 => 'SignedSender', 2 => 'SignedReceiver', 9 => 'SignedAll')), + 'signed_status' => array('type' => 'smallint(6)', 'label' => 'SignedStatus', 'enabled' => 1, 'visible' => -1, 'position' => 50, 'arrayofkeyval' => array(0 => 'NoSignature', 1 => 'SignedSender', 2 => 'SignedReceiver', 3 => 'SignedReceiverOnline', 9 => 'SignedAll')), 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 70), 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 75), 'fk_commercial_signature' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative Signature', 'enabled' => 1, 'visible' => -1, 'position' => 80), @@ -259,26 +259,16 @@ class Contrat extends CommonObject const STATUS_VALIDATED = 1; const STATUS_CLOSED = 2; - /* - * No signature + /** + * Signed statuses dictionary. Label used as key for string localizations. */ - const STATUS_NO_SIGNATURE = 0; - - /* - * Signed by sender - */ - const STATUS_SIGNED_SENDER = 1; - - /* - * Signed by receiver - */ - const STATUS_SIGNED_RECEIVER = 2; - - /* - * Signed by all - */ - const STATUS_SIGNED_ALL = 9; // To handle future kind of signature (ex: tripartite contract) - + const SIGNED_STATUSES = [ + 'STATUS_NO_SIGNATURE' => 0, + 'STATUS_SIGNED_SENDER' => 1, + 'STATUS_SIGNED_RECEIVER' => 2, + 'STATUS_SIGNED_RECEIVER_ONLINE' => 3, + 'STATUS_SIGNED_ALL' => 9 // To handle future kind of signature (ex: tripartite contract) + ]; /** * Constructor @@ -697,6 +687,7 @@ class Contrat extends CommonObject $sql .= " ref_supplier, ref_customer,"; $sql .= " ref_ext,"; $sql .= " entity,"; + $sql .= " signed_status,"; $sql .= " date_contrat as datecontrat,"; $sql .= " fk_user_author,"; $sql .= " fk_projet as fk_project,"; @@ -738,6 +729,7 @@ class Contrat extends CommonObject $this->entity = $obj->entity; $this->statut = $obj->status; $this->status = $obj->status; + $this->signed_status = $obj->signed_status; $this->date_contrat = $this->db->jdate($obj->datecontrat); $this->date_creation = $this->db->jdate($obj->datecontrat); @@ -1017,7 +1009,7 @@ class Contrat extends CommonObject // Insert contract $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (datec, fk_soc, fk_user_author, date_contrat,"; $sql .= " fk_commercial_signature, fk_commercial_suivi, fk_projet,"; - $sql .= " ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)"; + $sql .= " ref, entity, signed_status, note_private, note_public, ref_customer, ref_supplier, ref_ext)"; $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->socid).", ".((int) $user->id); $sql .= ", ".(dol_strlen($this->date_contrat) != 0 ? "'".$this->db->idate($this->date_contrat)."'" : "NULL"); $sql .= ",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) : "NULL"); @@ -1025,6 +1017,7 @@ class Contrat extends CommonObject $sql .= ",".($this->fk_project > 0 ? ((int) $this->fk_project) : "NULL"); $sql .= ", ".(dol_strlen($this->ref) <= 0 ? "null" : "'".$this->db->escape($this->ref)."'"); $sql .= ", ".((int) $conf->entity); + $sql .= ", ".((int) $this->signed_status); $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL"); $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL"); $sql .= ", ".(!empty($this->ref_customer) ? ("'".$this->db->escape($this->ref_customer)."'") : "NULL"); @@ -2001,6 +1994,7 @@ class Contrat extends CommonObject $text .= ($mode == 7 ? '' : ''); $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1, 'class="marginleft2"')) : ''; $text .= ($mode == 7 ? '' : ''); + $text .= $this->signed_status != '' ? ' '.$this->getLibSignedStatus(5) : ''; return $text; } else { return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode); @@ -2934,8 +2928,6 @@ class Contrat extends CommonObject return $return; } - // @Todo getLibSignedStatus, LibSignedStatus - /** * Set signed status * @@ -2947,6 +2939,69 @@ class Contrat extends CommonObject */ public function setSignedStatus(User $user, int $status = 0, int $notrigger = 0, $triggercode = ''): int { + global $langs; + $langs->loadLangs(array('contracts', 'commercial')); + $this->signed_status = $status; + $this->context['signature'] = $status; + switch ($status) { + case 0: + $this->context['actionmsg2'] = $langs->transnoentitiesnoconv('ContractUnsignedInDolibarr'); + break; + case 1: + $this->context['actionmsg2'] = $langs->transnoentitiesnoconv('SignedSender'); + break; + case 2: + $this->context['actionmsg2'] = $langs->transnoentitiesnoconv('SignedReceiver'); + break; + case 3: + $this->context['actionmsg2'] = $langs->transnoentitiesnoconv('SignedReceiverOnline'); + break; + case 9: + $this->context['actionmsg2'] = $langs->transnoentitiesnoconv('SignedAll'); + break; + } return $this->setSignedStatusCommon($user, $status, $notrigger, $triggercode); } + + /** + * Returns the label for signed status + * + * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto + * @return string Label + */ + public function getLibSignedStatus(int $mode = 0): string + { + global $langs; + $langs->load("commercial"); + $list_signed_status = $this->getSignedStatusLocalisedArray(); + $signed_status_label = $this->signed_status != '' ? $list_signed_status[$this->signed_status] : ''; + $signed_status_label_short = $this->signed_status != '' ? $list_signed_status[$this->signed_status] : ''; + $signed_status_code = 'status'.$this->signed_status; + return dolGetStatus($signed_status_label, $signed_status_label_short, '', $signed_status_code, $mode); + } + + /** + * Returns an array of signed statuses with associated localized labels + * + * @return array + */ + public function getSignedStatusLocalisedArray(): array + { + global $langs; + $langs->load("commercial"); + + $l10n_signed_status_labels = [ + self::SIGNED_STATUSES['STATUS_NO_SIGNATURE'] => 'NoSignature', + self::SIGNED_STATUSES['STATUS_SIGNED_SENDER'] => 'SignedSender', + self::SIGNED_STATUSES['STATUS_SIGNED_RECEIVER'] => 'SignedReceiver', + self::SIGNED_STATUSES['STATUS_SIGNED_RECEIVER_ONLINE'] => 'SignedReceiverOnline', + self::SIGNED_STATUSES['STATUS_SIGNED_ALL'] => 'SignedAll' + ]; + + $l10n_signed_status = []; + foreach (self::SIGNED_STATUSES as $signed_status_code) { + $l10n_signed_status[$signed_status_code] = $langs->transnoentitiesnoconv($l10n_signed_status_labels[$signed_status_code]); + } + return $l10n_signed_status; + } } diff --git a/htdocs/contrat/list.php b/htdocs/contrat/list.php index a79d3f31690..5760b6796ce 100644 --- a/htdocs/contrat/list.php +++ b/htdocs/contrat/list.php @@ -71,6 +71,7 @@ $search_ref_customer = GETPOST('search_ref_customer', 'alpha'); $search_ref_supplier = GETPOST('search_ref_supplier', 'alpha'); $search_all = (GETPOST('search_all', 'alphanohtml') != '') ? GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'); $search_status = GETPOST('search_status', 'alpha'); +$search_signed_status = GETPOST('search_signed_status', 'alpha'); $search_user = GETPOST('search_user', 'intcomma'); $search_sale = GETPOST('search_sale', 'intcomma'); $search_product_category = GETPOST('search_product_category', 'intcomma'); @@ -194,6 +195,7 @@ $arrayfields = array( 'c.tms' => array('label' => $langs->trans("DateModificationShort"), 'checked' => 0, 'position' => 500), 'lower_planned_end_date' => array('label' => $langs->trans("LowerDateEndPlannedShort"), 'checked' => 1, 'position' => 900, 'help' => $langs->trans("LowerDateEndPlannedShort")), 'status' => array('label' => $langs->trans("Status"), 'checked' => 1, 'position' => 1000), + 'c.signed_status' =>array('label' => $langs->trans('SignedStatus'), 'checked' => 0, 'position' => 1001), ); // Extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; @@ -277,6 +279,7 @@ if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x' $search_date_modif_endday = ""; $search_date_modif_end = ""; $search_status = ""; + $search_signed_status = ''; $toselect = array(); $search_type_thirdparty = ''; $searchCategoryCustomerList = array(); @@ -307,7 +310,7 @@ $now = dol_now(); $title = ""; $sql = 'SELECT'; -$sql .= " c.rowid, c.ref, c.datec as date_creation, c.tms as date_modification, c.date_contrat, c.statut, c.ref_customer, c.ref_supplier, c.note_private, c.note_public, c.entity,"; +$sql .= " c.rowid, c.ref, c.datec as date_creation, c.tms as date_modification, c.date_contrat, c.statut, c.ref_customer, c.ref_supplier, c.note_private, c.note_public, c.entity, c.signed_status,"; $sql .= ' s.rowid as socid, s.nom as name, s.name_alias, s.email, s.town, s.zip, s.fk_pays as country_id, s.client, s.code_client, s.status as company_status, s.logo as company_logo,'; $sql .= " typent.code as typent_code,"; $sql .= " state.code_departement as state_code, state.nom as state_name,"; @@ -489,6 +492,9 @@ if ($search_date_modif_start) { if ($search_date_modif_end) { $sql .= " AND c.tms <= '".$db->idate($search_date_modif_end)."'"; } +if ($search_signed_status != '' && $search_signed_status >= 0) { + $sql .= ' AND c.signed_status = '.urlencode($search_signed_status); +} // Add where from extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php'; @@ -713,6 +719,9 @@ if ($search_dfyear > 0) { if ($search_dfmonth > 0) { $param .= '&search_dfmonth='.urlencode((string) ($search_dfmonth)); } +if ($search_signed_status != '' && $search_signed_status >= 0) { + $param .= '&search_signed_status='.urlencode($search_signed_status); +} if ($search_sale > 0) { $param .= '&search_sale='.urlencode($search_sale); } @@ -933,6 +942,13 @@ if (!empty($arrayfields['c.date_contrat']['checked'])) { print ''; print ''; } +// Signed status +if (!empty($arrayfields['c.signed_status']['checked'])) { + print ''; + $list_signed_status = $object->getSignedStatusLocalisedArray(); + print $form->selectarray('search_signed_status', $list_signed_status, $search_signed_status, 1, 0, 0, '', 1, 0, 0, '', 'search_status'); + print ''; +} // Extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php'; @@ -1045,6 +1061,10 @@ if (!empty($arrayfields['c.date_contrat']['checked'])) { print_liste_field_titre($arrayfields['c.date_contrat']['label'], $_SERVER["PHP_SELF"], "c.date_contrat", "", $param, '', $sortfield, $sortorder, 'center '); $totalarray['nbfield']++; // For the column action } +if (!empty($arrayfields['c.signed_status']['checked'])) { + print_liste_field_titre($arrayfields['c.signed_status']['label'], $_SERVER["PHP_SELF"], "c.signed_status", "", $param, '', $sortfield, $sortorder, 'center '); + $totalarray['nbfield']++; +} // Extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php'; // Hook fields @@ -1103,6 +1123,7 @@ while ($i < $imaxinloop) { $contracttmp->nbofservicesopened = $obj->nb_running; $contracttmp->nbofservicesexpired = $obj->nb_expired; $contracttmp->nbofservicesclosed = $obj->nb_closed; + $contracttmp->signed_status = $obj->signed_status; $socstatic->id = $obj->socid; $socstatic->name = $obj->name; @@ -1296,6 +1317,13 @@ while ($i < $imaxinloop) { if (!empty($arrayfields['c.date_contrat']['checked'])) { print ''.dol_print_date($db->jdate($obj->date_contrat), 'day', 'tzserver').''; } + // Signed Status + if (!empty($arrayfields['c.signed_status']['checked'])) { + print ''.$contracttmp->getLibSignedStatus(5).''; + if (!$i) { + $totalarray['nbfield']++; + } + } // Extra fields include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php'; // Fields from hook diff --git a/htdocs/core/ajax/onlineSign.php b/htdocs/core/ajax/onlineSign.php index 9a5c32eb3ac..c87c4a5d74b 100644 --- a/htdocs/core/ajax/onlineSign.php +++ b/htdocs/core/ajax/onlineSign.php @@ -434,6 +434,8 @@ if ($action == "importSignature") { // Document format not supported to insert online signature. // We should just create an image file with the signature. } + $user = new User($db); + $object->setSignedStatus($user, $object::SIGNED_STATUSES['STATUS_SIGNED_RECEIVER_ONLINE'], 0, 'CONTRACT_MODIFY'); } } elseif ($mode == 'fichinter') { require_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php'; diff --git a/htdocs/core/class/notify.class.php b/htdocs/core/class/notify.class.php index a4d31fb297a..9b0a0b952d9 100644 --- a/htdocs/core/class/notify.class.php +++ b/htdocs/core/class/notify.class.php @@ -149,7 +149,8 @@ class Notify 'EXPENSE_REPORT_APPROVE', 'HOLIDAY_VALIDATE', 'HOLIDAY_APPROVE', - 'ACTION_CREATE' + 'ACTION_CREATE', + 'CONTRACT_MODIFY' ); /** @@ -904,6 +905,13 @@ class Notify $object_type = 'action'; $mesg = $outputlangs->transnoentitiesnoconv("EMailTextActionAdded", $link); break; + case 'CONTRACT_MODIFY': + $link = ''.$newref.''; + $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : ''; + $dir_output = $conf->contract->multidir_output; + $object_type = 'contract'; + $mesg = $outputlangs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info); + break; default: $object_type = $object->element; $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type); @@ -1202,6 +1210,13 @@ class Notify $object_type = 'action'; $mesg = $langs->transnoentitiesnoconv("EMailTextActionAdded", $link); break; + case 'CONTRACT_MODIFY': + $link = ''.$newref.''; + $context_info = array_key_exists('signature', $object->context) ? $object->getLibSignedStatus() : ''; + $dir_output = $conf->contract->multidir_output; + $object_type = 'contrat'; + $mesg = $langs->transnoentitiesnoconv("EMailTextContractModified", $link, $context_info); + break; default: $object_type = $object->element; $dir_output = $conf->$object_type->multidir_output[$object->entity ? $object->entity : $conf->entity]."/".get_exdir(0, 0, 0, 1, $object, $object_type); diff --git a/htdocs/langs/en_US/contracts.lang b/htdocs/langs/en_US/contracts.lang index 7eb3335cd8f..bbf25bb2f0c 100644 --- a/htdocs/langs/en_US/contracts.lang +++ b/htdocs/langs/en_US/contracts.lang @@ -36,10 +36,18 @@ ActivateAllOnContract=Activate all services CloseAContract=Close a contract ConfirmDeleteAContract=Are you sure you want to delete this contract and all its services? ConfirmValidateContract=Are you sure you want to validate this contract under name %s? +ConfirmSignContract=Are you sure you want to set this contract as signed ? +ConfirmUnsignContract=Are you sure you want to set this contract as unsigned ? ConfirmActivateAllOnContract=This will open all services (not yet active). Are you sure you want to open all services? ConfirmCloseContract=This will close all services (expired or not). Are you sure you want to close this contract? ConfirmCloseService=Are you sure you want to close this service with date %s? ValidateAContract=Validate a contract +UnsignContract=Unsign contract +ContractSign=Set Signed +ContractUnsign=Set Unsigned +ContractSignedInDolibarr=Contract signed +ContractSignedOnline=Contract signed online +ContractUnsignedInDolibarr=Contract unsigned ActivateService=Activate service ConfirmActivateService=Are you sure you want to activate this service with date %s? RefContract=Contract reference diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index d711e30bdfc..8b8717414ba 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -70,6 +70,7 @@ Notify_BILL_SUPPLIER_VALIDATE=Vendor invoice validated Notify_BILL_SUPPLIER_PAYED=Vendor invoice paid Notify_BILL_SUPPLIER_SENTBYMAIL=Vendor invoice sent by mail Notify_BILL_SUPPLIER_CANCELED=Vendor invoice canceled +Notify_CONTRACT_MODIFY=Contract modified Notify_CONTRACT_VALIDATE=Contract validated Notify_FICHINTER_VALIDATE=Intervention validated Notify_FICHINTER_MODIFY=Intervention modified @@ -234,6 +235,7 @@ EMailTextExpenseReportApproved=Expense report %s has been approved. EMailTextHolidayValidated=Leave request %s has been validated. EMailTextHolidayApproved=Leave request %s has been approved. EMailTextActionAdded=The action %s has been added to the Agenda. +EMailTextContractModified=The contract %s has been modified. %s ImportedWithSet=Importation data set DolibarrNotification=Automatic notification ResizeDesc=Enter new width OR new height. Ratio will be kept during resizing... From 812ac85b20d736fa36cdd0f039cdc78ce1284716 Mon Sep 17 00:00:00 2001 From: MDW Date: Tue, 10 Sep 2024 03:07:57 +0200 Subject: [PATCH 2/3] Qual: Improvements to actions_massactions.inc.php (#30805) * Qual: Fix phan notices in actions_massactions.inc # Qual: Fix phan notices in actions_massactions.inc This fixes up the notices in htdocs/core/actions_massactions.inc.php Separated from #30733. * Adjust baseline for actions_massactions.inc improvements * Update actions_massactions.inc.php --------- Co-authored-by: Laurent Destailleur --- dev/tools/phan/baseline.txt | 2 +- htdocs/core/actions_massactions.inc.php | 54 ++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/dev/tools/phan/baseline.txt b/dev/tools/phan/baseline.txt index e2c44ac3a66..295fc4eda2e 100644 --- a/dev/tools/phan/baseline.txt +++ b/dev/tools/phan/baseline.txt @@ -349,7 +349,7 @@ return [ 'htdocs/core/actions_extrafields.inc.php' => ['PhanTypeMismatchArgumentProbablyReal'], 'htdocs/core/actions_lineupdown.inc.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanUndeclaredGlobalVariable'], 'htdocs/core/actions_linkedfiles.inc.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredGlobalVariable'], - 'htdocs/core/actions_massactions.inc.php' => ['PhanPluginUnknownObjectMethodCall', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentNullableInternal', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchProperty', 'PhanUndeclaredGlobalVariable', 'PhanUndeclaredProperty'], + 'htdocs/core/actions_massactions.inc.php' => ['PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredProperty'], 'htdocs/core/actions_printing.inc.php' => ['PhanUndeclaredProperty'], 'htdocs/core/actions_sendmails.inc.php' => ['PhanPluginUndeclaredVariableIsset', 'PhanPossiblyUndeclaredGlobalVariable', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredGlobalVariable', 'PhanUndeclaredProperty'], 'htdocs/core/actions_setmoduleoptions.inc.php' => ['PhanPluginUnknownObjectMethodCall', 'PhanPossiblyUndeclaredGlobalVariable'], diff --git a/htdocs/core/actions_massactions.inc.php b/htdocs/core/actions_massactions.inc.php index 4d2d91e93e5..9e0781fb337 100644 --- a/htdocs/core/actions_massactions.inc.php +++ b/htdocs/core/actions_massactions.inc.php @@ -37,6 +37,18 @@ // $diroutputmassaction may be defined // $confirm +' +@phan-var-force ?string $permissiontoread +@phan-var-force ?string $permissiontodelete +@phan-var-force ?string $permissiontoclose +@phan-var-force ?string $permissiontoapprove +@phan-var-force ?int[] $toselect +@phan-var-force ?string $diroutputmassaction +@phan-var-force ?string $objectlabel +@phan-var-force ?string $option +@phan-var-force ?string $deliveryreceipt +'; + // Protection if (empty($objectclass) || empty($uploaddir)) { @@ -48,6 +60,14 @@ if (empty($massaction)) { } $error = 0; +// Note: list of strings for objectclass could be extended to accepted/expected classes +' +@phan-var-force "CommonObject"|"CommandeFournisseur"|"ConferenceOrBoothAttendee"|"Contrat"|"Contact"|"Expedition"|"ExpenseReport"|"Facture"|"FactureFournisseur"|"Fichinter"|"Holiday"|"Partnership"|"Project"|"Propal"|"Societe"|"SupplierProposal" $objectclass +@phan-var-force string $massaction +@phan-var-force string $uploaddir +'; + + // For backward compatibility if (!empty($permtoread) && empty($permissiontoread)) { $permissiontoread = $permtoread; @@ -89,6 +109,7 @@ if (!$error && $massaction == 'confirm_presend') { $contactidtosend = array(); $attachedfilesThirdpartyObj = array(); $oneemailperrecipient = (GETPOSTINT('oneemailperrecipient') ? 1 : 0); + $thirdparty = null; if (!$error) { $objecttmp = new $objectclass($db); @@ -109,21 +130,30 @@ if (!$error && $massaction == 'confirm_presend') { foreach ($toselect as $toselectid) { $objecttmp = new $objectclass($db); // we must create new instance because instance is saved into $listofobjectref array for future use + '@phan-var-force CommonObject $objecttmp'; $result = $objecttmp->fetch($toselectid); if ($result > 0) { $listofobjectid[$toselectid] = $toselectid; + + // Set $tmpobjectid that is ID of parent object (thirdparty or user in most cases) $tmpobjectid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid); if ($objecttmp->element == 'societe') { + '@phan-var-force Societe $objecttmp'; $tmpobjectid = $objecttmp->id; } elseif ($objecttmp->element == 'contact') { + '@phan-var-force Contact $objecttmp'; $tmpobjectid = $objecttmp->id; } elseif ($objecttmp->element == 'expensereport') { + '@phan-var-force ExpenseReport $objecttmp'; $tmpobjectid = $objecttmp->fk_user_author; } elseif ($objecttmp->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') { + '@phan-var-force Partnership $objecttmp'; $tmpobjectid = $objecttmp->fk_member; } elseif ($objecttmp->element == 'holiday') { + '@phan-var-force Holiday $objecttmp'; $tmpobjectid = $objecttmp->fk_user; } elseif ($objecttmp->element == 'conferenceorboothattendee') { + '@phan-var-force ConferenceOrBoothAttendee $objecttmp'; $tmpobjectid = $objecttmp->id; } if (empty($tmpobjectid)) { @@ -131,6 +161,7 @@ if (!$error && $massaction == 'confirm_presend') { } if ($objectclass == 'Facture') { + '@phan-var-force Facture $objecttmp'; $tmparraycontact = array(); $tmparraycontact = $objecttmp->liste_contact(-1, 'external', 0, 'BILLING'); if (is_array($tmparraycontact) && count($tmparraycontact) > 0) { @@ -139,6 +170,7 @@ if (!$error && $massaction == 'confirm_presend') { } } } elseif ($objectclass == 'CommandeFournisseur') { + '@phan-var-force CommandeFournisseur $objecttmp'; $tmparraycontact = array(); $tmparraycontact = $objecttmp->liste_contact(-1, 'external', 0, 'CUSTOMER'); if (is_array($tmparraycontact) && count($tmparraycontact) > 0) { @@ -201,7 +233,7 @@ if (!$error && $massaction == 'confirm_presend') { // Recipients are provided into free text $tmparray[] = trim(GETPOST('sendto', 'alphawithlgt')); } - if (count($receiver) > 0) { + if (count($receiver) > 0 && is_object($thirdparty)) { foreach ($receiver as $key => $val) { // Recipient was provided from combo list if ($val == 'thirdparty') { // Id of third party or user @@ -1136,6 +1168,11 @@ if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == ' // Generate document foreach object according to model linked to object // @todo : propose model selection if (!$error && $massaction == 'generate_doc' && $permissiontoread) { + // Complete with classes that use this massaction + <<<'EOPHAN' +@phan-var-force 'Commande'|'CommandeFournisseur'|'Contrat'|'Expedition'|'ExpenseReport'|'Facture'|'FactureFournisseur'|'Fichinter'|'Project'|'Propal'|'SupplierProposal' $objectclass +EOPHAN; + $db->begin(); $nbok = 0; foreach ($toselect as $toselectid) { @@ -1152,7 +1189,7 @@ if (!$error && $massaction == 'generate_doc' && $permissiontoread) { $newlang = $objecttmp->thirdparty->default_lang; // for proposal, order, invoice, ... } if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($objecttmp->default_lang)) { - $newlang = $objecttmp->default_lang; // for thirdparty + $newlang = $objecttmp->default_lang; // for thirdparty @phan-suppress-current-line PhanUndeclaredProperty } if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && empty($objecttmp->thirdparty)) { //load lang from thirdparty $objecttmp->fetch_thirdparty(); @@ -1264,6 +1301,8 @@ if (!$error && ($action == 'affecttag' && $confirm == 'yes') && $permissiontoadd } if (!$error && ($action == 'updateprice' && $confirm == 'yes') && $permissiontoadd) { + '@phan-var-force Product|ProductCustomerPrice $obj'; + '@phan-var-force Product|ProductCustomerPrice $object'; $nbok = 0; $db->begin(); if (GETPOSTISSET('pricerate')) { @@ -1309,9 +1348,10 @@ if (!$error && ($action == 'updateprice' && $confirm == 'yes') && $permissiontoa } if (!$error && ($action == 'setsupervisor' && $confirm == 'yes') && $permissiontoadd) { + '@phan-var-force User $object'; $nbok = 0; $db->begin(); - $supervisortoset = GETPOST('supervisortoset'); + $supervisortoset = GETPOSTINT('supervisortoset'); if (!empty($supervisortoset)) { foreach ($toselect as $toselectid) { $result = $object->fetch($toselectid); @@ -1359,7 +1399,7 @@ if (!$error && ($action == 'affectuser' && $confirm == 'yes') && $permissiontoad $res = $object->add_contact($usertoaffect, $projectrole, 'internal'); if ($res >= 0) { $taskstatic = new Task($db); - $task_array = $taskstatic->getTasksArray(0, 0, $object->id, 0, 0); + $task_array = $taskstatic->getTasksArray(null, null, $object->id, 0, 0); foreach ($task_array as $task) { $tasksToAffect = new Task($db); @@ -1597,6 +1637,7 @@ if (!$error && ($massaction == 'approveleave' || ($action == 'approveleave' && $ $db->begin(); $objecttmp = new $objectclass($db); + '@phan-var-force Holiday $objecttmp'; $nbok = 0; foreach ($toselect as $toselectid) { $result = $objecttmp->fetch($toselectid); @@ -1606,7 +1647,7 @@ if (!$error && ($massaction == 'approveleave' || ($action == 'approveleave' && $ continue; } if ($user->id == $objecttmp->fk_validator) { - $objecttmp->oldcopy = dol_clone($objecttmp, 2); + $objecttmp->oldcopy = dol_clone($objecttmp, 2); // @phan-suppress-current-line PhanTypeMismatchProperty $objecttmp->date_valid = dol_now(); $objecttmp->fk_user_valid = $user->id; @@ -1713,7 +1754,8 @@ if (!$error && ($massaction == 'approveleave' || ($action == 'approveleave' && $ } } -if (!$error && ($massaction == 'increaseholiday' || ($action == 'increaseholiday' && $confirm == 'yes')) && $permissiontoapprove) { +if (!$error && ($massaction == 'increaseholiday' || ($action == 'increaseholiday' && $confirm == 'yes')) && $permissiontoapprove && is_array($toselect)) { + '@phan-var-force Holiday $holiday'; // Supposing that $holiday is set, it is needed. $db->begin(); $objecttmp = new $objectclass($db); $nbok = 0; From f9a0d409a67e8cd7776fa90e3c03de8224a6c614 Mon Sep 17 00:00:00 2001 From: William Mead Date: Tue, 10 Sep 2024 03:30:57 +0200 Subject: [PATCH 3/3] Qual isolate commande fournisseur line class to separate file (#30898) * Moved supplier order line class to separate file. Removed unnecessary requires. * Updated PHPDoc --------- Co-authored-by: Laurent Destailleur --- .../class/fournisseur.commande.class.php | 501 +--------------- .../fournisseur.commande.ligne.class.php | 567 ++++++++++++++++++ 2 files changed, 568 insertions(+), 500 deletions(-) create mode 100644 htdocs/fourn/class/fournisseur.commande.ligne.class.php diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index 2b5cad6259d..02f82a17ccf 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -39,6 +39,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; +require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.ligne.class.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; if (isModEnabled('productbatch')) { require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; @@ -3727,503 +3728,3 @@ class CommandeFournisseur extends CommonOrder return $return; } } - - - -/** - * Class to manage line orders - */ -class CommandeFournisseurLigne extends CommonOrderLine -{ - /** - * @var string ID to identify managed object - */ - public $element = 'commande_fournisseurdet'; - - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element = 'commande_fournisseurdet'; - - /** - * @see CommonObjectLine - */ - public $parent_element = 'commande_fournisseur'; - - /** - * @see CommonObjectLine - */ - public $fk_parent_attribute = 'fk_commande_fournisseur'; - - public $oldline; - - /** - * Id of parent order - * @var int - */ - public $fk_commande; - - // From llx_commande_fournisseurdet - /** - * @var int ID - */ - public $fk_parent_line; - - /** - * @var int ID - */ - public $fk_facture; - - public $rang = 0; - - /** - * @var int special code - */ - public $special_code = 0; - - /** - * Unit price without taxes - * @var float - */ - public $pu_ht; - - public $date_start; - public $date_end; - public $fk_fournprice; - public $packaging; - public $pa_ht; - - // From llx_product_fournisseur_price - - /** - * Supplier reference of price when we added the line. May have been changed after line was added. - * @var string - */ - public $ref_supplier; - - /** - * @var string ref supplier - * @deprecated - * @see $ref_supplier - */ - public $ref_fourn; - - public $remise; - - - /** - * Constructor - * - * @param DoliDB $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - - /** - * Load line order - * - * @param int $rowid Id line order - * @return int Return integer <0 if KO, >0 if OK - */ - public function fetch($rowid) - { - $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,'; - $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,'; - $sql .= ' cd.remise, cd.remise_percent, cd.subprice,'; - $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,'; - $sql .= ' cd.total_localtax1, cd.total_localtax2,'; - $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,'; - $sql .= ' cd.date_start, cd.date_end, cd.fk_unit,'; - $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,'; - $sql .= ' c.fk_soc as socid'; - $sql .= ' FROM '.$this->db->prefix().'commande_fournisseur as c, '.$this->db->prefix().'commande_fournisseurdet as cd'; - $sql .= ' LEFT JOIN '.$this->db->prefix().'product as p ON cd.fk_product = p.rowid'; - $sql .= ' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid); - - $result = $this->db->query($sql); - if ($result) { - $objp = $this->db->fetch_object($result); - - if (!empty($objp)) { - $this->rowid = $objp->rowid; - $this->id = $objp->rowid; - $this->fk_commande = $objp->fk_commande; - $this->desc = $objp->description; - $this->qty = $objp->qty; - $this->ref_fourn = $objp->ref_supplier; - $this->ref_supplier = $objp->ref_supplier; - $this->subprice = $objp->subprice; - $this->tva_tx = $objp->tva_tx; - $this->localtax1_tx = $objp->localtax1_tx; - $this->localtax2_tx = $objp->localtax2_tx; - $this->localtax1_type = $objp->localtax1_type; - $this->localtax2_type = $objp->localtax2_type; - $this->remise = $objp->remise; - $this->remise_percent = $objp->remise_percent; - $this->fk_product = $objp->fk_product; - $this->info_bits = $objp->info_bits; - $this->total_ht = $objp->total_ht; - $this->total_tva = $objp->total_tva; - $this->total_localtax1 = $objp->total_localtax1; - $this->total_localtax2 = $objp->total_localtax2; - $this->total_ttc = $objp->total_ttc; - $this->product_type = $objp->product_type; - $this->special_code = $objp->special_code; - - $this->ref = $objp->product_ref; - - $this->product_ref = $objp->product_ref; - $this->product_label = $objp->product_label; - $this->product_desc = $objp->product_desc; - - if (getDolGlobalInt('PRODUCT_USE_SUPPLIER_PACKAGING')) { - // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line. - // Move this into another method and call it when required. - - // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty) - $sqlsearchpackage = 'SELECT rowid, packaging FROM '.$this->db->prefix()."product_fournisseur_price"; - $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")"; - $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product); - $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'"; - $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified - $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified - $sqlsearchpackage .= " AND fk_soc = ".((int) $objp->socid); - $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first - $sqlsearchpackage .= " LIMIT 1"; - - $resqlsearchpackage = $this->db->query($sqlsearchpackage); - if ($resqlsearchpackage) { - $objsearchpackage = $this->db->fetch_object($resqlsearchpackage); - if ($objsearchpackage) { - $this->fk_fournprice = $objsearchpackage->rowid; - $this->packaging = $objsearchpackage->packaging; - } - } else { - $this->error = $this->db->lasterror(); - return -1; - } - } - - $this->date_start = $this->db->jdate($objp->date_start); - $this->date_end = $this->db->jdate($objp->date_end); - $this->fk_unit = $objp->fk_unit; - - $this->multicurrency_subprice = $objp->multicurrency_subprice; - $this->multicurrency_total_ht = $objp->multicurrency_total_ht; - $this->multicurrency_total_tva = $objp->multicurrency_total_tva; - $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; - - $this->fetch_optionals(); - - $this->db->free($result); - return 1; - } else { - $this->error = 'Supplier order line with id='.$rowid.' not found'; - dol_syslog(get_class($this)."::fetch Error ".$this->error, LOG_ERR); - return 0; - } - } else { - dol_print_error($this->db); - return -1; - } - } - - /** - * Insert line into database - * - * @param int $notrigger 1 = disable triggers - * @return int Return integer <0 if KO, >0 if OK - */ - public function insert($notrigger = 0) - { - global $conf, $user; - - $error = 0; - - dol_syslog(get_class($this)."::insert rang=".$this->rang); - - // Clean parameters - if (empty($this->tva_tx)) { - $this->tva_tx = 0; - } - if (empty($this->localtax1_tx)) { - $this->localtax1_tx = 0; - } - if (empty($this->localtax2_tx)) { - $this->localtax2_tx = 0; - } - if (empty($this->localtax1_type)) { - $this->localtax1_type = '0'; - } - if (empty($this->localtax2_type)) { - $this->localtax2_type = '0'; - } - if (empty($this->total_localtax1)) { - $this->total_localtax1 = 0; - } - if (empty($this->total_localtax2)) { - $this->total_localtax2 = 0; - } - if (empty($this->rang)) { - $this->rang = 0; - } - if (empty($this->remise_percent)) { - $this->remise_percent = 0; - } - if (empty($this->info_bits)) { - $this->info_bits = 0; - } - if (empty($this->special_code)) { - $this->special_code = 0; - } - if (empty($this->fk_parent_line)) { - $this->fk_parent_line = 0; - } - if (empty($this->pa_ht)) { - $this->pa_ht = 0; - } - - // Multicurrency - if (!empty($this->multicurrency_code)) { - list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code); - } - if (empty($this->fk_multicurrency)) { - $this->multicurrency_code = $conf->currency; - $this->fk_multicurrency = 0; - $this->multicurrency_tx = 1; - } - - // Check parameters - if ($this->product_type < 0) { - return -1; - } - - $this->db->begin(); - - // Insertion dans base de la ligne - $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element; - $sql .= " (fk_commande, label, description, date_start, date_end,"; - $sql .= " fk_product, product_type, special_code, rang,"; - $sql .= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,"; - $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,"; - $sql .= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,"; - $sql .= " fk_parent_line)"; - $sql .= " VALUES (".$this->fk_commande.", '".$this->db->escape($this->label)."','".$this->db->escape($this->desc)."',"; - $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").","; - $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").","; - if ($this->fk_product) { - $sql .= $this->fk_product.","; - } else { - $sql .= "null,"; - } - $sql .= "'".$this->db->escape($this->product_type)."',"; - $sql .= (int) $this->special_code . ","; - $sql .= "'".$this->db->escape($this->rang)."',"; - $sql .= "'".$this->db->escape($this->qty)."', "; - $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").","; - $sql .= " ".price2num($this->tva_tx).", "; - $sql .= " ".price2num($this->localtax1_tx).","; - $sql .= " ".price2num($this->localtax2_tx).","; - $sql .= " '".$this->db->escape($this->localtax1_type)."',"; - $sql .= " '".$this->db->escape($this->localtax2_type)."',"; - $sql .= " ".((float) $this->remise_percent).", ".price2num($this->subprice, 'MU').", '".$this->db->escape($this->ref_supplier)."',"; - $sql .= " ".price2num($this->total_ht).","; - $sql .= " ".price2num($this->total_tva).","; - $sql .= " ".price2num($this->total_localtax1).","; - $sql .= " ".price2num($this->total_localtax2).","; - $sql .= " ".price2num($this->total_ttc).","; - $sql .= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null"); - $sql .= ", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) : "null"); - $sql .= ", '".$this->db->escape($this->multicurrency_code)."'"; - $sql .= ", ".($this->multicurrency_subprice ? price2num($this->multicurrency_subprice) : '0'); - $sql .= ", ".($this->multicurrency_total_ht ? price2num($this->multicurrency_total_ht) : '0'); - $sql .= ", ".($this->multicurrency_total_tva ? price2num($this->multicurrency_total_tva) : '0'); - $sql .= ", ".($this->multicurrency_total_ttc ? price2num($this->multicurrency_total_ttc) : '0'); - $sql .= ", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line : 'null'); - $sql .= ")"; - - dol_syslog(get_class($this)."::insert", LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) { - $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element); - $this->rowid = $this->id; - - if (!$error) { - $result = $this->insertExtraFields(); - if ($result < 0) { - $error++; - } - } - - if (!$error && !$notrigger) { - // Call trigger - $result = $this->call_trigger('LINEORDER_SUPPLIER_CREATE', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { - $this->db->commit(); - return 1; - } - - foreach ($this->errors as $errmsg) { - dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); - $this->errors[] = ($this->errors ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->errors[] = $this->db->error(); - $this->db->rollback(); - return -2; - } - } - /** - * Update the line object into db - * - * @param int $notrigger 1 = disable triggers - * @return int Return integer <0 si ko, >0 si ok - */ - public function update($notrigger = 0) - { - global $user; - - $error = 0; - - $this->db->begin(); - - $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET"; - $sql .= " description='".$this->db->escape($this->desc)."'"; - $sql .= ", ref='".$this->db->escape($this->ref_supplier)."'"; - $sql .= ", subprice='".price2num($this->subprice)."'"; - //$sql.= ",remise='".price2num($remise)."'"; - $sql .= ", remise_percent='".price2num($this->remise_percent)."'"; - - $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'"; - $sql .= ", tva_tx='".price2num($this->tva_tx)."'"; - $sql .= ", localtax1_tx='".price2num($this->localtax1_tx)."'"; - $sql .= ", localtax2_tx='".price2num($this->localtax2_tx)."'"; - $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'"; - $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'"; - $sql .= ", qty='".price2num($this->qty)."'"; - $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null"); - $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null"); - $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'"; - $sql .= ", total_ht='".price2num($this->total_ht)."'"; - $sql .= ", total_tva='".price2num($this->total_tva)."'"; - $sql .= ", total_localtax1='".price2num($this->total_localtax1)."'"; - $sql .= ", total_localtax2='".price2num($this->total_localtax2)."'"; - $sql .= ", total_ttc='".price2num($this->total_ttc)."'"; - $sql .= ", product_type=".$this->product_type; - $sql .= ", special_code=".(!empty($this->special_code) ? $this->special_code : 0); - $sql .= ($this->fk_unit ? ", fk_unit='".$this->db->escape($this->fk_unit)."'" : ", fk_unit=null"); - - // Multicurrency - $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice); - $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht); - $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva); - $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc); - - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(get_class($this)."::updateline", LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) { - if (!$error) { - $result = $this->insertExtraFields(); - if ($result < 0) { - $error++; - } - } - - if (!$error && !$notrigger) { - // Call trigger - $result = $this->call_trigger('LINEORDER_SUPPLIER_MODIFY', $user); - if ($result < 0) { - $this->db->rollback(); - return -1; - } - // End call triggers - } - - if (!$error) { - $this->db->commit(); - return 1; - } else { - $this->db->rollback(); - return -1; - } - } else { - $this->error = $this->db->lasterror(); - $this->db->rollback(); - return -1; - } - } - - /** - * Delete line in database - * - * @param User $user User making the change - * @param int $notrigger 1=Disable call to triggers - * @return int Return integer <0 if KO, >0 if OK - */ - public function delete($user, $notrigger = 0) - { - if (empty($user)) { - global $user; - } - - $error = 0; - - $this->db->begin(); - - // extrafields - $result = $this->deleteExtraFields(); - if ($result < 0) { - $this->db->rollback(); - return -1; - } - - $sql1 = 'UPDATE '.$this->db->prefix()."commandedet SET fk_commandefourndet = NULL WHERE rowid=".((int) $this->id); - $resql = $this->db->query($sql1); - if (!$resql) { - $this->db->rollback(); - return -1; - } - - $sql2 = 'DELETE FROM '.$this->db->prefix()."commande_fournisseurdet WHERE rowid=".((int) $this->id); - - dol_syslog(__METHOD__, LOG_DEBUG); - $resql = $this->db->query($sql2); - if ($resql) { - if (!$notrigger) { - // Call trigger - $result = $this->call_trigger('LINEORDER_SUPPLIER_DELETE', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { - $this->db->commit(); - return 1; - } - - foreach ($this->errors as $errmsg) { - dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); - $this->error .= ($this->error ? ', '.$errmsg : $errmsg); - } - $this->db->rollback(); - return -1 * $error; - } else { - $this->error = $this->db->lasterror(); - return -1; - } - } -} diff --git a/htdocs/fourn/class/fournisseur.commande.ligne.class.php b/htdocs/fourn/class/fournisseur.commande.ligne.class.php new file mode 100644 index 00000000000..95f8e0597df --- /dev/null +++ b/htdocs/fourn/class/fournisseur.commande.ligne.class.php @@ -0,0 +1,567 @@ + + * Copyright (C) 2004-2017 Laurent Destailleur + * Copyright (C) 2005-2012 Regis Houssin + * Copyright (C) 2007 Franky Van Liedekerke + * Copyright (C) 2010-2020 Juanjo Menent + * Copyright (C) 2010-2018 Philippe Grand + * Copyright (C) 2012-2015 Marcos García + * Copyright (C) 2013 Florian Henry + * Copyright (C) 2013 Cédric Salvador + * Copyright (C) 2018 Nicolas ZABOURI + * Copyright (C) 2018-2024 Frédéric France + * Copyright (C) 2018-2022 Ferran Marcet + * Copyright (C) 2021 Josep Lluís Amador + * Copyright (C) 2022 Gauthier VERDOL + * Copyright (C) 2024 Solution Libre SAS + * Copyright (C) 2024 MDW + * Copyright (C) 2024 William Mead + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/fourn/class/fournisseur.commande.ligne.class.php + * \ingroup fournisseur,commande + * \brief File of class to manage supplier order lines + */ + +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php'; +require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; + +/** + * Class to manage line orders + */ +class CommandeFournisseurLigne extends CommonOrderLine +{ + /** + * @var string ID to identify managed object + */ + public $element = 'commande_fournisseurdet'; + + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = 'commande_fournisseurdet'; + + /** + * @see CommonObjectLine + */ + public $parent_element = 'commande_fournisseur'; + + /** + * @see CommonObjectLine + */ + public $fk_parent_attribute = 'fk_commande_fournisseur'; + + /** + * @var CommandeFournisseurLigne + */ + public $oldline; + + /** + * Id of parent order + * @var int + */ + public $fk_commande; + + // From llx_commande_fournisseurdet + /** + * @var int ID + */ + public $fk_parent_line; + + /** + * @var int ID + */ + public $fk_facture; + + /** + * @var int rank + */ + public $rang = 0; + + /** + * @var int special code + */ + public $special_code = 0; + + /** + * Unit price without taxes + * @var float + */ + public $pu_ht; + + /** + * @var int|string|null + */ + public $date_start; + + /** + * @var int|string|null + */ + public $date_end; + + /** + * @var int + */ + public $fk_fournprice; + + /** + * @var float + */ + public $packaging; + + /** + * @var int + */ + public $pa_ht; + + // From llx_product_fournisseur_price + + /** + * Supplier reference of price when we added the line. May have been changed after line was added. + * @var string + */ + public $ref_supplier; + + /** + * @var string ref supplier + * @deprecated + * @see $ref_supplier + */ + public $ref_fourn; + + /** + * @var float|string + */ + public $remise; + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Load line order + * + * @param int $rowid Id line order + * @return int Return integer <0 if KO, >0 if OK + */ + public function fetch($rowid) + { + $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,'; + $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,'; + $sql .= ' cd.remise, cd.remise_percent, cd.subprice,'; + $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,'; + $sql .= ' cd.total_localtax1, cd.total_localtax2,'; + $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,'; + $sql .= ' cd.date_start, cd.date_end, cd.fk_unit,'; + $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,'; + $sql .= ' c.fk_soc as socid'; + $sql .= ' FROM '.$this->db->prefix().'commande_fournisseur as c, '.$this->db->prefix().'commande_fournisseurdet as cd'; + $sql .= ' LEFT JOIN '.$this->db->prefix().'product as p ON cd.fk_product = p.rowid'; + $sql .= ' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid); + + $result = $this->db->query($sql); + if ($result) { + $objp = $this->db->fetch_object($result); + + if (!empty($objp)) { + $this->rowid = $objp->rowid; + $this->id = $objp->rowid; + $this->fk_commande = $objp->fk_commande; + $this->desc = $objp->description; + $this->qty = $objp->qty; + $this->ref_fourn = $objp->ref_supplier; + $this->ref_supplier = $objp->ref_supplier; + $this->subprice = $objp->subprice; + $this->tva_tx = $objp->tva_tx; + $this->localtax1_tx = $objp->localtax1_tx; + $this->localtax2_tx = $objp->localtax2_tx; + $this->localtax1_type = $objp->localtax1_type; + $this->localtax2_type = $objp->localtax2_type; + $this->remise = $objp->remise; + $this->remise_percent = $objp->remise_percent; + $this->fk_product = $objp->fk_product; + $this->info_bits = $objp->info_bits; + $this->total_ht = $objp->total_ht; + $this->total_tva = $objp->total_tva; + $this->total_localtax1 = $objp->total_localtax1; + $this->total_localtax2 = $objp->total_localtax2; + $this->total_ttc = $objp->total_ttc; + $this->product_type = $objp->product_type; + $this->special_code = $objp->special_code; + + $this->ref = $objp->product_ref; + + $this->product_ref = $objp->product_ref; + $this->product_label = $objp->product_label; + $this->product_desc = $objp->product_desc; + + if (getDolGlobalInt('PRODUCT_USE_SUPPLIER_PACKAGING')) { + // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line. + // Move this into another method and call it when required. + + // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty) + $sqlsearchpackage = 'SELECT rowid, packaging FROM '.$this->db->prefix()."product_fournisseur_price"; + $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")"; + $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product); + $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'"; + $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified + $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified + $sqlsearchpackage .= " AND fk_soc = ".((int) $objp->socid); + $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first + $sqlsearchpackage .= " LIMIT 1"; + + $resqlsearchpackage = $this->db->query($sqlsearchpackage); + if ($resqlsearchpackage) { + $objsearchpackage = $this->db->fetch_object($resqlsearchpackage); + if ($objsearchpackage) { + $this->fk_fournprice = $objsearchpackage->rowid; + $this->packaging = $objsearchpackage->packaging; + } + } else { + $this->error = $this->db->lasterror(); + return -1; + } + } + + $this->date_start = $this->db->jdate($objp->date_start); + $this->date_end = $this->db->jdate($objp->date_end); + $this->fk_unit = $objp->fk_unit; + + $this->multicurrency_subprice = $objp->multicurrency_subprice; + $this->multicurrency_total_ht = $objp->multicurrency_total_ht; + $this->multicurrency_total_tva = $objp->multicurrency_total_tva; + $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc; + + $this->fetch_optionals(); + + $this->db->free($result); + return 1; + } else { + $this->error = 'Supplier order line with id='.$rowid.' not found'; + dol_syslog(get_class($this)."::fetch Error ".$this->error, LOG_ERR); + return 0; + } + } else { + dol_print_error($this->db); + return -1; + } + } + + /** + * Insert line into database + * + * @param int $notrigger 1 = disable triggers + * @return int Return integer <0 if KO, >0 if OK + */ + public function insert($notrigger = 0) + { + global $conf, $user; + + $error = 0; + + dol_syslog(get_class($this)."::insert rang=".$this->rang); + + // Clean parameters + if (empty($this->tva_tx)) { + $this->tva_tx = 0; + } + if (empty($this->localtax1_tx)) { + $this->localtax1_tx = 0; + } + if (empty($this->localtax2_tx)) { + $this->localtax2_tx = 0; + } + if (empty($this->localtax1_type)) { + $this->localtax1_type = '0'; + } + if (empty($this->localtax2_type)) { + $this->localtax2_type = '0'; + } + if (empty($this->total_localtax1)) { + $this->total_localtax1 = 0; + } + if (empty($this->total_localtax2)) { + $this->total_localtax2 = 0; + } + if (empty($this->rang)) { + $this->rang = 0; + } + if (empty($this->remise_percent)) { + $this->remise_percent = 0; + } + if (empty($this->info_bits)) { + $this->info_bits = 0; + } + if (empty($this->special_code)) { + $this->special_code = 0; + } + if (empty($this->fk_parent_line)) { + $this->fk_parent_line = 0; + } + if (empty($this->pa_ht)) { + $this->pa_ht = 0; + } + + // Multicurrency + if (!empty($this->multicurrency_code)) { + list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code); + } + if (empty($this->fk_multicurrency)) { + $this->multicurrency_code = $conf->currency; + $this->fk_multicurrency = 0; + $this->multicurrency_tx = 1; + } + + // Check parameters + if ($this->product_type < 0) { + return -1; + } + + $this->db->begin(); + + // Insertion dans base de la ligne + $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element; + $sql .= " (fk_commande, label, description, date_start, date_end,"; + $sql .= " fk_product, product_type, special_code, rang,"; + $sql .= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,"; + $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,"; + $sql .= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,"; + $sql .= " fk_parent_line)"; + $sql .= " VALUES (".$this->fk_commande.", '".$this->db->escape($this->label)."','".$this->db->escape($this->desc)."',"; + $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").","; + $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").","; + if ($this->fk_product) { + $sql .= $this->fk_product.","; + } else { + $sql .= "null,"; + } + $sql .= "'".$this->db->escape($this->product_type)."',"; + $sql .= (int) $this->special_code . ","; + $sql .= "'".$this->db->escape($this->rang)."',"; + $sql .= "'".$this->db->escape($this->qty)."', "; + $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").","; + $sql .= " ".price2num($this->tva_tx).", "; + $sql .= " ".price2num($this->localtax1_tx).","; + $sql .= " ".price2num($this->localtax2_tx).","; + $sql .= " '".$this->db->escape($this->localtax1_type)."',"; + $sql .= " '".$this->db->escape($this->localtax2_type)."',"; + $sql .= " ".((float) $this->remise_percent).", ".price2num($this->subprice, 'MU').", '".$this->db->escape($this->ref_supplier)."',"; + $sql .= " ".price2num($this->total_ht).","; + $sql .= " ".price2num($this->total_tva).","; + $sql .= " ".price2num($this->total_localtax1).","; + $sql .= " ".price2num($this->total_localtax2).","; + $sql .= " ".price2num($this->total_ttc).","; + $sql .= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null"); + $sql .= ", ".($this->fk_multicurrency ? ((int) $this->fk_multicurrency) : "null"); + $sql .= ", '".$this->db->escape($this->multicurrency_code)."'"; + $sql .= ", ".($this->multicurrency_subprice ? price2num($this->multicurrency_subprice) : '0'); + $sql .= ", ".($this->multicurrency_total_ht ? price2num($this->multicurrency_total_ht) : '0'); + $sql .= ", ".($this->multicurrency_total_tva ? price2num($this->multicurrency_total_tva) : '0'); + $sql .= ", ".($this->multicurrency_total_ttc ? price2num($this->multicurrency_total_ttc) : '0'); + $sql .= ", ".((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line : 'null'); + $sql .= ")"; + + dol_syslog(get_class($this)."::insert", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element); + $this->rowid = $this->id; + + if (!$error) { + $result = $this->insertExtraFields(); + if ($result < 0) { + $error++; + } + } + + if (!$error && !$notrigger) { + // Call trigger + $result = $this->call_trigger('LINEORDER_SUPPLIER_CREATE', $user); + if ($result < 0) { + $error++; + } + // End call triggers + } + + if (!$error) { + $this->db->commit(); + return 1; + } + + foreach ($this->errors as $errmsg) { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->errors[] = ($this->errors ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } else { + $this->errors[] = $this->db->error(); + $this->db->rollback(); + return -2; + } + } + /** + * Update the line object into db + * + * @param int $notrigger 1 = disable triggers + * @return int Return integer <0 si ko, >0 si ok + */ + public function update($notrigger = 0) + { + global $user; + + $error = 0; + + $this->db->begin(); + + $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET"; + $sql .= " description='".$this->db->escape($this->desc)."'"; + $sql .= ", ref='".$this->db->escape($this->ref_supplier)."'"; + $sql .= ", subprice='".price2num($this->subprice)."'"; + //$sql.= ",remise='".price2num($remise)."'"; + $sql .= ", remise_percent='".price2num($this->remise_percent)."'"; + + $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'"; + $sql .= ", tva_tx='".price2num($this->tva_tx)."'"; + $sql .= ", localtax1_tx='".price2num($this->localtax1_tx)."'"; + $sql .= ", localtax2_tx='".price2num($this->localtax2_tx)."'"; + $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'"; + $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'"; + $sql .= ", qty='".price2num($this->qty)."'"; + $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null"); + $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null"); + $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'"; + $sql .= ", total_ht='".price2num($this->total_ht)."'"; + $sql .= ", total_tva='".price2num($this->total_tva)."'"; + $sql .= ", total_localtax1='".price2num($this->total_localtax1)."'"; + $sql .= ", total_localtax2='".price2num($this->total_localtax2)."'"; + $sql .= ", total_ttc='".price2num($this->total_ttc)."'"; + $sql .= ", product_type=".$this->product_type; + $sql .= ", special_code=".(!empty($this->special_code) ? $this->special_code : 0); + $sql .= ($this->fk_unit ? ", fk_unit='".$this->db->escape($this->fk_unit)."'" : ", fk_unit=null"); + + // Multicurrency + $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice); + $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht); + $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva); + $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc); + + $sql .= " WHERE rowid = ".((int) $this->id); + + dol_syslog(get_class($this)."::updateline", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + if (!$error) { + $result = $this->insertExtraFields(); + if ($result < 0) { + $error++; + } + } + + if (!$error && !$notrigger) { + // Call trigger + $result = $this->call_trigger('LINEORDER_SUPPLIER_MODIFY', $user); + if ($result < 0) { + $this->db->rollback(); + return -1; + } + // End call triggers + } + + if (!$error) { + $this->db->commit(); + return 1; + } else { + $this->db->rollback(); + return -1; + } + } else { + $this->error = $this->db->lasterror(); + $this->db->rollback(); + return -1; + } + } + + /** + * Delete line in database + * + * @param User $user User making the change + * @param int $notrigger 1=Disable call to triggers + * @return int Return integer <0 if KO, >0 if OK + */ + public function delete($user, $notrigger = 0) + { + if (empty($user)) { + global $user; + } + + $error = 0; + + $this->db->begin(); + + // extrafields + $result = $this->deleteExtraFields(); + if ($result < 0) { + $this->db->rollback(); + return -1; + } + + $sql1 = 'UPDATE '.$this->db->prefix()."commandedet SET fk_commandefourndet = NULL WHERE rowid=".((int) $this->id); + $resql = $this->db->query($sql1); + if (!$resql) { + $this->db->rollback(); + return -1; + } + + $sql2 = 'DELETE FROM '.$this->db->prefix()."commande_fournisseurdet WHERE rowid=".((int) $this->id); + + dol_syslog(__METHOD__, LOG_DEBUG); + $resql = $this->db->query($sql2); + if ($resql) { + if (!$notrigger) { + // Call trigger + $result = $this->call_trigger('LINEORDER_SUPPLIER_DELETE', $user); + if ($result < 0) { + $error++; + } + // End call triggers + } + + if (!$error) { + $this->db->commit(); + return 1; + } + + foreach ($this->errors as $errmsg) { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } + $this->db->rollback(); + return -1 * $error; + } else { + $this->error = $this->db->lasterror(); + return -1; + } + } +}