Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_ext_list_assujetti.php
Go to the documentation of this file.
1 <?php
2  /*
3  * This file is part of NOALYSS.
4  *
5  * NOALYSS is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * NOALYSS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with NOALYSS; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  /* $Revision$ */
20 
21  // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
22 
23  /*!\file
24  * \brief
25  */
26 
27 require_once('class_listing.php');
28 require_once(NOALYSS_INCLUDE.'/class/class_fiche.php');
29 require_once('class_tva_parameter.php');
30 class Ext_List_Assujetti extends Listing {
31  protected $variable = array(
32  "id"=>"a_id",
33  "date_decl"=>"date_decl",
34  "start_periode"=>"start_periode",
35  "end_periode"=>"end_periode",
36  "xml_file"=>"xml_file",
37  "num_tva"=>"num_tva",
38  "name"=>"tva_name",
39  "adress"=>"adress",
40  "country"=>"country",
41  "flag_periode"=>"flag_periode",
42  "exercice"=>"exercice",
43  "periode_dec"=>"periode_dec"
44  );
45  private $aChild=array();
46  static function choose_periode($by_year=false) {
47 
48  // year
49  $year = new IText('year');
50  $year->size=4;
51  $str_year=$year->input();
52 
53 
54  $str_submit=HtmlInput::submit('decl',_('Afficher'));
55  $str_hidden=HtmlInput::extension().dossier::hidden();
56  if (isset($_REQUEST['sa']))
57  $str_hidden.=HtmlInput::hidden('sa',$_REQUEST['sa']);
58  $r='<form class="print" method="get">';
59  $r.="Année :".$str_year;
60  $r.=$str_submit;
61  $r.=$str_hidden;
62  $r.=HtmlInput::request_to_hidden(array('ac'));
63  $r.='</form>';
64  return $r;
65  }
66 
67  function from_array($p_array){
68  if ( isset($p_array['name_child'])) {
69  $name=$p_array['name_child'];
70  $qcode=$p_array['qcode'];
71  $code=$p_array['plugin_code'];
72  $tva_num=$p_array['tva_num_child'];
73  $amount=$p_array['amount'];
74  $amount_vat=$p_array['amount_vat'];
75 
76  // retrieve missing and compute an array
77  for ($i=0;$i<count($name);$i++){
78  $child=new Ext_List_Assujetti_Child($this->db);
79  $child->set_parameter('amount',$amount[$i]);
80  $child->set_parameter('amount_vat',$amount_vat[$i]);
81  $child->set_parameter('qcode',$qcode[$i]);
82  $child->set_parameter('name_child',$name[$i]);
83  $child->set_parameter('tva_num',$tva_num[$i]);
84 
85  $array[]=$child;
86  }//end for
87  $this->aChild=$array;
88  } else
89  $this->aChild=array();
90 
91  $this->start_periode=$p_array['start_periode'];
92  $this->end_periode=$p_array['end_periode'];
93  $this->flag_periode=$p_array['flag_periode'];
94  $this->tva_name=$p_array['name'];
95  $this->num_tva=$p_array['num_tva'];
96  $this->adress=$p_array['adress'];
97  $this->country=$p_array['country'];
98  $this->periode_dec=$p_array['periode_dec'];
99  $this->exercice=$p_array['exercice'];
100  }
101  function display() {
102  $r= '<form class="print" id="readonly">';
103  $r.=HtmlInput::request_to_hidden(array('ac'));
104  $r.=$this->display_info();
105  $r.=$this->display_declaration_amount(true);
106  $r.='</form>';
107  $js_remove=sprintf("onclick=\"if ( confirm('%s')){remove_form('%s',%d,%d,'lc');}\"",
108  "Vous confirmez vouloir effacer ?",
109  $_REQUEST['plugin_code'],
110  dossier::id(),
111  $this->a_id);
112  $r.=HtmlInput::button('Effacer','Effacer',$js_remove);
113 
114  $r.= create_script("$('readonly').disable();");
115  return $r;
116  }
117  function load() {
118  $sql="select * from tva_belge.assujetti where a_id=$1";
119 
120  $res=$this->db->get_array(
121  $sql,
122  array($this->a_id)
123  );
124  if ( $this->db->count() == 0 ) return 0;
125  foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
126  // load child
127  $sql="select * from tva_belge.assujetti_chld where a_id=$1";
128  $res=$this->db->get_array(
129  $sql,
130  array($this->a_id)
131  );
132  $nb=$this->db->count();
133  $array=array();
134  // retrieve missing and compute an array
135  for ($i=0;$i<$nb;$i++){
136  $child=new Ext_List_Assujetti_Child($this->db);
137  foreach ($res[$i] as $idx=>$value){
138  $child->$idx=$value;
139  }
140  $array[]=$child;
141  }//end for
142  $this->aChild=$array;
143 
144  return 1;
145  }
146  function verify() {
147  return 0;
148  }
149  function insert() {
150  $this->db->start();
151 
152  /* insert into the first table */
153  $sql=<<<EOF
154  INSERT INTO tva_belge.assujetti(
155  start_date, end_date, periodicity, tva_name,
156  num_tva, adress, country, periode_dec,exercice)
157  VALUES (to_date($1,'DD.MM.YYYY'),to_date($2,'DD.MM.YYYY'),$3,$4,$5,$6,$7,$8,$9) returning a_id;
158 EOF;
159  $this->a_id=$this->db->get_value($sql,
160  array(
161  $this->start_periode, /* 1 */
162  $this->end_periode,
163  $this->flag_periode, /* 3 */
164  $this->tva_name,
165  $this->num_tva, /* 5 */
166  $this->adress,
167  $this->country, /* 7 */
168  $this->periode_dec,
169  $this->exercice /* 9 */
170  )
171  );
172  /* insert into the child table */
173  for ($e=0;$e<count($this->aChild);$e++){
174  $this->aChild[$e]->set_parameter('depend',$this->a_id);
175  $this->aChild[$e]->insert();
176  }
177  $this->db->commit();
178 
179  }
180  function update() {
181  }
182 
183  function compute(){
184  /* retrieve accounting customer */
185  $code_customer=new Acc_Parm_Code($this->db);
186  $code_customer->p_code='CUSTOMER';
187  $code_customer->load();
188  $a=$this->find_tva_code('ASSUJETTI');
189  if (trim($a)=='') $a=-1;
190  $sql=<<<EOF
191  select sum(j_montant) as amount,j_qcode
192  from jrnx
193  where j_grpt in (select distinct j_grpt from quant_sold join jrnx using (j_id) where qs_vat_code in ($a) )
194  and j_poste::text like $1||'%'
195  and (j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date($3,'DD.MM.YYYY'))
196  group by j_qcode
197 EOF;
198  // get all of them
199  $all=$this->db->get_array($sql,array($code_customer->p_value,
200  $this->start_periode,
201  $this->end_periode
202  )
203  );
204  $array=array();
205 
206  // retrieve missing and compute an array
207  for ($i=0;$i<count($all);$i++){
208  $child=new Ext_List_Assujetti_Child($this->db);
209 
210 
211  $child->set_parameter('qcode',$all[$i]['j_qcode']);
212  $fiche=new Fiche($this->db);
213  $fiche->get_by_qcode($all[$i]['j_qcode'],false);
214  $num_tva=$fiche->strAttribut(ATTR_DEF_NUMTVA);
215  $child->set_parameter('tva_num',$num_tva);
216  $sq="select sum(qs_vat) from quant_sold
217 where qs_client = $1 and j_id in (select distinct j_id from jrnx where j_date >= to_date($2,'DD.MM.YYYY')
218  and j_date <= to_date($3,'DD.MM.YYYY')
219  )
220  and qs_vat_code in ($a)
221 ";
222  // if in the same operation, we use 2 different tva code, the amount is incorrect if one of them is
223  // excluded
224  $exclude="select coalesce(sum(qs_price),0) from quant_sold
225 where qs_client = $1 and j_id in (select distinct j_id from jrnx where j_date >= to_date($2,'DD.MM.YYYY')
226  and j_date <= to_date($3,'DD.MM.YYYY')
227  )
228  and qs_vat_code not in ($a)
229 ";
230  $excl=$this->db->get_value($exclude,array($fiche->id,
231  $this->start_periode,
232  $this->end_periode));
233  $amount_vat=$this->db->get_value($sq,array($fiche->id,
234  $this->start_periode,
235  $this->end_periode));
236  $amount=$all[$i]['amount']-$amount_vat-$excl;
237  $child->set_parameter('amount',$amount);
238  $child->set_parameter('amount_vat',$amount_vat);
239 
240  $child->set_parameter('name_child',$fiche->strAttribut(ATTR_DEF_NAME));
241 
242  $array[]=$child;
243  }//end for
244  $this->aChild=$array;
245  }
246  /**
247  *@todo finish it
248  */
249  function display_declaration_amount($readonly=false) {
250  $res='<fieldset><legend>Listing</legend>';
251  $res.= '<table id="tb_dsp" class="result" style="width:80%;">';
252  $clean=new IButton('rm_b');
253  $clean->label='Efface ligne';
254  $clean->javascript="deleteRow('tb_dsp',this);";
255 
256  $r='';
257  $r.=th('QuickCode');
258  $r.=th('Name');
259  $r.=th('Code Pays et numéro de TVA');
260  $r.=th('montant CA');
261  $r.=th('montant TVA');
262  $r.=th('');
263  $amount=0;$amount_vat=0;
264  $res.=tr($r);
265  for ($i=0;$i<count($this->aChild);$i++) {
266  $a=new IText('qcode[]',$this->aChild[$i]->get_parameter('qcode'));
267  $b=new IText('name_child[]',$this->aChild[$i]->get_parameter('name_child'));
268  $c=new IText('tva_num_child[]',$this->aChild[$i]->get_parameter('tva_num'));
269  $e=new INum('amount[]',$this->aChild[$i]->get_parameter('amount'));
270  $d=new INum('amount_vat[]',$this->aChild[$i]->get_parameter('amount_vat'));
271 
272  $amount+=round($this->aChild[$i]->get_parameter('amount'),2);
273  $amount_vat+=round($this->aChild[$i]->get_parameter('amount_vat'),2);
274  $r=td($a->input());
275  $r.=td($b->input());
276  $r.=td($c->input());
277  $r.=td($e->input());
278  $r.=td($d->input());
279  if (! $readonly) $r.=td($clean->input());
280  $res.=tr($r);
281 
282  }
283  if ( $readonly )
284  {
285  $r=td('');
286  $r.=td('');
287  $r.=td(hb('Total'));
288  $r.=td(hb(sprintf('%.02f',$amount)));
289  $r.=td(hb(sprintf('%.02f',$amount_vat)));
290  $res.=tr($r);
291  }
292 
293  $res.='</table>';
294  $res.='</fieldset>';
295  return $res;
296  }
297 }
298 
300  protected $variable=array(
301  "id"=>"ac_id",
302  "tva_num"=>"ac_tvanum",
303  "amount"=>"ac_amount",
304  "amount_vat"=>"ac_vat",
305  "depend"=>"a_id",
306  "qcode"=>"ac_qcode",
307  "name_child"=>'ac_name'
308  );
309  function insert() {
310 
311  $sql=<<<EOF
312  INSERT INTO tva_belge.assujetti_chld(
313  a_id, ac_tvanum, ac_amount, ac_vat, ac_qcode,
314  ac_name)
315  VALUES ($1, $2, $3, $4, $5, $6) returning ac_id;
316 EOF;
317  $this->ic_id=$this->db->get_value($sql,array(
318  $this->a_id,
319  $this->ac_tvanum,
320  $this->ac_amount,
321  $this->ac_vat,
322  $this->ac_qcode,
323  $this->ac_name
324  ));
325  }
326 
327 }
static choose_periode($by_year=false)
$select_type id
$r
display_info()
display the information about the company
if(isset($_POST['remove'])) $array
find_tva_code($p_array)
$res
$_REQUEST['sb']
Definition: am_print.php:39
$sql
display_declaration_amount($readonly=false)
get_parameter($p_string)
if($p_number->value==0) l les annuités seront recalculées et l pct[] ad_year[] select h_amount from amortissement amortissement_histo where a_id
$name