<?php

namespace App\Controllers;

use App\Controllers\AdminController;
use App\Models\Caisse;
use App\Models\Orders;
use CodeIgniter\Controller;
class CaisseController extends AdminController{
    public function __construct()
    {
        parent::__construct();
    }

    public function createCaisse(){
        $orderModel = new Orders();
        $paymentData = $orderModel->getPaymentModes();
        
        // Initialisation des totaux avec 0 au cas où il n'y aurait pas de données
        $total = isset($paymentData->total) ? $paymentData->total : 0;
        $total_mvola1 = isset($paymentData->total_mvola1) ? $paymentData->total_mvola1 : 0;
        $total_mvola2 = isset($paymentData->total_mvola2) ? $paymentData->total_mvola2 : 0;
        $total_espece1 = isset($paymentData->total_espece1) ? $paymentData->total_espece1 : 0;
        $total_espece2 = isset($paymentData->total_espece2) ? $paymentData->total_espece2 : 0;
        $total_virement_bancaire1 = isset($paymentData->total_virement_bancaire1) ? $paymentData->total_virement_bancaire1 : 0;
        $total_virement_bancaire2 = isset($paymentData->total_virement_bancaire2) ? $paymentData->total_virement_bancaire2 : 0;
        // Calcul des totaux pour chaque mode de paiement
        $total_mvola = $total_mvola1 + $total_mvola2;
        $total_espece = $total_espece1 + $total_espece2;
        $total_virement_bancaire = $total_virement_bancaire1 + $total_virement_bancaire2;
        // Fusion des données dans la variable $data
        $data =  [
            'total' => $total,
            'total_mvola' => $total_mvola,
            'total_espece' => $total_espece,
            'total_banque' => $total_virement_bancaire,
        ];
        $caisse = new Caisse();
        if($data) {
            return $caisse->updateCaisse($data);
        }
    }
}