<?php

class CategoriesList {

    var $id = 0;
    var $category_name = "";
    var $ledger_code = "";
    var $total = 0;
    var $optype = "";
    var $opbalance = 0;
    var $children_groups = array();
    var $children_ledgers = array();
    var $counter = 0;
    public static $temp_max = 0;
    public static $max_depth = 0;
    public static $csv_data = array();
    public static $csv_row = 0;

    function CategoriesList() {
        return;
    }

    function init($id,$year) {
        if ($id == 0) {
            $this->id = 0;
            $this->category_name = "None";
            $this->total = 0;
        } else {
            $result = mysql_query("SELECT * FROM item_categories WHERE id='$id'") or die(mysql_error());
            $array = mysql_fetch_assoc($result);
            $this->id = $array['id'];
            $this->category_name = $array['category_name'];
            $this->total = 0;
        }
        $this->add_sub_ledgers($year);
        $this->add_sub_groups($year);
    }

    function add_sub_groups($year) {
        $result = mysql_query("SELECT * FROM item_categories WHERE parent_id='$this->id'") or die(mysql_error());
        $counter = 0;
        while ($array = mysql_fetch_assoc($result)) {
            $this->children_groups[$counter] = new CategoriesList();
            $this->children_groups[$counter]->init($array['id'],$year);
            $this->total = $this->float_ops($this->total, $this->children_groups[$counter]->total, '+');
            $counter++;
        }
    }

    function add_sub_ledgers($year) {
        $result = mysql_query("SELECT * FROM stock_items WHERE group_id='$this->id'") or die(mysql_error());
        $counter = 0;
        while ($array = mysql_fetch_assoc($result)) {
            $this->children_ledgers[$counter]['id'] = $array['id'];
            $this->children_ledgers[$counter]['item'] = $array['item'];
            $this->children_ledgers[$counter]['ins_item_code'] = $array['ins_item_code'];
            $counter++;
        }
    }

    function account_st_short($c = 0) {
        $this->counter = $c;
        if ($this->id != 0) {
            echo "<tr class=\"tr-group\">";
            echo "<td style='font-size: 14px;background-color: #FFFFE6;' class=\"td-group\">";
            echo $this->print_space($this->counter);
            echo "&nbsp;" . $this->category_name;
            echo "</td>";
            echo "<td style='font-size: 14px;background-color: #FFFFE6;text-align:right' align=\"right\">" . convert_amount_dc($this->total) . $this->print_space($this->counter) . "</td>";
            echo "</tr>";
            if (count($this->children_ledgers) > 0) {
                $this->counter++;
                foreach ($this->children_ledgers as $id => $data) {
                    echo "<tr class=\"tr-ledger\">";
                    echo "<td style='font-size:14px' class=\"td-ledger\">";
                    echo $this->print_space($this->counter); ?>
                    &nbsp; <a title="<?php echo $data['ledger_code'].' : '.$data['item']?> Ledger Statement" href="#" onclick="getstep('view_ledger_statement','<?php echo $data['id'].'@@@'.$data['category_name'];?>')" style="cursor: pointer;text-decoration: underline"> <?php echo $data['ledger_code'].' : '.$data['item']?> </a>
            <?php   echo "</td>";
                    echo "<td style='font-size:14px;text-align:right' align=\"right\">" . convert_amount_dc($data['total']) . $this->print_space($this->counter) . "</td>";
                    echo "</tr>";
                }
                $this->counter--;
            }
        }
        foreach ($this->children_groups as $id => $data) {
            $this->counter++;
            $data->account_st_short($this->counter);
            $this->counter--;
        }
    }
    
    /* Display chart of accounts view */

    function account_st_main($c = 0) {

        $this->counter = $c;
        if ($this->id != 0) { ?>
            <tr style="cursor:pointer;" onclick="$('.item_category_<?php echo $this->id;?>').slideToggle();" class="tr-group">
            <?php
            echo "<td style='font-size: 14px;background-color: #FFFFE6;' class=\"td-group\">";
            echo $this->print_space($this->counter);
            echo "<i style='font-weight:normal' class='fa fa-plus'></i> ";
            if ($this->id <= 4)
                echo "&nbsp;<strong>" . $this->category_name . "</strong>";
            else
                echo "&nbsp;" . $this->category_name;
            echo "</td>";
            echo "<td style='font-size: 14px;background-color: #FFFFE6;'>Category Name</td>";

            if ($this->id <= 4) {
                echo "<td style='font-size: 14px;background-color: #FFFFE6;' class=\"td-actions\"></tr>";
            } else { ?>
            <td style='font-size: 14px;background-color: #FFFFE6;'> 
            </td>           
       <?php     }
            echo "</tr>";
            if (count($this->children_ledgers) > 0) {
                $this->counter++;
                foreach ($this->children_ledgers as $id => $data) {
                    echo "<tr style='' class='item_category_$this->id';>";
                    echo "<td style='font-size: 14px;'>";
                    echo $this->print_space($this->counter); ?>
                  &nbsp; <a title="<?php echo $data['ins_item_code'].' : '.$data['item']?> Item Details" href="#" onclick="getpage('edit_item_details_procurement','','','<?php echo $data['id'] ?>','','');" style="cursor: pointer;text-decoration: underline"> <?php echo $data['ins_item_code'].' : '.$data['item']?> </a>
                 <?php 
                    echo "</td>";
                    echo "<td style='font-size: 14px;'>Item Name</td>";?>
                   <td style='font-size: 14px;'>
                   </td> 
               <?php   echo "</tr>";
               }
                $this->counter--;
            }
        }
        foreach ($this->children_groups as $id => $data) {
            $this->counter++;
            $data->account_st_main($this->counter);
            $this->counter--;
        }
    }

    function print_space($count) {
        $html = "";
        for ($i = 1; $i <= $count; $i++) {
            $html .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        return $html;
    }


function float_ops($param1 = 0, $param2 = 0, $op = '') {
    $result = 0;
    $param1 = $param1 * 100;
    $param2 = $param2 * 100;
    $param1 = (int) round($param1, 0);
    $param2 = (int) round($param2, 0);
    switch ($op) {
        case '+':
            $result = $param1 + $param2;
            break;
        case '-':
            $result = $param1 - $param2;
            break;
        case '==':
            if ($param1 == $param2)
                return TRUE;
            else
                return FALSE;
            break;
        case '!=':
            if ($param1 != $param2)
                return TRUE;
            else
                return FALSE;
            break;
        case '<':
            if ($param1 < $param2)
                return TRUE;
            else
                return FALSE;
            break;
        case '>':
            if ($param1 > $param2)
                return TRUE;
            else
                return FALSE;
            break;
    }
    $result = $result / 100;
    return $result;
}


    /*
     * Return a array of sub ledgers with the object
     * Used in CF ledgers of type Assets and Liabilities
     */

   

}
