Commit 6694d4df authored by Cyrille Georgel's avatar Cyrille Georgel

more in README

parent b93ff712
<?php
# MIT License
#
# Copyright (c) 2017 Cyrille Georgel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
include_once 'config.php';
include_once 'pdo.php';
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
Class OVHBills extends Api
{
public function billsList($start, $end)
{
global $ovh;
$bills = $ovh->get('/me/bill?date.from=' . $start . '&date.to=' . $end);
return $bills;
}
public function billGetFile($bill)
{
/* Get the bill as a PDF file */
global $ovh;
$result = new StdClass();
if(!file_exists (BILLS_PATH))
{
mkdir(BILLS_PATH);
}
$billId = $ovh->get('/me/bill/' . $bill)['billId'];
$pdfUrl = $ovh->get('/me/bill/' . $bill)['pdfUrl'];
$localFile = BILLS_PATH . '/' . $billId . '.pdf';
if(!file_exists($localFile))
{
$pdf = file_get_contents($pdfUrl);
if(!file_put_contents($localFile, $pdf))
{
$result->status = 'ko';
$result->feedback = 'Bill ' . $bill . 'download: error';
}
else
{
$result->status = 'ok';
$result->feedback = 'Bill ' . $bill . ' download: success';
}
}
else
{
$result->status = 'ko';
$result->feedback = 'Bill ' . $bill . ' was already downloaded';
}
return $result;
}
public function billGetDetails($bill)
{
/*
Get the details of the bill
in 2 database tables :
bills / billsDetails
*/
global $ovh;
global $dbh;
$control = 0;
$sth = $dbh->prepare("INSERT INTO bills
(billId, billDate, password, orderId, url,
currency, priceWithoutTax, tax, priceWithTax, pdfUrl)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$document = $ovh->get('/me/bill/' . $bill);
$sth->execute(array(
$document['billId'],
$document['date'],
$document['password'],
$document['orderId'],
$document['url'],
$document['priceWithoutTax']['currencyCode'],
$document['priceWithoutTax']['value'],
$document['tax']['value'],
$document['priceWithTax']['value'],
$document['pdfUrl']
));
$control++;
if($sth->rowCount() == 1)
{
$control--;
}
$sth = $dbh->prepare("INSERT INTO billsDetails
(billId, billDetailId, description, domain,
periodStart, periodEnd, currency, quantity, unitPrice, totalPrice)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$lines = $ovh->get('/me/bill/' . $bill . '/details');
$control = count($lines);
foreach($lines as $line)
{
$detail = $ovh->get('/me/bill/' . $bill . '/details/' . $line);
$sth->execute(array(
$bill,
$detail['billDetailId'],
$detail['description'],
$detail['domain'],
$detail['periodStart'],
$detail['periodEnd'],
$document['priceWithoutTax']['currencyCode'],
$detail['quantity'],
$detail['unitPrice']['value'],
$detail['totalPrice']['value']
));
if($sth->rowCount() == 1)
{
$control--;
}
}
if($control == 0)
{
return true;
}
else
{
return false;
}
}
}
################################################################################
# Usage example:
$ovh = new OVHBills(APP_KEY, APP_SECRET, APP_ENDPOINT, APP_CONSKEY);
print 'Welcome ' . $ovh->get('/me')['firstname'] . PHP_EOL;
$bills = $ovh->billsList('2017-02-01', '2017-02-28');
# Just listing the bills:
print_r($bills);
# Then...
foreach($bills as $bill)
{
# You can download the bills as PDF files:
$download = $ovh->billGetFile($bill);
print strtoupper($download->status) . ' : ' . $download->feedback . PHP_EOL;
# And/Or get the content of the bills in the database:
if($ovh->billGetDetails($bill) == true)
{
print 'mysql insert : OK' . PHP_EOL;
}
else
{
print 'mysql insert : KO' . PHP_EOL;
}
}
?>
......@@ -5,8 +5,18 @@ OVHBills allows you to get your OVH bills the easy way:
* PDF file for each bill is downloaded to the path given in config file.
* Details of each bill is recorder in two tables of the database (bills and billsDetails).
Set the date from which you want to get the bills in config.php
Set the date from which you want to get the bills in config.php.
However, OVHBills is just getting the new bills.
For example, if you are running the script every night in a CRON task, you can put "3 days ago" : if for some reason the script doesn't run one night, you are not going to miss anything.
## What is the script doing?
* Creating the tables ***bills*** and ***billsDetails*** if they don't exist,
* Listing all the bills,
* Getting them as PDF files,
* Getting the details of each bills in the tables,
* Setting the amount of "deposit" for each bill,
* Setting the amount of "outConsumption" for each bill.
## PHP wrapper of OVH API
......
......@@ -22,6 +22,8 @@ define('BILLS_PATH', 'bills');
define('START', '1 year ago');
#define('START', '1 month ago');
#define('START', '1 week ago');
#define('START', '3 days ago');
#define('START', '2 days ago');
#define('START', '1 day ago');
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment