Ejemplo n.º 1
0
 public void docActionStatusChanged(DocActionStatusEvent event) {
   // Evento: Impresión fiscal de documento.
   if (event.getDocActionStatus() == DocActionStatusEvent.ST_FISCAL_PRINT_DOCUMENT) {
     PO document = (PO) event.getSource();
     // Se obtiene el nombre del tipo de documento.
     Integer docTypeID = (Integer) document.get_Value("C_DocTypeTarget_ID");
     String docTypeName = "";
     if (docTypeID != null && docTypeID > 0) {
       MDocType docType = MDocType.get(Env.getCtx(), docTypeID);
       docTypeName = " - " + docType.getPrintName();
     }
     // Se crea la ventana que muestra el estado de la impresora fiscal.
     final AInfoFiscalPrinter infoFiscalPrinter =
         new AInfoFiscalPrinter(
             null,
             m_pi.getWindowNo(),
             Msg.parseTranslation(Env.getCtx(), "@PrintingFiscalDocument@" + docTypeName));
     FiscalDocumentPrint fdp = (FiscalDocumentPrint) event.getParameter(0);
     // Se setea  la ventana de información tanto como listener de estado de la
     // impresora como el estado de la impresión del documento.
     fdp.addDocumentPrintListener(infoFiscalPrinter);
     fdp.setPrinterEventListener(infoFiscalPrinter);
     // Se efectúa la referencia cruzada entre el Impresor y la
     // ventana de información.
     infoFiscalPrinter.setFiscalDocumentPrint(fdp);
     // Se muestra la ventana en el thread de Swing.
     SwingUtilities.invokeLater(
         new Runnable() {
           public void run() {
             infoFiscalPrinter.setVisible(true);
           }
         });
   }
 }
Ejemplo n.º 2
0
  /**
   * Descripción de Método
   *
   * @return
   * @throws Exception
   */
  protected String doIt() throws Exception {
    // JOptionPane.showMessageDialog( null,"En CopyFromOrder.doit() :=","-...Fin",
    // JOptionPane.INFORMATION_MESSAGE );
    int To_C_Order_ID = getRecord_ID();

    log.info("From C_Order_ID=" + p_C_Order_ID + " to " + To_C_Order_ID);

    if (To_C_Order_ID == 0) {
      throw new IllegalArgumentException("Target C_Order_ID == 0");
    }

    // Buscar el pedido de la factura
    MInvoice invoice = null;
    if (p_C_Invoice_ID != 0 && p_C_Order_ID == 0) {
      invoice = new MInvoice(getCtx(), p_C_Invoice_ID, get_TrxName());
      p_C_Order_ID = invoice.getC_Order_ID();
    }

    if (p_C_Order_ID == 0) {
      throw new IllegalArgumentException("Source C_Order_ID == 0");
    }

    MOrder from = new MOrder(getCtx(), p_C_Order_ID, get_TrxName());
    MOrder to = new MOrder(getCtx(), To_C_Order_ID, get_TrxName());

    // Si el tipo de documento del order from es un presupuesto vencido, hay
    // que verificar si a configuración del tipo de documento destino
    // permite crear a partir de presupuestos vencidos
    MDocType docTypeTo = new MDocType(getCtx(), to.getC_DocTypeTarget_ID(), get_TrxName());
    if (from.isExpiredProposal(Env.getDate()) && !docTypeTo.isAllowProposalDue()) {
      throw new Exception(Msg.getMsg(getCtx(), "NotAllowedProposalDue"));
    }

    // Copiar la cabecera si así lo requiere
    if (copyHeader) {
      // Datos a setear luego de la copia de campos
      Integer docTypeID = to.getC_DocTypeTarget_ID();
      String docAction = to.getDocAction();
      String docStatus = to.getDocStatus();
      PO.copyValues(from, to);
      to.setC_DocTypeTarget_ID(docTypeID);
      to.setDocStatus(docStatus);
      to.setDocAction(docAction);
      to.setProcessed(false);
      to.setRef_Order_ID(from.getC_Order_ID());
      if (!to.save()) {
        throw new Exception(CLogger.retrieveErrorAsString());
      }
    }

    int no = to.copyLinesFrom(from, false, false); // no Attributes

    //

    return "@Copied@=" + no;
  } // doIt
 public void initializeCodeParts(MInvoice invoice, MDocType docType) {
   if (Util.isEmpty(getClientCuit(), true)) {
     getLog().severe("No existe CUIT de la compañia");
     return;
   }
   // b) Código de tipo de comprobante.
   if (Util.isEmpty(docType.getdocsubtypecae(), true)) {
     getLog().severe("No existe codigo de tipo de documento electronico");
     return;
   }
   setDocTypeCodeFE(docType.getdocsubtypecae());
   // c) Punto de venta.
   if (Util.isEmpty(invoice.getPuntoDeVenta(), true)) {
     getLog().severe("No existe punto de venta");
     return;
   }
   Integer puntoDeVenta = invoice.getPuntoDeVenta();
   String ptoVta = String.valueOf(puntoDeVenta);
   // El punto de venta debe ir con 0 cuando es menor a 1000
   if (puntoDeVenta < 1000) {
     ptoVta = "0";
     // Si es menor a 100, hay que agregar otro 0
     if (puntoDeVenta < 100) {
       ptoVta += "0";
     }
     // Si es menor a 10, hay que agregar otro 0
     if (puntoDeVenta < 10) {
       ptoVta += "0";
     }
     ptoVta = ptoVta + String.valueOf(puntoDeVenta);
   }
   setPuntoDeVenta(ptoVta);
   // d) Código de Autorización de Impresión (C.A.I.).
   // En realidad es el CAE lo que se debe imprimir
   if (Util.isEmpty(invoice.getcae(), true)) {
     getLog().severe("No existe CAE");
     return;
   }
   setCae(invoice.getcae());
   // e) Fecha de vencimiento.
   // Fecha de Vencimiento del CAE - Formato AAAAMMDD
   if (invoice.getvtocae() == null) {
     getLog().severe("No existe fecha de vencimieto de cae");
     return;
   }
   setCaeDueDate(new SimpleDateFormat("yyyyMMdd").format(invoice.getvtocae()));
 }
Ejemplo n.º 4
0
  /**
   * Descripción de Método
   *
   * @return
   * @throws java.lang.Exception
   */
  protected String doIt() throws java.lang.Exception {
    String errorLocatorNotFound = "'" + getMsg("LocatorNotFound") + ". '";
    String errorWarehouseNotFound = "'" + getMsg("WarehouseNotFound") + ". '";
    String errorInvalidLocator = "'" + getMsg("InvalidLocatorWarehouse") + ". '";
    String errorProductNotFound = "'" + getMsg("ProductNotFound") + ". '";
    String errorInvalidQtyCount = "'" + getMsg("InvalidQtyCount") + ". '";
    String errorNoInventory = "'" + getMsg("NoExistsInventory") + ". '";

    log.info("M_Locator_ID=" + p_M_Locator_ID + ",MovementDate=" + p_MovementDate);

    StringBuffer sql = null;
    int no = 0;
    String securityCheck = " AND AD_Client_ID=" + p_AD_Client_ID + " AND IsActive = 'Y' ";

    maintainInventoryImportTable(securityCheck);

    // Delete Old Imported

    if (p_DeleteOldImported) {
      sql =
          new StringBuffer("DELETE I_Inventory " + "WHERE I_IsImported='Y'").append(securityCheck);
      no = DB.executeUpdate(sql.toString());
      log.fine("Delete Old Impored =" + no);
    }

    // Reset

    sql =
        new StringBuffer(" UPDATE I_Inventory " + " SET AD_Client_ID   = COALESCE (AD_Client_ID,")
            .append(p_AD_Client_ID)
            .append(")," + "     AD_Org_ID      = COALESCE (AD_Org_ID,")
            .append(p_AD_Org_ID)
            .append(
                "),"
                    + "     IsActive       = COALESCE (IsActive, 'Y'),"
                    + "     Created        = COALESCE (Created, SysDate),"
                    + "     CreatedBy      = COALESCE (CreatedBy, 0),"
                    + "     Updated        = COALESCE (Updated, SysDate),"
                    + "     UpdatedBy      = COALESCE (UpdatedBy, 0),"
                    + "     I_ErrorMsg     = NULL, "
                    + "     I_IsImported   = 'N' "
                    + " WHERE I_IsImported <> 'Y' OR I_IsImported IS NULL")
            .append(securityCheck);

    no = DB.executeUpdate(sql.toString());
    log.info("Reset=" + no);

    // sql = new StringBuffer( "UPDATE I_Inventory i " + "SET I_IsImported='E',
    // I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Org, '" + " WHERE (AD_Org_ID IS NULL OR AD_Org_ID=0" + "
    // OR EXISTS (SELECT * FROM AD_Org oo WHERE i.AD_Org_ID=oo.AD_Org_ID AND (oo.IsSummary='Y' OR
    // oo.IsActive='N')))" + " AND I_IsImported<>'Y'" ).append( clientCheck );
    // no = DB.executeUpdate( sql.toString());

    // if( no != 0 ) {
    // log.warning( "Invalid Org=" + no );
    // }

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Movement Date
    //////////////////////////////////////////////////////////////////////////////////////
    sql =
        new StringBuffer(" UPDATE I_Inventory " + " SET MovementDate = ")
            .append(DB.TO_DATE(p_MovementDate))
            .append(" WHERE I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Movement Date = " + no);

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Warehouse and Locator
    //////////////////////////////////////////////////////////////////////////////////////

    // Warehouse From Value
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Warehouse_ID = "
                    + "		(SELECT M_Warehouse_ID "
                    + "	 	 FROM M_Warehouse w"
                    + "    	 WHERE (TRIM(i.WarehouseValue) = TRIM(w.Value) OR"
                    + "               TRIM(i.WarehouseValue) = TRIM(w.Name)) AND "
                    + "              i.AD_Client_ID = w.AD_Client_ID AND "
                    + "              w.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE WarehouseValue IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Warehouse from Value = " + no);

    // Locator From Value
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Locator_ID = "
                    + "		(SELECT M_Locator_ID "
                    + "	 	 FROM M_Locator l"
                    + "    	 WHERE i.LocatorValue = l.Value AND "
                    + "              i.AD_Client_ID = l.AD_Client_ID AND "
                    + "              l.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE LocatorValue IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Locator from Value = " + no);

    // locator from X,Y,Z
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Locator_ID="
                    + "		(SELECT M_Locator_ID "
                    + "        FROM M_Locator l"
                    + "        WHERE i.X=l.X AND i.Y=l.Y AND i.Z=l.Z AND "
                    + "              i.AD_Client_ID=l.AD_Client_ID AND "
                    + "              l.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE M_Locator_ID IS NULL AND "
                    + "       X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL AND "
                    + "       I_IsImported<>'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Locator from X,Y,Z = " + no);

    // Warehouse From Locator
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Warehouse_ID = "
                    + "		(SELECT M_Warehouse_ID "
                    + "        FROM M_Locator l "
                    + "        WHERE i.M_Locator_ID=l.M_Locator_ID) "
                    + " WHERE M_Warehouse_ID IS NULL AND "
                    + "       M_Locator_ID IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Warehouse from Locator = " + no);

    // locator default from warehouse
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Locator_ID = "
                    + "		(SELECT M_Locator_ID "
                    + "        FROM M_Locator l "
                    + "        WHERE i.M_Warehouse_ID=l.M_Warehouse_ID"
                    + "		 ORDER BY isdefault DESC "
                    + "		 LIMIT 1) "
                    + " WHERE M_Warehouse_ID IS NOT NULL AND "
                    + "       M_Locator_ID IS NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Locator Default from Warehouse = " + no);

    // Locator From parameter (default locator)
    if (p_M_Locator_ID != 0) {
      sql =
          new StringBuffer(" UPDATE I_Inventory " + " SET M_Locator_ID = ")
              .append(p_M_Locator_ID)
              .append(
                  ", " + "     LocatorValue = (SELECT Value FROM M_Locator WHERE M_Locator_ID = ")
              .append(p_M_Locator_ID)
              .append(") " + " WHERE M_Locator_ID IS NULL AND " + "       I_IsImported<>'Y'")
              .append(securityCheck);
      no = DB.executeUpdate(sql.toString());
      log.fine("Set Locator from Parameter = " + no);
    }

    // No locator detection
    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorLocatorNotFound
                    + " WHERE M_Locator_ID IS NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("Locator not found = " + no);
    }

    // No Warehouse detection
    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorWarehouseNotFound
                    + " WHERE M_Warehouse_ID IS NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("Warehouse not found = " + no);
    }

    // Locator warehouse not the line warehouse detection
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorInvalidLocator
                    + " WHERE i.M_Warehouse_ID IS NOT NULL AND "
                    + "       i.M_Locator_ID IS NOT NULL AND "
                    + "       i.I_IsImported <> 'Y' AND "
                    + "       i.M_Warehouse_ID <> (SELECT l.M_Warehouse_ID "
                    + "                            FROM M_Locator l "
                    + "                            WHERE l.M_Locator_ID = i.M_Locator_ID) ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("Locator Warehouse not the line Warehouse = " + no);
    }

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Product
    //////////////////////////////////////////////////////////////////////////////////////

    // ... From Value
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_Product_ID = "
                    + "		(SELECT M_Product_ID "
                    + "        FROM M_Product p"
                    + "        WHERE i.Value = p.Value AND "
                    + "              i.AD_Client_ID = p.AD_Client_ID AND "
                    + "              p.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE Value IS NOT NULL AND "
                    + "       I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Product from Value=" + no);

    // ... From UPC Instance
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET M_AttributeSetInstance_ID = "
                    + "		(SELECT M_AttributeSetInstance_ID "
                    + "		 FROM M_Product_UPC_Instance p "
                    + "        WHERE i.UPC = p.UPC AND "
                    + "		 	   i.M_Product_ID = p.M_Product_ID AND "
                    + "              i.AD_Client_ID = p.AD_Client_ID AND "
                    + "              p.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE M_AttributeSetInstance_ID IS NULL AND "
                    + "       UPC IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set AttributeInstance from UPC = " + no);

    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET INSTANCE_DESCRIPTION = "
                    + "		(SELECT Description "
                    + "		 FROM M_AttributeSetInstance p "
                    + "        WHERE i.M_AttributeSetInstance_ID = p.M_AttributeSetInstance_ID AND "
                    + "              i.AD_Client_ID = p.AD_Client_ID AND "
                    + "              p.IsActive = 'Y' AND "
                    + "              ROWNUM=1) "
                    + " WHERE INSTANCE_DESCRIPTION IS NULL AND "
                    + "       M_AttributeSetInstance_ID IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Instance_Description from AttributeInstance = " + no);

    if (no == 0) {
      // ... From UPC
      sql =
          new StringBuffer(
                  " UPDATE I_Inventory i "
                      + " SET M_Product_ID = "
                      + "		(SELECT M_Product_ID "
                      + "		 FROM M_Product p "
                      + "        WHERE i.UPC = p.UPC AND "
                      + "              i.AD_Client_ID = p.AD_Client_ID AND "
                      + "              p.IsActive = 'Y' AND "
                      + "              ROWNUM=1) "
                      + " WHERE M_Product_ID IS NULL AND "
                      + "       UPC IS NOT NULL AND "
                      + "       I_IsImported <> 'Y' ")
              .append(securityCheck);
      no = DB.executeUpdate(sql.toString());
      log.fine("Set Product from UPC = " + no);
    }

    // No product detection
    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorProductNotFound
                    + " WHERE M_Product_ID IS NULL AND "
                    + "       I_IsImported<>'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("Product not found = " + no);
    }

    // No QtyCount
    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorInvalidQtyCount
                    + " WHERE QtyCount IS NULL AND "
                    + "       I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("No QtyCount = " + no);
    }

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Organization from VALUE
    //////////////////////////////////////////////////////////////////////////////////////
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET AD_Org_ID = "
                    + "		(SELECT AD_Org_ID "
                    + "        FROM AD_Org p"
                    + "        WHERE i.OrgValue = p.Value AND "
                    + "              i.AD_Client_ID = p.AD_Client_ID AND "
                    + "              p.IsActive = 'Y'"
                    + "              ) "
                    + " WHERE OrgValue IS NOT NULL AND "
                    + "       I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Org from OrgValue=" + no);

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Organization
    //////////////////////////////////////////////////////////////////////////////////////
    sql =
        new StringBuffer(
                " UPDATE I_Inventory i "
                    + " SET AD_Org_ID = "
                    + "		(SELECT AD_Org_ID "
                    + "		 FROM M_Warehouse p "
                    + "        WHERE i.M_Warehouse_ID = p.M_Warehouse_ID AND "
                    + "              ROWNUM=1) "
                    + " WHERE M_Warehouse_ID IS NOT NULL AND "
                    + "       I_IsImported <> 'Y' ")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Organization from Warehouse = " + no);

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Inventory
    //////////////////////////////////////////////////////////////////////////////////////
    MDocType docTypePI =
        MDocType.getDocType(getCtx(), MDocType.DOCTYPE_MaterialPhysicalInventory, null);
    sql =
        new StringBuffer(
                "UPDATE I_Inventory i "
                    + "SET M_Inventory_ID = (SELECT m.m_inventory_id "
                    + "						FROM m_inventory m "
                    + "						WHERE m.documentno = i.inventory_documentno "
                    + "								AND m.m_warehouse_id = i.m_warehouse_id"
                    + "								AND m.docstatus IN ('DR','IP') "
                    + "								AND m.inventorykind = '"
                    + MInventory.INVENTORYKIND_PhysicalInventory
                    + "'"
                    + "								AND m.c_doctype_id = "
                    + docTypePI.getC_DocType_ID()
                    + "						LIMIT 1) "
                    + "WHERE inventory_documentno is not null "
                    + "		AND M_Warehouse_ID IS NOT NULL "
                    + "		AND I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Inventory ID From Inventory DocumentNo = " + no);

    // Error si tiene un nro de documento pero no existe tal inventario
    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'E', "
                    + "     I_ErrorMsg = COALESCE(I_ErrorMsg,'') || "
                    + errorNoInventory
                    + " WHERE inventory_documentno is not null AND "
                    + "		M_Inventory_ID IS NULL AND "
                    + "       I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    if (no != 0) {
      log.warning("No Inventory ID = " + no);
    }

    //////////////////////////////////////////////////////////////////////////////////////
    // Set Inventory Line
    //////////////////////////////////////////////////////////////////////////////////////
    sql =
        new StringBuffer(
                "UPDATE I_Inventory i "
                    + "SET M_InventoryLine_ID = (SELECT m.m_inventoryline_id "
                    + "						FROM m_inventoryline m "
                    + "						WHERE m.m_product_id = i.m_product_id "
                    + "								AND m.m_inventory_id = i.m_inventory_id"
                    + "								AND (CASE WHEN i.m_attributesetinstance_id is null THEN m.m_attributesetinstance_id = 0"
                    + "											WHEN i.m_attributesetinstance_id = 0 THEN m.m_attributesetinstance_id = 0"
                    + "											ELSE m.m_attributesetinstance_id = i.m_attributesetinstance_id "
                    + "										END)"
                    + "								AND m.isactive = 'Y'"
                    + "						LIMIT 1) "
                    + "WHERE M_Inventory_ID is not null "
                    + "		AND M_Product_ID is not null "
                    + "		AND I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    log.fine("Set Inventory Line ID = " + no);

    MInventory inventory = null;
    MInventoryLine line = null;
    int noInsert = 0;
    int noUpdate = 0;
    int noInsertLine = 0;
    int noUpdateLine = 0;
    boolean insertLine = false;

    // Go through Inventory Records

    sql =
        new StringBuffer(
                " SELECT I_Inventory_ID " + " FROM I_Inventory " + " WHERE I_IsImported = 'N'")
            .append(securityCheck)
            .append(
                " ORDER BY M_Inventory_ID, M_Warehouse_ID, TRUNC(MovementDate), I_Inventory_ID");

    try {
      PreparedStatement pstmt = DB.prepareStatement(sql.toString());
      ResultSet rs = pstmt.executeQuery();

      //

      int x_M_Warehouse_ID = -1;
      Timestamp x_MovementDate = null;
      X_I_Inventory imp = null;
      while (rs.next()) {
        imp = new X_I_Inventory(getCtx(), rs.getInt("I_Inventory_ID"), null);
        Timestamp MovementDate = TimeUtil.getDay(imp.getMovementDate());

        // Inventario existente
        if (!Util.isEmpty(imp.getM_Inventory_ID(), true)) {
          if (inventory == null || inventory.getM_Inventory_ID() != imp.getM_Inventory_ID()) {
            inventory = new MInventory(getCtx(), imp.getM_Inventory_ID(), null);
            manageInventory(inventory);
            noUpdate++;
          }
        } else if ((inventory == null)
            || (imp.getM_Warehouse_ID() != x_M_Warehouse_ID)
            || !MovementDate.equals(x_MovementDate)) {
          inventory = new MInventory(getCtx(), 0, null);
          inventory.setClientOrg(imp.getAD_Client_ID(), imp.getAD_Org_ID());
          inventory.setDescription("I " + imp.getM_Warehouse_ID() + " " + MovementDate);
          inventory.setM_Warehouse_ID(imp.getM_Warehouse_ID());
          inventory.setMovementDate(MovementDate);
          manageInventory(inventory);

          if (!inventory.save()) {
            log.log(Level.SEVERE, "Inventory not saved: " + CLogger.retrieveErrorAsString());

            break;
          }

          x_M_Warehouse_ID = imp.getM_Warehouse_ID();
          x_MovementDate = MovementDate;
          noInsert++;
        }

        // Line

        // Added by Lucas Hernandez - Kunan
        // int            M_AttributeSetInstance_ID = 0;

        // Línea de inventario existente
        if (!Util.isEmpty(imp.getM_InventoryLine_ID(), true)) {
          line = new MInventoryLine(getCtx(), imp.getM_InventoryLine_ID(), null);
          line.setQtyCount(imp.getQtyCount());
          line.setQtyInternalUse(line.getQtyBook().subtract(imp.getQtyCount()));
        } else {
          line =
              new MInventoryLine(
                  inventory,
                  imp.getM_Locator_ID(),
                  imp.getM_Product_ID(),
                  imp.getM_AttributeSetInstance_ID(),
                  imp.getQtyBook(),
                  imp.getQtyCount());
        }

        insertLine = line.getM_InventoryLine_ID() == 0;

        if (line.save()) {

          no =
              DB.executeUpdate(
                  "UPDATE I_Inventory SET i_isimported='Y',i_errormsg=null,m_inventory_id="
                      + line.getM_Inventory_ID()
                      + ",M_InventoryLine_ID="
                      + line.getM_InventoryLine_ID()
                      + ",processed='Y' WHERE i_inventory_id = "
                      + imp.getID());
          //                    imp.setI_IsImported( true );
          //                    imp.setI_ErrorMsg(null);
          //                    imp.setM_Inventory_ID( line.getM_Inventory_ID());
          //                    imp.setM_InventoryLine_ID( line.getM_InventoryLine_ID());
          //                    imp.setProcessed( true );

          //                    if( imp.save()) {
          if (no == 1) {
            if (insertLine) {
              noInsertLine++;
            } else {
              noUpdateLine++;
            }
          }
        } else {
          no =
              DB.executeUpdate(
                  "UPDATE I_Inventory SET i_isimported='E',i_errormsg="
                      + CLogger.retrieveErrorAsString()
                      + " WHERE i_inventory_id = "
                      + imp.getID());
        }
      }

      rs.close();
      pstmt.close();
    } catch (Exception e) {
      log.log(Level.SEVERE, "doIt", e);
    }

    // Set Error to indicator to not imported

    sql =
        new StringBuffer(
                " UPDATE I_Inventory "
                    + " SET I_IsImported = 'N', "
                    + "     Updated=SysDate "
                    + " WHERE I_IsImported <> 'Y'")
            .append(securityCheck);
    no = DB.executeUpdate(sql.toString());
    addLog(0, null, new BigDecimal(no), "@Errors@");

    // Post Import
    postImport();

    addLog(0, null, new BigDecimal(noInsert), "@M_Inventory_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noUpdate), "@M_Inventory_ID@: @Updated@");
    addLog(0, null, new BigDecimal(noInsertLine), "@M_InventoryLine_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noUpdateLine), "@M_InventoryLine_ID@: @Updated@");

    return "";
  } // doIt