237 lines
8.8 KiB
PHP
237 lines
8.8 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 DateTime;
|
|
use libraries\FormValidator;
|
|
|
|
class CRED_credit_step2 extends CRED_credit {
|
|
|
|
public function save_step_1($data) {
|
|
|
|
// Vérifier si l'adresse email contient @example.com
|
|
if(isset($data['email']) && strpos($data['email'], '@example.com') !== false) {
|
|
return [
|
|
'success' => false,
|
|
'errors' => ['email' => 'Adresse email invalide'],
|
|
'formatted_errors' => 'Adresse email invalide'
|
|
];
|
|
}
|
|
|
|
// Validation des données
|
|
$validator = new FormValidator($data, $this);
|
|
$errors = $validator->validateStep2($data);
|
|
|
|
if (!empty($errors)) {
|
|
// Retourner les erreurs pour affichage
|
|
return [
|
|
'success' => false,
|
|
'errors' => $errors,
|
|
'formatted_errors' => $validator->getFormattedErrors()
|
|
];
|
|
}
|
|
|
|
$currentCredit = $this->getCredit($data['credit-direct-token']);
|
|
|
|
$id_credit = $currentCredit->idCredit;
|
|
|
|
if (!is_object($currentCredit)) {
|
|
return false;
|
|
}
|
|
|
|
$borrower = $this->getBorrower($currentCredit);
|
|
$borrowerTableName = 'cdf_Emprunteur';
|
|
$borrowerData = [
|
|
'nom' => $data['lastname'],
|
|
'prenom' => $data['firstname'],
|
|
'telephone' => $data['phone'],
|
|
'email' => $data['email'],
|
|
'pays' => $data['country'],
|
|
'code_postal' => $data['zip'],
|
|
'FK_demande_creditdirect' => $currentCredit->idCredit
|
|
];
|
|
|
|
/* $borrowerData = [
|
|
'nom' => $data['lastname'],
|
|
'prenom' => $data['firstname'],
|
|
'telephone' => $data['phone'],
|
|
'email' => $data['email'],
|
|
'FK_agence' => $data['agency'],
|
|
'FK_demande_creditdirect' => $currentCredit->idCredit
|
|
]; */
|
|
|
|
if (is_object($borrower)) {
|
|
$this->wpdb->update($borrowerTableName, $borrowerData, [
|
|
'idemprunteur' => $borrower->idemprunteur
|
|
]);
|
|
} else {
|
|
$this->wpdb->insert($borrowerTableName, $borrowerData);
|
|
}
|
|
|
|
if(!empty($data['comment'])) {
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
array('commentaire' => $data['comment']),
|
|
array('idCredit' => $currentCredit->idCredit)
|
|
);
|
|
}
|
|
|
|
if (in_array($currentCredit->type_credit, $this->creditAutos)) {
|
|
$optionsTableName = 'cdf_Options_credit_auto';
|
|
|
|
|
|
$optionsData = [
|
|
'marque' => $data['marque'],
|
|
'date_immatriculation' => $data['vehicleregistrationdate'],
|
|
'nom_vendeur' => $data['sellername'],
|
|
'date_immatriculation' => !empty($data['vehicleregistrationdate']) ? (DateTime::createFromFormat('d/m/Y', $data['vehicleregistrationdate']) ? DateTime::createFromFormat('d/m/Y', $data['vehicleregistrationdate'])->format('Y-m-d') : $data['vehicleregistrationdate']) : null,
|
|
'adresse_vendeur' => $data['selleraddress'],
|
|
'prix_vehicule' => $data['vehicleprice'],
|
|
'montant_accompte' => $data['vehicule_accompte'],
|
|
'montant_reprise' => $data['vehicule_reprise'],
|
|
'montant_emprunt' => $data['vehicule_emprunt'],
|
|
'duree' => $data['vehicule_duree']
|
|
];
|
|
|
|
|
|
if (!is_null($currentCredit->FK_credit_auto)) {
|
|
$this->wpdb->update($optionsTableName, $optionsData, [
|
|
'idOptions_credit_auto' => $currentCredit->FK_credit_auto
|
|
]);
|
|
} else {
|
|
$this->wpdb->insert($optionsTableName, $optionsData);
|
|
|
|
$optionId = $this->wpdb->insert_id;
|
|
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
array('FK_credit_auto' => $optionId),
|
|
array('idCredit' => $currentCredit->idCredit)
|
|
);
|
|
}
|
|
}
|
|
|
|
if (in_array($currentCredit->type_credit, $this->one_step_credit_types)) {
|
|
$optionsTableName = 'cdf_Options_credit_hypotecaire';
|
|
$optionsData = [
|
|
'type_credit' => $data['estateloantype'],
|
|
'prix_achat' => $data['estatebuyingprice'],
|
|
'prix_construction_tvac' => $data['prix_achat_tvac'],
|
|
'valeur_batiment' => $data['valeur_batiment'],
|
|
'fonds_propre' => $data['estateequity'],
|
|
'compromis_signe' => $data['estatecompromise'],
|
|
'montant_revenu_cadastral' => $data['estatcadastralincome'],
|
|
'montant_a_emprunter' => $data['batiment_emprunt'],
|
|
'duree' => $data['batiment_duree']
|
|
];
|
|
|
|
if (!is_null($currentCredit->FK_credit_hypothecaire)) {
|
|
$this->wpdb->insert($optionsTableName, $optionsData, [
|
|
'FK_credit_hypothecaire' => $currentCredit->FK_credit_hypothecaire
|
|
]);
|
|
} else {
|
|
$this->wpdb->insert($optionsTableName, $optionsData);
|
|
|
|
$optionId = $this->wpdb->insert_id;
|
|
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
array('FK_credit_hypothecaire' => $optionId),
|
|
array('idCredit' => $currentCredit->idCredit)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Re-get the current credit to hydrate it
|
|
*/
|
|
$currentCredit = $this->getCredit($data['credit-direct-token']);
|
|
|
|
/**
|
|
* Send mail
|
|
*/
|
|
if ($currentCredit->last_step < 2) {
|
|
$agencies = $this->getAgencies();
|
|
$borrower = $this->getBorrower($currentCredit);
|
|
$map_credit_type = $this->getCreditTypes();
|
|
$mapHouseCreditTypes = $this->getHouseCreditTypes();
|
|
|
|
// S'assurer que le type de crédit est défini
|
|
if (!isset($currentCredit->type_credit) || !array_key_exists($currentCredit->type_credit, $map_credit_type)) {
|
|
error_log('Type de crédit inconnu: ' . $currentCredit->type_credit);
|
|
}
|
|
|
|
$is_credit_auto = $this->is_credit_auto($currentCredit);
|
|
$is_one_step_credit = in_array($currentCredit->type_credit, $this->one_step_credit_types);
|
|
|
|
|
|
//update the credit id in the cdf_Credit with comment data
|
|
|
|
if(!empty($data['comment'])) {
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
array('commentaire' => $data['comment']),
|
|
array('idCredit' => $id_credit)
|
|
);
|
|
}
|
|
|
|
ob_start();
|
|
include(_CRED_BASE_PATH_ . '/templates/email/credit-step2-mail.php');
|
|
$message = ob_get_clean();
|
|
|
|
|
|
ob_start();
|
|
include(_CRED_BASE_PATH_ . '/templates/email/clients_emails/credit-step2-mail-client.php');
|
|
$message_client = ob_get_clean();
|
|
|
|
$this->mailchimpSynchro($currentCredit, $borrower);
|
|
|
|
|
|
if(!isset($data['isback']) || $data['isback'] != '1') {
|
|
if(isset($data['type_credit_selected']) && !empty($data['type_credit_selected']) || isset($data['sub_loan_type']) && !empty($data['sub_loan_type']))
|
|
$type_credit_selected = isset($data['sub_loan_type']) ? $data['sub_loan_type'] : $data['type_credit_selected'];
|
|
|
|
|
|
$creditOptionsLabels = !empty($type_credit_selected) ? $this->getCreditLabel($type_credit_selected) : $map_credit_type[$currentCredit->type_credit];
|
|
|
|
// Exception : ne pas envoyer de mail si l'utilisateur connecté a l'ID 1
|
|
if (!is_user_logged_in() || get_current_user_id() != 1) {
|
|
// Envoyer l'email uniquement à l'administrateur (to_client = false)
|
|
$this->sendEmail('Demande de crédit', $message, $borrower, $currentCredit, [], false);
|
|
|
|
// Envoyer l'email à l'emprunteur (to_client = true)
|
|
$this->sendEmail('Demande de crédit', $message_client, $borrower, $currentCredit, [], true);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update Credit info
|
|
*/
|
|
$updateInfos = [
|
|
'last_update_date' => (new \DateTime())->format('Y-m-d H:i:s'),
|
|
];
|
|
|
|
if (intval($currentCredit->last_step) === 1) {
|
|
$updateInfos['last_step'] = '2';
|
|
}
|
|
|
|
$this->wpdb->update(
|
|
'cdf_Credit',
|
|
$updateInfos,
|
|
array('idCredit' => $currentCredit->idCredit)
|
|
);
|
|
|
|
$this->wpdb->suppress_errors = false;
|
|
|
|
return true;
|
|
}
|
|
} |