Exportar formulario maestro/detalle de ProcessMaker

Referencia: http://forum.processmaker.com/viewtopic.php?f=9&t=3637&p=10693#p10693

This is a trigger to export case APP_DATA for a expense report (master/detail form).

 

function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}

$maestro_col=array('NOMBRE','SUPERVISOR','EMPRESA','LOCAL','MONTOPAGADO','FECHA','GASTOS');//MASTER FORM FIELDS
//FIELD "GASTOS" IS AN ARRAY WHICH CONTAINS SEVERAL DETAIL ROWS
$detalle_col= array('TIPO', 'MOTIVO', 'DETALLE', 'LLEGADA', 'HORAITEM', 'HLLEGADA', 'IMPORTEITEM','NUMCOMPROBANTE');
//DETAIL FORM FIELDS, BELONG TO "GASTOS" ARRAY, WHICH IS A GRID FORM INSERTED ON THE MASTER EXPENSE REPORT FORM
$pro_uid = '6272277834cf7dae12a82d3013862769';//PROCESS ID
$AREA=@@AREA;//DEPARTMENT ID
$DESDE=@@DESDE;//BEGINNING DATE
$HASTA=@@HASTA;//END DATE

$querybasico="SELECT APP_NUMBER, APP_DATA FROM APPLICATION WHERE PRO_UID='$pro_uid' AND (DATE(APP_CREATE_DATE) BETWEEN '$DESDE' AND '$HASTA') AND APP_UID IN (SELECT APP_UID FROM APP_DELEGATION WHERE PRO_UID='$pro_uid' AND DEL_INDEX>=4)";//QUERY
$queryxarea="AND APP_INIT_USER IN (SELECT USR_UID FROM USERS WHERE DEP_UID='$AREA')";//QUERY BY DEPARTMENT
$queryorder="ORDER BY APP_CREATE_DATE DESC";//ORDERED BY DATE
$query="";

if ($AREA=='1')//FOR ALL DEPARTMENTS
{
$query=$querybasico;
}
else{
$query=$querybasico." ".$queryxarea;//FOR SPECIFIC DEPARTMENT
}
$query=$query." ".$queryorder;

$result = executeQuery($query);//GET DATA FROM MYSQL AS AN ASSOCIATIVE ARRAY
@@REFRIGERIOS = array();//FINAL RESULT ARRAY I NEED TO GET

$indicex = 1;
if (is_array($result) && count($result) > 0){
foreach ($result as $row){
$aVars = unserialize($row['APP_DATA']);//UNSERIALIZE CASE DATA, $aVars IS AN ASSOCIATIVE ARRAY
$maestro_arr= array();
$maestro_arr['ID']=$row['APP_NUMBER'];//MASTER ARRAY
foreach($aVars as $key => $value){
if(in_array($key,$maestro_col)){//LOOKUP ARRAY ITEM BY KEY IN MASTER FORM FIELDS ARRAY
$bVars = $value;
if ((is_array($bVars)) && (count($bVars)>0))//IF CASE VARIABLE IS AN ARRAY, IN THIS CASE I'M LOOKING FOR "GASTOS" CASE VARIABLE
{
$detalle_arr=array();//DETAILS ARRAY
foreach($bVars as $key => $value){//"GASTOS" IS AN ARRAY OF ARRAYS, SO ITERATE THROUGH IT
if (is_array($value)){//IF IS ARRAY
foreach ($maestro_arr as $mkey => $mvalue){
$detalle_arr[$mkey] = $mvalue;
}
foreach ($value as $k1 => $v1){//FINAL ITERATION
//TO GET TO DETAIL DATA, I HAVE TO ITERATE 4 TIMES:
//SQL ARRAY, CASE VARIABLES, "GASTOS" CASE VARIABLE WHICH IS AN ARRAY OF ARRAYS
//AND THROUGH EACH ARRAY (EXPENSE REPORT DETAIL ROW)
if(in_array($k1,$detalle_col)){//LOOKUP "GASTOS" ARRAY ITEMS BY KEY
$detalle_arr[$k1]=utf8_decode($v1);
}
}
@@REFRIGERIOS[$indicex] = $detalle_arr;//RESULT ARRAY
$indicex++;
}
}
}
else{
$maestro_arr[$key]=utf8_decode($value);//utf8_decode PHP function for spanish characters
}
}
}
}
}

$nrand=rand(1000,9999);
$filename = "REPORTE_REFRIGERIO_MOVILIDAD_" . $nrand. "_" . date('dmY') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
$data = @@REFRIGERIOS;
foreach($data as $row)
{
if(!$flag) {
echo implode("\t", array_keys($row)) . "\n"; //EXPORT HEADER INFORMATION
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\n"; //EXPORT DATA INFORMATION
}
exit;

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: