105 lines
4.3 KiB
PHP
105 lines
4.3 KiB
PHP
<?php
|
|
namespace models;
|
|
|
|
// Inclure FormValidator avant de l'utiliser
|
|
if (!class_exists('\libraries\FormValidator')) {
|
|
$formValidatorPath = WP_PLUGIN_DIR . '/ESI_creditDirect/app/libraries/FormValidator.php';
|
|
if (file_exists($formValidatorPath)) {
|
|
require_once $formValidatorPath;
|
|
}
|
|
}
|
|
|
|
use libraries\FormValidator;
|
|
|
|
class CRED_credit_step3 extends CRED_credit {
|
|
|
|
protected $currentCredit;
|
|
|
|
public function save_step_2($data, $currentCredit) {
|
|
|
|
// Validation des données
|
|
$validator = new FormValidator($data);
|
|
$errors = $validator->validateStep3($data);
|
|
|
|
if (!empty($errors)) {
|
|
// Retourner les erreurs pour affichage
|
|
return [
|
|
'success' => false,
|
|
'errors' => $errors,
|
|
'formatted_errors' => $validator->getFormattedErrors()
|
|
];
|
|
}
|
|
|
|
$this->currentCredit = $currentCredit;
|
|
|
|
foreach ($data as $k => $d) {
|
|
if ($d === '') {
|
|
$data[$k] = null;
|
|
}
|
|
}
|
|
|
|
$curreentUser = wp_get_current_user();
|
|
|
|
if($curreentUser->ID == 1) {
|
|
|
|
echo '<pre>';
|
|
echo 'data';
|
|
print_r($data);
|
|
echo '</pre>';
|
|
|
|
}
|
|
|
|
$this->update_emprunteur($data, $currentCredit);
|
|
$this->save_autre_credit_emprunteur($data,$currentCredit);
|
|
|
|
// Vérifier si hascoborrower est présent dans les données
|
|
// Note: pour les radio buttons, si aucun n'est coché, la clé peut être absente
|
|
// On vérifie aussi s'il y a des données de co-emprunteur pour détecter sa présence
|
|
$hasCoBorrowerInData = array_key_exists('hascoborrower', $data) && ($data['hascoborrower'] === '1' || $data['hascoborrower'] === 1);
|
|
$hasCoBorrowerFields = !empty($data['cofirstname']) || !empty($data['colastname']) || !empty($data['coemail']) || !empty($data['cophone']);
|
|
|
|
// Si hascoborrower est à '1' OU si des champs co-emprunteur sont présents, traiter le co-emprunteur
|
|
if ($hasCoBorrowerInData || $hasCoBorrowerFields) {
|
|
// Vérifier si le co-emprunteur existe déjà
|
|
$coBorrower = $this->getCoBorrower($currentCredit);
|
|
if (is_object($coBorrower)) {
|
|
// Le co-emprunteur existe, on le met à jour
|
|
$this->update_co_emprunteur($data, $currentCredit);
|
|
} else {
|
|
// Le co-emprunteur n'existe pas, on le crée seulement si hascoborrower === '1'
|
|
// (pour éviter de créer un co-emprunteur par erreur)
|
|
if ($hasCoBorrowerInData) {
|
|
// Vérifier que le borrower existe avant d'insérer le co-emprunteur
|
|
$borrower = $this->getBorrower($currentCredit);
|
|
if (is_object($borrower)) {
|
|
$insertId = $this->insert_co_emprunteur($data, $currentCredit);
|
|
// Vérifier si l'insertion a réussi
|
|
if ($insertId === false || $insertId === 0) {
|
|
error_log('Erreur lors de l\'insertion du co-emprunteur pour le crédit ID: ' . $currentCredit->idCredit);
|
|
error_log('Dernière erreur DB: ' . $this->wpdb->last_error);
|
|
error_log('Dernière requête: ' . $this->wpdb->last_query);
|
|
} else {
|
|
// L'insertion a réussi, recharger le crédit pour que getCoBorrower trouve le nouveau co-emprunteur
|
|
$currentCredit = $this->getCredit($currentCredit->token);
|
|
}
|
|
} else {
|
|
error_log('Impossible de créer le co-emprunteur : borrower non trouvé pour le crédit ID: ' . $currentCredit->idCredit);
|
|
}
|
|
}
|
|
}
|
|
// Sauvegarder les autres crédits du co-emprunteur seulement si hascoborrower === '1'
|
|
if ($hasCoBorrowerInData) {
|
|
$this->save_autre_credit_co_emprunteur($data,$currentCredit);
|
|
}
|
|
}
|
|
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
array(
|
|
'last_update_date' => (new \DateTime())->format('Y-m-d H:i:s'),
|
|
'last_step' => '3'
|
|
),
|
|
array('idCredit' => $this->currentCredit->idCredit)
|
|
);
|
|
}
|
|
} |