Search in sources :

Example 1 with MProjectTask

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

the class ProjectPhaseGenOrder method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (clear text)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    m_C_ProjectPhase_ID = getRecord_ID();
    log.info("doIt - C_ProjectPhase_ID=" + m_C_ProjectPhase_ID);
    if (m_C_ProjectPhase_ID == 0)
        throw new IllegalArgumentException("C_ProjectPhase_ID == 0");
    MProjectPhase fromPhase = new MProjectPhase(getCtx(), m_C_ProjectPhase_ID, get_TrxName());
    MProject fromProject = ProjectGenOrder.getProject(getCtx(), fromPhase.getC_Project_ID(), get_TrxName());
    MOrder order = new MOrder(fromProject, true, MOrder.DocSubTypeSO_OnCredit);
    order.setDescription(order.getDescription() + " - " + fromPhase.getName());
    if (!order.save())
        throw new Exception("Could not create Order");
    //	Create an order on Phase Level
    if (fromPhase.getM_Product_ID() != 0) {
        MOrderLine ol = new MOrderLine(order);
        ol.setLine(fromPhase.getSeqNo());
        StringBuffer sb = new StringBuffer(fromPhase.getName());
        if (fromPhase.getDescription() != null && fromPhase.getDescription().length() > 0)
            sb.append(" - ").append(fromPhase.getDescription());
        ol.setDescription(sb.toString());
        //
        ol.setM_Product_ID(fromPhase.getM_Product_ID(), true);
        ol.setQty(fromPhase.getQty());
        ol.setPrice();
        if (fromPhase.getPriceActual() != null && fromPhase.getPriceActual().compareTo(Env.ZERO) != 0)
            ol.setPrice(fromPhase.getPriceActual());
        ol.setTax();
        if (!ol.save())
            log.log(Level.SEVERE, "doIt - Lines not generated");
        return "@C_Order_ID@ " + order.getDocumentNo() + " (1)";
    }
    //	Project Phase Lines
    int count = 0;
    MProjectLine[] lines = fromPhase.getLines();
    for (int i = 0; i < lines.length; i++) {
        MOrderLine ol = new MOrderLine(order);
        ol.setLine(lines[i].getLine());
        ol.setDescription(lines[i].getDescription());
        //
        ol.setM_Product_ID(lines[i].getM_Product_ID(), true);
        ol.setQty(lines[i].getPlannedQty().subtract(lines[i].getInvoicedQty()));
        ol.setPrice();
        if (lines[i].getPlannedPrice() != null && lines[i].getPlannedPrice().compareTo(Env.ZERO) != 0)
            ol.setPrice(lines[i].getPlannedPrice());
        ol.setDiscount();
        ol.setTax();
        if (ol.save())
            count++;
    }
    //	for all lines
    if (lines.length != count)
        log.log(Level.SEVERE, "Lines difference - ProjectLines=" + lines.length + " <> Saved=" + count);
    //	Project Tasks
    MProjectTask[] tasks = fromPhase.getTasks();
    for (int i = 0; i < tasks.length; i++) {
        MOrderLine ol = new MOrderLine(order);
        ol.setLine(tasks[i].getSeqNo());
        StringBuffer sb = new StringBuffer(tasks[i].getName());
        if (tasks[i].getDescription() != null && tasks[i].getDescription().length() > 0)
            sb.append(" - ").append(tasks[i].getDescription());
        ol.setDescription(sb.toString());
        //
        ol.setM_Product_ID(tasks[i].getM_Product_ID(), true);
        ol.setQty(tasks[i].getQty());
        ol.setPrice();
        ol.setTax();
        if (ol.save())
            count++;
    }
    //	for all lines
    if (tasks.length != count - lines.length)
        log.log(Level.SEVERE, "doIt - Lines difference - ProjectTasks=" + tasks.length + " <> Saved=" + count);
    return "@C_Order_ID@ " + order.getDocumentNo() + " (" + count + ")";
}
Also used : MOrder(org.compiere.model.MOrder) MProjectTask(org.compiere.model.MProjectTask) MProjectPhase(org.compiere.model.MProjectPhase) MOrderLine(org.compiere.model.MOrderLine) MProject(org.compiere.model.MProject) MProjectLine(org.compiere.model.MProjectLine)

Aggregations

MOrder (org.compiere.model.MOrder)1 MOrderLine (org.compiere.model.MOrderLine)1 MProject (org.compiere.model.MProject)1 MProjectLine (org.compiere.model.MProjectLine)1 MProjectPhase (org.compiere.model.MProjectPhase)1 MProjectTask (org.compiere.model.MProjectTask)1