Beispiel #1
0
  @Override
  public BillDto insert(BillDto d) {

    ErrorTypedConditions.checkArgument(
        StringUtils.isNotBlank(d.getStartDate()),
        "La fecha es requerida",
        ErrorType.VALIDATION_ERRORS);

    List<BillProductDto> billProducts = d.getBillProducts();

    ErrorTypedConditions.checkArgument(
        billProducts != null && !billProducts.isEmpty(),
        String.format("No se registran productos asociados a la factura"),
        ErrorType.PRODUCT_REQUIRED);

    ErrorTypedConditions.checkArgument(
        d.getClientId() != null, "id de cliente requerido", ErrorType.VALIDATION_ERRORS);

    Client client = this.clientDao.findById(d.getClientId());

    ErrorTypedConditions.checkArgument(
        client != null,
        String.format("Cliente no encontrado con id: %s", d.getClientId()),
        ErrorType.CLIENT_NOT_FOUND);

    ErrorTypedConditions.checkArgument(
        d.getTraderId() != null, "id de vendedor requerido", ErrorType.VALIDATION_ERRORS);

    Trader trader = this.traderDao.findById(d.getTraderId());

    ErrorTypedConditions.checkArgument(
        trader != null,
        String.format("Vendedor no encontrado con id: %s", d.getTraderId()),
        ErrorType.TRADER_NOT_FOUND);

    ErrorTypedConditions.checkArgument(
        trader.getStatus() == Trader.Status.Activo,
        String.format(
            "Vendedor con id: %s esta Inactivo para realizar ventas. Por favor activelo nuevamente.",
            d.getTraderId()),
        ErrorType.TRADER_NOT_FOUND);

    ErrorTypedConditions.checkArgument(
        d.getCreditNumber() != null, "nro de credito requerido", ErrorType.VALIDATION_ERRORS);

    Bill found = this.getDao().findByCreditNumber(d.getCreditNumber());

    ErrorTypedConditions.checkArgument(
        found == null,
        String.format("Ya existe factura con nro. de credito %s", d.getCreditNumber()),
        ErrorType.VALIDATION_ERRORS);

    Bill e = new Bill();

    for (BillProductDto p : billProducts) {
      BillProduct bp = new BillProduct();
      bp.setAmount(NumberUtils.toBigDecimal(p.getAmount()));
      bp.setBill(e);
      bp.setCount(p.getCount());
      bp.setDailyInstallment(NumberUtils.toBigDecimal(p.getDailyInstallment()));
      Product product = this.productDao.findById(p.getProductId());

      ErrorTypedConditions.checkArgument(
          product != null,
          String.format("Producto no encontrado con id: %s", p.getProductId()),
          ErrorType.PRODUCT_NOT_FOUND);

      bp.setProduct(product);
      product.decrementStock(p.getCount());
      this.productDao.saveOrUpdate(product);
      e.addBillProduct(bp);
    }
    e.setClient(client);
    Date startDate = DateUtils.parseDate(d.getStartDate());
    e.setStartDate(startDate);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startDate);

    int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

    e.setWeekOfYear(weekOfYear);

    int month = calendar.get(Calendar.MONTH);
    e.setMonth(month);

    int year = calendar.get(Calendar.YEAR);
    e.setYear(year);

    final BigDecimal calculatedTotalAmount = e.calculateTotalAmount();
    //		if(calculatedTotalAmount.compareTo(NumberUtils.toBigDecimal(d.getTotalAmount())) != 0){
    //			throw new ErrorTypedException("Error de validacion de importe total",
    // ErrorType.UNKNOWN_ERROR);
    //		}
    e.setTotalAmount(calculatedTotalAmount);
    e.setTotalAmountToLiq(calculatedTotalAmount);

    BigDecimal calculatedTotalDailyInstallment = e.calculateTotalDailyInstallment();
    //
    //	if(calculatedTotalDailyInstallment.compareTo(NumberUtils.toBigDecimal(d.getTotalDailyInstallment())) != 0){
    //			throw new ErrorTypedException("Error de validacion de total de valor de cuota diaria",
    // ErrorType.UNKNOWN_ERROR);
    //		}
    e.setTotalDailyInstallment(calculatedTotalDailyInstallment);

    Date now = new Date();

    int days = DateUtils.daysBetween(startDate, now);

    e.setOverdueDays(days);
    e.setRemainingAmount(calculatedTotalAmount);
    e.setTrader(trader);
    e.setEndDate(e.calculateEndDate());

    ErrorTypedConditions.checkArgument(
        d.getCollectorId() != null, "Id de cobrador es requerido", ErrorType.VALIDATION_ERRORS);

    Collector collector = this.collectorDao.findById(d.getCollectorId());

    ErrorTypedConditions.checkArgument(
        collector != null,
        String.format("Cobrador no encontrado con id: %s", d.getCollectorId()),
        ErrorType.VALIDATION_ERRORS);

    e.setCollector(collector);
    e.setCreditNumber(Long.valueOf(d.getCreditNumber()));
    this.getDao().saveOrUpdate(e);
    /*
    Payment payment = new Payment();
    payment.setAmount(calculatedTotalDailyInstallment);

    payment.setBill(e);
    payment.setCollector(collector);
    payment.setDate(startDate);

    e.addPayment(payment);
    // le resto el 1er pago ...
    e.setRemainingAmount(NumberUtils.subtract(calculatedTotalAmount, calculatedTotalDailyInstallment));
    */
    e.setStatus(Status.ACTIVE);

    this.getDao().saveOrUpdate(e);

    return this.getTransformer().transform(e);
  }
Beispiel #2
0
  @Override
  public BillDetailDto searchDetail(Long billId) {
    BillDetailDto d = new BillDetailDto();

    Bill bill = this.getDao().findById(billId);

    ErrorTypedConditions.checkArgument(bill != null, ErrorType.BILL_NOT_FOUND);

    d.setClientAddress(bill.getClient().getCompanyAddress());
    d.setClientName(bill.getClient().getName());
    d.setCreditAmount(NumberUtils.toString(bill.getTotalAmount()));
    d.setCreditDate(DateUtils.toDate(bill.getStartDate()));

    BigDecimal firstPayment = BigDecimal.ZERO;
    if (bill.getPayments() != null && bill.getPayments().size() > 0) {
      firstPayment = bill.getPayments().get(0).getAmount();
    }

    d.setFirstInstallmentAmount(NumberUtils.toString(firstPayment));
    d.setInstallmentAmount(NumberUtils.toString(bill.getTotalDailyInstallment()));

    d.setWeekAmount(
        NumberUtils.toString(
            NumberUtils.multiply(bill.getTotalDailyInstallment(), new BigDecimal(7))));

    d.setCurrentAmount(
        NumberUtils.toString(
            NumberUtils.subtract(bill.getTotalAmount(), bill.getRemainingAmount())));

    d.setRemainingAmount(NumberUtils.toString(bill.getRemainingAmount()));
    d.setTraderName(bill.getTrader().getName());
    d.setCreditNumber(StringUtils.toString(bill.getCreditNumber()));
    d.setCompletedDate(
        (bill.getCompletedDate() != null)
            ? DateUtils.toDate(bill.getCompletedDate())
            : StringUtils.EMPTY);
    d.setStatus(bill.getStatus().name());
    d.setCollectorId(bill.getCollector().getId());
    d.setCollectorDescription(
        String.valueOf(bill.getCollector().getZone())
            + " / "
            + bill.getCollector().getDescription());
    d.setCustomerCompanyType(
        (StringUtils.isNotBlank(bill.getClient().getCompanyType()))
            ? bill.getClient().getCompanyType()
            : StringUtils.EMPTY);

    for (Payment p : bill.getPayments()) {
      BillDetailPaymentDto r = new BillDetailPaymentDto();
      r.setAmount(NumberUtils.toString(p.getAmount()));
      r.setCollector(
          String.valueOf(
              (p.getCollector() != null)
                  ? p.getCollector().getZone() + " / " + p.getCollector().getDescription()
                  : StringUtils.EMPTY));
      r.setDate(DateUtils.toDate(p.getDate()));
      r.setId(p.getId());
      r.setTraderPayment(p.isTraderPayment() ? "SI" : " - ");

      d.getPayments().add(r);
    }

    for (Dev p : bill.getDevList()) {
      BillDetailDevolutionDto r = new BillDetailDevolutionDto();
      r.setAmount(NumberUtils.toString(p.getAmount()));
      r.setDate(DateUtils.toDate(p.getDate()));
      r.setInstallmentAmount(NumberUtils.toString(p.getInstallmentAmount()));
      r.setProductDescription(p.getProduct().getDescription());
      r.setObservations(p.getObservations());
      r.setCount(p.getProductCount());

      d.getDevolutions().add(r);
    }
    for (BillProduct p : bill.getBillProducts()) {
      BillProductDetailDto r = new BillProductDetailDto();
      r.setCount((p.getCount() != null) ? String.valueOf(p.getCount()) : "0");
      r.setCodProducto(p.getProduct().getCode());
      r.setDescription(
          p.getProduct().getDescription()
              + " / "
              + NumberUtils.toString(p.getProduct().getPrice()));
      r.setInstallmentAmount(NumberUtils.toString(p.getDailyInstallment()));
      r.setTotalAmount(NumberUtils.toString(p.getAmount()));

      d.getProducts().add(r);
    }
    for (Discount e : bill.getDiscounts()) {
      BillDetailDiscountDto r = new BillDetailDiscountDto();
      r.setAmount(NumberUtils.toString(e.getAmount()));
      r.setDate(DateUtils.toDate(e.getDate()));
      r.setObservations(e.getObservations());

      d.getDiscounts().add(r);
    }
    for (ProductReduction e : bill.getProductReductionList()) {
      BillDetailReductionDto r = new BillDetailReductionDto();
      r.setId(e.getId());
      r.setAmount(NumberUtils.toString(e.getAmount()));
      r.setDate(DateUtils.toDate(e.getDate()));
      r.setObservations(e.getObservations());

      d.getReductions().add(r);
    }

    return d;
  }