FIX The status of customer was not synchronized with opportunity status

This commit is contained in:
Laurent Destailleur 2025-10-13 15:00:26 +02:00
parent 41634d08ca
commit 9f6f781e01

View file

@ -188,7 +188,7 @@ if (empty($reshook)) {
$action = '';
// For backward compatibility
$object->statut = $object::STATUS_DRAFT; // this already set for $object->status by $object->setStatut()
$object->statut = $object::STATUS_DRAFT; // this is already set for $object->status by $object->setStatut()
}
// Action add
@ -358,11 +358,24 @@ if (empty($reshook)) {
}
}
if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
// If opportunities are used and the customer is not yet a customer
if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES') && $object->usage_opportunity) {
if ($object->opp_amount && ($object->opp_status <= 0)) {
$error++;
setEventMessages($langs->trans("ErrorOppStatusRequiredIfAmount"), null, 'errors');
}
if (!$error) {
if ((int) $object->thirdparty->client == 0 || (int) $object->thirdparty->client == 2) { // If not yet customer
// Get ID of the special opportunity status code 'WON'
$idoppstatuswon = (int) dol_getIdFromCode($db, 'WON', 'c_lead_status', 'code', 'rowid');
if (!$error && $object->opp_status == $idoppstatuswon) {
// Switch the thirdparty into a customer
$object->thirdparty->setAsCustomer();
}
}
}
}
if (!$error) {
@ -395,7 +408,7 @@ if (empty($reshook)) {
}
}
// Check if we must change status
// Check if we must change status of project
if (GETPOST('closeproject')) {
$resclose = $object->setClose($user);
if ($resclose < 0) {
@ -419,6 +432,7 @@ if (empty($reshook)) {
}
}
// Set opportunity status
if ($action == 'set_opp_status' && $user->hasRight('projet', 'creer')) {
if (GETPOSTISSET('opp_status')) {
$object->opp_status = $opp_status;
@ -435,6 +449,8 @@ if (empty($reshook)) {
}
if (!$error) {
$db->begin();
$result = $object->update($user);
if ($result < 0) {
$error++;
@ -444,13 +460,28 @@ if (empty($reshook)) {
setEventMessages($object->error, $object->errors, 'errors');
}
}
}
if ($error) {
$db->rollback();
$action = 'edit';
// If opportunities are used and the customer is not yet a customer
if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES') && $object->usage_opportunity) {
if ((int) $object->thirdparty->client == 0 || (int) $object->thirdparty->client == 2) { // If not yet customer
// Get ID of the special opportunity status code 'WON'
$idoppstatuswon = (int) dol_getIdFromCode($db, 'WON', 'c_lead_status', 'code', 'rowid');
if (!$error && $object->opp_status == $idoppstatuswon) {
// Switch the thirdparty into a customer
$object->thirdparty->setAsCustomer();
}
}
}
if ($error) {
$db->rollback();
$action = 'edit';
} else {
$db->commit();
}
} else {
$db->commit();
$action = 'edit';
}
}
@ -1246,8 +1277,8 @@ if ($action == 'create' && $user->hasRight('projet', 'creer')) {
print '</div>';
print '<div id="divtocloseproject" class="inline-block valign clearboth paddingtop" style="display: none;">';
print '<input type="checkbox" id="inputcloseproject" name="closeproject" />';
print '<label for="inputcloseproject">';
print '<input type="checkbox" id="inputcloseproject" name="closeproject" class="valignmiddle" />';
print '<label for="inputcloseproject" class="opacitymedium valignmiddle">';
print $form->textwithpicto($langs->trans("AlsoCloseAProject"), $langs->trans("AlsoCloseAProjectTooltip")).'</label>';
print ' </div>';