protected void processRules(OrderDTO order, Integer userId) throws TaskException { rulesMemoryContext.add(order); // add OrderDTO to rules memory context order.setCurrency(new CurrencyDAS().find(order.getCurrency().getId())); if (order.getCreateDate() == null) { order.setCreateDate(new Date()); } // needed for calls to 'rateOrder' if (order.getPricingFields() != null) { for (PricingField field : order.getPricingFields()) { rulesMemoryContext.add(field); } } try { UserDTOEx user = DTOFactory.getUserDTOEx(userId); rulesMemoryContext.add(user); ContactBL contact = new ContactBL(); contact.set(userId); ContactDTOEx contactDTO = contact.getDTO(); rulesMemoryContext.add(contactDTO); } catch (Exception e) { throw new TaskException(e); } executeRules(); }
private String composeLine(InvoiceDTO invoice, InvoiceLineDTO invoiceLine, Integer userId) { StringBuffer line = new StringBuffer(); ContactBL contact = new ContactBL(); contact.set(userId); // cono line.append("\"" + emptyIfNull(contact.getEntity().getPostalCode()) + "\""); line.append(","); // custno line.append("\"" + userId + "\""); line.append(","); // naddrcode line.append("\"" + "000" + "\""); line.append(","); // lookupnm line.append("\"" + emptyIfNull(contact.getEntity().getOrganizationName()) + "\""); line.append(","); // totallineamt line.append("\"" + invoiceLine.getAmount() + "\""); line.append(","); // period line.append("\"" + new SimpleDateFormat("yyyyMM").format(invoice.getCreateDatetime()) + "\""); line.append(","); // name line.append("\"" + emptyIfNull(contact.getEntity().getOrganizationName()) + "\""); line.append(","); // deliveryaddr line.append("\"" + emptyIfNull(contact.getEntity().getAddress1()) + "\""); line.append(","); // city line.append("\"" + emptyIfNull(contact.getEntity().getCity()) + "\""); line.append(","); // state line.append("\"" + emptyIfNull(contact.getEntity().getStateProvince()) + "\""); line.append(","); // zip5 line.append("\"" + emptyIfNull(contact.getEntity().getPostalCode()) + "\""); line.append(","); // totdue - round to two decimals line.append("\"" + new UserBL().getBalance(userId).round(new MathContext(2)) + "\""); line.append(","); // qty line.append("\"" + invoiceLine.getQuantity() + "\""); line.append(","); // description line.append("\"" + invoiceLine.getDescription() + "\""); line.append(","); // invoiceno line.append("\"" + invoice.getNumber() + "\""); line.append(","); // custstatus line.append("\"" + "TRUE" + "\""); LOG.debug("Line to export:" + line); return line.toString(); }
@Override // implements abstract method public NVPList buildRequest(PaymentDTOEx payment, SvcType transaction) throws PluggableTaskException { NVPList request = new NVPList(); request.add(PARAMETER_MERCHANT_ID, getMerchantId()); request.add(PARAMETER_STORE_ID, getStoreId()); request.add(PARAMETER_TERMINAL_ID, getTerminalId()); request.add(PARAMETER_SELLER_ID, getSellerId()); request.add(PARAMETER_PASSWORD, getPassword()); ContactBL contact = new ContactBL(); contact.set(payment.getUserId()); request.add(WorldPayParams.General.STREET_ADDRESS, contact.getEntity().getAddress1()); request.add(WorldPayParams.General.CITY, contact.getEntity().getCity()); request.add(WorldPayParams.General.STATE, contact.getEntity().getStateProvince()); request.add(WorldPayParams.General.ZIP, contact.getEntity().getPostalCode()); request.add(WorldPayParams.General.FIRST_NAME, contact.getEntity().getFirstName()); request.add(WorldPayParams.General.LAST_NAME, contact.getEntity().getLastName()); request.add(WorldPayParams.General.COUNTRY, contact.getEntity().getCountryCode()); request.add(WorldPayParams.General.AMOUNT, formatDollarAmount(payment.getAmount())); request.add(WorldPayParams.General.SVC_TYPE, transaction.getCode()); CreditCardDTO card = payment.getCreditCard(); request.add(WorldPayParams.CreditCard.CARD_NUMBER, card.getNumber()); request.add( WorldPayParams.CreditCard.EXPIRATION_DATE, EXPIRATION_DATE_FORMAT.format(card.getCcExpiry())); if (card.getSecurityCode() != null) { request.add( WorldPayParams.CreditCard.CVV2, String.valueOf(payment.getCreditCard().getSecurityCode())); } return request; }
/** * Will add lines with headers and footers to make an invoice with subaccounts more readable. The * lines have to be already sorted. * * @param lines */ private void addHeadersFooters(List<InvoiceLineDTO> lines, ResourceBundle bundle) { Integer nowProcessing = Integer.valueOf(-1); BigDecimal total = null; int totalLines = lines.size(); int subaccountNumber = 0; LOG.debug("adding headers & footers." + totalLines); for (int idx = 0; idx < totalLines; idx++) { InvoiceLineDTO line = (InvoiceLineDTO) lines.get(idx); if (line.getTypeId() == Constants.INVOICE_LINE_TYPE_SUB_ACCOUNT && !line.getSourceUserId().equals(nowProcessing)) { // line break nowProcessing = line.getSourceUserId(); subaccountNumber++; // put the total first if (total != null) { // it could be the first subaccount InvoiceLineDTO totalLine = new InvoiceLineDTO(); totalLine.setDescription(bundle.getString("invoice.line.subAccount.footer")); totalLine.setAmount(total); lines.add(idx, totalLine); idx++; totalLines++; } total = BigDecimal.ZERO; // now the header anouncing a new subaccout InvoiceLineDTO headerLine = new InvoiceLineDTO(); try { ContactBL contact = new ContactBL(); contact.set(nowProcessing); StringBuffer text = new StringBuffer(); text.append(subaccountNumber + " - "); text.append(bundle.getString("invoice.line.subAccount.header1")); text.append( " " + bundle.getString("invoice.line.subAccount.header2") + " " + nowProcessing); if (contact.getEntity().getFirstName() != null) { text.append(" " + contact.getEntity().getFirstName()); } if (contact.getEntity().getLastName() != null) { text.append(" " + contact.getEntity().getLastName()); } headerLine.setDescription(text.toString()); lines.add(idx, headerLine); idx++; totalLines++; } catch (Exception e) { LOG.error("Exception", e); return; } } // update the total if (total != null) { // there had been at least one sub-account processed if (line.getTypeId() == Constants.INVOICE_LINE_TYPE_SUB_ACCOUNT) { total = total.add(line.getAmount()); } else { // this is the last total to display, from now on the // lines are not of subaccounts InvoiceLineDTO totalLine = new InvoiceLineDTO(); totalLine.setDescription(bundle.getString("invoice.line.subAccount.footer")); totalLine.setAmount(total); lines.add(idx, totalLine); total = null; // to avoid repeating } } } // if there are no lines after the last subaccount, we need // a total for it if (total != null) { // only if it wasn't added before InvoiceLineDTO totalLine = new InvoiceLineDTO(); totalLine.setDescription(bundle.getString("invoice.line.subAccount.footer")); totalLine.setAmount(total); lines.add(totalLine); } LOG.debug("done " + lines.size()); }
/** * @param userId * @param newInvoice * @param process It can be null. */ public void create( Integer userId, NewInvoiceDTO newInvoice, BillingProcessDTO process, Integer executorUserId) { // find out the entity id PreferenceBL pref = new PreferenceBL(); UserBL user = null; Integer entityId; if (process != null) { entityId = process.getEntity().getId(); } else { // this is a manual invoice, there's no billing process user = new UserBL(userId); entityId = user.getEntityId(userId); } // verify if this entity is using the 'continuous invoice date' // preference try { pref.set(entityId, Constants.PREFERENCE_CONTINUOUS_DATE); if (StringUtils.isNotBlank(pref.getString())) { Date lastDate = com.sapienter.jbilling.common.Util.parseDate(pref.getString()); LOG.debug("Last date invoiced: " + lastDate); if (lastDate.after(newInvoice.getBillingDate())) { LOG.debug( "Due date is before the last recorded date. Moving due date forward for continuous invoice dates."); newInvoice.setBillingDate(lastDate); } else { // update the lastest date only if this is not a review if (newInvoice.getIsReview() == null || newInvoice.getIsReview() == 0) { pref.createUpdateForEntity( entityId, Constants.PREFERENCE_CONTINUOUS_DATE, com.sapienter.jbilling.common.Util.parseDate(newInvoice.getBillingDate())); } } } } catch (EmptyResultDataAccessException e) { // not interested, ignore } // in any case, ensure that the due date is => that invoice date if (newInvoice.getDueDate().before(newInvoice.getBillingDate())) { LOG.debug("Due date before billing date, moving date up to billing date."); newInvoice.setDueDate(newInvoice.getBillingDate()); } // ensure that there are only so many decimals in the invoice int decimals = Constants.BIGDECIMAL_SCALE; try { pref.set(entityId, Constants.PREFERENCE_INVOICE_DECIMALS); decimals = pref.getInt(); } catch (EmptyResultDataAccessException e) { // not interested, ignore } LOG.debug("Rounding " + newInvoice.getTotal() + " to " + decimals + " decimals"); if (newInvoice.getTotal() != null) { newInvoice.setTotal(newInvoice.getTotal().setScale(decimals, Constants.BIGDECIMAL_ROUND)); } if (newInvoice.getBalance() != null) { newInvoice.setBalance(newInvoice.getBalance().setScale(decimals, Constants.BIGDECIMAL_ROUND)); } // some API calls only accept ID's and do not pass meta-fields // update and validate meta-fields if they've been populated if (newInvoice.getMetaFields() != null && !newInvoice.getMetaFields().isEmpty()) { newInvoice.updateMetaFieldsWithValidation(entityId, newInvoice); } // create the invoice row invoice = invoiceDas.create(userId, newInvoice, process); // add delegated/included invoice links if (newInvoice.getIsReview() == 0) { for (InvoiceDTO dto : newInvoice.getInvoices()) { dto.setInvoice(invoice); } } // add the customer notes if it applies try { pref.set(entityId, Constants.PREFERENCE_SHOW_NOTE_IN_INVOICE); } catch (EmptyResultDataAccessException e) { // use the default then } if (pref.getInt() == 1) { if (user == null) { user = new UserBL(userId); } if (user.getEntity().getCustomer() != null && user.getEntity().getCustomer().getNotes() != null) { // append the notes if there's some text already there newInvoice.setCustomerNotes( (newInvoice.getCustomerNotes() == null) ? user.getEntity().getCustomer().getNotes() : newInvoice.getCustomerNotes() + " " + user.getEntity().getCustomer().getNotes()); } } // notes might come from the customer, the orders, or both if (newInvoice.getCustomerNotes() != null && newInvoice.getCustomerNotes().length() > 0) { invoice.setCustomerNotes(newInvoice.getCustomerNotes()); } // calculate/compose the number String numberStr = null; if (newInvoice.getIsReview() != null && newInvoice.getIsReview() == 1) { // invoices for review will be seen by the entity employees // so the entity locale will be used EntityBL entity = new EntityBL(entityId); ResourceBundle bundle = ResourceBundle.getBundle("entityNotifications", entity.getLocale()); numberStr = bundle.getString("invoice.review.number"); } else if (newInvoice.getPublicNumber() == null || newInvoice.getPublicNumber().length() == 0) { String prefix; try { pref.set(entityId, Constants.PREFERENCE_INVOICE_PREFIX); prefix = pref.getString(); if (prefix == null) { prefix = ""; } } catch (EmptyResultDataAccessException e) { prefix = ""; } int number; try { pref.set(entityId, Constants.PREFERENCE_INVOICE_NUMBER); number = pref.getInt(); } catch (EmptyResultDataAccessException e1) { number = 1; } numberStr = prefix + number; // update for the next time number++; pref.createUpdateForEntity(entityId, Constants.PREFERENCE_INVOICE_NUMBER, number); } else { // for upload of legacy invoices numberStr = newInvoice.getPublicNumber(); } invoice.setPublicNumber(numberStr); // set the invoice's contact info with the current user's primary ContactBL contact = new ContactBL(); contact.set(userId); contact.createForInvoice(contact.getDTO(), invoice.getId()); // add a log row for convenience if (null != executorUserId) { eLogger.audit( executorUserId, userId, Constants.TABLE_INVOICE, invoice.getId(), EventLogger.MODULE_INVOICE_MAINTENANCE, EventLogger.ROW_CREATED, null, null, null); } else { eLogger.auditBySystem( entityId, userId, Constants.TABLE_INVOICE, invoice.getId(), EventLogger.MODULE_INVOICE_MAINTENANCE, EventLogger.ROW_CREATED, null, null, null); } }
protected void processRules(OrderDTO newOrder) throws TaskException { // now we have the line with good defaults, the order and the item // These have to be visible to the rules KnowledgeBase knowledgeBase; try { knowledgeBase = readKnowledgeBase(); } catch (Exception e) { throw new TaskException(e); } session = knowledgeBase.newStatefulKnowledgeSession(); List<Object> rulesMemoryContext = new ArrayList<Object>(); rulesMemoryContext.add(helperOrder); // add OrderDTO to rules memory context newOrder.setCurrency(new CurrencyDAS().find(newOrder.getCurrency().getId())); if (newOrder.getCreateDate() == null) { newOrder.setCreateDate(new Date()); } rulesMemoryContext.add(newOrder); for (OrderLineDTO line : newOrder.getLines()) { if (line.getItem() != null) { ItemBL item = new ItemBL(line.getItemId()); rulesMemoryContext.add( item.getDTO( helperOrder.getLanguage(), helperOrder.getUserId(), helperOrder.getEntityId(), helperOrder.getCurrencyId())); } rulesMemoryContext.add(line); } if (newOrder.getPricingFields() != null && newOrder.getPricingFields().size() > 0) { for (PricingField pf : newOrder.getPricingFields()) { rulesMemoryContext.add(pf); } } try { Integer userId = newOrder.getBaseUserByUserId().getId(); UserDTOEx user = DTOFactory.getUserDTOEx(userId); rulesMemoryContext.add(user); ContactBL contact = new ContactBL(); contact.set(userId); ContactDTOEx contactDTO = contact.getDTO(); rulesMemoryContext.add(contactDTO); // Add the subscriptions OrderBL order = new OrderBL(); for (OrderDTO myOrder : order.getActiveRecurringByUser(userId)) { for (OrderLineDTO myLine : myOrder.getLines()) { rulesMemoryContext.add(new Subscription(myLine)); } } } catch (Exception e) { throw new TaskException(e); } session.setGlobal("order", helperOrder); // then execute the rules executeStatefulRules(session, rulesMemoryContext); }