public without sharing class NewQuoteDevideController {
    private String oid;
    private String qid;
    private String ot;
    private String cid;
    private String rturl;

    public NewQuoteDevideController(ApexPages.StandardController controller) {
        this();
    }

    public NewQuoteDevideController() {
        oid = System.currentPageReference().getParameters().get('oppid');
        qid = System.currentPageReference().getParameters().get('id');
        ot = System.currentPageReference().getParameters().get('openType');
        cid = System.currentPageReference().getParameters().get('copyid');
        rturl = System.currentPageReference().getParameters().get('retURL');
    }

    public PageReference init() {
        PageReference pr = null;

        String quoId = '';
        String oppId = '';
        if (cid != null && cid.length() > 0) {
            quoId = cid;
        } else if (qid != null && qid.length() > 0) {
            quoId = qid;
        } else if (oid != null && oid.length() > 0) {
            oppId = oid;
        }

        boolean isService = false;
        if (ot == 'service') {
            isService = true;
        } else {
            if (quoId != null && quoId.length() > 0) {
                List<Quote> quoList = [select Id, Quote_Type__c from Quote where Id = :quoId];
                String rt = quoList[0].Quote_Type__c;
                isService = rt == 'service';
            }

            if (oppId != null && oppId.length() > 0) {
                List<Opportunity> oppList = [select Id, RecordTypeId from Opportunity where Id = :oppId];
                String rt = oppList[0].RecordTypeId;
                isService = rt.substring(0, 15) == System.Label.RT_SSBD_Service;
            }
        }

        String cStr = '';
        if (cid != null && cid.length() > 0) {
            cStr = '&copyid=' + cid;
        }
        String rStr = '';
        if (rturl != null && rturl.length() > 0) {
            rStr = '&retURL=' + rturl;
        }
        if (oid != null && oid.length() > 0) {
            if (isService == true) {
                pr = new PageReference('/apex/NewQuoteService?oppid=' + oid + '&openType=service' + cStr + rStr);
            } else {
                pr = new PageReference('/apex/NewQuoteEntry?oppid=' + oid + cStr + rStr);
            }
        } else if (qid != null && qid.length() > 0) {
            if (isService == true) {
                pr = new PageReference('/apex/NewQuoteService?id=' + qid + '&openType=service' + rStr);
            } else {
                pr = new PageReference('/apex/NewQuoteEntry?id=' + qid + rStr);
            }
        }

        pr.setRedirect(true);
        return pr;
    }
}