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
00029 require 'class_stock_sql.php';
00030
00031 class Stock extends Stock_Sql
00032 {
00033
00034
00035
00036
00037
00038 function history($p_array)
00039 {
00040
00041 $sql = $this->create_query_histo($p_array);
00042 require_once 'class_sort_table.php';
00043 $p_url = HtmlInput::get_to_string(array("gDossier", "ac", "wcard", "wdate_start", "wdate_end", "wrepo",
00044 "wamount_start", "wamount_end", "wcode_stock", "wdirection"));
00045
00046 $tb = new Sort_Table();
00047 $tb->add("Date", $p_url, " order by real_date asc", "order by real_date desc", "da", "dd");
00048 $tb->add("Code Stock", $p_url, " order by sg_code asc", "order by sg_code desc", "sa", "sd");
00049 $tb->add("Dépôt", $p_url, " order by r_name asc", "order by r_name desc", "ra", "rd");
00050 $tb->add("Fiche", $p_url, " order by 2 asc", "order by 2 desc", "fa", "fd");
00051 $tb->add("Commentaire", $p_url, " order by coalesce(sg_comment,jr_comment) asc", "order by coalesce(sg_comment,jr_comment) desc", "ca", "cd");
00052 $tb->add("Montant", $p_url, " order by j_montant asc", "order by j_montant desc", "ja", "jd");
00053 $tb->add("Quantité", $p_url, " order by sg_quantity asc", "order by sg_quantity desc", "qa", "qd");
00054 $tb->add("IN/OUT", $p_url, " order by (case when sg_type='c' then 'OUT' when sg_type='c' then 'IN' end ) asc", "order by (case when sg_type='c' then 'OUT' when sg_type='c' then 'IN' end ) desc", "ta", "td");
00055 $order = (isset($p_array['ord'])) ? $p_array['ord'] : 'da';
00056
00057 $sql.=$tb->get_sql_order($order);
00058 $step = $_SESSION['g_pagesize'];
00059 $page = (isset($_GET['offset'])) ? $_GET['page'] : 1;
00060 $offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0;
00061
00062 $res = $this->cn->exec_sql($sql);
00063
00064 $max_row = Database::num_row($res);
00065
00066 $nav_bar = navigation_bar($offset, $max_row, 0, $page);
00067
00068 if ($step != -1)
00069 $res = $this->cn->exec_sql($sql . " , sg_id asc limit " . $step . " offset " . $offset);
00070 $max_row = Database::num_row($res);
00071
00072 $this->search_box_button();
00073 $this->search_box($p_array);
00074 require_once 'template/stock_histo.php';
00075 $this->export_stock_histo_form();
00076 echo HtmlInput::print_window();
00077 }
00078
00079 function export_stock_histo_form()
00080 {
00081 echo '<form style="display:inline" method="GET" action="export.php">';
00082 echo HtmlInput::get_to_hidden(array("gDossier", "wcard", "wdate_start", "wdate_end", "wrepo",
00083 "wamount_start", "wamount_end", "wcode_stock", "wdirection"));
00084 echo HtmlInput::hidden('act','CSV:StockHisto');
00085 echo HtmlInput::submit('stockhisto','Export CSV');
00086 echo '</form>';
00087 }
00088 function export_stock_summary_list_form()
00089 {
00090 echo '<form style="display:inline" method="GET" action="export.php">';
00091 echo HtmlInput::get_to_hidden(array("gDossier", "state_exercice"));
00092 echo HtmlInput::hidden('act','CSV:StockResmList');
00093
00094 echo HtmlInput::submit('stockresm','Export CSV');
00095 echo '</form>';
00096 }
00097 function search_box_button()
00098 {
00099 $bt = HtmlInput::button("Recherche", "Recherche", ' onclick="$(\'histo_search_d\').show();"');
00100 echo $bt;
00101 }
00102
00103 function search_box($p_array)
00104 {
00105
00106 global $g_user;
00107 $wrepo = HtmlInput::select_stock($this->cn, "wrepo", 'R');
00108 $wrepo->value[] = array('value' => -1, 'label' => 'Tous les dépôts');
00109
00110 $wdate_start = new IDate('wdate_start');
00111 $wdate_end = new IDate('wdate_end');
00112 $wamount_start = new INum('wamount_start');
00113 $wamount_end = new INum('wamount_end');
00114 $wcard = new ICard('wcard');
00115 $wcode_stock = new ICard('wcode_stock');
00116 $wdirection = new ISelect("wdirection");
00117
00118
00119 $wrepo->selected = HtmlInput::default_value("wrepo", -1, $p_array);
00120
00121
00122 $exercice = $g_user->get_exercice();
00123 $periode = new Periode($this->cn);
00124 list($periode_start, $periode_end) = $periode->get_limit($exercice);
00125
00126 $wdate_start->value = HtmlInput::default_value("wdate_start", $periode_start->first_day(), $p_array);
00127 $wdate_end->value = HtmlInput::default_value("wdate_end", $periode_end->last_day(), $p_array);
00128
00129 $wamount_start->value = HtmlInput::default_value("wamount_start", 0, $p_array);
00130 $wamount_end->value = HtmlInput::default_value("wamount_end", 0, $p_array);
00131
00132
00133 $wcard->extra = "all";
00134 $wcard->set_attribute("typecard", "all");
00135 $wcard->value = HtmlInput::default_value("wcard", "", $p_array);
00136
00137
00138 $wcode_stock->extra = " [sql] fd_id=500000 ";
00139 $wcode_stock->set_attribute("typecard", "[sql] fd_id=500000");
00140 $wcode_stock->value = HtmlInput::default_value("wcard", "", $p_array);
00141
00142
00143 $wcode_stock->value = HtmlInput::default_value("wcode_stock", "", $p_array);
00144
00145
00146 $wdirection->value = array(
00147 array('value' => "-1", 'label' => "Tout"),
00148 array('value' => "c", 'label' => "OUT"),
00149 array('value' => "d", 'label' => "IN")
00150 );
00151 $wdirection->selected = HtmlInput::default_value("wdirection", "-1", $p_array);
00152
00153 require_once 'template/stock_histo_search.php';
00154 }
00155
00156 function create_query_histo($p_array)
00157 {
00158 global $cn,$g_user;
00159 $profile=$g_user->get_profile();
00160 $sql = "
00161 select sg_id,
00162 sg.f_id,
00163 (select ad_value from fiche_Detail as fd1 where ad_id=1 and fd1.f_id=jx.f_id) as fname,
00164 (select ad_value from fiche_Detail as fd1 where ad_id=23 and fd1.f_id=jx.f_id) as qcode,
00165 sg_code,
00166 coalesce(sg_comment,jr_comment) as ccomment,
00167 sg_exercice,
00168 r_name,
00169 sg.r_id,
00170 j_montant,
00171 jr_date,
00172 sg_quantity,
00173 case when sg_type='c' then 'OUT' when sg_type='d' then 'IN' end as direction,
00174 jr_internal,
00175 jr_id,
00176 coalesce(sg_date,jr_date) as real_date,
00177 to_char(coalesce(sg_date,jr_date),'DD.MM.YY') as cdate
00178 from stock_goods as sg
00179 join stock_repository as sr on (sg.r_id=sr.r_id)
00180 left join jrnx as jx on (sg.j_id=jx.j_id)
00181 left join jrn as j on (j.jr_grpt_id=jx.j_grpt)
00182 where
00183 sg.r_id in (select r_id from profile_sec_repository where p_id = $profile)";
00184 $and = " and ";
00185 $clause = "";
00186 if (isset($p_array['wdate_start']) && $p_array['wdate_start'] != '')
00187 {
00188 $clause = $and." to_date('" . sql_string($p_array['wdate_start']) . "','DD.MM.YYYY')<=coalesce(sg_date,jr_date) ";
00189 }
00190 if (isset($p_array['wdate_end']) && $p_array['wdate_end'] != '')
00191 {
00192 $clause.=$and . " to_date('" . sql_string($p_array['wdate_end']) . "','DD.MM.YYYY')>=coalesce(sg_date,jr_date) ";
00193 }
00194 if (isset($p_array['wamount_start']) && $p_array['wamount_start'] != '' && isNumber($p_array['wamount_start']) == 1
00195 && $p_array['wamount_start'] != 0 )
00196 {
00197 $clause.=$and . " j_montant >= " . sql_string($p_array['wamount_start']);
00198 }
00199 if (isset($p_array['wamount_end'])
00200 && $p_array['wamount_end'] != ''
00201 && $p_array['wamount_end'] != 0
00202 && isNumber($p_array['wamount_end']) == 1)
00203 {
00204 $clause.=$and . " j_montant <= " . sql_string($p_array['wamount_end']);
00205 }
00206 if (isset($p_array['wcard']) && $p_array['wcard'] != '')
00207 {
00208 $f = new Fiche($this->cn);
00209 $f->get_by_qcode($p_array['wcard'], false);
00210 if ($f->id != 0)
00211 {
00212 $clause.=$and . " sg.f_id = " . sql_string($f->id);
00213 }
00214 }
00215 if (isset($p_array['wcode_stock']) && $p_array['wcode_stock'] != "")
00216 {
00217 $clause.=$and . " upper(sg_code) = upper('" . sql_string(trim($p_array['wcode_stock'])) . "')";
00218 }
00219 if (isset($p_array['wrepo']) && $p_array['wrepo'] != -1)
00220 {
00221 $clause.=$and . " sg.r_id = " . sql_string($p_array['wrepo']);
00222 }
00223 if (isset($p_array['wdirection']) && $p_array['wdirection'] != -1)
00224 {
00225 $clause.=$and . " sg.sg_type = '" . sql_string($p_array['wdirection']) . "'";
00226 }
00227
00228 return $sql . $clause;
00229 }
00230
00231 function summary($p_array)
00232 {
00233 global $cn, $g_user;
00234 $tmp_id=$this->build_tmp_table($p_array);
00235
00236 $a_repository = $g_user->get_available_repository('R');
00237 $a_code = $cn->get_array("select distinct sg_code from tmp_stockgood_detail where s_id=$1", array($tmp_id));
00238 if (isset($p_array['present']))
00239 {
00240 $present = $p_array['present'];
00241 }
00242 else
00243 {
00244 $present = 'T';
00245 }
00246 if ($present == 'T')
00247 {
00248 require_once 'template/stock_summary_table.php';
00249 }
00250 if ($present == 'L')
00251 {
00252 require_once 'template/stock_summary_list.php';
00253 $this->export_stock_summary_list_form();
00254
00255 }
00256 echo HtmlInput::print_window();
00257 }
00258 function build_tmp_table($p_array)
00259 {
00260 global $cn,$g_user;
00261
00262 if (isset ($p_array['state_exercice']))
00263 {
00264 $periode=new Periode($cn);
00265 $date= format_date($p_array['state_exercice'], 'YYYY-MM-DD');
00266 $periode->find_periode($date);
00267
00268 } else {
00269 $periode_id=$g_user->get_periode();
00270 $periode=new Periode($cn,$periode_id);
00271 }
00272 $periode->load();
00273 $tmp_id = $cn->get_next_seq("public.tmp_stockgood_s_id_seq");
00274 $cn->exec_sql("delete from tmp_stockgood where s_date < now() - interval '2 days' ");
00275 $cn->exec_sql("insert into tmp_stockgood(s_id) values ($1)", array($tmp_id));
00276
00277
00278 $a_repository = $g_user->get_available_repository('R');
00279
00280
00281 $sql_repo_detail = "
00282 insert into tmp_stockgood_detail(s_id,sg_code,s_qin,r_id,f_id)
00283 select $tmp_id,trim(sg_code), coalesce(sum(sg_quantity),0) as qin,r_id,f_id
00284 from stock_goods
00285 where
00286 sg_type='d'
00287 and j_id is not null
00288 and j_id in (select j_id from jrnx where j_tech_per in (select p_id from parm_periode where p_exercice=$2))
00289 and r_id in (select r_id from profile_sec_repository where p_id=$1)
00290 group by r_id,trim(sg_code),f_id
00291 ";
00292
00293
00294 $cn->exec_sql($sql_repo_detail, array($g_user->get_profile(), $periode->p_exercice));
00295 $sql_repo_detail = "
00296 insert into tmp_stockgood_detail(s_id,sg_code,s_qout,r_id,f_id)
00297 select $tmp_id,trim(sg_code) , coalesce(sum(sg_quantity),0) as qout,r_id,f_id
00298 from stock_goods as sg
00299 where
00300 sg_type='c'
00301 and sg.j_id is not null
00302 and sg.j_id in (select j_id from jrnx where j_tech_per in (select p_id from parm_periode where p_exercice=$2))
00303 and sg.r_id in (select r_id from profile_sec_repository where p_id=$1)
00304 group by r_id,trim(sg_code),f_id
00305 ";
00306 $cn->exec_sql($sql_repo_detail, array($g_user->get_profile(), $periode->p_exercice));
00307
00308
00309 $sql_repo_detail = "
00310 insert into tmp_stockgood_detail(s_id,sg_code,s_qin,r_id,f_id)
00311 select $tmp_id,trim(sg_code) , coalesce(sum(sg_quantity),0) as qin,r_id,f_id
00312 from stock_goods as sg
00313 where
00314 sg_type='d'
00315 and j_id is null
00316 and sg_date >= ( select min(p_start) from parm_periode where p_exercice=$2)
00317 and sg_date <= ( select max(p_end) from parm_periode where p_exercice=$2)
00318 and sg.r_id in (select r_id from profile_sec_repository where p_id=$1)
00319 group by r_id,trim(sg_code),f_id
00320 ";
00321
00322 $cn->exec_sql($sql_repo_detail, array($g_user->get_profile(), $periode->p_exercice));
00323 $sql_repo_detail = "
00324 insert into tmp_stockgood_detail(s_id,sg_code,s_qout,r_id,f_id)
00325 select $tmp_id,trim(sg_code), coalesce(sum(sg_quantity),0) as qout,r_id,f_id
00326 from stock_goods
00327 where
00328 sg_type='c'
00329 and j_id is null
00330 and r_id in (select r_id from profile_sec_repository where p_id=$1)
00331 and sg_date <= to_date($2,'DD.MM.YYYY')
00332 group by r_id,trim(sg_code),f_id
00333 ";
00334 $cn->exec_sql($sql_repo_detail, array($g_user->get_profile(), $periode->p_exercice));
00335 return $tmp_id;
00336 }
00337
00338 }
00339
00340 ?>