credit-direct/app/models/credit-step1.php
2025-12-18 09:44:42 +01:00

456 lines
17 KiB
PHP

<?php
namespace models;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class CRED_credit_step1 extends CRED_credit
{
protected $currentCredit;
public function save_step_0($data)
{
$save_all_form = 0;
try {
/**
* Save data
*/
$this->wpdb->insert('cdf_Credit', array(
'type_credit' => $data['loan_type'],
'sel_credit' => $data['sub_loan_type'],
'capital' => $data['selected_capital'],
'duree' => $data['selected_months'],
'cout_total' => $data['hidden_cout_total_value'],
'mensualite' => $data['hidden_montant_total_value'],
'taux_nominal_annuel' => $data['hidden_taeg_value'],
'rgpd' => 'oui',
'create_date' => (new \DateTime())->format('Y-m-d H:i:s')
));
$current_credit_id = $this->wpdb->insert_id;
$token = $this->generateToken($current_credit_id);
$currentCredit = $this->getCredit($token);
/* if($currentCredit->type_credit != 'pat')
return $token;
else
$this->save_step_1_pat($data); */
return $token;
} catch (\Exception $e) {
die($e->getMessage());
}
}
public function save_step_1_all($data) {
if(isset($data['email']) && strpos($data['email'], '@example.com') !== false) {
return false; // Rejeter la demande si l'adresse email contient @example.com
}
$currentCredit = $this->getCredit($data['credit-direct-token']);
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'],
'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);
}
/**
* Re-get the current credit to hydrate it
*/
$currentCredit = $this->getCredit($data['credit-direct-token']);
$this->update_emprunteur($data, $currentCredit);
$this->save_autre_credit_emprunteur($data, $currentCredit);
if (array_key_exists('hascoborrower', $data) && $data['hascoborrower'] === '1') {
$this->insert_co_emprunteur($data, $currentCredit);
$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' => '4'
),
array('idCredit' => $this->currentCredit->idCredit)
);
}
public function save_one_step($data) {
$email_demande = $data['email'];
/* if($this->is_webdev_user()) {
echo '<pre>';
print_r($data);
echo '</pre>';
die();
} */
if(strpos($email_demande, '@example.com') !== false) {
return false; // Rejeter la demande si l'adresse email contient @example.com
}
$currentCredit = $this->getCredit($data['credit-direct-token']);
$included_hypo_credits = ['am','amr','cied','frais_notaire','cdp'];
if (!is_object($currentCredit)) {
return false;
}
/* if($this->is_webdev_user()) {
echo '<pre>';
print_r($data);
echo '</pre>';
die();
} */
/* echo '<pre>';
print_r($data);
echo '</pre>';
die(); */
$ddn = null;
if (!empty($data['birthdate'])) {
$ddn = \DateTime::createFromFormat('d/m/Y', $data['birthdate']);
if ($ddn) {
$ddn = $ddn->format('Y-m-d');
}
}
// print_r($ddn);
$independent_since = \DateTime::createFromFormat('d/m/Y', $data['independent_since']);
if($independent_since) {
$independent_since = $independent_since->format('Y-m-d');
} else {
$independent_since = '';
}
$borrower = $this->getBorrower($currentCredit);
$borrowerTableName = 'cdf_Emprunteur';
$borrowerData = [
'nom' => $data['lastname'],
'prenom' => $data['firstname'],
'telephone' => $data['phone'],
'email' => $data['email'],
'date_naissance' => $ddn ?: '',
'nationalité' => $data['nationality'],
'adresse' => $data['address'],
'code_postal' => $data['zip'],
'localite' => $data['city'],
'pays' => $data['country'],
'contract_type' => $data['contract_type'],
'independent_since' => $independent_since,
'FK_profession' => $data['job'],
'FK_etat_civil' => $data['civilstatus'],
'salaire' => $data['salary'],
'annual_taxable_income' => isset($data['annual_taxable_income']) ? $data['annual_taxable_income'] : null,
'FK_demande_creditdirect' => $currentCredit->idCredit,
];
/* if($this->is_webdev_user()) {
echo '<pre>';
print_r($borrowerData);
echo '</pre>';
die();
} */
if (is_object($borrower)) {
$this->wpdb->update($borrowerTableName, $borrowerData, [
'idemprunteur' => $borrower->idemprunteur
]);
} else {
$this->wpdb->insert($borrowerTableName, $borrowerData);
}
//has another batiment
if(isset($data['isowner']) && $data['isowner'] === '1') {
$this->save_another_batiment($data, $currentCredit);
}
if(!empty($data['comment'])) {
$this->wpdb->update(
'cdf_Credit',
array('commentaire' => $data['comment']),
array('idCredit' => $currentCredit->idCredit)
);
}
if (array_key_exists('hascoborrower', $data) && $data['hascoborrower'] === '1') {
$this->insert_co_emprunteur_one_step($data);
}
if (in_array($currentCredit->type_credit, $included_hypo_credits)) {
$this->save_FK_credit_hypothecaire($currentCredit,$data);
}
if (
$currentCredit->type_credit == 'fin_neuve'
|| $currentCredit->type_credit == 'fin_occ_m3a'
|| $currentCredit->type_credit == 'fin_occ_p3a'
) {
$this->save_FK_credit_auto($currentCredit,$data);
}
$this->save_to_credits_listing($currentCredit);
}
public function save_another_batiment($data, $currentCredit) {
$borrower = $this->getBorrower($currentCredit);
if (!is_object($borrower)) {
return;
}
// Sauvegarder les bâtiments
if (isset($data['isowner']) && $data['isowner'] === '1') {
if (isset($data['building']) && is_array($data['building'])) {
$building_ids = []; // Pour stocker les IDs des bâtiments créés
foreach ($data['building'] as $index => $building) {
// Ignorer les données fantômes des templates (clés __INDEX__)
if ($index === '__INDEX__') {
continue;
}
// Insertion du bâtiment
$batiment_pays = '';
if (isset($building['inbelgium']) && $building['inbelgium'] === '0' && isset($building['country'])) {
$batiment_pays = sanitize_text_field($building['country']);
}
$this->wpdb->insert(
'cdf_Emprunteur_Batiments',
array(
'FK_emprunteur' => $borrower->idemprunteur,
'is_habitation' => isset($building['ishabitation']) ? intval($building['ishabitation']) : 0,
'Is_rapport' => isset($building['israpport']) ? intval($building['israpport']) : 0,
'en_belgique' => isset($building['inbelgium']) ? intval($building['inbelgium']) : 1,
'batiment_pays' => $batiment_pays,
'revenu_locatif' => isset($building['hasrentalincome']) ? intval($building['hasrentalincome']) : 0,
'montant_locatif' => isset($building['rentalamount']) ? floatval($building['rentalamount']) : 0
)
);
// Récupérer l'ID du bâtiment créé
$building_id = $this->wpdb->insert_id;
if ($building_id) {
$building_ids[$index] = $building_id;
}
}
// Sauvegarder les crédits associés aux bâtiments
if (isset($data['hasbuildingloans']) && $data['hasbuildingloans'] === '1') {
if (isset($data['buildingloan']) && is_array($data['buildingloan'])) {
foreach ($data['buildingloan'] as $loanIndex => $loan) {
// Ignorer les données fantômes des templates (clés __LOANINDEX__)
if ($loanIndex === '__LOANINDEX__') {
continue;
}
// Traiter la date première échéance
$firstPaymentDate = null;
if (!empty($loan['firstpayment'])) {
// Format attendu : jj-mm-aaaa
$date = \DateTime::createFromFormat('d-m-Y', $loan['firstpayment']);
if ($date) {
$firstPaymentDate = $date->format('Y-m-d');
}
}
// Utiliser le dernier bâtiment créé comme FK_autre_batiment
// Dans une version plus avancée, on pourrait associer chaque crédit à un bâtiment spécifique
$building_id = !empty($building_ids) ? end($building_ids) : 0;
// Convertir la durée d'années en mois
$duree_mois = isset($loan['duration']) ? intval($loan['duration']) * 12 : 0;
$this->wpdb->insert(
'cdf_Autre_credit',
array(
'FK_emprunteur' => $borrower->idemprunteur,
'FK_autre_batiment' => $building_id,
'FK_type_creance' => isset($loan['type']) ? intval($loan['type']) : null,
'banque' => isset($loan['bankname']) ? sanitize_text_field($loan['bankname']) : '',
'montant' => isset($loan['capital']) ? floatval($loan['capital']) : 0,
'duree_credit' => $duree_mois,
'mensualite' => isset($loan['monthly']) ? floatval($loan['monthly']) : 0,
'date_premiere_echeance' => $firstPaymentDate,
'cloture' => 0,
'solde_restant_du' => 0
)
);
}
}
}
}
}
// Gérer les suppressions de crédits si nécessaire
if (isset($data['delbuildingloans']) && !empty($data['delbuildingloans'])) {
$kuids = explode(',', $data['delbuildingloans']);
foreach ($kuids as $kuid) {
$kuid = trim($kuid);
if (!empty($kuid) && is_numeric($kuid)) {
$this->wpdb->delete(
'cdf_Autre_credit',
array('idautrecredit' => intval($kuid))
);
}
}
}
}
public function insert_co_emprunteur_one_step($data) {
$currentCredit = $this->getCredit($data['credit-direct-token']);
$borrower = $this->getBorrower($currentCredit);
$this->wpdb->insert('cdf_Emprunteur',array(
'nom' => $data['colastname'],
'prenom' => $data['cofirstname'],
'date_naissance' => $data['cobirthdate'],
'nationalité' => $data['conationality'],
'adresse' => $data['coaddress'],
'code_postal' => $data['cozip'],
'localite' => $data['cocity'],
'pays' => $data['cocountry'],
'contract_type' => $data['cocontract_type'],
'independent_since' => isset($data['coindependent_since']) ? $data['coindependent_since'] : null,
'salaire' => $data['cosalary'],
'annual_taxable_income' => isset($data['coannual_taxable_income']) ? $data['coannual_taxable_income'] : null,
'parent_emprunteur' => $borrower->idemprunteur,
'FK_demande_creditdirect' => $currentCredit->idCredit,
'FK_etat_civil' => $data['cocivilstatus'],
'FK_profession' => $data['cojob']
));
}
public function save_FK_credit_hypothecaire($currentCredit,$data) {
$included_hypo_credits = ['am','amr','cied','frais_notaire','cdp'];
if (in_array($currentCredit->type_credit, $included_hypo_credits)) {
$optionsTableName = 'cdf_Options_credit_hypotecaire';
$optionsData = [
'type_credit' => $data['estateloantype'],
'prix_achat' => $data['estatebuyingprice'],
'fonds_propre' => $data['estateequity'],
'compromis_signe' => $data['estatecompromise'],
'montant_a_emprunter' => $data['batiment_emprunt'],
'duree' => $data['batiment_duree'],
];
/**
* job,contract_type,salary
*/
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)
);
}
$currentCredit = $this->getCredit($data['credit-direct-token']);
/* echo '<pre>';
print_r($currentCredit);
echo '</pre>';
die(); */
}
return $currentCredit;
}
public function save_FK_credit_auto($currentCredit,$data) {
if (
$currentCredit->type_credit == 'fin_neuve'
|| $currentCredit->type_credit == 'fin_occ_m3a'
|| $currentCredit->type_credit == 'fin_occ_p3a'
) {
$optionsTableName = 'cdf_Options_credit_auto';
$optionsData = [
'marque' => $data['marque'],
'date_immatriculation' => $data['vehicleregistrationdate'],
'nom_vendeur' => $data['sellername'],
'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)
);
}
/* $currentCredit = $this->getCredit($data['credit-direct-token']); */
}
/* return $currentCredit; */
}
}