<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Maintenance_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }
// UPDATE LEASE,AVERAGE,MAXIMUM AND LAST PRICES AS STANDARD PRICE   HUSSAIN 04/03/2012
    function ticketsupdate($cutt, $receipts, $money) {

//     UPDATE TICKETLINES TL JOIN TICKETS T JOIN RECEIPTS R ON R.ID=T.ID AND TL.TICKET=T.ID
//SET UNITS = UNITS - UNITS*50/100
//WHERE R.MONEY = 'c5c45d9d-fb19-4068-a3be-e4d5dbf9b61b' AND T.TICKETID NOT IN(2176, 1851, 2554, 1917);

//         $this->db->set('UNITS', 'UNITS-(UNITS*' . $cutt/100 . ')', false);
////        $this->db->where('R.MONEY' ,'c5c45d9d-fb19-4068-a3be-e4d5dbf9b61b');
//        $this->db->join('TICKETS AS T', 'TICKETLINES.TICKET=T.ID', 'LEFT');
//        $this->db->join('RECEIPTS AS R', 'R.ID=T.ID AND R.MONEY="c5c45d9d-fb19-4068-a3be-e4d5dbf9b61b"', 'LEFT');
//        return ($this->db->update('TICKETLINES')) ? true : false;
//. '" AND T.TICKETID NOT IN(' . $receipts . ')'
     $this->db->trans_begin();

   $query1='UPDATE TICKETLINES TL JOIN TICKETS T JOIN RECEIPTS R ON R.ID=T.ID AND TL.TICKET=T.ID ' .
     'SET UNITS = UNITS - UNITS*' . $cutt/100 . 'WHERE R.MONEY =  "' . $money . '"';

   $query2='UPDATE PAYMENTS P JOIN RECEIPTS R JOIN TICKETS T  ON R.ID=P.RECEIPT AND R.ID=T.ID
                  SET TOTAL = TOTAL - TOTAL*' . $cutt/100 . 'WHERE R.MONEY = "' . $money . '"';
   if($receipts)
   {
       $query1=$query1 . ' AND T.TICKETID NOT IN(' . $receipts . ')';
       $query2=$query2 . ' AND T.TICKETID NOT IN(' . $receipts . ')';
   }


$this->db->query($query1);
$this->db->query($query2);


if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
}
else
{
    $this->db->trans_commit();
}

    }

}