From 9f6f781e019f039b7786093c66e8a005f01e4fde Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 13 Oct 2025 15:00:26 +0200 Subject: [PATCH] FIX The status of customer was not synchronized with opportunity status --- htdocs/projet/card.php | 51 +++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/htdocs/projet/card.php b/htdocs/projet/card.php index aa369255f6e..db992712f6e 100644 --- a/htdocs/projet/card.php +++ b/htdocs/projet/card.php @@ -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 ''; print '';