Skip to content

Commit

Permalink
Added usd rate column
Browse files Browse the repository at this point in the history
  • Loading branch information
rustyJ4ck committed May 12, 2015
1 parent 078f837 commit 3db7ee5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
27 changes: 27 additions & 0 deletions app/Currency/Cbrf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* ProjectMayhem
* @author Golovkin Vladimir <[email protected]> http://www.skillz.ru
*/

namespace YMon\Currency;

class Cbrf {

function getUSDRate()
{
// simplexml?
$buff = file_get_contents('http://www.cbr.ru/scripts/XML_daily.asp');

if ($buff) {
preg_match(
'/<CharCode>USD<\/CharCode>.*<Value>([\d\,]+)<\/Value>/Us', $buff, $m
);
$usd = str_replace(',','.',$m[1]);
return round(floatval($usd), 2);

}
}
}

24 changes: 22 additions & 2 deletions app/YMon.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

namespace YMon;

use PHPExcel_Cell;
use YMon\Currency\Cbrf;
use YMon\Model\Product;
use YMon\Util\Logger;

class YMon {

protected $usdColumn = 'M';

/** @var int Update interval, hours */
private $updateInterval = 6;
protected $updateInterval = 6;

protected $sheetsPath;

private $sheetsPath;
private $dryRun = false;

function __construct($root = false) {
$this->sheetsPath = $root ? $root : (__DIR__ . '/../sheets');
Expand Down Expand Up @@ -127,6 +133,7 @@ function parseBook($book) {
if (!$uptodate) {
$updated++;
$this->updateProductsRow($sheet, $products, $rowID);
$this->updateUSDRate($sheet, $rowID);
}

break;
Expand All @@ -141,6 +148,17 @@ function parseBook($book) {
return $updated;
}

/**
* @param \PHPExcel_Worksheet $sheet
*/
function updateUSDRate($sheet, $rowID) {
$column = PHPExcel_Cell::columnIndexFromString($this->usdColumn) - 1;
$cell = $sheet->getCellByColumnAndRow($column, $rowID);
$usdRate = new Cbrf();
$cell->setValue($value = $usdRate->getUSDRate());
Logger::d('.. USD$ %.2f %d %d', $value, $column, $rowID);
}

/**
* @param \PHPExcel_Worksheet $sheet
* @param $products
Expand Down Expand Up @@ -192,6 +210,8 @@ function readExcel($inputFileName) {

function saveExcel($excel, $file) {

if ($this->dryRun) return;

Logger::d('XLS.Save: %s', $file);
$type = preg_match('@\.xlsx$@', $file) ? 'Excel2007' : 'Excel5';
$writer = \PHPExcel_IOFactory::createWriter($excel, $type);
Expand Down
Binary file modified sheets/example.xlsx
Binary file not shown.

0 comments on commit 3db7ee5

Please sign in to comment.