<commit_before>package com.elfec.cobranza.business_logic.printer;

import java.util.Locale;

import org.apache.commons.lang.WordUtils;
import org.joda.time.DateTime;

import com.elfec.cobranza.helpers.text_format.AccountFormatter;
import com.elfec.cobranza.model.Category;
import com.elfec.cobranza.model.CoopReceipt;
import com.elfec.cobranza.model.Supply;
import com.elfec.cobranza.model.SupplyStatus;
import com.elfec.cobranza.model.printer.CPCLCommand;
import com.elfec.cobranza.model.printer.CPCLCommand.Justify;
import com.elfec.cobranza.model.printer.CPCLCommand.Unit;

/**
 * Clase que se encarga de generar el comando de impresin de una factura
 * @author drodriguez
 *
 */
public class ReceiptGenerator {
	/**
	 * Define el tamao mximo de caracteres que puede ocupar una lnea de texto<br>
	 * Se lo utiliza para formatear la direccin.
	 */
	private static final int WRAP_LIMIT = 30;
	
	/**
	 * Genera el comando cpcl de impresin del recibo
	 * @param receipt
	 * @return
	 */
	public static CPCLCommand generateCommand(CoopReceipt receipt)
	{
		CPCLCommand command = new CPCLCommand(200, 400, 7.9).inUnit(Unit.IN_CENTIMETERS );
		assignHeaderData(command, receipt);
		assignReceiptData(command, receipt);
		command.print();
		return command;
	}
	
	/**
	 * Asigna la informacin de la factura a la cabecera en el comando de la impresora
	 * @param command
	 */
	private static void assignHeaderData(CPCLCommand command, CoopReceipt receipt)
	{
		command.justify(Justify.CENTER)
		.text("TAHOMA15.CPF", 0, 0, 0, 0.049, 0.076, "FACTURA ORIGINAL")
		.setFont("TAHOMA11.CPF")
		.multilineText(0.44, 0, 0, 0.7, "NIT: 1023213028", "FACTURA No.: "+receipt.getReceiptNumber(),
				"AUTORIZACIN: "+receipt.getAuthorizationNumber())
		.setFont("TAHOMA8P.CPF")
	    .text( 0, 0, 2.1, 0.025, 0.025, "Actividad Econmica:")
		.text( 0, 0, 2.45, "Venta de Energa Elctrica");
	}
	
	/**
	 * Asigna la informacin de la factura al segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptData(CPCLCommand command, CoopReceipt receipt)
	{
		assignReceiptLeftData(command, receipt);
		assignReceiptRightData(command, receipt);
	}
	

	/**
	 * Asigna la informacin de la columna izquierda de la factura al segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptLeftData(CPCLCommand command, CoopReceipt receipt)
	{
		SupplyStatus powerSupplyStatus = receipt.getPowerSupplyStatus();
		command.justify(Justify.LEFT, 3)
		.setFont("TAHOMA8P.CPF")
		.text(0, 0.3, 3.3, 0.03, 0.03, "FECHA EMISIN:")
		.text(0, 3.1, 3.3, receipt.getIssueDate().toString("dd/MM/yyyy"))
		.text("TAHOMA11.CPF", 0, 0.3, 3.65, 0.035, 0.035, "NUS:")
		.text("TAHOMA11.CPF", 0, 1.4, 3.65, 0.035, 0.035, ""+receipt.getSupplyId())
		.text(0, 0.3, 4.15, 0.03, 0.03, "CUENTA:")
		.text(0, 1.8, 4.15, AccountFormatter.formatAccountNumber(receipt.getSupplyNumber()))
		.text(0, 0.3, 4.5, 0.03, 0.03, "NOMBRE:")
		.text(0, 1.85, 4.5, Supply.findSupplyByNUSOrAccount(receipt.getSupplyId(), receipt.getSupplyNumber()).getClientName())
		.multilineText(0.37, 0, 0.35, 4.85, "NIT/CI: "+receipt.getNIT(), 
				wrapAddress("DIRECCIN: "+receipt.getClientAddress()),
				"CATEGORA: "+Category.getFullCategoryDesc(receipt.getCategoryId()),
				"MEDIDOR: "+receipt.getMeterNumber(),
				"POTENCIA: "+(powerSupplyStatus==null?0:powerSupplyStatus.getBilledConsume()));
	}
	
	/**
	 * Asigna la informacin de la columna derecha de la factura al 
	 * segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptRightData(CPCLCommand command, CoopReceipt receipt)
	{
		DateTime period = new DateTime(receipt.getYear(), receipt.getPeriodNumber(),1,0,0);
		command.justify(Justify.LEFT, 4.6)
		.setFont("TAHOMA8P.CPF")
		.multilineText(0.37, 0, 5.4, 3.3, 
				"CONSUMO (kWh): "+receipt.getSupplyStatus().getBilledConsume(),
				"PERIODO: "+period.toString("MMM/yyyy").toUpperCase(Locale.getDefault()),
				"DE: "+receipt.getSupplyStatus().getLastReadingDate().toString("dd/MM/yyyy")
				+"  A: "+receipt.getSupplyStatus().getDate().toString("dd/MM/yyyy"),
				"LECTURA ANTERIOR: "+receipt.getSupplyStatus().getLastReading(),
				"LECTURA ACTUAL: "+receipt.getSupplyStatus().getReading(),
				"FECHA PAGO: "+(DateTime.now().toString("dd/MM/yyyy hh:mm")),
				"VENCIMIENTO: "+receipt.getExpirationDate().toString("dd/MM/yyyy"),
				"DIAS MOROSIDAD: 23",
				"PRXIMA EMISIN: 06/01/2015");
	}
	
	/**
	 * Si es necesario parte la direccin en pedazos para que se impriman de forma
	 * subsecuente
	 * @param fullAddress
	 * @return
	 */
	private static String wrapAddress(String fullAddress)
	{
		return WordUtils.wrap(fullAddress, WRAP_LIMIT);
	}
}
<commit_msg>factura realizada hasta la doble columna<commit_after>package com.elfec.cobranza.business_logic.printer;

import java.util.Locale;

import org.apache.commons.lang.WordUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;

import com.elfec.cobranza.helpers.text_format.AccountFormatter;
import com.elfec.cobranza.model.Category;
import com.elfec.cobranza.model.CoopReceipt;
import com.elfec.cobranza.model.Supply;
import com.elfec.cobranza.model.SupplyStatus;
import com.elfec.cobranza.model.printer.CPCLCommand;
import com.elfec.cobranza.model.printer.CPCLCommand.Justify;
import com.elfec.cobranza.model.printer.CPCLCommand.Unit;

/**
 * Clase que se encarga de generar el comando de impresin de una factura
 * @author drodriguez
 *
 */
public class ReceiptGenerator {
	/**
	 * Define el tamao mximo de caracteres que puede ocupar una lnea de texto<br>
	 * Se lo utiliza para formatear la direccin.
	 */
	private static final int WRAP_LIMIT = 30;
	
	/**
	 * Genera el comando cpcl de impresin del recibo
	 * @param receipt
	 * @return
	 */
	public static CPCLCommand generateCommand(CoopReceipt receipt)
	{
		CPCLCommand command = new CPCLCommand(200, 400, 7.9).inUnit(Unit.IN_CENTIMETERS );
		assignHeaderData(command, receipt);
		assignReceiptData(command, receipt);
		command.print();
		return command;
	}
	
	/**
	 * Asigna la informacin de la factura a la cabecera en el comando de la impresora
	 * @param command
	 */
	private static void assignHeaderData(CPCLCommand command, CoopReceipt receipt)
	{
		command.justify(Justify.CENTER)
		.text("TAHOMA15.CPF", 0, 0, 0, 0.049, 0.076, "FACTURA ORIGINAL")
		.setFont("TAHOMA11.CPF")
		.multilineText(0.44, 0, 0, 0.7, "NIT: 1023213028", "FACTURA No.: "+receipt.getReceiptNumber(),
				"AUTORIZACIN: "+receipt.getAuthorizationNumber())
		.setFont("TAHOMA8P.CPF")
	    .text( 0, 0, 2.1, 0.025, 0.025, "Actividad Econmica:")
		.text( 0, 0, 2.45, "Venta de Energa Elctrica");
	}
	
	/**
	 * Asigna la informacin de la factura al segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptData(CPCLCommand command, CoopReceipt receipt)
	{
		assignReceiptLeftData(command, receipt);
		assignReceiptRightData(command, receipt);
	}
	

	/**
	 * Asigna la informacin de la columna izquierda de la factura al segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptLeftData(CPCLCommand command, CoopReceipt receipt)
	{
		String clientName = wrapName(Supply.findSupplyByNUSOrAccount(receipt.getSupplyId(), receipt.getSupplyNumber()).getClientName());
		double extraSpacing = ((clientName.split("\r\n").length-1)*0.37);
		command.justify(Justify.LEFT, 3)
		.setFont("TAHOMA8P.CPF")
		.text(0, 0.3, 3.3, 0.03, 0.03, "FECHA EMISIN:")
		.text(0, 3, 3.3, receipt.getIssueDate().toString("dd/MM/yyyy"))
		.text("TAHOMA11.CPF", 0, 0.3, 3.65, 0.035, 0.035, "NUS:")
		.text("TAHOMA11.CPF", 0, 1.4, 3.65, 0.035, 0.035, ""+receipt.getSupplyId())
		.text(0, 0.3, 4.15, 0.03, 0.03, "CUENTA:")
		.text(0, 1.8, 4.15, AccountFormatter.formatAccountNumber(receipt.getSupplyNumber()))
		.text(0, 0.3, 4.5, 0.03, 0.03, "NOMBRE:")
		.multilineText(0.37, 0, 1.85, 4.5, clientName)
		.multilineText(0.37, 0, 0.35, 4.85+extraSpacing, "NIT/CI: "+receipt.getNIT(), 
				wrapAddress("DIRECCIN: "+receipt.getClientAddress()),
				"CATEGORA: "+Category.getFullCategoryDesc(receipt.getCategoryId()),
				"MEDIDOR: "+receipt.getMeterNumber());
	}
	
	/**
	 * Asigna la informacin de la columna derecha de la factura al 
	 * segundo sector de la factura en el comando de la impresora
	 * @param command
	 */
	private static void assignReceiptRightData(CPCLCommand command, CoopReceipt receipt)
	{
		SupplyStatus powerSupplyStatus = receipt.getPowerSupplyStatus();
		DateTime period = new DateTime(receipt.getYear(), receipt.getPeriodNumber(),1,0,0);
		int daysPastDue = Days.daysBetween(receipt.getExpirationDate(), DateTime.now()).getDays();
		command.justify(Justify.LEFT, 4.6)
		.setFont("TAHOMA8P.CPF")
		.multilineText(0.37, 0, 5.4, 3.3, 
				"CONSUMO (kWh): "+receipt.getSupplyStatus().getBilledConsume(),
				"POTENCIA: "+(powerSupplyStatus==null?0:powerSupplyStatus.getBilledConsume()),
				"PERIODO: "+period.toString("MMM/yyyy").toUpperCase(Locale.getDefault()),
				"DE: "+receipt.getSupplyStatus().getLastReadingDate().toString("dd/MM/yyyy")
				+"  A: "+receipt.getSupplyStatus().getDate().toString("dd/MM/yyyy"),
				"LECTURA ANTERIOR: "+receipt.getSupplyStatus().getLastReading(),
				"LECTURA ACTUAL: "+receipt.getSupplyStatus().getReading(),
				"FECHA PAGO: "+(DateTime.now().toString("dd/MM/yyyy hh:mm")),
				"VENCIMIENTO: "+receipt.getExpirationDate().toString("dd/MM/yyyy"),
				"DIAS MOROSIDAD: "+(daysPastDue<0?0:daysPastDue),
				"PRXIMA EMISIN: "+receipt.getIssueDate().plusDays(33).toString("dd/MM/yyyy"));
	}
	
	/**
	 * Si es necesario parte el nombre en pedazos para que se impriman de forma
	 * subsecuente
	 * @param name
	 * @return la cadena con lineas de salto respentando el WRAP_LIMIT
	 */
	private static String wrapName(String name)
	{
		return WordUtils.wrap(name, WRAP_LIMIT-9).replace("\n", "\r\n");
	}
	
	/**
	 * Si es necesario parte la direccin en pedazos para que se impriman de forma
	 * subsecuente
	 * @param fullAddress
	 * @return la cadena con lineas de salto respentando el WRAP_LIMIT
	 */
	private static String wrapAddress(String fullAddress)
	{
		return WordUtils.wrap(fullAddress, WRAP_LIMIT).replace("\n", "\r\n");
	}
}

{'fraction_non_alphanumeric': 0.14174530596361515, 'fraction_numerical': 0.03512014787430684, 'mean_word_length': 4.532831001076426, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '258555', 'n_tokens_mistral': 4392, 'n_tokens_neox': 3815, 'n_words': 830}