noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_stock_goods.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of NOALYSS.
5  *
6  * NOALYSS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * NOALYSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with NOALYSS; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 // Copyright Author Dany De Bontridder danydb@aevalys.eu
22 
23 /**
24  * @file
25  * @brief Manage the goods
26  *
27  */
28 require_once NOALYSS_INCLUDE.'/database/class_stock_goods_sql.php';
29 
31 {
32 /**
33  * if an array if receive the keys are
34  * p_date
35  * p_depot
36  * f_idX f_id
37  * sg_codeX
38  * sg_type0
39  *
40  * @remark $cn database connx
41  * @param $p_array
42  * if an array if receive the keys are
43  * p_date
44  * p_depot
45  * f_idX f_id
46  * sg_codeX
47  * sg_type0
48  * @param $p_readonly true or false
49  */
50  function input($p_array = null, $p_readonly = false)
51  {
52  global $cn;
53 
54  if ($p_array != null)
55  {
56  extract($p_array, EXTR_SKIP);
57  }
58  else
59  {
60  $p_date = '';
61  $p_motif = '';
62  $p_depot = 0;
63  }
64  $date = new IDate('p_date', $p_date);
65  $date->setReadOnly($p_readonly);
66  $motif = new IText('p_motif', $p_motif);
67  $motif->setReadOnly($p_readonly);
68  $motif->size = 80;
69  $idepo = HtmlInput::select_stock($cn, "p_depot", "W");
70  $idepo->setReadOnly($p_readonly);
71  if (count($idepo->value) == 0)
72  {
73  NoAccess();
74  die();
75  }
76  $idepo->selected = $p_depot;
77  if ($p_readonly ) {
78  $nb=$row;
79  } else {
80  if (isset ($row ) )
81  {
83  }else {
85  }
86  }
87  for ($e = 0; $e < $nb; $e++)
88  {//ATTR_DEF_STOCKfiche_
89  $sg_code[$e] = new ICard('sg_code' . $e);
90  $sg_code[$e]->extra = "[sql] fd_id = 500000";
91  $sg_code[$e]->set_attribute("typecard", $sg_code[$e]->extra);
92  $sg_code[$e]->set_attribute("label", "label" . $e);
93  $sg_code[$e]->value = (isset(${'sg_code' . $e})) ? ${'sg_code' . $e} : '';
94  $sg_quantity[$e] = new INum('sg_quantity' . $e);
95  $sg_quantity[$e]->value = (isset(${'sg_quantity' . $e})) ? ${'sg_quantity' . $e} : '';
96  $label[$e] = new ISpan("label$e");
97  if (trim($sg_code[$e]->value) != '')
98  {
99  $label[$e]->value = $cn->get_value("select vw_name from vw_fiche_attr where quick_code=$1", array($sg_code[$e]->value));
100  }
101  $sg_code[$e]->setReadOnly($p_readonly);
102  $sg_quantity[$e]->setReadOnly($p_readonly);
103  if ( isset (${'sg_type'.$e})) {
104  $sg_type[$e]=(${'sg_type'.$e}=='c')?'OUT':'IN';
105  }
106  if ( isset (${'f_id'.$e})) {
107  $fiche[$e]=new Fiche($this->cn,${'f_id'.$e});
108  }
109  }
110  $select_exercice=new ISelect('p_exercice');
111  $select_exercice->value=$cn->make_array('select distinct p_exercice,p_exercice from parm_periode order by 1 desc');
112 
113  require_once NOALYSS_TEMPLATE.'/stock_inv.php';
114  }
115 
117  {
118  global $cn;
119  try
120  {
121  if (isDate($p_array['p_date']) == null)
122  throw new Exception('Date invalide');
123  $cn->start();
124  $ch = new Stock_Change_Sql($cn);
125  $ch->setp("c_comment", $p_array['p_motif']);
126  $ch->setp("r_id", $p_array['p_depot']);
127  $ch->setp("c_date", $p_array['p_date']);
128  $ch->setp('tech_user', $_SESSION['g_user']);
129  $ch->insert();
130  $per = new Periode($cn);
131  $periode = $per->find_periode($p_array['p_date']);
132  $exercice = $per->get_exercice($periode);
133  $nb=$p_array['row'];
134  for ($i = 0; $i < $nb; $i++)
135  {
136  $a = new Stock_Goods_Sql($cn);
137  if ($p_array['sg_quantity' . $i] != 0 &&
138  trim($p_array['sg_code' . $i]) != '')
139  {
140  $stock= strtoupper(trim($p_array['sg_code' . $i]));
141  $fiche=new Fiche($cn);
142  $fiche->get_by_qcode($p_array['sg_code' . $i]);
143  /*
144  * check if code stock does exist
145  */
146  $count=$cn->get_value('select count(*) from fiche_detail where ad_id=$1 and ad_value=$2',
147  array(ATTR_DEF_STOCK,$stock));
148  if ( $count==0) {
149  throw new Exception(_("Code stock inutilisé").h($stock));
150  }
151  $a->f_id=$fiche->id;
152  $a->sg_code = $stock;
153  $a->sg_quantity = abs($p_array['sg_quantity' . $i]);
154  $a->sg_type = ($p_array['sg_quantity' . $i] > 0) ? 'd' : 'c';
155  $a->sg_comment = $p_array['p_motif'];
156  $a->tech_user = $_SESSION['g_user'];
157  $a->r_id = $p_array['p_depot'];
158  $a->sg_exercice = $exercice;
159  $a->c_id = $ch->c_id;
160  $a->sg_date=$p_array['p_date'];
161  $a->insert();
162  }
163  }
164  $cn->commit();
165  }
166  catch (Exception $exc)
167  {
168  error_log( $exc->getTraceAsString());
169  echo $exc->getMessage();
170  throw $exc;
171  }
172  }
173  /**
174  * Insert into stock_goods from ACH and VEN
175  * @param type $p_array KEY : db => database conx, j_id => jrnx.j_id,goods=> f_id of the goods
176  * 'quant' => quantity ,'dir'=> d or c (c for sales OUT and d for purchase IN),'repo'=>r_id of the
177  * repository (stock_repository.r_id
178  */
179  static function insert_goods(&$p_cn,$p_array)
180  {
181  global $g_user;
182  extract ($p_array, EXTR_SKIP);
183  if ($g_user->can_write_repo($repo) == false)
184  return false;
185 
186  // Retrieve the good account for stock
187  $code = new Fiche($p_cn);
188  $code->get_by_qcode($goods);
189  $code_marchandise = $code->strAttribut(ATTR_DEF_STOCK);
190  if ($code_marchandise == NOTFOUND || $code_marchandise=='')
191  return false;
192 
193  $exercice = $g_user->get_exercice();
194 
195  if ($exercice == 0)
196  throw new Exception('Annee invalide erreur');
197 
198  $Res = $p_cn->exec_sql("insert into stock_goods (
199  j_id,
200  f_id,
201  sg_code,
202  sg_quantity,
203  sg_type,sg_exercice,r_id ) values ($1,$2,$3,$4,$5,$6,$7)", array(
204  $p_array['j_id'],
205  $code->id,
206  $code_marchandise,
207  $p_array['quant'],
208  $p_array['dir'],
209  $exercice,
210  $p_array['repo']
211  )
212  );
213  return $Res;
214  }
215  /**
216  * Return an array, used by Stock_Goods::input
217  * @remark type $cn
218  * @param type $p_array
219  * @throws Exception
220  */
222  {
223  global $cn;
224  $year=HtmlInput::default_value("p_exercice", "", $p_array);
225  $depot=HtmlInput::default_value("p_depot", "", $p_array);
226  if ($year=="")
227  throw new Exception(_('Inventaire invalide'), 10);
228  if ($depot=="")
229  throw new Exception(_('Dépôt invalide'), 20);
230 
231  // compute state_exercice
232  $periode=new Periode($cn);
233  $periode->p_id=$cn->get_value("select min(p_id) from parm_periode where p_exercice=$1", array($year));
234  $first_day=$periode->first_day();
235 
236  // compute array for stock
237  $array['state_exercice']=$first_day;
238 
239  $stock=new Stock($cn);
240  $rowid=$stock->build_tmp_table($array);
241 
242  // compute first day of the next year
243  $next_year=$year+1;
244  $periode=new Periode($cn);
245  $periode->p_id=$cn->get_value("select min(p_id) from parm_periode where p_exercice=$1", array($next_year));
246 
247  if ($periode->p_id=="")
248  $array['p_date']="";
249  else
250  $array['p_date']=$periode->first_day();
251 
252  // Compute an array compatible with Stock_Goods::input
253  $array['p_motif']=_('Inventaire ').$year;
254  $array['p_depot']=$depot;
255 
256  $result=$cn->get_array("
257  select sg_code,sum(coalesce(s_qin,0)-coalesce(s_qout,0)) tot_
258  from tmp_stockgood_detail
259  where
260  s_id=$1 and r_id=$2
261  group by sg_code",
262  array($rowid,$depot));
263  for ($e=0;$e< count($result);$e++) {
264  $array['sg_code'.$e]=$result[$e]['sg_code'];
265  $array['sg_quantity'.$e]=$result[$e]['tot_'];
266  }
267  $array['row']=$e;
268  return $array;
269 
270  }
271 }
272 
273 ?>
$stock
NoAccess($js=1)
Echo no access and stop.
Definition: ac_common.php:426
input($p_array=null, $p_readonly=false)
if an array if receive the keys are p_date p_depot f_idX f_id sg_codeX sg_type0
record_save($p_array)
const ATTR_DEF_STOCK
Definition: constant.php:195
take_last_inventory($p_array)
Return an array, used by Stock_Goods::input.
$code
For the periode tables parm_periode and jrn_periode.
define Class fiche and fiche def, those class are using class attribut. When adding or modifing new c...
Definition: class_fiche.php:44
static select_stock($p_cn, $p_name, $p_mode)
Create an ISelect object containing the available repository for reading or writing.
$poste extra
global $g_user
Find the default module or the first one.
Definition: action.inc.php:24
count($p_where="", $p_array=null)
function trim(s)
remove trailing and heading space
Definition: scripts.js:95
$input_from cn
Definition: balance.inc.php:71
h($p_string)
to protect again bad characters which can lead to a cross scripting attack the string to be diplayed ...
Definition: ac_common.php:38
$count
Definition: modele.inc.php:255
isDate($p_date)
Definition: ac_common.php:223
Input HTML for the card show buttons, in the file, you have to add card.js How to use : ...
static insert_goods(&$p_cn, $p_array)
Insert into stock_goods from ACH and VEN.
static default_value($ind, $default, $array)
return default if the value if the value doesn't exist in the array
const MAX_ARTICLE_STOCK
Definition: constant.php:117
if(isset($_REQUEST['gDossier'])&&$_REQUEST['gDossier']<>0) $repo
const NOTFOUND
Definition: constant.php:114
Html Input : Input a date format dd.mm.yyyy The property title should be set to indicate what it is e...
Definition: class_idate.php:31
This class handles only the numeric input, the input will call a javascript to change comma to period...
Definition: class_inum.php:40