Guest User

Untitled

a guest
Jan 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. public function queryMonth($fecha1,$fecha2)
  2. {
  3.  
  4.  
  5. $res = array();
  6. $rowres = "";
  7.  
  8.  
  9. $stmt = sqlsrv_query( $this->conn,"with cte AS (
  10. select DISTINCT c.CodeCompany as CompanyCode,
  11. CONVERT(char(6), GETDATE(), 112) as Period,
  12. a.idClave as EmployeID,
  13. c.nombre + ' ' + c.apellidos as EmployeName,
  14. d.idDepto as Depto,
  15. t.HorasAlMes as PlanWorkHoursMonth,
  16. SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
  17.  
  18.  
  19. from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
  20. JOIN tblDepto d ON c.fkDepto=d.idDepto
  21. JOIN tblTurno t ON c.fkTurno=t.idTurno
  22.  
  23. where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and
  24. CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1
  25.  
  26. group by
  27. c.CodeCompany,
  28. a.idClave,
  29. d.idDepto,
  30. t.HorasAlMes,
  31. c.nombre + ' ' + c.apellidos)
  32.  
  33. select CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,
  34.  
  35.  
  36. OvertimeHoursMonth=
  37. case
  38. when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
  39.  
  40. else 0
  41.  
  42. end,
  43.  
  44. LeaveHoursMonth=
  45.  
  46. case
  47. when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
  48. else 0
  49.  
  50. end
  51.  
  52.  
  53.  
  54. from cte");
  55.  
  56.  
  57.  
  58. if( $stmt === false) {
  59. die( print_r( sqlsrv_errors(), true) );
  60. }
  61.  
  62. $archivo = 'archivo.txt';
  63.  
  64. $handler = fopen($archivo,'a');
  65. while($row = sqlsrv_fetch_array($stmt)) {
  66. fwrite($handler,$row['CompanyCode']);
  67. fwrite($handler,$row['Period']);
  68. }
  69. fclose($handler);
  70.  
  71. while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
  72.  
  73. $rowres .= '<tr class="success">
  74. <td>'.$row["CompanyCode"].' </td>
  75. <td>'.$row["Period"].' </td>
  76. <td>'.$row["EmployeID"]. '</td>
  77. <td>'.utf8_encode($row["EmployeName"]). '</td>
  78. <td>'.$row["Depto"]. '</td>
  79. <td>'.$row["PlanWorkHoursMonth"].'</td>
  80. <td>'.$row["ActualWorkHoursMonth"].'</td>
  81. <td>'.$row["OvertimeHoursMonth"].'</td>
  82. <td>'.$row["LeaveHoursMonth"].'</td>
  83.  
  84. </tr>';
  85. }
  86.  
  87.  
  88. sqlsrv_free_stmt( $stmt);
  89. return $rowres;
  90.  
  91.  
  92. }
  93.  
  94. if (isset($_POST['date1']) && isset($_POST['date2']))
  95. {
  96.  
  97.  
  98. echo $db->queryMonth($_POST['date1'],$_POST['date2']);
  99.  
  100.  
  101. }else
  102. {
  103. echo "<p> Coloque fecha de inicio y fecha de fin </p>";
  104. }
  105.  
  106.  
  107. ?>
  108.  
  109. $archivo = 'archivo.txt';
  110.  
  111. $handler = fopen($archivo,'a');
  112. while($row = sqlsrv_fetch_array($stmt)) {
  113. fwrite($handler,$row['CompanyCode']);
  114. fwrite($handler,$row['Period']);
  115. }
  116. fclose($handler);
  117.  
  118. $archivo = 'archivo.txt';
  119. header('Content-Type: application/force-download');
  120. header('Content-Disposition: attachment; filename='.$archivo);
  121. header('Content-Transfer-Encoding: binary');
  122. header('Content-Length: '.filesize($archivo));
  123.  
  124. include($archivo);
  125.  
  126. $archivo = 'archivo.txt';
  127. header('Content-Type: application/force-download');
  128. header('Content-Disposition: attachment; filename='.$archivo);
  129. header('Content-Transfer-Encoding: binary');
  130. header('Content-Length: '.filesize($archivo));
  131.  
  132. include($archivo);
  133.  
  134.  
  135.  
  136.  
  137. class database {
  138.  
  139. private $conn;
  140. private $serverName;
  141. private $connectionInfo;
  142.  
  143. public function __construct()
  144. {
  145. $serverName = "000.000.00.00"; // Ip de conexion
  146. $connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión
  147.  
  148. $this->conn = sqlsrv_connect( $serverName, $connectionInfo );
  149.  
  150. if( $this->conn === false ) {
  151.  
  152. die( print_r( sqlsrv_errors(), true)); // muestra mensaje si falla
  153. }
  154.  
  155. }
  156.  
  157.  
  158.  
  159.  
  160. public function consulta($fecha1,$fecha2)
  161. {
  162.  
  163.  
  164. $res = array();
  165. $rowres = "";
  166.  
  167.  
  168. echo $fecha1;
  169.  
  170. $stmt = sqlsrv_query( $this->conn,"with cte AS (
  171. select DISTINCT c.CodeCompany as CompanyCode,
  172. CONVERT(char(6), GETDATE(), 112) as Period,
  173. a.idClave as EmployeID,
  174. c.nombre + ' ' + c.apellidos as EmployeName,
  175. d.idDepto as Depto,
  176. t.HorasAlMes as PlanWorkHoursMonth,
  177. SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
  178.  
  179.  
  180. from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
  181. JOIN tblDepto d ON c.fkDepto=d.idDepto
  182. JOIN tblTurno t ON c.fkTurno=t.idTurno
  183.  
  184. where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and
  185. CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1
  186.  
  187. group by
  188. c.CodeCompany,
  189. a.idClave,
  190. d.idDepto,
  191. t.HorasAlMes,
  192. c.nombre + ' ' + c.apellidos)
  193.  
  194. select CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,
  195.  
  196.  
  197. OvertimeHoursMonth=
  198. case
  199. when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
  200.  
  201. else 0
  202.  
  203. end,
  204.  
  205. LeaveHoursMonth=
  206.  
  207. case
  208. when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
  209. else 0
  210.  
  211. end
  212.  
  213.  
  214.  
  215. from cte");
  216.  
  217.  
  218. if( $stmt === false) {
  219. die( print_r( sqlsrv_errors(), true) );
  220. }
  221.  
  222.  
  223.  
  224.  
  225. $archivo= 'miarchivo1.txt';
  226.  
  227.  
  228. $handler = fopen($archivo,'w');
  229. while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
  230. fwrite($handler,$row["CompanyCode"]);
  231. fwrite($handler,$row["Period"]);
  232. }
  233. fclose($handler);
  234.  
  235.  
  236.  
  237.  
  238.  
  239. while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
  240.  
  241. $rowres .= '<tr class="success">
  242. <td>'.$row["CompanyCode"].' </td>
  243. <td>'.$row["Period"].' </td>
  244. <td>'.$row["EmployeID"]. '</td>
  245. <td>'.utf8_encode($row["EmployeName"]). '</td>
  246. <td>'.$row["Depto"]. '</td>
  247. <td>'.$row["PlanWorkHoursMonth"].'</td>
  248. <td>'.$row["ActualWorkHoursMonth"].'</td>
  249. <td>'.$row["OvertimeHoursMonth"].'</td>
  250. <td>'.$row["LeaveHoursMonth"].'</td>
  251.  
  252. </tr>';
  253.  
  254.  
  255. }
  256.  
  257.  
  258. sqlsrv_free_stmt( $stmt);
  259. return $rowres;
  260.  
  261.  
  262.  
  263.  
  264.  
  265. }
  266.  
  267. <div class="main">
  268.  
  269. <div class="main-inner">
  270.  
  271. <div class="container">
  272.  
  273. <div class="row">
  274.  
  275. <div class="span12">
  276.  
  277. <div class="widget ">
  278. <a name="head"></a>
  279. <div class="widget-header">
  280. <i class="icon-user"></i>
  281. <h3> R.1 </h3>
  282. </div> <!-- /widget-header -->
  283.  
  284. <div class="widget-content">
  285. <div class="tabbable">
  286. <br>
  287. <form id="edit-profile" method="POST" action ="document.php" class="form-horizontal">
  288. <fieldset>
  289.  
  290. <div class="control-group">
  291.  
  292. <label class="control-label" for="date1">Fecha Inicio:</label>
  293. <div class="controls">
  294. <input type="text" id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else
  295. echo date('d-m-Y'); ?>">
  296. </div> <!-- /controls -->
  297. </div> <!-- /control-group -->
  298.  
  299. <div class="control-group">
  300. <label class="control-label" for="date2">Fecha Final:</label>
  301. <div class="controls">
  302. <input type="text" id="date2" name="date2" value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else
  303. echo date('d-m-Y'); ?>" >
  304. </div> <!-- /controls -->
  305. </div> <!-- /control-group -->
  306.  
  307. <br />
  308.  
  309. <div class="form-actions">
  310. <button type="submit" class="btn btn-primary">Buscar </button>
  311.  
  312. </div> <!-- /form-actions -->
  313. </fieldset>
  314. </form>
  315.  
  316.  
  317. <table id="example" class="table table-striped table-bordered" >
  318.  
  319.  
  320. <thead>
  321. <tr>
  322. <th>CompanyCode</th>
  323. <th>Period</th>
  324. <th>EmployeID</th>
  325. <th>EmployeName</th>
  326. <th>Depto</th>
  327. <th>PlanWorkHoursMonth</th>
  328. <th>ActualWorkHoursMonth</th>
  329. <th>OvertimeHoursMonth</th>
  330. <th>LeaveHoursMonth</th>
  331. </tr>
  332. </thead>
  333. <tbody>
  334.  
  335. <?php
  336.  
  337.  
  338.  
  339.  
  340. if (isset($_POST['date1']) && isset($_POST['date2']))
  341. {
  342.  
  343.  
  344. echo $db->consulta($_POST['date1'],$_POST['date2']);
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351. }else
  352. {
  353. echo "<p> Coloque fecha de inicio y fecha de fin </p>";
  354. }
  355.  
  356.  
  357. ?>
  358.  
  359. </tbody>
  360. </table>
  361.  
  362.  
  363. </div>
  364.  
  365. </div>
  366.  
  367.  
  368. </div>
  369. </div>
  370. </div>
  371.  
  372.  
  373.  
  374.  
  375. </div> <!-- /row -->
  376. </div> <!-- /container -->
  377. </div> <!-- /main-inner -->
Add Comment
Please, Sign In to add comment