<?php

/**
 * file: users_migrate.php
 *
 * Move Contacts to a User.
 */

class UsersMigrateShell extends Shell {


    var $uses = array('User', 'Contact', 'PrincipleContact', 'PurchaseOrder', 'Enquiry');


    function main() {

	$principleContactsCount = $this->PrincipleContact->find('count');
	$contactsCount = $this->Contact->find('count');
	$contacts = $this->Contact->find('all');

	$usersCount = $this->User->find('count');

	$this->printCountLine($principleContactsCount, 'Principle Contact');
	$this->printCountLine($contactsCount, 'Customer Contacts');
	$this->printCountLine($usersCount, 'Users');

	$totalCount = $principleContactsCount + $contactsCount + $usersCount;
	$this->printCountLine($totalCount, 'Total Users we should have after migration');
	$principleContacts = $this->PrincipleContact->find('all');
	$newPrincipleUsers = array();
	foreach($principleContacts as $contact) {
	    $newUser = array();

	    $newUser['User']['principle_id'] = $contact['PrincipleContact']['principle_id'];
	    $newUser['User']['type'] = 'principle';
	    //   $newUser['User']['username'] = trim($contact['PrincipleContact']['first_name']).trim($contact['PrincipleContact']['last_name']);
	    $newUser['User']['first_name'] = $contact['PrincipleContact']['first_name'];
	    $newUser['User']['last_name'] = $contact['PrincipleContact']['last_name'];
	    $newUser['User']['email'] = $contact['PrincipleContact']['email'];
	    $newUser['User']['phone'] = $contact['PrincipleContact']['phone'];
	    $newUser['User']['fax'] = $contact['PrincipleContact']['fax'];
	    $newUser['User']['notes'] = $contact['PrincipleContact']['notes'];
	    $newUser['User']['job_title'] = $contact['PrincipleContact']['job_title'];
	    $this->User->create();
	    //$newPrincipleUsers[] = $newUser;
	    $this->User->save($newUser);




	}

	$customerContacts = $this->Contact->find('all');
	$newCustomerUsers = array();
	foreach($customerContacts as $contact) {
	    $newUser = array();

	    $oldID = $contact['Contact']['id'];

	    $newUser['User']['customer_id'] = $contact['Contact']['customer_id'];
	    $newUser['User']['type'] = 'contact';
	    // $newUser['User']['username'] = trim($contact['Contact']['first_name']).' '.trim($contact['Contact']['last_name']);
	    $newUser['User']['first_name'] = $contact['Contact']['first_name'];
	    $newUser['User']['last_name'] = $contact['Contact']['last_name'];
	    $newUser['User']['email'] = $contact['Contact']['email'];
	    $newUser['User']['phone'] = $contact['Contact']['phone'];
	    $newUser['User']['fax'] = $contact['Contact']['fax'];
	    $newUser['User']['notes'] = $contact['Contact']['notes'];
	    $newUser['User']['job_title'] = $contact['Contact']['job_title'];
	    $newUser['User']['phone_extension'] = $contact['Contact']['phone_extension'];
	    $newUser['User']['direct_phone'] = $contact['Contact']['direct_phone'];
	    $newUser['User']['mobile'] = $contact['Contact']['mobile'];
	    $this->User->create($newUser);
	    //$newPrincipleUsers[] = $newUser;
	    if($this->User->save($newUser)) {
		$newID = $this->User->getLastInsertID();
		$sql = "UPDATE enquiries SET contact_user_id = {$newID} WHERE contact_id = {$oldID}"; 
		$this->Enquiry->query($sql);
		echo $sql."\n";
	    }
	    else {
		echo "Unable to create new user. {$newUser['User']['email']}\n";
	    }

	}
	$newCount = $this->User->find('count');
	$this->printCountLine($newCount, 'Now have Total Users after migration');

	if($newCount != $totalCount) {
	    echo "Error! We expected to have {$totalCount}. We have {$newCount} \n";
	}
	else {
	    echo "Yay! Sucessfully imported the contacts.\n";
	}

	

	//print_r($newPrincipleUsers);

	//$this->User->save($newPrincipleUsers);

    }


    function printCountLine($count, $model) {
	echo $model ." count:". $count ."\n";
    }

}


?>