All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.cmatic.LancamentoMM Maven / Gradle / Ivy

/*
 * Copyright 2013 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.cmatic;

import static br.com.objectos.way.cmatic.WayCMatic.toTxtPart;
import static com.google.common.collect.Lists.transform;

import java.util.List;

import org.joda.time.LocalDate;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

/**
 * @author [email protected] (Edenir Norberto Anschau)
 */
public class LancamentoMM implements IsLancamento {

  interface Construtor {

    int getNumero();

    LocalDate getData();

    double getValorDebito();
    double getValorCredito();

    List getLancamentos();

  }

  private static final String HEADER_TRAILER = Strings.repeat(" ", 5 + 200 + 42 + 42);

  private final int numero;
  private final LocalDate data;
  private final double valorDebito;
  private final double valorCredito;
  private final List lancamentos;

  private LancamentoMM(Construtor construtor) {
    numero = construtor.getNumero();
    data = construtor.getData();
    valorDebito = construtor.getValorDebito();
    valorCredito = construtor.getValorCredito();
    lancamentos = construtor.getLancamentos();
  }

  static ConstrutorDeLancamentoMM multiploMultiplo(int numero, LocalDate data) {
    return new ConstrutorDeLancamentoMM(numero, data);
  }

  static LancamentoMM of(Construtor construtor) {
    return new LancamentoMM(construtor);
  }

  @Override
  public List toLog() {
    List log = ImmutableList.of();

    double diff = valorDebito - valorCredito;
    if (Math.abs(diff) > 0.01) {
      String tmpl = "[ERRO] %s: diferença contábil. Débito - Crédito = %.2f";
      String msg = String.format(tmpl, data.toString("dd/MM/yy"), diff);
      log = ImmutableList.of(msg);
    }

    return log;
  }

  @Override
  public String toTxt() {
    Iterable parts = toLines0();
    return Joiner.on("\r\n").join(parts);
  }

  private Iterable toLines0() {
    String _header = header();
    List header = ImmutableList.of(_header);
    List subs = transform(lancamentos, new SubToTxt());
    return Iterables.concat(header, subs);
  }

  private String header() {
    return new StringBuilder()
        .append(toTxtPart("%07d", numero, 7))
        .append(data.toString("dd/MM"))
        .append("M      ")
        .append("M      ")
        .append(toTxtPart("%-17.2f", valorDebito, 17))
        .append(HEADER_TRAILER)
        .toString();
  }

  private class SubToTxt implements Function {
    @Override
    public String apply(SubLancamento input) {
      return input
          .comNumero(numero)
          .toLancamentoTxt();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy