diff --git a/include/class.lock.php b/include/class.lock.php
index d6bcbad9dc66105803fbb49a267252d068423242..c61771e67f5a3b166734e41d8c6ee3ebd03c977c 100644
--- a/include/class.lock.php
+++ b/include/class.lock.php
@@ -18,139 +18,130 @@
  * Mainly used as a helper...
  */
 
-class TicketLock {
-    var $id;
-    var $ht;
-    
-    function TicketLock($id, $tid=0) {
-        $this->id=0;
-        $this->load($id, $tid);
-    }
-
-    function load($id=0, $tid=0) {
-
-        if(!$id && $this->ht['id'])
-            $id=$this->ht['id'];
-
-        $sql='SELECT l.*, TIME_TO_SEC(TIMEDIFF(expire,NOW())) as timeleft '
-            .' ,IF(s.staff_id IS NULL,"staff",CONCAT_WS(" ", s.lastname, s.firstname)) as staff '
-            .' FROM '.TICKET_LOCK_TABLE. ' l '
-            .' LEFT JOIN '.STAFF_TABLE.' s ON(s.staff_id=l.staff_id) '
-            .' WHERE lock_id='.db_input($id);
+class TicketLock extends VerySimpleModel {
 
-        if($tid) 
-            $sql.=' AND ticket_id='.db_input($tid);
+    static $meta = array(
+        'table' => TICKET_LOCK_TABLE,
+        'pk' => array('lock_id'),
+        'joins' => array(
+            'ticket' => array(
+                'constraint' => array('ticket_id' => 'TicketModel.ticket_id'),
+            ),
+            'staff' => array(
+                'constraint' => array('staff_id' => 'Staff.staff_id'),
+            ),
+        ),
+    );
 
-        if(!($res=db_query($sql)) || !db_num_rows($res))
-            return false;
+    var $expiretime;
 
-        $this->ht=db_fetch_array($res);
-        $this->id=$this->ht['id']=$this->ht['lock_id'];
-        $this->ht['expiretime']=time()+$this->ht['timeleft'];
-        
-        return true;
-    }
-  
-    function reload() {
-        return $this->load();
+    function __onload() {
+        if (isset($this->expire))
+            $this->expiretime = strtotime($this->expire);
     }
 
     function getId() {
-        return $this->id;
+        return $this->lock_id;
     }
 
     function getStaffId() {
-        return $this->ht['staff_id'];
+        return $this->staff_id;
     }
 
     function getStaffName() {
-        return $this->ht['staff'];
+        return $this->staff->getName();
     }
 
     function getCreateTime() {
-        return $this->ht['created'];
+        return $this->created;
     }
 
     function getExpireTime() {
-        return $this->ht['expire'];
+        return $this->expire;
     }
     //Get remaiming time before the lock expires
     function getTime() {
-        return $this->isExpired()?0:($this->ht['expiretime']-time());
+        return $this->isExpired()?0:($this->expiretime-time());
     }
 
     //Should we be doing realtime check here? (Ans: not really....expiretime is local & based on loadtime)
     function isExpired() {
-        return (time()>$this->ht['expiretime']);
+        return (time()>$this->expiretime);
     }
-   
+
     //Renew existing lock.
     function renew($lockTime=0) {
 
         if(!$lockTime || !is_numeric($lockTime)) //XXX: test to  make it works.
-            $lockTime = '(TIME_TO_SEC(TIMEDIFF(expire,created))/60)';
-            
-
-        $sql='UPDATE '.TICKET_LOCK_TABLE
-            .' SET expire=DATE_ADD(NOW(),INTERVAL '.$lockTime.' MINUTE) '
-            .' WHERE lock_id='.db_input($this->getId());
-        //echo $sql;
-        if(!db_query($sql) || !db_affected_rows())
-            return false;
-        
-        $this->reload();
-        
-        return true;
+            $lockTime = $cfg->getLockTime();
+
+        $this->expire = SqlExpression::plus(
+            SqlFunction::NOW(),
+            SqlInterval::MINUTE($lockTime)
+        );
+        return $this->save();
     }
 
     //release aka delete a lock.
     function release() {
-        //FORCED release - we don't give a ....
-        $sql='DELETE FROM '.TICKET_LOCK_TABLE.' WHERE lock_id='.db_input($this->getId()).' LIMIT 1';
-        return (db_query($sql) && db_affected_rows());
+        return $this->delete();
     }
 
     /* ----------------------- Static functions ---------------------------*/
-    function lookup($id, $tid) {
-        return ($id  && ($lock = new TicketLock($id,$tid)) && $lock->getId()==$id)?$lock:null;
+    static function lookup($id, $tid=false) {
+        if ($tid)
+            return parent::lookup(array('lock_id' => $id, 'ticket_id' => $tid));
+        else
+            return parent::lookup($id);
     }
 
-    //Create a ticket lock...this function assumes the caller checked for access & validity of ticket & staff x-ship.    
-    function acquire($ticketId, $staffId, $lockTime) {
+    //Create a ticket lock...this function assumes the caller checked for access & validity of ticket & staff x-ship.
+    static function acquire($ticketId, $staffId, $lockTime) {
 
-        if(!$ticketId or !$staffId or !$lockTime)
+        if (!$ticketId or !$staffId or !$lockTime)
             return 0;
 
-
-        //Cleanup any expired locks on the ticket.
-        db_query('DELETE FROM '.TICKET_LOCK_TABLE.' WHERE ticket_id='.db_input($ticketId).' AND expire<NOW()');
-        //create the new lock.
-        $sql='INSERT IGNORE INTO '.TICKET_LOCK_TABLE.' SET created=NOW() '
-            .',ticket_id='.db_input($ticketId)
-            .',staff_id='.db_input($staffId)
-            .',expire=DATE_ADD(NOW(),INTERVAL '.$lockTime.' MINUTE) ';
-
-        return db_query($sql)?db_insert_id():0;
-    }
-
-    function create($ticketId, $staffId, $lockTime) {
-        if(($id=self::acquire($ticketId, $staffId, $lockTime)))
-            return self::lookup($id);
-    }
-
-    //Simply remove ALL locks a user (staff) holds on a ticket(s).
-    function removeStaffLocks($staffId, $ticketId=0) {
-        $sql='DELETE FROM '.TICKET_LOCK_TABLE.' WHERE staff_id='.db_input($staffId);
-        if($ticketId)
-            $sql.=' AND ticket_id='.db_input($ticketId);
-
-        return db_query($sql);
-    }
-
-    //Called  via cron
-    function cleanup() {
-        //Cleanup any expired locks.
-        db_query('DELETE FROM '.TICKET_LOCK_TABLE.' WHERE expire<NOW()');
+        // Cleanup any expired locks on the ticket.
+        static::objects()->filter(array(
+            'ticket_id' => $ticketId,
+            'expire__lt' => SqlFunction::NOW()
+        ))->delete();
+
+        // Create the new lock.
+        $lock = parent::create(array(
+            'created' => SqlFunction::NOW(),
+            'ticket_id' => $ticketId,
+            'staff_id' => $staffId,
+            'expire' => SqlExpression::plus(
+                SqlFunction::NOW(),
+                SqlInterval::MINUTE($lockTime)
+            ),
+        ));
+        if ($lock->save(true))
+            return $lock;
+    }
+
+    static function create($ticketId, $staffId, $lockTime) {
+        if ($lock = self::acquire($ticketId, $staffId, $lockTime))
+            return $lock;
+    }
+
+    // Simply remove ALL locks a user (staff) holds on a ticket(s).
+    static function removeStaffLocks($staffId, $ticketId=0) {
+        $locks = static::objects()->filter(array(
+            'staff_id' => $staffId,
+        ));
+        if ($ticketId)
+            $locks->filter(array('ticket_id' => $ticketId));
+
+        return $locks->delete();
+    }
+
+    // Called via cron
+    static function cleanup() {
+        static::objects()->filter(array(
+            'expire__lt' => SqlFunction::NOW()
+        ))->delete();
     }
 }
 ?>
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 807ea26819b0299bd2f630086363a969cd04f0cb..e21b1bc46cacb269fb9b5befe0cee12e82e065d1 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -166,7 +166,7 @@ class Ticket {
     }
 
     function isLocked() {
-        return ($this->getLockId());
+        return null !== $this->getLock();
     }
 
     function checkStaffAccess($staff) {
@@ -393,15 +393,7 @@ class Ticket {
         return $info;
     }
 
-    function getLockId() {
-        return $this->ht['lock_id'];
-    }
-
     function getLock() {
-
-        if(!$this->tlock && $this->getLockId())
-            $this->tlock= TicketLock::lookup($this->getLockId(), $this->getId());
-
         return $this->tlock;
     }
 
@@ -421,10 +413,9 @@ class Ticket {
             return $lock;
         }
         //No lock on the ticket or it is expired
-        $this->tlock = null; //clear crap
-        $this->ht['lock_id'] = TicketLock::acquire($this->getId(), $staffId, $lockTime); //Create a new lock..
+        $this->tlock = TicketLock::acquire($this->getId(), $staffId, $lockTime); //Create a new lock..
         //load and return the newly created lock if any!
-        return $this->getLock();
+        return $this->tlock;
     }
 
     function getDept() {