<?php
/*
 *   This file is part of NOALYSS.
 *
 *   NOALYSS is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   NOALYSS is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NOALYSS; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
// Copyright Author Dany De Bontridder danydb@aevalys.eu 6/01/24
/*! 
 * \file
 * \brief Operation OUVERTURE
 */

/**
 * @class Operation_Opening
 * @brief Operation ouverture
 */
class Operation_Opening extends Operation_Exercice
{
    private $signature;
    private $from_folder;
    private $exercice;

    function __construct($p_id)
    {
        parent::__construct($p_id);
        $this->signature = 'opening';

    }

    /**
     * @return mixed
     */
    public function get_from_folder()
    {
        return $this->from_folder;
    }

    /**
     * @param mixed $from_folder
     */
    public function set_from_folder($from_folder): void
    {
        $this->from_folder = $from_folder;
    }

    /**
     * @return mixed
     */
    public function get_exercice()
    {
        return $this->exercice;
    }

    /**
     * @param mixed $exercice
     */
    public function set_exercice($exercice): void
    {
        $this->exercice = $exercice;
    }

    public function get_signature(): string
    {
        return $this->signature;
    }

    function from_request()
    {
        $http = new HttpInput();
        $this->exercice = $http->request("exercice", "number");
        $this->from_folder = $http->request("dos_id", "number");
    }

    /**
     * @brief insert data into data operation_exercice and operation_exercice_detail
     * @return void
     */
    function insert()
    {
        $cn = Dossier::connect();
        $other_dossier = new Database($this->from_folder);

        $sql = "
with total_account as (
	select sum(a.montant) as tot_amount, j_poste, f_id
             from
             (select j_id, case when j_debit='t' then j_montant
             else j_montant * (-1) end  as montant
             from jrnx) as a
             join jrnx using (j_id)
             join parm_periode on (j_tech_per = p_id )
             where
             p_exercice=$1
             and j_poste::text not like '7%'
             and j_poste::text not like '6%'
             group by j_poste,f_id
             having (sum(a.montant) != 0 ) 
             )
select t1.tot_amount
	,t1.j_poste
	,(select pcm_lib from tmp_pcmn where pcm_val=t1.j_poste) as lib_accounting
	,t1.f_id
	,(select ad_value fd2 from fiche_detail fd2 where fd2.f_id=t1.f_id and fd2.ad_id=23) qcode
,(select fd3.ad_value from fiche_detail fd3 where fd3.f_id=t1.f_id and fd3.ad_id=1) f_name
,abs(t1.tot_amount) atot_amount
,case when tot_amount <0 then 'f' else 't' end debit
from total_account t1
";
        $exercice_report=$this->exercice+1;
        $this->operation_exercice_sql->setp("oe_type", $this->signature)
            ->setp("oe_dossier_id", $this->from_folder)
            ->set("oe_text",_("Ecriture ouverture $exercice_report"))
            ->setp("oe_exercice", $this->exercice);
        try {
            $cn->start();
            $this->operation_exercice_sql->insert();
            $array = $other_dossier->get_array($sql, array($this->exercice));
            if (empty($array)) return;
            foreach ($array as $item) {
                $row = new Operation_Exercice_Detail_SQL($cn);
                $row->oe_id = $this->operation_exercice_sql->oe_id;
                $row->oed_poste = (empty($item['qcode']))?$item["j_poste"]:null;
                $row->oed_qcode = $item["qcode"];
                $row->oed_amount = $item['atot_amount'];
                $row->oed_label=(empty($item['qcode']))?$item['lib_accounting']:$item['f_name'];
                $row->oed_debit = $item["debit"];
                $row->save();
            }
            $cn->commit();

        } catch (\Exception $e) {
            $cn->rollback();
            echo $e->getMessage();
            throw $e;
        }

    }
    public function display_result()
    {
        echo h2("Ouverture compte");

        parent::display_result(); // TODO: Change the autogenerated stub
    }
}