Exemple #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);
  }
  @Override
  protected TraderDto doTransform(Trader e, int depth) {
    TraderDto d = new TraderDto();
    d.setAddress(e.getAddress());
    d.setCity(e.getCity());
    d.setDocumentNumber(e.getDocumentNumber());
    d.setDocumentType(e.getDocumentType());
    d.setEmail(e.getEmail());
    d.setId(e.getId());
    d.setName(e.getName());
    d.setNearStreets(e.getNearStreets());
    d.setPhone(e.getPhone());
    d.setSupervisor(e.isSupervisor());
    d.setParentId((e.getParent() != null) ? e.getParent().getId() : null);
    d.setParentDescription((e.getParent() != null) ? e.getParent().getName() : null);
    if (e.getTraders() != null && !e.getTraders().isEmpty()) {
      for (Trader t : e.getTraders()) {
        d.getListOfTraderIds().add(t.getId());
      }
    }
    d.setStatus(e.getStatus().name());

    return d;
  }