<?php

header('Content-Type: text/html; charset=UTF-8');

include('bd.php');

require('fpdf/fpdf.php');

require('alphapdf.php');

date_default_timezone_set("America/La_Paz");

include "core/autoload.php";

include "core/app/model/AssistanceData.php";

include "core/app/model/CreditData.php";

include "core/app/model/PayData.php";

include "core/app/model/PayEmpData.php";

include "core/app/model/PersonData.php";

include "core/app/model/PlaceData.php";

include "core/app/model/Sell2Data.php";

include "core/app/model/UserData.php";

include "core/app/model/MaterialData.php";

include "core/app/model/ProductData.php";

include "core/app/model/Operation2Data.php";

include "core/app/model/OperationTypeData.php";





class PDF extends FPDF

{



	function Header()

	{

		$dia = date('d');

		$anio = date('Y');

		$mes = date('m');



		//Logo

		$this->SetFillColor(0, 0, 0);

		$this->Image('logo.jpg', 0, 2, 45);

		//Arial bold 15

		$this->SetFont('Arial', 'B', 8);

		//Movernos a la derecha

		$this->Cell(30);

		//T�tulo

		$this->Cell(23, 5, 'PLASTICOS BAUTISTA | A & B', 0, 0, 'C');

		$this->Cell(150);

		$this->SetFont('Arial', 'B', 9);

		$this->Cell(21, 5, 'COCHABAMBA', 0, 1, 'C');

		$this->Cell(20);

		$this->Cell(31, 5, 'A & B', 0, 0, 'C');

		$this->Cell(65);

		$this->SetFont('Arial', 'B', 12);

		$this->Cell(31, 5, 'PLANILLA DE SUELDOS', 0, 0, 'C');

		$this->Cell(45);

		$this->SetFont('Arial', 'B', 9);

		$this->Cell(10, 5, 'DIA', 0, 0, 'C');

		$this->Cell(5);

		$this->Cell(10, 5, 'MES', 0, 0, 'C');

		$this->Cell(5);

		$this->Cell(10, 5, utf8_decode('AÑO'), 0, 1, 'C');



		$this->SetFont('Arial', '', 8);

		$this->Cell(31, 4, 'Km. 7.5 carretera Sacaba', 0, 0);

		$this->Cell(158);

		$this->Cell(15, 4, $dia, 0, 0, 'C');

		$this->Cell(15, 4, $mes, 0, 0, 'C');

		$this->Cell(15, 4, $anio, 0, 1, 'C');

		$this->Cell(31, 4, 'Esmeralda Norte - Zona Puntiti', 0, 1);

		$this->Cell(31, 4, 'Telf: 4245622 - 76418600 - 76490462', 0, 1);

		$this->Cell(31, 4, 'Email: ventas@plasticosbautista-ayb.com', 0, 1);

		$this->Cell(31, 4, 'www.plasticosbautista-ayb.com', 0, 1);

		//Salto de l�nea

		$this->Ln(5);

		$this->SetAlpha(0.1);

		$this->Image('fondo.png', 48, 80, $fpdf->w, $fpdf->h);

		$this->SetAlpha(1);
	}

	function Footer()

	{



		$this->SetY(-20);

		$this->SetFont('Arial', 'I', 8);





		$this->Cell(100, 5, '_______________________________________', 0, 0, 'C');

		//$this->Cell(70 ,5,'NOMBRE:................................',0,0,'C');

		$this->Cell(70, 5, '', 0, 1, 'C');

		$this->Cell(100, 5, 'DPTO. ADM. Y FINANZAS', 0, 0, 'C');

		//$this->Cell(70 ,5,'CI:...........',0,0,'C');

		$this->Cell(70, 5, '', 0, 1, 'C');

		//$this->Cell(70 ,5,'Recibido',0,0,'C');

		//$this->Cell(70 ,5,'Entregado',0,0,'C');

		//$this->Cell(70 ,5,'V.B.',0,1,'C');

	}

	function addNumTVA($tva)

	{

		$this->SetFont("Arial", "B", 10);

		$r1  = $this->w - 80;

		$r2  = $r1 + 70;

		$y1  = 80;

		$y2  = $y1 + 10;

		$mid = $y1 + (($y2 - $y1) / 2);

		$this->RoundedRect($r1, $y1, ($r2 - $r1), ($y2 - $y1), 2.5, 'D');

		$this->Line($r1, $mid, $r2, $mid);

		$this->SetXY($r1 + 16, $y1 + 1);

		$this->Cell(40, 4, "TVA Intracommunautaire", '', '', "C");

		$this->SetFont("Arial", "", 10);

		$this->SetXY($r1 + 16, $y1 + 5);

		$this->Cell(40, 5, $tva, '', '', "C");
	}
}



$sell = PayData::getAll();

foreach ($sell as $se) {

	if ($se->id == $_GET["id"]) {

		$mes = $se->month;
	}
}

//$client = PersonData::getById($_GET["id"]);

//$user = $sell->getUser();



//$operations = array();





$pdf  = new PDF('P', 'mm', array(279, 216));

$pdf->AliasNbPages();

$pdf->AddPage();



$pdf->SetFont('Arial', '', 10);





//set font to arial, bold, 14pt

$pdf->SetFont('Arial', 'B', 10);

//$mes = $_GET["id"];



if ($mes == 1)

	$letra = 'Enero';

if ($mes == 2)

	$letra = 'Febrero';

if ($mes == 3)

	$letra = 'Marzo';

if ($mes == 3)

	$letra = 'Abril';

if ($mes == 5)

	$letra = 'Mayo';

if ($mes == 6)

	$letra = 'Junio';

if ($mes == 7)

	$letra = 'Julio';

if ($mes == 8)

	$letra = 'Agosto';

if ($mes == 9)

	$letra = 'Septiembre';

if ($mes == 10)

	$letra = 'Octubre';

if ($mes == 11)

	$letra = 'Noviembre';

if ($mes == 12)

	$letra = 'Diciembre';



//$pdf->Cell(100 ,5,'Lugar y Fecha : La Paz, '.$fecha_l,0,1);//end of line

//$pdf->Cell(100 ,5,'Hora : '.date('H:i'),0,1);//end of line



$pdf->Cell(0, 5, 'CORRESPONDIENTE AL MES DE ' . $letra, 1, 1);



//$pdf->Cell(0 ,5,'DIRECCION: '.$client->address1,1,1);



//make a dummy empty cell as a vertical spacer

$pdf->Cell(189, 10, '', 0, 1); //end of line



//invoice contents

$pdf->SetFont('Arial', 'B', 6);



$pdf->Cell(7, 5, 'Nro', 1, 0);

$pdf->Cell(15, 5, 'CI', 1, 0, 'C');

$pdf->Cell(40, 5, 'Nombre Completo', 1, 0);

$pdf->Cell(10, 5, 'NAC', 1, 0);

$pdf->Cell(15, 5, 'Fecha Nac.', 1, 0, 'C');

$pdf->Cell(9, 5, 'SEXO', 1, 0, 'C');

$pdf->Cell(16, 5, 'Fecha Ingreso', 1, 0, 'C');

$pdf->Cell(25, 5, 'Cargo', 1, 0);

$pdf->Cell(13, 5, 'Dias Falta', 1, 0);

$pdf->Cell(15, 5, 'Haber Basico', 1, 0);

$pdf->Cell(13, 5, 'Dias Trab', 1, 0);

$pdf->Cell(15, 5, 'Total Ganado', 1, 0);

$pdf->Cell(13, 5, 'Horas Extra', 1, 0);

$pdf->Cell(13, 5, 'Horas Extra', 1, 0);

$pdf->Cell(13, 5, 'Adelantos', 1, 0);

$pdf->Cell(20, 5, 'Liquido Pagable', 1, 1); //end of line



$pdf->SetFont('Arial', '', 6);



//Numbers are right-aligned so we give 'R' after new line parameter

//display the items



foreach ($sell as $s) {

	if ($s->id == $_GET["id"]) {

		$pays = PayEmpData::getAll();

		$id = 0;

		foreach ($pays as $p) {

			if ($p->pay_id == $_GET["id"] && $p->type == 1) {

				$u = UserData::getById($p->user_id);

				$assis = AssistanceData::getAllByIdDate($p->user_id, $mes);

				//if(count($assis)>0){

				$dtr = 0;
				$dfp = 0;
				$dfs = 0;
				$dr = 0;
				$dc = 0;
				$dv = 0;
				$dp = 0;
				$da = 0;
				$dfe = 0;
				$df = 0;
				$dhe = 0;

				$dtt = 0;

				$stm = 0;

				foreach ($assis as $as) {

					if ($as->kind_id == 1) {
						$dtr += 1;
					} else if ($as->kind_id == 2) {
						$dfp += 1;
					} else if ($as->kind_id == 3) {
						$dfs += 1;
					} else if ($as->kind_id == 4) {
						$dr += 1;
					} else if ($as->kind_id == 5) {
						$dc += 1;
					} else if ($as->kind_id == 6) {
						$dv += 1;
					} else if ($as->kind_id == 7) {
						$dp += 1;
					} else if ($as->kind_id == 8) {
						$da += 1;
					} else if ($as->kind_id == 9) {
						$dfe += 1;
					} else if ($as->kind_id == 10) {
						$df += 1;
					} else if ($as->kind_id == 11) {
						$dhe += 1;
					}
				}

				$dtt = $dtr + $dr + $dc + $dv + $dp + $da + $dfe;

				$dff = $dfp + $dfs;

				if (count($assis > 0)) {

					$sd = @($u->pay / count($assis));
				} else {

					$sd = 0;
				}

				$id += 1;

				$pdf->Cell(7, 5, $id, 1, 0);

				$pdf->Cell(15, 5, $u->email, 1, 0, 'C');

				$pdf->Cell(40, 5, $u->name . ' ' . $u->lastname, 1, 0);

				$pdf->Cell(10, 5, 'BOL', 1, 0);



				$dt1 = date_create($u->date_birth);

				$df1 = date_format($dt1, 'd/m/Y');

				$pdf->Cell(15, 5, $df1, 1, 0, 'C');

				$pdf->Cell(9, 5, $u->sex, 1, 0, 'C');



				$dt2 = date_create($u->date_in);

				$df2 = date_format($dt2, 'd/m/Y');

				$pdf->Cell(16, 5, $df2, 1, 0, 'C	');

				$pdf->Cell(25, 5, $u->getPlace()->name, 1, 0);

				$pdf->Cell(13, 5, $dff, 1, 0);

				$pdf->Cell(15, 5, number_format($u->pay, 2, '.', ','), 1, 0);

				$pdf->Cell(13, 5, $dtt, 1, 0);

				$pdf->Cell(15, 5, number_format($u->pay - ($dff * $sd), 2, '.', ','), 1, 0);

				$ds = PayEmpData::getByUserId($p->user_id);

				$dis = 0;

				$hs = 0;

				$hh = 0;

				foreach ($ds as $d) {

					if ($ds->pay_id == $_GET["id"]) {

						if ($d->type == 2) {

							if ($d->kind < 3) {

								$dis += $d->q;
							} else {

								$hs += $d->q;

								$hh += $d->hour;
							}
						}
					}
				}

				$pdf->Cell(13, 5, $hh, 1, 0);

				$pdf->Cell(13, 5, $hs, 1, 0);

				$pdf->Cell(13, 5, $dis, 1, 0);

				$pdf->Cell(20, 5, number_format($u->pay - ($dff * $sd) + $hs - $dis, 2, '.', ','), 1, 1);



				//}

			}
		}
	}

	//$operations = PayEmpData::getByPayId($s->id);





}

$pdf->Cell(189, 10, '', 0, 1); //end of line



//summary

$pdf->SetFont('Arial', '', 20);

$pdf->Cell(25, 5, '', 0, 1);

//$pdf->Cell(25 ,10,'TOTAL CREDITO BS.',0,1);

//$pdf->Cell(25 ,10,'TOTAL PAGOS BS.'.number_format($a,2,'.',','),0,1);

//$pdf->Cell(25 ,10,'DEUDA BS.'.number_format($d-$a,2,'.',','),0,1);

$pdf->SetFont('Arial', '', 10);







$pdf->Output();
