<?php $this->load->view("partial/header"); ?>

<script type="text/javascript">
$(function(){
	function recalc(){
		var totalSistema = 0;
		var totalContado = 0;
		$('.row-detalle').each(function(){
			var sistema = parseFloat($(this).data('sistema')) || 0;
			var moneda = $(this).find('.moneda').val();
			var contado = parseFloat($(this).find('.contado').val()) || 0;
			var tasa = parseFloat($('#cambio_tasa').val()) || 1;
			var contadoUsd = (moneda === 'BS') ? (tasa > 0 ? contado / tasa : 0) : contado;
			$(this).find('.contado_usd').text(contadoUsd.toFixed(2));
			$(this).find('.diff').text((contadoUsd - sistema).toFixed(2));
			totalSistema += sistema;
			totalContado += contadoUsd;
		});
		$('#total_sistema').text(totalSistema.toFixed(2));
		$('#total_contado').text(totalContado.toFixed(2));
		$('#total_diff').text((totalContado - totalSistema).toFixed(2));
	}

	$('.contado, .moneda, #cambio_tasa').on('keyup change', recalc);
	recalc();
});
</script>

<div class="row">
	<div class="col-md-12">
		<div class="panel panel-piluku">
			<div class="panel-heading">
				<h3 class="panel-title"><?php echo lang('cierre_edit'); ?> #<?php echo (int)$cierre->id; ?></h3>
			</div>
			<div class="panel-body">
				<?php if ($this->session->flashdata('manage_success_message')) { ?>
					<div class="alert alert-success"><?php echo H($this->session->flashdata('manage_success_message')); ?></div>
				<?php } ?>

				<div class="row" style="margin-bottom:15px;">
					<div class="col-md-8">
						<strong><?php echo lang('cierre_fecha'); ?>:</strong> <?php echo H($cierre->fecha); ?> &nbsp; 
						<strong><?php echo lang('cierre_caja'); ?>:</strong> <?php echo H($cierre->caja_nombre); ?> &nbsp;
						<strong><?php echo lang('cierre_bolsa'); ?>:</strong> <?php echo H($cierre->bolsa_manual); ?>
					</div>
					<div class="col-md-4 text-right">
						<?php $view_txt = lang('common_view'); $view_txt = $view_txt ? $view_txt : 'Ver'; ?>
						<?php echo anchor('cierre/view/'.$cierre->id, '<span class="ion-eye"></span> '.$view_txt, array('class'=>'btn btn-default')); ?>
						<?php if (strtolower(trim($cierre->estado)) === 'pendiente' && $this->Employee->has_module_action_permission('cierre','complete',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
							<?php echo anchor('cierre/review_complete/'.$cierre->id, '<span class="ion-checkmark"></span> '.lang('cierre_reviewed_complete'), array('class'=>'btn btn-success', 'style'=>'margin-left:6px;')); ?>
						<?php } ?>
					<?php if ($this->Employee->has_module_action_permission('cierre','add_update',$this->Employee->get_logged_in_employee_info()->person_id)) { ?>
							<?php echo anchor('cierre/delete/'.$cierre->id, '<span class="ion-trash-a"></span> '.lang('common_delete'), array('class'=>'btn btn-danger', 'style'=>'margin-left:6px;', 'onclick'=>'return confirm("'.H(lang('cierre_confirm_delete')).'");')); ?>
						<?php } ?>
					</div>
				</div>

				<?php echo form_open('cierre/edit/'.$cierre->id); ?>

				<div class="form-group">
					<label><?php echo lang('cierre_tasa'); ?></label>
					<input id="cambio_tasa" type="number" step="0.0001" name="cambio_tasa" class="form-control" value="<?php echo H($cierre->cambio_tasa); ?>" readonly>
					<p class="help-block"><?php echo lang('cierre_tasa_help'); ?></p>
				</div>

				<div class="table-responsive">
					<table class="table table-bordered">
						<thead>
							<tr>
								<th><?php echo lang('cierre_tipo_pago'); ?></th>
								<th class="text-right"><?php echo lang('cierre_sistema_usd'); ?></th>
								<th><?php echo lang('cierre_moneda'); ?></th>
								<th class="text-right"><?php echo lang('cierre_contado'); ?></th>
								<th class="text-right"><?php echo lang('cierre_contado_usd'); ?></th>
								<th class="text-right"><?php echo lang('cierre_diferencia'); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach($detalle as $d) { ?>
								<tr class="row-detalle" data-sistema="<?php echo (float)$d->monto_sistema_usd; ?>">
									<td><?php echo H($d->tipo_pago); ?></td>
									<td class="text-right"><?php echo number_format((float)$d->monto_sistema_usd, 2); ?></td>
									<td style="width:90px;">
										<select class="form-control moneda" name="detalle[<?php echo (int)$d->id; ?>][moneda]">
											<option value="USD" <?php echo $d->moneda=='USD'?'selected':''; ?>>USD</option>
											<option value="BS" <?php echo $d->moneda=='BS'?'selected':''; ?>>BS</option>
										</select>
									</td>
									<td style="width:160px;"><input class="form-control contado" type="number" step="0.01" name="detalle[<?php echo (int)$d->id; ?>][monto_contado]" value="<?php echo H($d->monto_contado); ?>"></td>
									<td class="text-right contado_usd"><?php echo number_format((float)$d->monto_contado_usd, 2); ?></td>
									<td class="text-right diff"><?php echo number_format((float)$d->diferencia_usd, 2); ?></td>
								</tr>
							<?php } ?>
						</tbody>
						<tfoot>
							<tr>
								<th class="text-right"><?php echo lang('cierre_totales'); ?></th>
								<th class="text-right" id="total_sistema"></th>
								<th></th>
								<th></th>
								<th class="text-right" id="total_contado"></th>
								<th class="text-right" id="total_diff"></th>
							</tr>
						</tfoot>
					</table>
				</div>

				<div class="form-group">
					<label><?php echo lang('cierre_notas'); ?></label>
					<textarea name="notas" class="form-control" rows="3"><?php echo H($cierre->notas); ?></textarea>
				</div>

				<div class="form-group">
					<button type="submit" class="btn btn-primary"><?php echo lang('common_save'); ?></button>
					<?php echo anchor('cierre', lang('common_cancel'), array('class'=>'btn btn-default')); ?>
				</div>

				<?php echo form_close(); ?>
			</div>
		</div>
	</div>
</div>

<?php $this->load->view("partial/footer"); ?>
