Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 157
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
generaGridEditOptions
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
380
doSumasGridServer
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
72
preparaSumasGridServer
0.00% covered (danger)
0.00%
0 / 105
0.00% covered (danger)
0.00%
0 / 1
2862
1<?php
2
3function generaGridEditOptions($tabla='', $fieldValue='', $fieldDesc='', $extraCond='', $a='', $js=false, $jsconparams=false, $tablaFunc='', $emptyOption=false, $emptyOptVal='', $tablaFuncParam=true, $forCM=false)
4{
5    $grid_eo = '';
6    $arrCM = array();
7
8    if(empty($tabla))
9        return $grid_eo;
10
11    if(!empty($extraCond))
12        $extraCond = " WHERE $extraCond";
13    //echo "<pre>".print_r($tablaFuncParam, true)."</pre>";
14    if($tabla == 'banco_cuenta_mov_link' || !empty($tablaFunc))
15    {
16        $tablaFunc = empty($tablaFunc) ? 'arrayLinkOrigen' : $tablaFunc;
17
18        if($tablaFuncParam === true)
19        {
20            $fieldValue=0;
21            $fieldDesc=1;
22            $arr_vals = $tablaFunc($tablaFuncParam);
23        }
24        else
25        {
26            $tablaFuncParam = explode(',',$tablaFuncParam);
27            if(sizeof($tablaFuncParam) > 1)
28                $arr_vals = $tablaFunc($tablaFuncParam[0], $tablaFuncParam[1]);
29            else
30                $arr_vals = $tablaFunc($tablaFuncParam[0]);
31
32        }
33    }
34    else
35    {
36        if(strpos($tabla, '_json_') !== false) {
37            $arr_vals = json_decode(obtenCatalogo($fieldValue), true);
38        } else {
39            $sql = "SELECT $fieldValue$fieldDesc FROM $tabla $extraCond";
40            $arr_vals = ia_sqlArrayIndx($sql);
41        }
42    }
43
44    //echo "<pre>".print_r($arr_vals, true)."</pre>";
45    $av = '';
46    if(is_array($arr_vals) && !empty($arr_vals))
47    {
48        if(!empty($a))
49        {
50            foreach($arr_vals as $k=>$v)
51            {
52                if($js === true)
53                {
54                    if($jsconparams === true)
55                        $av = "<a href=\"javascript:void(0);\" onclick=\"javascript:$a;\" data-value=\"".$v[$fieldValue]."\"><strong>".$v[$fieldDesc]."</strong></a>";
56                    else
57                        $av = "<a href=\"javascript:$a(\'".$v[$fieldValue]."\');\"><strong>".$v[$fieldDesc]."</strong></a>";
58                }
59                else
60                    $av = "<a href=\"$a?$fieldValue=".$v[$fieldValue] ."\" target=\"_blank\"><strong>".$v[$fieldDesc]."</strong></a>";
61
62                $grid_eo .= "'". $v[$fieldValue] ."':'$av',";
63
64                $arrCM[$v[$fieldValue]] = $av;
65            }
66        }
67        else
68        {
69            foreach($arr_vals as $k=>$v)
70            {
71                if(gettype($v) !== "array") continue;
72                $grid_eo .= "'". $v[$fieldValue] ."':'". $v[$fieldDesc] ."',";
73                $arrCM[$v[$fieldValue]] = $v;
74            }
75        }
76        $grid_eo = substr($grid_eo,0,-1);
77
78        //echo "<pre>".print_r($grid_eo, true)."</pre>";
79
80        if($emptyOption == true)
81        {
82            $grid_eo = "'". $emptyOptVal ."':''," . $grid_eo;
83        }
84    }
85
86    //echo "<pre>".print_r($arrCM, true)."</pre>";
87    //echo "<pre>".print_r($grid_eo, true)."</pre>";
88
89    return $forCM ? $arrCM : $grid_eo;
90}
91
92//VCA sumasGridServer las columnas que se van a sumar.
93/**
94 * @param $response
95 * @param $pk_field
96 */
97function doSumasGridServer(&$response, $pk_field, $origenFunc = false, $sumasGridServer = array())
98{
99    $colSums = $sumasGridServer['iasumCols'];
100
101    $table=$sumasGridServer['iactbl'] == "cuentat_mov" ? "cuentat_mov_tmp" : $sumasGridServer['iactbl'];
102
103    if(empty($colSums))
104        return;
105
106    //Solución final. Hacemos una tabla temporal y así no hacemos cosas raras.
107
108    if(empty($sumasGridServer['iaGridFiltrosWhere']) && !empty($sumasGridServer['iaSoloSumasWhere']))
109    {
110        $sumasGridServer['iaSoloSumasWhere'] = trim($sumasGridServer['iaSoloSumasWhere']);
111        $wherePos = strpos($sumasGridServer['iaSoloSumasWhere'], "WHERE");
112        if($wherePos !== false)
113            $wherePos = " ";
114        else
115            $wherePos = " WHERE ";
116        $sumasGridServer['iaSoloSumasWhere'] = $wherePos . (substr($sumasGridServer['iaSoloSumasWhere'], 0, strlen("AND")) === "AND" ? substr(strstr($sumasGridServer['iaSoloSumasWhere']," "), 1) : $sumasGridServer['iaSoloSumasWhere']);
117    }
118
119    $response->iasumColsSQL = $colSums = " SELECT $colSums /** ##sumasGridServer(18-06-19, func)## **/ FROM " . (empty($sumasGridServer['iasumFuncTable']) ? $table : $sumasGridServer['iasumFuncTable']) . $sumasGridServer['iaSoloSumasWhere'];
120    $response->iasumCols = ia_singleton($colSums);
121
122
123    /**
124    $myFile = "doSumasGridServer.txt";
125    $fh = fopen($myFile, 'w') or die("can't open file");
126    $bleh = "<pre>".print_r($sumasGridServer, true)."</pre>"
127    ."\r\nsql:$colSums\r\n,"
128    ."\r\nrespónse:<pre>".print_r($response->iasumCols, true)."</pre>";
129    fwrite($fh, $bleh);
130    fclose($fh);
131     **/
132
133    return true;
134}
135
136function preparaSumasGridServer(&$sumasGridServer=array())
137{
138    foreach( $sumasGridServer['iasum'] as $k=>$v)
139    {
140        if(!array_key_exists('extra', $v))
141            $v['extra'] = 'no';
142        if(!array_key_exists('vir', $v))
143            $v['vir'] = 'no';
144
145        if($v['extra'] == 'no')
146            $sumasGridServer['iasumArr'][$k] = $v['col'];
147        if($v['vir'] == 'no' || $v['extra'] == 'si')
148            $sumasGridServer['iasumcond'] .= "$k";
149    }
150
151    $sumasGridServer['iasumcond'] = substr($sumasGridServer['iasumcond'],1);
152    //$sumasGridServer['iasum']=str_replace('%2C',',',$sumasGridServer['iasum']);
153    //$sumasGridServer['iasumArr']=explode(',', $sumasGridServer['iasum']);
154
155
156    $moneda = $sumasGridServer['iasum_moneda'];
157    $activo = $sumasGridServer['iasum_activo'];
158    $link = $sumasGridServer['iasum_link'];
159
160    $moneda_vals = empty($sumasGridServer['iasum_moneda_vals']) ? array('1','2') : $sumasGridServer['iasum_moneda_vals'];
161
162//    echo "<pre>".print_r($sumasGridServer, true)."</pre>";
163//    die();
164    $moneda = empty($moneda) ? false : true;
165    $activo = empty($activo) ? false : true;
166    $link = empty($link) ? false : true;
167
168    $sumasGridServer['iasumcond'] .= $moneda ? "," . $sumasGridServer['iasum_moneda'] . ($activo ? "," . $sumasGridServer['iasum_activo'] .
169            ($link ? "," . $sumasGridServer['iasum_link'] : "") : "") : "";
170
171
172
173    $colSums=''; $colSumsCond=''; $tc = ""; $operacion = "SUM";
174
175    foreach( $sumasGridServer['iasumArr'] as $k=>$v)
176    {
177        $operacion = array_key_exists('oper', $sumasGridServer['iasum'][$k]) ? $sumasGridServer['iasum'][$k]['oper'] : "SUM";
178        $colSums.="$operacion($v) as suma_".$k;
179    }
180
181    $colSums = substr($colSums,1);
182
183
184    $thenCount = " THEN 1 ELSE 0 END) ";
185    $thenSum = "";
186
187    $giasumMoneda = "{moneda}";
188    $giasumLink = "{link}";
189    $giasumActivo = "{activo}";
190
191    $gPiasum_moneda = $sumasGridServer['iasum_moneda'];
192    $gPiasum_link = $sumasGridServer['iasum_link'];
193    $gPiasum_activo = $sumasGridServer['iasum_activo'];
194
195    $monpesos_txt = $moneda ? " SUM(CASE WHEN ($giasumMoneda = '$moneda_vals[0]') " : "";
196    $monusd_txt = $moneda ? " SUM(CASE WHEN ($giasumMoneda = '$moneda_vals[1]') " : "";
197
198
199    if($sumasGridServer['_app'] === 'nota_bodega') {
200        $activo_txt = $activo ? $monpesos_txt." AND $giasumActivo = '1' " : $monpesos_txt;
201        $activono_txt = $activo ? $monpesos_txt." AND $giasumActivo = '0' " : $monpesos_txt;
202        $activousd_txt = $activo ? $monusd_txt." AND $giasumActivo = '1' " : $monusd_txt;
203        $activonousd_txt = $activo ? $monusd_txt." AND $giasumActivo = '0' " : $monusd_txt;
204    } else {
205        $activo_txt = $activo ? $monpesos_txt." AND ($giasumActivo = '1' OR $giasumActivo = 'si') " : $monpesos_txt;
206        $activono_txt = $activo ? $monpesos_txt." AND ($giasumActivo = '0' OR $giasumActivo = 'no') " : $monpesos_txt;
207        $activousd_txt = $activo ? $monusd_txt." AND ($giasumActivo = '1' OR $giasumActivo = 'si') " : $monusd_txt;
208        $activonousd_txt = $activo ? $monusd_txt." AND ($giasumActivo = '0' OR $giasumActivo = 'no') " : $monusd_txt;
209    }
210
211
212    $link_txt = str_replace("($giasumActivo =", "(($giasumActivo =", $activo_txt) . " AND $giasumLink = '2') ";
213    $linkno_txt  = str_replace("($giasumActivo =", "(($giasumActivo =", $activo_txt) . " AND $giasumLink = '1') ";
214    $linkusd_txt = str_replace("($giasumActivo =", "(($giasumActivo =", $activousd_txt) . " AND $giasumLink = '2') ";
215    $linknousd_txt  = str_replace("($giasumActivo =", "(($giasumActivo =", $activousd_txt) . " AND $giasumLink = '1') ";
216
217
218    $activo_txt = empty($activo_txt) ? "" : $activo_txt;
219    $activono_txt = empty($activono_txt) ? "" : $activono_txt;
220    $activousd_txt = empty($activousd_txt) ? "" : $activousd_txt;
221    $activonousd_txt = empty($activonousd_txt) ? "" : $activonousd_txt;
222
223    $rn_txt = $moneda ? " /** Pesos, activo*/ $activo_txt $thenCount AS rn," : "/** La más sencilla */ COUNT(*) as rn,";
224    $rnusd_txt = $moneda ? " /** USD, activo */ $activousd_txt $thenCount AS rn_usd," : "";
225
226    $rn_ca_txt = $activo ? " /** Pesos, inactivo */ $activono_txt $thenCount AS rn_ca," : "";
227    $rnusd_ca_txt = $activo ? " /** USD, inactivo */ $activonousd_txt $thenCount AS rn_ca_usd," : "";
228
229
230
231    //$ooo=rand();
232    //ia_query("INSERT INTO dime (script,dime) VALUES ('GALLO FLACIDO $ooo','moneda: $moneda, activo: $activo
233    // ### ".addslashes(json_encode($sumasGridServer['iasumcondArr']))."')");
234
235    //La más sencilla, sólo pesos y sus sumas.
236    //La regla será, si hay moneda tendrá un sufijo "_usd", cancelado "_ca"
237    if(!$moneda && !$activo)
238        $sumasGridServer['iasumCols'] = str_replace($giasumActivo, $gPiasum_activo, $rn_txt)." ".$colSums;
239    else
240    {
241        $tc = "";
242        $colSums = " "
243            .str_replace(array($giasumMoneda, $giasumActivo), array($gPiasum_moneda, $gPiasum_activo), $rn_txt)." "
244            .str_replace(array($giasumMoneda, $giasumActivo), array($gPiasum_moneda, $gPiasum_activo), $rnusd_txt)." "
245            .str_replace(array($giasumMoneda, $giasumActivo), array($gPiasum_moneda, $gPiasum_activo), $rn_ca_txt)." "
246            .str_replace(array($giasumMoneda, $giasumActivo), array($gPiasum_moneda, $gPiasum_activo), $rnusd_ca_txt)." ";
247
248        $colSums = vx_trim($colSums);
249        $colSums = rtrim($colSums,',');
250
251        foreach( $sumasGridServer['iasumArr'] as $k=>$d)
252        {
253            $thenSum = " THEN ($d) ELSE 0 END) ";
254            $oriK = $sumasGridServer['iasum'][$k];
255
256            $monPers = array_key_exists("iasum_moneda", $oriK) ? $oriK["iasum_moneda"] : $gPiasum_moneda;
257            $linkPers = array_key_exists("iasum_link", $oriK) ? $oriK["iasum_link"] : $gPiasum_link;
258            $actPers = array_key_exists("iasum_activo", $oriK) ? $oriK["iasum_activo"] : $gPiasum_activo;
259
260            $arr_giaMonAct = array($giasumMoneda,$giasumActivo);
261            $arr_MonActPers = array($monPers,$actPers);
262
263            $arr_giaMonLinkAct = array($giasumMoneda, $giasumLink, $giasumActivo);
264            $arr_MonLinkActPers = array($monPers, $linkPers, $actPers);
265
266            //Por si mandan una acción personalizada en algún campo.
267            if (array_key_exists("iasum_oper", $oriK) && strlen($oriK['iasum_oper'])) {
268                $tc .= "
269, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activo_txt) . " $thenCount AS iasum_oper_$k" . "_rn
270, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activousd_txt) . " $thenCount AS iasum_oper_$k" . "_rn_usd
271, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activono_txt) . " $thenCount AS iasum_oper_$k" . "_rn_ca
272, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activonousd_txt) . " $thenCount AS iasum_oper_$k" . "_rn_ca_usd
273
274, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activo_txt) . " $thenSum AS iasum_oper_$k
275" . str_replace($arr_giaMonAct, $arr_MonActPers, $activousd_txt) . " $thenSum AS iasum_oper_$k" . "_usd
276, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activono_txt) . " $thenSum AS iasum_oper_$k" . "_ca
277, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activonousd_txt) . " $thenSum AS iasum_oper_$k" . "_ca_usd";
278            }
279
280            /////////////////////////////////////////////////////////
281
282            $tc .= "
283, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activo_txt) . " $thenSum AS suma_$k
284" . str_replace($arr_giaMonAct, $arr_MonActPers, $activousd_txt) . " $thenSum AS suma_$k" . "_usd
285, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activono_txt) . " $thenSum AS suma_$k" . "_ca
286, " . str_replace($arr_giaMonAct, $arr_MonActPers, $activonousd_txt) . " $thenSum AS suma_$k" . "_ca_usd";
287            /////////////////////////////////////////////////////////
288
289            if($link) {
290                // Con link
291                $tc .= "
292, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $link_txt) . " $thenSum AS link_$k
293" . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linkusd_txt) . " $thenSum AS link_$k" . "_usd
294, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linkno_txt) . " $thenSum AS link_$k" . "_no
295, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linknousd_txt) . " $thenSum AS link_$k" . "_no_usd
296, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $link_txt) . " $thenCount AS link_$k" . "_rn
297, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linkusd_txt) . " $thenCount AS link_$k" . "_rn_usd
298, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linkno_txt) . " $thenCount AS link_$k" . "_rn_no
299, " . str_replace($arr_giaMonLinkAct, $arr_MonLinkActPers, $linknousd_txt) . " $thenCount AS link_$k" . "_rn_no_usd";
300            }
301        }
302
303        $colSums .= $tc;
304        //$colSums = substr($colSums,1);
305
306
307        $sumasGridServer['iasumCols'] = $colSums;
308    }
309
310    //Genero un where especial para cuando sólo quieren sumas y vienen los IDs como parámetro.
311    if($sumasGridServer['iaSoloSumas'] == "SI")
312    {
313        $tmpWhere = '';
314        if(is_array($sumasGridServer['iaids']) && !empty($sumasGridServer['iaids']))
315            foreach($sumasGridServer['iaids'] as $k=>$v)
316            {
317                $tmpWhere.= ",'$v'";
318            }
319        if(strlen($tmpWhere))
320        {
321            $tmpWhere = substr($tmpWhere,1);
322            $tmpWhere = " $sumasGridServer[iacpk] IN ( $tmpWhere )";
323        }
324
325        if ($sumasGridServer['iacc'] === 'app_ajuste') {
326            $tmpWhere.=" AND cancelado <> 'Si'";
327        }
328
329        $sumasGridServer['iaSoloSumasWhere'] = empty($tmpWhere) ? "" : " WHERE $tmpWhere";
330    }
331
332    //Genero un where especial para cuando vengan filtros y además tenga busqueda rápida
333    if($sumasGridServer['iaGridFiltros'] == "SI")
334    {
335        $sumasGridServer['iaSoloSumasWhere'] = empty($sumasGridServer['iaSoloSumasWhere']) ? "" : " AND " . trim(str_replace("WHERE", "", trim($sumasGridServer['iaSoloSumasWhere'])));
336    }
337
338    /**
339    $myFile = "sumasGridServer.txt";
340    $fh = fopen($myFile, 'w') or die("can't open file");
341    $bleh = "<pre>".print_r($sumasGridServer, true)."</pre>";
342    fwrite($fh, $bleh);
343    fclose($fh);
344     **/
345
346}