<?php
/////////////////////////////////////
include("../../../assets/conn/conexion.php"); 
$conn = new connexion($host_avanws,$user_avanws,$pass_avanws,$name_avanws,$port_avanws);
$conn->query("SET NAMES 'utf8'");
//AUTH
include('func_dapda_pedidos.php');
$post = json_decode(file_get_contents('php://input'), true);
$url = 'http://test.papelcero.es/api/avanws.php';
$servicio = "servToken";
$servicio_key = getServiceKey($conn, $servicio);
$data = array(
    "grant_type"=>"client_credentials",
    "client_secret"=> $post["client_secret"],
    "client_id"=> $post["client_id"],
    "service"=>"servToken",
    "code"=>"8356e8922cdccb82gh538fu2"
);
$headers = [];
$response = curlPost($url,$data,$headers);
if(getToken($response)){
    $quotation_id = $post["json"]['quotation_id'];
    $valida_pedido = validUniqPedido($conn, $quotation_id);
    if ($valida_pedido == 0) {
        //MANDATORIES
        $company_id =  $post["json"]['company_id'];
        $company_name = $post["json"]['company_name'];
        $date_of_contract_printing = $post["json"]['date_of_contract_printing'];
        $generated_id = $post["json"]['generated_id'];
        $sales_module_type = $post["json"]['sales_module_type'];
        $agent_name = $post["json"]['agent_name'];
        $customer_name = $post["json"]['customer']['name'];
        $customer_fiscalNumber = $post["json"]['customer']['fiscal_number'];
        $customer_units = $post["json"]['units'];
        $model_name = $post["json"]['economic_data']['model']['name'];
        $model_amount = $post["json"]['economic_data']['model']['amount'];
        $taxable_base_amount = $post["json"]['economic_data']['taxable_base_amount'];
        $retail_price = $post["json"]['economic_data']['retail_price'];
        //OPTIONALS
        $customer_surname = (isset($post["json"]['customer']['surname'])) ? $post["json"]['customer']['surname'] : "";
        $customer_second_surname = (isset($post["json"]['customer']['second_surname'])) ? $post["json"]['customer']['second_surname'] : "";
        $customer_address = (isset($post["json"]['customer']['address'])) ? $post["json"]['customer']['address'] : "";
        $customer_restAddress= (isset($post["json"]['customer']['restAddress'])) ? $post["json"]['customer']['restAddress'] : "";
        $customer_email = (isset($post["json"]['customer']['email'])) ? $post["json"]['customer']['email'] : "";
        $customer_phone_number = (isset($post["json"]['customer']['phone_number'])) ? $post["json"]['customer']['phone_number'] : 0;
        $model_manufacturer_reference = (isset($post["json"]['economic_data']['model']['manufacturer_reference'])) ? $post["json"]['economic_data']['model']['manufacturer_reference'] : "";
        $model_registration_date = (isset($post["json"]['economic_data']['model']['registration_date'])) ? $post["json"]['economic_data']['model']['registration_date'] : NULL;
        $model_registration_plate = (isset($post["json"]['economic_data']['model']['registration_plate'])) ? $post["json"]['economic_data']['model']['registration_plate'] : "";
        $model_kilometers = (isset($post["json"]['economic_data']['model']['kilometers'])) ? $post["json"]['economic_data']['model']['kilometers'] : 0;
        $model_warranty = (isset($post["json"]['economic_data']['model']['warranty'])) ? $post["json"]['economic_data']['model']['warranty'] : "";
        $model_vin = (isset($post["json"]['economic_data']['model']['vin'])) ? $post["json"]['economic_data']['model']['vin'] : "";
        $color_name = (isset($post["json"]['economic_data']['color']['name'])) ? $post["json"]['economic_data']['color']['name'] : "";
        $color_amount = (isset($post["json"]['economic_data']['color']['amount'])) ? $post["json"]['economic_data']['color']['amount'] : 0.00;
        $trim_name = (isset($post["json"]['economic_data']['trim']['name'])) ? $post["json"]['economic_data']['trim']['name'] : "";
        $trim_amount = (isset($post["json"]['economic_data']['trim']['amount'])) ? $post["json"]['economic_data']['trim']['amount'] : 0.00;
        $kit_and_transport_amount = (isset($post["json"]['economic_data']['kit_and_transport_amount'])) ? $post["json"]['economic_data']['kit_and_transport_amount'] : 0.00;
        $taxes_vat_percentage = (isset($post["json"]['economic_data']['taxes']['vat_percentage'])) ? $post["json"]['economic_data']['taxes']['vat_percentage'] : 0.00;
        $taxes_vat_amount = (isset($post["json"]['economic_data']['taxes']['vat_amount'])) ? $post["json"]['economic_data']['taxes']['vat_amount'] : 0.00;
        $taxes_local_tax_percentage = (isset($post["json"]['economic_data']['taxes']['local_tax_percentage'])) ? $post["json"]['economic_data']['taxes']['local_tax_percentage'] : 0.00;
        $taxes_local_tax_amount = (isset($post["json"]['economic_data']['taxes']['local_tax_amount'])) ? $post["json"]['economic_data']['taxes']['local_tax_amount'] : 0.00;
        $payment_mode_financing_total_financed_amount = (isset($post["json"]['economic_data']['payment_mode']['financing']['total_financed_amount'])) ? $post["json"]['economic_data']['payment_mode']['financing']['total_financed_amount'] : 0.00;
        $payment_mode_financing_quote = (isset($post["json"]['economic_data']['payment_mode']['financing']['quote'])) ? $post["json"]['economic_data']['payment_mode']['financing']['quote'] : 0.00;
        $payment_mode_financing_duration_months = (isset($post["json"]['economic_data']['payment_mode']['financing']['duration_months'])) ? $post["json"]['economic_data']['payment_mode']['financing']['duration_months'] : 0;
        $payment_mode_down_payment = (isset($post["json"]['economic_data']['payment_mode']['down_payment'])) ? $post["json"]['economic_data']['payment_mode']['down_payment'] : 0.00;
        $payment_mode_valuation_vehicle_description = (isset($post["json"]['economic_data']['payment_mode']['valuation']['vehicle_description'])) ? $post["json"]['economic_data']['payment_mode']['valuation']['vehicle_description'] : "";
        $payment_mode_valuation_amount = (isset($post["json"]['economic_data']['payment_mode']['valuation']['amount'])) ? $post["json"]['economic_data']['payment_mode']['valuation']['amount'] : 0.00;
        $payment_mode_amount_remaining = (isset($post["json"]['economic_data']['payment_mode']['amount_remaining'])) ? $post["json"]['economic_data']['payment_mode']['amount_remaining'] : 0.00;
        if (isset($post["json"]['economic_data']['atypical_summary']['financing_offered'])) {
            if ($post["json"]['economic_data']['atypical_summary']['financing_offered'] === true) {
                $atypical_summary_financing_offered = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['financing_offered'] === false) {
                $atypical_summary_financing_offered = 0;
            }
        }else {
            $atypical_summary_financing_offered = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['financing_performed'])) {
            if ($post["json"]['economic_data']['atypical_summary']['financing_performed'] === true) {
                $atypical_summary_financing_performed = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['financing_performed'] === false) {
                $atypical_summary_financing_performed = 0;
            }
        }else {
            $atypical_summary_financing_performed = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['additional_warranty_offered'])) {
            if ($post["json"]['economic_data']['atypical_summary']['additional_warranty_offered'] === true) {
                $atypical_summary_additional_warranty_offered = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['additional_warranty_offered'] === false) {
                $atypical_summary_additional_warranty_offered = 0;
            }
        }else {
            $atypical_summary_additional_warranty_offered = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['additional_warranty_performed'])) {
            if ($post["json"]['economic_data']['atypical_summary']['additional_warranty_performed'] === true) {
                $atypical_summary_additional_warranty_performed = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['additional_warranty_performed'] === false) {
                $atypical_summary_additional_warranty_performed = 0;
            }
        }else {
            $atypical_summary_additional_warranty_performed = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['insurance_offered'])) {
            if ($post["json"]['economic_data']['atypical_summary']['insurance_offered'] === true) {
                $atypical_summary_insurance_offered = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['insurance_offered'] === false) {
                $atypical_summary_insurance_offered = 0;
            }
        }else {
            $atypical_summary_insurance_offered = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['insurance_performed'])) {
            if ($post["json"]['economic_data']['atypical_summary']['insurance_performed'] === true) {
                $atypical_summary_insurance_performed = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['insurance_performed'] === false) {
                $atypical_summary_insurance_performed = 0;
            }
        }else {
            $atypical_summary_insurance_performed = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['agency_offered'])) {
            if ($post["json"]['economic_data']['atypical_summary']['agency_offered'] === true) {
                $atypical_summary_agency_offered = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['agency_offered'] === false) {
                $atypical_summary_agency_offered = 0;
            }
        }else {
            $atypical_summary_agency_offered = 0;
        }
        if (isset($post["json"]['economic_data']['atypical_summary']['agency_performed'])) {
            if ($post["json"]['economic_data']['atypical_summary']['agency_performed'] === true) {
                $atypical_summary_agency_performed = 1;
            }elseif  ($post["json"]['economic_data']['atypical_summary']['agency_performed'] === false) {
                $atypical_summary_agency_performed = 0;
            }
        }else {
            $atypical_summary_agency_performed = 0;
        }
        $profitability_summary_discount_limit_percentage = (isset($post["json"]['economic_data']['profitability_summary']['discount_limit_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['discount_limit_percentage'] : 0.00;
        $profitability_summary_profitability_limit_percentage = (isset($post["json"]['economic_data']['profitability_summary']['profitability_limit_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['profitability_limit_percentage'] : 0.00;
        $profitability_summary_commercial_activities_total_percentage = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_total_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_total_percentage'] : 0.00;
        $profitability_summary_commercial_activities_total_amount = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_total_amount'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_total_amount'] : 0.00;
        $profitability_summary_commercial_activities_brand_percentage = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_brand_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_brand_percentage'] : 0.00;
        $profitability_summary_commercial_activities_brand_amount = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_brand_amount'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_brand_amount'] : 0.00;
        $profitability_summary_commercial_activities_dealer_percentage = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_dealer_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_dealer_percentage'] : 0.00;
        $profitability_summary_commercial_activities_dealer_amount = (isset($post["json"]['economic_data']['profitability_summary']['commercial_activities_dealer_amount'])) ? $post["json"]['economic_data']['profitability_summary']['commercial_activities_dealer_amount'] : 0.00;
        $discounts_percentage = (isset($post["json"]['economic_data']['profitability_summary']['discounts_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['discounts_percentage'] : 0.00;
        $discounts_amount = (isset($post["json"]['economic_data']['profitability_summary']['discounts_amount'])) ? $post["json"]['economic_data']['profitability_summary']['discounts_amount'] : 0.00;
        $discounts_total_percentage = (isset($post["json"]['economic_data']['profitability_summary']['discounts_total_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['discounts_total_percentage'] : 0.00;
        $discounts_total_amount = (isset($post["json"]['economic_data']['profitability_summary']['discounts_total_amount'])) ? $post["json"]['economic_data']['profitability_summary']['discounts_total_amount'] : 0.00;
        $remaining_discount_limit_percentage = (isset($post["json"]['economic_data']['profitability_summary']['remaining_discount_limit_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['remaining_discount_limit_percentage'] : 0.00;
        $remaining_discount_limit_amount = (isset($post["json"]['economic_data']['profitability_summary']['remaining_discount_limit_amounbt'])) ? $post["json"]['economic_data']['profitability_summary']['remaining_discount_limit_amounbt'] : 0.00;
        $profitability_percentage = (isset($post["json"]['economic_data']['profitability_summary']['profitability_percentage'])) ? $post["json"]['economic_data']['profitability_summary']['profitability_percentage'] : 0.00;
        $profitability_amount = (isset($post["json"]['economic_data']['profitability_summary']['profitability_amount'])) ? $post["json"]['economic_data']['profitability_summary']['profitability_amount'] : 0.00;
        if( isset($company_id) AND isset($company_name) AND isset($quotation_id) AND isset($date_of_contract_printing) AND isset($generated_id) AND isset($sales_module_type) 
        AND isset($customer_name) AND isset($agent_name) AND isset($customer_fiscalNumber) AND isset($customer_units) AND isset($model_name) AND isset($model_name)
        AND isset($taxable_base_amount) AND isset($retail_price) 
        ) {   
            if(checkExistPedido ($conn, $generated_id) == 1){ deletePedido ($conn, $generated_id); }
            $sql = " INSERT INTO INTER_DAPDA_PEDIDOS ( CLAVE, COMPANY_ID, COMPANY_NAME, QUOTATION_ID, DATE_OF_CONTRACT_PRINTING, GENERATED_ID, SALES_MODULE_TYPE, AGENT_NAME, CUSTOMER_NAME, CUSTOMER_SURNAME,
            CUSTOMER_SECOND_SURNAME, CUSTOMER_ADDRESS, CUSTOMER_FISCALNUMBER, CUSTOMER_EMAIL, CUSTOMER_PHONE_NUMBER, CUSTOMER_UNITS, CUSTOMER_RESTADDRESS, MODEL_NAME, MODEL_MANUFACTURER_REFERENCE, 
            MODEL_AMOUNT, MODEL_REGISTRATION_DATE, MODEL_REGISTRATION_PLATE, MODEL_KILOMETERS, MODEL_WARRANTY, MODEL_VIN, COLOR_NAME, COLOR_AMOUNT, TRIM_NAME, TRIM_AMOUNT, 
                KIT_AND_TRANSPORT_AMOUNT, TAXABLE_BASE_AMOUNT, TAXES_VAT_PERCENTAGE, TAXES_VAT_AMOUNT, TAXES_LOCAL_TAX_PERCENTAGE, TAXES_LOCAL_TAX_AMOUNT, RETAIL_PRICE, 
                PAYMENT_MODE_FINANCING_TOTAL_FINANCED_AMOUNT, PAYMENT_MODE_FINANCING_QUOTE, PAYMENT_MODE_FINANCING_DURATION_MONTHS, PAYMENT_MODE_DOWN_PAYMENT, PAYMENT_MODE_VALUATION_VEHICLE_DESCRIPTION, 
                PAYMENT_MODE_VALUATION_AMOUNT, PAYMENT_MODE_AMOUNT_REMAINING, ATYPICAL_SUMMARY_FINANCING_OFFERED, ATYPICAL_SUMMARY_FINANCING_PERFORMED, ATYPICAL_SUMMARY_ADDITIONAL_WARRANY_OFFERED,
                ATYPICAL_SUMMARY_ADDITIONAL_WARRANY_PERFORMED, ATYPICAL_SUMMARY_INSURANCE_OFFERED, ATYPICAL_SUMMARY_INSURANCE_PERFORMED, ATYPICAL_SUMMARY_AGENCY_OFFERED, 
                ATYPICAL_SUMMARY_AGENCY_PERFORMED, PROFITABILITY_SUMMARY_DISCOUNT_LIMIT_PERCENTAGE, PROFITABILITY_SUMMARY_PROFITABILITY_LIMIT_PERCENTAGE,
                PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_TOTAL_PERCENTAGE, PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_TOTAL_AMOUNT, PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_BRAND_PERCENTAGE, 
                PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_BRAND_AMOUNT, PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_DEALER_PERCENTAGE, PROFITABILITY_SUMMARY_COMMERCIAL_ACTIVITIES_DEALER_AMOUNT, 
                DISCOUNTS_PERCENTAGE, DISCOUNTS_AMOUNT, DISCOUNTS_TOTAL_PERCENTAGE, DISCOUNTS_TOTAL_AMOUNT, REMAINING_DISCOUNT_LIMIT_PERCENTAGE, REMAINING_DISCOUNT_LIMIT_AMOUNT, 
                PROFITABILITY_PERCENTAGE, PROFITABILITY_AMOUNT)  
        VALUES ( '$generated_id', '$company_id', '$company_name', '$quotation_id', '$date_of_contract_printing', '$generated_id', '$sales_module_type', '$agent_name', '$customer_name', '$customer_surname',
            '$customer_second_surname', '$customer_address', '$customer_fiscalNumber', '$customer_email', '$customer_phone_number', $customer_units, '$customer_restAddress', '$model_name', '$model_manufacturer_reference',
            $model_amount, '$model_registration_date', '$model_registration_plate', $model_kilometers, '$model_warranty', '$model_vin', '$color_name', $color_amount, '$trim_name', $trim_amount, 
            $kit_and_transport_amount, $taxable_base_amount, $taxes_vat_percentage, $taxes_vat_amount, $taxes_local_tax_percentage, $taxes_local_tax_amount, $retail_price, 
            $payment_mode_financing_total_financed_amount, $payment_mode_financing_quote, $payment_mode_financing_duration_months, $payment_mode_down_payment, '$payment_mode_valuation_vehicle_description',
            $payment_mode_valuation_amount, $payment_mode_amount_remaining, $atypical_summary_financing_offered, $atypical_summary_financing_performed, $atypical_summary_additional_warranty_offered, 
            $atypical_summary_additional_warranty_performed, $atypical_summary_insurance_offered, $atypical_summary_insurance_performed, $atypical_summary_agency_offered,
            $atypical_summary_agency_performed, $profitability_summary_discount_limit_percentage, $profitability_summary_profitability_limit_percentage, 
            $profitability_summary_commercial_activities_total_percentage, $profitability_summary_commercial_activities_total_amount, $profitability_summary_commercial_activities_brand_percentage,
            $profitability_summary_commercial_activities_brand_amount, $profitability_summary_commercial_activities_dealer_percentage, $profitability_summary_commercial_activities_dealer_amount, 
            $discounts_percentage, $discounts_amount, $discounts_total_percentage, $discounts_total_amount, $remaining_discount_limit_percentage, $remaining_discount_limit_amount, 
            $profitability_percentage, $profitability_amount)";
            mysqli_query($conn, $sql); 


            $q_insert = mysqli_affected_rows($conn);
            
            for($i = 0; $i<=2; $i++){
                $pos = $i + 1; 
                $name =  $post["json"]['economic_data']['optional_equipment'][$i]['name'];
                $amount =  $post["json"]['economic_data']['optional_equipment'][$i]['amount'];
                $sql = " INSERT INTO INTER_DAPDA_PEDIDOS_OPCIONES ( CLAVE, POS, NAME, AMOUNT) VALUES ( '$generated_id', $pos, '$name', $amount)";
                mysqli_query($conn, $sql); 
            }

            for($i = 0; $i<=1; $i++){
                $pos = $i + 1;
                $description =  $post["json"]['economic_data']['commercial_activities'][$i]['description'];
                $dealer_amount =  $post["json"]['economic_data']['commercial_activities'][$i]['dealer_amount'];
                $dealer_percentage =  $post["json"]['economic_data']['commercial_activities'][$i]['dealer_percentage'];
                $brand_amount =  $post["json"]['economic_data']['commercial_activities'][$i]['brand_amount'];
                $brand_percentage =  $post["json"]['economic_data']['commercial_activities'][$i]['brand_percentage'];
                $total_amount =  $post["json"]['economic_data']['commercial_activities'][$i]['total_amount'];
                $total_percentage =  $post["json"]['economic_data']['commercial_activities'][$i]['total_percentage'];
                $sql = " INSERT INTO  INTER_DAPDA_PEDIDOS_DESCUENTOS (CLAVE, POS, DESCRIPTION, DEALER_AMOUNT, DEALER_PERCETAGE, BRAND_AMOUNT, BRAND_PERCENTAGE, TOTAL_AMOUNT, TOTAL_PERCENTAGE)
                    VALUES ( '$generated_id', $pos, '$description', $dealer_amount, $dealer_percentage, $brand_amount, $brand_percentage, $total_amount, $total_percentage)";
                mysqli_query($conn, $sql); 
            }

            for($i = 0; $i<=2; $i++){
                $pos = $i + 1; 
                $type =  $post["json"]['economic_data']['quotation_concepts_before_taxes'][$i]['type'];
                $name =  $post["json"]['economic_data']['quotation_concepts_before_taxes'][$i]['name'];
                $amount =  $post["json"]['economic_data']['quotation_concepts_before_taxes'][$i]['amount'];
                $sql = " INSERT INTO INTER_DAPDA_PEDIDOS_LIN_SINIMP (CLAVE, POS, TYPE, NAME, AMOUNT) VALUES ('$generated_id', $pos, '$type', '$name', $amount)";
                mysqli_query($conn, $sql);
            }
            
            for($i = 0; $i<=4; $i++){
                $pos = $i + 1;
                $type =  $post["json"]['economic_data']['quotation_concepts_after_taxes'][$i]['type'];
                $name =  $post["json"]['economic_data']['quotation_concepts_after_taxes'][$i]['name'];
                $amount =  $post["json"]['economic_data']['quotation_concepts_after_taxes'][$i]['amount'];
                $sql = " INSERT INTO INTER_DAPDA_PEDIDOS_LIN_CONIMP ( CLAVE, POS, TYPE, NAME, AMOUNT) VALUES ('$generated_id', $pos, '$type', '$name', $amount)";
                mysqli_query($conn, $sql);
            }
            $dataD = ($q_insert > 0) ? array ( "code" => "Ok!","message" => "New record created successfully") : array ( "code" => "Not ok!","message" => "New record cant created");
        }else{
            $dataD = array ( 
                "code" => "Not ok",
                "message" => "mandatory data is missing"
            );
        }
    }else {
        $dataD = array ( 
            "code" => "Ok!",
            "message" => "Update successfully"
        );
    }
}else{
    $dataD = array ( 
        "code" => "not Ok",
        "message" => "no token"
    );
}
$result= json_encode($dataD);  
print_r($result);
?>