Apmgr / application / modules / unit / models / LeaseWizard.php

<?php
/**
 * Created on Mar 5, 2010 by rnelson
 * @name apmgr
 * @package application.modules.unit.models
 * <p>
 * Model to encapsulate wizard actions since lease contacts multiple models
 * Initially was gonna be apart from a table, but had to save to db in order
 * to access the wizard object (had issue with the registry retrieving the wizard obj)
 * </p>
 */
	
	
class Unit_Model_LeaseWizard extends ZFModel_ParentModel {        
    
    //protected $wizardVariables;
    
    protected $effectiveDate;
    protected $unitId;
    protected $modelRentScheduleId;
    protected $modelRentScheduleItemId;
    protected $discount;
    protected $tenet;
    
    protected $accountLinkId;
    protected $referenceNumber;
    protected $comment;
    
    public function __construct(array $options = null) {                    
        parent::__construct( $options );                       
        $this->setDbTable('Unit_Model_DbTable_LeaseWizard'); 
    }   	
    
    public function getEffectiveDate(){
	return $this->effectiveDate;
    }
    public function setEffectiveDate( $var ){
	$this->effectiveDate = $var;
    } 
    
    public function setUnitId( $var ){
	$this->unitId = $var;
    }  
    public function getUnitId(){
	return $this->unitId;
    }   
    
    public function setModelRentScheduleId( $var ){
	$this->modelRentScheduleId = $var;
    }  
    public function getModelRentScheduleId(){
	return $this->modelRentScheduleId;
    }      
    
    public function setModelRentScheduleItemId( $var ){
	$this->modelRentScheduleItemId = $var;
    }  
    public function getModelRentScheduleItemId(){
	return $this->modelRentScheduleItemId;
    }
    
    public function getModelRentScheduleItemObj(){
	$item = new Unit_Model_ModelRentScheduleItem();	
	$scheduleItem = $item->findById( $this->getModelRentScheduleItemId() );	
	return $scheduleItem;
    }
    
    public function getUnit(){
	$item = new Unit_Model_Unit();	
	return $item->findById( $this->getUnitId() );		
    }
    
    public function getAccountLink(){
	$item = new Financial_Model_AccountLink();	
	return $item->findById( $this->getAccountLinkId() );		
    }   
    
    public function setDiscount( $var ){
	$temp=$var;
	
	if( is_array($var) )
	    $temp = serialize( $var );	    
	        	
	$this->discount = $temp;
    }  
    
    public function getDiscount(){	
	return unserialize( $this->discount );	
    }
    
    // passes in a user object 
    public function setTenet( $var ){
	$temp=$var;
	
	if( is_object($var) ) {
	    $tenetArray = $this->getTenet();	 
	    $name = $var->getFirstName() . ' ' . $var->getLastName();
	    $tenetArray[$var->getId()] = $name;
	    $temp = serialize( $tenetArray );	    
	}        	
	$this->tenet = $temp;
    }
    
    /**
     *  Used for removing tenets from list in lease wizard
     */
    public function removeTenet( $tenetId ){	
	$tenetArray = $this->getTenet();	 	
	unset($tenetArray[$tenetId]);
	$this->tenet = serialize( $tenetArray );
    }
    
    public function getTenet( $format=false ){
	if($format){
	    $temp = unserialize( $this->tenet );
	    $return=array();
	    foreach( $temp as $id=>$name )
	        $return[] = array('id'=>$id, 'name'=>$name );
	    return $return;	
	}	    
	else
	    return unserialize( $this->tenet );	
    }
    
    
    public function setAccountLinkId( $var ){
	$this->accountLinkId = $var;
    }  
    public function getAccountLinkId(){
	return $this->accountLinkId;
    }
    
    public function setComment( $var ){
	$this->comment = $var;
    }  
    public function getComment(){
	return $this->comment;
    }
    
    public function setReferenceNumber( $var ){
	$this->referenceNumber = $var;
    }  
    public function getReferenceNumber(){
	return $this->referenceNumber;
    }
    
    /**
     *  Used on the first page of the lease wizard.  I couldn't think of any other place that the most recent
     *  module schedules via unitId other than in the lease wizard.  The others would merely pull from model
     *  or display the existing lease schedule already attached to a unit which is a seperate model and table.
     */
    function  fetchLatestScheduleByUnitId()   {
	$unit = new Unit_Model_Unit();
	$unit = $unit->findById( $this->getUnitId() );	
	
        $rentSchedule = new Unit_Model_ModelRentSchedule();	
	$rentSchedule->setEffectiveDate( $this->getEffectiveDate() );
	$latestSchedule = $rentSchedule->getLatestSchedule( $unit->getUnitModelId() );
	
	return $latestSchedule;
    }     
    
    /**
     * Format vars for the lease model save function
     */
    function createLease() {
	// tenets
		
	$db = Zend_Registry::get('db'); // used for all in transaction		
	
	/* lease
	   lease schedule
	   tenet
	   create bills - manipulate accounting - date posted
	                  reverse accounting on bill cancellation   
	*/	
	
	try {
	    $db->beginTransaction();	    
	    // set all to use the same db adapter so the transaction can be rolled back if smtg fails
	    
	    $formValues['dbAdapter'] = $db;
	    
	    // TODO:  make little functions for this, maybe	    
	    // grab the numMonths on the rentschedule
	    $rsi = new Unit_Model_ModelRentScheduleItem($formValues);	    
	    $rentScheduleItem = $rsi->findById( $this->getModelRentScheduleItemId() );    
	    
	    
	    
	    // Set End Date
	    $months = $rentScheduleItem->getNumMonths();
	    $endDate = new Zend_Date($this->getEffectiveDate(),'YYYY-MM-dd' );
	    $endDate->add( $months, Zend_Date::MONTH);
	    //var_dump( $endDate->toString('YYYY-MM-dd') );
	    $endDate->sub( 1, Zend_Date::DAY);
	    //var_dump( $endDate->toString('YYYY-MM-dd') );
	    //die;
	
	    // Set and save to lease table
	    $lease = new Unit_Model_Lease( $formValues );
	    $lease->setEffectiveDate( $this->getEffectiveDate() );
	    $lease->setUnitId( $this->getUnitId() );
	    $lease->setModelRentScheduleId( $this->getModelRentScheduleId()  );
	    $lease->setLastDay( $endDate->toString('YYYY-MM-dd') );
		
	    $billCreation = new Financial_Model_BillCreation( array( 'db'=>$db ) );	
	    $billModel = $billCreation->getBillModel();	
	    // TODO:  Make sure this shit is localized - damn you zend_date		
	    
	    
	    $leaseId = $lease->save();
	    
	    // ties bill to lease schedule item
	    $billLeaseModel = new Financial_Model_BillLease( array( 'db'=>$db ) );	    
	    $billLeaseModel->setLeaseId( $leaseId );
	    
	    // Set and save discounts to leaseDiscount table
	    $leaseSchedule = new Unit_Model_LeaseSchedule( $formValues );
	    $leaseSchedule->setLeaseId( $leaseId );    	    
	    
	    // set discounts
	    $discount = $this->getDiscount();
	   	    
	    // save tenets	    
	    $tenetModel = new Unit_Model_Tenet($formValues);
	    $tenetModel->setLeaseId( $leaseId );
	   
	    foreach( $this->getTenet() as $id=>$user ) {
		$tenetModel->setUserId( $id );
		$tenetModel->save();
	    }    	    	    	    	           	      
	     
	    $rent = $rentScheduleItem->getRentAmount();    	        
	    
	    // set common bill info
	    $billModel->setAccountLinkId( $this->getAccountLinkId() );
	    
	    // set transaction info
	    $transactionModel = $billCreation->getTransactionModel();
	    $transactionModel->setAction('lease creation');
	    $transactionModel->setComment( $this->getComment());	    
	    
	    // set account transaction info
	    $accountTransactionModel = $billCreation->getAccountTransactionModel();
	    $accountTransactionModel->setReferenceNumber( $this->getReferenceNumber() );
	    $accountTransactionModel->setAccountLink( $this->getAccountLink() );	    
	    	    	    	    	        
	    for ( $i=1;  $i <= $rentScheduleItem->getNumMonths(); $i++ ) {
		// amount from rent model - discount = original amount due
		$disc = ( isset( $discount[$i] ) )? $discount[$i] : 0;
		$amountDue = $rent - $disc;				
		$billModel->setOriginalAmountDue( $amountDue );
		
		// set accountTransacton amounts - currently does not throw the discount into it's own pool but this can easily be added.
		$accountTransactionModel->setAmount( $amountDue );
		
		//  Second param needed or else 2010-05-1 is being read as Jan 5 2010 instead of May 1 2010
		$date = new Zend_Date($this->getEffectiveDate(),'YYYY-MM-dd' );
		//Zend_Date::setOptions(array('format_type' => 'php'));
		//$date =  new Zend_Date($this->getWizardVariable( 'effectiveDate' ), null,Zend_Registry::get('Zend_Locale'));					
		//var_dump( $date ); die;
		$date->add( $i, Zend_Date::MONTH);				
		
		// TODO:  Make sure this shit is localized - damn you zend_date		
		$billModel->setDueDate( $date->toString('YYYY-MM-dd') );
		$accountTransactionModel->setDatePosted( $date->toString('YYYY-MM-dd') );		
		
		// save lease schedule item
		$leaseSchedule->setMonth( $i );
		$leaseSchedule->setDiscount( $disc );
		$leaseScheduleItemId = $leaseSchedule->save();	
		
		// create bill
	        $billId = $billCreation->createBill();
		
		// TODO add to billLease table
		$billLeaseModel->setBillId( $billId );
		$billLeaseModel->setLeaseScheduleId( $leaseScheduleItemId );		
		$billLeaseModel->save();
	    } 
	    $db->commit();
	}
	catch ( Exception $e) {	    
	    $db->rollBack();	    
	    echo $e->getMessage();
	}
    }
}
?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.