require_once('../include/orm.php'); require_once('../include/globals.php'); require_once('../include/util.php'); $_import_importerdo_db_connected = $Link == ''; if($_import_importerdo_db_connected) $Link = connectDb(); define('DBCONNECTED', 1); debugstartpage(); if( isset($_x) ) { //if( $_x != '' ) { $_x = $_x == '' ? array() : StringTokenize($_x, ","); $_result = array(); $_result['version'] = array('actual' => 13); $_result['visibility'] = array(); $_initialvars = arrayExclude(get_defined_vars(), array("_*", "form_*", "s_*", "GLOBALS", "Link", "thisname")); } require('../include/base.events.php'); // isbasewindow: // On Open while( ($__action == "" or $__action == "executedonopen") and (empty($_x) or in('onopen', $_x)) ) { $__action = "executedonopen"; print("");// Chequea tabla por: // filas duplicadas (por unique1 y unique2) // campos requridos (por rf) // filas a actualizar (por unique1 o unique2 en base de datos) // control de total (por control) logToFile("importer4do"); logToFile("data: " . $_REQUEST['data']); $dva = trim($dv) == "" ? array() : StringTokenize($dv, ","); $rfa = trim($rf) == "" ? array() : StringTokenize($rf, ","); $adata = json_decode($_REQUEST['data']); $acols = json_decode($_REQUEST['cols']); $acolsoriginal = $acols; $aunique = array(); if( trim($unique1) != "" ) $aunique[] = StringTokenize($unique1, ","); if( trim($unique2) != "" ) $aunique[] = StringTokenize($unique2, ","); if( trim($unique3) != "" ) $aunique[] = StringTokenize($unique3, ","); $acrel = array(); if( trim($crel) != "" ) $acrel = StringTokenize($crel, "|"); $rstatuscolumn = sqlgetarray($Link, "SHOW COLUMNS FROM $t LIKE 'rowstatus'"); logToFile("Row status column array: " . print_r($rstatuscolumn, true)); $isstatus = !empty($rstatuscolumn); $rredondeocolumn = sqlgetarray($Link, "SHOW COLUMNS FROM $t LIKE 'redondeo'"); $isredondeo = !empty($rredondeocolumn); $rfa = array_map('strtolower', $rfa); $acols = array_map('strtolower', $acols); logToFile('default values: ' . print_r($dva, true)); logToFile('required fields: ' . print_r($rfa, true)); logToFile('columns: ' . print_r($acols, true)); $status = array(); foreach($adata as $irow => $row) { $statusrow = array(''); foreach($row as $icol => $cell) { if( trim($cell) != "" and $statusrow[0] == "" ) $statusrow[0] = "A agregar"; } if( $statusrow[0] == '' ) { $status[] = $statusrow; continue; } // Campos requeridos (rf) foreach($row as $icol => $cell) { //logToFile("row " . $irow . " col " . $icol . " = " . $cell); // Chequea campo requerido (rfa) $rfi = searchInArray($acols[$icol], $rfa); if( $rfi >= 0 ) { //logToFile("required! " . $cell); if( trim($cell) == "" ) { //logToFile("!!!"); $statusrow[] = $icol; $statusrow[0] = "Obligatorios"; } } } // Chequea uniques for($iu = 1; $iu <= sizeof($aunique); $iu++) { // Chequea unique1 if( sizeof($aunique[$iu]) > 0 ) { $ttext = ""; foreach($aunique[$iu] as $ii1 => $cunique) { if( trim($row[searchInArray($cunique, $acols)]) != "" ) $ttext = $ttext . $row[searchInArray($cunique, $acols)] . ","; } logToFile("Unique row " . $irow . " : " . $ttext); if( $ttext != "" ) { foreach($adata as $irow2 => $row2) { if( $irow2 == $irow ) continue; $ttext2 = ""; foreach($aunique[$iu] as $ii1 => $cunique) { if( trim($row2[searchInArray($cunique, $acols)]) != "" ) $ttext2 = $ttext2 . $row2[searchInArray($cunique, $acols)] . ","; } if( $ttext == $ttext2 ) { foreach($aunique[$iu] as $ii1 => $cunique) { $statusrow[] = searchInArray($cunique, $acols); } $statusrow[0] = "Fila repetida"; } } } } // Chequea unique1 contra la base de datos if( sizeof($aunique[$iu]) > 0 and $statusrow[0] == 'A agregar' ) { $sql = "select count(*) from $t where "; foreach($dva as $ii2 => $dvalue) { $sql .= $dvalue . " and "; } $ttext = ""; foreach($aunique[$iu] as $ii1 => $cunique) { if( $ii1 > 0 ) $sql .= " and "; $sql .= $cunique . " = '" . $row[searchInArray($cunique, $acols)] . "'"; } $tcant = sqlget($Link, $sql); if( $tcant == 1 ) { //$statusrow[0] = "A actualizar"; $statusrow[0] = "Ya existe!"; } } } // Chequea crel if( !empty($acrel) ) { if( trim($row[searchInArray($acrel[0], $acols)]) != "" ) { $tsql = "select count(*) from " . $acrel[1] . " where " . $acrel[0] . " = '" . $row[searchInArray($acrel[0], $acols)] . "' " . (sizeof($acrel) > 2 ? " and " . $acrel[2] : ""); //logToFile("CHECKING: $tsql"); $tcant = sqlget($Link, $tsql); if( $tcant == 0 and is_number($row[searchInArray($acrel[0], $acols)]) ) { $tsql = "select count(*) from " . $acrel[1] . " where cast(" . $acrel[0] . " as unsigned int) = '" . $row[searchInArray($acrel[0], $acols)] . "' " . (sizeof($acrel) > 2 ? " and " . $acrel[2] : ""); $tcant = sqlget($Link, $tsql); } if( $tcant > 1 ) { $statusrow[0] = "Encontrado mas de 1!"; } else if( $tcant == 0 ) { $statusrow[0] = "No encontrado!"; } if( $acrel[3] == 'int' and !is_number($row[searchInArray($acrel[0], $acols)]) ) $statusrow[0] = ""; } else { continue; } } // Chequea total if( isset($total) and $total != "" ) { //$cell $value = $row[searchInArray($total, $acols)]; if( trim($value) != "" and is_number(intelligentnumberconversion($value)) ) { $totalvalue = intelligentnumberconversion($value); $totalcalc = 0; foreach($row as $icol => $cell) { if( $acols[$icol] != "" ) logToFile('Verifying to sum total ' . $acols[$icol] . " " . $cell); if( !empty($acrel) and trim($acols[$icol]) == trim($acrel[0]) ) { } else if( trim($acols[$icol]) == trim($total) ) { } else if( $acols[$icol] != "" and is_number(intelligentnumberconversion($cell)) ) { logToFile(' sum to total'); $totalcalc += intelligentnumberconversion($cell); } } $dif = $totalvalue - $totalcalc; if( abs(round($dif,2)) > 15 ) { // error $statusrow[0] = "" . $dif . ""; } else if( abs(round($dif,2)) != 0 ) { // Si al dif es de menos de 15 agrega un concepto a la base PUNITORIOS $ired = searchInArray("Redondeo", $acols); if( $ired == -1 ) { $acols[] = 'Redondeo'; $ired = sizeof($acols) - 1; if( !$isredondeo ) { $rredondeocolumn = sqlexec($Link, "alter table $t add redondeo decimal(11,2)"); $isredondeo = true; } } $adata[$irow][] = round($dif,2); } } } $status[] = $statusrow; } if( isset($doimport) ) { logToFile('columns to import: ' . print_r($acols, true)); logToFile('first row to import: ' . print_r($adata[0], true)); if( startsWith($t, "temp" ) ) { sqlexec($Link, "delete from $t"); } $reg = 0; $reg2 = 0; foreach($adata as $irow => $row) { $taction = 0; switch(strip_tags($status[$irow][0])) { case 'A agregar': $taction = 1; break; case 'A actualizar': $taction = 2; break; } if( $taction != 1 ) continue; if( $taction == 1 ) { $sql = "insert into $t("; $values = " values("; } else { $sql = "update $t"; $values = " set "; } $aagregar = false; $tcols = 0; foreach($row as $icol => $cell) { if( $acols[$icol] == "" ) continue; if( trim($cell) != "" ) $aagregar = true; if( $tcols > 0 ) { if( $taction == 1 ) $sql .= ", "; $values .= ", "; } if( $taction == 1 ) { $sql = $sql . str_replace(' ', '_', str_replace('%', 'porcentaje', $acols[$icol])); $values = $values . "'" . intelligentnumberconversion($cell) . "'"; } else { $values = $values . str_replace(' ', '_', str_replace('%', 'porcentaje', $acols[$icol])); $values = $values . " = '" . intelligentnumberconversion($cell) . "'"; } $tcols++; } foreach($dva as $dv1) { $dv2 = StringTokenize($dv1, "="); $sql .= ", " . $dv2[0]; $values .= ", " . $dv2[1]; } if( $taction == 1 ) { if( $isstatus ) { $sql .= ", rowstatus"; $values .= ", 'Agregada'"; } $values = $values . ")"; $sql = $sql . ") " . $values; } else { $sql = $sql . ") " . $values; } if( $aagregar ) { sqlexec($Link, $sql); if( $taction == 1 ) { $reg++; } else { $reg2++; } } } // Guarda plantilla if( $tfolder == "" ) $tfolder = "../include/plantillas"; //debug('[importer4do] saving plantilla ' . $tfolder . "/" . $tit . ".cfg" ); //if( !is_dir($tfolder) ) mkdir($tfolder); //writeToFile($tfolder . "/" . $tit . ".cfg", arrayToString($acolsoriginal)); debug("[importer4do] Agregados $reg registros en $t."); //print(json_encode("Agregados $reg y actualizados $reg2 registros en $t.")); print(json_encode(array("Agregados $reg registros en $t."))); disconnectDb($Link); exit(0); } // Guarda plantilla if( $tfolder == "" ) $tfolder = "../include/plantillas"; //debug('[importer4do] saving plantilla ' . $tfolder . "/" . $tit . ".cfg" ); //if( !is_dir($tfolder) ) mkdir($tfolder); //writeToFile($tfolder . "/" . $tit . ".cfg", arrayToString($acolsoriginal)); print(json_encode($status)); disconnectDb($Link); exit(0); // Si puso header location para ir a otra pagina cierra el script if( !isset($_x) and $__exiting ) { disconnectDb($Link); exit(0); } break; } // PHP Vars while( ($__action == "" or $__action == "executedonopen") and isset($_GET['_x']) and empty($_x) ) { $__row0 = array(); $_result['morevars'] = $__row0; break; } if( has($_x, 'pdf') or has($_x, 'pdfprint') or has($_x, 'pdfprint2') ) { require_once('../include/fpdfreport.php'); $_pdf = new PDF((isset($_orientation) ? $_orientation : 'P'),'pt', (isset($_size) ? $_size : 'A4' )); $_pdf->initialPage = ($_page != '' ? $_page : 1); //$_pdf->SetAutoPageBreak(true); $_pdf->title = ""; $_pdf->SetTopMargin(50); $_pdf->AliasNbPages(); $_pdf->AddPage(); } if( has($_x, 'pdf') or has($_x, 'pdfprint') or has($_x, 'pdfprint2') ) { $_pdf->Output(); } ?> if( !isset($_x) ) { ?> if( isset($_export) ) { ?> header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=importerdo.xls"); //echo(''); //echo(''); echo(''); ?> } else { ?>
require('../include/base.head.php'); ?> class="popup" } ?> > } ?> if( !isset($_export) ) { ?> } ?>