<div class="container mt-5 mb-5">
  <div class="mb-4">
    <div class="d-flex align-items-center">
      <img src="../assets/itsolution24/img/logo-favicons/1_favicon.png" alt="Logo TERRA PRO" style="max-height: 100px; margin-right: 15px;">
      <div>
        <h2 class="font-weight-bold mt-2 mb-1">TERRA PRO</h2>
        <h4 class="text-secondary mb-0">Recibo de Pago</h4>
      </div>
    </div>
   
  </div>
</div>


<div class="row">
	<div class="col-md-12">
		<div class="table-responsive">
			<table class="table table-bordered table-striped table-condensed mb-0">
			<tbody>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Tipo de movimiento'); ?>
					</td>
					<td class="w-70">
						<?php echo get_the_income_source($invoice['source_id'], 'source_name'); ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Comprobante NRO'); ?>
					</td>
					<td class="w-70">
						<?php echo $invoice['invoice_id']; ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('label_title'); ?>
					</td>
					<td class="w-70">
						<?php echo $invoice['title']; ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Cliente'); ?>
					</td>
					<td class="w-70">
						<?php echo get_the_customer($invoice['customer_id'], 'customer_name'); ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('label_note'); ?>
					</td>
					<td class="w-70">
						<?php echo $invoice['details']; ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Cotizacion'); ?>
					</td>
					<td class="w-70">
						<?php echo  $invoice['cotizacion_dolar']; ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Nro Comprobante caja'); ?>
					</td>
					<td class="w-70">
						<?php echo $invoice['ref_no']; ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('label_datetime'); ?>
					</td>
					<td class="w-70">
						<?php echo format_date($invoice['created_at']); ?>
					</td>
				</tr>
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('Recibido por'); ?>
					</td>
					<td class="w-70">
						<?php echo get_the_user($invoice['created_by'], 'username'); ?>
					</td>
				</tr>
				
				<tr>
					<td class="w-30 bg-gray text-right">
						<?php echo trans('label_attachment'); ?>
					</td>
					<td class="w-70">
						<?php
						if (isset($invoice['image']) && ((FILEMANAGERPATH && is_file(FILEMANAGERPATH.$invoice['image']) && file_exists(FILEMANAGERPATH.$invoice['image'])) || (is_file(DIR_STORAGE . 'products' . $invoice['image']) && file_exists(DIR_STORAGE . 'products' . $invoice['image'])))) : ?>
						<a target="_blink" href="<?php echo FILEMANAGERURL ? FILEMANAGERURL : root_url().'/storage/products'; ?>/<?php echo $invoice['image']; ?>"><img  src="<?php echo FILEMANAGERURL ? FILEMANAGERURL : root_url().'/storage/products'; ?>/<?php echo $invoice['image']; ?>" width="40" height="50"></a>
						<?php
					endif; ?>
					</td>
				</tr>
				<tr>
					
				</tr>
			</tbody>
			<?php
			
			
			// Conexi�n a la base de datos (ajustar seg�n tus credenciales)
			$servername = "localhost";
			$username = "u628200248_terrapro";
			$password = "Terrapro2025";
			$dbname = "u628200248_terrapro";

			$id_trans = $invoice['ref_no'];

			$conn = new mysqli($servername, $username, $password, $dbname);

			// Verificar la conexi�n
			if ($conn->connect_error) {
				die("Conexi�n fallida: " . $conn->connect_error);
			}

			// Obtener los registros de la tabla
			$sql = "SELECT * FROM cheques WHERE invoice_no = '" . $invoice['ref_no'] . "'";
			$result = $conn->query($sql);

			?>

			<table class="table table-striped">
    <thead>
        <tr>
            <th>Numero de cheque</th>
            <th>Importe</th>
            <th>Fecha de Cobro</th>
            <th>CUIT TITULAR</th>
            <th>Nro asociado</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $total_cheque = 0; // Inicializa el total del importe
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row["numero"] . "</td>";
                
               
                // Suma el importe actual al total
                $total_cheque += $row["importe"];
                
                echo "<td> $ "  . currency_format($row["importe"]) . "</td>";
				echo "<td>" . $row["fecha_pago"] . "</td>";
                echo "<td>" . $row["cuit"] . "</td>";
                echo "<td>" . $row["invoice_id"] . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='5'>No hay pagos con cheque</td></tr>";
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="1"></td>
            <td style="text-align: right;">Sub Total: $ <?php echo currency_format($total_cheque); ?></td>
            <td colspan="1"></td>
        </tr>
    </tfoot>
</table>

			<?php
			// Obtener los registros de la tabla
			$sql = "SELECT * FROM bank_transaction_price WHERE ref_no = '" . $invoice['ref_no'] . "' AND account_id_ != 2 AND account_id_ != 5 AND account_id_ != 7";
			$result = $conn->query($sql);

			?>

			<table class="table table-striped">
    <thead>
        <tr>
            <th>Num ref</th>
            <th>Importe</th>
            <th>Caja</th>
           
        </tr>
    </thead>
    <tbody>
        <?php
        $total_efectivo = 0; // Inicializa el total del importe
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row["ref_no"] . "</td>";
                //$total_importe = 0;
                // Suma el importe actual al total
                $total_efectivo += $row["amount"];
                
                echo "<td> $ "  . currency_format($row["amount"]) . "</td>";
                echo "<td>" . get_the_bank_account($row['account_id_'], 'account_details') . "</td>";
              
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='4'>No hay ingresos en otros conceptos</td></tr>";
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2" style="text-align: right;">Total: $ <?php echo currency_format($total_efectivo); ?></td>
        </tr>
    </tfoot>
</table>

<?php
			// Obtener los registros de la tabla
			$sql = "SELECT * FROM bank_transaction_price WHERE ref_no = '" . $invoice['ref_no'] . "' AND account_id_ = 5 ";
			$result = $conn->query($sql);

			?>

			<table class="table table-striped">
    <thead>
        <tr>
            <th>Num ref</th>
            <th>Importe</th>
            <th>Caja</th>
            <th>Cotizacion</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $total_dolar = 0; // Inicializa el total del importe
		$cotizacion = 0;
		$total_pesos = 0;
		
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row["ref_no"] . "</td>";
                
                // Suma el importe actual al total
                $total_dolar += $row["amount"];
				$cotizacion += $row["cotizacion_billete"];
				$total_pesos = $total_dolar * $cotizacion;
                
                echo "<td> $ "  . currency_format($row["amount"]) . "</td>";
                echo "<td>" . get_the_bank_account($row['account_id_'], 'account_details') . "</td>";
                echo "<td>" . $row["cotizacion_billete"] . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='4'>No hay ingresos en otros conceptos</td></tr>";
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2" style="text-align: right;">Sub Total: $ <?php echo currency_format($total_pesos); ?></td>
        </tr>
		<tr>
           
        </tr>
		
    </tfoot>
</table>




			<script src="scripts.js"> </script>
			<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"> </script>
			<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"> </script>
			<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"> </script>


			<?php
			// Cerrar la conexi�n a la base de datos
			$conn->close();
			?>


		</div>
	</div>
</div>


</body>
</html>