*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// Copyright Author Dany De Bontridder danydb@aevalys.eu
/**
 * @file
 * @brief export in CSV the summary of stock in list
 *
 */
if (!defined('ALLOWED')) {
    die('Appel direct ne sont pas permis');
}
require_once NOALYSS_INCLUDE . '/class_stock.php';
global $cn;
// var_dump($_GET);
$stock = new Stock($cn);
$tmp_id = $stock->build_tmp_table($_GET);
header('Pragma: public');
header('Content-type: application/csv');
header('Content-Disposition: attachment;filename="stock-summary-list.csv"', FALSE);
?>
"Depot";"Adresse";"Ville";"Pays";"Code Stock";"Fiches";"IN";"OUT";"DIFF"
<?php 
$a_repo = $cn->get_array("select distinct t.r_id,r_name,r_adress,r_city,r_country from stock_repository as s join tmp_stockgood_detail as t\n\ton (s.r_id=t.r_id)\n\twhere\n\ts_id=\$1\n\torder by 2\n\t", array($tmp_id));
for ($r = 0; $r < count($a_repo); $r++) {
    $a_stock = $cn->get_array("\n\t\t\t\t\tselect coalesce(sum(s_qin),0) as qin,coalesce(sum(s_qout),0) as qout,sg_code\n\t\t\t\t\t\tfrom tmp_stockgood_detail  where r_id=\$1 and s_id=\$2\n\t\t\t\t\t\tgroup by sg_code\n\t\t\t\t\t\torder by sg_code\n\n\t\t\t\t\t", array($a_repo[$r]['r_id'], $tmp_id));
    for ($s = 0; $s < count($a_stock); $s++) {
        $a_card = $cn->get_array("\n\t\t\t\t\t\tselect f_id,vw_name,quick_code\n\t\t\t\t\t\tfrom vw_fiche_attr\n\t\t\t\t\t\twhere\n\t\t\t\t\t f_id in (\n\t\t\t\t\t\tselect distinct f_id from fiche_detail\n\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\tad_id=19 and\n\t\t\t\t\t\t\tad_value=\$1)\n\t\t\t\t\t\torder by vw_name,quick_code\n\t\t\t\t\t", array($a_stock[$s]['sg_code']));
        printf('"%s";', $a_repo[$r]['r_name']);
        printf('"%s";', $a_repo[$r]['r_adress']);
        printf('"%s";', $a_repo[$r]['r_city']);
        printf('"%s";', $a_repo[$r]['r_country']);
 /**
  * Return an array, used by Stock_Goods::input 
  * @global type $cn
  * @param type $p_array
  * @throws Exception
  */
 function take_last_inventory($p_array)
 {
     global $cn;
     $year = HtmlInput::default_value("p_exercice", "", $p_array);
     $depot = HtmlInput::default_value("p_depot", "", $p_array);
     if ($year == "") {
         throw new Exception(_('Inventaire invalide'), 10);
     }
     if ($depot == "") {
         throw new Exception(_('Dépôt invalide'), 20);
     }
     // compute state_exercice
     $periode = new Periode($cn);
     $periode->p_id = $cn->get_value("select min(p_id) from parm_periode where p_exercice=\$1", array($year));
     $first_day = $periode->first_day();
     // compute array for stock
     $array['state_exercice'] = $first_day;
     $stock = new Stock($cn);
     $rowid = $stock->build_tmp_table($array);
     // compute first day of the next year
     $next_year = $year + 1;
     $periode = new Periode($cn);
     $periode->p_id = $cn->get_value("select min(p_id) from parm_periode where p_exercice=\$1", array($next_year));
     if ($periode->p_id == "") {
         $array['p_date'] = "";
     } else {
         $array['p_date'] = $periode->first_day();
     }
     // Compute an array compatible with Stock_Goods::input
     $array['p_motif'] = _('Inventaire ') . $year;
     $array['p_depot'] = $depot;
     $result = $cn->get_array("\n                select sg_code,sum(coalesce(s_qin,0)-coalesce(s_qout,0)) tot_\n                from tmp_stockgood_detail \n                where \n                s_id=\$1 and r_id=\$2 \n                group by sg_code", array($rowid, $depot));
     for ($e = 0; $e < count($result); $e++) {
         $array['sg_code' . $e] = $result[$e]['sg_code'];
         $array['sg_quantity' . $e] = $result[$e]['tot_'];
     }
     $array['row'] = $e;
     return $array;
 }