Search in sources :

Example 1 with MTimeExpense

use of org.compiere.model.MTimeExpense in project adempiere by adempiere.

the class ExpenseAPInvoice method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (clear text)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws java.lang.Exception {
    StringBuffer sql = new StringBuffer("SELECT * " + "FROM S_TimeExpense e " + "WHERE e.Processed='Y'" + //	#1
    " AND e.AD_Client_ID=?");
    if (m_C_BPartner_ID != 0)
        //	#2
        sql.append(" AND e.C_BPartner_ID=?");
    if (m_DateFrom != null)
        //	#3
        sql.append(" AND e.DateReport >= ?");
    if (m_DateTo != null)
        //	#4
        sql.append(" AND e.DateReport <= ?");
    sql.append(" AND EXISTS (SELECT * FROM S_TimeExpenseLine el " + "WHERE e.S_TimeExpense_ID=el.S_TimeExpense_ID" + " AND el.C_InvoiceLine_ID IS NULL" + " AND el.ConvertedAmt<>0) " + "ORDER BY e.C_BPartner_ID, e.S_TimeExpense_ID");
    //
    int old_BPartner_ID = -1;
    MInvoice invoice = null;
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        int par = 1;
        pstmt.setInt(par++, getAD_Client_ID());
        if (m_C_BPartner_ID != 0)
            pstmt.setInt(par++, m_C_BPartner_ID);
        if (m_DateFrom != null)
            pstmt.setTimestamp(par++, m_DateFrom);
        if (m_DateTo != null)
            pstmt.setTimestamp(par++, m_DateTo);
        rs = pstmt.executeQuery();
        while (//	********* Expense Line Loop
        rs.next()) {
            MTimeExpense te = new MTimeExpense(getCtx(), rs, get_TrxName());
            //	New BPartner - New Order
            if (te.getC_BPartner_ID() != old_BPartner_ID) {
                completeInvoice(invoice);
                MBPartner bp = new MBPartner(getCtx(), te.getC_BPartner_ID(), get_TrxName());
                //
                log.info("New Invoice for " + bp);
                invoice = new MInvoice(getCtx(), 0, null);
                invoice.setClientOrg(te.getAD_Client_ID(), te.getAD_Org_ID());
                //	API
                invoice.setC_DocTypeTarget_ID(MDocType.DOCBASETYPE_APInvoice);
                invoice.setDocumentNo(te.getDocumentNo());
                //
                invoice.setBPartner(bp);
                if (invoice.getC_BPartner_Location_ID() == 0) {
                    log.log(Level.SEVERE, "No BP Location: " + bp);
                    addLog(0, te.getDateReport(), null, "No Location: " + te.getDocumentNo() + " " + bp.getName());
                    invoice = null;
                    break;
                }
                invoice.setM_PriceList_ID(te.getM_PriceList_ID());
                invoice.setSalesRep_ID(te.getDoc_User_ID());
                String descr = Msg.translate(getCtx(), "S_TimeExpense_ID") + ": " + te.getDocumentNo() + " " + DisplayType.getDateFormat(DisplayType.Date).format(te.getDateReport());
                invoice.setDescription(descr);
                if (!invoice.save())
                    new IllegalStateException("Cannot save Invoice");
                old_BPartner_ID = bp.getC_BPartner_ID();
            }
            MTimeExpenseLine[] tel = te.getLines(false);
            for (int i = 0; i < tel.length; i++) {
                MTimeExpenseLine line = tel[i];
                //	Already Invoiced or nothing to be reimbursed
                if (line.getC_InvoiceLine_ID() != 0 || Env.ZERO.compareTo(line.getQtyReimbursed()) == 0 || Env.ZERO.compareTo(line.getPriceReimbursed()) == 0)
                    continue;
                //	Update Header info
                if (line.getC_Activity_ID() != 0 && line.getC_Activity_ID() != invoice.getC_Activity_ID())
                    invoice.setC_Activity_ID(line.getC_Activity_ID());
                if (line.getC_Campaign_ID() != 0 && line.getC_Campaign_ID() != invoice.getC_Campaign_ID())
                    invoice.setC_Campaign_ID(line.getC_Campaign_ID());
                if (line.getC_Project_ID() != 0 && line.getC_Project_ID() != invoice.getC_Project_ID())
                    invoice.setC_Project_ID(line.getC_Project_ID());
                if (!invoice.save())
                    new IllegalStateException("Cannot save Invoice");
                //	Create OrderLine
                MInvoiceLine il = new MInvoiceLine(invoice);
                //
                if (line.getM_Product_ID() != 0)
                    il.setM_Product_ID(line.getM_Product_ID(), true);
                //	Entered/Invoiced
                il.setQty(line.getQtyReimbursed());
                il.setDescription(line.getDescription());
                //
                il.setC_Project_ID(line.getC_Project_ID());
                il.setC_ProjectPhase_ID(line.getC_ProjectPhase_ID());
                il.setC_ProjectTask_ID(line.getC_ProjectTask_ID());
                il.setC_Activity_ID(line.getC_Activity_ID());
                il.setC_Campaign_ID(line.getC_Campaign_ID());
                //
                //	il.setPrice();	//	not really a list/limit price for reimbursements
                //
                il.setPrice(line.getPriceReimbursed());
                il.setTax();
                if (!il.save())
                    new IllegalStateException("Cannot save Invoice Line");
                //	Update TEL
                line.setC_InvoiceLine_ID(il.getC_InvoiceLine_ID());
                line.saveEx();
            }
        //	for all expense lines
        }
    //	********* Expense Line Loop
    } catch (Exception e) {
        log.log(Level.SEVERE, sql.toString(), e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    completeInvoice(invoice);
    return "@Created@=" + m_noInvoices;
}
Also used : MInvoiceLine(org.compiere.model.MInvoiceLine) MInvoice(org.compiere.model.MInvoice) PreparedStatement(java.sql.PreparedStatement) MBPartner(org.compiere.model.MBPartner) ResultSet(java.sql.ResultSet) MTimeExpense(org.compiere.model.MTimeExpense) MTimeExpenseLine(org.compiere.model.MTimeExpenseLine)

Example 2 with MTimeExpense

use of org.compiere.model.MTimeExpense in project adempiere by adempiere.

the class ExpenseSOrder method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message to be translated
	 *  @throws Exception
	 */
protected String doIt() throws java.lang.Exception {
    StringBuffer sql = new StringBuffer("SELECT * FROM S_TimeExpenseLine el " + //	#1
    "WHERE el.AD_Client_ID=?" + //	Business Partner && to be invoiced
    " AND el.C_BPartner_ID>0 AND el.IsInvoiced='Y'" + //	not invoiced yet
    " AND el.C_OrderLine_ID IS NULL" + //	processed only
    " AND EXISTS (SELECT * FROM S_TimeExpense e " + "WHERE el.S_TimeExpense_ID=e.S_TimeExpense_ID AND e.Processed='Y')");
    if (p_C_BPartner_ID != 0)
        //	#2
        sql.append(" AND el.C_BPartner_ID=?");
    if (p_DateFrom != null || m_DateTo != null) {
        sql.append(" AND EXISTS (SELECT * FROM S_TimeExpense e " + "WHERE el.S_TimeExpense_ID=e.S_TimeExpense_ID");
        if (p_DateFrom != null)
            //	#3
            sql.append(" AND e.DateReport >= ?");
        if (m_DateTo != null)
            //	#4
            sql.append(" AND e.DateReport <= ?");
        sql.append(")");
    }
    sql.append(" ORDER BY el.C_BPartner_ID, el.C_Project_ID, el.S_TimeExpense_ID, el.Line");
    //
    MBPartner oldBPartner = null;
    int old_Project_ID = -1;
    MTimeExpense te = null;
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        int par = 1;
        pstmt.setInt(par++, getAD_Client_ID());
        if (p_C_BPartner_ID != 0)
            pstmt.setInt(par++, p_C_BPartner_ID);
        if (p_DateFrom != null)
            pstmt.setTimestamp(par++, p_DateFrom);
        if (m_DateTo != null)
            pstmt.setTimestamp(par++, m_DateTo);
        rs = pstmt.executeQuery();
        while (//	********* Expense Line Loop
        rs.next()) {
            MTimeExpenseLine tel = new MTimeExpenseLine(getCtx(), rs, get_TrxName());
            if (!tel.isInvoiced())
                continue;
            //	New BPartner - New Order
            if (oldBPartner == null || oldBPartner.getC_BPartner_ID() != tel.getC_BPartner_ID()) {
                completeOrder();
                oldBPartner = new MBPartner(getCtx(), tel.getC_BPartner_ID(), get_TrxName());
            }
            //	New Project - New Order
            if (old_Project_ID != tel.getC_Project_ID()) {
                completeOrder();
                old_Project_ID = tel.getC_Project_ID();
            }
            if (te == null || te.getS_TimeExpense_ID() != tel.getS_TimeExpense_ID())
                te = new MTimeExpense(getCtx(), tel.getS_TimeExpense_ID(), get_TrxName());
            //
            processLine(te, tel, oldBPartner);
        }
    //	********* Expense Line Loop
    } catch (Exception e) {
        log.log(Level.SEVERE, sql.toString(), e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    completeOrder();
    return "@Created@=" + m_noOrders;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MBPartner(org.compiere.model.MBPartner) MTimeExpense(org.compiere.model.MTimeExpense) MTimeExpenseLine(org.compiere.model.MTimeExpenseLine)

Example 3 with MTimeExpense

use of org.compiere.model.MTimeExpense in project adempiere by adempiere.

the class WebInfo method getExpenses.

//	getActivity
/**
	 * 	Get Expenses
	 *	@return expense reports
	 */
public ArrayList<MTimeExpense> getExpenses() {
    m_infoMessage = null;
    ArrayList<MTimeExpense> list = new ArrayList<MTimeExpense>();
    String sql = "SELECT * FROM S_TimeExpense " + "WHERE C_BPartner_ID=? " + "ORDER BY Created DESC";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, getC_BPartner_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) list.add(new MTimeExpense(m_ctx, rs, null));
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    log.fine("#" + list.size());
    return list;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MTimeExpense(org.compiere.model.MTimeExpense)

Example 4 with MTimeExpense

use of org.compiere.model.MTimeExpense in project adempiere by adempiere.

the class ProjectIssue method issueExpense.

//	issueReceipt
/**
	 *	Issue Expense Report
	 *	@return Message (clear text)
	 */
private String issueExpense() {
    //	Get Expense Report
    MTimeExpense expense = new MTimeExpense(getCtx(), m_S_TimeExpense_ID, get_TrxName());
    if (!expense.isProcessed())
        throw new IllegalArgumentException("Time+Expense not processed - " + expense);
    //	for all expense lines
    MTimeExpenseLine[] expenseLines = expense.getLines(false);
    int counter = 0;
    for (int i = 0; i < expenseLines.length; i++) {
        //	Need to have a Product
        if (expenseLines[i].getM_Product_ID() == 0)
            continue;
        //	Need to have Quantity
        if (expenseLines[i].getQty() == null || expenseLines[i].getQty().signum() == 0)
            continue;
        //	Need to the same project
        if (expenseLines[i].getC_Project_ID() != m_project.getC_Project_ID())
            continue;
        //	not issued yet
        if (projectIssueHasExpense(expenseLines[i].getS_TimeExpenseLine_ID()))
            continue;
        //	Find Location
        int M_Locator_ID = 0;
        //	MProduct product = new MProduct (getCtx(), expenseLines[i].getM_Product_ID());
        //	if (product.isStocked())
        M_Locator_ID = MStorage.getM_Locator_ID(expense.getM_Warehouse_ID(), //	no ASI
        expenseLines[i].getM_Product_ID(), //	no ASI
        0, expenseLines[i].getQty(), null);
        if (//	Service/Expense - get default (and fallback)
        M_Locator_ID == 0)
            M_Locator_ID = expense.getM_Locator_ID();
        //	Create Issue
        MProjectIssue pi = new MProjectIssue(m_project);
        pi.setMandatory(M_Locator_ID, expenseLines[i].getM_Product_ID(), expenseLines[i].getQty());
        if (//	default today
        m_MovementDate != null)
            pi.setMovementDate(m_MovementDate);
        if (m_Description != null && m_Description.length() > 0)
            pi.setDescription(m_Description);
        else if (expenseLines[i].getDescription() != null)
            pi.setDescription(expenseLines[i].getDescription());
        pi.setS_TimeExpenseLine_ID(expenseLines[i].getS_TimeExpenseLine_ID());
        pi.process();
        //	Find/Create Project Line
        MProjectLine pl = new MProjectLine(m_project);
        //	setIssue
        pl.setMProjectIssue(pi);
        pl.saveEx();
        addLog(pi.getLine(), pi.getMovementDate(), pi.getMovementQty(), null);
        counter++;
    }
    return "@Created@ " + counter;
}
Also used : MProjectIssue(org.compiere.model.MProjectIssue) MTimeExpense(org.compiere.model.MTimeExpense) MTimeExpenseLine(org.compiere.model.MTimeExpenseLine) MProjectLine(org.compiere.model.MProjectLine)

Example 5 with MTimeExpense

use of org.compiere.model.MTimeExpense in project adempiere by adempiere.

the class WebInfo method getExpense.

//	getExpenses
/**
	 * 	Get Expense Report.
	 * 	Needs to have ID set first
	 *	@return invoice of BP with ID
	 */
public MTimeExpense getExpense() {
    m_infoMessage = null;
    MTimeExpense retValue = null;
    String sql = "SELECT * FROM S_TimeExpense WHERE C_BPartner_ID=? AND S_TimeExpense_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, getC_BPartner_ID());
        pstmt.setInt(2, m_id);
        rs = pstmt.executeQuery();
        if (rs.next())
            retValue = new MTimeExpense(m_ctx, rs, null);
    } catch (Exception e) {
        log.log(Level.SEVERE, "S_TimeExpense_ID=" + m_id, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (retValue == null)
        retValue = new MTimeExpense(m_ctx, 0, null);
    log.fine("S_TimeExpense_ID=" + m_id + " - " + retValue);
    return retValue;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MTimeExpense(org.compiere.model.MTimeExpense)

Aggregations

MTimeExpense (org.compiere.model.MTimeExpense)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 MTimeExpenseLine (org.compiere.model.MTimeExpenseLine)3 MBPartner (org.compiere.model.MBPartner)2 ArrayList (java.util.ArrayList)1 MInvoice (org.compiere.model.MInvoice)1 MInvoiceLine (org.compiere.model.MInvoiceLine)1 MProjectIssue (org.compiere.model.MProjectIssue)1 MProjectLine (org.compiere.model.MProjectLine)1