From: www-data Date: Mon, 18 Sep 2023 20:39:06 +0000 (+0200) Subject: [MVC] Update Transaction entry X-Git-Url: https://git.ebersold.fr/?a=commitdiff_plain;h=022b1ee6d2ea0099efa04cc2e30ff9969fc5498f;p=www%2Fsyndic.git [MVC] Update Transaction entry --- diff --git a/app/compta/api_booking.php b/app/compta/api_booking.php index f23635e..8d1ce70 100644 --- a/app/compta/api_booking.php +++ b/app/compta/api_booking.php @@ -134,7 +134,7 @@ _EOF; /** * */ - private function updateEntry($trans_id,$entry) + public function updateEntry($trans_id,$entry) { $entry_id = $entry['entry_id']; if ( isset($entry['key_charge']) ) diff --git a/app/compta/controlers/transaction.php b/app/compta/controlers/transaction.php index 90c1bcb..ffb460a 100644 --- a/app/compta/controlers/transaction.php +++ b/app/compta/controlers/transaction.php @@ -65,13 +65,49 @@ class Transaction return $page; } - public function updatePost($_id) + public function updatePost() { - require_once(controler::$basedir."/views/transaction_update.php"); - $this->_model->get($_id); - $page = new \compta\views\transaction\Update($this->_model); + require_once(controler::$basedir."/views/transaction_detail.php"); + //$this->_model->get($_id); + $page = new \compta\views\transaction\Detail($this->_model); + return $page; + } + /** + * If successfull update, show detail, or else + * show entry with Field on Error. For instance + * - account not leaf or does not exists + * - amount in debit and credit + * - amount is zero in debit and credit + */ + public function updateEntryPost() + { + require_once(controler::$basedir."/views/transaction_detail.php"); + //$this->_model->get($_id); + // TODO: Get entry check result from model + $page = new \compta\views\transaction\Detail($this->_model); return $page; } + + /** + * Display a transaction entry + */ + public function entry($_tid,$_eid) + { + $model = $this->_model; + require_once(controler::$basedir."/views/transaction_entry.php"); + $model->get($_tid); + $model->setEditEntry($_eid); + $page = new \compta\views\transaction\Entry($this->_model); + return $page; + } + + public function search() + { + } + + public function searchPost() + { + } } ?> diff --git a/app/compta/models/transaction.php b/app/compta/models/transaction.php index f85cad8..37f2465 100644 --- a/app/compta/models/transaction.php +++ b/app/compta/models/transaction.php @@ -27,6 +27,9 @@ class Transaction implements \JsonSerializable $i = 0; foreach( get_object_vars($this) as $k => $v) { $this->{$k} = $c[$i++]; } + } else + { + $this->id = -1; } } /** @@ -71,12 +74,12 @@ class Entry implements \JsonSerializable } } /** - * Required by JsonSerializable + * Required by JsonSerializable. Aligned with api_booking update_Entry */ public function jsonSerialize() { return [ - 'id' => $this->id + 'entry_id' => $this->id , 'entry_date' => $this->entry_date , 'entry_type' => $this->entry_type , 'acc_id' => $this->acc_id @@ -93,11 +96,23 @@ class Model implements \JsonSerializable { private $summary; - - private $_entries; + private $editEntry = 0; + + private $formValid = false; + public $_id; public $_mouvements; + + private $updatePostParams = Array( "id", "rec_trans_date" ,"rec_voucher_date" ,"rec_voucher_ref" + , "rec_trans_label" ); + private $updatePostCheck = Array( "", "str_date" ,"str_date" ,"str" + , "str"); + private $updateEntryPostParams = Array( "id", "rec_trans_date" ,"rec_voucher_date" ,"rec_voucher_ref" + , "rec_trans_label" ); + private $updateEntryPostCheck = Array( "", "str_date" ,"str_date" ,"str" + , "str"); + public $_entries = Array(); // Array of entries /** * Constructor */ @@ -105,12 +120,76 @@ class Model implements \JsonSerializable { $this->_session = $_sess; $this->_auth_cfg = $_auth; + $this->_summary = new Transaction(); + if (\AEB::$REQUEST['btRURecord'] != null) + { + $this->FormValid = $this->getUpdatePost(); + } + if (\AEB::$REQUEST['btRNRecord'] != null) + { + $this->FormValid = $this->getUpdatePost(); + } + /* view entry */ + if (\AEB::$REQUEST['editEntry'] != null) + { + try + { + $this->setEditEntry ( \AEB::$REQUEST['editEntry'] ); + } catch (Exception $e) + { + } + } + /* updateEntry */ + if (\AEB::$REQUEST['btUpdateEntry'] != null) + { + $e = null; + try + { + list($this->formValid,$e) = $this->getUpdateEntryPost ( ); + if ($this->formValid == true ) + { + $this->updateEntry($this->_id,$e); + + $this->get($this->_id); + } else + { + $this->get($this->_id); + } + } catch (Exception $e) + { + $this->get($this->_id); + } + } } + private function getSession() { return $this->_session; } + + /** + * \brief Verify that $e is a number. Throw an Exception if it's not the case + * + */ + public function setEditEntry($e) + { + $v = new \Validator(); + $v->num_int($e,"Failed edit_entry not int"); + $this->editEntry = $e; + } + + /** + * \brief Return true if the given Entry matches the one to be edited. + */ + public function isEntryEdit($e) + { + if ( ( $e->id == $this->editEntry ) && ( $this->editEntry != 0 ) ) + { + return true; + } + return false; + } /** * Get Transaction data from Database */ @@ -129,11 +208,202 @@ class Model implements \JsonSerializable } $this->_summary = new Transaction($r['transaction']); } + + /** + * Get Transaction data from Database + */ + private function updateEntry($trans_id,$e) + { + $_cfg = $this->_auth_cfg; + $pcmn = new \Booking( $this->getSession() + , $_cfg + , $this->getSession()->getDb()); + $r = $pcmn->updateEntry($trans_id,$e->jsonSerialize()); // Get Transaction + } public function getEntries() { return $this->_entries; } + /** + * Get Form Parameters in case of post Transaction + */ + private function getUpdateEntryPost() + { + $formValid = true; + $count = 0; + $valid = true; + $v = new \Validator(); + $this->_id = \AEB::$REQUEST["entry_trans_id"]; + + $k = "entry_"; // Check if that key exists + $arr = Array("id","acc_id","entry_type","amount_debit","amount_credit"); + $check = Array("num_int","num_int","","num_float","num_float"); + $e = new Entry(); + + // Check unique parameters + foreach ( $arr as $param) + { + $p = $k.$param; + $pv = \AEB::$REQUEST[$p]; + try { + $ck = $check[$count++]; + if ($ck != "" and $pv != "") + { + if ($ck == "num_float" and $pv!= "") + { + $pv = number_format(floatval($pv),3,'.',','); + } + $v->{$ck}($pv,"Not valid ".$param." va=".$pv."\n"); + } + if ($param == 'amount_debit' and $pv != 0.0 ) + { + $e->amount = $pv; + $e->dc = 'd'; + } else if ($param == 'amount_credit' and $pv != 0.0) + { + $e->amount = $pv; + $e->dc = 'c'; + } else + $e->{$param} = $pv; + } + catch (\Exception $exp) + { + error_log("Parameter failure:".$exp->getMessage()."\n"); + $formValid = false; + } + } + // Only add Entries with an amount not equal to 0. + if ($e->amount == 0.0 or ($e->acc_id == -1)) + { + $formValid = false; + } + return array($formValid,$e); + + } + /** + * Get Form Parameters in case of post Transaction + */ + private function getUpdatePost() + { + $formValid = true; + $count = 0; + $valid = true; + $v = new \Validator(); + $this->id = \AEB::$REQUEST['rec_id']; + $this->summary->id = $this->id; + // Check unique parameters + $f = reset($this->updatePostParams); + foreach ( $this->updatePostParams as $param) + { + if ($f == $param ) + { + $count++; + continue; // skip first element. + } + $pv = \AEB::$REQUEST[$param]; + try + { + $ck = $this->updatePostCheck[$count++]; + if ($ck != "") + { + $v->{$ck}($pv,"Not valid ".$param." value=".$pv."\n"); + } + // TODO remove once solved + $this->{$param} = $pv; + $this->summary->{str_replace("rec_","",$param)} = $pv; + } + catch (\Exception $e) + { + error_log("Parameter failure ".$e->getMessage()); + $formValid = false; + } + } + // Get Through post Entries + $count = 0; + for ( ; $count < 15 ; $count++) + { + $k = 'entry'.$count.'_id'; // Check if that key exists + if ( \AEB::$REQUEST->offsetExists($k) == true) + { + if ( ! $this->getEntryPost(\AEB::$REQUEST[$k],$count,$v) ) + { + $formValid = false; + } + } + else + { + break; + } + } + } + /** + * Retrieve the parameters from one line. + */ + private function getEntryPost($id,$line,$v) + { + $fvalid = true; // Form entry is valid + $k = "entry".$line.'_'; // Check if that key exists + $arr = Array("acc_id","acc_name","entry_type","amount_debit","amount_credit"); + $check = Array("num_int","","","num_float","num_float"); + $e = new Entry(); + $e->id = $id; + $count = 0; + if ($line == -1) + { + $k = "entry_"; + } + // Check unique parameters + foreach ( $arr as $param) + { + $p = $k.$param; + $pv = \AEB::$REQUEST[$p]; + try { + $ck = $check[$count++]; + if ($ck != "") + { + $v->{$ck}($pv,"Not valid ".$param." va=".$pv."\n"); + } + if ($param == 'amount_debit' and $pv != 0.0 ) + { + $e->amount = $pv; + $e->dc = 'd'; + } else if ($param == 'amount_credit' and $pv != 0.0) + { + $e->amount = $pv; + $e->dc = 'c'; + } else + $e->{$param} = $pv; + } + catch (\Exception $exp) + { + error_log("Parameter failure:".$exp->getMessage()); + $fvalid = false; + } + } + // Only add Entries with an amount not equal to 0. + if ($e->amount != 0.0 and ($e->acc_id != -1)) + { + $this->_entries[] = $e; + } + return $fvalid; + } + + /** + * Adapter method Return entry account type + * Eg: BQ, VT, AO, AN + */ + public function getEntryTypes() + { + $_cfg = $this->_auth_cfg; + $pcmn = new \Retrieve( $this->getSession() + , $_cfg + , $this->getSession()->getDb()); + $r = $pcmn->getEntryTypes( ); // Get Transaction + return $r; + } + + /** /** * Required by JsonSerializable */ @@ -141,7 +411,8 @@ class Model implements \JsonSerializable { return [ 'transi' => $this->_summary - , 'entries' => $this->_entries]; + , 'entries' => $this->_entries + ]; } } diff --git a/app/compta/services.php b/app/compta/services.php index 31978b5..66a2dbb 100644 --- a/app/compta/services.php +++ b/app/compta/services.php @@ -103,9 +103,12 @@ $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/entry/?:id/?:eid',"transaction#entry")->setRoot($ctrl_dir); $r->get('/transaction/remove/?:id',"transaction#remove")->setRoot($ctrl_dir); +$r->get('/transaction/removeEntry/?:id/?:eid',"transaction#removeEntry")->setRoot($ctrl_dir); $r->get('/transaction/update/?:id',"transaction#update")->setRoot($ctrl_dir); $r->post('/transaction/updatePost',"transaction#updatePost")->setRoot($ctrl_dir); +$r->post('/transaction/updateEntryPost',"transaction#updateEntryPost")->setRoot($ctrl_dir); /* Bilan services*/ $r->get('/bilan',"bilan#index")->setRoot($ctrl_dir); diff --git a/app/compta/views/account_detail.php b/app/compta/views/account_detail.php index 752d260..aeac199 100644 --- a/app/compta/views/account_detail.php +++ b/app/compta/views/account_detail.php @@ -37,8 +37,8 @@ const XmlAccount =<< Solde: - 0.0 - 0.0 + + @@ -116,8 +116,14 @@ _EOF; $retour->setAttribute('link',"/app/compta/services.php/".\AEB::$REQUEST['b']); // Ok Handle solde $solde = $this->getElementById($xml,"accSoldeCompte"); + $solde_debit = $this->getElementById($xml,"accSoldeDebit"); + $solde_credit = $this->getElementById($xml,"accSoldeCredit"); $el = $xml->createTextNode("compte ".$this->_model->_solde[2]); $solde->appendChild($el); + $el = $xml->createTextNode("".$this->_model->_solde[0]); + $solde_debit->appendChild($el); + $el = $xml->createTextNode("".$this->_model->_solde[1]); + $solde_credit->appendChild($el); } /** @@ -128,20 +134,24 @@ _EOF; $template=<<<_EOF %s -%s +%s %s %s %s _EOF; $frag = ""; $count = 0; + $back = \AEB::$REQUEST['b'].":/account/detail/".$this->_model->_id; $lst = $this->_model->_mouvements; foreach( $lst as $row) { $debit = sprintf("%.2f",$row[3]); $credit = sprintf("%.2f",$row[4]); $frag.=sprintf($template - ,++$count % 2?"odd":"even" ,$row[0],$row[5] ,$row[1],$row[2] + ,++$count % 2?"odd":"even" + ,$row[0] + ,$row[5] ,$back,$row[1] + ,$row[2] ,$row[3] == 0.0 ? "" : $debit ,$row[2] > $row[3] ? "red" : "black" ,$row[4] == 0.0? "" : $credit); diff --git a/app/compta/views/transaction_detail.php b/app/compta/views/transaction_detail.php index 9cdd649..64fd486 100644 --- a/app/compta/views/transaction_detail.php +++ b/app/compta/views/transaction_detail.php @@ -132,7 +132,19 @@ _EOF; // Update return link $retour = $this->getElementById($xml,"btRetour"); $retour->removeAttribute('link'); - $retour->setAttribute('link',"/app/compta/services.php/".\AEB::$REQUEST['b']); + $back_array = explode(':',\AEB::$REQUEST['b']); + $depth = count($back_array); + if ($depth> 1) + { + $current_back = $back_array[$depth - 1]; + unset( $back_array[$depth - 1] ); + + $b = "?b=".implode(':',$back_array); + $retour->setAttribute('link',"/app/compta/services.php/".$current_back.$b); + }else + { + $retour->setAttribute('link',"/app/compta/services.php/".\AEB::$REQUEST['b']); + } // Ok Handle buttons $panelButtons= $this->getElementById($xml,"pButtons"); $nel = $xml->createDocumentFragment(); @@ -148,21 +160,25 @@ _EOF; $template=<<<_EOF %s -%s +%s %s %s %s _EOF; $frag = ""; $count = 0; + $model = $this->_model; $lst = $this->_model->_mouvements; + //$lst = $this->_model->_entries; foreach( $lst as $row) { + //$row = $row_entry->jsonSerialize(); $debit = $row[4] == 'd'?sprintf("%.2f",$row[5]):0.0; $credit = $row[4] == 'c'?sprintf("%.2f",$row[5]):0.0; $frag.=sprintf($template , ++$count % 2?"odd":"even" ,$row[1] - , $row[2] ,$row[3],$row[6] + , $model->_id,$row[0] ,$row[3] + , $row[6] /* Type */ , $debit == 0.0 ? "" : $debit , $debit > $credit ? "red" : "black" , $credit == 0.0? "" : $credit); diff --git a/app/compta/views/transaction_entry.php b/app/compta/views/transaction_entry.php new file mode 100644 index 0000000..a316441 --- /dev/null +++ b/app/compta/views/transaction_entry.php @@ -0,0 +1,253 @@ + + + + + +Gestion comptable + + + + + + + + +
+ + + + +
+
+ +
+
+XML; +/** + * @brief Update GUI with the list of + * documents available. All decomptes + */ +class Entry extends \PageCompta + implements \IActionResult +{ + /* To fill combo with right selection */ + private $selected_type = 'BQ'; + + function __construct($model) + { + $this->_model = $model; + parent::__construct(XmlAccount,true); + } + + public function render() + { + $this->show(); + } + + /** + * @brief insert a script in the page to update menu link + * + */ + private function script() + { + $s=<<<_EOF + +_EOF; + echo $s; + } + + /** + * @brief allow sub classes to modify xml file + */ + protected function _updateXML(&$xml) + { + $buttons=<<<_EOF + + + +_EOF; + $sum = $this->_model->_summary->jsonSerialize(); + $fields= Array('title','date','voucher_ref','voucher_date'); + $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(); + /* Fill Table with entries */ + $nel->appendXML($this->fillTable()); + $f->appendChild($nel); + /* Update title from xml template */ + foreach (array_keys($fields) as $key) + { + $title = $this->getElementById($xml,$fields[$key]); + $el = $xml->createTextNode(" ".$values[$key]); + $title->appendChild($el); + } + // Update return link + $retour = $this->getElementById($xml,"btRetour"); + $retour->removeAttribute('link'); + $retour->setAttribute('link',"/app/compta/services.php/".\AEB::$REQUEST['b']); + // Ok Handle buttons + $panelButtons= $this->getElementById($xml,"pButtons"); + $nel = $xml->createDocumentFragment(); + $nel->appendXML($buttons); + $panelButtons->appendChild($nel); + } + + /** + * @brief Fill bilan table + */ + private function fillTable() + { + $template=<<<_EOF + +%s +%s +%s +%s +%s +%s +_EOF; + $templateEdit=<<<_EOF + + + +%s + + + +%s + + + %s + + + + + + + + + +_EOF; + $frag = ""; + $count = 0; + $model = $this->_model; + //$lst = $this->_model->_mouvements; + $lst = $this->_model->_entries; +/** + 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 + */ + foreach( $lst as $row) + { + $debit = $row->dc == 'd'?sprintf("%.2f",$row->amount):0.0; + $credit = $row->dc == 'c'?sprintf("%.2f",$row->amount):0.0; + if ($model->isEntryEdit($row)) + { + $entryS0=<<<_EOF + +_EOF; + $entryS1=<<<_EOF + +_EOF; + $types = $model->getEntryTypes(); + $selected = ""; + foreach ($types as $t) + { + if ($t[0] == $row->entry_type) + { + $selected.=sprintf($entryS1,$t[1],$t[0]); + } else + { + $selected.=sprintf($entryS0,$t[1],$t[0]); + } + } + $frag.=sprintf($templateEdit + , ++$count % 2?"odd":"even" + , $model->_id + , $row->id + , $row->entry_date + , $row->acc_id /* Account ID */ + , $row->acc_name + , $selected /* Type */ + , $debit == 0.0 ? "" : $debit + , $debit > $credit ? "red" : "black" + , $credit == 0.0? "" : $credit); + } else + { + $frag.=sprintf($template + , ++$count % 2?"odd":"even" ,$row->entry_date + , $row->acc_id /* Account ID */ + , $model->_id,$row->id ,$row->acc_name + , $row->entry_type /* Type */ + , $debit == 0.0 ? "" : $debit + , $debit > $credit ? "red" : "black" + , $credit == 0.0? "" : $credit); + } + } + return $frag; + } + +} + +?> diff --git a/app/compta/views/transaction_update.php b/app/compta/views/transaction_update.php index bd92184..8f51cc9 100644 --- a/app/compta/views/transaction_update.php +++ b/app/compta/views/transaction_update.php @@ -39,13 +39,13 @@ const xmlTransactionUpdate =<<
- +
- +
- +
- +
@@ -74,7 +74,7 @@ const xmlTransactionUpdate =<< - +