Example usage for java.math BigDecimal abs

List of usage examples for java.math BigDecimal abs

Introduction

In this page you can find the example usage for java.math BigDecimal abs.

Prototype

public BigDecimal abs() 

Source Link

Document

Returns a BigDecimal whose value is the absolute value of this BigDecimal , and whose scale is this.scale() .

Usage

From source file:org.efaps.esjp.accounting.util.data.ImportDetails.java

protected List<Document> checkAccounts(final Parameter _parameter, final File _file,
        final Map<String, Instance> _docMap, final DateTime _date, final Boolean _inverse)
        throws IOException, EFapsException {
    final List<Document> ret = new ArrayList<>();
    final CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"));
    final List<String[]> entries = reader.readAll();
    reader.close();/*  w w  w .  ja  v a2  s  .c o  m*/
    entries.remove(0);
    int i = 1;
    final Map<String, Document> map = new HashMap<>();
    for (final String[] row : entries) {
        i++;
        final String docNumber = row[0];
        final String ruc = row[1];
        final String dateStr = row[2];
        final String accountStr = row[5];
        final String accountDesc = row[4];
        final DecimalFormat formater = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
        formater.setParseBigDecimal(true);
        final String amountMEStr = row[6];
        final String amountMNStr = row[7];

        final QueryBuilder queryBldr = new QueryBuilder(CIAccounting.AccountAbstract);
        queryBldr.addWhereAttrEqValue(CIAccounting.AccountAbstract.Name, accountStr.trim());
        final InstanceQuery query = queryBldr.getQuery();
        query.executeWithoutAccessCheck();
        if (query.next()) {
            ImportDetails.LOG.info("Found account: '{}' ", accountStr);
            final String[] docSplit = docNumber.split("-");
            if (docSplit.length != 2 && _docMap != null) {
                ImportDetails.LOG.warn(
                        "Document '{}'  - Line: {} has no '-' to distinguish SerialNumber and No.", docNumber,
                        i);
            } else {
                try {
                    final Formatter criteria = new Formatter();
                    String name = docNumber;
                    if (_docMap != null) {
                        final String serialNo = docSplit[0];
                        final String docNo = docSplit[1];
                        final int serial = Integer.parseInt(serialNo.trim().replaceAll("\\D", ""));
                        final int no = Integer.parseInt(docNo.trim().replaceAll("\\D", ""));
                        criteria.format("%03d-%06d", serial, no);
                        name = criteria.toString();
                    }

                    Document doc;
                    if (map.containsKey(name)) {
                        doc = map.get(name);
                    } else {
                        if (_docMap != null && _docMap.containsKey(name)) {
                            doc = new Document(name, _docMap.get(name), ruc, dateStr, accountDesc);
                        } else {
                            doc = new Document(name, null, ruc, dateStr, accountDesc);
                        }
                    }

                    BigDecimal amountME = (BigDecimal) formater.parse(amountMEStr);
                    BigDecimal amountMN = (BigDecimal) formater.parse(amountMNStr);

                    if (_inverse) {
                        amountME = amountME.negate();
                        amountMN = amountMN.negate();
                    }

                    if (amountMN.compareTo(BigDecimal.ZERO) >= 0) {
                        doc.addAmountMECredit(amountME);
                        doc.addAmountMNCredit(amountMN);
                    } else {
                        doc.addAmountMEDebit(amountME);
                        doc.addAmountMNDebit(amountMN);
                    }

                    final Map<String, Account> accounts = doc.getAccounts();
                    Account acc;
                    if (accounts.containsKey(accountStr)) {
                        acc = accounts.get(accountStr);
                    } else {
                        acc = new Account(accountStr, accountDesc);
                        accounts.put(accountStr, acc);
                    }
                    acc.addAmountME(amountME);
                    acc.addAmountMN(amountMN);
                    acc.setInstance(query.getCurrentValue());

                    map.put(name, doc);

                    criteria.close();
                } catch (final NumberFormatException e) {
                    ImportDetails.LOG.error("wrong format for document '{}'", docNumber);
                } catch (final ParseException e) {
                    ImportDetails.LOG.error("wrong format for amounts '{}' - '{}'", amountMEStr, amountMNStr);
                }
            }
        } else {
            ImportDetails.LOG.error("Not found account: {}", accountStr);
        }
    }

    final Instance periodInst = getPeriodInstance();
    for (final Document doc : map.values()) {
        final BigDecimal amountCreditMN = doc.getAmountMNCredit() != null ? doc.getAmountMNCredit()
                : BigDecimal.ZERO;
        final BigDecimal amountDebitMN = doc.getAmountMNDebit() != null ? doc.getAmountMNDebit()
                : BigDecimal.ZERO;
        final BigDecimal amountMN = amountCreditMN.add(amountDebitMN);
        final BigDecimal amountCreditME = doc.getAmountMECredit() != null ? doc.getAmountMECredit()
                : BigDecimal.ZERO;
        final BigDecimal amountDebitME = doc.getAmountMEDebit() != null ? doc.getAmountMEDebit()
                : BigDecimal.ZERO;
        final BigDecimal amountME = amountCreditME.add(amountDebitME);
        if (BigDecimal.ZERO.compareTo(amountMN) == 0 && BigDecimal.ZERO.compareTo(amountME) == 0) {
            ImportDetails.LOG.info(
                    "For Document: '{}'. Sum of Credit with Debit Amount (ME): '{}' + '{}' and Credit with Debit Amount (MN): '{}' + '{}' are Zero (0)",
                    doc.getName(), amountCreditME, amountDebitME, amountCreditMN, amountDebitMN);
        } else {
            ImportDetails.LOG.error(
                    "For Document: '{}'. Sum of Credit with Debit Amount (ME): '{}' + '{}' = '{}' and Credit with Debit Amount (MN): '{}' + '{}' = '{}'",
                    doc.getName(), amountCreditME, amountDebitME, amountME, amountCreditMN, amountDebitMN,
                    amountMN);
        }

        final Insert insert = new Insert(CIAccounting.TransactionOpeningBalance);
        insert.add(CIAccounting.TransactionOpeningBalance.Date, _date);
        final StringBuilder descBldr = new StringBuilder()
                .append(doc.getInstance() != null ? doc.getInstance().getType().getLabel() : "Sin Documento")
                .append(": ").append(doc.getName()).append(" - RUC: ").append(doc.getRuc()).append(" - ")
                .append(doc.getDate()).append(" - ").append(doc.getDesc());

        insert.add(CIAccounting.TransactionOpeningBalance.Description, descBldr.toString());
        insert.add(CIAccounting.TransactionOpeningBalance.Status,
                Status.find(CIAccounting.TransactionStatus.Open));
        insert.add(CIAccounting.TransactionOpeningBalance.PeriodLink, periodInst);
        insert.executeWithoutAccessCheck();

        if (_docMap != null) {
            final Instance instance = insert.getInstance();
            new Create().connectDocs2Transaction(_parameter, instance, doc.getInstance());
        }

        final Map<String, Account> accounts = doc.getAccounts();
        final Instance basCur = Currency.getBaseCurrency();
        for (final Account acc : accounts.values()) {
            final Insert insertpos = new Insert(
                    acc.getAmountMN().compareTo(BigDecimal.ZERO) > 0 ? CIAccounting.TransactionPositionCredit
                            : CIAccounting.TransactionPositionDebit);
            insertpos.add(CIAccounting.TransactionPositionAbstract.AccountLink, acc.getInstance());
            insertpos.add(CIAccounting.TransactionPositionAbstract.Amount, acc.getAmountMN());
            insertpos.add(CIAccounting.TransactionPositionAbstract.CurrencyLink, basCur);
            insertpos.add(CIAccounting.TransactionPositionAbstract.Rate, acc.getRateObject());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateAmount, acc.getAmountME());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateCurrencyLink, 1);
            insertpos.add(CIAccounting.TransactionPositionAbstract.TransactionLink, insert.getInstance());
            insertpos.executeWithoutAccessCheck();
        }

        if (amountCreditMN.compareTo(amountDebitMN.abs()) != 0
                && amountCreditMN.subtract(amountDebitMN.abs()).abs().compareTo(new BigDecimal("0.05")) <= 0) {
            Insert insertpos = null;
            Account acc = null;
            if (amountCreditMN.compareTo(amountDebitMN.abs()) > 0) {
                acc = getRoundingAccount(AccountingSettings.PERIOD_ROUNDINGDEBIT);
                acc.addAmountMN(amountCreditMN.subtract(amountDebitMN.abs()).negate());
                acc.addAmountME(amountCreditME.subtract(amountDebitME.abs()).negate());
                insertpos = new Insert(CIAccounting.TransactionPositionDebit);
            } else {
                acc = getRoundingAccount(AccountingSettings.PERIOD_ROUNDINGCREDIT);
                acc.addAmountMN(amountDebitMN.abs().subtract(amountCreditMN));
                acc.addAmountME(amountDebitME.abs().subtract(amountCreditME));
                insertpos = new Insert(CIAccounting.TransactionPositionCredit);
            }
            insertpos.add(CIAccounting.TransactionPositionAbstract.AccountLink, acc.getInstance());
            insertpos.add(CIAccounting.TransactionPositionAbstract.Amount, acc.getAmountMN());
            insertpos.add(CIAccounting.TransactionPositionAbstract.CurrencyLink, basCur);
            insertpos.add(CIAccounting.TransactionPositionAbstract.Rate, acc.getRateObject());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateAmount, acc.getAmountME());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateCurrencyLink, 1);
            insertpos.add(CIAccounting.TransactionPositionAbstract.TransactionLink, insert.getInstance());
            insertpos.executeWithoutAccessCheck();
        } else if (amountCreditMN.compareTo(amountDebitMN.abs()) != 0
                && amountCreditMN.subtract(amountDebitMN.abs()).abs().compareTo(new BigDecimal("0.05")) > 0) {
            Insert insertpos = null;
            final Account acc = getRoundingAccount(AccountingSettings.PERIOD_TRANSFERACCOUNT);
            ;
            if (amountCreditMN.compareTo(amountDebitMN.abs()) > 0) {
                acc.addAmountMN(amountCreditMN.subtract(amountDebitMN.abs()).negate());
                acc.addAmountME(amountCreditME.subtract(amountDebitME.abs()).negate());
                insertpos = new Insert(CIAccounting.TransactionPositionDebit);
            } else {
                acc.addAmountMN(amountDebitMN.abs().subtract(amountCreditMN));
                acc.addAmountME(amountDebitME.abs().subtract(amountCreditME));
                insertpos = new Insert(CIAccounting.TransactionPositionCredit);
            }
            insertpos.add(CIAccounting.TransactionPositionAbstract.AccountLink, acc.getInstance());
            insertpos.add(CIAccounting.TransactionPositionAbstract.Amount, acc.getAmountMN());
            insertpos.add(CIAccounting.TransactionPositionAbstract.CurrencyLink, basCur);
            insertpos.add(CIAccounting.TransactionPositionAbstract.Rate, acc.getRateObject());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateAmount, acc.getAmountME());
            insertpos.add(CIAccounting.TransactionPositionAbstract.RateCurrencyLink, 1);
            insertpos.add(CIAccounting.TransactionPositionAbstract.TransactionLink, insert.getInstance());
            insertpos.executeWithoutAccessCheck();
        }
    }

    return ret;
}

From source file:org.ofbiz.accounting.invoice.InvoiceServices.java

public static Map<String, Object> createInvoiceForOrder(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");

    if (DECIMALS == -1 || ROUNDING == -1) {
        return ServiceUtil.returnError(
                UtilProperties.getMessage(resource, "AccountingAritmeticPropertiesNotConfigured", locale));
    }/*from  w w  w.j a  v  a2s. c  o m*/

    String orderId = (String) context.get("orderId");
    List<GenericValue> billItems = UtilGenerics.checkList(context.get("billItems"));
    String invoiceId = (String) context.get("invoiceId");

    if (UtilValidate.isEmpty(billItems)) {
        Debug.logVerbose("No order items to invoice; not creating invoice; returning success", module);
        return ServiceUtil
                .returnSuccess(UtilProperties.getMessage(resource, "AccountingNoOrderItemsToInvoice", locale));
    }

    try {
        GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId)
                .queryOne();
        if (orderHeader == null) {
            return ServiceUtil
                    .returnError(UtilProperties.getMessage(resource, "AccountingNoOrderHeader", locale));
        }

        // figure out the invoice type
        String invoiceType = null;

        String orderType = orderHeader.getString("orderTypeId");
        if (orderType.equals("SALES_ORDER")) {
            invoiceType = "SALES_INVOICE";
        } else if (orderType.equals("PURCHASE_ORDER")) {
            invoiceType = "PURCHASE_INVOICE";
        }

        // Set the precision depending on the type of invoice
        int invoiceTypeDecimals = UtilNumber.getBigDecimalScale("invoice." + invoiceType + ".decimals");
        if (invoiceTypeDecimals == -1)
            invoiceTypeDecimals = DECIMALS;

        // Make an order read helper from the order
        OrderReadHelper orh = new OrderReadHelper(orderHeader);

        // get the product store
        GenericValue productStore = orh.getProductStore();

        // get the shipping adjustment mode (Y = Pro-Rate; N = First-Invoice)
        String prorateShipping = productStore != null ? productStore.getString("prorateShipping") : "Y";
        if (prorateShipping == null) {
            prorateShipping = "Y";
        }

        // get the billing parties
        String billToCustomerPartyId = orh.getBillToParty().getString("partyId");
        String billFromVendorPartyId = orh.getBillFromParty().getString("partyId");

        // get some price totals
        BigDecimal shippableAmount = orh.getShippableTotal(null);
        BigDecimal orderSubTotal = orh.getOrderItemsSubTotal();

        // these variables are for pro-rating order amounts across invoices, so they should not be rounded off for maximum accuracy
        BigDecimal invoiceShipProRateAmount = ZERO;
        BigDecimal invoiceSubTotal = ZERO;
        BigDecimal invoiceQuantity = ZERO;

        GenericValue billingAccount = orderHeader.getRelatedOne("BillingAccount", false);
        String billingAccountId = billingAccount != null ? billingAccount.getString("billingAccountId") : null;

        Timestamp invoiceDate = (Timestamp) context.get("eventDate");
        if (UtilValidate.isEmpty(invoiceDate)) {
            // TODO: ideally this should be the same time as when a shipment is sent and be passed in as a parameter
            invoiceDate = UtilDateTime.nowTimestamp();
        }
        // TODO: perhaps consider billing account net days term as well?
        Long orderTermNetDays = orh.getOrderTermNetDays();
        Timestamp dueDate = null;
        if (orderTermNetDays != null) {
            dueDate = UtilDateTime.getDayEnd(invoiceDate, orderTermNetDays);
        }

        // create the invoice record
        if (UtilValidate.isEmpty(invoiceId)) {
            Map<String, Object> createInvoiceContext = FastMap.newInstance();
            createInvoiceContext.put("partyId", billToCustomerPartyId);
            createInvoiceContext.put("partyIdFrom", billFromVendorPartyId);
            createInvoiceContext.put("billingAccountId", billingAccountId);
            createInvoiceContext.put("invoiceDate", invoiceDate);
            createInvoiceContext.put("dueDate", dueDate);
            createInvoiceContext.put("invoiceTypeId", invoiceType);
            // start with INVOICE_IN_PROCESS, in the INVOICE_READY we can't change the invoice (or shouldn't be able to...)
            createInvoiceContext.put("statusId", "INVOICE_IN_PROCESS");
            createInvoiceContext.put("currencyUomId", orderHeader.getString("currencyUom"));
            createInvoiceContext.put("userLogin", userLogin);

            // store the invoice first
            Map<String, Object> createInvoiceResult = dispatcher.runSync("createInvoice", createInvoiceContext);
            if (ServiceUtil.isError(createInvoiceResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, createInvoiceResult);
            }

            // call service, not direct entity op: delegator.create(invoice);
            invoiceId = (String) createInvoiceResult.get("invoiceId");
        }

        // order roles to invoice roles
        List<GenericValue> orderRoles = orderHeader.getRelated("OrderRole", null, null, false);
        Map<String, Object> createInvoiceRoleContext = FastMap.newInstance();
        createInvoiceRoleContext.put("invoiceId", invoiceId);
        createInvoiceRoleContext.put("userLogin", userLogin);
        for (GenericValue orderRole : orderRoles) {
            createInvoiceRoleContext.put("partyId", orderRole.getString("partyId"));
            createInvoiceRoleContext.put("roleTypeId", orderRole.getString("roleTypeId"));
            Map<String, Object> createInvoiceRoleResult = dispatcher.runSync("createInvoiceRole",
                    createInvoiceRoleContext);
            if (ServiceUtil.isError(createInvoiceRoleResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, createInvoiceRoleResult);
            }
        }

        // order terms to invoice terms.
        // TODO: it might be nice to filter OrderTerms to only copy over financial terms.
        List<GenericValue> orderTerms = orh.getOrderTerms();
        createInvoiceTerms(delegator, dispatcher, invoiceId, orderTerms, userLogin, locale);

        // billing accounts
        // List billingAccountTerms = null;
        // for billing accounts we will use related information
        if (billingAccount != null) {
            /*
             * jacopoc: billing account terms were already copied as order terms
             *          when the order was created.
            // get the billing account terms
            billingAccountTerms = billingAccount.getRelated("BillingAccountTerm", null, null, false);
                    
            // set the invoice terms as defined for the billing account
            createInvoiceTerms(delegator, dispatcher, invoiceId, billingAccountTerms, userLogin, locale);
            */
            // set the invoice bill_to_customer from the billing account
            List<GenericValue> billToRoles = billingAccount.getRelated("BillingAccountRole",
                    UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), null, false);
            for (GenericValue billToRole : billToRoles) {
                if (!(billToRole.getString("partyId").equals(billToCustomerPartyId))) {
                    createInvoiceRoleContext = UtilMisc.toMap("invoiceId", invoiceId, "partyId",
                            billToRole.get("partyId"), "roleTypeId", "BILL_TO_CUSTOMER", "userLogin",
                            userLogin);
                    Map<String, Object> createInvoiceRoleResult = dispatcher.runSync("createInvoiceRole",
                            createInvoiceRoleContext);
                    if (ServiceUtil.isError(createInvoiceRoleResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceRoleFromOrder", locale),
                                null, null, createInvoiceRoleResult);
                    }
                }
            }

            // set the bill-to contact mech as the contact mech of the billing account
            if (UtilValidate.isNotEmpty(billingAccount.getString("contactMechId"))) {
                Map<String, Object> createBillToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                        "contactMechId", billingAccount.getString("contactMechId"), "contactMechPurposeTypeId",
                        "BILLING_LOCATION", "userLogin", userLogin);
                Map<String, Object> createBillToContactMechResult = dispatcher
                        .runSync("createInvoiceContactMech", createBillToContactMechContext);
                if (ServiceUtil.isError(createBillToContactMechResult)) {
                    return ServiceUtil.returnError(
                            UtilProperties.getMessage(resource,
                                    "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                            null, null, createBillToContactMechResult);
                }
            }
        } else {
            List<GenericValue> billingLocations = orh.getBillingLocations();
            if (UtilValidate.isNotEmpty(billingLocations)) {
                for (GenericValue ocm : billingLocations) {
                    Map<String, Object> createBillToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                            "contactMechId", ocm.getString("contactMechId"), "contactMechPurposeTypeId",
                            "BILLING_LOCATION", "userLogin", userLogin);
                    Map<String, Object> createBillToContactMechResult = dispatcher
                            .runSync("createInvoiceContactMech", createBillToContactMechContext);
                    if (ServiceUtil.isError(createBillToContactMechResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                                null, null, createBillToContactMechResult);
                    }
                }
            } else {
                Debug.logWarning("No billing locations found for order [" + orderId
                        + "] and none were created for Invoice [" + invoiceId + "]", module);
            }
        }

        // get a list of the payment method types
        //DEJ20050705 doesn't appear to be used: List paymentPreferences = orderHeader.getRelated("OrderPaymentPreference", null, null, false);

        // create the bill-from (or pay-to) contact mech as the primary PAYMENT_LOCATION of the party from the store
        GenericValue payToAddress = null;
        if (invoiceType.equals("PURCHASE_INVOICE")) {
            // for purchase orders, the pay to address is the BILLING_LOCATION of the vendor
            GenericValue billFromVendor = orh.getPartyFromRole("BILL_FROM_VENDOR");
            if (billFromVendor != null) {
                List<GenericValue> billingContactMechs = billFromVendor.getRelatedOne("Party", false)
                        .getRelated("PartyContactMechPurpose",
                                UtilMisc.toMap("contactMechPurposeTypeId", "BILLING_LOCATION"), null, false);
                if (UtilValidate.isNotEmpty(billingContactMechs)) {
                    payToAddress = EntityUtil.getFirst(billingContactMechs);
                }
            }
        } else {
            // for sales orders, it is the payment address on file for the store
            payToAddress = PaymentWorker.getPaymentAddress(delegator, productStore.getString("payToPartyId"));
        }
        if (payToAddress != null) {
            Map<String, Object> createPayToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                    "contactMechId", payToAddress.getString("contactMechId"), "contactMechPurposeTypeId",
                    "PAYMENT_LOCATION", "userLogin", userLogin);
            Map<String, Object> createPayToContactMechResult = dispatcher.runSync("createInvoiceContactMech",
                    createPayToContactMechContext);
            if (ServiceUtil.isError(createPayToContactMechResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource,
                                "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                        null, null, createPayToContactMechResult);
            }
        }

        // sequence for items - all OrderItems or InventoryReservations + all Adjustments
        int invoiceItemSeqNum = 1;
        String invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                INVOICE_ITEM_SEQUENCE_ID_DIGITS);

        // create the item records
        for (GenericValue currentValue : billItems) {
            GenericValue itemIssuance = null;
            GenericValue orderItem = null;
            GenericValue shipmentReceipt = null;
            if ("ItemIssuance".equals(currentValue.getEntityName())) {
                itemIssuance = currentValue;
            } else if ("OrderItem".equals(currentValue.getEntityName())) {
                orderItem = currentValue;
            } else if ("ShipmentReceipt".equals(currentValue.getEntityName())) {
                shipmentReceipt = currentValue;
            } else {
                Debug.logError("Unexpected entity " + currentValue + " of type " + currentValue.getEntityName(),
                        module);
            }

            if (orderItem == null && itemIssuance != null) {
                orderItem = itemIssuance.getRelatedOne("OrderItem", false);
            } else if ((orderItem == null) && (shipmentReceipt != null)) {
                orderItem = shipmentReceipt.getRelatedOne("OrderItem", false);
            } else if ((orderItem == null) && (itemIssuance == null) && (shipmentReceipt == null)) {
                Debug.logError(
                        "Cannot create invoice when orderItem, itemIssuance, and shipmentReceipt are all null",
                        module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                        "AccountingIllegalValuesPassedToCreateInvoiceService", locale));
            }
            GenericValue product = null;
            if (orderItem.get("productId") != null) {
                product = orderItem.getRelatedOne("Product", false);
            }

            // get some quantities
            BigDecimal billingQuantity = null;
            if (itemIssuance != null) {
                billingQuantity = itemIssuance.getBigDecimal("quantity");
                BigDecimal cancelQty = itemIssuance.getBigDecimal("cancelQuantity");
                if (cancelQty == null) {
                    cancelQty = ZERO;
                }
                billingQuantity = billingQuantity.subtract(cancelQty).setScale(DECIMALS, ROUNDING);
            } else if (shipmentReceipt != null) {
                billingQuantity = shipmentReceipt.getBigDecimal("quantityAccepted");
            } else {
                BigDecimal orderedQuantity = OrderReadHelper.getOrderItemQuantity(orderItem);
                BigDecimal invoicedQuantity = OrderReadHelper.getOrderItemInvoicedQuantity(orderItem);
                billingQuantity = orderedQuantity.subtract(invoicedQuantity);
                if (billingQuantity.compareTo(ZERO) < 0) {
                    billingQuantity = ZERO;
                }
            }
            if (billingQuantity == null)
                billingQuantity = ZERO;

            // check if shipping applies to this item.  Shipping is calculated for sales invoices, not purchase invoices.
            boolean shippingApplies = false;
            if ((product != null) && (ProductWorker.shippingApplies(product))
                    && (invoiceType.equals("SALES_INVOICE"))) {
                shippingApplies = true;
            }

            BigDecimal billingAmount = orderItem.getBigDecimal("unitPrice").setScale(invoiceTypeDecimals,
                    ROUNDING);

            Map<String, Object> createInvoiceItemContext = FastMap.newInstance();
            createInvoiceItemContext.put("invoiceId", invoiceId);
            createInvoiceItemContext.put("invoiceItemSeqId", invoiceItemSeqId);
            createInvoiceItemContext.put("invoiceItemTypeId",
                    getInvoiceItemType(delegator, (orderItem.getString("orderItemTypeId")),
                            (product == null ? null : product.getString("productTypeId")), invoiceType,
                            "INV_FPROD_ITEM"));
            createInvoiceItemContext.put("description", orderItem.get("itemDescription"));
            createInvoiceItemContext.put("quantity", billingQuantity);
            createInvoiceItemContext.put("amount", billingAmount);
            createInvoiceItemContext.put("productId", orderItem.get("productId"));
            createInvoiceItemContext.put("productFeatureId", orderItem.get("productFeatureId"));
            createInvoiceItemContext.put("overrideGlAccountId", orderItem.get("overrideGlAccountId"));
            //createInvoiceItemContext.put("uomId", "");
            createInvoiceItemContext.put("userLogin", userLogin);

            String itemIssuanceId = null;
            if (itemIssuance != null && itemIssuance.get("inventoryItemId") != null) {
                itemIssuanceId = itemIssuance.getString("itemIssuanceId");
                createInvoiceItemContext.put("inventoryItemId", itemIssuance.get("inventoryItemId"));
            }
            // similarly, tax only for purchase invoices
            if ((product != null) && (invoiceType.equals("SALES_INVOICE"))) {
                createInvoiceItemContext.put("taxableFlag", product.get("taxable"));
            }

            Map<String, Object> createInvoiceItemResult = dispatcher.runSync("createInvoiceItem",
                    createInvoiceItemContext);
            if (ServiceUtil.isError(createInvoiceItemResult)) {
                return ServiceUtil
                        .returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceItemFromOrder", locale),
                                null, null, createInvoiceItemResult);
            }

            // this item total
            BigDecimal thisAmount = billingAmount.multiply(billingQuantity).setScale(invoiceTypeDecimals,
                    ROUNDING);

            // add to the ship amount only if it applies to this item
            if (shippingApplies) {
                invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAmount)
                        .setScale(invoiceTypeDecimals, ROUNDING);
            }

            // increment the invoice subtotal
            invoiceSubTotal = invoiceSubTotal.add(thisAmount).setScale(100, ROUNDING);

            // increment the invoice quantity
            invoiceQuantity = invoiceQuantity.add(billingQuantity).setScale(invoiceTypeDecimals, ROUNDING);

            // create the OrderItemBilling record
            Map<String, Object> createOrderItemBillingContext = FastMap.newInstance();
            createOrderItemBillingContext.put("invoiceId", invoiceId);
            createOrderItemBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
            createOrderItemBillingContext.put("orderId", orderItem.get("orderId"));
            createOrderItemBillingContext.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
            createOrderItemBillingContext.put("itemIssuanceId", itemIssuanceId);
            createOrderItemBillingContext.put("quantity", billingQuantity);
            createOrderItemBillingContext.put("amount", billingAmount);
            createOrderItemBillingContext.put("userLogin", userLogin);
            if ((shipmentReceipt != null) && (shipmentReceipt.getString("receiptId") != null)) {
                createOrderItemBillingContext.put("shipmentReceiptId", shipmentReceipt.getString("receiptId"));
            }

            Map<String, Object> createOrderItemBillingResult = dispatcher.runSync("createOrderItemBilling",
                    createOrderItemBillingContext);
            if (ServiceUtil.isError(createOrderItemBillingResult)) {
                return ServiceUtil
                        .returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingOrderItemBillingFromOrder", locale),
                                null, null, createOrderItemBillingResult);
            }

            if ("ItemIssuance".equals(currentValue.getEntityName())) {
                List<GenericValue> shipmentItemBillings = EntityQuery.use(delegator).from("ShipmentItemBilling")
                        .where("shipmentId", currentValue.get("shipmentId"), "shipmentItemSeqId",
                                currentValue.get("shipmentItemSeqId"))
                        .queryList();
                if (UtilValidate.isEmpty(shipmentItemBillings)) {

                    // create the ShipmentItemBilling record
                    GenericValue shipmentItemBilling = delegator.makeValue("ShipmentItemBilling",
                            UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId));
                    shipmentItemBilling.put("shipmentId", currentValue.get("shipmentId"));
                    shipmentItemBilling.put("shipmentItemSeqId", currentValue.get("shipmentItemSeqId"));
                    shipmentItemBilling.create();
                }
            }

            String parentInvoiceItemSeqId = invoiceItemSeqId;
            // increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);

            // Get the original order item from the DB, in case the quantity has been overridden
            GenericValue originalOrderItem = EntityQuery.use(delegator).from("OrderItem")
                    .where("orderId", orderId, "orderItemSeqId", orderItem.get("orderItemSeqId")).queryOne();

            // create the item adjustment as line items
            List<GenericValue> itemAdjustments = OrderReadHelper.getOrderItemAdjustmentList(orderItem,
                    orh.getAdjustments());
            for (GenericValue adj : itemAdjustments) {

                // Check against OrderAdjustmentBilling to see how much of this adjustment has already been invoiced
                BigDecimal adjAlreadyInvoicedAmount = null;
                try {
                    Map<String, Object> checkResult = dispatcher.runSync("calculateInvoicedAdjustmentTotal",
                            UtilMisc.toMap("orderAdjustment", adj));
                    adjAlreadyInvoicedAmount = (BigDecimal) checkResult.get("invoicedTotal");
                } catch (GenericServiceException e) {
                    Debug.logError(e, "Accounting trouble calling calculateInvoicedAdjustmentTotal service",
                            module);
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                            "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
                }

                // If the absolute invoiced amount >= the abs of the adjustment amount, the full amount has already been invoiced,
                //  so skip this adjustment
                if (adj.get("amount") == null) { // JLR 17/4/7 : fix a bug coming from POS in case of use of a discount (on item(s) or sale, item(s) here) and a cash amount higher than total (hence issuing change)
                    continue;
                }
                if (adjAlreadyInvoicedAmount.abs().compareTo(
                        adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).abs()) > 0) {
                    continue;
                }

                BigDecimal originalOrderItemQuantity = OrderReadHelper.getOrderItemQuantity(originalOrderItem);
                BigDecimal amount = ZERO;
                if (originalOrderItemQuantity.signum() != 0) {
                    if (adj.get("amount") != null) {
                        // pro-rate the amount
                        // set decimals = 100 means we don't round this intermediate value, which is very important
                        amount = adj.getBigDecimal("amount").divide(originalOrderItemQuantity, 100, ROUNDING);
                        amount = amount.multiply(billingQuantity);
                        // Tax needs to be rounded differently from other order adjustments
                        if (adj.getString("orderAdjustmentTypeId").equals("SALES_TAX")) {
                            amount = amount.setScale(TAX_DECIMALS, TAX_ROUNDING);
                        } else {
                            amount = amount.setScale(invoiceTypeDecimals, ROUNDING);
                        }
                    } else if (adj.get("sourcePercentage") != null) {
                        // pro-rate the amount
                        // set decimals = 100 means we don't round this intermediate value, which is very important
                        BigDecimal percent = adj.getBigDecimal("sourcePercentage");
                        percent = percent.divide(new BigDecimal(100), 100, ROUNDING);
                        amount = billingAmount.multiply(percent);
                        amount = amount.divide(originalOrderItemQuantity, 100, ROUNDING);
                        amount = amount.multiply(billingQuantity);
                        amount = amount.setScale(invoiceTypeDecimals, ROUNDING);
                    }
                }
                if (amount.signum() != 0) {
                    Map<String, Object> createInvoiceItemAdjContext = FastMap.newInstance();
                    createInvoiceItemAdjContext.put("invoiceId", invoiceId);
                    createInvoiceItemAdjContext.put("invoiceItemSeqId", invoiceItemSeqId);
                    createInvoiceItemAdjContext.put("invoiceItemTypeId", getInvoiceItemType(delegator,
                            adj.getString("orderAdjustmentTypeId"), null, invoiceType, "INVOICE_ITM_ADJ"));
                    createInvoiceItemAdjContext.put("quantity", BigDecimal.ONE);
                    createInvoiceItemAdjContext.put("amount", amount);
                    createInvoiceItemAdjContext.put("productId", orderItem.get("productId"));
                    createInvoiceItemAdjContext.put("productFeatureId", orderItem.get("productFeatureId"));
                    createInvoiceItemAdjContext.put("overrideGlAccountId", adj.get("overrideGlAccountId"));
                    createInvoiceItemAdjContext.put("parentInvoiceId", invoiceId);
                    createInvoiceItemAdjContext.put("parentInvoiceItemSeqId", parentInvoiceItemSeqId);
                    //createInvoiceItemAdjContext.put("uomId", "");
                    createInvoiceItemAdjContext.put("userLogin", userLogin);
                    createInvoiceItemAdjContext.put("taxAuthPartyId", adj.get("taxAuthPartyId"));
                    createInvoiceItemAdjContext.put("taxAuthGeoId", adj.get("taxAuthGeoId"));
                    createInvoiceItemAdjContext.put("taxAuthorityRateSeqId", adj.get("taxAuthorityRateSeqId"));

                    // some adjustments fill out the comments field instead
                    String description = (UtilValidate.isEmpty(adj.getString("description"))
                            ? adj.getString("comments")
                            : adj.getString("description"));
                    createInvoiceItemAdjContext.put("description", description);

                    // invoice items for sales tax are not taxable themselves
                    // TODO: This is not an ideal solution. Instead, we need to use OrderAdjustment.includeInTax when it is implemented
                    if (!(adj.getString("orderAdjustmentTypeId").equals("SALES_TAX"))) {
                        createInvoiceItemAdjContext.put("taxableFlag", product.get("taxable"));
                    }

                    // If the OrderAdjustment is associated to a ProductPromo,
                    // and the field ProductPromo.overrideOrgPartyId is set,
                    // copy the value to InvoiceItem.overrideOrgPartyId: this
                    // represent an organization override for the payToPartyId
                    if (UtilValidate.isNotEmpty(adj.getString("productPromoId"))) {
                        try {
                            GenericValue productPromo = adj.getRelatedOne("ProductPromo", false);
                            if (UtilValidate.isNotEmpty(productPromo.getString("overrideOrgPartyId"))) {
                                createInvoiceItemAdjContext.put("overrideOrgPartyId",
                                        productPromo.getString("overrideOrgPartyId"));
                            }
                        } catch (GenericEntityException e) {
                            Debug.logError(e, "Error looking up ProductPromo with id ["
                                    + adj.getString("productPromoId") + "]", module);
                        }
                    }

                    Map<String, Object> createInvoiceItemAdjResult = dispatcher.runSync("createInvoiceItem",
                            createInvoiceItemAdjContext);
                    if (ServiceUtil.isError(createInvoiceItemAdjResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceItemFromOrder", locale),
                                null, null, createInvoiceItemAdjResult);
                    }

                    // Create the OrderAdjustmentBilling record
                    Map<String, Object> createOrderAdjustmentBillingContext = FastMap.newInstance();
                    createOrderAdjustmentBillingContext.put("orderAdjustmentId",
                            adj.getString("orderAdjustmentId"));
                    createOrderAdjustmentBillingContext.put("invoiceId", invoiceId);
                    createOrderAdjustmentBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
                    createOrderAdjustmentBillingContext.put("amount", amount);
                    createOrderAdjustmentBillingContext.put("userLogin", userLogin);

                    Map<String, Object> createOrderAdjustmentBillingResult = dispatcher
                            .runSync("createOrderAdjustmentBilling", createOrderAdjustmentBillingContext);
                    if (ServiceUtil.isError(createOrderAdjustmentBillingResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingOrderAdjustmentBillingFromOrder", locale),
                                null, null, createOrderAdjustmentBillingContext);
                    }

                    // this adjustment amount
                    BigDecimal thisAdjAmount = amount;

                    // adjustments only apply to totals when they are not tax or shipping adjustments
                    if (!"SALES_TAX".equals(adj.getString("orderAdjustmentTypeId"))
                            && !"SHIPPING_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))) {
                        // increment the invoice subtotal
                        invoiceSubTotal = invoiceSubTotal.add(thisAdjAmount).setScale(100, ROUNDING);

                        // add to the ship amount only if it applies to this item
                        if (shippingApplies) {
                            invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAdjAmount)
                                    .setScale(invoiceTypeDecimals, ROUNDING);
                        }
                    }

                    // increment the counter
                    invoiceItemSeqNum++;
                    invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                            INVOICE_ITEM_SEQUENCE_ID_DIGITS);
                }
            }
        }

        // create header adjustments as line items -- always to tax/shipping last
        Map<GenericValue, BigDecimal> shipAdjustments = FastMap.newInstance();
        Map<GenericValue, BigDecimal> taxAdjustments = FastMap.newInstance();

        List<GenericValue> headerAdjustments = orh.getOrderHeaderAdjustments();
        for (GenericValue adj : headerAdjustments) {

            // Check against OrderAdjustmentBilling to see how much of this adjustment has already been invoiced
            BigDecimal adjAlreadyInvoicedAmount = null;
            try {
                Map<String, Object> checkResult = dispatcher.runSync("calculateInvoicedAdjustmentTotal",
                        UtilMisc.toMap("orderAdjustment", adj));
                adjAlreadyInvoicedAmount = ((BigDecimal) checkResult.get("invoicedTotal"))
                        .setScale(invoiceTypeDecimals, ROUNDING);
            } catch (GenericServiceException e) {
                Debug.logError(e, "Accounting trouble calling calculateInvoicedAdjustmentTotal service",
                        module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                        "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
            }

            // If the absolute invoiced amount >= the abs of the adjustment amount, the full amount has already been invoiced,
            //  so skip this adjustment
            if (null == adj.get("amount")) { // JLR 17/4/7 : fix a bug coming from POS in case of use of a discount (on item(s) or sale, sale here) and a cash amount higher than total (hence issuing change)
                continue;
            }
            if (adjAlreadyInvoicedAmount.abs()
                    .compareTo(adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).abs()) > 0) {
                continue;
            }

            if ("SHIPPING_CHARGES".equals(adj.getString("orderAdjustmentTypeId"))) {
                shipAdjustments.put(adj, adjAlreadyInvoicedAmount);
            } else if ("SALES_TAX".equals(adj.getString("orderAdjustmentTypeId"))) {
                taxAdjustments.put(adj, adjAlreadyInvoicedAmount);
            } else {
                // these will effect the shipping pro-rate (unless commented)
                // other adjustment type
                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, orderSubTotal,
                        invoiceSubTotal, adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING),
                        invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
                // invoiceShipProRateAmount += adjAmount;
                // do adjustments compound or are they based off subtotal? Here we will (unless commented)
                // invoiceSubTotal += adjAmount;

                // increment the counter
                invoiceItemSeqNum++;
                invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                        INVOICE_ITEM_SEQUENCE_ID_DIGITS);
            }
        }

        // next do the shipping adjustments.  Note that we do not want to add these to the invoiceSubTotal or orderSubTotal for pro-rating tax later, as that would cause
        // numerator/denominator problems when the shipping is not pro-rated but rather charged all on the first invoice
        for (GenericValue adj : shipAdjustments.keySet()) {
            BigDecimal adjAlreadyInvoicedAmount = shipAdjustments.get(adj);

            if ("N".equalsIgnoreCase(prorateShipping)) {

                // Set the divisor and multiplier to 1 to avoid prorating
                BigDecimal divisor = BigDecimal.ONE;
                BigDecimal multiplier = BigDecimal.ONE;

                // The base amount in this case is the adjustment amount minus the total already invoiced for that adjustment, since
                //  it won't be prorated
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING)
                        .subtract(adjAlreadyInvoicedAmount);
                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
                        baseAmount, invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
            } else {

                // Pro-rate the shipping amount based on shippable information
                BigDecimal divisor = shippableAmount;
                BigDecimal multiplier = invoiceShipProRateAmount;

                // The base amount in this case is the adjustment amount, since we want to prorate based on the full amount
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING);
                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
                        baseAmount, invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
            }

            // Increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);
        }

        // last do the tax adjustments
        String prorateTaxes = productStore != null ? productStore.getString("prorateTaxes") : "Y";
        if (prorateTaxes == null) {
            prorateTaxes = "Y";
        }
        for (Map.Entry<GenericValue, BigDecimal> entry : taxAdjustments.entrySet()) {
            GenericValue adj = entry.getKey();
            BigDecimal adjAlreadyInvoicedAmount = entry.getValue();
            BigDecimal adjAmount = null;

            if ("N".equalsIgnoreCase(prorateTaxes)) {

                // Set the divisor and multiplier to 1 to avoid prorating
                BigDecimal divisor = BigDecimal.ONE;
                BigDecimal multiplier = BigDecimal.ONE;

                // The base amount in this case is the adjustment amount minus the total already invoiced for that adjustment, since
                //  it won't be prorated
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(TAX_DECIMALS, TAX_ROUNDING)
                        .subtract(adjAlreadyInvoicedAmount);
                adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor,
                        multiplier, baseAmount, TAX_DECIMALS, TAX_ROUNDING, userLogin, dispatcher, locale);
            } else {

                // Pro-rate the tax amount based on shippable information
                BigDecimal divisor = orderSubTotal;
                BigDecimal multiplier = invoiceSubTotal;

                // The base amount in this case is the adjustment amount, since we want to prorate based on the full amount
                BigDecimal baseAmount = adj.getBigDecimal("amount");
                adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor,
                        multiplier, baseAmount, TAX_DECIMALS, TAX_ROUNDING, userLogin, dispatcher, locale);
            }
            invoiceSubTotal = invoiceSubTotal.add(adjAmount).setScale(invoiceTypeDecimals, ROUNDING);

            // Increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);
        }

        // check for previous order payments
        List<GenericValue> orderPaymentPrefs = EntityQuery.use(delegator).from("OrderPaymentPreference").where(
                EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderId),
                EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED"))
                .queryList();
        List<GenericValue> currentPayments = FastList.newInstance();
        for (GenericValue paymentPref : orderPaymentPrefs) {
            List<GenericValue> payments = paymentPref.getRelated("Payment", null, null, false);
            currentPayments.addAll(payments);
        }
        // apply these payments to the invoice if they have any remaining amount to apply
        for (GenericValue payment : currentPayments) {
            if ("PMNT_VOID".equals(payment.getString("statusId"))
                    || "PMNT_CANCELLED".equals(payment.getString("statusId"))) {
                continue;
            }
            BigDecimal notApplied = PaymentWorker.getPaymentNotApplied(payment);
            if (notApplied.signum() > 0) {
                Map<String, Object> appl = FastMap.newInstance();
                appl.put("paymentId", payment.get("paymentId"));
                appl.put("invoiceId", invoiceId);
                appl.put("billingAccountId", billingAccountId);
                appl.put("amountApplied", notApplied);
                appl.put("userLogin", userLogin);
                Map<String, Object> createPayApplResult = dispatcher.runSync("createPaymentApplication", appl);
                if (ServiceUtil.isError(createPayApplResult)) {
                    return ServiceUtil
                            .returnError(
                                    UtilProperties.getMessage(resource,
                                            "AccountingErrorCreatingInvoiceFromOrder", locale),
                                    null, null, createPayApplResult);
                }
            }
        }

        // Should all be in place now. Depending on the ProductStore.autoApproveInvoice setting, set status to INVOICE_READY (unless it's a purchase invoice, which we set to INVOICE_IN_PROCESS)
        String autoApproveInvoice = productStore != null ? productStore.getString("autoApproveInvoice") : "Y";
        if (!"N".equals(autoApproveInvoice)) {
            String nextStatusId = "PURCHASE_INVOICE".equals(invoiceType) ? "INVOICE_IN_PROCESS"
                    : "INVOICE_READY";
            Map<String, Object> setInvoiceStatusResult = dispatcher.runSync("setInvoiceStatus",
                    UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "statusId", nextStatusId,
                            "userLogin", userLogin));
            if (ServiceUtil.isError(setInvoiceStatusResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, setInvoiceStatusResult);
            }
        }

        Map<String, Object> resp = ServiceUtil.returnSuccess();
        resp.put("invoiceId", invoiceId);
        resp.put("invoiceTypeId", invoiceType);
        return resp;
    } catch (GenericEntityException e) {
        Debug.logError(e, "Entity/data problem creating invoice from order items: " + e.toString(), module);
        return ServiceUtil.returnError(
                UtilProperties.getMessage(resource, "AccountingEntityDataProblemCreatingInvoiceFromOrderItems",
                        UtilMisc.toMap("reason", e.toString()), locale));
    } catch (GenericServiceException e) {
        Debug.logError(e, "Service/other problem creating invoice from order items: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                "AccountingServiceOtherProblemCreatingInvoiceFromOrderItems",
                UtilMisc.toMap("reason", e.toString()), locale));
    }
}

From source file:org.efaps.esjp.accounting.transaction.Recalculate_Base.java

/**
 * Creates the gain loss4 document account.
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @return the return// w ww.j a  v a  2s  .  com
 * @throws EFapsException on error
 */
public Return createGainLoss4DocumentAccount(final Parameter _parameter) throws EFapsException {
    final Set<String> setAccounts = getAccounts4DocumentConfig(_parameter);

    final String[] oidsDoc = (String[]) Context.getThreadContext().getSessionAttribute(
            CIFormAccounting.Accounting_GainLoss4DocumentAccountForm.selectedDocuments.name);
    final DateTime dateTo = new DateTime(_parameter
            .getParameterValue(CIFormAccounting.Accounting_GainLoss4DocumentAccountForm.transactionDate.name));
    for (final String oid : oidsDoc) {
        final Instance instDoc = Instance.get(oid);

        final QueryBuilder attrQuerBldr = new QueryBuilder(CIAccounting.Transaction2SalesDocument);
        attrQuerBldr.addWhereAttrEqValue(CIAccounting.Transaction2SalesDocument.ToLink, instDoc);
        final AttributeQuery attrQuery = attrQuerBldr
                .getAttributeQuery(CIAccounting.Transaction2SalesDocument.FromLink);

        // filter classification Document
        final QueryBuilder attrQueryBldr2 = new QueryBuilder(CIAccounting.Transaction);
        attrQueryBldr2.addWhereAttrEqValue(CIAccounting.Transaction.PeriodLink,
                _parameter.getInstance().getId());
        attrQueryBldr2.addWhereAttrInQuery(CIAccounting.Transaction.ID, attrQuery);
        final AttributeQuery attrQuery2 = attrQuerBldr.getAttributeQuery(CIAccounting.Transaction.ID);

        final QueryBuilder queryBldr = new QueryBuilder(CIAccounting.TransactionPositionAbstract);
        queryBldr.addWhereAttrInQuery(CIAccounting.TransactionPositionAbstract.TransactionLink, attrQuery2);
        final MultiPrintQuery multi = queryBldr.getPrint();
        multi.addAttribute(CIAccounting.TransactionPositionAbstract.Amount,
                CIAccounting.TransactionPositionAbstract.RateAmount,
                CIAccounting.TransactionPositionAbstract.CurrencyLink,
                CIAccounting.TransactionPositionAbstract.RateCurrencyLink);
        final SelectBuilder selAcc = new SelectBuilder()
                .linkto(CIAccounting.TransactionPositionAbstract.AccountLink).oid();
        multi.addSelect(selAcc);
        multi.execute();
        final Map<String, AccountInfo> map = new HashMap<>();
        while (multi.next()) {
            final Instance accountInst = Instance.get(multi.<String>getSelect(selAcc));
            if (setAccounts.contains(accountInst.getOid())) {
                final BigDecimal oldRateAmount = multi
                        .<BigDecimal>getAttribute(CIAccounting.TransactionPositionAbstract.RateAmount);
                final BigDecimal oldAmount = multi
                        .<BigDecimal>getAttribute(CIAccounting.TransactionPositionAbstract.Amount);
                final Instance targetCurrInst = Instance.get(CIERP.Currency.getType(),
                        multi.<Long>getAttribute(CIAccounting.TransactionPositionAbstract.RateCurrencyLink));
                final Instance currentInst = Instance.get(CIERP.Currency.getType(),
                        multi.<Long>getAttribute(CIAccounting.TransactionPositionAbstract.CurrencyLink));

                final PriceUtil priceUtil = new PriceUtil();
                final BigDecimal[] rates = priceUtil.getRates(_parameter, targetCurrInst, currentInst);
                final BigDecimal rate = rates[2];
                final BigDecimal newAmount = oldRateAmount.divide(rate, BigDecimal.ROUND_HALF_UP);

                BigDecimal gainloss = BigDecimal.ZERO;
                if (!currentInst.equals(targetCurrInst)) {
                    gainloss = newAmount.subtract(oldAmount);
                } else {
                    gainloss = newAmount;
                }
                if (map.containsKey(accountInst.getOid())) {
                    final AccountInfo tarAcc = map.get(accountInst.getOid());
                    tarAcc.addAmount(gainloss);
                } else {
                    final AccountInfo tarAcc = new AccountInfo(accountInst, gainloss);
                    tarAcc.setAmountRate(gainloss);
                    tarAcc.setCurrInstance(currentInst);
                    map.put(accountInst.getOid(), tarAcc);
                }
            }
        }

        if (!map.isEmpty()) {
            Insert insert = null;
            CurrencyInst curr = null;
            BigDecimal gainlossSum = BigDecimal.ZERO;
            for (final Entry<String, AccountInfo> entry : map.entrySet()) {
                final AccountInfo tarAcc = entry.getValue();
                final Instance instAcc = Instance.get(entry.getKey());
                final BigDecimal gainloss = tarAcc.getAmount();
                gainlossSum = gainlossSum.add(gainloss);
                final Instance currInstance = tarAcc.getCurrInstance();
                curr = new CurrencyInst(currInstance);
                final Map<String, String[]> mapVal = validateInfo(_parameter, gainloss);
                final String[] accs = mapVal.get("accs");
                final String[] check = mapVal.get("check");

                if (checkAccounts(accs, 0, check).length() > 0) {
                    if (gainloss.compareTo(BigDecimal.ZERO) != 0) {
                        final String[] accOids = mapVal.get("accountOids");
                        if (insert == null) {
                            final DateTimeFormatter formater = DateTimeFormat.mediumDate();
                            final String dateStr = dateTo
                                    .withChronology(Context.getThreadContext().getChronology())
                                    .toString(formater.withLocale(Context.getThreadContext().getLocale()));
                            final StringBuilder description = new StringBuilder();
                            description
                                    .append(DBProperties
                                            .getProperty("Accounting_DocumentAccountForm.TxnRecalculate.Label"))
                                    .append(" ").append(dateStr);
                            insert = new Insert(CIAccounting.Transaction);
                            insert.add(CIAccounting.Transaction.Description, description.toString());
                            insert.add(CIAccounting.Transaction.Date, dateTo);
                            insert.add(CIAccounting.Transaction.PeriodLink, _parameter.getInstance().getId());
                            insert.add(CIAccounting.Transaction.Status,
                                    Status.find(CIAccounting.TransactionStatus.uuid, "Open").getId());
                            insert.execute();
                        }

                        Insert insertPos = new Insert(CIAccounting.TransactionPositionCredit);
                        insertPos.add(CIAccounting.TransactionPositionCredit.TransactionLink, insert.getId());
                        if (gainloss.signum() < 0) {
                            insertPos.add(CIAccounting.TransactionPositionCredit.AccountLink, instAcc.getId());
                        } else {
                            insertPos.add(CIAccounting.TransactionPositionCredit.AccountLink,
                                    Instance.get(accOids[0]).getId());
                        }
                        insertPos.add(CIAccounting.TransactionPositionCredit.Amount, gainloss.abs());
                        insertPos.add(CIAccounting.TransactionPositionCredit.RateAmount, gainloss.abs());
                        insertPos.add(CIAccounting.TransactionPositionCredit.CurrencyLink,
                                currInstance.getId());
                        insertPos.add(CIAccounting.TransactionPositionCredit.RateCurrencyLink,
                                currInstance.getId());
                        insertPos.add(CIAccounting.TransactionPositionCredit.Rate,
                                new Object[] { BigDecimal.ONE, BigDecimal.ONE });
                        insertPos.execute();

                        insertPos = new Insert(CIAccounting.TransactionPositionDebit);
                        insertPos.add(CIAccounting.TransactionPositionDebit.TransactionLink, insert.getId());
                        if (gainloss.signum() < 0) {
                            insertPos.add(CIAccounting.TransactionPositionDebit.AccountLink,
                                    Instance.get(accOids[0]).getId());
                        } else {
                            insertPos.add(CIAccounting.TransactionPositionDebit.AccountLink, instAcc.getId());
                        }
                        insertPos.add(CIAccounting.TransactionPositionDebit.Amount, gainloss.abs().negate());
                        insertPos.add(CIAccounting.TransactionPositionDebit.RateAmount,
                                gainloss.abs().negate());
                        insertPos.add(CIAccounting.TransactionPositionDebit.CurrencyLink, currInstance.getId());
                        insertPos.add(CIAccounting.TransactionPositionDebit.RateCurrencyLink,
                                currInstance.getId());
                        insertPos.add(CIAccounting.TransactionPositionDebit.Rate,
                                new Object[] { BigDecimal.ONE, BigDecimal.ONE });
                        insertPos.execute();
                    }
                }
            }
            if (insert != null) {
                final Instance instance = insert.getInstance();
                // create classifications
                final Classification classification1 = (Classification) CIAccounting.TransactionClass.getType();
                final Insert relInsert1 = new Insert(classification1.getClassifyRelationType());
                relInsert1.add(classification1.getRelLinkAttributeName(), instance.getId());
                relInsert1.add(classification1.getRelTypeAttributeName(), classification1.getId());
                relInsert1.execute();

                final Insert classInsert1 = new Insert(classification1);
                classInsert1.add(classification1.getLinkAttributeName(), instance.getId());
                classInsert1.execute();

                final Classification classification = (Classification) CIAccounting.TransactionClassGainLoss
                        .getType();
                final Insert relInsert = new Insert(classification.getClassifyRelationType());
                relInsert.add(classification.getRelLinkAttributeName(), instance.getId());
                relInsert.add(classification.getRelTypeAttributeName(), classification.getId());
                relInsert.execute();

                final Insert classInsert = new Insert(classification);
                classInsert.add(classification.getLinkAttributeName(), instance.getId());
                classInsert.add(CIAccounting.TransactionClassGainLoss.Amount, gainlossSum);
                classInsert.add(CIAccounting.TransactionClassGainLoss.RateAmount, gainlossSum);
                classInsert.add(CIAccounting.TransactionClassGainLoss.CurrencyLink, curr.getInstance().getId());
                classInsert.add(CIAccounting.TransactionClassGainLoss.RateCurrencyLink,
                        curr.getInstance().getId());
                classInsert.add(CIAccounting.TransactionClassGainLoss.Rate,
                        new Object[] { BigDecimal.ONE, BigDecimal.ONE });
                classInsert.execute();

                new Create().connectDocs2Transaction(_parameter, instance, instDoc);
            }
        }
    }

    return new Return();
}

From source file:org.apache.ofbiz.accounting.invoice.InvoiceServices.java

public static Map<String, Object> createInvoiceForOrder(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");

    if (DECIMALS == -1 || ROUNDING == -1) {
        return ServiceUtil.returnError(
                UtilProperties.getMessage(resource, "AccountingAritmeticPropertiesNotConfigured", locale));
    }/*  w ww.jav a2s  . c o m*/

    String orderId = (String) context.get("orderId");
    List<GenericValue> billItems = UtilGenerics.checkList(context.get("billItems"));
    String invoiceId = (String) context.get("invoiceId");

    if (UtilValidate.isEmpty(billItems)) {
        Debug.logVerbose("No order items to invoice; not creating invoice; returning success", module);
        return ServiceUtil
                .returnSuccess(UtilProperties.getMessage(resource, "AccountingNoOrderItemsToInvoice", locale));
    }

    try {
        GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId)
                .queryOne();
        if (orderHeader == null) {
            return ServiceUtil
                    .returnError(UtilProperties.getMessage(resource, "AccountingNoOrderHeader", locale));
        }

        // figure out the invoice type
        String invoiceType = null;

        String orderType = orderHeader.getString("orderTypeId");
        if (orderType.equals("SALES_ORDER")) {
            invoiceType = "SALES_INVOICE";
        } else if (orderType.equals("PURCHASE_ORDER")) {
            invoiceType = "PURCHASE_INVOICE";
        }

        // Set the precision depending on the type of invoice
        int invoiceTypeDecimals = UtilNumber.getBigDecimalScale("invoice." + invoiceType + ".decimals");
        if (invoiceTypeDecimals == -1)
            invoiceTypeDecimals = DECIMALS;

        // Make an order read helper from the order
        OrderReadHelper orh = new OrderReadHelper(orderHeader);

        // get the product store
        GenericValue productStore = orh.getProductStore();

        // get the shipping adjustment mode (Y = Pro-Rate; N = First-Invoice)
        String prorateShipping = productStore != null ? productStore.getString("prorateShipping") : "Y";
        if (prorateShipping == null) {
            prorateShipping = "Y";
        }

        // get the billing parties
        String billToCustomerPartyId = orh.getBillToParty().getString("partyId");
        String billFromVendorPartyId = orh.getBillFromParty().getString("partyId");

        // get some price totals
        BigDecimal shippableAmount = orh.getShippableTotal(null);
        BigDecimal shippableQuantity = orh.getShippableQuantity(null);
        BigDecimal orderSubTotal = orh.getOrderItemsSubTotal();
        BigDecimal orderQuantity = orh.getTotalOrderItemsQuantity();

        // these variables are for pro-rating order amounts across invoices, so they should not be rounded off for maximum accuracy
        BigDecimal invoiceShipProRateAmount = ZERO;
        BigDecimal invoiceShippableQuantity = ZERO;
        BigDecimal invoiceSubTotal = ZERO;
        BigDecimal invoiceQuantity = ZERO;

        GenericValue billingAccount = orderHeader.getRelatedOne("BillingAccount", false);
        String billingAccountId = billingAccount != null ? billingAccount.getString("billingAccountId") : null;

        Timestamp invoiceDate = (Timestamp) context.get("eventDate");
        if (UtilValidate.isEmpty(invoiceDate)) {
            // TODO: ideally this should be the same time as when a shipment is sent and be passed in as a parameter
            invoiceDate = UtilDateTime.nowTimestamp();
        }
        // TODO: perhaps consider billing account net days term as well?
        Long orderTermNetDays = orh.getOrderTermNetDays();
        Timestamp dueDate = null;
        if (orderTermNetDays != null) {
            dueDate = UtilDateTime.getDayEnd(invoiceDate, orderTermNetDays);
        }

        // create the invoice record
        if (UtilValidate.isEmpty(invoiceId)) {
            Map<String, Object> createInvoiceContext = new HashMap<String, Object>();
            createInvoiceContext.put("partyId", billToCustomerPartyId);
            createInvoiceContext.put("partyIdFrom", billFromVendorPartyId);
            createInvoiceContext.put("billingAccountId", billingAccountId);
            createInvoiceContext.put("invoiceDate", invoiceDate);
            createInvoiceContext.put("dueDate", dueDate);
            createInvoiceContext.put("invoiceTypeId", invoiceType);
            // start with INVOICE_IN_PROCESS, in the INVOICE_READY we can't change the invoice (or shouldn't be able to...)
            createInvoiceContext.put("statusId", "INVOICE_IN_PROCESS");
            createInvoiceContext.put("currencyUomId", orderHeader.getString("currencyUom"));
            createInvoiceContext.put("userLogin", userLogin);

            // store the invoice first
            Map<String, Object> createInvoiceResult = dispatcher.runSync("createInvoice", createInvoiceContext);
            if (ServiceUtil.isError(createInvoiceResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, createInvoiceResult);
            }

            // call service, not direct entity op: delegator.create(invoice);
            invoiceId = (String) createInvoiceResult.get("invoiceId");
        }

        // order roles to invoice roles
        List<GenericValue> orderRoles = orderHeader.getRelated("OrderRole", null, null, false);
        Map<String, Object> createInvoiceRoleContext = new HashMap<String, Object>();
        createInvoiceRoleContext.put("invoiceId", invoiceId);
        createInvoiceRoleContext.put("userLogin", userLogin);
        for (GenericValue orderRole : orderRoles) {
            createInvoiceRoleContext.put("partyId", orderRole.getString("partyId"));
            createInvoiceRoleContext.put("roleTypeId", orderRole.getString("roleTypeId"));
            Map<String, Object> createInvoiceRoleResult = dispatcher.runSync("createInvoiceRole",
                    createInvoiceRoleContext);
            if (ServiceUtil.isError(createInvoiceRoleResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, createInvoiceRoleResult);
            }
        }

        // order terms to invoice terms.
        // TODO: it might be nice to filter OrderTerms to only copy over financial terms.
        List<GenericValue> orderTerms = orh.getOrderTerms();
        createInvoiceTerms(delegator, dispatcher, invoiceId, orderTerms, userLogin, locale);

        // billing accounts
        // List billingAccountTerms = null;
        // for billing accounts we will use related information
        if (billingAccount != null) {
            /*
             * jacopoc: billing account terms were already copied as order terms
             *          when the order was created.
            // get the billing account terms
            billingAccountTerms = billingAccount.getRelated("BillingAccountTerm", null, null, false);
                    
            // set the invoice terms as defined for the billing account
            createInvoiceTerms(delegator, dispatcher, invoiceId, billingAccountTerms, userLogin, locale);
            */
            // set the invoice bill_to_customer from the billing account
            List<GenericValue> billToRoles = billingAccount.getRelated("BillingAccountRole",
                    UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), null, false);
            for (GenericValue billToRole : billToRoles) {
                if (!(billToRole.getString("partyId").equals(billToCustomerPartyId))) {
                    createInvoiceRoleContext = UtilMisc.toMap("invoiceId", invoiceId, "partyId",
                            billToRole.get("partyId"), "roleTypeId", "BILL_TO_CUSTOMER", "userLogin",
                            userLogin);
                    Map<String, Object> createInvoiceRoleResult = dispatcher.runSync("createInvoiceRole",
                            createInvoiceRoleContext);
                    if (ServiceUtil.isError(createInvoiceRoleResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceRoleFromOrder", locale),
                                null, null, createInvoiceRoleResult);
                    }
                }
            }

            // set the bill-to contact mech as the contact mech of the billing account
            if (UtilValidate.isNotEmpty(billingAccount.getString("contactMechId"))) {
                Map<String, Object> createBillToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                        "contactMechId", billingAccount.getString("contactMechId"), "contactMechPurposeTypeId",
                        "BILLING_LOCATION", "userLogin", userLogin);
                Map<String, Object> createBillToContactMechResult = dispatcher
                        .runSync("createInvoiceContactMech", createBillToContactMechContext);
                if (ServiceUtil.isError(createBillToContactMechResult)) {
                    return ServiceUtil.returnError(
                            UtilProperties.getMessage(resource,
                                    "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                            null, null, createBillToContactMechResult);
                }
            }
        } else {
            List<GenericValue> billingLocations = orh.getBillingLocations();
            if (UtilValidate.isNotEmpty(billingLocations)) {
                for (GenericValue ocm : billingLocations) {
                    Map<String, Object> createBillToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                            "contactMechId", ocm.getString("contactMechId"), "contactMechPurposeTypeId",
                            "BILLING_LOCATION", "userLogin", userLogin);
                    Map<String, Object> createBillToContactMechResult = dispatcher
                            .runSync("createInvoiceContactMech", createBillToContactMechContext);
                    if (ServiceUtil.isError(createBillToContactMechResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                                null, null, createBillToContactMechResult);
                    }
                }
            } else {
                Debug.logWarning("No billing locations found for order [" + orderId
                        + "] and none were created for Invoice [" + invoiceId + "]", module);
            }
        }

        // get a list of the payment method types
        //DEJ20050705 doesn't appear to be used: List paymentPreferences = orderHeader.getRelated("OrderPaymentPreference", null, null, false);

        // create the bill-from (or pay-to) contact mech as the primary PAYMENT_LOCATION of the party from the store
        GenericValue payToAddress = null;
        if (invoiceType.equals("PURCHASE_INVOICE")) {
            // for purchase orders, the pay to address is the BILLING_LOCATION of the vendor
            GenericValue billFromVendor = orh.getPartyFromRole("BILL_FROM_VENDOR");
            if (billFromVendor != null) {
                List<GenericValue> billingContactMechs = billFromVendor.getRelatedOne("Party", false)
                        .getRelated("PartyContactMechPurpose",
                                UtilMisc.toMap("contactMechPurposeTypeId", "BILLING_LOCATION"), null, false);
                if (UtilValidate.isNotEmpty(billingContactMechs)) {
                    payToAddress = EntityUtil.getFirst(billingContactMechs);
                }
            }
        } else {
            // for sales orders, it is the payment address on file for the store
            payToAddress = PaymentWorker.getPaymentAddress(delegator, productStore.getString("payToPartyId"));
        }
        if (payToAddress != null) {
            Map<String, Object> createPayToContactMechContext = UtilMisc.toMap("invoiceId", invoiceId,
                    "contactMechId", payToAddress.getString("contactMechId"), "contactMechPurposeTypeId",
                    "PAYMENT_LOCATION", "userLogin", userLogin);
            Map<String, Object> createPayToContactMechResult = dispatcher.runSync("createInvoiceContactMech",
                    createPayToContactMechContext);
            if (ServiceUtil.isError(createPayToContactMechResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource,
                                "AccountingErrorCreatingInvoiceContactMechFromOrder", locale),
                        null, null, createPayToContactMechResult);
            }
        }

        // sequence for items - all OrderItems or InventoryReservations + all Adjustments
        int invoiceItemSeqNum = 1;
        String invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                INVOICE_ITEM_SEQUENCE_ID_DIGITS);

        // create the item records
        for (GenericValue currentValue : billItems) {
            GenericValue itemIssuance = null;
            GenericValue orderItem = null;
            GenericValue shipmentReceipt = null;
            if ("ItemIssuance".equals(currentValue.getEntityName())) {
                itemIssuance = currentValue;
            } else if ("OrderItem".equals(currentValue.getEntityName())) {
                orderItem = currentValue;
            } else if ("ShipmentReceipt".equals(currentValue.getEntityName())) {
                shipmentReceipt = currentValue;
            } else {
                Debug.logError("Unexpected entity " + currentValue + " of type " + currentValue.getEntityName(),
                        module);
            }

            if (orderItem == null && itemIssuance != null) {
                orderItem = itemIssuance.getRelatedOne("OrderItem", false);
            } else if ((orderItem == null) && (shipmentReceipt != null)) {
                orderItem = shipmentReceipt.getRelatedOne("OrderItem", false);
            } else if ((orderItem == null) && (itemIssuance == null) && (shipmentReceipt == null)) {
                Debug.logError(
                        "Cannot create invoice when orderItem, itemIssuance, and shipmentReceipt are all null",
                        module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                        "AccountingIllegalValuesPassedToCreateInvoiceService", locale));
            }
            GenericValue product = null;
            if (orderItem.get("productId") != null) {
                product = orderItem.getRelatedOne("Product", false);
            }

            // get some quantities
            BigDecimal billingQuantity = null;
            if (itemIssuance != null) {
                billingQuantity = itemIssuance.getBigDecimal("quantity");
                BigDecimal cancelQty = itemIssuance.getBigDecimal("cancelQuantity");
                if (cancelQty == null) {
                    cancelQty = ZERO;
                }
                billingQuantity = billingQuantity.subtract(cancelQty).setScale(DECIMALS, ROUNDING);
            } else if (shipmentReceipt != null) {
                billingQuantity = shipmentReceipt.getBigDecimal("quantityAccepted");
            } else {
                BigDecimal orderedQuantity = OrderReadHelper.getOrderItemQuantity(orderItem);
                BigDecimal invoicedQuantity = OrderReadHelper.getOrderItemInvoicedQuantity(orderItem);
                billingQuantity = orderedQuantity.subtract(invoicedQuantity);
                if (billingQuantity.compareTo(ZERO) < 0) {
                    billingQuantity = ZERO;
                }
            }
            if (billingQuantity == null)
                billingQuantity = ZERO;

            // check if shipping applies to this item.  Shipping is calculated for sales invoices, not purchase invoices.
            boolean shippingApplies = false;
            if ((product != null) && (ProductWorker.shippingApplies(product))
                    && (invoiceType.equals("SALES_INVOICE"))) {
                shippingApplies = true;
            }

            BigDecimal billingAmount = BigDecimal.ZERO;
            GenericValue OrderAdjustment = EntityUtil.getFirst(orderItem.getRelated("OrderAdjustment",
                    UtilMisc.toMap("orderAdjustmentTypeId", "VAT_TAX"), null, false));
            /* Apply formula to get actual product price to set amount in invoice item
            Formula is: productPrice = (productPriceWithTax.multiply(100)) / (orderAdj sourcePercentage + 100))
            product price = (43*100) / (20+100) = 35.83 (Here product price is 43 with VAT)
             */
            if (UtilValidate.isNotEmpty(OrderAdjustment)
                    && (OrderAdjustment.getBigDecimal("amount").signum() == 0)
                    && UtilValidate.isNotEmpty(OrderAdjustment.getBigDecimal("amountAlreadyIncluded"))
                    && OrderAdjustment.getBigDecimal("amountAlreadyIncluded").signum() != 0) {
                BigDecimal sourcePercentageTotal = OrderAdjustment.getBigDecimal("sourcePercentage")
                        .add(new BigDecimal(100));
                billingAmount = orderItem.getBigDecimal("unitPrice")
                        .divide(sourcePercentageTotal, 100, ROUNDING).multiply(new BigDecimal(100))
                        .setScale(invoiceTypeDecimals, ROUNDING);
            } else {
                billingAmount = orderItem.getBigDecimal("unitPrice").setScale(invoiceTypeDecimals, ROUNDING);
            }

            Map<String, Object> createInvoiceItemContext = new HashMap<String, Object>();
            createInvoiceItemContext.put("invoiceId", invoiceId);
            createInvoiceItemContext.put("invoiceItemSeqId", invoiceItemSeqId);
            createInvoiceItemContext.put("invoiceItemTypeId",
                    getInvoiceItemType(delegator, (orderItem.getString("orderItemTypeId")),
                            (product == null ? null : product.getString("productTypeId")), invoiceType,
                            "INV_FPROD_ITEM"));
            createInvoiceItemContext.put("description", orderItem.get("itemDescription"));
            createInvoiceItemContext.put("quantity", billingQuantity);
            createInvoiceItemContext.put("amount", billingAmount);
            createInvoiceItemContext.put("productId", orderItem.get("productId"));
            createInvoiceItemContext.put("productFeatureId", orderItem.get("productFeatureId"));
            createInvoiceItemContext.put("overrideGlAccountId", orderItem.get("overrideGlAccountId"));
            createInvoiceItemContext.put("userLogin", userLogin);

            String itemIssuanceId = null;
            if (itemIssuance != null && itemIssuance.get("inventoryItemId") != null) {
                itemIssuanceId = itemIssuance.getString("itemIssuanceId");
                createInvoiceItemContext.put("inventoryItemId", itemIssuance.get("inventoryItemId"));
            }
            // similarly, tax only for purchase invoices
            if ((product != null) && (invoiceType.equals("SALES_INVOICE"))) {
                createInvoiceItemContext.put("taxableFlag", product.get("taxable"));
            }

            Map<String, Object> createInvoiceItemResult = dispatcher.runSync("createInvoiceItem",
                    createInvoiceItemContext);
            if (ServiceUtil.isError(createInvoiceItemResult)) {
                return ServiceUtil
                        .returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceItemFromOrder", locale),
                                null, null, createInvoiceItemResult);
            }

            // this item total
            BigDecimal thisAmount = billingAmount.multiply(billingQuantity).setScale(invoiceTypeDecimals,
                    ROUNDING);

            // add to the ship amount only if it applies to this item
            if (shippingApplies) {
                invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAmount)
                        .setScale(invoiceTypeDecimals, ROUNDING);
                invoiceShippableQuantity = invoiceQuantity.add(billingQuantity).setScale(invoiceTypeDecimals,
                        ROUNDING);
            }

            // increment the invoice subtotal
            invoiceSubTotal = invoiceSubTotal.add(thisAmount).setScale(100, ROUNDING);

            // increment the invoice quantity
            invoiceQuantity = invoiceQuantity.add(billingQuantity).setScale(invoiceTypeDecimals, ROUNDING);

            // create the OrderItemBilling record
            Map<String, Object> createOrderItemBillingContext = new HashMap<String, Object>();
            createOrderItemBillingContext.put("invoiceId", invoiceId);
            createOrderItemBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
            createOrderItemBillingContext.put("orderId", orderItem.get("orderId"));
            createOrderItemBillingContext.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
            createOrderItemBillingContext.put("itemIssuanceId", itemIssuanceId);
            createOrderItemBillingContext.put("quantity", billingQuantity);
            createOrderItemBillingContext.put("amount", billingAmount);
            createOrderItemBillingContext.put("userLogin", userLogin);
            if ((shipmentReceipt != null) && (shipmentReceipt.getString("receiptId") != null)) {
                createOrderItemBillingContext.put("shipmentReceiptId", shipmentReceipt.getString("receiptId"));
            }

            Map<String, Object> createOrderItemBillingResult = dispatcher.runSync("createOrderItemBilling",
                    createOrderItemBillingContext);
            if (ServiceUtil.isError(createOrderItemBillingResult)) {
                return ServiceUtil
                        .returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingOrderItemBillingFromOrder", locale),
                                null, null, createOrderItemBillingResult);
            }

            if ("ItemIssuance".equals(currentValue.getEntityName())) {
                /* Find ShipmentItemBilling based on shipmentId, shipmentItemSeqId, invoiceId, invoiceItemSeqId as
                   because if any order item has multiple quantity and reserved by multiple inventories then there will be multiple invoice items.
                   In that case ShipmentItemBilling was creating only for one invoice item. Fixed under OFBIZ-6806.
                */
                List<GenericValue> shipmentItemBillings = EntityQuery.use(delegator).from("ShipmentItemBilling")
                        .where("shipmentId", currentValue.get("shipmentId"), "shipmentItemSeqId",
                                currentValue.get("shipmentItemSeqId"), "invoiceId", invoiceId,
                                "invoiceItemSeqId", invoiceItemSeqId)
                        .queryList();
                if (UtilValidate.isEmpty(shipmentItemBillings)) {

                    // create the ShipmentItemBilling record
                    GenericValue shipmentItemBilling = delegator.makeValue("ShipmentItemBilling",
                            UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId));
                    shipmentItemBilling.put("shipmentId", currentValue.get("shipmentId"));
                    shipmentItemBilling.put("shipmentItemSeqId", currentValue.get("shipmentItemSeqId"));
                    shipmentItemBilling.create();
                }
            }

            String parentInvoiceItemSeqId = invoiceItemSeqId;
            // increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);

            // Get the original order item from the DB, in case the quantity has been overridden
            GenericValue originalOrderItem = EntityQuery.use(delegator).from("OrderItem")
                    .where("orderId", orderId, "orderItemSeqId", orderItem.get("orderItemSeqId")).queryOne();

            // create the item adjustment as line items
            List<GenericValue> itemAdjustments = OrderReadHelper.getOrderItemAdjustmentList(orderItem,
                    orh.getAdjustments());
            for (GenericValue adj : itemAdjustments) {

                // Check against OrderAdjustmentBilling to see how much of this adjustment has already been invoiced
                BigDecimal adjAlreadyInvoicedAmount = null;
                try {
                    Map<String, Object> checkResult = dispatcher.runSync("calculateInvoicedAdjustmentTotal",
                            UtilMisc.toMap("orderAdjustment", adj));
                    adjAlreadyInvoicedAmount = (BigDecimal) checkResult.get("invoicedTotal");
                } catch (GenericServiceException e) {
                    Debug.logError(e, "Accounting trouble calling calculateInvoicedAdjustmentTotal service",
                            module);
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                            "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
                }

                //                    if (adj.get("amount") == null) { TODO check usage with webPos. Was: fix a bug coming from POS in case of use of a discount (on item(s) or sale, item(s) here) and a cash amount higher than total (hence issuing change)
                //                        continue;
                //                    }
                // Set adjustment amount as amountAlreadyIncluded to continue invoice item creation process
                Boolean isTaxIncludedInPrice = adj.getString("orderAdjustmentTypeId").equals("VAT_TAX")
                        && UtilValidate.isNotEmpty(adj.getBigDecimal("amountAlreadyIncluded"))
                        && adj.getBigDecimal("amountAlreadyIncluded").signum() != 0;
                if ((adj.getBigDecimal("amount").signum() == 0) && isTaxIncludedInPrice) {
                    adj.set("amount", adj.getBigDecimal("amountAlreadyIncluded"));
                }
                // If the absolute invoiced amount >= the abs of the adjustment amount, the full amount has already been invoiced, so skip this adjustment
                if (adjAlreadyInvoicedAmount.abs().compareTo(
                        adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).abs()) > 0) {
                    continue;
                }

                BigDecimal originalOrderItemQuantity = OrderReadHelper.getOrderItemQuantity(originalOrderItem);
                BigDecimal amount = ZERO;
                if (originalOrderItemQuantity.signum() != 0) {
                    if (adj.get("amount") != null) {
                        if ("PROMOTION_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))
                                && adj.get("productPromoId") != null) {
                            /* Find negative amountAlreadyIncluded in OrderAdjustment to subtract it from discounted amount.
                                                                  As we stored negative sales tax amount in order adjustment for discounted item.
                             */
                            List<EntityExpr> exprs = UtilMisc.toList(
                                    EntityCondition.makeCondition("orderId", EntityOperator.EQUALS,
                                            orderItem.getString("orderId")),
                                    EntityCondition.makeCondition("orderItemSeqId", EntityOperator.EQUALS,
                                            orderItem.getString("orderItemSeqId")),
                                    EntityCondition.makeCondition("orderAdjustmentTypeId",
                                            EntityOperator.EQUALS, "VAT_TAX"),
                                    EntityCondition.makeCondition("amountAlreadyIncluded",
                                            EntityOperator.LESS_THAN, BigDecimal.ZERO));
                            EntityCondition andCondition = EntityCondition.makeCondition(exprs,
                                    EntityOperator.AND);
                            GenericValue orderAdjustment = EntityUtil.getFirst(delegator
                                    .findList("OrderAdjustment", andCondition, null, null, null, false));
                            if (UtilValidate.isNotEmpty(orderAdjustment)) {
                                amount = adj.getBigDecimal("amount")
                                        .subtract(orderAdjustment.getBigDecimal("amountAlreadyIncluded"))
                                        .setScale(100, ROUNDING);
                            } else {
                                amount = adj.getBigDecimal("amount");
                            }
                        } else {
                            // pro-rate the amount
                            // set decimals = 100 means we don't round this intermediate value, which is very important
                            if (isTaxIncludedInPrice) {
                                BigDecimal priceWithTax = originalOrderItem.getBigDecimal("unitPrice");
                                // Get tax included in item price
                                amount = priceWithTax.subtract(billingAmount);
                                amount = amount.multiply(billingQuantity);
                                // get adjustment amount
                                /* Get tax amount of other invoice and calculate remaining amount need to store in invoice item(Handle case of of partial shipment and promotional item)
                                                                      to adjust tax amount in invoice item. 
                                 */
                                BigDecimal otherInvoiceTaxAmount = BigDecimal.ZERO;
                                GenericValue orderAdjBilling = EntityUtil.getFirst(delegator.findByAnd(
                                        "OrderAdjustmentBilling",
                                        UtilMisc.toMap("orderAdjustmentId", adj.getString("orderAdjustmentId")),
                                        null, false));
                                if (UtilValidate.isNotEmpty(orderAdjBilling)) {
                                    List<GenericValue> invoiceItems = delegator.findByAnd("InvoiceItem",
                                            UtilMisc.toMap("invoiceId", orderAdjBilling.getString("invoiceId"),
                                                    "invoiceItemTypeId", "ITM_SALES_TAX", "productId",
                                                    originalOrderItem.getString("productId")),
                                            null, isTaxIncludedInPrice);
                                    for (GenericValue invoiceItem : invoiceItems) {
                                        otherInvoiceTaxAmount = otherInvoiceTaxAmount
                                                .add(invoiceItem.getBigDecimal("amount"));
                                    }
                                    if (otherInvoiceTaxAmount.compareTo(BigDecimal.ZERO) > 0) {
                                        BigDecimal remainingAmount = adj.getBigDecimal("amountAlreadyIncluded")
                                                .subtract(otherInvoiceTaxAmount);
                                        amount = amount.min(remainingAmount);
                                    }
                                }
                                amount = amount.min(adj.getBigDecimal("amountAlreadyIncluded")).setScale(100,
                                        ROUNDING);
                            } else {
                                amount = adj.getBigDecimal("amount").divide(originalOrderItemQuantity, 100,
                                        ROUNDING);
                                amount = amount.multiply(billingQuantity);
                            }
                        }
                        // Tax needs to be rounded differently from other order adjustments
                        if (adj.getString("orderAdjustmentTypeId").equals("SALES_TAX")) {
                            amount = amount.setScale(TAX_DECIMALS, TAX_ROUNDING);
                        } else {
                            amount = amount.setScale(invoiceTypeDecimals, ROUNDING);
                        }
                    } else if (adj.get("sourcePercentage") != null) {
                        // pro-rate the amount
                        // set decimals = 100 means we don't round this intermediate value, which is very important
                        BigDecimal percent = adj.getBigDecimal("sourcePercentage");
                        percent = percent.divide(new BigDecimal(100), 100, ROUNDING);
                        amount = billingAmount.multiply(percent);
                        amount = amount.divide(originalOrderItemQuantity, 100, ROUNDING);
                        amount = amount.multiply(billingQuantity);
                        amount = amount.setScale(invoiceTypeDecimals, ROUNDING);
                    }
                }
                if (amount.signum() != 0) {
                    Map<String, Object> createInvoiceItemAdjContext = new HashMap<String, Object>();
                    createInvoiceItemAdjContext.put("invoiceId", invoiceId);
                    createInvoiceItemAdjContext.put("invoiceItemSeqId", invoiceItemSeqId);
                    createInvoiceItemAdjContext.put("invoiceItemTypeId", getInvoiceItemType(delegator,
                            adj.getString("orderAdjustmentTypeId"), null, invoiceType, "INVOICE_ITM_ADJ"));
                    createInvoiceItemAdjContext.put("quantity", BigDecimal.ONE);
                    createInvoiceItemAdjContext.put("amount", amount);
                    createInvoiceItemAdjContext.put("productId", orderItem.get("productId"));
                    createInvoiceItemAdjContext.put("productFeatureId", orderItem.get("productFeatureId"));
                    createInvoiceItemAdjContext.put("overrideGlAccountId", adj.get("overrideGlAccountId"));
                    createInvoiceItemAdjContext.put("parentInvoiceId", invoiceId);
                    createInvoiceItemAdjContext.put("parentInvoiceItemSeqId", parentInvoiceItemSeqId);
                    createInvoiceItemAdjContext.put("userLogin", userLogin);
                    createInvoiceItemAdjContext.put("taxAuthPartyId", adj.get("taxAuthPartyId"));
                    createInvoiceItemAdjContext.put("taxAuthGeoId", adj.get("taxAuthGeoId"));
                    createInvoiceItemAdjContext.put("taxAuthorityRateSeqId", adj.get("taxAuthorityRateSeqId"));

                    // some adjustments fill out the comments field instead
                    String description = (UtilValidate.isEmpty(adj.getString("description"))
                            ? adj.getString("comments")
                            : adj.getString("description"));
                    createInvoiceItemAdjContext.put("description", description);

                    // invoice items for sales tax are not taxable themselves
                    // TODO: This is not an ideal solution. Instead, we need to use OrderAdjustment.includeInTax when it is implemented
                    if (!(adj.getString("orderAdjustmentTypeId").equals("SALES_TAX"))) {
                        createInvoiceItemAdjContext.put("taxableFlag", product.get("taxable"));
                    }

                    // If the OrderAdjustment is associated to a ProductPromo,
                    // and the field ProductPromo.overrideOrgPartyId is set,
                    // copy the value to InvoiceItem.overrideOrgPartyId: this
                    // represent an organization override for the payToPartyId
                    if (UtilValidate.isNotEmpty(adj.getString("productPromoId"))) {
                        try {
                            GenericValue productPromo = adj.getRelatedOne("ProductPromo", false);
                            if (UtilValidate.isNotEmpty(productPromo.getString("overrideOrgPartyId"))) {
                                createInvoiceItemAdjContext.put("overrideOrgPartyId",
                                        productPromo.getString("overrideOrgPartyId"));
                            }
                        } catch (GenericEntityException e) {
                            Debug.logError(e, "Error looking up ProductPromo with id ["
                                    + adj.getString("productPromoId") + "]", module);
                        }
                    }

                    Map<String, Object> createInvoiceItemAdjResult = dispatcher.runSync("createInvoiceItem",
                            createInvoiceItemAdjContext);
                    if (ServiceUtil.isError(createInvoiceItemAdjResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingInvoiceItemFromOrder", locale),
                                null, null, createInvoiceItemAdjResult);
                    }

                    // Create the OrderAdjustmentBilling record
                    Map<String, Object> createOrderAdjustmentBillingContext = new HashMap<String, Object>();
                    createOrderAdjustmentBillingContext.put("orderAdjustmentId",
                            adj.getString("orderAdjustmentId"));
                    createOrderAdjustmentBillingContext.put("invoiceId", invoiceId);
                    createOrderAdjustmentBillingContext.put("invoiceItemSeqId", invoiceItemSeqId);
                    createOrderAdjustmentBillingContext.put("amount", amount);
                    createOrderAdjustmentBillingContext.put("userLogin", userLogin);

                    Map<String, Object> createOrderAdjustmentBillingResult = dispatcher
                            .runSync("createOrderAdjustmentBilling", createOrderAdjustmentBillingContext);
                    if (ServiceUtil.isError(createOrderAdjustmentBillingResult)) {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource,
                                        "AccountingErrorCreatingOrderAdjustmentBillingFromOrder", locale),
                                null, null, createOrderAdjustmentBillingContext);
                    }

                    // this adjustment amount
                    BigDecimal thisAdjAmount = amount;

                    // adjustments only apply to totals when they are not tax or shipping adjustments
                    if (!"SALES_TAX".equals(adj.getString("orderAdjustmentTypeId"))
                            && !"SHIPPING_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))) {
                        // increment the invoice subtotal
                        invoiceSubTotal = invoiceSubTotal.add(thisAdjAmount).setScale(100, ROUNDING);

                        // add to the ship amount only if it applies to this item
                        if (shippingApplies) {
                            invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAdjAmount)
                                    .setScale(invoiceTypeDecimals, ROUNDING);
                        }
                    }

                    // increment the counter
                    invoiceItemSeqNum++;
                    invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                            INVOICE_ITEM_SEQUENCE_ID_DIGITS);
                }
            }
        }

        // create header adjustments as line items -- always to tax/shipping last
        Map<GenericValue, BigDecimal> shipAdjustments = new HashMap<GenericValue, BigDecimal>();
        Map<GenericValue, BigDecimal> taxAdjustments = new HashMap<GenericValue, BigDecimal>();

        List<GenericValue> headerAdjustments = orh.getOrderHeaderAdjustments();
        for (GenericValue adj : headerAdjustments) {

            // Check against OrderAdjustmentBilling to see how much of this adjustment has already been invoiced
            BigDecimal adjAlreadyInvoicedAmount = null;
            try {
                Map<String, Object> checkResult = dispatcher.runSync("calculateInvoicedAdjustmentTotal",
                        UtilMisc.toMap("orderAdjustment", adj));
                adjAlreadyInvoicedAmount = ((BigDecimal) checkResult.get("invoicedTotal"))
                        .setScale(invoiceTypeDecimals, ROUNDING);
            } catch (GenericServiceException e) {
                Debug.logError(e, "Accounting trouble calling calculateInvoicedAdjustmentTotal service",
                        module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                        "AccountingTroubleCallingCalculateInvoicedAdjustmentTotalService", locale));
            }

            //                if (null == adj.get("amount")) { TODO check usage with webPos. Was: fix a bug coming from POS in case of use of a discount (on item(s) or sale, sale here) and a cash amount higher than total (hence issuing change)
            //                    continue;
            //                }
            // If the absolute invoiced amount >= the abs of the adjustment amount, the full amount has already been invoiced, so skip this adjustment
            if (adjAlreadyInvoicedAmount.abs().compareTo(
                    adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING).abs()) >= 0) {
                continue;
            }

            if ("SHIPPING_CHARGES".equals(adj.getString("orderAdjustmentTypeId"))) {
                shipAdjustments.put(adj, adjAlreadyInvoicedAmount);
            } else if ("SALES_TAX".equals(adj.getString("orderAdjustmentTypeId"))) {
                taxAdjustments.put(adj, adjAlreadyInvoicedAmount);
            } else {
                // these will effect the shipping pro-rate (unless commented)
                // other adjustment type
                BigDecimal divisor = orderSubTotal;
                BigDecimal multiplier = invoiceSubTotal;
                if (BigDecimal.ZERO.compareTo(multiplier) == 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
                    // if multiplier and divisor are equal to zero then use the quantities instead of the amounts
                    // this is useful when the order has free items and misc charges
                    divisor = orderQuantity;
                    multiplier = invoiceQuantity;
                }

                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
                        adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING),
                        invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
                // invoiceShipProRateAmount += adjAmount;
                // do adjustments compound or are they based off subtotal? Here we will (unless commented)
                // invoiceSubTotal += adjAmount;

                // increment the counter
                invoiceItemSeqNum++;
                invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                        INVOICE_ITEM_SEQUENCE_ID_DIGITS);
            }
        }

        // next do the shipping adjustments.  Note that we do not want to add these to the invoiceSubTotal or orderSubTotal for pro-rating tax later, as that would cause
        // numerator/denominator problems when the shipping is not pro-rated but rather charged all on the first invoice
        for (GenericValue adj : shipAdjustments.keySet()) {
            BigDecimal adjAlreadyInvoicedAmount = shipAdjustments.get(adj);

            if ("N".equalsIgnoreCase(prorateShipping)) {

                // Set the divisor and multiplier to 1 to avoid prorating
                BigDecimal divisor = BigDecimal.ONE;
                BigDecimal multiplier = BigDecimal.ONE;

                // The base amount in this case is the adjustment amount minus the total already invoiced for that adjustment, since
                //  it won't be prorated
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING)
                        .subtract(adjAlreadyInvoicedAmount);
                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
                        baseAmount, invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
            } else {

                // Pro-rate the shipping amount based on shippable information
                BigDecimal divisor = shippableAmount;
                BigDecimal multiplier = invoiceShipProRateAmount;
                if (BigDecimal.ZERO.compareTo(multiplier) == 0 && BigDecimal.ZERO.compareTo(divisor) == 0) {
                    // if multiplier and divisor are equal to zero then use the quantities instead of the amounts
                    // this is useful when the order has free items and shipping charges
                    divisor = shippableQuantity;
                    multiplier = invoiceShippableQuantity;
                }

                // The base amount in this case is the adjustment amount, since we want to prorate based on the full amount
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(invoiceTypeDecimals, ROUNDING);
                calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor, multiplier,
                        baseAmount, invoiceTypeDecimals, ROUNDING, userLogin, dispatcher, locale);
            }

            // Increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);
        }

        // last do the tax adjustments
        String prorateTaxes = productStore != null ? productStore.getString("prorateTaxes") : "Y";
        if (prorateTaxes == null) {
            prorateTaxes = "Y";
        }
        for (Map.Entry<GenericValue, BigDecimal> entry : taxAdjustments.entrySet()) {
            GenericValue adj = entry.getKey();
            BigDecimal adjAlreadyInvoicedAmount = entry.getValue();
            BigDecimal adjAmount = null;

            if ("N".equalsIgnoreCase(prorateTaxes)) {

                // Set the divisor and multiplier to 1 to avoid prorating
                BigDecimal divisor = BigDecimal.ONE;
                BigDecimal multiplier = BigDecimal.ONE;

                // The base amount in this case is the adjustment amount minus the total already invoiced for that adjustment, since
                //  it won't be prorated
                BigDecimal baseAmount = adj.getBigDecimal("amount").setScale(TAX_DECIMALS, TAX_ROUNDING)
                        .subtract(adjAlreadyInvoicedAmount);
                adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor,
                        multiplier, baseAmount, TAX_DECIMALS, TAX_ROUNDING, userLogin, dispatcher, locale);
            } else {

                // Pro-rate the tax amount based on shippable information
                BigDecimal divisor = orderSubTotal;
                BigDecimal multiplier = invoiceSubTotal;

                // The base amount in this case is the adjustment amount, since we want to prorate based on the full amount
                BigDecimal baseAmount = adj.getBigDecimal("amount");
                adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, divisor,
                        multiplier, baseAmount, TAX_DECIMALS, TAX_ROUNDING, userLogin, dispatcher, locale);
            }
            invoiceSubTotal = invoiceSubTotal.add(adjAmount).setScale(invoiceTypeDecimals, ROUNDING);

            // Increment the counter
            invoiceItemSeqNum++;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);
        }

        // check for previous order payments
        List<GenericValue> orderPaymentPrefs = EntityQuery.use(delegator).from("OrderPaymentPreference").where(
                EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderId),
                EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PAYMENT_CANCELLED"))
                .queryList();
        List<GenericValue> currentPayments = new LinkedList<GenericValue>();
        for (GenericValue paymentPref : orderPaymentPrefs) {
            List<GenericValue> payments = paymentPref.getRelated("Payment", null, null, false);
            currentPayments.addAll(payments);
        }
        // apply these payments to the invoice if they have any remaining amount to apply
        for (GenericValue payment : currentPayments) {
            if ("PMNT_VOID".equals(payment.getString("statusId"))
                    || "PMNT_CANCELLED".equals(payment.getString("statusId"))) {
                continue;
            }
            BigDecimal notApplied = PaymentWorker.getPaymentNotApplied(payment);
            if (notApplied.signum() > 0) {
                Map<String, Object> appl = new HashMap<String, Object>();
                appl.put("paymentId", payment.get("paymentId"));
                appl.put("invoiceId", invoiceId);
                appl.put("billingAccountId", billingAccountId);
                appl.put("amountApplied", notApplied);
                appl.put("userLogin", userLogin);
                Map<String, Object> createPayApplResult = dispatcher.runSync("createPaymentApplication", appl);
                if (ServiceUtil.isError(createPayApplResult)) {
                    return ServiceUtil
                            .returnError(
                                    UtilProperties.getMessage(resource,
                                            "AccountingErrorCreatingInvoiceFromOrder", locale),
                                    null, null, createPayApplResult);
                }
            }
        }

        // Should all be in place now. Depending on the ProductStore.autoApproveInvoice setting, set status to INVOICE_READY (unless it's a purchase invoice, which we set to INVOICE_IN_PROCESS)
        String autoApproveInvoice = productStore != null ? productStore.getString("autoApproveInvoice") : "Y";
        if (!"N".equals(autoApproveInvoice)) {
            String nextStatusId = "PURCHASE_INVOICE".equals(invoiceType) ? "INVOICE_IN_PROCESS"
                    : "INVOICE_READY";
            Map<String, Object> setInvoiceStatusResult = dispatcher.runSync("setInvoiceStatus",
                    UtilMisc.<String, Object>toMap("invoiceId", invoiceId, "statusId", nextStatusId,
                            "userLogin", userLogin));
            if (ServiceUtil.isError(setInvoiceStatusResult)) {
                return ServiceUtil.returnError(
                        UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceFromOrder", locale),
                        null, null, setInvoiceStatusResult);
            }
        }

        Map<String, Object> resp = ServiceUtil.returnSuccess();
        resp.put("invoiceId", invoiceId);
        resp.put("invoiceTypeId", invoiceType);
        return resp;
    } catch (GenericEntityException e) {
        Debug.logError(e, "Entity/data problem creating invoice from order items: " + e.toString(), module);
        return ServiceUtil.returnError(
                UtilProperties.getMessage(resource, "AccountingEntityDataProblemCreatingInvoiceFromOrderItems",
                        UtilMisc.toMap("reason", e.toString()), locale));
    } catch (GenericServiceException e) {
        Debug.logError(e, "Service/other problem creating invoice from order items: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource,
                "AccountingServiceOtherProblemCreatingInvoiceFromOrderItems",
                UtilMisc.toMap("reason", e.toString()), locale));
    }
}

From source file:org.egov.egf.commons.EgovCommon.java

/**
 * This method will return the total amount for the payment that are approved and cheques not assigned.
 *
 * @param VoucherDate/*ww  w.  ja  v  a 2 s. c  o m*/
 * @param bankaccountId
 * @return
 */
@SuppressWarnings("unchecked")
public BigDecimal getAmountForApprovedPaymentAndChequeNotAssigned(final Date voucherDate,
        final Integer bankaccountId) {
    LOGGER.debug("EgovCommon | getAmountForApprovedPaymentAndChequeNotAssigned");
    BigDecimal bankBalance = BigDecimal.ZERO;
    try {
        final Bankaccount bankAccount = (Bankaccount) getPersistenceService()
                .find("from Bankaccount where id=?", bankaccountId);
        StringBuffer paymentQuery = new StringBuffer();
        // query to fetch vouchers for which no cheque has been assigned
        paymentQuery = paymentQuery.append(
                "SELECT (case when sum(gl.debitAmount) is null then 0 else sum(gl.debitAmount) end   -  case when sum(gl.creditAmount) is "
                        + " null then 0 else sum(gl.creditAmount) ) as amount FROM  GeneralLedger gl ,voucherheader vh, "
                        + " Paymentheader ph ,eg_wf_states es ,egf_instrumentvoucher iv right outer join voucherheader vh1 on "
                        + "vh1.id =iv.VOUCHERHEADERID WHERE gl.voucherHeaderId=vh.id and "
                        + "ph.voucherheaderid=vh.id and gl.glcodeId=" + bankAccount.getChartofaccounts().getId()
                        + " and "
                        + "vh.voucherDate >= (select startingDate from FinancialYear where  startingDate <= :date AND endingDate >=:date) and "
                        + " vh.voucherDate <= :date and ph.state_id=es.id and es.value='END' and vh.status=0 and vh1.id=vh.id and iv.VOUCHERHEADERID is null ")
                .append(" union ")
                // query to fetch vouchers for which cheque has been
                // assigned and surrendered
                .append("SELECT (case when sum(gl.debitAmount) is null then 0 else sum(gl.debitAmount) end - case when sum(gl.creditAmount) is "
                        + "null then 0 else sum(gl.creditAmount) ) as amount FROM  GeneralLedger gl ,voucherheader vh, "
                        + " Paymentheader ph ,eg_wf_states es ,egf_instrumentvoucher iv,egw_status egws,(select ih1.id,ih1.id_status from egf_instrumentheader "
                        + "ih1, (select bankid,bankaccountid,instrumentnumber,max(lastmodifieddate) as lastmodifieddate from egf_instrumentheader group by bankid,"
                        + "bankaccountid,instrumentnumber) max_rec where max_rec.bankid=ih1.bankid and max_rec.bankaccountid=ih1.bankaccountid and max_rec.instrumentnumber=ih1.instrumentnumber "
                        + "and max_rec.lastmodifieddate=ih1.lastmodifieddate) ih WHERE gl.voucherHeaderId=vh.id and "
                        + "ph.voucherheaderid=vh.id and gl.glcodeId=" + bankAccount.getChartofaccounts().getId()
                        + " and "
                        + "vh.voucherDate >= (select startingDate from FinancialYear where  startingDate <= :date AND endingDate >=:date) and"
                        + " vh.voucherDate <= :date and ph.state_id=es.id and es.value='END' and vh.status=0 and  iv.voucherheaderid=vh.id and iv.instrumentheaderid=ih.id and "
                        + "ih.id_status=egws.id and egws.description in ('Surrendered','Surrender_For_Reassign')");
        final List<Object> list = persistenceService.getSession().createSQLQuery(paymentQuery.toString())
                .setDate("date", voucherDate).list();
        final BigDecimal amount = (BigDecimal) list.get(0);
        bankBalance = amount == null ? BigDecimal.ZERO : amount;
        LOGGER.debug(
                "Total payment amount that are approved by FM Unit but cheque not yet assigned:" + bankBalance);
    } catch (final Exception e) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("exception occuered while getting cash balance" + e.getMessage(), e);
        throw new HibernateException(e);
    }
    return bankBalance.abs();
}

From source file:com.lp.webapp.zemecs.CommandZE.java

public synchronized String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

    super.execute(request, response);

    if (request.getCookies() != null) {
        for (int i = 0; i < request.getCookies().length; i++) {
            Cookie cookie = request.getCookies()[i];
            cookie.setMaxAge(10000000);// w  ww . j  a v  a2s . c  o  m
            response.addCookie(cookie);
        }
    }

    String mandant = request.getParameter("mandant");

    Locale localeLogon = getMandantFac().getLocaleDesHauptmandanten();

    String locale = request.getParameter("locale");

    String localeCookie = getCookieValue("locale", request);

    if (localeCookie != null && localeCookie.length() > 3) {
        locale = localeCookie;
    }

    if (locale != null && locale.length() > 3) {
        localeLogon = new Locale(locale.substring(0, 2), locale.substring(2, 4));
    }

    TheClientDto theclientDto = null;
    synchronized (mutex) {
        theclientDto = getLogonFac().logon(Helper.getFullUsername(sUser),
                Helper.getMD5Hash((sUser + new String("lpwebappzemecs")).toCharArray()), localeLogon, null,
                null, new Timestamp(System.currentTimeMillis()));

        if (mandant != null && mandant.length() > 0) {

            theclientDto = getLogonFac().logon(Helper.getFullUsername(sUser),
                    Helper.getMD5Hash((sUser + "lpwebappzemecs").toCharArray()), localeLogon, mandant,
                    theclientDto, new Timestamp(System.currentTimeMillis()));
        } else {
            BenutzerDto benutzerDto = getBenutzerFac().benutzerFindByCBenutzerkennung("lpwebappzemecs",
                    new String(Helper.getMD5Hash("lpwebappzemecs" + "lpwebappzemecs")));
            mandant = benutzerDto.getMandantCNrDefault();
        }
    }

    getTheClient(request, response).setTheClientDto(theclientDto);

    if (command.equals(TheApp.CMD_ZE_BDESTATION)) {
        String ausweis = request.getParameter("ausweis");
        getTheClient(request, response).setSMsg("");

        if (ausweis != null && ausweis.length() > 1) {
            // Personal suchen
            PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis.substring(2));
            if (personalDto != null) {
                personalDto.setPartnerDto(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

                HashMap<String, Serializable> hmParameter = new HashMap<String, Serializable>();
                ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
                zeitdatenDto.setCWowurdegebucht("BDE-Station " + request.getRemoteHost());
                zeitdatenDto.setPersonalIId(personalDto.getIId());
                hmParameter.put("zeitdaten", zeitdatenDto);
                hmParameter.put("person", personalDto.getPartnerDto().formatFixTitelName1Name2());
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
                return getSJSPNext();
            } else {
                getTheClient(request, response)
                        .setSMsg("Ausweis " + ausweis + " bei diesem Mandanten nicht gefunden! ");
            }
        } else {
            getTheClient(request, response).setSMsg("");
        }
    }
    if (command.equals(TheApp.CMD_ZE_BDESTATION2)) {

        HashMap<String, Serializable> hmParameter = (HashMap<String, Serializable>) getTheClient(request,
                response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String option = request.getParameter("option");
        getTheClient(request, response).setSMsg("");

        ParametermandantDto parameterBdeMitTaetigkeitDto = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_BDE_MIT_TAETIGKEIT);

        Boolean bBdeMitTaetigkeit = (Boolean) parameterBdeMitTaetigkeitDto.getCWertAsObject();
        com.lp.server.artikel.service.ArtikelDto artikelDtoDefaultArbeiztszeit = null;

        if (bBdeMitTaetigkeit == false) {
            ParametermandantDto parameterDtoDefaultarbeitszeit = getParameterFac().getMandantparameter(mandant,
                    ParameterFac.KATEGORIE_ALLGEMEIN, ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

            if (parameterDtoDefaultarbeitszeit != null && parameterDtoDefaultarbeitszeit.getCWert() != null
                    && !parameterDtoDefaultarbeitszeit.getCWert().trim().equals("")) {
                try {
                    artikelDtoDefaultArbeiztszeit = getArtikelFac()
                            .artikelFindByCNr(parameterDtoDefaultarbeitszeit.getCWert(), theclientDto);
                    zeitdatenDto.setArtikelIId(artikelDtoDefaultArbeiztszeit.getIId());

                } catch (RemoteException ex2) {
                    myLogger.error("Default-Arbeitszeitartikel " + parameterDtoDefaultarbeitszeit.getCWert()
                            + " nicht vorhanden.", ex2);
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } else {
                myLogger.error("Default-Arbeitszeitartikel " + parameterDtoDefaultarbeitszeit.getCWert()
                        + " nicht definiert.");
                setSJSPNext("bdestation.jsp");
                return getSJSPNext();
            }
        }

        if (option != null && option.length() > 2) {

            // Auftrag
            if (option.substring(0, 2).equals("$A") || option.substring(0, 3).equals("$EA")) {
                try {

                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_BELEGNUMMERNFORMAT_STELLEN_GESCHAEFTSJAHR);

                    if (parameterDto != null) {
                        if (parameterDto.getCWert() != null && parameterDto.getCWert().equals("4")) {
                            if (option.charAt(4) == 47) {
                                option = "$A"
                                        + Helper.konvertiereDatum2StelligAuf4Stellig(option.substring(2, 4))
                                        + option.substring(4);
                            }
                        }
                    }
                    AuftragDto auftragDto = null;
                    if (option.substring(0, 2).equals("$A")) {

                        auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, option.substring(2),
                                theclientDto);
                    } else {
                        auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, option.substring(3),
                                theclientDto);

                    }

                    AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                            .auftragpositionFindByAuftrag(auftragDto.getIId());

                    if (auftragDto.getAuftragstatusCNr()
                            .equals(com.lp.server.auftrag.service.AuftragServiceFac.AUFTRAGSTATUS_ERLEDIGT)) {
                        setSJSPNext("bdestation.jsp");
                        getTheClient(request, response).setSMsg("Auf Auftrag " + option.substring(2)
                                + " mit Status " + auftragDto.getAuftragstatusCNr().trim()
                                + " darf nicht gebucht werden! ");
                        return getSJSPNext();
                    } else {
                        if (auftragpositionDtos != null && auftragpositionDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                            zeitdatenDto.setIBelegartid(auftragpositionDtos[0].getBelegIId());
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_AUFTRAG);
                            hmParameter.put("beleg", "A" + option.substring(2));

                            MaschineDto maschineDto = new MaschineDto();
                            maschineDto.setCBez("");
                            hmParameter.put("maschine", maschineDto);

                            if (option.substring(0, 2).equals("$A")) {

                                if (bBdeMitTaetigkeit == false) {
                                    setSJSPNext("bdestation.jsp");
                                    getTheClient(request, response).setSMsg(
                                            getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                                    artikelDtoDefaultArbeiztszeit.getCNr(), theclientDto));

                                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false,
                                            theclientDto);
                                    return getSJSPNext();

                                } else {
                                    setSJSPNext("bdestation4.jsp");
                                }
                            } else {
                                hmParameter.put("beleg", "A" + option.substring(3));
                                setSJSPNext("bdestation3gutschlecht.jsp");

                            }

                            return getSJSPNext();
                        } else {
                            getTheClient(request, response)
                                    .setSMsg("Auftrag " + option.substring(2) + " hat keine Positionen! ");
                            setSJSPNext("bdestation.jsp");
                            return getSJSPNext();
                        }
                    }

                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response).setSMsg(
                            "Auftrag '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }
            } // Los
            else if (option.substring(0, 2).equals("$L") || option.substring(0, 3).equals("$EL")) {
                try {

                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_BELEGNUMMERNFORMAT_STELLEN_GESCHAEFTSJAHR);

                    if (parameterDto != null) {
                        if (parameterDto.getCWert() != null && parameterDto.getCWert().equals("4")) {
                            if (option.charAt(4) == 47) {
                                option = "$L"
                                        + Helper.konvertiereDatum2StelligAuf4Stellig(option.substring(2, 4))
                                        + option.substring(4);
                            }
                        }
                    }

                    com.lp.server.fertigung.service.LosDto losDto = null;

                    if (option.substring(0, 2).equals("$L")) {
                        losDto = getFertigungFac().losFindByCNrMandantCNr(option.substring(2), mandant);

                    } else {
                        losDto = getFertigungFac().losFindByCNrMandantCNr(option.substring(3), mandant);

                    }

                    // WH 18-01-2006: Los benoetigt keine Positionen

                    if (losDto.getStatusCNr()
                            .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ANGELEGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_AUSGEGEBEN)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_GESTOPPT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ERLEDIGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_STORNIERT)) {
                        getTheClient(request, response)
                                .setSMsg("Auf Los " + option.substring(2) + " mit Status "
                                        + losDto.getStatusCNr().trim() + " darf nicht gebucht werden! ");
                        setSJSPNext("bdestation.jsp");
                        return getSJSPNext();
                    } else {
                        zeitdatenDto.setIBelegartid(losDto.getIId());
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);

                        if (option.substring(0, 2).equals("$L")) {

                            hmParameter.put("beleg", "L" + option.substring(2));

                            if (bBdeMitTaetigkeit == false) {
                                setSJSPNext("bdestation.jsp");
                                getTheClient(request, response).setSMsg(
                                        getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                                artikelDtoDefaultArbeiztszeit.getCNr(), theclientDto));

                                getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true,
                                        theclientDto);
                                return getSJSPNext();
                            } else {
                                setSJSPNext("bdestation3.jsp");
                            }
                        } else {
                            hmParameter.put("beleg", "L" + option.substring(3));
                            setSJSPNext("bdestation3gutschlecht.jsp");
                        }

                        return getSJSPNext();

                    }
                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response)
                            .setSMsg("Los '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } // Kombi-Code
            else if (option.length() > 1 && option.substring(0, 2).equals("$V")) {
                setSJSPNext("bdestation.jsp");

                if (option.length() < 12) {

                    getTheClient(request, response).setSMsg("Kombicode muss 10-Stellig sein ");
                    return getSJSPNext();
                }

                try {

                    ParametermandantDto parameter = getParameterFac().getMandantparameter(
                            theclientDto.getMandant(), ParameterFac.KATEGORIE_FERTIGUNG,
                            ParameterFac.PARAMETER_LOSNUMMER_AUFTRAGSBEZOGEN);
                    int iVerlaengerungLosnummer = 0;
                    if ((Integer) parameter.getCWertAsObject() >= 1) {
                        iVerlaengerungLosnummer = 2;
                    }

                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(option.substring(2, 12 + iVerlaengerungLosnummer), mandant);

                    // WH 18-01-2006: Los benoetigt keine Positionen

                    if (losDto.getStatusCNr()
                            .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ANGELEGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_AUSGEGEBEN)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_GESTOPPT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_ERLEDIGT)
                            || losDto.getStatusCNr()
                                    .equals(com.lp.server.fertigung.service.FertigungFac.STATUS_STORNIERT)) {
                        getTheClient(request, response)
                                .setSMsg("Auf Los " + option.substring(2) + " mit Status "
                                        + losDto.getStatusCNr().trim() + " darf nicht gebucht werden! ");
                    } else {
                        zeitdatenDto.setIBelegartid(losDto.getIId());
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);

                        String maschine = option.substring(12 + iVerlaengerungLosnummer,
                                14 + iVerlaengerungLosnummer);

                        String taetigkeit = option.substring(14 + iVerlaengerungLosnummer);

                        MaschineDto maschineDto = new MaschineDto();
                        maschineDto.setCBez(maschine);
                        hmParameter.put("maschine", maschineDto);

                        hmParameter.put("beleg", "L" + option.substring(2, 12 + iVerlaengerungLosnummer));

                        com.lp.server.artikel.service.ArtikelDto artikelDto = null;
                        try {
                            artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto);

                            zeitdatenDto.setArtikelIId(
                                    getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto).getIId());

                        } catch (RemoteException ex2) {
                            getTheClient(request, response)
                                    .setSMsg("T\u00E4tigkeit '" + taetigkeit + "' nicht gefunden! ");
                            return getSJSPNext();
                        }

                        com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelDto.getIId());

                        if (dtos != null && dtos.length > 0) {

                            if (!maschine.trim().equals("") && !maschine.equals("--")) {

                                try {
                                    Integer maschineIId = getZeiterfassungsFac()
                                            .maschineFindByCIdentifikationsnr(maschine).getIId();

                                    com.lp.server.fertigung.service.LossollarbeitsplanDto[] sollaDtos = getFertigungFac()
                                            .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(
                                                    zeitdatenDto.getIBelegartid(),
                                                    zeitdatenDto.getArtikelIId());

                                    if (sollaDtos != null && sollaDtos.length > 0) {
                                        MaschinenzeitdatenDto maschinenzeitdatenDto = new MaschinenzeitdatenDto();
                                        maschinenzeitdatenDto.setLossollarbeitsplanIId(sollaDtos[0].getIId());
                                        maschinenzeitdatenDto.setMaschineIId(maschineIId);
                                        maschinenzeitdatenDto
                                                .setPersonalIIdGestartet(zeitdatenDto.getPersonalIId());
                                        maschinenzeitdatenDto.setTVon(zeitdatenDto.getTZeit());
                                        getZeiterfassungsFac().createMaschinenzeitdaten(maschinenzeitdatenDto,
                                                theclientDto);
                                    }

                                } catch (RemoteException ex2) {
                                    getTheClient(request, response)
                                            .setSMsg("Maschine '" + maschine + "' nicht gefunden! ");
                                    return getSJSPNext();
                                }
                            }

                            // PJ 15388
                            if (maschine.equals("--")) {
                                hmParameter.put("fertig", "");
                                if (dtos != null && dtos.length > 0) {

                                    LossollarbeitsplanDto dto = dtos[0];
                                    dto.setBFertig(Helper.boolean2Short(true));

                                    try {
                                        getFertigungFac().updateLossollarbeitsplan(dto, theclientDto);
                                        getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                                getTheClient(request, response).getData(),
                                                taetigkeit.substring(2), theclientDto));
                                        return getSJSPNext();
                                    } catch (EJBExceptionLP ex2) {
                                        getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                                        return getSJSPNext();
                                    }
                                } else {
                                    getTheClient(request, response).setSMsg("Das Los "
                                            + option.substring(2, 12 + iVerlaengerungLosnummer)
                                            + " hat keinen entsprechen Arbeitsgang mit der Artikelnummer "
                                            + taetigkeit.substring(2));
                                    return getSJSPNext();
                                }
                            }

                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        } else {
                            com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                                    .lossollarbeitsplanFindByLosIId(losDto.getIId());
                            if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                                zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                            } else {
                                // Bemerkung
                                getTheClient(request, response)
                                        .setSMsg("Los " + option.substring(2) + " hat keine Positionen");
                                return getSJSPNext();

                            }
                        }

                        try {
                            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true,
                                    theclientDto);
                            getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                    getTheClient(request, response).getData(), taetigkeit, theclientDto));

                        } catch (EJBExceptionLP ex2) {
                            getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                            return getSJSPNext();
                        }
                    }
                } catch (EJBExceptionLP ex) {
                    getTheClient(request, response)
                            .setSMsg("Los '" + option.substring(2) + "' bei diesem Mandanten nicht gefunden! ");
                    return getSJSPNext();

                }
                return getSJSPNext();
            } // Sondertaetigkeit
            else {

                if (option.substring(1).equals("SALDO")) {

                    java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);
                    ts = com.lp.util.Helper.cutTimestamp(ts);

                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(ts.getTime());

                    String saldoMitUrlaub = "";
                    try {
                        saldoMitUrlaub = getZeiterfassungsFac().erstelleMonatsAbrechnungFuerBDE(
                                zeitdatenDto.getPersonalIId(), new Integer(c.get(Calendar.YEAR)),
                                new Integer(c.get(Calendar.MONTH)), false, new java.sql.Date(ts.getTime()),
                                theclientDto, true, false);

                    } catch (EJBExceptionLP ex7) {

                        if (ex7.getCause() instanceof EJBExceptionLP) {
                            EJBExceptionLP e = (EJBExceptionLP) ex7.getCause();
                            if (e != null && e
                                    .getCode() == EJBExceptionLP.FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM) {
                                getTheClient(request, response)
                                        .setSMsg(new String("FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM"));
                                return getSJSPNext();

                            }
                        }
                        getTheClient(request, response).setSMsg(new String(ex7.getMessage()));
                        setSJSPNext("bdestation.jsp");

                    }

                    getTheClient(request, response).setSMsg(saldoMitUrlaub);

                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();

                } else if (option.substring(1).equals("TAGESSALDO")) {

                    java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);

                    Double d = getZeiterfassungsFac().berechneTagesArbeitszeit(zeitdatenDto.getPersonalIId(),
                            new java.sql.Date(System.currentTimeMillis()), theclientDto);

                    StringBuffer sb = new StringBuffer();
                    sb.append("Tagesarbeitszeit bis jetzt: "
                            + Helper.rundeKaufmaennisch(new BigDecimal(d.doubleValue()), 2).doubleValue()
                            + "h");
                    sb.append("\r\n");

                    getTheClient(request, response).setSMsg(new String(sb));
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();

                } else {

                    try {
                        TaetigkeitDto taetigkeitDto = getZeiterfassungsFac()
                                .taetigkeitFindByCNr(option.substring(1), theclientDto);

                        zeitdatenDto.setTaetigkeitIId(taetigkeitDto.getIId());
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true, theclientDto);
                        getTheClient(request, response).setSMsg(
                                getMeldungGebuchtFuerBDE(hmParameter, option.substring(1), theclientDto));
                    } catch (EJBExceptionLP ex1) {
                        getTheClient(request, response)
                                .setSMsg("Sondert\u00E4tigkeit '" + option.substring(1) + "' nicht gefunden! ");
                    }
                    hmParameter.put("zeitdaten", zeitdatenDto);
                    setSJSPNext("bdestation.jsp");

                }
                getTheClient(request, response).setData(hmParameter);
                return getSJSPNext();
            }
        }

    } else if (command.equals(TheApp.CMD_ZE_BDESTATION3)) {

        HashMap<String, Serializable> hmParameter = (HashMap<String, Serializable>) getTheClient(request,
                response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String option = request.getParameter("option");
        getTheClient(request, response).setSMsg("");

        if (option != null && option.length() > 0) {

            if (option.equals("$PLUS")) {
                // CK: PJ5589
                String beleg = (String) hmParameter.get("beleg");

                if (beleg.substring(0, 1).equals("L")) {
                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                    ZeitverteilungDto zeitverteilungDto = new ZeitverteilungDto();
                    zeitverteilungDto.setLosIId(losDto.getIId());
                    zeitverteilungDto.setTZeit(new Timestamp(System.currentTimeMillis()));
                    zeitverteilungDto.setPersonalIId(zeitdatenDto.getPersonalIId());
                    try {
                        getZeiterfassungsFac().createZeitverteilung(zeitverteilungDto, theclientDto);
                    } catch (EJBExceptionLP e) {
                        hmParameter.remove("beleg");
                        getTheClient(request, response).setData(hmParameter);
                        getTheClient(request, response)
                                .setSMsg("Los '" + losDto.getCNr() + "' wurde bereits mit $PLUS gebucht");
                        setSJSPNext("bdestation2.jsp");
                        return getSJSPNext();
                    }

                } else {
                    getTheClient(request, response).setSMsg("$PLUS ist nur f\u00FCr Lose m\u00F6glich.");
                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

                hmParameter.remove("beleg");
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
                return getSJSPNext();

            } else if (option.equals("$STORNO")) {
                getZeiterfassungsFac().removeZeitverteilungByPersonalIIdUndTag(zeitdatenDto.getPersonalIId(),
                        new Timestamp(System.currentTimeMillis()));
                hmParameter.remove("beleg");
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation2.jsp");
            } else if (option.equals("$SPERREN")) {

                String beleg = (String) hmParameter.get("beleg");
                if (beleg.substring(0, 1).equals("L")) {
                    com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                            .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                    if (losDto.getStuecklisteIId() != null) {
                        Integer artikelIId = getStuecklisteFac()
                                .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto)
                                .getArtikelIId();

                        SperrenDto sDto = getArtikelFac().sperrenFindBDurchfertigung(theclientDto);

                        if (sDto != null) {

                            ArtikelsperrenDto aspDtoVorhanden = getArtikelFac()
                                    .artikelsperrenFindByArtikelIIdSperrenIIdOhneExc(artikelIId, sDto.getIId());
                            if (aspDtoVorhanden == null) {
                                ArtikelsperrenDto spDto = new ArtikelsperrenDto();
                                spDto.setArtikelIId(artikelIId);
                                spDto.setSperrenIId(sDto.getIId());

                                PersonalDto pDto = getPersonalFac()
                                        .personalFindByPrimaryKey(zeitdatenDto.getPersonalIId(), theclientDto);

                                String grund = beleg + " " + pDto.getPartnerDto().getCName1nachnamefirmazeile1()
                                        + " " + pDto.getPartnerDto().getCName2vornamefirmazeile2();

                                if (grund.length() > 80) {
                                    grund = grund.substring(0, 79);
                                }

                                spDto.setCGrund(grund);
                                getArtikelFac().createArtikelsperren(spDto, theclientDto);
                                getTheClient(request, response)
                                        .setSMsg("Los " + losDto.getCNr() + " durch Fertigung gesperrt.");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            } else {
                                getTheClient(request, response)
                                        .setSMsg("St\u00FCckliste bereits durch Fertigung gesperrt.");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            }
                        } else {
                            getTheClient(request, response)
                                    .setSMsg("Fertigungssperre in Grunddaten nicht definiert.");
                            setSJSPNext("bdestation.jsp");
                            return getSJSPNext();
                        }

                    }

                    setSJSPNext("bdestation.jsp");
                    return getSJSPNext();
                }

            } else if (option.equals("$FERTIG")) {
                hmParameter.put("fertig", "");
                MaschineDto maschineDto = new MaschineDto();
                maschineDto.setCBez("Fertig");
                hmParameter.put("maschine", maschineDto);
                getTheClient(request, response).setData(hmParameter);
                setSJSPNext("bdestation4.jsp");
                return getSJSPNext();
            } else {

                setSJSPNext("bdestation.jsp");
                // MASCHINE
                if (option.substring(0, 2).equals("$M")) {

                    String maschine = option.substring(2);

                    try {
                        MaschineDto maschineDto = getZeiterfassungsFac()
                                .maschineFindByCIdentifikationsnr(maschine);
                        hmParameter.put("zeitdaten", zeitdatenDto);
                        hmParameter.put("maschine", maschineDto);
                        setSJSPNext("bdestation4.jsp");
                        return getSJSPNext();

                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response).setSMsg("Maschine '" + maschine + "' nicht gefunden! ");
                        return getSJSPNext();
                    }

                } // TAETIGKEIT
                else {

                    String taetigkeit = option.substring(2);

                    com.lp.server.artikel.service.ArtikelDto artikelDto = null;
                    try {
                        artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto);

                        zeitdatenDto.setArtikelIId(
                                getArtikelFac().artikelFindByCNr(taetigkeit, theclientDto).getIId());

                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response)
                                .setSMsg("T\u00E4tigkeit '" + taetigkeit + "' nicht gefunden! ");
                        return getSJSPNext();
                    }

                    String beleg = (String) hmParameter.get("beleg");

                    if (beleg.substring(0, 1).equals("L")) {
                        com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                                .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                        ZeitverteilungDto[] zvDtos = getZeiterfassungsFac()
                                .zeitverteilungFindByPersonalIIdUndTag(zeitdatenDto.getPersonalIId(),
                                        new Timestamp(System.currentTimeMillis()));
                        if (zvDtos != null & zvDtos.length > 0) {

                            if (zvDtos[0].getArtikelIId() == null) {
                                // Abschlussbuchung eintragen
                                ZeitverteilungDto zv = zvDtos[0];
                                zv.setIId(null);
                                zv.setLosIId(losDto.getIId());
                                zv.setArtikelIId(artikelDto.getIId());
                                try {
                                    getZeiterfassungsFac().createZeitverteilung(zv, theclientDto);
                                } catch (EJBExceptionLP e) {
                                    hmParameter.remove("beleg");
                                    getTheClient(request, response).setData(hmParameter);
                                    getTheClient(request, response).setSMsg(
                                            "Los '" + losDto.getCNr() + "' wurde bereits mit $PLUS gebucht");
                                    setSJSPNext("bdestation2.jsp");
                                    return getSJSPNext();
                                }

                                hmParameter.remove("beleg");
                                getTheClient(request, response).setData(hmParameter);
                                getTheClient(request, response)
                                        .setSMsg("Beginnbuchungen f\u00FCr 'Zeitverteilung' abgeschlossen");
                                setSJSPNext("bdestation.jsp");
                                return getSJSPNext();
                            }
                        }

                        com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelDto.getIId());

                        if (dtos != null && dtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        } else {
                            com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                                    .lossollarbeitsplanFindByLosIId(losDto.getIId());
                            if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                                zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                            } else {
                                // Bemerkung
                                getTheClient(request, response)
                                        .setSMsg("Los " + beleg.substring(1) + " hat keine Positionen");
                                return getSJSPNext();

                            }
                        }
                    }

                    try {
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, true, theclientDto);
                        getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                                getTheClient(request, response).getData(), taetigkeit, theclientDto));

                        return getSJSPNext();
                    } catch (EJBExceptionLP ex2) {
                        getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                        return getSJSPNext();
                    }
                }
            }
        }
    } else if (command.equals(TheApp.CMD_ZE_BDESTATION3GUTSCHLECHT)) {
        HashMap<Object, Object> hmParameter = (HashMap<Object, Object>) getTheClient(request, response)
                .getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");

        String gutstueck = request.getParameter("gutstueck");
        if (gutstueck.equals("")) {
            gutstueck = "0";
        }
        String schlechtstueck = request.getParameter("schlechtstueck");
        if (schlechtstueck.equals("")) {
            schlechtstueck = "0";
        }

        BigDecimal bdGutstueck = null;
        BigDecimal bdSchlechtstueck = null;
        try {
            bdGutstueck = new BigDecimal(gutstueck);
            bdSchlechtstueck = new BigDecimal(schlechtstueck);
        } catch (NumberFormatException ex9) {
            getTheClient(request, response)
                    .setSMsg("Gut/Schlechtst\u00FCck d\u00FCrfen nur aus Zahlen bestehen.");
            setSJSPNext("bdestation.jsp");
            return getSJSPNext();
        }

        if (bdGutstueck.doubleValue() < 0 || bdSchlechtstueck.doubleValue() < 0) {
            getTheClient(request, response).setSMsg("Gut/Schlechtst\u00FCck m\u00FCssen Positiv sein.");
            setSJSPNext("bdestation.jsp");
            return getSJSPNext();
        }
        Integer taetigkeitIId_Ende = getZeiterfassungsFac()
                .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId();
        ZeitdatenDto zeitdatenDtoEnde = new ZeitdatenDto();
        zeitdatenDtoEnde.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 1000));
        zeitdatenDtoEnde.setTaetigkeitIId(taetigkeitIId_Ende);
        zeitdatenDtoEnde.setPersonalIId(zeitdatenDto.getPersonalIId());

        // Hole letzten begonnenen Auftrag und hinterlege gut/schlechtstueck
        Session session = FLRSessionFactory.getFactory().openSession();
        org.hibernate.Criteria liste = session.createCriteria(FLRZeitdaten.class);
        liste.add(Expression.eq(ZeiterfassungFac.FLR_ZEITDATEN_PERSONAL_I_ID, zeitdatenDto.getPersonalIId()));
        liste.add(Expression.gt(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT,
                Helper.cutTimestamp(zeitdatenDto.getTZeit())));

        liste.addOrder(Order.desc(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT));
        List<?> letzerAuftrag = liste.list();

        Iterator<?> it = letzerAuftrag.iterator();
        ZeitdatenDto letzterAuftrag = null;

        while (it.hasNext()) {
            FLRZeitdaten flrLetzerAuftrag = (FLRZeitdaten) it.next();

            if (flrLetzerAuftrag.getC_belegartnr() != null && flrLetzerAuftrag.getI_belegartid() != null) {
                if (flrLetzerAuftrag.getC_belegartnr().equals(zeitdatenDto.getCBelegartnr())
                        && flrLetzerAuftrag.getI_belegartid().equals(zeitdatenDto.getIBelegartid())) {
                    letzterAuftrag = getZeiterfassungsFac()
                            .zeitdatenFindByPrimaryKey(flrLetzerAuftrag.getI_id(), theclientDto);
                    break;
                }
            } else if (flrLetzerAuftrag.getTaetigkeit_i_id() != null
                    && flrLetzerAuftrag.getTaetigkeit_i_id().equals(taetigkeitIId_Ende)) {
                break;
            }

        }

        if (letzterAuftrag != null) {
            // Hier eintragen
            // letzterAuftrag.setNGut(bdGutstueck);
            // letzterAuftrag.setNSchlecht(bdSchlechtstueck);
            getZeiterfassungsFac().updateZeitdaten(letzterAuftrag, theclientDto);
            // und buche ENDE
            getZeiterfassungsFac().createZeitdaten(zeitdatenDtoEnde, false, false, false, theclientDto);

        } else {
            // was nun?
            // Beginn und ende Buchen
            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, false, false, false, theclientDto);
            getZeiterfassungsFac().createZeitdaten(zeitdatenDtoEnde, false, false, false, theclientDto);

        }

        session.close();
        getTheClient(request, response).setSMsg(
                getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(), null, theclientDto));

        setSJSPNext("bdestation.jsp");
        return getSJSPNext();
    }

    else if (command.equals(TheApp.CMD_ZE_BDESTATION4)) {
        HashMap<?, ?> hmParameter = (HashMap<?, ?>) getTheClient(request, response).getData();
        ZeitdatenDto zeitdatenDto = (ZeitdatenDto) hmParameter.get("zeitdaten");
        zeitdatenDto.setTZeit(new Timestamp(System.currentTimeMillis()));

        String taetigkeit = request.getParameter("taetigkeit");
        getTheClient(request, response).setSMsg("");
        if (taetigkeit != null && taetigkeit.length() > 0) {

            setSJSPNext("bdestation.jsp");
            com.lp.server.artikel.service.ArtikelDto artikelDto = null;
            try {
                artikelDto = getArtikelFac().artikelFindByCNr(taetigkeit.substring(2), theclientDto);

                zeitdatenDto.setArtikelIId(artikelDto.getIId());
            } catch (EJBExceptionLP ex2) {
                getTheClient(request, response)
                        .setSMsg("T\u00E4tigkeit '" + taetigkeit.substring(2) + "' nicht gefunden! ");
                return getSJSPNext();
            }

            String beleg = (String) hmParameter.get("beleg");

            if (beleg.substring(0, 1).equals("L")) {
                com.lp.server.fertigung.service.LosDto losDto = getFertigungFac()
                        .losFindByCNrMandantCNr(beleg.substring(1), mandant);

                com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                        .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                artikelDto.getIId());

                if (hmParameter.containsKey("fertig")) {

                    if (dtos != null && dtos.length > 0) {

                        LossollarbeitsplanDto dto = dtos[0];
                        dto.setBFertig(Helper.boolean2Short(true));

                        ParametermandantDto parameterDtoTriggerTops = getParameterFac().getMandantparameter(
                                mandant, ParameterFac.KATEGORIE_FERTIGUNG,
                                ParameterFac.PARAMETER_TRIGGERT_TRUMPF_TOPS_ABLIEFERUNG);

                        try {
                            getFertigungFac().updateLossollarbeitsplan(dto, theclientDto);

                            // PJ 17916
                            if (parameterDtoTriggerTops.getCWert() != null
                                    && parameterDtoTriggerTops.getCWert().trim().length() > 0) {
                                ArtikelDto aDto = getArtikelFac().artikelFindByCNrMandantCNrOhneExc(
                                        parameterDtoTriggerTops.getCWert().trim(), theclientDto.getMandant());

                                if (aDto == null) {
                                    getTheClient(request, response).setSMsg(
                                            "Der Artikel, der im Parameter TRIGGERT_TRUMPF_TOPS_ABLIEFERUNG hinterlegt ist, exisitiert nicht! "
                                                    + parameterDtoTriggerTops.getCWert());
                                    return getSJSPNext();
                                }
                                if (aDto != null && aDto.getIId().equals(artikelDto.getIId())) {
                                    getFertigungFac().bucheTOPSArtikelAufHauptLager(losDto.getIId(),
                                            theclientDto, null);
                                }
                            }

                            getTheClient(request, response)
                                    .setSMsg(getMeldungGebuchtFuerBDE(getTheClient(request, response).getData(),
                                            taetigkeit.substring(2), theclientDto));
                            return getSJSPNext();
                        } catch (EJBExceptionLP ex2) {
                            getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                            return getSJSPNext();
                        }
                    } else {
                        getTheClient(request, response).setSMsg("Das Los " + beleg.substring(1)
                                + " hat keinen entsprechen Arbeitsgang mit der Artikelnummer "
                                + taetigkeit.substring(2));
                        return getSJSPNext();
                    }

                }

                if (dtos != null && dtos.length > 0) {
                    zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                } else {
                    com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtosErstePosition = getFertigungFac()
                            .lossollarbeitsplanFindByLosIId(losDto.getIId());
                    if (dtosErstePosition != null && dtosErstePosition.length > 0) {
                        zeitdatenDto.setIBelegartpositionid(dtosErstePosition[0].getIId());
                    } else {
                        // Bemerkung
                        getTheClient(request, response)
                                .setSMsg("Los " + beleg.substring(1) + " hat keine Positionen");
                        return getSJSPNext();

                    }
                }
            }

            // Maschinenzeitdaten buchen (geht nur auf Los)

            if (hmParameter.containsKey("maschine") && zeitdatenDto.getCBelegartnr() != null
                    && zeitdatenDto.getCBelegartnr().equals(LocaleFac.BELEGART_LOS)
                    && zeitdatenDto.getIBelegartid() != null) {
                MaschineDto maschineDto = (MaschineDto) hmParameter.get("maschine");

                com.lp.server.fertigung.service.LossollarbeitsplanDto[] dtos = getFertigungFac()
                        .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(zeitdatenDto.getIBelegartid(),
                                zeitdatenDto.getArtikelIId());

                if (dtos != null && dtos.length > 0) {
                    MaschinenzeitdatenDto maschinenzeitdatenDto = new MaschinenzeitdatenDto();
                    maschinenzeitdatenDto.setPersonalIIdGestartet(zeitdatenDto.getPersonalIId());
                    maschinenzeitdatenDto.setTVon(zeitdatenDto.getTZeit());
                    maschinenzeitdatenDto.setLossollarbeitsplanIId(dtos[0].getIId());
                    maschinenzeitdatenDto.setMaschineIId(maschineDto.getIId());
                    getZeiterfassungsFac().createMaschinenzeitdaten(maschinenzeitdatenDto, theclientDto);

                }
            }

            try {
                getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                getTheClient(request, response).setSMsg(getMeldungGebuchtFuerBDE(
                        getTheClient(request, response).getData(), taetigkeit.substring(2), theclientDto));
                return getSJSPNext();
            } catch (EJBExceptionLP ex2) {
                getTheClient(request, response).setSMsg("Fehler beim Buchen!");
                return getSJSPNext();
            }

        }
    } else if (command.equals(TheApp.CMD_ZE_MECS_ONLCHECK)) {
        String beleg = request.getParameter("beleg");

        if (beleg == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'beleg' muss angegeben werden");
            return null;
        }

        beleg = beleg.trim();

        if (beleg.length() < 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Parameter 'beleg' muss mindestens 2 Zeichen lang sein");
            return null;
        }

        String status = null;
        BigDecimal offeneMenge = new BigDecimal(0);

        String ueberliefernErlaubt = "1";
        try {

            if (beleg.substring(0, 2).equals("$A")) {

                AuftragDto auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, beleg.substring(2),
                        theclientDto);
                status = auftragDto.getAuftragstatusCNr();

            } else if (beleg.substring(0, 2).equals("$L")) {
                LosDto losDto = getFertigungFac().losFindByCNrMandantCNr(beleg.substring(2), mandant);
                status = losDto.getStatusCNr();

                BigDecimal erledigteMenge = getFertigungFac().getErledigteMenge(losDto.getIId(), theclientDto);
                offeneMenge = losDto.getNLosgroesse().subtract(erledigteMenge);

                if (losDto.getStuecklisteIId() != null) {
                    StuecklisteDto stkDto = getStuecklisteFac()
                            .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto);

                    if (Helper.short2boolean(stkDto.getBUeberlieferbar()) == false) {
                        ueberliefernErlaubt = "0";
                    }

                }

            }

        } catch (EJBExceptionLP ex8) {
            status = "Beleg existiert nicht";
        }

        StringBuffer sb = new StringBuffer();

        sb.append(Helper.fitString2Length(beleg, 40, ' '));
        sb.append(Helper.fitString2Length(status, 40, ' '));

        // Offene Menge 17 stellig

        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
        dfs.setDecimalSeparator('.');
        DecimalFormat dFormat = new DecimalFormat("0.0000", dfs);
        if (offeneMenge.doubleValue() < 0) {
            sb.append("-");
        } else {
            sb.append(" ");
        }
        sb.append(Helper.fitString2LengthAlignRight(dFormat.format(offeneMenge.abs()), 16, ' '));
        sb.append(ueberliefernErlaubt);
        sb.append("\r\n");

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_ONLINECHECK_ABL)) {
        String beleg = request.getParameter("beleg");
        String menge = request.getParameter("menge");

        if (beleg == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'beleg' muss angegeben werden");
            return null;
        }
        if (menge == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'menge' muss angegeben werden");
            return null;
        }

        BigDecimal nMenge = new BigDecimal(menge.trim());

        beleg = beleg.trim();

        if (beleg.length() < 2) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Parameter 'beleg' muss mindestens 2 Zeichen lang sein");
            return null;
        }

        String status = null;
        BigDecimal offeneMenge = new BigDecimal(0);
        boolean ueberliefernErlaubt = true;
        try {

            if (beleg.substring(0, 2).equals("$A")) {

                AuftragDto auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant, beleg.substring(2),
                        theclientDto);
                status = auftragDto.getAuftragstatusCNr();

            } else if (beleg.substring(0, 2).equals("$L")) {
                LosDto losDto = getFertigungFac().losFindByCNrMandantCNr(beleg.substring(2), mandant);
                status = losDto.getStatusCNr();

                BigDecimal erledigteMenge = getFertigungFac().getErledigteMenge(losDto.getIId(), theclientDto);
                offeneMenge = losDto.getNLosgroesse().subtract(erledigteMenge);
                if (losDto.getStuecklisteIId() != null) {
                    StuecklisteDto stkDto = getStuecklisteFac()
                            .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theclientDto);
                    ueberliefernErlaubt = Helper.short2boolean(stkDto.getBUeberlieferbar());
                }

            }

        } catch (EJBExceptionLP ex8) {
            status = "Beleg existiert nicht";
        }

        StringBuffer sb = new StringBuffer();

        // Zeile1
        sb.append(Helper.fitString2Length(beleg, 40, ' '));
        sb.append(Helper.fitString2Length(status, 40, ' '));
        sb.append("\r\n");
        // Offene Menge 17 stellig

        // Zeile2

        // Zuerst 3 Stellen Fehlernummer: 000= Abliefern moeglich 001=
        // Status erlaubt kein Abliefern - 002=
        // Menge der Ablieferung zu gross

        String fehlercode = "";
        String text1 = "";
        String text2 = "";
        if (status.equals(LocaleFac.STATUS_ERLEDIGT) || status.equals(LocaleFac.STATUS_STORNIERT)
                || status.equals(LocaleFac.STATUS_ANGELEGT) || status.equals(LocaleFac.STATUS_GESTOPPT)) {
            fehlercode = "001";
            text1 = "Nicht erlaubt!";
            text2 = "Status: " + status;
        } else {

            if (nMenge.doubleValue() <= offeneMenge.doubleValue()) {
                // Wenn Abliefermenge kleiner als Offene Menge, dann =OK
                fehlercode = "000";
                text1 = "Ablieferung";
                text2 = "erlaubt";
            } else {
                if (ueberliefernErlaubt == false) {
                    fehlercode = "002";
                    text1 = "Nicht erlaubt!";

                    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance();
                    dfs.setDecimalSeparator('.');
                    DecimalFormat dFormat = new DecimalFormat("#####0", dfs);
                    text2 = "Nur " + Helper.fitString2LengthAlignRight(dFormat.format(offeneMenge), 6, ' ')
                            + " Stk offen";
                } else {
                    fehlercode = "000";
                    text1 = "Ablieferung";
                    text2 = "erlaubt";
                }
            }
        }
        sb.append(fehlercode);

        // 37 Leerstellen
        sb.append(Helper.fitString2Length("", 37, ' '));

        // Text1
        sb.append(Helper.fitString2Length(text1, 20, ' '));
        // Text2
        sb.append(Helper.fitString2Length(text2, 20, ' '));

        sb.append("\r\n");

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_AUSWEISE)) {
        String fingerprint = request.getParameter("fingerprint");

        if (fingerprint != null) {
            StringBuffer sb = new StringBuffer();
            PersonalfingerDto[] personalfingerDtos = getZutrittscontrollerFac().personalfingerFindAll();
            for (int i = 0; i < personalfingerDtos.length; i++) {
                PersonalfingerDto personalfingerDto = personalfingerDtos[i];
                String id = personalfingerDto.getIId() + "";
                id = Helper.fitString2LengthAlignRight(id, 8, '0');
                StringBuffer tmp = new StringBuffer();
                // unbedingt nach ausweis sortieren
                tmp.setLength(0);
                tmp.append(Helper.fitString2Length(id, 20, ' '));

                PersonalDto personalDto = getPersonalFac()
                        .personalFindByPrimaryKey(personalfingerDto.getPersonalIId(), theclientDto);

                tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
                tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

                String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
                String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

                if (sVorname == null) {
                    sVorname = "";
                }
                tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
                sb.append(tmp).append("\r\n");

            }
            getTheClient(request, response).setSMsg(new String(sb));
        } else {

            // Ausweisnummern holen
            StringBuffer sb = new StringBuffer();
            PersonalDto[] personalDtos = getPersonalFac().personalFindByCAusweisSortiertNachCAusweis();

            ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                    ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_LEAD_IN_AUSWEISNUMMER_MECS);

            String leadIn = "";
            if (parameterDto.getCWert() != null) {
                leadIn = parameterDto.getCWert().trim();
            }

            for (int i = 0; i < personalDtos.length; i++) {
                PersonalDto personalDto = personalDtos[i];
                personalDto.setPartnerDto(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));
                StringBuffer tmp = new StringBuffer();
                // unbedingt nach ausweis sortieren
                tmp.setLength(0);
                tmp.append(Helper.fitString2Length(leadIn + personalDto.getCAusweis(), 20, ' ')); // ausweis
                tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
                tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

                String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
                String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

                if (sVorname == null) {
                    sVorname = "";
                }

                tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
                sb.append(tmp).append("\r\n");

            }

            getTheClient(request, response).setSMsg(new String(sb));
        }
    } else if (command.equals(TheApp.CMD_ZE_MECS_ERLAUBTETAETIGKEITEN)) {

        Session session = FLRSessionFactory.getFactory().openSession();
        org.hibernate.Criteria liste = session.createCriteria(FLRTaetigkeit.class);
        liste.add(Expression.eq(ZeiterfassungFac.FLR_TAETIGKEIT_B_BDEBUCHBAR, Helper.boolean2Short(true)));
        liste.addOrder(Order.asc("c_nr"));
        List<?> lReisezeiten = liste.list();
        Iterator<?> it = lReisezeiten.iterator();

        StringBuffer sb = new StringBuffer();

        while (it.hasNext()) {
            FLRTaetigkeit flrTaetigkeit = (FLRTaetigkeit) it.next();
            StringBuffer tmp = new StringBuffer();

            tmp.setLength(0);
            tmp.append('$');
            tmp.append(Helper.fitString2LengthAlignRight(flrTaetigkeit.getC_nr(), 14, ' ')); // persnr
            sb.append(tmp).append("\r\n");

        }
        session.close();

        getTheClient(request, response).setSMsg(new String(sb));

    } else if (command.equals(TheApp.CMD_ZE_MECS_PERSSTAMM)) {
        // Personalstamm holen

        StringBuffer sb = new StringBuffer();

        // unbedingt nach personalnummer sortieren
        PersonalDto[] personalDtos = getPersonalFac().personalFindByCAusweisSortiertNachPersonalnr();
        for (int i = 0; i < personalDtos.length; i++) {
            PersonalDto personalDto = personalDtos[i];
            personalDto.setPartnerDto(
                    getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));
            StringBuffer tmp = new StringBuffer();

            tmp.setLength(0);
            tmp.append(Helper.fitString2LengthAlignRight(personalDto.getCPersonalnr() + "", 5, '0')); // persnr
            tmp.append(Helper.fitString2Length("", 3, ' ')); // zutrkl

            String sVorname = personalDto.getPartnerDto().getCName2vornamefirmazeile2();
            String sNachname = personalDto.getPartnerDto().getCName1nachnamefirmazeile1();

            if (sVorname == null) {
                sVorname = "";
            }

            tmp.append(Helper.fitString2Length(sVorname + " " + sNachname, 25, ' ')); // name
            sb.append(tmp).append("\r\n");

        }
        getTheClient(request, response).setSMsg(new String(sb));
    } else if (command.equals(TheApp.CMD_ZE_MECS_SALDO)) {
        String ausweis = "";
        try {
            ausweis = request.getParameter("ausweis");
        } catch (Exception e) {
            getTheClient(request, response).setBResponseIsReady(true);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'ausweis' nicht angegeben");
            myLogger.error("doPost; Exception aufgetreten", e);
            return null;
        }
        if (ausweis.startsWith("$P")) {
            ausweis = ausweis.substring(2);
        }

        ausweis = ausweis.trim();

        ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_PERSONAL, ParameterFac.PARAMETER_LEAD_IN_AUSWEISNUMMER_MECS);

        String leadIn = "";
        if (parameterDto.getCWert() != null) {
            leadIn = parameterDto.getCWert().trim();
            int iLaenge = leadIn.length();
            if (ausweis.length() > iLaenge) {
                ausweis = ausweis.substring(iLaenge);
            }
        }

        PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis);

        personalDto.setPartnerDto(
                getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

        java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis() - 3600000 * 24);
        ts = com.lp.util.Helper.cutTimestamp(ts);

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(ts.getTime());

        String urlaub = null;
        try {
            urlaub = getZeiterfassungsFac().erstelleMonatsAbrechnungFuerBDE(personalDto.getIId(),
                    new Integer(c.get(Calendar.YEAR)), new Integer(c.get(Calendar.MONTH)), false,
                    new java.sql.Date(ts.getTime()), theclientDto, true, false);

        } catch (EJBExceptionLP ex7) {

            if (ex7.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP e = (EJBExceptionLP) ex7.getCause();
                if (e != null && e.getCode() == EJBExceptionLP.FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM) {
                    getTheClient(request, response)
                            .setSMsg(new String("FEHLER_PERSONAL_FEHLER_BEI_EINTRITTSDATUM"));
                    return getSJSPNext();

                }
            }
            getTheClient(request, response).setSMsg(new String(ex7.getMessage()));
            setSJSPNext("bdestation.jsp");

        }

        getTheClient(request, response).setSMsg(urlaub);

    } else if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)
            || command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHENFINGERPRINT)) {
        String record = null;

        if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)) {
            record = request.getParameter("record");
        } else {
            record = request.getParameter("recordfingerprint");

        }

        record = Helper.fitString2Length(record, 200, ' ');

        String schluesselNr = record.substring(19, 39).trim();
        String zeit = record.substring(5, 19);
        String taetigkeit = record.substring(3, 5);
        // SP753
        String terminal = record.substring(64, 86);
        // Damit die Sollzeitenueberschreitungspruefeung nicht durchgefuehrt
        // wird:
        terminal = "ZT:" + terminal;
        terminal = terminal.trim();

        boolean bAbliefern = false;

        ArtikelDto artikelDtoTaetigkeit = null;

        if (record.substring(130, 155).trim().equals("$ABLIEFERN")) {
            bAbliefern = true;
        } else {

            artikelDtoTaetigkeit = getArtikelFac().artikelFindByCNrOhneExc(record.substring(132, 155).trim(),
                    theclientDto);
        }

        ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
        ZeitdatenDto zeitdatenDtoEnde = new ZeitdatenDto();
        zeitdatenDto.setCWowurdegebucht(terminal);
        zeitdatenDtoEnde.setCWowurdegebucht(terminal);

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, new Integer(zeit.substring(0, 4)).intValue());
        c.set(Calendar.MONTH, new Integer(zeit.substring(4, 6)).intValue() - 1);
        c.set(Calendar.DAY_OF_MONTH, new Integer(zeit.substring(6, 8)).intValue());
        c.set(Calendar.HOUR_OF_DAY, new Integer(zeit.substring(8, 10)).intValue());
        c.set(Calendar.MINUTE, new Integer(zeit.substring(10, 12)).intValue());
        c.set(Calendar.SECOND, new Integer(zeit.substring(12, 14)).intValue());

        zeitdatenDto.setTZeit(new java.sql.Timestamp(c.getTime().getTime()));
        zeitdatenDtoEnde.setTZeit(new java.sql.Timestamp(c.getTime().getTime() + 1000));
        zeitdatenDto.setTAendern(zeitdatenDto.getTZeit());
        zeitdatenDtoEnde.setTAendern(zeitdatenDtoEnde.getTZeit());

        // Wenn hier NullPointerException, dann kann kein Personal mit
        // Ausweisnummer gefunden werden
        Integer personalIId = null;

        if (schluesselNr.startsWith("$P")) {
            try {
                personalIId = getPersonalFac().personalFindByCAusweis(schluesselNr.substring(2)).getIId();
            } catch (NullPointerException ex11) {
                String msg = "Person mit Ausweis " + schluesselNr + " nicht vorhanden. ORIGINAL-Request:"
                        + record;
                myLogger.error(msg, ex11);
                response.setStatus(HttpServletResponse.SC_OK);
                return getSJSPNext();

            }

        } else {
            if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHEN)) {
                try {
                    personalIId = getPersonalFac().personalFindByCAusweis(schluesselNr).getIId();
                } catch (NullPointerException ex11) {
                    String msg = "Person mit Ausweis " + schluesselNr + " nicht vorhanden. ORIGINAL-Request:"
                            + record;
                    myLogger.error(msg, ex11);

                    response.setStatus(HttpServletResponse.SC_OK);
                    return getSJSPNext();

                }

            } else if (command.equals(TheApp.CMD_ZE_MECS_ZEITBUCHENFINGERPRINT)) {
                Integer i = new Integer(schluesselNr);
                getZutrittscontrollerFac().personalfingerFindByPrimaryKey(i).getPersonalIId();
                personalIId = getZutrittscontrollerFac().personalfingerFindByPrimaryKey(i).getPersonalIId();
            }
        }

        zeitdatenDto.setPersonalIId(personalIId);
        zeitdatenDtoEnde.setPersonalIId(personalIId);
        zeitdatenDtoEnde.setTaetigkeitIId(getZeiterfassungsFac()
                .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId());

        // Taetigkeiten, die MECS liefert muessen in der Tabelle LP_KEYVALUE
        // uebersetzt werden (als String)
        // Bsp: MECSTERMINAL|B1|KOMMT|java.lang.String
        try {
            String sTaetigkeit = null;

            if (schluesselNr.startsWith("$P")) {
                sTaetigkeit = record.substring(110, 126);
                Integer taetigkeitIId_Ende = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_ENDE, theclientDto).getIId();
                String gutStueck = record.substring(160, 172);
                String schlechtStueck = record.substring(173, 189);

                BigDecimal nGutStueck = new BigDecimal(gutStueck.trim());
                BigDecimal nSchlechtStueck = new BigDecimal(schlechtStueck.trim());
                Integer artikelIId = null;

                if (artikelDtoTaetigkeit == null) {
                    ParametermandantDto parameterDto = getParameterFac().getMandantparameter(mandant,
                            ParameterFac.KATEGORIE_ALLGEMEIN,
                            ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

                    if (parameterDto != null && parameterDto.getCWert() != null
                            && !parameterDto.getCWert().trim().equals("")) {
                        try {
                            artikelIId = getArtikelFac().artikelFindByCNr(parameterDto.getCWert(), theclientDto)
                                    .getIId();

                        } catch (RemoteException ex2) {
                            myLogger.error("Default-Arbeitszeitartikel " + parameterDto.getCWert()
                                    + " nicht vorhanden.", ex2);
                            return getSJSPNext();
                        }

                    } else {
                        myLogger.error(
                                "Default-Arbeitszeitartikel " + parameterDto.getCWert() + " nicht definiert.");
                        return getSJSPNext();
                    }
                } else {
                    artikelIId = artikelDtoTaetigkeit.getIId();
                }

                if (sTaetigkeit.startsWith("$A")) {

                    AuftragDto auftragDto = null;
                    try {
                        if (sTaetigkeit.startsWith("$A")) {
                            auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant,
                                    sTaetigkeit.substring(2).trim(), theclientDto);
                        } else {
                            auftragDto = getAuftragFac().auftragFindByMandantCNrCNr(mandant,
                                    sTaetigkeit.substring(3).trim(), theclientDto);

                        }
                    } catch (RemoteException ex8) {
                        zeitdatenDto.setCBemerkungZuBelegart(
                                "Auftrag " + sTaetigkeit.substring(2).trim() + " konnte nicht gefunden werden");
                        zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);

                        return getSJSPNext();
                    }
                    // Wenn Auftragsbeginn ->
                    if (sTaetigkeit.startsWith("$A")) {
                        AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                                .auftragpositionFindByAuftrag(auftragDto.getIId());

                        if (auftragpositionDtos.length > 0) {
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_AUFTRAG);
                            zeitdatenDto.setArtikelIId(artikelIId);
                            zeitdatenDto.setIBelegartid(auftragDto.getIId());
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                        } else {
                            myLogger.error("Buchung von MECS-TERMINAL, Ausweis: " + schluesselNr + ", Auftrag"
                                    + sTaetigkeit + " hat keine Positionen.");
                            return getSJSPNext();
                        }
                    }
                } else if (sTaetigkeit.startsWith("$EL") || sTaetigkeit.startsWith("$L")) {

                    com.lp.server.fertigung.service.LosDto losDto = null;
                    try {
                        if (sTaetigkeit.startsWith("$L")) {

                            losDto = getFertigungFac().losFindByCNrMandantCNr(

                                    sTaetigkeit.substring(2).trim(), mandant);

                        } else {
                            losDto = getFertigungFac().losFindByCNrMandantCNr(

                                    sTaetigkeit.substring(3).trim(), mandant);

                        }

                    } catch (EJBExceptionLP ex10) {
                        zeitdatenDto.setCBemerkungZuBelegart(
                                "Los " + sTaetigkeit.substring(2).trim() + " konnte nicht gefunden werden");
                        zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                        return getSJSPNext();
                    }

                    if (bAbliefern == true) {
                        LosablieferungDto losablieferungDto = new LosablieferungDto();
                        losablieferungDto.setLosIId(losDto.getIId());
                        String menge = record.substring(155, 170);
                        BigDecimal nMenge = new BigDecimal(menge.trim());
                        losablieferungDto.setNMenge(nMenge);
                        losablieferungDto.setTAendern(zeitdatenDto.getTZeit());

                        if (nMenge.doubleValue() > 0) {
                            // lt. FM
                            BigDecimal bdBisherErledigt = getFertigungFac().getErledigteMenge(losDto.getIId(),
                                    theclientDto);

                            if (bdBisherErledigt.add(nMenge).doubleValue() > losDto.getNLosgroesse()
                                    .doubleValue()) {
                                getFertigungFac().aendereLosgroesse(losDto.getIId(),
                                        bdBisherErledigt.add(nMenge).intValue(), false, theclientDto);

                                // SP933
                                losDto.setNLosgroesse(bdBisherErledigt.add(nMenge));

                            }

                            try {
                                getFertigungFac().bucheMaterialAufLos(losDto, nMenge, false, false, true,
                                        theclientDto, null, false);
                            } catch (Exception e1) {
                                // Terminal darf keinen Fehler bekommen
                            }

                            getFertigungFac().createLosablieferungFuerTerminalOhnePreisberechnung(
                                    losablieferungDto, theclientDto, false);

                            try {
                                getFertigungFac().aktualisiereNachtraeglichPreiseAllerLosablieferungen(
                                        losDto.getIId(), theclientDto, true);
                            } catch (Exception e) {
                                // PREISBERECHNUNG FEHLGESCHLAGEN
                                myLogger.error("Preisberechnung der Ablieferungen f\u00FCr Los "
                                        + losDto.getCNr() + " fehlgeschlagen. Bitte manuell ausfuehren", e);
                            }
                        }

                        // PJ17748

                        ParametermandantDto parameterAblieferungBuchtEndeDto = getParameterFac()
                                .getMandantparameter(mandant, ParameterFac.KATEGORIE_FERTIGUNG,
                                        ParameterFac.PARAMETER_ABLIEFERUNG_BUCHT_ENDE);

                        Boolean bAblieferungBuchtEndeDto = (Boolean) parameterAblieferungBuchtEndeDto
                                .getCWertAsObject();

                        if (bAblieferungBuchtEndeDto == true) {
                            zeitdatenDto.setTaetigkeitIId(taetigkeitIId_Ende);
                            zeitdatenDto.setCBelegartnr(null);
                            zeitdatenDto.setArtikelIId(null);
                            zeitdatenDto.setIBelegartid(null);
                            zeitdatenDto.setIBelegartpositionid(null);
                            Integer zeitdatenIId = getZeiterfassungsFac().createZeitdaten(zeitdatenDto, false,
                                    false, false, theclientDto);

                            // PJ17797
                            if (nMenge.doubleValue() > 0) {
                                if (getMandantFac().darfAnwenderAufZusatzfunktionZugreifen(
                                        MandantFac.ZUSATZFUNKTION_STUECKRUECKMELDUNG, theclientDto)) {

                                    Integer lossollarbeitsplanIId = null;
                                    LossollarbeitsplanDto[] sollDtos = getFertigungFac()
                                            .lossollarbeitsplanFindByLosIId(losDto.getIId());
                                    if (sollDtos.length > 0) {
                                        lossollarbeitsplanIId = sollDtos[sollDtos.length - 1].getIId();
                                    } else {
                                        lossollarbeitsplanIId = getFertigungFac()
                                                .defaultArbeitszeitartikelErstellen(losDto, theclientDto);
                                    }

                                    LosgutschlechtDto losgutschlechtDto = new LosgutschlechtDto();
                                    losgutschlechtDto.setZeitdatenIId(zeitdatenIId);
                                    losgutschlechtDto.setLossollarbeitsplanIId(lossollarbeitsplanIId);
                                    losgutschlechtDto.setNGut(nMenge);
                                    losgutschlechtDto.setNSchlecht(new BigDecimal(0));
                                    losgutschlechtDto.setNInarbeit(new BigDecimal(0));

                                    getFertigungFac().createLosgutschlecht(losgutschlechtDto, theclientDto);
                                }
                            }

                        }

                        return getSJSPNext();
                    }

                    // Wenn Auftragsbeginn ->
                    if (sTaetigkeit.startsWith("$L")) {
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                        zeitdatenDto.setArtikelIId(artikelIId);
                        zeitdatenDto.setIBelegartid(losDto.getIId());

                        LossollarbeitsplanDto[] sollDtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(losDto.getIId(),
                                        artikelIId);
                        if (sollDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(sollDtos[0].getIId());
                        }
                    } else {
                        // Hole letzten begonnenen Auftrag und hinterlege
                        // gut/schlechtstueck
                        Session session = FLRSessionFactory.getFactory().openSession();
                        org.hibernate.Criteria liste = session.createCriteria(FLRZeitdaten.class);
                        liste.add(Expression.eq(ZeiterfassungFac.FLR_ZEITDATEN_PERSONAL_I_ID, personalIId));
                        /*
                         * liste.add(Expression.eq(ZeiterfassungFac.
                         * FLR_ZEITDATEN_C_BELEGARTNR ,
                         * LocaleFac.BELEGART_LOS)); liste.add(Expression.eq
                         * (ZeiterfassungFac.FLR_ZEITDATEN_I_BELEGARTID,
                         * losDto.getIId()));
                         */
                        liste.add(Expression.gt(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT,
                                Helper.cutTimestamp(zeitdatenDto.getTZeit())));

                        liste.addOrder(Order.desc(ZeiterfassungFac.FLR_ZEITDATEN_T_ZEIT));
                        // liste.setMaxResults(1);
                        List<?> lReisezeiten = liste.list();

                        Iterator<?> it = lReisezeiten.iterator();
                        zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                        zeitdatenDto.setArtikelIId(artikelIId);
                        zeitdatenDto.setIBelegartid(losDto.getIId());

                        ZeitdatenDto letzterAuftrag = null;
                        while (it.hasNext()) {
                            FLRZeitdaten flrLetzerAuftrag = (FLRZeitdaten) it.next();

                            if (flrLetzerAuftrag.getC_belegartnr() != null
                                    && flrLetzerAuftrag.getI_belegartid() != null) {
                                if (flrLetzerAuftrag.getC_belegartnr().equals(zeitdatenDto.getCBelegartnr())
                                        && flrLetzerAuftrag.getI_belegartid()
                                                .equals(zeitdatenDto.getIBelegartid())) {
                                    letzterAuftrag = getZeiterfassungsFac().zeitdatenFindByPrimaryKey(
                                            flrLetzerAuftrag.getI_id(), theclientDto);
                                    break;
                                }
                            } else if (flrLetzerAuftrag.getTaetigkeit_i_id() != null
                                    && flrLetzerAuftrag.getTaetigkeit_i_id().equals(taetigkeitIId_Ende)) {
                                break;
                            }

                        }

                        if (letzterAuftrag != null) {
                            // Hier eintragen
                            ZeitdatenDto auftragsbeginn = getZeiterfassungsFac()
                                    .zeitdatenFindByPrimaryKey(letzterAuftrag.getIId(), theclientDto);

                            // auftragsbeginn.setNGut(nGutStueck);
                            // auftragsbeginn.setNSchlecht(nSchlechtStueck);
                            getZeiterfassungsFac().updateZeitdaten(auftragsbeginn, theclientDto);
                            // und buche ENDE
                            zeitdatenDto = zeitdatenDtoEnde;

                        } else {
                            zeitdatenDto.setCBelegartnr(LocaleFac.BELEGART_LOS);
                            zeitdatenDto.setArtikelIId(artikelIId);
                            zeitdatenDto.setIBelegartid(losDto.getIId());
                            // zeitdatenDto.setNGut(nGutStueck);
                            // zeitdatenDto.setNSchlecht(nSchlechtStueck);

                            getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false,
                                    theclientDto);
                            zeitdatenDto = zeitdatenDtoEnde;
                        }
                        session.close();

                        /*
                         * if (lReisezeiten.size() > 0) { FLRZeitdaten
                         * flrZeitdaten = (FLRZeitdaten)
                         * lReisezeiten.iterator().next();
                         * 
                         * ZeitdatenDto losbeginn = getZeiterfassungsFac().
                         * zeitdatenFindByPrimaryKey(flrZeitdaten.getI_id(),
                         * cNrUser);
                         * 
                         * losbeginn.setNGut(nGutStueck);
                         * losbeginn.setNSchlecht(nSchlechtStueck);
                         * getZeiterfassungsFac().updateZeitdaten(losbeginn,
                         * cNrUser); //und buche ENDE zeitdatenDto =
                         * zeitdatenDtoEnde; } else {
                         * zeitdatenDto.setCBelegartnr
                         * (LocaleFac.BELEGART_LOS);
                         * zeitdatenDto.setArtikelIId(artikelIId);
                         * zeitdatenDto.setIBelegartid(losDto.getIId());
                         * zeitdatenDto.setNGut(nGutStueck);
                         * zeitdatenDto.setNSchlecht(nSchlechtStueck);
                         * getZeiterfassungsFac
                         * ().createZeitdaten(zeitdatenDto, true, true,
                         * cNrUser); zeitdatenDto = zeitdatenDtoEnde; }
                         * 
                         * session.close();
                         */

                    }

                } else {
                    zeitdatenDto.setTaetigkeitIId(getZeiterfassungsFac().taetigkeitFindByCNr(
                            Helper.fitString2Length(sTaetigkeit.substring(1), 15, ' '), theclientDto).getIId());
                }

            }

            else {
                sTaetigkeit = getSystemServicesFac()
                        .keyvalueFindByPrimaryKey(SystemServicesFac.KEYVALUE_MECSTERMINAL, taetigkeit)
                        .getCValue();
                if (sTaetigkeit != null && !sTaetigkeit.equals(ZeiterfassungFac.TAETIGKEIT_REISE.trim())) {
                    zeitdatenDto.setTaetigkeitIId(getZeiterfassungsFac()
                            .taetigkeitFindByCNr(Helper.fitString2Length(sTaetigkeit, 15, ' '), theclientDto)
                            .getIId());
                }
            }

            // Resezeiten wenn Taetigkeit REISE
            if (sTaetigkeit != null && sTaetigkeit.equals(ZeiterfassungFac.TAETIGKEIT_REISE.trim())) {
                ReiseDto reiseDto = new ReiseDto();
                reiseDto.setPersonalIId(personalIId);

                // Letzte Reise von HEUTE holen
                // Heute 00:00 Uhr
                Calendar cTemp = Calendar.getInstance();
                cTemp.setTimeInMillis(zeitdatenDto.getTZeit().getTime());
                cTemp.set(Calendar.HOUR_OF_DAY, 0);
                cTemp.set(Calendar.MINUTE, 0);
                cTemp.set(Calendar.SECOND, 0);
                cTemp.set(Calendar.MILLISECOND, 0);

                Session sessReise = FLRSessionFactory.getFactory().openSession();
                org.hibernate.Criteria reisezeiten = sessReise.createCriteria(FLRReise.class);
                reisezeiten.add(Expression.eq(ZeiterfassungFac.FLR_REISE_PERSONAL_I_ID, personalIId));
                reisezeiten.add(Expression.ge(ZeiterfassungFac.FLR_REISE_T_ZEIT,
                        new Timestamp(cTemp.getTimeInMillis())));
                reisezeiten.add(Expression.lt(ZeiterfassungFac.FLR_REISE_T_ZEIT, zeitdatenDto.getTZeit()));
                reisezeiten.addOrder(Order.desc(ZeiterfassungFac.FLR_REISE_T_ZEIT));
                reisezeiten.setMaxResults(1);
                List<?> lReisezeiten = reisezeiten.list();

                if (lReisezeiten.size() == 0) {
                    reiseDto.setBBeginn(Helper.boolean2Short(true));
                } else {
                    FLRReise flrReise = (FLRReise) lReisezeiten.get(0);
                    if (Helper.short2boolean(flrReise.getB_beginn()) == true) {
                        reiseDto.setBBeginn(Helper.boolean2Short(false));
                    } else {
                        reiseDto.setBBeginn(Helper.boolean2Short(true));

                    }
                }

                reiseDto.setTZeit(zeitdatenDto.getTZeit());

                Integer partnerMandant = getMandantFac().mandantFindByPrimaryKey(mandant, theclientDto)
                        .getPartnerIId();
                PartnerDto partnerDto = getPartnerFac().partnerFindByPrimaryKey(partnerMandant, theclientDto);

                if (partnerDto.getLandplzortIId() == null) {
                    throw new Exception("Mandant hat kein Land hinterlegt");
                }

                DiaetenDto[] dtos = getZeiterfassungsFac()
                        .diaetenFindByLandIId(partnerDto.getLandplzortDto().getIlandID());

                if (dtos.length == 0) {
                    // Einen anlegen
                    DiaetenDto dto = new DiaetenDto();
                    dto.setCBez(partnerDto.getLandplzortDto().getLandDto().getCName());
                    dto.setLandIId(partnerDto.getLandplzortDto().getIlandID());
                    reiseDto.setDiaetenIId(getZeiterfassungsFac().createDiaeten(dto));
                } else {
                    reiseDto.setDiaetenIId(dtos[0].getIId());
                }

                getZeiterfassungsFac().createReise(reiseDto, theclientDto);
                response.setStatus(HttpServletResponse.SC_OK);
                response.flushBuffer();
                getTheClient(request, response).setBResponseIsReady(true);

                return getSJSPNext();
            }
        } catch (Exception ex3) {
            ex3.printStackTrace();
            // lt. FM darf an das MECS-Terminal nur Status=200
            // zurueckgegeben werden
            response.setStatus(HttpServletResponse.SC_OK);
            return getSJSPNext();
        }

        getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);

        response.setStatus(HttpServletResponse.SC_OK);
        response.flushBuffer();
        getTheClient(request, response).setBResponseIsReady(true);
    } else if (command.equals(TheApp.CMD_ZE_QUICKZE)) {

    } else if (command.equals(TheApp.CMD_ZE_RECHNERSTART1)) {
        int i = 0;
    } else if (command.equals(TheApp.CMD_ZE_QUICKZEITERFASSUNG)) {
        if (getTheClient(request, response).getSMsg() == null) {
            getTheClient(request, response).setSMsg("");
        }

        String username = getCookieValue("anmeldename", request);
        String password = getCookieValue("pass", request);

        if (localeCookie != null && localeCookie.length() > 3) {
            localeLogon = new Locale(localeCookie.substring(0, 2), localeCookie.substring(2, 4));
        }

        if (username == null || password == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es wurde kein Benutzername oder Kennwort angegeben. Bitte verwenden Sie http://?????cmd=quickze");

        }
        try {
            theclientDto = getLogonFac().logon(Helper.getFullUsername(username),
                    Helper.getMD5Hash((username + password).toCharArray()), localeLogon, null, null,
                    new Timestamp(System.currentTimeMillis()));
        } catch (EJBExceptionLP ex12) {

            int code = ex12.getCode();
            if (code == EJBExceptionLP.FEHLER_BEI_FINDBYPRIMARYKEY) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' konnte im System nicht gefunden werden");
            } else if (code == EJBExceptionLP.FEHLER_FALSCHES_KENNWORT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Kennwort f\u00FCr Benutzername '" + username + "' ist falsch.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_IST_GESPERRT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' ist gesperrt.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_IST_NICHT_MEHR_GUELTIG) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' ist nicht mehr g\u00FCltig.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_DARF_SICH_BEI_DIESEM_MANDANTEN_NICHT_ANMELDEN) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzername '" + username + "' darf sich bei dem Mandanten nicht anmelden.");
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_KEIN_EINTRAG_IN_BENUTZERMANDANT) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Kein Eintrag in Benutzermandant f\u00FCr Benutzername '" + username + "'.");
            } else if (ex12.getCode() == EJBExceptionLP.FEHLER_MAXIMALE_BENUTZERANZAHL_UEBERSCHRITTEN) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Maximale Benutzeranzahl \u00FCberschritten '" + username + "'.");
                return null;
            } else if (code == EJBExceptionLP.FEHLER_BENUTZER_DARF_SICH_IN_DIESER_SPRACHE_NICHT_ANMELDEN) {
                ArrayList<?> al = ((EJBExceptionLP) ex12.getCause()).getAlInfoForTheClient();
                String zusatz = "";
                if (al.size() > 0 && al.get(0) instanceof Locale) {
                    Locale loc = (Locale) al.get(0);
                    zusatz = "(" + loc.getDisplayLanguage() + "|" + loc.getDisplayCountry() + ")";
                }
                response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                        "Benutzer '" + username + "' darf sich in '" + zusatz + "' nicht anmelden.");
            }

            return null;
        }

        PersonalDto personalDto = getPersonalFac().personalFindByPrimaryKey(theclientDto.getIDPersonal(),
                theclientDto);

        personalDto.setPartnerDto(
                getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto));

        HashMap<String, Object> hmData = new HashMap<String, Object>();

        TextDto textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.sondertaetigkeit",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_sondertaetigkeit", textDto.getCText());
        } else {
            hmData.put("bezeichnung_sondertaetigkeit", "Sondert\u00E4tigkeit");
        }

        // Belegarten holen

        Map<String, String> b = getZeiterfassungsFac().getBebuchbareBelegarten(theclientDto);

        hmData.put("belegarten", b);

        String firstBelegart = (String) b.keySet().iterator().next();
        String belegart = null;
        if (request.getParameter("belegart") == null) {
            belegart = firstBelegart;
        } else {
            belegart = request.getParameter("belegart");
        }

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenerauftrag",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offeneslos",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenesangebot",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.offenesprojekt",
                    theclientDto.getMandant(), theclientDto.getLocUiAsString());
        }
        if (textDto != null) {
            hmData.put("bezeichnung_offenerauftrag", textDto.getCText());
        } else {
            hmData.put("bezeichnung_offenerauftrag", "Offener Beleg");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.taetigkeit",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_taetigkeit", textDto.getCText());
        } else {
            hmData.put("bezeichnung_taetigkeit", "T\u00E4tigkeit");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.kunde",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_kunde", textDto.getCText());
        } else {
            hmData.put("bezeichnung_kunde", "Kunde");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.belegart",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_belegart", textDto.getCText());
        } else {
            hmData.put("bezeichnung_belegart", "Belegart");
        }

        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.bemerkung",
                theclientDto.getMandant(), theclientDto.getLocUiAsString());
        if (textDto != null) {
            hmData.put("bezeichnung_bemerkung", textDto.getCText());
        } else {
            hmData.put("bezeichnung_bemerkung", "Bemerkung");
        }

        hmData.put("locale", Helper.locale2String(localeLogon).trim());
        hmData.put("mandant", mandant);
        hmData.put("person", personalDto.getPartnerDto().formatAnrede());

        // Kunden mit offenen Auftraegen holen
        Session session = FLRSessionFactory.getFactory().openSession();
        String sQuery = "";
        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
            sQuery = "SELECT flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id "
                    + " FROM FLRAuftrag AS auftrag WHERE (auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' OR auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT
                    + "') AND auftrag.mandant_c_nr='" + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id  ORDER BY flrkunde.flrpartner.c_name1nachnamefirmazeile1 ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            sQuery = "SELECT flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id "
                    + " FROM FLRAngebot AS angebot WHERE (angebot.angebotstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "') AND angebot.mandant_c_nr='" + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrkunde.flrpartner.c_name1nachnamefirmazeile1, flrkunde.flrpartner.c_name2vornamefirmazeile2, flrkunde.flrpartner.i_id  ORDER BY flrkunde.flrpartner.c_name1nachnamefirmazeile1 ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            sQuery = "SELECT flrpartner.c_name1nachnamefirmazeile1, flrpartner.c_name2vornamefirmazeile2, flrpartner.i_id "
                    + " FROM FLRProjekt AS projekt WHERE projekt.status_c_nr<>'"
                    + ProjektServiceFac.PROJEKT_STATUS_STORNIERT
                    + "' AND projekt.t_erledigungsdatum IS NULL AND projekt.mandant_c_nr='"
                    + theclientDto.getMandant() + "'"
                    + "  GROUP BY flrpartner.c_name1nachnamefirmazeile1, flrpartner.c_name2vornamefirmazeile2, flrpartner.i_id  ORDER BY flrpartner.c_name1nachnamefirmazeile1 ASC";

        }
        LinkedHashMap<Object, Object> tmKunden = new LinkedHashMap<Object, Object>();
        Integer firstKunde = null;
        if (!belegart.equals(LocaleFac.BELEGART_LOS)) {
            Query kunden = session.createQuery(sQuery);

            List<?> resultList = kunden.list();

            Iterator<?> resultListIterator = resultList.iterator();

            int row = 0;

            while (resultListIterator.hasNext()) {
                Object o[] = (Object[]) resultListIterator.next();
                if (row == 0) {
                    firstKunde = (Integer) o[2];
                }
                if (o[1] == null) {
                    tmKunden.put(o[2], o[0]);

                } else {
                    tmKunden.put(o[2], o[0] + " " + o[1]);

                }
                row++;
            }
            session.close();
        } else {

            sQuery = "SELECT los " + " FROM FLRLosReport AS los WHERE (los.status_c_nr='"
                    + LocaleFac.STATUS_AUSGEGEBEN + "' OR los.status_c_nr='" + LocaleFac.STATUS_IN_PRODUKTION
                    + "' OR los.status_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT + "')  AND los.mandant_c_nr='"
                    + theclientDto.getMandant()
                    + "' AND ( los.flrauftrag IS NOT NULL OR los.flrkunde IS NOT NULL) ";

            Query kunden = session.createQuery(sQuery);

            List<?> resultList = kunden.list();

            Iterator<?> resultListIterator = resultList.iterator();

            int row = 0;

            while (resultListIterator.hasNext()) {
                FLRLosReport los = (FLRLosReport) resultListIterator.next();

                Integer partnerIId = null;
                String kundenname = "";
                if (los.getFlrauftrag() != null) {
                    partnerIId = los.getFlrauftrag().getFlrkunde().getFlrpartner().getI_id();

                    kundenname += los.getFlrauftrag().getFlrkunde().getFlrpartner()
                            .getC_name1nachnamefirmazeile1();

                    if (los.getFlrauftrag().getFlrkunde().getFlrpartner()
                            .getC_name2vornamefirmazeile2() != null) {
                        kundenname += " " + los.getFlrauftrag().getFlrkunde().getFlrpartner()
                                .getC_name2vornamefirmazeile2();
                    }

                } else {
                    partnerIId = los.getFlrkunde().getFlrpartner().getI_id();
                    kundenname += los.getFlrkunde().getFlrpartner().getC_name1nachnamefirmazeile1();

                    if (los.getFlrkunde().getFlrpartner().getC_name2vornamefirmazeile2() != null) {
                        kundenname += " " + los.getFlrkunde().getFlrpartner().getC_name2vornamefirmazeile2();
                    }
                }

                if (row == 0) {
                    firstKunde = partnerIId;
                }
                if (!tmKunden.containsKey(partnerIId)) {

                    tmKunden.put(partnerIId, kundenname);
                }

                row++;
            }

            tmKunden = (LinkedHashMap) Helper.sortByValue(tmKunden);

            // leeren Kunden einfuegen
            tmKunden.put("", "--KEIN--");

            session.close();
        }
        hmData.put("kunden", tmKunden);

        // Sondertaetigkeiten holen

        Map<Integer, String> m = getZeiterfassungsFac()
                .getAllSprSondertaetigkeitenNurBDEBuchbar(theclientDto.getLocUiAsString());

        hmData.put("taetigkeiten", m);

        // Kunden holen

        Integer kunde = null;
        if (request.getParameter("kunde") == null) {
            kunde = firstKunde;
        } else {
            if (!request.getParameter("kunde").equals("")) {
                if (!request.getParameter("kunde").equals(" ")) {
                    if (!request.getParameter("kunde").trim().equals("null")) {
                        kunde = new Integer(request.getParameter("kunde").trim());
                    }
                }

            }
        }

        hmData.put("selectedbelegart", belegart);

        session = FLRSessionFactory.getFactory().openSession();

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

            sQuery = "SELECT auftrag.i_id, auftrag.c_nr, auftrag.c_bez, auftrag.t_liefertermin, auftrag.flrkunde.flrpartner.i_id "
                    + " FROM FLRAuftrag AS auftrag WHERE (auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' OR auftrag.auftragstatus_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT
                    + "') AND auftrag.flrkunde.flrpartner.i_id=" + kunde
                    + " AND auftrag.b_versteckt=0 ORDER BY auftrag.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
            sQuery = "SELECT los.i_id, los.c_nr, los.c_projekt, los.t_produktionsende, coalesce(auftragpartner.i_id,kundepartner.i_id) "
                    + " FROM FLRLosReport AS los LEFT OUTER JOIN los.flrauftrag.flrkunde.flrpartner as auftragpartner LEFT OUTER JOIN los.flrkunde.flrpartner as kundepartner  WHERE (los.status_c_nr='"
                    + LocaleFac.STATUS_AUSGEGEBEN + "' OR los.status_c_nr='" + LocaleFac.STATUS_IN_PRODUKTION
                    + "' OR los.status_c_nr='" + LocaleFac.STATUS_TEILERLEDIGT + "') ";
            if (kunde != null) {
                sQuery += " AND ( auftragpartner.i_id=" + kunde + " OR kundepartner.i_id=" + kunde + ")";

            } else {
                sQuery += " AND ( auftragpartner.i_id IS NULL AND kundepartner.i_id IS NULL)";
            }
            sQuery += " ORDER BY los.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
            sQuery = "SELECT angebot.i_id, angebot.c_nr, angebot.c_bez, angebot.t_realisierungstermin, angebot.flrkunde.flrpartner.i_id "
                    + " FROM FLRAngebot AS angebot WHERE angebot.angebotstatus_c_nr='" + LocaleFac.STATUS_OFFEN
                    + "' AND angebot.flrkunde.flrpartner.i_id=" + kunde + "  ORDER BY angebot.c_nr ASC";
        } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            sQuery = "SELECT projekt.i_id, projekt.c_nr, projekt.c_titel, projekt.t_zielwunschdatum, projekt.partner_i_id "
                    + " FROM FLRProjekt AS projekt WHERE projekt.status_c_nr<>'"
                    + ProjektServiceFac.PROJEKT_STATUS_STORNIERT
                    + "' AND projekt.t_erledigungsdatum IS NULL AND projekt.partner_i_id=" + kunde
                    + " ORDER BY projekt.c_nr ASC";
        }
        Query auftraege = session.createQuery(sQuery);

        List<?> resultList = auftraege.list();

        Iterator resultListIterator = resultList.iterator();
        LinkedHashMap<Object, Object> tmAuftraege = new LinkedHashMap<Object, Object>();
        Object partnerIId = null;
        String selectedAuftragId = null;
        while (resultListIterator.hasNext()) {
            Object o[] = (Object[]) resultListIterator.next();
            partnerIId = (Integer) o[4];

            if (o[2] == null) {
                tmAuftraege.put(o[0], o[1]);

            } else {
                tmAuftraege.put(o[0], o[1] + " " + o[2]);

            }
            if (selectedAuftragId == null) {
                selectedAuftragId = o[0].toString();
            }
        }

        session.close();
        hmData.put("auftraege", tmAuftraege);

        hmData.put("selectedkunde", partnerIId);

        if (request.getParameter("auftrag") != null && request.getParameter("auftrag").length() > 0) {
            selectedAuftragId = request.getParameter("auftrag");

        }

        // Artikel zu Auftrag holen
        session = FLRSessionFactory.getFactory().openSession();

        if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRAuftragposition AS a WHERE a.flrauftrag.i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRLossollarbeitsplan AS a WHERE a.los_i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {

            sQuery = "SELECT a.i_id, a.flrartikel.i_id FROM FLRAngebotposition AS a WHERE a.flrangebot.i_id="
                    + selectedAuftragId + " AND a.flrartikel.artikelart_c_nr='"
                    + ArtikelFac.ARTIKELART_ARBEITSZEIT + "'";
        }
        LinkedHashMap<Object, Object> tmArtikel = new LinkedHashMap<Object, Object>();
        if (!belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
            Query artikelListe = session.createQuery(sQuery);

            resultList = artikelListe.list();

            resultListIterator = resultList.iterator();

            if (resultList.size() > 0) {
                tmArtikel.put(-1, " - - - - - - Beleg - - - - - -");
            }

            while (resultListIterator.hasNext()) {
                Object[] zeile = (Object[]) resultListIterator.next();

                Integer artikelIId = (Integer) zeile[1];

                String sollIst = "";

                if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {

                    BigDecimal bdSoll = getAuftragpositionFac()
                            .auftragpositionFindByPrimaryKey((Integer) zeile[0]).getNMenge();
                    sollIst = "; Soll: " + Helper.formatZahl(bdSoll, 2, theclientDto.getLocUi());

                    Double dIst;
                    try {

                        boolean bZuvieleZeitbuchungen = getZeiterfassungsFac()
                                .sindZuvieleZeitdatenEinesBelegesVorhanden(belegart,
                                        new Integer(selectedAuftragId), theclientDto);
                        if (bZuvieleZeitbuchungen == false) {

                            dIst = getZeiterfassungsFac().getSummeZeitenEinesBeleges(belegart,
                                    new Integer(selectedAuftragId), (Integer) zeile[0], null, null, null,
                                    theclientDto);
                            sollIst += " Ist: " + Helper.formatZahl(dIst, 2, theclientDto.getLocUi());
                        }
                    } catch (Exception e) {
                        sollIst += " Ist: ERR";
                    }

                }

                String artikel = getArtikelFac().artikelFindByPrimaryKey(artikelIId, theclientDto)
                        .formatArtikelbezeichnung() + sollIst;
                if (!tmArtikel.containsKey(artikelIId)) {
                    tmArtikel.put(artikelIId, artikel);

                }
            }
            session.close();
        }
        // Artikel des Auftrags + Artikel aus Personalverfuegbarkeit holen

        if (selectedAuftragId != null) {

            PersonalverfuegbarkeitDto[] personalverfuegbarkeitDtos = getPersonalFac()
                    .personalverfuegbarkeitFindByPersonalIId(personalDto.getIId());
            if (personalverfuegbarkeitDtos.length > 0) {
                tmArtikel.put(-2, " - - - Verf\u00FCgbarkeit - - - ");
            }
            for (int i = 0; i < personalverfuegbarkeitDtos.length; i++) {
                PersonalverfuegbarkeitDto v = personalverfuegbarkeitDtos[i];
                String artikel = getArtikelFac().artikelFindByPrimaryKey(v.getArtikelIId(), theclientDto)
                        .formatArtikelbezeichnung();
                tmArtikel.put(v.getArtikelIId(), artikel);
            }

            hmData.put("selectedauftrag", selectedAuftragId);

        }

        ParametermandantDto parameterDtoDefaultarbeitszeit = getParameterFac().getMandantparameter(mandant,
                ParameterFac.KATEGORIE_ALLGEMEIN, ParameterFac.PARAMETER_DEFAULT_ARBEITSZEITARTIKEL);

        if (parameterDtoDefaultarbeitszeit != null && parameterDtoDefaultarbeitszeit.getCWert() != null
                && !parameterDtoDefaultarbeitszeit.getCWert().trim().equals("")) {

            ArtikelDto artikelDtoDefaultArbeiztszeit = getArtikelFac()
                    .artikelFindByCNr(parameterDtoDefaultarbeitszeit.getCWert(), theclientDto);
            tmArtikel.put(-3, " - - - Default-Arbeitszeitartikel - - -");
            tmArtikel.put(artikelDtoDefaultArbeiztszeit.getIId(),
                    artikelDtoDefaultArbeiztszeit.formatArtikelbezeichnung());
        }

        hmData.put("artikel", tmArtikel);

        // Zeitbuchen
        String bucheauftrag = request.getParameter("bucheauftrag");
        String buchesondertaetigkeit = request.getParameter("buchesondertaetigkeit");

        // Zeit buchen
        ZeitdatenDto zeitdatenDto = new ZeitdatenDto();
        zeitdatenDto.setPersonalIId(personalDto.getIId());
        Timestamp tZeit = new Timestamp(System.currentTimeMillis());
        zeitdatenDto.setCWowurdegebucht("Quick-ZE " + request.getRemoteHost());
        String meldung = "";

        zeitdatenDto.setTZeit(tZeit);
        String bemerkung = request.getParameter("bemerkung");
        zeitdatenDto.setCBemerkungZuBelegart(bemerkung);

        if (bucheauftrag != null && bucheauftrag.length() > 0) {

            if (request.getParameter("artikel") != null) {

                Integer artikelId = new Integer(request.getParameter("artikel"));
                if (artikelId > 0) {

                    Integer auftragIId = new Integer(selectedAuftragId.trim());

                    String s = "Auf ";

                    if (kunde != null) {
                        PartnerDto partnerDto = getPartnerFac().partnerFindByPrimaryKey(kunde, theclientDto);
                        s += partnerDto.formatFixName1Name2() + ", ";
                    }

                    if (belegart.equals(LocaleFac.BELEGART_AUFTRAG)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("quickze.auftrag",
                                theclientDto.getMandant(), theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Auftrag ";
                        }

                        com.lp.server.auftrag.service.AuftragDto auftragDto = getAuftragFac()
                                .auftragFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCBezProjektbezeichnung() != null) {
                            s += " " + auftragDto.getCBezProjektbezeichnung();
                        }

                        com.lp.server.auftrag.service.AuftragpositionDto[] auftragpositionDtos = getAuftragpositionFac()
                                .auftragpositionFindByAuftrag(auftragIId);
                        if (auftragpositionDtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(auftragpositionDtos[0].getIId());
                        }
                    } else if (belegart.equals(LocaleFac.BELEGART_ANGEBOT)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc("angb.angebot",
                                theclientDto.getMandant(), theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Angebot ";
                        }

                        com.lp.server.angebot.service.AngebotDto auftragDto = getAngebotFac()
                                .angebotFindByPrimaryKey(auftragIId, theclientDto);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCBez() != null) {
                            s += " " + auftragDto.getCBez();
                        }

                    } else if (belegart.equals(LocaleFac.BELEGART_PROJEKT)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc(
                                "lp.projekt.modulname", theclientDto.getMandant(),
                                theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Projekt ";
                        }

                        com.lp.server.projekt.service.ProjektDto auftragDto = getProjektFac()
                                .projektFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCTitel() != null) {
                            s += " " + auftragDto.getCTitel();
                        }

                    } else if (belegart.equals(LocaleFac.BELEGART_LOS)) {
                        textDto = getSystemMultilanguageFac().textFindByPrimaryKeyOhneExc(
                                "fert.tab.unten.los.title", theclientDto.getMandant(),
                                theclientDto.getLocUiAsString());

                        if (textDto != null) {
                            s += textDto.getCText() + " ";
                        } else {
                            s += "Los ";
                        }

                        LosDto auftragDto = getFertigungFac().losFindByPrimaryKey(auftragIId);
                        s += auftragDto.getCNr();
                        if (auftragDto.getCProjekt() != null) {
                            s += " " + auftragDto.getCProjekt();
                        }

                        LossollarbeitsplanDto[] dtos = getFertigungFac()
                                .lossollarbeitsplanFindByLosIIdArtikelIIdTaetigkeit(auftragIId, artikelId);
                        if (dtos.length > 0) {
                            zeitdatenDto.setIBelegartpositionid(dtos[0].getIId());
                        }

                    }
                    zeitdatenDto.setPersonalIId(personalDto.getIId());
                    zeitdatenDto.setCBelegartnr(belegart);
                    zeitdatenDto.setIBelegartid(auftragIId);
                    zeitdatenDto.setArtikelIId(artikelId);

                    ArtikelDto artikelDto = getArtikelFac().artikelFindByPrimaryKey(artikelId, theclientDto);

                    meldung += s + ", " + artikelDto.formatArtikelbezeichnung();

                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                    meldung += " um " + Helper.formatTime(tZeit, localeLogon) + " gebucht.";
                    getTheClient(request, response).setSMsg(meldung);
                }
            } else {

                getTheClient(request, response).setSMsg("Keine Auftragsposition ausgew\u00E4hlt");
            }
        } else if (buchesondertaetigkeit != null && buchesondertaetigkeit.length() > 0) {
            String zusatz = request.getParameter("zusatz");

            if (zusatz != null && zusatz.length() > 0) {
                // Zeit ist immer jetzt
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(zeitdatenDto.getTZeit().getTime());
                ZeitdatenDto[] letzeBuchungen = getZeiterfassungsFac()
                        .zeitdatenFindZeitdatenEinesTagesUndEinerPersonOnheBelegzeiten(
                                zeitdatenDto.getPersonalIId(), Helper.cutTimestamp(zeitdatenDto.getTZeit()),
                                zeitdatenDto.getTZeit());
                Integer taetigkeitIId_Kommt = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_KOMMT, theclientDto).getIId();
                Integer taetigkeitIId_Unter = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_UNTER, theclientDto).getIId();
                Integer taetigkeitIId_Geht = getZeiterfassungsFac()
                        .taetigkeitFindByCNr(ZeiterfassungFac.TAETIGKEIT_GEHT, theclientDto).getIId();
                if (zusatz.equals("spezialkommt")) {

                    if (letzeBuchungen.length == 0) {
                        // Zuerst Kommt und dann UNTER
                        ZeitdatenDto dtoKommt = new ZeitdatenDto();
                        dtoKommt.setTaetigkeitIId(taetigkeitIId_Kommt);
                        dtoKommt.setPersonalIId(zeitdatenDto.getPersonalIId());
                        dtoKommt.setCWowurdegebucht("Spezial-Kommt");
                        // Zeit 100 MS vorher
                        dtoKommt.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                        getZeiterfassungsFac().createZeitdaten(dtoKommt, false, false, false, theclientDto);
                        // Taetigkeit GEHT Buchen
                        ZeitdatenDto dtoUnter = new ZeitdatenDto();
                        dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                        dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                        dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                        // Zeit 100 MS nachher
                        dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 96));
                        getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);
                    } else if (letzeBuchungen.length == 1) {
                        Integer letztetaetigkeit = letzeBuchungen[0].getTaetigkeitIId();
                        // Wenn nur Kommt, dann Unter buchen
                        if (taetigkeitIId_Kommt.equals(letztetaetigkeit)) {
                            // Taetigkeit UNTER Buchen
                            ZeitdatenDto dtoUnter = new ZeitdatenDto();
                            dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                            dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                            dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                            dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                            getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);

                        }
                    } else if (letzeBuchungen.length > 1) {
                        Integer letztetaetigkeit = letzeBuchungen[letzeBuchungen.length - 1].getTaetigkeitIId();
                        if (taetigkeitIId_Kommt.equals(letztetaetigkeit)) {
                            // Taetigkeit UNTER Buchen
                            ZeitdatenDto dtoUnter = new ZeitdatenDto();
                            dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                            dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                            dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                            dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                            getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false, theclientDto);

                        } else {

                            // Wenn letzte Taetigkeit ein Geht ist wird
                            // Kommt
                            // und Unter gebucht
                            if (!taetigkeitIId_Geht.equals(letztetaetigkeit)) {

                                int iSondertaetigkeitenHintereinander = 1;
                                for (int i = letzeBuchungen.length - 2; i >= 0; i--) {
                                    ZeitdatenDto dto = letzeBuchungen[i];
                                    if (letztetaetigkeit.equals(dto.getTaetigkeitIId())) {
                                        iSondertaetigkeitenHintereinander++;
                                    } else {
                                        break;
                                    }
                                    letztetaetigkeit = dto.getTaetigkeitIId();
                                }

                                if (iSondertaetigkeitenHintereinander % 2 == 0) {
                                    // Taetigkeit UNTER Buchen
                                    ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                    dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                                    dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                    dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                    dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                    getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                            theclientDto);

                                    /**
                                     * @todo 100ms vorher Projekt-ENDE
                                     *       buchen
                                     */
                                }
                            } else {
                                // Taetigkeit KOMMT Buchen
                                ZeitdatenDto dtoKommt = new ZeitdatenDto();
                                dtoKommt.setTaetigkeitIId(taetigkeitIId_Kommt);
                                dtoKommt.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoKommt.setCWowurdegebucht("Spezial-Kommt");
                                dtoKommt.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoKommt, false, false, false,
                                        theclientDto);
                                // Taetigkeit UNTER Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Unter);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Kommt");
                                // Zeit 100 MS nachher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() + 96));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            }
                        }
                    }

                } else if (zusatz.equals("spezialgeht")) {
                    if (letzeBuchungen.length > 1) {

                        Integer letztetaetigkeit = letzeBuchungen[letzeBuchungen.length - 1].getTaetigkeitIId();
                        // Wenn letzte Taetigkeit kein geht ist, sonst wird
                        // geht verschmissen
                        if (!taetigkeitIId_Geht.equals(letztetaetigkeit)) {

                            int iSondertaetigkeitenHintereinander = 1;
                            for (int i = letzeBuchungen.length - 2; i >= 0; i--) {
                                ZeitdatenDto dto = letzeBuchungen[i];
                                if (letztetaetigkeit.equals(dto.getTaetigkeitIId())) {
                                    iSondertaetigkeitenHintereinander++;
                                } else {
                                    break;
                                }
                                letztetaetigkeit = dto.getTaetigkeitIId();
                            }

                            if (iSondertaetigkeitenHintereinander % 2 == 1) {
                                // Sondertaetigkeit Ende Buchen
                                ZeitdatenDto dtoSonderEnde = new ZeitdatenDto();
                                dtoSonderEnde.setTaetigkeitIId(letztetaetigkeit);
                                dtoSonderEnde.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoSonderEnde.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoSonderEnde.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime() - 96));
                                getZeiterfassungsFac().createZeitdaten(dtoSonderEnde, false, false, false,
                                        theclientDto);
                                // Taetigkeit GEHT Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Geht);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            } else {
                                // Taetigkeit GEHT Buchen
                                ZeitdatenDto dtoUnter = new ZeitdatenDto();
                                dtoUnter.setTaetigkeitIId(taetigkeitIId_Geht);
                                dtoUnter.setPersonalIId(zeitdatenDto.getPersonalIId());
                                dtoUnter.setCWowurdegebucht("Spezial-Geht");
                                // Zeit 100 MS vorher
                                dtoUnter.setTZeit(new Timestamp(zeitdatenDto.getTZeit().getTime()));
                                getZeiterfassungsFac().createZeitdaten(dtoUnter, false, false, false,
                                        theclientDto);

                            }
                        }

                    }

                } else {
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "zusatz '" + zusatz + "' unbekannt");
                }
                setSJSPNext("mecs.jsp");
                return getSJSPNext();
            } else {

                if (request.getParameter("taetigkeit") != null) {

                    Integer taetigkeitId = new Integer(request.getParameter("taetigkeit"));
                    zeitdatenDto.setTaetigkeitIId(taetigkeitId);

                    TaetigkeitDto dto = getZeiterfassungsFac().taetigkeitFindByPrimaryKey(taetigkeitId,
                            theclientDto);
                    meldung += dto.getBezeichnung();

                    getZeiterfassungsFac().createZeitdaten(zeitdatenDto, true, true, false, theclientDto);
                    meldung += " um " + Helper.formatTime(tZeit, localeLogon) + " gebucht.";
                    getTheClient(request, response).setSMsg(meldung);
                } else {
                    getTheClient(request, response).setSMsg("Keine T\u00E4tigkeit ausgew\u00E4hlt");
                }
            }
        }
        getTheClient(request, response).setData(hmData);
        // AD+CK logout wegen usercount
        synchronized (mutex) { // PJ 15986
            getLogonFac().logout(theclientDto);
        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_TERMINAL)) {
        // Personalstamm holen

        String master = request.getParameter("master");

        try {
            ZutrittscontrollerDto zutrittscontrollerDto = getZutrittscontrollerFac()
                    .zutrittscontrollerFindByCNr(master);
            ZutrittsobjektDto[] zutrittsobjektDtos = getZutrittscontrollerFac()
                    .zutrittsobjektFindByZutrittscontrollerIId(zutrittscontrollerDto.getIId());

            StringBuffer objekte = new StringBuffer();

            for (int i = 0; i < zutrittsobjektDtos.length; i++) {
                objekte.append(Helper.fitString2Length(zutrittsobjektDtos[i].getCNr(), 6, ' ')); // terminal-
                // id
                objekte.append(Helper.fitString2Length(zutrittsobjektDtos[i].getCAdresse(), 100, ' ')); // adresse
                objekte.append("\r\n");
            }
            myLogger.info(command + ":" + new String(objekte));

            getTheClient(request, response).setSMsg(new String(objekte));
        }

        catch (RemoteException ex5) {
            if (ex5.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP lpex = (EJBExceptionLP) ex5.getCause();
                if (lpex.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                    myLogger.error("Zutrittscontroller '" + master + "' nicht angelegt", ex5);
                } else {
                    myLogger.error(ex5.getMessage(), ex5);
                }
            }

        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_RELAIS)) {
        // Personalstamm holen

        String termid = request.getParameter("termid");
        if (termid == null || termid.length() == 0) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es ist der Parameter termid='LanPortName' erforderlich");
            return null;
        }
        try {
            ZutrittsobjektDto zutrittsobjektDto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);

            StringBuffer objekte = new StringBuffer();

            objekte.append("10"); // readerid
            objekte.append("0"); // port
            objekte.append(zutrittsobjektDto.getCRelais()); // relais
            String oeffnungszeit = zutrittsobjektDto.getFOeffnungszeit().toString();
            oeffnungszeit = oeffnungszeit.replaceAll(",", ".");
            objekte.append(Helper.fitString2LengthAlignRight(oeffnungszeit, 4, ' ')); // oeffnungszeit
            objekte.append(zutrittsobjektDto.getZutrittsleserCNr().trim()); // readerid
            objekte.append("\r\n");
            myLogger.info(command + ":" + new String(objekte));

            getTheClient(request, response).setSMsg(new String(objekte));
        } catch (EJBExceptionLP ex4) {

            if (ex4.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");
                return null;
            } else {
                ex4.printStackTrace();
                myLogger.error(ex4.getMessage(), ex4);
            }

        }
    } else if (command.equals(TheApp.CMD_ZU_MECS_ZUTRITT)) {
        String termid = request.getParameter("termid");
        try {
            ZutrittsobjektDto dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);

            String s = getZutrittscontrollerFac().getZutrittsdatenFuerEinObjektFuerMecs(dto.getIId(),
                    theclientDto);
            myLogger.info(command + ":" + new String(s));

            getTheClient(request, response).setSMsg(new String(s));
        } catch (EJBExceptionLP ex4) {
            if (ex4.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                myLogger.error("Zutrittsobjekt '" + termid + "' nicht angelegt", ex4);
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");

            } else {
                myLogger.error(ex4.getMessage(), ex4);
                response.sendError(HttpServletResponse.SC_NOT_FOUND, ex4.getMessage());
            }

        }

    } else if (command.equals(TheApp.CMD_ZU_MECS_AUSWEISE_ZUTRITT)) {
        PersonalzutrittsklasseDto[] dtos = getZutrittscontrollerFac().personalzutrittsklassenFindByTGueltigab(
                new Timestamp(System.currentTimeMillis()), theclientDto);

        ArrayList<StringBuffer> alDaten = new ArrayList<StringBuffer>();

        for (int i = 0; i < dtos.length; i++) {
            StringBuffer sb = new StringBuffer();
            sb.append("10");

            // Hole personalDto
            PersonalDto personalDto = getPersonalFac().personalFindByPrimaryKeySmall(dtos[i].getPersonalIId());

            sb.append(Helper.fitString2Length(personalDto.getCAusweis(), 20, ' '));
            sb.append(Helper.fitString2Length(personalDto.getCPersonalnr().toString(), 10, ' '));
            sb.append(Helper.fitString2Length("", 24, ' '));

            // Hole Zutrittsklasse
            ZutrittsklasseDto zutrittsklasseDto = getZutrittscontrollerFac()
                    .zutrittsklasseFindByPrimaryKey(dtos[i].getZutrittsklasseIId());
            sb.append(Helper.fitString2Length(zutrittsklasseDto.getCNr(), 3, ' '));
            alDaten.add(sb);
        }

        // Besucherausweise
        String[] ausweise = getZutrittscontrollerFac()
                .zutrittonlinecheckAusweiseFindByTGueltigab(new Timestamp(System.currentTimeMillis()));

        for (int i = 0; i < ausweise.length; i++) {
            StringBuffer sb = new StringBuffer();
            sb.append("10");
            sb.append(Helper.fitString2Length(ausweise[i], 20, ' '));
            sb.append(Helper.fitString2Length("", 10, ' '));
            sb.append(Helper.fitString2Length("", 24, ' '));
            sb.append(Helper.fitString2Length(ZutrittscontrollerFac.ZUTRITTSKLASSE_ONLINECHECK, 3, ' '));
            alDaten.add(sb);
        }

        // sortieren
        String datenGesamt = "";
        for (int i = alDaten.size() - 1; i > 0; --i) {
            for (int j = 0; j < i; ++j) {
                if ((new String(alDaten.get(j))).compareTo(new String(alDaten.get(j + 1))) > 0) {
                    StringBuffer lagerbewegungDtoTemp = alDaten.get(j);
                    alDaten.set(j, alDaten.get(j + 1));
                    alDaten.set(j + 1, lagerbewegungDtoTemp);
                }
            }
        }

        for (int i = 0; i < alDaten.size(); i++) {

            StringBuffer sbTemp = alDaten.get(i);
            sbTemp.append("\r\n");
            datenGesamt += new String(sbTemp);
        }

        myLogger.info(command + ":" + datenGesamt);
        getTheClient(request, response).setSMsg(datenGesamt);
    } else if (command.startsWith(TheApp.CMD_ZU_MECS_ZUTRITT_ONLINE_CHECK)) {
        String termid = request.getParameter("termid");

        String card = request.getParameter("card");
        String pin = request.getParameter("pin");

        try {
            ZutrittsobjektDto dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);
            boolean b = getZutrittscontrollerFac().onlineCheck(card, pin,
                    new Timestamp(System.currentTimeMillis()), dto.getIId());
            if (b == true) {
                myLogger.info(command + ": ZUTRITT ERLAUBT");
                getTheClient(request, response).setSMsg("A");

            } else {
                myLogger.info(command + ": ZUTRITT VERWEIGERT");
                getTheClient(request, response).setSMsg("Z");
            }
        } catch (EJBExceptionLP ex4) {
            if (ex4.getCause() instanceof EJBExceptionLP) {
                EJBExceptionLP lpex = (EJBExceptionLP) ex4.getCause();
                if (lpex.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {

                    response.sendError(HttpServletResponse.SC_NOT_FOUND,
                            "Zutrittsobjekt '" + termid + "' nicht angelegt");
                    myLogger.error("Zutrittsobjekt '" + termid + "' nicht angelegt", ex4);
                } else {
                    myLogger.error(ex4.getMessage(), ex4);
                    response.sendError(HttpServletResponse.SC_NOT_FOUND, ex4.getMessage());
                }
            }
        }

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_ZUTRITT_EVENTS)) {
        String termid = request.getParameter("termid");

        if (termid == null || termid.length() == 0) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Es ist der Parameter termid='LanPortName' erforderlich");
            return null;
        }

        ZutrittsobjektDto dto = null;
        try {
            dto = getZutrittscontrollerFac().zutrittsobjektFindByCNr(termid);
        } catch (EJBExceptionLP e) {
            if (e.getCode() == EJBExceptionLP.FEHLER_BEI_FIND) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        "Zutrittsobjekt '" + termid + "' nicht angelegt");
                return null;
            } else {
                e.printStackTrace();
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Unerwarteter Fehler aufgetreten.");
                return null;
            }
        }

        String s = getZutrittscontrollerFac().getZutrittsEventsFuerMecs(dto.getIId(), theclientDto);
        myLogger.info(command + ":" + s);
        getTheClient(request, response).setSMsg(s);

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_MAXTRANSNR)) {
        getTheClient(request, response).setSMsg("999");
    } else if (command.startsWith(TheApp.CMD_ZU_MECS_LOG)) {

        String record = command.substring(17);
        ZutrittslogDto dto = new ZutrittslogDto();

        Calendar c = Calendar.getInstance();
        String zeitpunkt = record.substring(5, 19);
        int iJahr = new Integer(zeitpunkt.substring(0, 4));
        int iMonat = new Integer(zeitpunkt.substring(4, 6)) - 1;
        int iTag = new Integer(zeitpunkt.substring(6, 8));
        int iStunden = new Integer(zeitpunkt.substring(8, 10));
        int iMinuten = new Integer(zeitpunkt.substring(10, 12));
        int iSekunden = new Integer(zeitpunkt.substring(12, 14));

        c.set(iJahr, iMonat, iTag, iStunden, iMinuten, iSekunden);
        dto.setTZeitpunkt(new Timestamp(c.getTimeInMillis()));
        String personalnr = record.substring(19, 24);
        String erlaubt = record.substring(24, 27);
        String objekt = record.substring(46, 67).trim();
        String ausweis = record.substring(106, 135).trim();
        String event = record.substring(126, 137).trim();

        PersonalDto personalDto = getPersonalFac().personalFindByCAusweis(ausweis);

        if (personalDto != null || event.equals("PINONLINE") || personalnr.equals("?????")
                || personalnr.equals("     ")) {
            if (personalDto != null) {
                dto.setCPerson(
                        getPartnerFac().partnerFindByPrimaryKey(personalDto.getPartnerIId(), theclientDto)
                                .formatFixAnredeTitelName2Name1());
                dto.setMandantCNr(personalDto.getMandantCNr());
            } else if (personalnr.equals("     ")) {
                dto.setCPerson("Besucher");
                dto.setMandantCNr(mandant);
            } else if (event != null && event.equals("PINONLINE")) {
                dto.setCPerson("Tempor\u00E4rer Pin-Code");
                dto.setMandantCNr(mandant);
            } else {
                dto.setCPerson("Unbekannt");
                dto.setMandantCNr(mandant);
            }

            if (erlaubt.equals("ZZ1")) {
                dto.setBErlaubt(Helper.boolean2Short(true));
            } else {
                dto.setBErlaubt(Helper.boolean2Short(false));
            }

            dto.setCAusweis(ausweis);
            dto.setCZutrittscontroller(null);

            try {
                ZutrittsobjektDto zutrittsobjektDto = getZutrittscontrollerFac()
                        .zutrittsobjektFindByCNr(objekt);

                dto.setCZutrittsobjekt(
                        zutrittsobjektDto.getBezeichnung() + "-" + zutrittsobjektDto.getCAdresse());
                dto.setCZutrittscontroller(getZutrittscontrollerFac()
                        .zutrittscontrollerFindByPrimaryKey(zutrittsobjektDto.getZutrittscontrollerIId())
                        .getCNr());
                dto.setMandantCNrObjekt(zutrittsobjektDto.getMandantCNr());
            } catch (RemoteException ex6) {
                dto.setCZutrittsobjekt("Zutrittsobjekt unbekannt");
            }

            getZutrittscontrollerFac().createZutrittslog(dto);
        }

        myLogger.info(command);

    } else if (command.startsWith(TheApp.CMD_ZU_MECS_TEMPLATES)) {

        String sAendern = request.getParameter("changedsince");

        PersonalfingerDto[] personalfingerDtos = null;
        if (sAendern == null) {
            personalfingerDtos = getZutrittscontrollerFac().personalfingerFindAll();

        } else {

            Calendar c = Calendar.getInstance();
            int iJahr = new Integer(sAendern.substring(0, 4));
            int iMonat = new Integer(sAendern.substring(4, 6)) - 1;
            int iTag = new Integer(sAendern.substring(6, 8));
            int iStunden = new Integer(sAendern.substring(8, 10));
            int iMinuten = new Integer(sAendern.substring(10, 12));

            c.set(iJahr, iMonat, iTag, iStunden, iMinuten, 0);
            c.set(Calendar.MILLISECOND, 0);

            personalfingerDtos = getZutrittscontrollerFac()
                    .personalfingerFindByTAendern(new java.sql.Timestamp(c.getTimeInMillis()), theclientDto);
        }
        StringBuffer sb = new StringBuffer();
        // Zuerts alle loeschen
        sb.append(Helper.fitString2LengthAlignRight("0", 5, ' '));
        sb.append(Helper.fitString2LengthAlignRight("0", 2, ' '));
        sb.append(Helper.fitString2Length("X", 512, 'X'));
        StringBuffer zeit = new StringBuffer();
        Calendar cAendern = Calendar.getInstance();
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.YEAR) + "", 4, '0'));
        zeit.append(Helper.fitString2Length((cAendern.get(Calendar.MONTH) + 1) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.DAY_OF_MONTH) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.HOUR_OF_DAY) + "", 2, '0'));
        zeit.append(Helper.fitString2Length(cAendern.get(Calendar.MINUTE) + "", 2, '0'));

        sb.append(zeit);
        sb.append("\r\n");

        for (int i = 0; i < personalfingerDtos.length; i++) {
            PersonalfingerDto personalfingerDto = personalfingerDtos[i];
            sb.append(Helper.fitString2LengthAlignRight(personalfingerDto.getIId() + "", 5, ' '));
            sb.append(Helper.fitString2LengthAlignRight("1", 2, ' '));
            String templateBase64 = new String(
                    org.apache.commons.codec.binary.Base64.encodeBase64(personalfingerDto.getOTemplate1()));
            sb.append(Helper.fitString2Length(templateBase64, 512, ' '));

            cAendern = Calendar.getInstance();
            cAendern.setTimeInMillis(personalfingerDto.getTAendern().getTime());
            zeit = new StringBuffer();
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.YEAR) + "", 4, '0'));
            zeit.append(Helper.fitString2Length((cAendern.get(Calendar.MONTH) + 1) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.DAY_OF_MONTH) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.HOUR_OF_DAY) + "", 2, '0'));
            zeit.append(Helper.fitString2Length(cAendern.get(Calendar.MINUTE) + "", 2, '0'));

            sb.append(zeit);
            sb.append("\r\n");

            if (personalfingerDto.getOTemplate2() != null) {
                sb.append(Helper.fitString2LengthAlignRight(personalfingerDto.getIId() + "", 5, ' '));
                sb.append(Helper.fitString2LengthAlignRight("2", 2, ' '));
                templateBase64 = new String(
                        org.apache.commons.codec.binary.Base64.encodeBase64(personalfingerDto.getOTemplate2()));
                sb.append(Helper.fitString2Length(templateBase64, 512, ' '));
                sb.append(zeit);
                if (i == personalfingerDtos.length - 1) {
                    // sb.append("\r");
                } else {
                    sb.append("\r\n");
                }
            }

        }
        getTheClient(request, response).setSMsg(new String(sb));

    }

    return getSJSPNext();
}

From source file:org.openbravo.erpCommon.ad_forms.AcctServer.java

public BigDecimal convertAmount(BigDecimal _amount, boolean isReceipt, String dateAcct, String table_ID,
        String record_ID, String currencyIDFrom, String currencyIDTo, DocLine line, AcctSchema as, Fact fact,
        String Fact_Acct_Group_ID, String seqNo, ConnectionProvider conn, boolean bookDifferences)
        throws ServletException {
    BigDecimal amtDiff = BigDecimal.ZERO;
    if (_amount == null || _amount.compareTo(BigDecimal.ZERO) == 0) {
        return _amount;
    }//  ww w .j  a va2 s  .c o m
    String conversionDate = dateAcct;
    String strDateFormat = OBPropertiesProvider.getInstance().getOpenbravoProperties()
            .getProperty("dateFormat.java");
    final SimpleDateFormat dateFormat = new SimpleDateFormat(strDateFormat);
    ConversionRateDoc conversionRateDoc = getConversionRateDoc(table_ID, record_ID, currencyIDFrom,
            currencyIDTo);
    BigDecimal amtFrom = BigDecimal.ZERO;
    BigDecimal amtFromSourcecurrency = BigDecimal.ZERO;
    BigDecimal amtTo = BigDecimal.ZERO;
    if (table_ID.equals(TABLEID_Invoice)) {
        Invoice invoice = OBDal.getInstance().get(Invoice.class, record_ID);
        conversionDate = dateFormat.format(invoice.getAccountingDate());
    } else if (table_ID.equals(TABLEID_Payment)) {
        FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, record_ID);
        conversionDate = dateFormat.format(payment.getPaymentDate());
    } else if (table_ID.equals(TABLEID_Transaction)) {
        FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class, record_ID);
        conversionDate = dateFormat.format(transaction.getDateAcct());
    }
    if (conversionRateDoc != null && record_ID != null) {
        amtFrom = applyRate(_amount, conversionRateDoc, true);
    } else {
        // I try to find a reversal rate for the doc, if exists i apply it reversal as well
        conversionRateDoc = getConversionRateDoc(table_ID, record_ID, currencyIDTo, currencyIDFrom);
        if (conversionRateDoc != null) {
            amtFrom = applyRate(_amount, conversionRateDoc, false);
        } else {
            String convertedAmt = getConvertedAmt(_amount.toString(), currencyIDFrom, currencyIDTo,
                    conversionDate, "", AD_Client_ID, AD_Org_ID, conn);
            if (convertedAmt != null && !"".equals(convertedAmt)) {
                amtFrom = new BigDecimal(convertedAmt);
            } else {
                throw new OBException("@NotConvertible@");
            }
        }
    }
    ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID, currencyIDFrom,
            currencyIDTo);
    if (AD_Table_ID.equals(TABLEID_Invoice)) {
        Invoice invoice = OBDal.getInstance().get(Invoice.class, Record_ID);
        conversionDate = dateFormat.format(invoice.getAccountingDate());
    } else if (AD_Table_ID.equals(TABLEID_Payment)) {
        FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, Record_ID);
        conversionDate = dateFormat.format(payment.getPaymentDate());
    } else if (AD_Table_ID.equals(TABLEID_Transaction) || AD_Table_ID.equals(TABLEID_Reconciliation)) {
        String transactionID = Record_ID;
        // When TableID= Reconciliation info is loaded from transaction
        if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                && line instanceof DocLine_FINReconciliation) {
            transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
        }
        FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class, transactionID);
        conversionDate = dateFormat.format(transaction.getDateAcct());
        conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transaction.getId(),
                currencyIDFrom, currencyIDTo);
    } else {
        conversionDate = dateAcct;
    }
    if (conversionRateCurrentDoc != null) {
        amtTo = applyRate(_amount, conversionRateCurrentDoc, true);
        amtFromSourcecurrency = applyRate(amtFrom, conversionRateCurrentDoc, false);
    } else {
        // I try to find a reversal rate for the doc, if exists i apply it reversal as well
        if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                && line instanceof DocLine_FINReconciliation) {
            String transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
            conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transactionID, currencyIDTo,
                    currencyIDFrom);
        } else {
            conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID, currencyIDTo,
                    currencyIDFrom);
        }
        if (conversionRateCurrentDoc != null) {
            amtTo = applyRate(_amount, conversionRateCurrentDoc, false);
            amtFromSourcecurrency = applyRate(amtFrom, conversionRateCurrentDoc, true);
        } else {
            String convertedAmt = getConvertedAmt(_amount.toString(), currencyIDFrom, currencyIDTo,
                    conversionDate, "", AD_Client_ID, AD_Org_ID, conn);
            if (convertedAmt != null && !"".equals(convertedAmt)) {
                amtTo = new BigDecimal(convertedAmt);
            } else {
                throw new OBException("@NotConvertible@");
            }
            if (amtTo.compareTo(BigDecimal.ZERO) != 0)
                amtFromSourcecurrency = amtFrom.multiply(_amount).divide(amtTo, conversionRatePrecision,
                        BigDecimal.ROUND_HALF_EVEN);
            else
                amtFromSourcecurrency = amtFrom;
        }
    }
    amtDiff = (amtTo).subtract(amtFrom);
    // Add differences related to Different rates for accounting among currencies
    // _amount * ((TrxRate *
    // AccountingRateCurrencyFromCurrencyTo)-AccountingRateCurrencyDocCurrencyTo)
    amtDiff = amtDiff.add(calculateMultipleRatesDifferences(_amount, currencyIDFrom, currencyIDTo, line, conn));
    Currency currencyTo = OBDal.getInstance().get(Currency.class, currencyIDTo);
    amtDiff = amtDiff.setScale(currencyTo.getStandardPrecision().intValue(), BigDecimal.ROUND_HALF_EVEN);
    if (bookDifferences) {
        if ((!isReceipt && amtDiff.compareTo(BigDecimal.ZERO) == 1)
                || (isReceipt && amtDiff.compareTo(BigDecimal.ZERO) == -1)) {
            fact.createLine(line, getAccount(AcctServer.ACCTTYPE_ConvertGainDefaultAmt, as, conn), currencyIDTo,
                    "", amtDiff.abs().toString(), Fact_Acct_Group_ID, seqNo, DocumentType, conn);
        } else if (amtDiff.compareTo(BigDecimal.ZERO) != 0) {
            fact.createLine(line, getAccount(AcctServer.ACCTTYPE_ConvertChargeDefaultAmt, as, conn),
                    currencyIDTo, amtDiff.abs().toString(), "", Fact_Acct_Group_ID, seqNo, DocumentType, conn);
        } else {
            return amtFromSourcecurrency;
        }
    }
    if (log4j.isDebugEnabled())
        log4j.debug("Amt from: " + amtFrom + "[" + currencyIDFrom + "]" + " Amt to: " + amtTo + "["
                + currencyIDTo + "] - amtFromSourcecurrency: " + amtFromSourcecurrency);
    // return value in original currency
    return amtFromSourcecurrency;
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

private void bucheNegativesollmengenAufLager(LosDto losDto, BigDecimal bdGesamtAbgeliefert,
        TheClientDto theClientDto) {/*from w  w  w . j  ava2  s  . c  o m*/

    Query query = em.createNamedQuery("LossollmaterialfindByLosIId");
    query.setParameter(1, losDto.getIId());
    Collection<?> cl = query.getResultList();

    LossollmaterialDto[] sollmat = assembleLossollmaterialDtos(cl);

    Integer lagerIId = null;

    ParametermandantDto parametermandantDto;
    try {
        parametermandantDto = getParameterFac().getMandantparameter(theClientDto.getMandant(),
                ParameterFac.KATEGORIE_FERTIGUNG,
                ParameterFac.PARAMETER_NEGATIVE_SOLLMENGEN_BUCHEN_AUF_ZIELLAGER);

        if (((java.lang.Boolean) parametermandantDto.getCWertAsObject()).booleanValue() == true) {
            lagerIId = losDto.getLagerIIdZiel();
        } else {
            // Laeger des Loses
            LoslagerentnahmeDto[] laeger = loslagerentnahmeFindByLosIId(losDto.getIId());
            lagerIId = laeger[0].getLagerIId();
        }

    } catch (RemoteException e) {
        throwEJBExceptionLPRespectOld(e);
    }

    for (int i = 0; i < sollmat.length; i++) {
        LossollmaterialDto sollmatZeile = sollmat[i];

        if (sollmatZeile.getNMenge().doubleValue() < 0) {

            // Sollsatzgroesse
            BigDecimal ssg = sollmatZeile.getNMenge().abs().divide(losDto.getNLosgroesse(), 10,
                    BigDecimal.ROUND_HALF_EVEN);

            BigDecimal soll = bdGesamtAbgeliefert.multiply(ssg);

            BigDecimal ausgegeben = getAusgegebeneMenge(sollmatZeile.getIId(), null, theClientDto).abs();

            BigDecimal mengeNeu = soll.subtract(ausgegeben);

            if (mengeNeu.doubleValue() > 0) {
                LosistmaterialDto istmat = new LosistmaterialDto();
                istmat.setLagerIId(lagerIId);
                istmat.setLossollmaterialIId(sollmat[i].getIId());
                istmat.setNMenge(mengeNeu.abs());
                istmat.setBAbgang(Helper.boolean2Short(false));

                createLosistmaterial(istmat, null, theClientDto);
            }

        }
    }
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

private void materialwertAllerLosablieferungenNeuBerechnen(LosDto losDto, TheClientDto theClientDto) {

    Query query = em.createNamedQuery("LosablieferungfindByLosIId");
    query.setParameter(1, losDto.getIId());
    Collection<?> losablieferungs = query.getResultList();
    LossollmaterialDto[] losmat = lossollmaterialFindByLosIId(losDto.getIId());

    BigDecimal bdAbgeliefertGesamt = getErledigteMenge(losDto.getIId(), theClientDto);

    BigDecimal bdVorherigerWertDerAblieferung = new BigDecimal(0);

    boolean bErledigt = false;

    if (losDto.getStatusCNr().equals(LocaleFac.STATUS_ERLEDIGT)
            || bdAbgeliefertGesamt.doubleValue() >= losDto.getNLosgroesse().doubleValue()) {
        bErledigt = true;//from   ww w .  j av  a 2 s  . c  o  m
    }

    HashMap<Integer, LagerbewegungDto[]> hmLagerbewegungen = new HashMap<Integer, LagerbewegungDto[]>();

    for (int i = 0; i < losmat.length; i++) {

        BigDecimal bdPreis = getAusgegebeneMengePreis(losmat[i].getIId(), null, theClientDto);

        LosistmaterialDto[] istmatDtos = losistmaterialFindByLossollmaterialIId(losmat[i].getIId());

        ArrayList al = new ArrayList();

        for (int j = 0; j < istmatDtos.length; j++) {

            List<SeriennrChargennrMitMengeDto> snrDtos = getLagerFac()
                    .getAllSeriennrchargennrEinerBelegartposition(LocaleFac.BELEGART_LOS,
                            istmatDtos[j].getIId());

            for (int k = 0; k < snrDtos.size(); k++) {

                LagerbewegungDto bewDto = getLagerFac().getLetzteintrag(LocaleFac.BELEGART_LOS,
                        istmatDtos[j].getIId(), snrDtos.get(k).getCSeriennrChargennr());

                bewDto.setNGestehungspreis(bdPreis);

                al.add(bewDto);

            }

        }

        LagerbewegungDto[] returnArray = new LagerbewegungDto[al.size()];
        hmLagerbewegungen.put(losmat[i].getIId(), (LagerbewegungDto[]) al.toArray(returnArray));

    }

    BigDecimal abgeliefert = new BigDecimal(0);

    int iAblieferung = 0;

    for (Iterator<?> iter = losablieferungs.iterator(); iter.hasNext();) {
        Losablieferung losablieferung = (Losablieferung) iter.next();
        iAblieferung++;
        abgeliefert = abgeliefert.add(losablieferung.getNMenge());

        BigDecimal bdWertIstProAblieferung = new BigDecimal(0.0000);

        for (int i = 0; i < losmat.length; i++) {

            // Sollsatzgroesse der Position

            BigDecimal ssg = losmat[i].getNMenge()
                    .divide(losDto.getNLosgroesse(), 4, BigDecimal.ROUND_HALF_EVEN).multiply(abgeliefert);

            BigDecimal bdMengeIst = new BigDecimal(0.0000);

            BigDecimal bdWertIstProPosition = new BigDecimal(0.0000);

            if (hmLagerbewegungen.containsKey(losmat[i].getIId())) {
                LagerbewegungDto[] bewDtos = (LagerbewegungDto[]) hmLagerbewegungen.get(losmat[i].getIId());

                if (bewDtos != null) {

                    for (int l = 0; l < bewDtos.length; l++) {

                        if (bErledigt && iter.hasNext() == false) {

                            if (Helper.short2boolean(bewDtos[l].getBAbgang())) {

                                bdMengeIst = bdMengeIst.add(bewDtos[l].getNMenge());
                                bdWertIstProPosition = bdWertIstProPosition
                                        .add(bewDtos[l].getNGestehungspreis().multiply(bewDtos[l].getNMenge()));

                            } else {
                                bdMengeIst = bdMengeIst.subtract(bewDtos[l].getNMenge());
                                bdWertIstProPosition = bdWertIstProPosition.subtract(
                                        bewDtos[l].getNEinstandspreis().multiply(bewDtos[l].getNMenge()));
                            }
                        } else {
                            if (losablieferung.getTAendern().getTime() >= bewDtos[l].getTBuchungszeit()
                                    .getTime()) {

                                if (bdMengeIst.abs().add(bewDtos[l].getNMenge()).doubleValue() > ssg.abs()
                                        .doubleValue()) {

                                    BigDecimal mengeSSg = null;

                                    if (bdMengeIst.abs().doubleValue() < ssg.abs().doubleValue()) {
                                        mengeSSg = ssg.subtract(bdMengeIst);
                                    } else {
                                        mengeSSg = new BigDecimal(0);
                                    }

                                    bdMengeIst = bdMengeIst.add(bewDtos[l].getNMenge());

                                    if (Helper.short2boolean(bewDtos[l].getBAbgang())) {

                                        bdWertIstProPosition = bdWertIstProPosition
                                                .add(bewDtos[l].getNGestehungspreis().multiply(mengeSSg));
                                    } else {
                                        bdWertIstProPosition = bdWertIstProPosition
                                                .add(bewDtos[l].getNEinstandspreis().multiply(mengeSSg));
                                    }

                                } else {

                                    bdMengeIst = bdMengeIst.add(bewDtos[l].getNMenge());

                                    if (Helper.short2boolean(bewDtos[l].getBAbgang())) {

                                        bdWertIstProPosition = bdWertIstProPosition.add(bewDtos[l]
                                                .getNGestehungspreis().multiply(bewDtos[l].getNMenge()));
                                    } else {
                                        bdWertIstProPosition = bdWertIstProPosition.subtract(bewDtos[l]
                                                .getNEinstandspreis().multiply(bewDtos[l].getNMenge()));
                                    }
                                }

                            }
                        }

                        System.out.println(bewDtos[l].getIIdBuchung() + ":" + bdWertIstProPosition);
                    }

                    System.out.println(losmat[i].getArtikelIId() + ":" + bdWertIstProPosition);

                    bdWertIstProAblieferung = bdWertIstProAblieferung.add(bdWertIstProPosition);
                }

            }
        }

        losablieferung.setNMaterialwert(bdWertIstProAblieferung.subtract(bdVorherigerWertDerAblieferung)
                .divide(losablieferung.getNMenge(), 4, BigDecimal.ROUND_HALF_EVEN));

        em.merge(losablieferung);
        em.flush();

        bdVorherigerWertDerAblieferung = bdWertIstProAblieferung;

    }

}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

@TransactionTimeout(5000)
public TreeMap<String, Object[]> aktualisiereLoseAusStueckliste(Integer stuecklisteIId,
        TheClientDto theClientDto) {//from  w  w w.  j  a  va2s.c  o  m

    TreeMap<String, Object[]> tmAktualisierteLose = new TreeMap<String, Object[]>();

    ParametermandantDto parameter = null;
    try {
        parameter = getParameterFac().getMandantparameter(theClientDto.getSMandantenwaehrung(),
                ParameterFac.KATEGORIE_STUECKLISTE,
                ParameterFac.PARAMETER_BEI_LOS_AKTUALISIERUNG_MATERIAL_NACHBUCHEN);
    } catch (RemoteException e1) {
        throwEJBExceptionLPRespectOld(e1);
    }
    boolean bMaterialNachbuchen = (Boolean) parameter.getCWertAsObject();

    Session session = FLRSessionFactory.getFactory().openSession();

    String queryString = "SELECT l FROM FLRLosReport l WHERE l.status_c_nr IN ('" + LocaleFac.STATUS_ANGELEGT
            + "','" + LocaleFac.STATUS_AUSGEGEBEN + "','" + LocaleFac.STATUS_IN_PRODUKTION
            + "') AND l.mandant_c_nr='" + theClientDto.getMandant() + "' AND l.stueckliste_i_id IS NOT NULL ";

    if (stuecklisteIId != null) {
        queryString += " AND l.stueckliste_i_id=" + stuecklisteIId;
    }

    org.hibernate.Query query = session.createQuery(queryString);
    List<?> results = query.list();
    Iterator<?> resultListIterator = results.iterator();
    while (resultListIterator.hasNext()) {
        FLRLosReport los = (FLRLosReport) resultListIterator.next();

        if (los.getStatus_c_nr().equals(LocaleFac.STATUS_ANGELEGT)) {
            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);
            aktualisiereSollMaterialAusStueckliste(los.getI_id(), theClientDto);
        } else {

            LoslagerentnahmeDto[] laeger = loslagerentnahmeFindByLosIId(los.getI_id());

            aktualisiereSollArbeitsplanAusStueckliste(los.getI_id(), theClientDto);

            // Alle stuecklistenpositionen holen + Hilfsstueckliste und dann
            // verdichten
            HashMap<Integer, StuecklistepositionDto> hmStuecklistenposition = getFertigungFac()
                    .holeAlleLossollmaterialFuerStuecklistenAktualisierung(los.getStueckliste_i_id(),
                            los.getN_losgroesse(), 0, null, theClientDto);

            LossollmaterialDto[] sollmatDtos = lossollmaterialFindByLosIId(los.getI_id());

            for (int i = 0; i < sollmatDtos.length; i++) {
                try {
                    if (!Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())) {
                        // War vor SP2000 --> &&
                        // sollmatDtos[i].getNMenge().doubleValue() > 0

                        LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                                sollmatDtos[i].getIId());
                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);
                        ArtikelDto artikelDto = getArtikelFac()
                                .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);

                        Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                .getC_nr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                .getCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                .getEinheitCNr();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                .getNMenge();
                        if (artikelDto.getArtikelsprDto() != null) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCBez();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                    .getArtikelsprDto().getCZbez();
                        }

                        if (hmStuecklistenposition.containsKey(sollmatDtos[i].getArtikelIId())) {

                            // Mengen abziehen
                            StuecklistepositionDto stklPos = hmStuecklistenposition
                                    .get(sollmatDtos[i].getArtikelIId());

                            sollmatDtos[i].setNMenge(stklPos.getNMenge());

                            Lossollmaterial sollmatBean = em.find(Lossollmaterial.class,
                                    sollmatDtos[i].getIId());

                            BigDecimal diffSollmenge = stklPos.getNMenge().subtract(sollmatBean.getNMenge());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = diffSollmenge;

                            sollmatBean.setNMenge(stklPos.getNMenge());
                            sollmatBean.setIBeginnterminoffset(stklPos.getIBeginnterminoffset());

                            BigDecimal diff = bdAusgegeben.subtract(sollmatDtos[i].getNMenge());

                            if (diff.doubleValue() == 0 && diffSollmenge.doubleValue() != 0) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {

                                if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false && Helper
                                        .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                                    if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                            || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                                        sollmatDtos[i] = null;

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                0);
                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                        continue;
                                    }

                                    if (diff.doubleValue() > 0) {

                                        for (int j = 0; j < istmaterialDtos.length; j++) {
                                            if (diff.doubleValue() > 0) {
                                                BigDecimal istmenge = istmaterialDtos[j].getNMenge();

                                                BigDecimal bdMengeNeu = null;

                                                if (diff.doubleValue() > istmenge.doubleValue()) {
                                                    bdMengeNeu = new BigDecimal(0);
                                                    diff = diff.subtract(istmenge);
                                                } else {
                                                    bdMengeNeu = istmenge.subtract(diff);
                                                    diff = new BigDecimal(0);
                                                }

                                                updateLosistmaterialMenge(istmaterialDtos[j].getIId(),
                                                        bdMengeNeu, theClientDto);
                                            }
                                        }

                                        BigDecimal bdAusgegebenNachher = getAusgegebeneMenge(
                                                sollmatDtos[i].getIId(), null, theClientDto);

                                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegebenNachher
                                                .subtract(bdAusgegeben);

                                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                    } else {
                                        BigDecimal bdAbzubuchendeMenge = diff.abs();
                                        for (int j = 0; j < laeger.length; j++) {
                                            // wenn noch was abzubuchen ist
                                            // (Menge >
                                            // 0)
                                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                                BigDecimal bdLagerstand = null;
                                                if (Helper
                                                        .short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                                    bdLagerstand = getLagerFac().getLagerstand(
                                                            artikelDto.getIId(), laeger[j].getLagerIId(),
                                                            theClientDto);

                                                } else {
                                                    bdLagerstand = new BigDecimal(999999999);
                                                }
                                                // wenn ein lagerstand da
                                                // ist
                                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                                    BigDecimal bdMengeVonLager;
                                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                                        // wenn mehr als
                                                        // ausreichend
                                                        // auf
                                                        // lager
                                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                                    } else {
                                                        // dann nur den
                                                        // lagerstand
                                                        // entnehmen
                                                        bdMengeVonLager = bdLagerstand;
                                                    }
                                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                                    istmat.setLossollmaterialIId(sollmatDtos[i].getIId());
                                                    istmat.setNMenge(bdMengeVonLager);

                                                    if (sollmatDtos[i].getNMenge().doubleValue() > 0) {
                                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                                    } else {
                                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                                    }

                                                    // ist-wert anlegen und
                                                    // lagerbuchung
                                                    // durchfuehren
                                                    createLosistmaterial(istmat, null, theClientDto);
                                                    // menge reduzieren
                                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge
                                                            .subtract(bdMengeVonLager);
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;

                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                                } else {
                                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                                            0);
                                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            hmStuecklistenposition.remove(sollmatDtos[i].getArtikelIId());
                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                        } else {

                            // Los-Sollmaterial loeschen

                            verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = sollmatDtos[i]
                                    .getNMenge().multiply(new BigDecimal(-1));

                            if (bMaterialNachbuchen == true || istmaterialDtos.length == 0
                                    || (bMaterialNachbuchen == false && Helper
                                            .short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {
                                for (int j = 0; j < istmaterialDtos.length; j++) {
                                    removeLosistmaterial(istmaterialDtos[j], theClientDto);
                                }

                                removeLossollmaterial(sollmatDtos[i], theClientDto);

                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                        .multiply(new BigDecimal(-1));
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                            } else {
                                sollmatDtos[i].setNMenge(new BigDecimal(0));
                                updateLossollmaterial(sollmatDtos[i], theClientDto);
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                        0);
                                tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                        los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            }

                            getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS,
                                    sollmatDtos[i].getIId(), false, theClientDto);
                            sollmatDtos[i] = null;

                        }

                    }
                } catch (RemoteException e) {
                    throwEJBExceptionLPRespectOld(e);
                }

            }

            // Nun alle uebrigen Sollmaterial=0 loeschen
            for (int i = 0; i < sollmatDtos.length; i++) {
                if (sollmatDtos[i] != null && !Helper.short2boolean(sollmatDtos[i].getBNachtraeglich())
                        && sollmatDtos[i].getNMenge().doubleValue() == 0) {

                    // Los-Sollmaterial loeschen
                    LosistmaterialDto[] istmaterialDtos = losistmaterialFindByLossollmaterialIId(
                            sollmatDtos[i].getIId());

                    boolean bAlleIstMaterialSindNull = true;

                    for (int z = 0; z < istmaterialDtos.length; z++) {
                        if (istmaterialDtos[z].getNMenge().doubleValue() != 0) {
                            bAlleIstMaterialSindNull = false;
                        }
                    }
                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(sollmatDtos[i].getArtikelIId(), theClientDto);
                    if (bMaterialNachbuchen == true || bAlleIstMaterialSindNull == true
                            || (bMaterialNachbuchen == false
                                    && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmatDtos[i].getIId(), null,
                                theClientDto);

                        for (int j = 0; j < istmaterialDtos.length; j++) {
                            removeLosistmaterial(istmaterialDtos[j], theClientDto);
                        }

                        verknuepfungZuBestellpositionUndArbeitsplanLoeschen(sollmatDtos[i].getIId());
                        removeLossollmaterial(sollmatDtos[i], theClientDto);

                        if (bdAusgegeben.doubleValue() != 0) {

                            Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                                    .getC_nr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                                    .getCNr();
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                                    .getEinheitCNr();
                            if (artikelDto.getArtikelsprDto() != null) {
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCBez();
                                oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                        .getArtikelsprDto().getCZbez();
                            }
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdAusgegeben
                                    .multiply(new BigDecimal(-1));

                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = new BigDecimal(
                                    0);

                            tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                    los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                        }
                    }

                }
            }

            // Alle noch nicht verbrauchten neu eintragen

            Iterator it = hmStuecklistenposition.keySet().iterator();
            while (it.hasNext()) {

                try {
                    LagerDto lagerDtoMandant = getLagerFac().getHauptlagerDesMandanten(theClientDto);
                    Integer artikelIId = (Integer) it.next();

                    StuecklistepositionDto stklPos = hmStuecklistenposition.get(artikelIId);
                    BigDecimal bdAbzubuchendeMenge = stklPos.getNMenge();

                    ArtikelDto artikelDto = getArtikelFac()
                            .artikelFindByPrimaryKeySmall(stklPos.getArtikelIId(), theClientDto);

                    Object[] oZeileReport = new Object[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ANZAHL_SPALTEN];
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_LOSNUMMER] = los
                            .getC_nr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ARTIKELNUMMER] = artikelDto
                            .getCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_EINHEIT] = artikelDto
                            .getEinheitCNr();
                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = stklPos
                            .getNMenge();

                    if (artikelDto.getArtikelsprDto() != null) {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCBez();
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_ZUSATZBEZEICHNUNG] = artikelDto
                                .getArtikelsprDto().getCZbez();
                    }

                    LossollmaterialDto losMatDto = new LossollmaterialDto();
                    losMatDto.setArtikelIId(stklPos.getArtikelIId());
                    losMatDto.setBNachtraeglich(Helper.boolean2Short(false));
                    losMatDto.setCKommentar(stklPos.getCKommentar());
                    losMatDto.setCPosition(stklPos.getCPosition());
                    losMatDto.setFDimension1(stklPos.getFDimension1());
                    losMatDto.setFDimension2(stklPos.getFDimension2());
                    losMatDto.setFDimension3(stklPos.getFDimension3());
                    losMatDto.setILfdnummer(stklPos.getILfdnummer());
                    losMatDto.setEinheitCNr(stklPos.getEinheitCNr());
                    losMatDto.setLosIId(los.getI_id());
                    losMatDto.setMontageartIId(stklPos.getMontageartIId());
                    losMatDto.setNMenge(stklPos.getNMenge());
                    losMatDto.setISort(new Integer(0));

                    BigDecimal bdSollpreis = getLagerFac().getGemittelterGestehungspreisEinesLagers(
                            stklPos.getArtikelIId(), lagerDtoMandant.getIId(), theClientDto);
                    losMatDto.setNSollpreis(bdSollpreis);

                    Integer sollmatIId = createLossollmaterial(losMatDto, theClientDto).getIId();
                    if (bMaterialNachbuchen == true || (bMaterialNachbuchen == false
                            && Helper.short2boolean(artikelDto.getBLagerbewirtschaftet()) == false)) {

                        if (Helper.short2boolean(artikelDto.getBSeriennrtragend())
                                || Helper.short2boolean(artikelDto.getBChargennrtragend())) {
                            oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_BEMERKUNG] = "Der Artikel ist SNR/CNR behaftet und wurde nicht ber\u00FCcksichtigt.";

                            tmAktualisierteLose.put(los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                            continue;
                        }

                        for (int j = 0; j < laeger.length; j++) {
                            // wenn noch was abzubuchen ist
                            // (Menge >
                            // 0)
                            if (bdAbzubuchendeMenge.compareTo(new BigDecimal(0)) == 1) {
                                BigDecimal bdLagerstand = null;
                                if (Helper.short2boolean(artikelDto.getBLagerbewirtschaftet())) {

                                    bdLagerstand = getLagerFac().getLagerstand(artikelDto.getIId(),
                                            laeger[j].getLagerIId(), theClientDto);

                                } else {
                                    bdLagerstand = new BigDecimal(999999999);
                                }
                                // wenn ein lagerstand da ist
                                if (bdLagerstand.compareTo(new BigDecimal(0)) == 1) {
                                    BigDecimal bdMengeVonLager;
                                    if (bdLagerstand.compareTo(bdAbzubuchendeMenge) == 1) {
                                        // wenn mehr als
                                        // ausreichend
                                        // auf
                                        // lager
                                        bdMengeVonLager = bdAbzubuchendeMenge;
                                    } else {
                                        // dann nur den
                                        // lagerstand
                                        // entnehmen
                                        bdMengeVonLager = bdLagerstand;
                                    }
                                    LosistmaterialDto istmat = new LosistmaterialDto();
                                    istmat.setLagerIId(laeger[j].getLagerIId());
                                    istmat.setLossollmaterialIId(sollmatIId);
                                    istmat.setNMenge(bdMengeVonLager);

                                    if (stklPos.getNMenge().doubleValue() > 0) {
                                        istmat.setBAbgang(Helper.boolean2Short(true));
                                    } else {
                                        istmat.setBAbgang(Helper.boolean2Short(false));
                                    }

                                    // ist-wert anlegen und
                                    // lagerbuchung
                                    // durchfuehren
                                    createLosistmaterial(istmat, null, theClientDto);
                                    // menge reduzieren
                                    bdAbzubuchendeMenge = bdAbzubuchendeMenge.subtract(bdMengeVonLager);

                                    oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMengeVonLager;
                                    tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                            los.getC_nr() + artikelDto.getCNr(), oZeileReport);

                                }
                            }
                        }
                    } else {
                        oZeileReport[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = new BigDecimal(
                                0);
                        tmAktualisierteLose = add2TreeMap(tmAktualisierteLose,
                                los.getC_nr() + artikelDto.getCNr(), oZeileReport);
                    }

                    getFehlmengeFac().aktualisiereFehlmenge(LocaleFac.BELEGART_LOS, losMatDto.getIId(), false,
                            theClientDto);

                } catch (RemoteException e) {
                    throwEJBExceptionLPRespectOld(e);
                }
            }

            // Los aktualisieren
            Los losUpd = em.find(Los.class, los.getI_id());
            losUpd.setTAktualisierungstueckliste(getTimestamp());

        }

    }

    return tmAktualisierteLose;
}