<?php


    $_table = 'module_shop';

    if(file_exists('_modules/shop/l18n/'.$cfg['admin']['language'].'.php')) {

        require_once('_modules/shop/l18n/'.$cfg['admin']['language'].'.php');
    }
    $table_config = array(
        'table' => 'module_shop',
        'multiSelect' => true,    
        'idField'=>'modShpId',
    );
    $table_fields = array( 
        'modShpUsersTable' => array(
            'type' => 'table'                         
        ), 
        'modShpUsersVariable' => array(
            'type' => 'text',
            'length' => 60,
            'size' => 150,                         
        ),                  
    
        'modShpMailFrom' => array(
            'type' => 'text',
            'length' => 80,
            'size' => 200,        
        ),
        
        'modShpMailCC' => array(
            'type' => 'text',
            'length' => 80,
            'size' => 200,        
        ),        
    
        // wiadomosc wysylane przy zmianie statusu
        'modShpSMStatus'=> array (
            'type' => 'text',          
            'length' => '80',
            'size' => '100',
            'arrayField' => 'StatusMessages'
        ),
        'modShpSMSubject'=> array (
            'type' => 'text',          
            'length' => '200',
            'size' => '150',
            'arrayField' => 'StatusMessages'
        ),        
        'modShpSMMessage'=> array (
            'type' => 'htmlarea',            
            'editor' => 'basic',
            'height' => '300',
            'base64' => true,
            'arrayField' => 'StatusMessages'           
        ),

                                              
        'modShpStatusMessages' => array(      
            'type' => 'array_fields', 
            'fields' => array('modShpSMStatus', 'modShpSMSubject', 'modShpSMMessage'),       
        ), 
                       
    
    
    
    
    
        // produkty
        'modShpTabProducts' => array(
            'type' => 'tab',  
            'title' => _t('modShpTabProducts')                       
        ),        
                      
        'modShpProductsTable' => array(
            'type' => 'table'                         
        ),
        'modShpFieldLabel' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),        
        'modShpFieldNetto' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),
        'modShpFieldVAT' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),        
        'modShpFieldBrutto' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),
        
        'modShpFieldAttrGroups' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),
        'modShpFieldAttributes' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpProductsTable',
        ),                     
        
        
        

        
        
        // zamowienia
        'modShpTabOrders' => array(
            'type' => 'tab',  
            'title' => _t('modShpTabOrders')                       
        ),            
        
        'modShpOrdersTable' => array(
            'type' => 'table'
        ),
                           
              
        'modShpPayment'=>array(
            'type'=>'text',
            'length' => 200,
            'size' => 150,
            'arrayField' => 'Payments'
        ),
        
        'modShpPaymentNetto'=> array (
            'type' => 'float',
            'arrayField' => 'Payments',
            'formula' => '{PaymentBrutto}.val(currency({PaymentNetto}.val()*(({PaymentVAT}.val()/100)+1)))'
        ),
        'modShpPaymentVAT'=> array (
            'type' => 'int',
            'arrayField' => 'Payments',
            'default' => 23,
            'formula' => '{PaymentBrutto}.val(currency({PaymentNetto}.val()*(({PaymentVAT}.val()/100)+1)))'
        ),            
        'modShpPaymentBrutto'=> array (
            'type' => 'float',
            'arrayField' => 'Payments',
            'formula' => '{PaymentNetto}.val(currency({PaymentBrutto}.val()/(({PaymentVAT}.val()/100)+1)))'            
        ),        
        
        'modShpPayments' => array(      
            'type' => 'array_fields', 
            'fields' => array('modShpPayment', 'modShpPaymentNetto', 'modShpPaymentVAT', 'modShpPaymentBrutto'),       
        ),         
        
        
        'modShpShipment'=> array (
            'type' => 'text',
            'length' => 200,
            'size' => 150,
            'arrayField' => 'Shipments'
        ),
        'modShpShipmentMode'=> array (
            'type' => 'select',
            'values' => array('only_once', 'every_position'),
            'labels' => array(_t('only_once'), _t('every_position')),
            'arrayField' => 'Shipments'            
        ), 
        'modShpShipmentPayment'=> array (
            'type' => 'select',
            'values' => array('prepaid', 'ondelivery' ,'p24', 'payu'),
            'labels' => array(_t('prepaid'), _t('ondelivery'), _t('p24'), _t('payu')),
            'arrayField' => 'Shipments'            
        ),            

        
        'modShpShipmentNetto'=> array (
            'type' => 'float',
            'arrayField' => 'Shipments',
            'formula' => '{ShipmentBrutto}.val(currency({ShipmentNetto}.val()*(({ShipmentVAT}.val()/100)+1)))'
        ),
        'modShpShipmentVAT'=> array (
            'type' => 'int',
            'arrayField' => 'Shipments',
            'default' => 23,
            'formula' => '{ShipmentBrutto}.val(currency({ShipmentNetto}.val()*(({ShipmentVAT}.val()/100)+1)))'
        ),            
        'modShpShipmentBrutto'=> array (
            'type' => 'float',
            'arrayField' => 'Shipments',
            'formula' => '{ShipmentNetto}.val(currency({ShipmentBrutto}.val()/(({ShipmentVAT}.val()/100)+1)))'            
        ),
		'modShpShipmentRanges'=>array(
			'type'=>'keyvalue_ro',  
            'arrayField' => 'Shipments'  			
		),		

        'modShpShipmentFormula'=>array(
            'type' => 'text',
            'length' => 500,
            'size' => 300,
            'arrayField' => 'Shipments'              
        ),                                        
        'modShpShipments' => array(      
            'type' => 'array_fields', 
            'fields' => array('modShpShipment', 'modShpShipmentMode', 'modShpShipmentPayment', 'modShpShipmentNetto', 'modShpShipmentVAT', 'modShpShipmentBrutto', 'modShpShipmentRanges', 'modShpShipmentFormula'),       
        ), 
        'modShpFreeShipmentFrom' => array(
            'type' => 'float',            
        ),
        
           
     
                
        
        'modShpOrderNumberFormat' => array(
            'type' => 'text',
            'size' => 200,
            'length' => 40,
            'legend' => _t('legend_OrderNumberFormat')
        ),
        'modShpCurrentOrderNumber' => array(
            'type' => 'text',
            'size' => 200,
            'length' => 40,
            'legend' => '<span style="color: #ce0000;">'._t('legend_CurrentOrderNumber').'</span>'
        ),    
        
        
        'modShpXFField' => array(
            'type' => 'table_field',
            'filter_table' => 'modShpOrdersTable',
            'arrayField' => 'XFValidators'            
        ),           
        
        'modShpXFValidKey'=> array (
            'type' => 'text',
            'size' => 150,
            'length' => 60,
            'arrayField' => 'XFValidators'
        ), 
        'modShpXFValidMode'=> array (
            'type' => 'select',
            'values' => array('any', 'email', 'url', 'int', 'float', 'password'),
            'labels' => array(_t('any'), _t('email'), _t('url'), _t('int'), _t('float'), _t('password')),
            'arrayField' => 'XFValidators'            
        ),
        'modShpXFValidAttributes'=> array (
            'type' => 'checkboxes',
            'values' => array('required', 'no_schars'),
            'labels' => array(_t('required'), _t('no_schars')),
            'arrayField' => 'XFValidators'            
        ),     
        'modShpXFValidMin'=> array (
            'type' => 'text',
            'size' => 50,
            'length' => 4,
            'arrayField' => 'XFValidators'
        ),  
        'modShpXFValidMax'=> array (
            'type' => 'text',
            'size' => 50,
            'length' => 4,
            'arrayField' => 'XFValidators'
        ),                     
                        
        'modShpXFValidators' => array(      
            'type' => 'array_fields', 
            'fields' => array('modShpXFField','modShpXFValidKey', 'modShpXFValidMode', 'modShpXFValidMax', 'modShpXFValidAttributes', 'modShpXFValidMin'),       
        ),  
        
            
        /*
        
        'modUsrFieldEmail' => array(
            'type' => 'table_field',
            'filter_table' => 'modUsrTable',
        ),
        'modUsrRegistrationMode' => array(
            'type' => 'select',
            'values' => array('no_hello_activation', 'hello', 'activation', 'hello_activation'),
            'labels' => array(_t('no_hello_activation'), _t('hello'), _t('activation'), _t('hello_activation')),            
        ),
        'modUsrTabMessagesTemplates' => array(
            'type'=>'tab',
            'title'=>_t('MessagesTemplates')        
        ),
        'modUsrMTActivation' => array(
            'type' => 'htmlarea',            
            'editor' => 'basic',
            'height' => '300',
            'language' => true,
            'legend'=>_t('legend_MTActivation') 
        ),   
        'modUsrMTHello' => array(
            'type' => 'htmlarea',            
            'editor' => 'basic',
            'height' => '300',
            'language' => true,
            'legend'=>_t('legend_MTHello')             
        ),
        'modUsrMTPasswordReminder' => array(
            'type' => 'htmlarea',            
            'editor' => 'basic',
            'height' => '300',
            'language' => true,
            'legend'=>_t('legend_MTPasswordReminder')              
        ), 
        */                                          
                
                       
                
    );


    function module_products() {
        global $module, $db, $cfg, $module_settings;
        if(!$module_settings['modShpProductsTable']) {
            $_SESSION['backend_alert'][]=array('type'=>'danger','message'=>'WrongModuleConfiguration','xtra'=>'modShpProductsTable');        
            Header('Location: /'.$cfg['admin']['slug'].'/modules/settings?id='.$module['modId']);
            exit;
        }
        $table=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM tables WHERE tabName='".$module_settings['modShpProductsTable']."'"));
        Header('Location: /'.$cfg['admin']['slug'].'/table/view?tid='.$table['tabId']);
        exit;                        
    }
    
    function module_orders() {
        global $module, $db, $cfg, $module_settings;
        if(!$module_settings['modShpOrdersTable']) {
            $_SESSION['backend_alert'][]=array('type'=>'danger','message'=>'WrongModuleConfiguration','xtra'=>'modShpOrdersTable');        
            Header('Location: /'.$cfg['admin']['slug'].'/modules/settings?id='.$module['modId']);
            exit;
        }
        $table=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM tables WHERE tabName='".$module_settings['modShpOrdersTable']."'"));
        Header('Location: /'.$cfg['admin']['slug'].'/table/view?tid='.$table['tabId']);
        exit;                        
    }    
    
    function module_users() {
        global $module, $db, $cfg, $module_settings;
        if(!$module_settings['modShpUsersTable']) {
            $_SESSION['backend_alert'][]=array('type'=>'danger','message'=>'WrongModuleConfiguration','xtra'=>'modShpUsersTable');        
            Header('Location: /'.$cfg['admin']['slug'].'/modules/settings?id='.$module['modId']);
            exit;
        }
        $table=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM tables WHERE tabName='".$module_settings['modShpUsersTable']."'"));
        Header('Location: /'.$cfg['admin']['slug'].'/table/view?tid='.$table['tabId']);
        exit;                        
    }     
     
    require_once('functions.php');
     
    function module_change_status() {
        global $module, $db, $cfg, $module_settings, $_url;
        //echo '<pre>';
        //print_r($module_settings);
        //exit;
        $orders_table=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM tables WHERE tabName='".$module_settings['modShpOrdersTable']."'"));  
        $order=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM ".$module_settings['modShpOrdersTable']." WHERE ".$orders_table['tabPrefix']."Id='".$_GET['id']."'"));
        
        $ppath='/'.$_url[0].'/'.$_url[1].'/do_change_status';
        //echo $ppath;
       // exit;
        $statuses=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM customFields WHERE cfiTable='".$module_settings['modShpOrdersTable']."' AND cfiName='order_status'"));
        $statuses=json_decode($statuses['cfiParameters'], true);
        //exit;    
        require_once('views/change_status.php');
    } 
    
    function module_do_change_status()  {
        global $module, $db, $cfg, $module_settings, $_url;
        $orders_table=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM tables WHERE tabName='".$module_settings['modShpOrdersTable']."'")); 
        $order=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM ".$module_settings['modShpOrdersTable']." WHERE ".$orders_table['tabPrefix']."Id='".$_POST['id']."'"));
        mysqli_query($db, "UPDATE ".$module_settings['modShpOrdersTable']." SET `".$orders_table['tabPrefix']."#order_status`='".$_POST['status']."' WHERE ".$orders_table['tabPrefix']."Id='".$_POST['id']."'");

        
        if($_POST['notify']) {
            $mdata=json_decode($module_settings['modShpStatusMessages'], true);
            $notify='';
            $subject='';
            foreach($mdata as $key=>$item) {
                if($item['modShpSMStatus']==$_POST['status']) {
                    $notify=urldecode(base64_decode(substr($item['modShpSMMessage'],18)));
                    $subject=$item['modShpSMSubject'];
                    break;
                }
            }   
            if($notify) {
                $notify= parse_notify($notify, $_POST['id']);
                $subject= parse_notify($subject, $_POST['id']);
                //echo $notify;
                //exit;
                mail_send($module_settings['modShpMailFrom'], $order[$orders_table['tabPrefix'].'#del_email'], $subject, $notify);   
                $alert[]=array('type'=>'success','message'=>_t('OrderStatusChanged').'. '._t('NotifySent'));
            } else {
                $alert[]=array('type'=>'success','message'=>_t('OrderStatusChanged'));            
            }              
        } else {
            $alert[]=array('type'=>'success','message'=>_t('OrderStatusChanged'));
        }   
        $_SESSION['backend_alert']=$alert;    
        echo 'OK';
    }
     

?>
