00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 require_once 'class_stock_goods_sql.php';
00029
00030 class Stock_Goods extends Stock_Goods_Sql
00031 {
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 function input($p_array = null, $p_readonly = false)
00051 {
00052 global $cn;
00053
00054 if ($p_array != null)
00055 {
00056 extract($p_array);
00057 }
00058 else
00059 {
00060 $p_date = '';
00061 $p_motif = '';
00062 $p_depot = 0;
00063 }
00064 $date = new IDate('p_date', $p_date);
00065 $date->setReadOnly($p_readonly);
00066 $motif = new IText('p_motif', $p_motif);
00067 $motif->setReadOnly($p_readonly);
00068 $motif->size = 80;
00069 $idepo = HtmlInput::select_stock($cn, "p_depot", "W");
00070 $idepo->setReadOnly($p_readonly);
00071 if (count($idepo->value) == 0)
00072 {
00073 NoAccess();
00074 die();
00075 }
00076 $idepo->selected = $p_depot;
00077 if ($p_readonly ) {
00078 $nb=$row;
00079 } else {
00080 if (isset ($row ) )
00081 {
00082 $nb=($row > MAX_ARTICLE_STOCK)?$row:MAX_ARTICLE_STOCK;
00083 }else {
00084 $nb=MAX_ARTICLE_STOCK;
00085 }
00086 }
00087 for ($e = 0; $e < $nb; $e++)
00088 {
00089 $sg_code[$e] = new ICard('sg_code' . $e);
00090 $sg_code[$e]->extra = "[sql] fd_id = 500000";
00091 $sg_code[$e]->set_attribute("typecard", $sg_code[$e]->extra);
00092 $sg_code[$e]->set_attribute("label", "label" . $e);
00093 $sg_code[$e]->value = (isset(${'sg_code' . $e})) ? ${'sg_code' . $e} : '';
00094 $sg_quantity[$e] = new INum('sg_quantity' . $e);
00095 $sg_quantity[$e]->value = (isset(${'sg_quantity' . $e})) ? ${'sg_quantity' . $e} : '';
00096 $label[$e] = new ISpan("label$e");
00097 if (trim($sg_code[$e]->value) != '')
00098 {
00099 $label[$e]->value = $cn->get_value("select vw_name from vw_fiche_attr where quick_code=$1", array($sg_code[$e]->value));
00100 }
00101 $sg_code[$e]->setReadOnly($p_readonly);
00102 $sg_quantity[$e]->setReadOnly($p_readonly);
00103 if ( isset (${'sg_type'.$e})) {
00104 $sg_type[$e]=(${'sg_type'.$e}=='c')?'OUT':'IN';
00105 }
00106 if ( isset (${'f_id'.$e})) {
00107 $fiche[$e]=new Fiche($this->cn,${'f_id'.$e});
00108 }
00109 }
00110 $select_exercice=new ISelect('p_exercice');
00111 $select_exercice->value=$cn->make_array('select distinct p_exercice,p_exercice from parm_periode order by 1 desc');
00112
00113 require_once 'template/stock_inv.php';
00114 }
00115
00116 function record_save($p_array)
00117 {
00118 global $cn;
00119 try
00120 {
00121 if (isDate($p_array['p_date']) == null)
00122 throw new Exception('Date invalide');
00123 $cn->start();
00124 $ch = new Stock_Change_Sql($cn);
00125 $ch->setp("c_comment", $p_array['p_motif']);
00126 $ch->setp("r_id", $p_array['p_depot']);
00127 $ch->setp("c_date", $p_array['p_date']);
00128 $ch->setp('tech_user', $_SESSION['g_user']);
00129 $ch->insert();
00130 $per = new Periode($cn);
00131 $periode = $per->find_periode($p_array['p_date']);
00132 $exercice = $per->get_exercice($periode);
00133 $nb=$p_array['row'];
00134 for ($i = 0; $i < $nb; $i++)
00135 {
00136 $a = new Stock_Goods_Sql($cn);
00137 if ($p_array['sg_quantity' . $i] != 0 &&
00138 trim($p_array['sg_code' . $i]) != '')
00139 {
00140 $stock= strtoupper(trim($p_array['sg_code' . $i]));
00141 $fiche=new Fiche($cn);
00142 $fiche->get_by_qcode($p_array['sg_code' . $i]);
00143
00144
00145
00146 $count=$cn->get_value('select count(*) from fiche_detail where ad_id=$1 and ad_value=$2',
00147 array(ATTR_DEF_STOCK,$stock));
00148 if ( $count==0) {
00149 throw new Exception("Code stock inexistant");
00150 }
00151 $a->f_id=$fiche->id;
00152 $a->sg_code = $stock;
00153 $a->sg_quantity = abs($p_array['sg_quantity' . $i]);
00154 $a->sg_type = ($p_array['sg_quantity' . $i] > 0) ? 'd' : 'c';
00155 $a->sg_comment = $p_array['p_motif'];
00156 $a->tech_user = $_SESSION['g_user'];
00157 $a->r_id = $p_array['p_depot'];
00158 $a->sg_exercice = $exercice;
00159 $a->c_id = $ch->c_id;
00160 $a->sg_date=$p_array['p_date'];
00161 $a->insert();
00162 }
00163 }
00164 $cn->commit();
00165 }
00166 catch (Exception $exc)
00167 {
00168 echo $exc->getTraceAsString();
00169 throw $exc;
00170 }
00171 }
00172
00173
00174
00175
00176
00177
00178 static function insert_goods(&$p_cn,$p_array)
00179 {
00180 global $g_user;
00181 extract ($p_array);
00182 if ($g_user->can_write_repo($repo) == false)
00183 return false;
00184
00185
00186 $code = new Fiche($p_cn);
00187 $code->get_by_qcode($goods);
00188 $code_marchandise = $code->strAttribut(ATTR_DEF_STOCK);
00189 if ($code_marchandise == NOTFOUND || $code_marchandise=='')
00190 return false;
00191
00192 $exercice = $g_user->get_exercice();
00193
00194 if ($exercice == 0)
00195 throw new Exception('Annee invalide erreur');
00196
00197 $Res = $p_cn->exec_sql("insert into stock_goods (
00198 j_id,
00199 f_id,
00200 sg_code,
00201 sg_quantity,
00202 sg_type,sg_exercice,r_id ) values ($1,$2,$3,$4,$5,$6,$7)", array(
00203 $p_array['j_id'],
00204 $code->id,
00205 $code_marchandise,
00206 $p_array['quant'],
00207 $p_array['dir'],
00208 $exercice,
00209 $p_array['repo']
00210 )
00211 );
00212 return $Res;
00213 }
00214
00215
00216
00217
00218
00219
00220 function take_last_inventory($p_array)
00221 {
00222 global $cn;
00223 $year=HtmlInput::default_value("p_exercice", "", $p_array);
00224 $depot=HtmlInput::default_value("p_depot", "", $p_array);
00225 if ($year=="")
00226 throw new Exception(_('Inventaire invalide'), 10);
00227 if ($depot=="")
00228 throw new Exception(_('Dépôt invalide'), 20);
00229
00230
00231 $periode=new Periode($cn);
00232 $periode->p_id=$cn->get_value("select min(p_id) from parm_periode where p_exercice=$1", array($year));
00233 $first_day=$periode->first_day();
00234
00235
00236 $array['state_exercice']=$first_day;
00237
00238 $stock=new Stock($cn);
00239 $rowid=$stock->build_tmp_table($array);
00240
00241
00242 $next_year=$year+1;
00243 $periode=new Periode($cn);
00244 $periode->p_id=$cn->get_value("select min(p_id) from parm_periode where p_exercice=$1", array($next_year));
00245
00246 if ($periode->p_id=="")
00247 $array['p_date']="";
00248 else
00249 $array['p_date']=$periode->first_day();
00250
00251
00252 $array['p_motif']=_('Inventaire ').$year;
00253 $array['p_depot']=$depot;
00254
00255 $result=$cn->get_array("
00256 select sg_code,sum(coalesce(s_qin,0)-coalesce(s_qout,0)) tot_
00257 from tmp_stockgood_detail
00258 where
00259 s_id=$1 and r_id=$2
00260 group by sg_code",
00261 array($rowid,$depot));
00262 for ($e=0;$e< count($result);$e++) {
00263 $array['sg_code'.$e]=$result[$e]['sg_code'];
00264 $array['sg_quantity'.$e]=$result[$e]['tot_'];
00265 }
00266 $array['row']=$e;
00267 return $array;
00268
00269 }
00270 }
00271
00272 ?>