Plugins  LAST
 All Data Structures Files Functions Variables Pages
modop_save.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 save the operation
25  */
26 require_once NOALYSS_INCLUDE.'/class/class_acc_ledger_purchase.php';
27 require_once NOALYSS_INCLUDE.'/class/class_acc_ledger_sold.php';
28 require_once('class_modop_operation.php');
29 /* ---------------------------------------------------------------------- */
30 // Purchase
31 /* ---------------------------------------------------------------------- */
32 if ( $_POST['jrn_type'] == 'ACH')
33 {
34  $jrn=new Acc_Ledger_Purchase($cn,$_POST['p_jrn']);
35  try
36  {
37  $op=new Modop_Operation($cn,$_POST['ext_jr_internal']);
38  $op->suspend_receipt();
39  $op->suspend_strict();
40  $pj=$_POST['e_pj'];
41  $oldpj= microtime();
42  $cn->exec_sql('update jrn set jr_pj_number=$1 where jr_id=$2',
43  array($oldpj,$_POST['ext_jr_id']));
44  $new_internal=$jrn->insert($_POST);
45  }
46  catch (Exception $e)
47  {
48  alert($e->getMessage());
49  exit();
50  }
51  $cn->commit();
52 
53  /* we delete the old operation */
54  $cn->start();
55  /* in stock_goods */
56  $cn->exec_sql('delete from stock_goods where j_id in (select j_id from jrnx join jrn on (j_grpt=jr_grpt_id) where jr_id=$1)',
57  array($_POST['ext_jr_id']));
58 
59  /* in jrnx */
60  $cn->exec_sql('delete from jrnx where j_grpt in (select jr_grpt_id from jrn where jr_id=$1)',
61  array($_POST['ext_jr_id']));
62 
63  /* in jrn, retrieve info to reattach the receipt */
64  $attach=$cn->get_array('select jr_pj,jr_pj_name,jr_pj_type from jrn where jr_id=$1',
65  array($_POST['ext_jr_id']));
66 
67 
68  // delete old data from jrn
69  $cn->exec_sql('delete from jrn where jr_id=$1',array($_POST['ext_jr_id']));
70 
71  $cn->exec_sql('update jrn set jr_id=$1,jr_internal=$2,jr_pj_number=$3 where jr_internal=$4',
72  array($_POST['ext_jr_id'],$_POST['ext_jr_internal'],$pj,$new_internal));
73  if ( $attach[0]['jr_pj_name'] != '' && ! isset ($_POST['gen_invoice']))
74  {
75  $cn->exec_sql('update jrn set jr_pj=$1,jr_pj_type=$2,jr_pj_name=$3 where jr_id=$4',
76  array($attach[0]['jr_pj'],$attach[0]['jr_pj_type'],$attach[0]['jr_pj_name'],$_POST['ext_jr_id']));
77  }
78  /* in quant_purchase */
79  $cn->exec_sql('update quant_purchase set qp_internal=$1 where qp_internal=$2',
80  array($_POST['ext_jr_internal'],$new_internal));
81 
82  $cn->commit();
83  echo '<h2 class="info"> '._('Enregistrement').' </h2>';
84  echo "<h2 >" . _('Opération sauvée') .$_POST['ext_jr_internal'] ;
85  if ($jrn->pj != '')
86  echo ' Piece : ' . h($jrn->pj);
87  echo "</h2>";
88  if (strcmp($jrn->pj, $_POST['e_pj']) != 0)
89  {
90  echo '<h3 class="notice"> ' . _('Attention numéro pièce existante, elle a du être adaptée') . '</h3>';
91  }
92  /* Save the additional information into jrn_info */
93  $obj = new Acc_Ledger_Info($cn);
94  $obj->save_extra($jrn->jr_id, $_POST);
95  printf('<a class="line" style="display:inline" href="javascript:modifyOperation(%d,%d)">%s</a><hr>', $_POST['ext_jr_id'], dossier::id(), $new_internal);
96  // Feedback
97  $jrn->internal=$_POST['ext_jr_internal'];
98  echo $jrn->confirm($_POST, true);
99  if (isset($jrn->doc))
100  {
101  echo '<span class="invoice">';
102  echo $jrn->doc;
103  echo '</span>';
104  }
105 
106  echo '</div>';
107 }
108 /* ---------------------------------------------------------------------- */
109 // SOLD
110 /* ---------------------------------------------------------------------- */
111 if ( $_POST['jrn_type'] == 'VEN')
112 {
113  $jrn=new Acc_Ledger_Sold($cn,$_POST['p_jrn']);
114  $pj=$_POST['e_pj'];
115  try
116  { $cn->start();
117  $op=new Modop_Operation($cn,$_POST['ext_jr_internal']);
118  $op->suspend_receipt();
119  $op->suspend_strict();
120 
121  $oldpj= microtime();
122  $cn->exec_sql('update jrn set jr_pj_number=$1 where jr_id=$2',
123  array($oldpj,$_POST['ext_jr_id']));
124  $new_internal=$jrn->insert($_POST);
125  }
126  catch (Exception $e)
127  {
128  alert($e->getMessage());
129  exit();
130  }
131  $cn->commit();
132  /* we delete the old operation */
133  $cn->start();
134 
135  /* in stock_goods */
136  $cn->exec_sql('delete from stock_goods where j_id in (select j_id from jrnx join jrn on (j_grpt=jr_grpt_id) where jr_id=$1)',
137  array($_POST['ext_jr_id']));
138 
139  /* in jrnx */
140  $cn->exec_sql('delete from jrnx where j_grpt in (select jr_grpt_id from jrn where jr_id=$1)',
141  array($_POST['ext_jr_id']));
142 
143  /* in jrn */
144  $attach=$cn->get_array('select jr_pj,jr_pj_name,jr_pj_type from jrn where jr_id=$1',
145  array($_POST['ext_jr_id']));
146 
147  $cn->exec_sql('delete from jrn where jr_id=$1',array($_POST['ext_jr_id']));
148  $cn->exec_sql('update jrn set jr_id=$1,jr_internal=$2,jr_pj_number=$3 where jr_internal=$4',
149  array($_POST['ext_jr_id'],$_POST['ext_jr_internal'],$pj,$new_internal));
150 
151  if ( $attach[0]['jr_pj_name'] != '' && ! isset ($_POST['gen_invoice']))
152  {
153  $cn->exec_sql('update jrn set jr_pj=$1,jr_pj_type=$2,jr_pj_name=$3 where jr_id=$4',
154  array($attach[0]['jr_pj'],$attach[0]['jr_pj_type'],$attach[0]['jr_pj_name'],$_POST['ext_jr_id']));
155  }
156 
157  /* in quant_sold */
158  $cn->exec_sql('update quant_sold set qs_internal=$1 where qs_internal=$2',
159  array($_POST['ext_jr_internal'],$new_internal));
160 
161  $cn->commit();
162  /* Show button */
163  echo '<h2 class="info"> Enregistrement </h2>';
164  $jr_id=$_POST['ext_jr_id'];
165 
166  echo "<h2 >"._('Opération sauvée');
167  if ( $jrn->pj != '') echo ' Piece : '.h($jrn->pj);
168  echo "</h2>";
169  if ( strcmp($jrn->pj,$_POST['e_pj']) != 0 )
170  {
171  echo '<h3 class="notice"> '._('Attention numéro pièce existante, elle a du être adaptée').'</h3>';
172  }
173 
174  printf ('<a class="line" style="display:inline" href="javascript:modifyOperation(%d,%d)">%s</a><hr>',
175  $jr_id,dossier::id(),$new_internal);
176  $jrn->internal=$_POST['ext_jr_internal'];
177  echo $jrn->confirm($_POST,true);
178  /* Show link for Invoice */
179  if (isset ($jrn->doc) )
180  {
181  echo '<span class="invoice">';
182  echo $jrn->doc;
183  echo '</span>';
184  }
185 
186 
187  /* Save the additional information into jrn_info */
188  $obj=new Acc_Ledger_Info($cn);
189  $obj->save_extra($jr_id,$_POST);
190 
191 
192  echo '</div>';
193 }
194 /* ---------------------------------------------------------------------- */
195 // ODS
196 /* ---------------------------------------------------------------------- */
197 if ( $_POST['jrn_type'] == 'ODS')
198 {
199  $jrn=new Acc_Ledger($cn,$_POST['p_jrn']);
200  try
201  {
202  $op=new Modop_Operation($cn,$_POST['ext_jr_internal']);
203  $op->suspend_receipt();
204  $op->suspend_strict();
205  $pj=$_POST['e_pj'];
206  $_POST['e_pj']=microtime();
207  $jrn->save($_POST);
208  $new_internal=$jrn->internal;
209  }
210  catch (Exception $e)
211  {
212  alert($e->getMessage());
213  exit();
214  }
215  /* we delete the old operation */
216  $cn->start();
217 
218  /* in jrnx */
219  $cn->exec_sql('delete from jrnx where j_grpt in (select jr_grpt_id from jrn where jr_id=$1)',
220  array($_POST['ext_jr_id']));
221 
222  /* in jrn */
223  $attach=$cn->get_array('select jr_pj,jr_pj_name,jr_pj_type from jrn where jr_id=$1',
224  array($_POST['ext_jr_id']));
225  $cn->exec_sql('delete from jrn where jr_id=$1',array($_POST['ext_jr_id']));
226  $cn->exec_sql('update jrn set jr_id=$1,jr_internal=$2,jr_pj_number=$3 where jr_internal=$4',
227  array($_POST['ext_jr_id'],$_POST['ext_jr_internal'],$pj,$new_internal));
228  if ( $attach[0]['jr_pj_name'] != '')
229  {
230  $cn->exec_sql('update jrn set jr_pj=$1,jr_pj_type=$2,jr_pj_name=$3 where jr_id=$4',
231  array($attach[0]['jr_pj'],$attach[0]['jr_pj_type'],$attach[0]['jr_pj_name'],$_POST['ext_jr_id']));
232  }
233 
234  $cn->commit();
235 
236 }
237 
238 /* ---------------------------------------------------------------------- */
239 // Purchase
240 /* ---------------------------------------------------------------------- */
241 if ( $_POST['jrn_type'] == 'FIN')
242 {
243  extract ($_POST);
244  $user=new User($cn);
245  try
246  {
247  /* verify if the card can be used in this ledger */
248  if ( $user->check_jrn($p_jrn) != 'W' )
249  throw new Exception (_('Accès interdit'),20);
250  /* check if there is a customer */
251  if ( strlen(trim($e_bank_account)) == 0 )
252  throw new Exception(_('Vous n\'avez pas donné de banque'),11);
253 
254  /* check if the date is valid */
255  if ( isDate($e_date) == null )
256  {
257  throw new Exception('Date invalide', 2);
258  }
259  $fiche=new fiche($cn);
260  $fiche->get_by_qcode($e_bank_account);
261  if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
262  throw new Exception('La fiche '.$e_bank_account.'n\'a pas de poste comptable',8);
263  if ( $fiche->belong_ledger($p_jrn,'cred') !=1 && $fiche->belong_ledger($p_jrn,'deb') !=1 )
264  throw new Exception('La fiche '.$e_bank_account.'n\'est pas accessible à ce journal',10);
265  $fiche=new fiche($cn);
266  $fiche->get_by_qcode($e_other);
267  if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
268  throw new Exception('La fiche '.$e_other.'n\'a pas de poste comptable',8);
269  if ( $fiche->belong_ledger($p_jrn,'deb') !=1 )
270  throw new Exception('La fiche '.$e_other.'n\'est pas accessible à ce journal',10);
271  if ( isNumber($ {'e_other_amount'}) == 0 )
272  throw new Exception('La fiche '.$e_other.'a un montant invalide ['.$e_other_amount.']',6);
273  }
274  catch (Exception $e)
275  {
276  echo $e->getMessage();
277  exit();
278  }
279 
280  try
281  {
282  $cn->start();
283  /* find periode thanks the date */
284  $periode=new Periode($cn);
285  $periode->find_periode($e_date);
286  if ($periode->is_closed())
287  throw new Exception ('Période fermée');
288 
289  /* update amount */
290  $cn->exec_sql("update jrnx set j_montant=$1,j_jrn_def=$3,j_date=to_date($4,'DD.MM.YYYY'),j_tech_per=$5,j_tech_date=now() where j_grpt in (select jr_grpt_id from jrn where jr_id=$2)",array(abs($e_other_amount),$ext_jr_id,$p_jrn,$e_date,$periode->p_id));
291 
292 
293  /* in jrn */
294  $cn->exec_sql("update jrn set jr_montant=$1,jr_comment=$2,jr_date=to_date($3,'DD.MM.YYYY'),jr_def_id=$4,jr_tech_per=$5,jr_pj_number=$6,jr_tech_date=now() where jr_id=$7",
295  array(abs($e_other_amount),$e_other_comment,$e_date,$p_jrn,$periode->p_id,$e_pj,$ext_jr_id));
296  /* in quant_fin */
297  /* find the f_id of the bank */
298  $fbank=new Fiche($cn);
299  $fbank->get_by_qcode($e_bank_account);
300  $post_bank=$fbank->strAttribut(ATTR_DEF_ACCOUNT);
301 
302  $fother=new Fiche($cn);
303  $fother->get_by_qcode($e_other);
304  $post_other=$fother->strAttribut(ATTR_DEF_ACCOUNT);
305  if ($e_other_amount > 0 )
306  {
307  $cn->exec_sql('update jrnx set j_poste=$1,j_qcode=$2 where j_debit=false and j_grpt in (select jr_grpt_id from jrn where jr_id=$3)',array($post_other,$e_other,$ext_jr_id));
308  $cn->exec_sql('update jrnx set j_poste=$1,j_qcode=$2 where j_debit=true and j_grpt in (select jr_grpt_id from jrn where jr_id=$3)',array($post_bank,$e_bank_account,$ext_jr_id));
309  }
310  else
311  {
312  $cn->exec_sql('update jrnx set j_poste=$1,j_qcode=$2 where j_debit=false and j_grpt in (select jr_grpt_id from jrn where jr_id=$3)',array($post_bank,$e_bank_account,$ext_jr_id));
313  $cn->exec_sql('update jrnx set j_poste=$1,j_qcode=$2 where j_debit=true and j_grpt in (select jr_grpt_id from jrn where jr_id=$3)',array($post_other,$e_other,$ext_jr_id));
314  }
315  $cn->exec_sql('update quant_fin set qf_bank=$1,qf_amount=$3,qf_other=$2 where jr_id=$4',array($fbank->id,$fother->id,$e_other_amount,$ext_jr_id));
316  $cn->commit();
317  }
318  catch (Exception $e)
319  {
320  $cn->rollback();
321  echo $e->getMessage();
322  exit();
323  }
324  echo h2info('Opération sauvée');
325 }
$select_type id
$periode
if($oPeriode->is_closed()==1) $op
global $cn