<?php

/*

MIRAKL GET BANK INFOS

Permet de récupérer l'IBAN dans MIRAKL et le pousser sur le compte Wallet.
Utilise l'API SOAP pour poster les bank-infos afin de ne pas envoyer le RIB en même temps.
Ce script était un hack pour compenser les bugs sur le connecteur Mirakl où les bank infos ne remontaient pas.

En 2018, ce script ne derait plus avoir à être utilisé.

configuration :
remplir les infos demandées dans config.Mirakl.inc.php

Par exemple :
    'myentity' => array(
        'resource_mirakl'   => '', // URL of Mirakl Backoffice, with https
        'mirakl_front_key'  => '', // Mirakl Front Key

        'hipay_env'     => 'prod', // or test
        'wsLogin'       => '', // Technical account login
        'wsPassword'    => '', // Technical account password
    ),

*/



// MySQL Connect
require_once("../shared/functions.php");
$db = connecti();

if(isset($_GET['debug']) && $_GET['debug'] > 0) { $debug = $_GET['debug']; }
else $debug = 0;

if(isset($_POST['MiraklID']) && $_POST['MiraklID'] > 0) { $MiraklID = (int) trim($_POST['MiraklID']); }
else $MiraklID = '';

if(isset($_POST['AccountID']) && $_POST['AccountID'] > 0) { $AccountID = (int) trim($_POST['AccountID']); }
else $AccountID = '';

if(isset($_POST['PasteFromExcel']) && $_POST['PasteFromExcel'] != '') { $PasteFromExcel = trim($_POST['PasteFromExcel']); }
else $PasteFromExcel = '';

if(isset($_POST['Marketplace']) && $_POST['Marketplace'] != '') { $Marketplace = trim($_POST['Marketplace']); }

// Mirakl Connect
require_once("config.Mirakl.inc.php");

$inserted = $updated = $error = $lastid_sql = $finished = 0;
$response = array();

if($debug) krumo($_POST);

if($MiraklID && $AccountID) {

    include("processor.Mirakl.inc.php");

}
elseif($PasteFromExcel) {

    $AllShops = explode("\n", $PasteFromExcel);
    if($debug) krumo($AllShops);

    if(is_array($AllShops) && count($AllShops) > 0) {
        foreach($AllShops as $Shop) {

            if(strlen($Shop) > 10) {
                $ShopRaw = explode("\t", $Shop);

                // Mirakl ID
                preg_match("/mirakl_[a-zA-Z0-9]+_([0-9]+)/", trim($ShopRaw[1]),  $MiraklIDMatch);
                if($debug) krumo($MiraklIDMatch);
                $MiraklID   = (int) $MiraklIDMatch[1];

                // Account ID
                $AccountID = htmlentities($ShopRaw[0], null, 'utf-8');
                $AccountID = (int) str_replace("&nbsp;", "", trim($AccountID));

                // Process if Data found
                if($MiraklID && $AccountID) {
                    if($debug) { echo "\n<br>Found Shop $MiraklID and Account $AccountID"; }
                    include("processor.Mirakl.inc.php");
                }
            }
        }
    } else {
        $j = $count($response);
        $j++;
        $response[$j]->Message = "Paste from Excel inconsistent.";
        $response[$j]->Type = 'danger';
    }

    $MiraklID = $AccountID = '';
}

$db->close();
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta charset="utf-8">
        <title>Mirakl - Get Bank Info</title>
        
        <script src="//code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
    
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- Optional theme -->
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

        <!-- Latest compiled and minified JavaScript -->
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

        <script src="Mirakl.js"></script>   
    </head>
    <body>
        <div class="container-fluid">

            <div class="row">
<?php
                if(isset($response) && count($response) > 0) {
                    foreach($response as $key => $resp) { ?>
                        <div class="alert alert-<?php echo $resp->Type; ?>">Account <b><?php echo $key; ?></b>, Shop <b><?php echo $resp->MiraklID; ?></b> : <?php echo $resp->Message; ?></div>
<?php
                    }
                }
?>
            </div>

            <h2>Get IBAN from Mirakl and insert to HiPay</h2>
            <div>

                <form name="Mirakl" action="" method="post">

                    <h3>Choose Marketplace</h3>

                    <div class="form-group">
                        <label class="sr-only" for="Marketplace">Marketplace</label>
                        <select name="Marketplace" id="Marketplace" class="form-control form-control-lg input-lg">
<?php
                        if(is_array($params)) {
                            foreach($params as $key => $param) {
                                    echo "<option value='$key'";

                                    if($Marketplace == $key) {
                                        echo " selected='selected'";
                                    }

                                    echo ">".ucfirst($key)."</option>\n";
                            }
                        } else die('No Marketplace recorded');
?>
                        </select>
                    </div>

                    <h3>Insert Data</h3>

                    <div class="form-group">
                        <label for="PasteFromBO">Paste from Wallet BO</label>
                        <div class="input-group">
                        <input type="text" class="form-control" id="PasteFromBO" placeholder="Id	Id EU	Nom	Devise	Type	Date création	Etat	Login"> <!--  value="9779083  	9484543 	Jean-Yves BERNARD	EUR	Principal	12 juin 2017	Normal	mirakl_Lilis_2078" -->
                        <div class="input-group-addon" id="ConvertFromBO"><a href="#">convert</a></div>
                        </div>
                    </div>

                    <h5><em>Or insert text manually</em></h5>

                    <div class="form-group">
                        <label for="MiraklID">Mirakl Shop ID</label>
                        <input name="MiraklID" id="MiraklID" value="<?php echo $MiraklID; ?>" class="form-control form-control-lg input-lg" type="number" />
                    </div>
                    
                    <div class="form-group">
                        <label for="AccountID">Account ID</label>
                        <input name="AccountID" id="AccountID" value="<?php echo $AccountID; ?>" class="form-control form-control-lg input-lg" type="number" />
                    </div>

                    <h3>Process Multiple shops</h3>

                    <div class="form-group">
                        <label for="PasteFromExcel">Paste multiple lines from Excel (separator <em>space</em> or <em>TAB</em>)</label>
                        <textarea name="PasteFromExcel" id="PasteFromExcel" class="form-control" rows="3" placeholder="AccountID1 Login1\nAccountID2 Login2\nAccountID3 Login3"></textarea>
                    </div>

                    <div class="form-group">
                        <input type="submit" name="submit" value="Insert" class="btn btn-primary" />
                    </div>

                </form>

            </div>
        </div>
    </body>
</html>