require_once(dirname(__FILE__)."/../api_pcmn.php");
 require_once(dirname(__FILE__)."/../api_retrieve.php");
 
+//SELECT trans_id,voucher_ref,voucher_date,act_trans_date,trans_label  
+class Transaction implements \JsonSerializable
+{
+    /* Order is important don't change */
+    public $id           = -1;
+    public $voucher_ref  = -1;
+    public $voucher_date = "2022-01-01"; // Could be HA, AN, what ever
+    public $trans_date   = "";   // Account name
+    public $trans_label  = "";  // Debit or credit
+
+    /**
+     * Build entry from SQL request Retrieve getTransaction
+     */
+    public function __construct($c = null)
+    {
+        if ($c != null)
+        {
+            $i = 0;
+            foreach( get_object_vars($this) as $k => $v)
+            { $this->{$k} = $c[$i++]; }
+        }
+    }
+    /**
+     * Required by JsonSerializable
+     */
+    public function jsonSerialize()
+    {
+        return [
+          'id'           => $this->id
+        , 'voucher_date' => $this->voucher_date
+        , 'voucher_ref'  => $this->voucher_ref
+        , 'trans_date'   => $this->trans_date
+        , 'trans_label'  => $this->trans_label
+        ];
+    }
+};
+
+
+class Entry implements \JsonSerializable
+{
+    //    $arr   = Array("id","acc_id","acc_name","entry_type","amount_debit","amount_credit");
+    // trans_entry_id,entry_date,te.acc_id,acc.acc_name,debit_credit,amount,te.entry_type 
+    /* Order is important don't change */
+    public $id         = -1;
+    public $entry_date = "2022-01-01"; // Could be HA, AN, what ever
+    public $acc_id     = -1;
+    public $acc_name   = "";   // Account name
+    public $dc         = "d";  // Debit or credit
+    public $amount     = 0.0;
+    public $entry_type = "BQ"; // Could be HA, AN, what ever
+
+    /**
+     * Build entry from SQL request Retrieve getTransaction
+     */
+    public function __construct($c = null)
+    {
+        if ($c != null)
+        {
+            $i = 0;
+            foreach( get_object_vars($this) as $k => $v)
+            { $this->{$k} = $c[$i++]; }
+        }
+    }
+    /**
+     * Required by JsonSerializable
+     */
+    public function jsonSerialize()
+    {
+        return [
+          'id'         => $this->id
+        , 'entry_date' => $this->entry_date
+        , 'entry_type' => $this->entry_type
+        , 'acc_id'     => $this->acc_id
+        , 'amount'     => $this->amount
+        , 'dc'         => $this->dc
+        , 'acc_name'   => $this->acc_name];
+    }
+};
+
 /**
  * @brief Data model for a transaction. 
  */
     
     private $_entries;
 
+    public  $_id;
+    public  $_mouvements;
     /**
      * Constructor
      */
         $r    = $pcmn->getTransaction( $_id); // Get Transaction
         $this->_id         = $_id;
         $this->_mouvements = $r['entries'];
-        $this->_summary    = $r['transaction'];
+        foreach($r['entries'] as $e)
+        {
+            $this->_entries[] = new Entry($e);
+        }
+        $this->_summary    = new Transaction($r['transaction']);
+    }
+
+    public function getEntries()
+    {
+        return $this->_entries;
     }
     /**
      * Required by JsonSerializable
 
       </tfoot>
     </table>
   </panel>
+  <panel id="pButtons" resp-width="98%" class="col-12" style="min-height:14px;">
+<!-- 
+-->
+  </panel>
   <div class="clearfix"></div>
     <!-- Script Section -->
   </content>
      */
     protected function _updateXML(&$xml)
     {
-        $sum   = $this->_model->_summary;
+        $buttons=<<<_EOF
+    <button id='btTrBack' href="/app/compta/services.php/transaction/update" style="float:left;">Retour</button>
+    <button id='btTrUpdate' href="/app/compta/services.php/transaction/update/{$this->_model->_id}" style="float:left;">Modifier</button>
+    <button id='btTrErase' style="float:left;" href="/app/compta/services.php/transaction/remove/{$this->_model->_id}">Supprimer</button>
+_EOF;
+        $sum   = $this->_model->_summary->jsonSerialize();
         $fields= Array('title','date','voucher_ref','voucher_date');
-        $values= Array($sum[4],$sum[3],$sum[1],$sum[2]);
+        $values= Array($sum['trans_label'],$sum['trans_date'],$sum['voucher_ref'],$sum['voucher_date']);
         $tbody = $xml->getElementsByTagName('tbody');
         $f       = $tbody->item(0);
         $nel     = $xml->createDocumentFragment();
         $retour = $this->getElementById($xml,"btRetour");
         $retour->removeAttribute('link');
         $retour->setAttribute('link',"/app/compta/services.php/".\AEB::$REQUEST['b']);
-        // Ok Handle solde
-        //$solde = $this->getElementById($xml,"accSoldeCompte");
-        //$el    = $xml->createTextNode("compte ".$this->_model->_solde[2]);
-        //$solde->appendChild($el);
+        // Ok Handle buttons 
+        $panelButtons= $this->getElementById($xml,"pButtons");
+        $nel     = $xml->createDocumentFragment();
+        $nel->appendXML($buttons);
+        $panelButtons->appendChild($nel);
     }
 
     /**
 
--- /dev/null
+<?php
+
+namespace compta\views\transaction; 
+require_once(dirname(__FILE__)."/../../../phplib/iface.ActionResult.php");
+
+require_once(dirname(__FILE__)."/../pages.php");
+require_once(dirname(__FILE__)."/../api_exercice.php");
+require_once(dirname(__FILE__)."/../api/recurrent.php");
+require_once(dirname(__FILE__)."/../api_booking.php");
+
+const xmlTransactionUpdate =<<<XML
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<?xslt-param name='alias' value='/andre/'?>
+<?xml-stylesheet type="text/xsl" href="xsl/gui.xsl?alias='/andre/'"?>
+<gui langue='fr' style="padding: 0;">
+<liens/>
+<title langue='fr'>Gestion comptable</title>
+<content>
+  <!-- The script does not work. Looks like an order issue-->
+  <script>
+  (function() {
+    console.log("Try to update mh1a");
+    var mh1a = $('mh1').firstChild;
+    mh1a.href = "/fr/app/compta/services.php/";
+  })(); 
+  </script>
+  <script>
+  </script>
+
+<menu>
+</menu>
+
+  <panel id="pTransaction" class="col-11 pg-account" style="min-height:150px;">
+    <h2 id="title" style="width:100%">Mettre à jour une transaction</h2>
+
+      <form method="post" action="/app/compta/services.php/transaction/updatePost">
+        <group id="group" > 
+          <grouptitle>Transaction</grouptitle>
+          <groupcontent>
+            <FieldSet labelWidth="160" style="border:none 0px;">
+              <TextField type="hidden" id="rec_id" asp-for="_id" style=""></TextField>
+              <TextField width="300" labelWidth="250" id="rec_desc" asp-for="_summary.trans_label" style="">
+                <Label style="">Intitulé</Label></TextField><br/>
+              <TextField type="date" width="190" id="rec_next" asp-for="_summary.trans_date" style="">
+                <Label style="">Date enregistrement</Label></TextField><br/>
+              <TextField type="date" width="190" id="rec_end" asp-for="_summary.voucher_date" style="">
+                <Label style="">Date Facture</Label></TextField><br/>
+              <TextField width="300" id="rec_amount" asp-for="_summary.voucher_ref" style="">
+                <Label style="">Rérérence</Label></TextField><br />
+            </FieldSet>
+          </groupcontent>
+       </group>
+        <h2>Les écritures</h2>
+      <table autoScroll='true' id="rec_entries" height="220"  style="margin: 0px 0px 0px 15px;width: 98%;">
+      <caption>Compte</caption>
+      <thead resp-width="100%" >
+      <tr style="">
+        <th resp-width='5%'>+</th>
+        <th resp-width="25%">Compte</th>
+        <th resp-width="30%" >Désignation</th>
+        <th resp-width='10%'>Jrnx</th>
+        <th resp-width="15%">Débit</th>
+        <th  resp-width="15%">Crédit</th></tr>
+      </thead>
+      <tbody style="overflow:auto;">
+      </tbody>
+      <tfoot>
+         <tr><td id="recSoldeCompte" colspan="3">Solde:</td>
+           <td style="text-align:right;" id="recSoldeDebit"></td>
+           <td style="text-align:right;" id="recSoldeCredit"></td></tr>
+      </tfoot>
+    </table>
+    <panel resp-width="98%" style="min-height:14px;padding-top:15px;">
+              <!-- 
+               -->
+            <button id='btRURecord' type="submit" style="float:left;margin-right: 15px;">Modifier</button>
+            <button id='btRUCancel' href="#" style="float:left;">Annuler</button>
+          </panel>
+        </form>
+  </panel>
+  <div class="clearfix"></div>
+    <!-- Script Section -->
+  </content>
+</gui>
+XML;
+
+
+/**
+ * @brief Update GUI with the list of 
+ * documents available. All decomptes
+ */
+class Update extends \PageCompta
+             implements \IActionResult 
+{
+    private $_model;
+
+    function __construct($model)
+    {
+        $this->_model = $model;
+        parent::__construct(xmlTransactionUpdate,true);
+    }
+
+    public function render()
+    {
+        $this->show();
+    }
+
+  /**
+   * @brief insert a script in the page to update menu link
+   *
+   */
+    private function script($loc = "/app/compta/services.php/")
+    {
+        $s=<<<_EOF
+<script>
+(function() {
+  var mh1a = $('mh1').firstChild;
+  mh1a.href = "{$loc}";
+})();
+</script>
+_EOF;
+    echo $s;
+  }
+    /**
+     * @brief allow sub classes to modify xml file
+     */
+    protected function _updateXML(&$xml)
+    {
+        // Update the title
+        $tbody   = $xml->getElementsByTagName("tbody");
+        $nel     = $xml->createDocumentFragment();
+        $nel->appendXML($this->fillEntries());
+        $tbody->item(0)->appendChild($nel);
+        /* Update menu */
+        $tbody   = $xml->getElementsByTagName('menu');
+        $f       = $tbody->item(0);
+        $items   =<<<_EOF
+  <item id="btRetour" link="/app/compta/services.php/transaction/detail/%s" class="" title="Retour" />
+_EOF;
+        $frag=sprintf($items
+                     ,$this->_model->_id);
+        $nel     = $xml->createDocumentFragment();
+        $nel->appendXML($frag);
+        $f->appendChild($nel);
+        // Update asp for fields
+        $this->updateAspfor($xml,$this->_model);
+    }
+
+    private function fillEntries()
+    {
+        $model = $this->_model;
+        $count = 0;
+        $frag  = "";
+        //trans_entry_id,entry_date,te.acc_id,acc.acc_name,debit_credit,amount,te.entry_type 
+        $arr   = Array("id","acc_id","acc_name","entry_type","amount_debit","amount_credit");
+        $tplt  =<<<_EOF
+<td><TextField type="%s" id="entry%d_%s" value="%s" style="width: 95%%;"></TextField></td>
+_EOF;
+        $tplt_ro  =<<<_EOF
+<td><TextField type="%s" id="entry%d_%s" value="%s" readOnly="1" style="width: 95%%;"></TextField></td>
+_EOF;
+        // Add some empty entries to fill the gap.
+        $entries = $model->getEntries();
+        $ce = count($model->getEntries());
+        if ($count < 8 ) 
+        {
+            for ($i = 8 - $ce ; $i > 0 ; $i--)
+            { $entries[] = new \compta\models\transaction\Entry(); }
+
+        }
+        foreach($entries as $e)
+        {
+            $i  = 0;
+            $fe = "<tr>";
+            foreach($arr as $f)
+            {
+                if ($f == "amount_debit" ) 
+                {
+                    if ($e->dc == 'd')
+                    {
+                           $fe.=sprintf($tplt,"text",$count,$f,$e->amount);
+                    } else {
+                        $fe.=sprintf($tplt,"text",$count,$f,"0.0");
+                    }
+                } else if ($f == "id")
+                {
+                    $fe.=sprintf($tplt,"hidden",$count,$f,$e->{$f});
+                } else if ($f == "amount_credit")
+                {
+                    if ($e->dc == 'c')
+                    {
+                        $fe.=sprintf($tplt,"text",$count,$f,$e->amount);
+                    } else {
+                        $fe.=sprintf($tplt,"text",$count,$f,"0.0");
+                    }
+                } else if ($f == "acc_name")
+               {
+                    $fe.=sprintf($tplt_ro,"text",$count,$f,$e->{$f});
+               } else
+                    $fe.=sprintf($tplt,"text",$count,$f,$e->{$f});
+            }
+        $fe.="</tr>";
+        $frag.=$fe;
+        $count++;
+        }
+        return $frag;
+    }
+}
+
+?>