Continued on MVC to edit transaction
authorroot <root@n3150.home>
Thu, 3 Aug 2023 16:39:11 +0000 (18:39 +0200)
committerroot <root@n3150.home>
Thu, 3 Aug 2023 16:39:11 +0000 (18:39 +0200)
app/compta/controlers/transaction.php
app/compta/models/transaction.php
app/compta/pages.php
app/compta/services.php
app/compta/views/transaction_detail.php
app/compta/views/transaction_update.php [new file with mode: 0644]

index eb39a658d58c3fdc387866d309079b3a432f6728..165ffb6d7000242e9ede9715e43fe2f2abc72b81 100644 (file)
@@ -39,7 +39,7 @@ class Transaction
     {
     }
 
-    public function remove()
+    public function remove($_id)
     {
     }
 
@@ -57,8 +57,12 @@ class Transaction
     {
     }
 
-    public function update()
+    public function update($_id)
     {
+        require_once(controler::$basedir."/views/transaction_update.php");
+        $this->_model->get($_id);
+        $page = new \compta\views\transaction\Update($this->_model);
+        return $page;
     }
 }
 
index 3a8bbbb1e6ea7a0552010f2d5e4af6f5c6690bf7..f85cad803f7bbe5d780b27300add08376c4980a5 100644 (file)
@@ -7,6 +7,85 @@ require_once(dirname(__FILE__)."/../api_booking.php");
 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. 
  */
@@ -17,6 +96,8 @@ class Model implements \JsonSerializable
     
     private $_entries;
 
+    public  $_id;
+    public  $_mouvements;
     /**
      * Constructor
      */
@@ -42,7 +123,16 @@ class Model implements \JsonSerializable
         $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
index 1644fe2c1c2d7eaff4ce1e78c9a27e58d2c6dcc9..b4e8ea631c88b649b5a0278f72e2a1bd963a2c5e 100644 (file)
@@ -117,7 +117,7 @@ class PageCompta extends AppPage {
 
         } else
         { // Get attribute directly from object model
-            return $model->{$value->getAttribute("asp-for")};
+            return $model->{$asp};
         }
     }
     /**
index 4c45db36ce9e23c18295975bd5006d70229b0c6b..31978b5599e77310e5997d3f8ecdc5f6ed2ba76f 100644 (file)
@@ -103,6 +103,7 @@ $r->get('/transaction',"transaction#index")->setRoot($ctrl_dir);
 $r->get('/transaction/',"transaction#index")->setRoot($ctrl_dir);
 $r->get('/transaction/nouveau',"transaction#nouveau")->setRoot($ctrl_dir);
 $r->get('/transaction/detail/?:id',"transaction#detail")->setRoot($ctrl_dir);
+$r->get('/transaction/remove/?:id',"transaction#remove")->setRoot($ctrl_dir);
 $r->get('/transaction/update/?:id',"transaction#update")->setRoot($ctrl_dir);
 $r->post('/transaction/updatePost',"transaction#updatePost")->setRoot($ctrl_dir);
 
index 7bbddb14c81c9b1fb114277fcc24a9a4291c08d2..9cdd649721bbddaaf30d2529ad07f5a6d0181334 100644 (file)
@@ -46,6 +46,10 @@ const XmlAccount =<<<XML
       </tfoot>
     </table>
   </panel>
+  <panel id="pButtons" resp-width="98%" class="col-12" style="min-height:14px;">
+<!-- 
+-->
+  </panel>
   <div class="clearfix"></div>
     <!-- Script Section -->
   </content>
@@ -104,9 +108,14 @@ _EOF;
      */
     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();
@@ -124,10 +133,11 @@ _EOF;
         $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);
     }
 
     /**
diff --git a/app/compta/views/transaction_update.php b/app/compta/views/transaction_update.php
new file mode 100644 (file)
index 0000000..bd92184
--- /dev/null
@@ -0,0 +1,210 @@
+<?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;
+    }
+}
+
+?>