import com.rameses.rcp.annotations.*
import com.rameses.rcp.common.*
import com.rameses.osiris2.client.*
import com.rameses.osiris2.common.*
import java.rmi.server.*;
import com.rameses.util.*;

public class BusinessApplicationUtil {

    @Service("BusinessApplicationService")
    def service;

    @Service("BusinessPermitTypeService")
    def permitTypeSvc;

    @Caller 
    def caller;

    def _permitType;
    def permitTypes;
    def apptypes = ['NEW','RENEW'];
    def officeTypes = ['MAIN', 'BRANCH'];

    def _apptype;
    public void setApptype(o) {
        _apptype = o;
       if(caller.entity) caller.entity.apptype = o;
    }
    public def getApptype() {
        return _apptype;
    }

    public void load() {
        permitTypes = permitTypeSvc.getList();
    }

    public void init(def o) {
        def entity = service.initNew( o );
        load();
        entity.yearstarted = entity.appyear;
        caller.entity = entity;
        if( entity.permittype ) {
            _permitType = permitTypes.find{ it.objid == entity.permittype };
        }    
        if( entity.business?.permittype ) {
            _permitType = permitTypes.find{ it.objid == entity.business.permittype };
        }    
    }
    
    void save() {
        def o = caller.entity;
        if( _permitType == null ) {
            _permitType = [objid: 'BUSINESS' ];
        }
        o.business.permittype = _permitType.objid;
        if ( !o.appyear ) o.appyear = o.activeyear; 
        if ( !o.objid ) o.objid = 'BAPP'+ new UID(); 

        caller.entity = service.create( o );
    }

    def create(def o) {
        return service.create( o );
    }
            
    def print() {
        def entity = caller.entity;
        def op = Inv.lookupOpener( "business_application:reports", [entity: entity] );
        op.target = 'self';
        return op;
    }

    void copyName() {
        def entity = caller.entity;
        entity.business.tradename = entity.business.businessname;
        caller.binding.refresh("entity.business.tradename");
        caller.binding.focus("entity.business.tradename");
    }

    void reset() {;}

    void release() {
        caller.entity = service.release(caller.entity);
    }

    void cancel() {
        if( MsgBox.confirm('You are about to cancel this application. Continue?')) {
            caller.entity = service.cancel(caller.entity);
        }
    }

    void open() { 
        def data = service.open( caller.entity ); 
        if ( caller.entity ) {
            caller.entity.clear(); 
            caller.entity.putAll( data ); 
        } else {
            caller.entity = data; 
        } 
    }

    public void setPermitType(def o) {
        _permitType = o;
        caller.entity.business.permittype = o.objid;
    }

    public def getPermitType() {
        return _permitType;
    }

    public def getPermitTypeOptions() {
        if( !_permitType ) return [:]; 
        return _permitType.options;
    }

    public def getAddressType() {
        return permitTypeOptions.address_type;
    }

    public def getCopyOwnerAddress() {
        def t = permitTypeOptions.copy_owner_address;
        if( !t) return false;
        return t;
    }

    public def getPermitTypeTitle() {
        return _permitType.title;
    }

    public void updateInfos() {
        def o = caller.entity;
        def b = [businessid: o.business.objid, activeyear: o.appyear, applicationid: o.objid];
        b.lobs = o.lobs;
        b.appinfos = o.appinfos;
        b.requirements = o.requirements;
        service.updateInfos(b);
    }
}