<?php

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

class Deletetransactions_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

function ticketsupdate($cutt, $receipts, $money,$product,$Categorytotal, $proinc, $catinc) {

$this->db->trans_begin();



// DELETE ALL OPEN ITEMS
$query4='DELETE FROM TICKETLINES WHERE TICKET IN (SELECT ID FROM RECEIPTS WHERE MONEY="' . $money . '") AND PRODUCT IS NULL';
$this->db->query($query4);

$query1='UPDATE TICKETLINES TL JOIN TICKETS T JOIN RECEIPTS R ON R.ID=T.ID  JOIN PRODUCTS PR ON PR.ID=TL.PRODUCT JOIN CATEGORIES C ON PR.CATEGORY=C.ID AND TL.TICKET=T.ID 
 SET TL.UNITS = TL.UNITS - FLOOR(TL.UNITS * ' . $cutt/100 . ') WHERE R.MONEY =  "' . $money . '" ';

$query2='UPDATE PAYMENTS P  JOIN (SELECT TL.TICKET, sum(TL.PRICE * TL.UNITS) AS TLSUM FROM TICKETLINES TL GROUP BY TL.TICKET) AS RSLT ON P.RECEIPT=RSLT.TICKET SET P.TOTAL=RSLT.TLSUM';

if($product)
   {
    if($proinc=="1")   
        $query1=$query1 . ' AND PR.NAME IN(' . $product . ')';
    else
        $query1=$query1 . ' AND PR.NAME NOT IN(' . $product . ')';
   }

   if($Categorytotal)
   {
       if($catinc=="1")
        $query1=$query1 . ' AND C.NAME IN(' . $Categorytotal . ')';
       else
        $query1=$query1 . ' AND C.NAME NOT IN(' . $Categorytotal . ')';
   }

 if($receipts)
   {
       $query1=$query1 . ' 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();
}

    }

}