<?php
class saimod_webcraft_billing_satelite extends \SYSTEM\SAI\SaiModule {
    public static function sai_mod_saimod_webcraft_billing_satelite(){
        $token = \SYSTEM\CONFIG\config::get(config_ids::WEBCRAFT_BILLING_TOKEN);
        $billing = json_decode(file_get_contents('http://mojotrollz.eu/web/webcraft/api.php?call=billing&action=billing&token='.$token),true);
        $project = json_decode(file_get_contents('http://mojotrollz.eu/web/webcraft/api.php?call=billing&action=project&token='.$token),true);
        $customer = json_decode(file_get_contents('http://mojotrollz.eu/web/webcraft/api.php?call=billing&action=customer&token='.$token),true);
        
        $vars = array('content_project' => '');
        if($project['status']){
            foreach($project['result'] as $row){                
                $row['customer_company'] = $row['customer_company'] == "" ? $row['customer_company'] = "" : $row['customer_company'] .= " - ";
                $vars['content_project'] .= \SYSTEM\PAGE\replace::replaceFile(\SYSTEM\SERVERPATH(new PSAI(),'saimod_webcraft_billing_satelite/saimod_webcraft_billing_satelite_content_project.tpl'), $row);}
        }
        
        $vars['content_billing'] = '';
        if($billing['status']){
            foreach($billing['result'] as $row){
                $row['pdf_link'] = './sai.php?sai_mod=saimod_webcraft_billing_satelite&action=getpdf&billid='.$row['ID'];
                $row['state'] = $row['payed_date'] == "0000-00-00 00:00:00"  ? "important" : "success";
                $vars['content_billing'] .= \SYSTEM\PAGE\replace::replaceFile(\SYSTEM\SERVERPATH(new PSAI(),'saimod_webcraft_billing_satelite/saimod_webcraft_billing_satelite_content_billing.tpl'), $row);}
        }        
        
        if($customer['status']){
            $vars =  array_merge($vars,$customer['result']);}        
        return \SYSTEM\PAGE\replace::replaceFile(\SYSTEM\SERVERPATH(new PSAI(),'saimod_webcraft_billing_satelite/saimod_webcraft_billing_satelite.tpl'), $vars);}
   
    public static function sai_mod_saimod_webcraft_billing_satelite_action_getpdf($billid){
        $token = \SYSTEM\CONFIG\config::get(config_ids::WEBCRAFT_BILLING_TOKEN);
        header("Content-type: application/pdf");
        return file_get_contents('http://mojotrollz.eu/web/webcraft/api.php?call=billing&action=bill&token='.$token.'&ID='.$billid);}
    
    public static function html_li_menu(){return '<li><a href="#" saimenu="saimod_webcraft_billing_satelite">Rechnungen</a></li><li class="divider"></li>';}
    public static function right_public(){return false;}    
    public static function right_right(){return \SYSTEM\SECURITY\Security::check(\SYSTEM\SECURITY\RIGHTS::SYS_SAI);}
    public static function sai_mod_saimod_webcraft_billing_satelite_flag_js(){
        return \SYSTEM\LOG\JsonResult::toString(
            array(  \SYSTEM\WEBPATH(new PSAI(),'saimod_webcraft_billing_satelite/saimod_webcraft_billing_satelite.js')));}    
    public static function sai_mod_saimod_webcraft_billing_satelite_flag_css(){
        return \SYSTEM\LOG\JsonResult::toString(
            array(  \SYSTEM\WEBPATH(new PSAI(),'saimod_webcraft_billing_satelite/saimod_webcraft_billing_satelite.css')));}
    
}
