<?php
/**
 * Created by PhpStorm.
 * User: Ludah
 * Date: 12/08/2017
 * Time: 3:19 PM
 */
function add_new_service_pricing($dbc,$countryCode,$general_service,$service_name,$minimum,$maximum){
    $user_id = $_SESSION['uid'];
    $q = mysqli_query($dbc, "INSERT INTO services_tbl
      (senderCountry, general_service, serviceName, minimum, maximum, added_by) VALUES 
      ('$countryCode','$general_service','$service_name','$minimum','$maximum','$user_id')
    ");
    if($q){
        return true;
    }
}

function edit_new_service_pricing($dbc,$id,$countryCode,$general_service,$service_name,$minimum,$maximum){
    $user_id = $_SESSION['uid'];
    $q = mysqli_query($dbc,"UPDATE services_tbl SET
        senderCountry = '$countryCode',
        general_service = '$general_service',
        serviceName = '$service_name',
        minimum = '$minimum',
        maximum = '$maximum',
        added_by = '$user_id' WHERE 
        id = '$id'
     ");
    if($q){
        return true;
    }
}

function get_service_pricing_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM services_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_pricing_policy_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM pricing_policy_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_all_from_service_pricing($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM services_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function add_pricing_policy($dbc,$senderGroup,$senderAccType,$recipientGroup,$recipientAccType,$serviceType,$price,$priceType,$senderCountry,$recipientCountry,$sysCommission,$senderCommission,$recipientCommission){
    $user = $_SESSION['uid'];
    $q = mysqli_query($dbc, " INSERT INTO pricing_policy_tbl
      (senderUGroup, senderAccountType, recipientUGroup, recipientAccountType, serviceType, price_in_percent_absolute, priceType, pricing_by,senderCountry,recipientCountry,sysCommission,senderCommission,recipientCommission) VALUES 
      ('$senderGroup','$senderAccType','$recipientGroup','$recipientAccType','$serviceType','$price','$priceType','$user','$senderCountry','$recipientCountry','$sysCommission','$senderCommission','$recipientCommission')
    ");
    if($q){
        return true;
    }
}

function update_pricing_policy($dbc,$id,$senderGroup,$senderAccType,$recipientGroup,$recipientAccType,$serviceType,$price,$priceType,$senderCountry,$recipientCountry,$sysCommission,$senderCommission,$recipientCommission){
    $q = mysqli_query($dbc, "UPDATE pricing_policy_tbl SET 
      senderUGroup = '$senderGroup',
      senderAccountType = '$senderAccType',
      recipientUGroup = '$recipientGroup',
      recipientAccountType = '$recipientAccType',
      serviceType = '$serviceType',
      price_in_percent_absolute = '$price',
      priceType = '$priceType',
      senderCountry = '$senderCountry',
      recipientCountry = '$recipientCountry',
      sysCommission = '$sysCommission',
      senderCommission = '$senderCommission',
      recipientCommission = '$recipientCommission'
      WHERE id = '$id'
      
    ");
    if($q){
        return true;
    }
}

function get_service_id_during_translation_MT($dbc,$senderData,$amount,$general_service){
    //$general_service = "MT";
    $q = mysqli_query($dbc,"SELECT id FROM services_tbl WHERE 
      general_service = '$general_service' AND 
      countryCode = '$senderData[countryCode]' AND 
      (minimum<='$amount' AND maximum>= '$amount')
    ");
    $data = mysqli_fetch_assoc($q);
    return $data['id'];

}
function get_service_id($dbc,$senderData,$amount,$general_service){
    //$general_service = "MT";
    $q = mysqli_query($dbc,"SELECT id FROM new_services_tbl WHERE 
      general_service = '$general_service' AND 
      senderCountry = '$senderData[countryCode]' AND 
      (minimum<='$amount' AND maximum>= '$amount')
    ");
    $data = mysqli_fetch_assoc($q);
    return $data['id'];

}

function get_pricing_during_translation_MT($dbc,$senderData,$recipientData,$serviceID){

    $q = mysqli_query($dbc, "SELECT * FROM pricing_policy_tbl WHERE 
      senderUGroup = '$senderData[uGroup]' AND 
      senderAccountType = '$senderData[accountType]' AND 
      recipientUGroup = '$recipientData[uGroup]' AND 
      recipientAccountType = '$recipientData[accountType]' AND 
      serviceType = '$serviceID'
    ");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_pricing($dbc,$senderData,$recipientData,$serviceID){

    $q = mysqli_query($dbc, "SELECT * FROM pricing_policy_tbl WHERE 
      senderUGroup = '$senderData[uGroup]' AND 
      senderAccountType = '$senderData[accountType]' AND 
      senderCountry = '$senderData[countryCode]' AND 
      recipientUGroup = '$recipientData[uGroup]' AND 
      recipientAccountType = '$recipientData[accountType]' AND
      recipientCountry = '$recipientData[countryCode]' AND
      serviceType = '$serviceID'
    ");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_pricing_by_id($dbc,$id){
    $q = mysqli_query($dbc, "SELECT * FROM pricing_policy_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;

}