<?php
	$this->load->library('PrinterUtilities');
	
	$util = new PrinterUtilities();
	
	if(function_exists("printer_open")){
		/**PRINTER NAME */
		$printerName = 'POS-80CC';

		$printer = printer_open($printerName);

		if($printer){
		   
			define('DPI_X', printer_get_option($printer, PRINTER_RESOLUTION_X));
			define('DPI_Y', printer_get_option($printer, PRINTER_RESOLUTION_Y));

			/**CONFIGS */
			define('PAPER_WIDTH', 79); /**MM */
			define('MARGIN_X', 3);
			define('TITLE_FONT_HEIGHT', 4); /**MM */
			define('SUBTITLE_FONT_HEIGHT', 3.5); /**MM */
			define('BODY_FONT_HEIGHT', 3); /**MM */
			define('TITLE_FONT_FACE','Arial');
			define('SUBTITLE_FONT_FACE','Arial');
			define('BODY_FONT_FACE', 'Arial');

			/**Title font */
			$titleFontHeight = round((TITLE_FONT_HEIGHT * 0.0393701) * DPI_Y);
			$titleFont = printer_create_font(TITLE_FONT_FACE, $titleFontHeight, 0, 700, false, false, false, 0);

			/**Subtitle Font */
			$subtitleFontHeight = round((SUBTITLE_FONT_HEIGHT * 0.0393701) * DPI_Y);
			$subtitleBold = printer_create_font(SUBTITLE_FONT_FACE, $subtitleFontHeight, 0, 700, false, false, false, 0);
			$subtitleRegular = printer_create_font(SUBTITLE_FONT_FACE, $subtitleFontHeight, 0, 400, false, false, false, 0);

			/**Body Font */
			$bodyFontHeight = round((BODY_FONT_HEIGHT * 0.0393701) * DPI_Y);
			$bodyFontBold = printer_create_font(BODY_FONT_FACE, $bodyFontHeight, 0, 700, false, false, false, 0);
			$bodyFontRegular = printer_create_font(BODY_FONT_FACE, $bodyFontHeight, 0, 400, false, false, false, 0);


			/**Products */
			$productos = $util->sortProducts($productos_normal, 22);

			/**Init */
			printer_set_option($printer, PRINTER_MODE, 'RAW');
			printer_set_option($printer, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_CUSTOM);
			printer_set_option($printer, PRINTER_FORMAT_CUSTOM, PAPER_WIDTH);

			printer_start_doc($printer, 'Comprobante '.$doc_numero);
			printer_start_page($printer);
			$YPOSAcum = 0;
			/**Nombre del negocio */
			printer_select_font($printer, $titleFont);
			$titleLines = $util->centertext(strtoupper($nombre_empresa), PAPER_WIDTH, TITLE_FONT_HEIGHT, MARGIN_X);
			foreach($titleLines as $line){
				printer_draw_text($printer, $line['text'], $util->mmToDots($line['xPos'], DPI_X), $YPOSAcum);
				$YPOSAcum += $util->mmToDots(TITLE_FONT_HEIGHT, DPI_Y);
			}
			/**Tipo de documento */
			printer_select_font($printer, $subtitleRegular);
			$docLines = $util->centertext('Recibo No.: '.$doc_numero, PAPER_WIDTH, TITLE_FONT_HEIGHT, MARGIN_X);
			foreach($docLines as $line){
				printer_draw_text($printer, $line['text'], $util->mmToDots($line['xPos'], DPI_X), $YPOSAcum);
				$YPOSAcum += $util->mmToDots(TITLE_FONT_HEIGHT, DPI_Y);
			}
			$YPOSAcum += $util->mmToDots(TITLE_FONT_HEIGHT, DPI_Y);
			/**Table header */
			printer_select_font($printer, $bodyFontBold);

			printer_draw_text($printer, 'CANT', 0, $YPOSAcum);
			printer_draw_text($printer, 'DESCRIPCION', $util->mmToDots(10, DPI_X), $YPOSAcum);
			printer_draw_text($printer, 'PRECIO.', $util->mmToDots(45, DPI_X), $YPOSAcum);
			printer_draw_text($printer, 'TOTAL', $util->mmToDots(60, DPI_X), $YPOSAcum);

			$YPOSAcum += $util->mmToDots(BODY_FONT_HEIGHT, DPI_Y);
			printer_draw_line ($printer , 0, $YPOSAcum, $util->mmToDots(PAPER_WIDTH, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(1, DPI_Y);
			/**Table body */
			printer_select_font($printer, $bodyFontRegular);
			/**productos */
			foreach($productos as $producto){
				if($producto->cant)
					printer_draw_text($printer,number_format($producto->cant, 2),  $util->mmToDots(0, DPI_X) , $YPOSAcum);
				if($producto->desc)
					printer_draw_text($printer,strtoupper(utf8_decode($producto->desc)), $util->mmToDots(9, DPI_X), $YPOSAcum);
				if($producto->precio)
					printer_draw_text($printer,number_format($producto->precio,2), $util->mmToDots(47, DPI_X), $YPOSAcum);
				if($producto->total)
					printer_draw_text($printer,number_format($producto->total,2), $util->mmToDots(62, DPI_X), $YPOSAcum);
			
				$YPOSAcum += $util->mmToDots(BODY_FONT_HEIGHT, DPI_Y);
			}
			printer_draw_line ($printer , 0, $YPOSAcum, $util->mmToDots(PAPER_WIDTH, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(1, DPI_Y);
			/**total */
			printer_select_font($printer, $subtitleBold);
			printer_draw_text($printer, 'TOTAL', $util->mmToDots(44, DPI_X), $YPOSAcum);
			printer_draw_text($printer, '$', $util->mmToDots(56, DPI_X), $YPOSAcum);
			printer_draw_text($printer, number_format($totales['totalTotal'], 2), $util->mmToDots(59, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**efectivo */
			printer_select_font($printer, $subtitleRegular);
			printer_draw_text($printer, 'EFECTIVO', $util->mmToDots(38, DPI_X), $YPOSAcum);
			printer_draw_text($printer, '$', $util->mmToDots(56, DPI_X), $YPOSAcum);
			printer_draw_text($printer, number_format($efectivo, 2), $util->mmToDots(59, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**Cambio */
			printer_draw_text($printer, 'CAMBIO', $util->mmToDots(41, DPI_X), $YPOSAcum);
			printer_draw_text($printer, '$', $util->mmToDots(56, DPI_X), $YPOSAcum);
			printer_draw_text($printer, number_format(($efectivo - $totales['totalTotal']), 2), $util->mmToDots(59, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**Cajero */
			printer_select_font($printer, $bodyFontRegular);
			printer_draw_text($printer, 'Cajero: '.$cajero, $util->mmToDots(0, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**fecha */
			printer_draw_text($printer, 'Fecha: '.$fecha, $util->mmToDots(0, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**Referencia */
			printer_draw_text($printer, 'Ref.: '.$referencia, $util->mmToDots(0, DPI_X), $YPOSAcum);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			/**mensaje */
			printer_select_font($printer, $subtitleRegular);
			$YPOSAcum += $util->mmToDots(SUBTITLE_FONT_HEIGHT, DPI_Y);
			$msgLines = $util->centertext($mensaje_ticket, PAPER_WIDTH, BODY_FONT_HEIGHT, MARGIN_X);
			foreach($msgLines as $line){
				printer_draw_text($printer, $line['text'], $util->mmToDots($line['xPos'], DPI_X), $YPOSAcum);
				$YPOSAcum += $util->mmToDots(BODY_FONT_HEIGHT, DPI_Y);
			}

			printer_end_page($printer);
			printer_end_doc($printer);
			printer_close($printer);
		}
	}
?>