public function by_day() { $conn = $this->get_connection(); $cedola_factory = new Cedola(); $month_start = date('Y-m-d', mktime(0, 0, 0, $_REQUEST['id'], 1, date('Y'))); $next_month = date('Y-m-d', mktime(0, 0, 0, $_REQUEST['id'] + 1, 1, date('Y'))); $cedole = $cedola_factory->find_all(array('where_clause' => "`stacco` >= '{$month_start}' AND `stacco` < '{$next_month}'", 'order_by' => '`tasso` DESC, `stacco` ASC')); $portfolio_stock_factory = new PortfolioStock(); $portfolio_stocks = $portfolio_stock_factory->find_all(array(`where_clause` => "`utente` = '{$conn->escape($_COOKIE['username'])}'")); $in_portfolio = []; foreach ($portfolio_stocks as $portfolio_stock) { $in_portfolio[] = $portfolio_stock->isin; } // print_r($cedole); $this->cedole = []; foreach ($cedole as $cedola) { $cedola->stock = new Stock(); $cedola->stock->find_by_id($cedola->isin); $cedola->in_portfolio = in_array($cedola->isin, $in_portfolio); $this->cedole[] = $cedola; } // print_r($this->cedole); $this->cedole_by_day = array(); foreach ($this->cedole as $cedola) { $s = strtotime($cedola->stacco); $d = date('d', $s); if (!isset($this->cedole_by_month[$d])) { $this->cedole_by_month[$d] = array(); } $this->cedole_by_day[$d][] = $cedola; } ksort($this->cedole_by_day); // print_r($this->cedole_by_day); }
public function run($context) { $bond_count = 0; $yield_count = 0; $bond_factory = new Bond(); $today = date('Y-m-d'); foreach ($context->stocks as $stock) { if ($stock->tipo != 'obbligazione') { // printf("%s is not a bond, skipping\n", $stock->isin); continue; } // $bonds = $bond_factory->find_all(array('where_clause' => "`stacco` != '0000-00-00' AND `cadenza` > 0 AND `scadenza` > '{$today}'")); $bond = new Bond(); if (!$bond->find_by_id($stock->isin)) { // printf("%s does not have a bond, skipping\n", $stock->isin); continue; } // Cleanup yields beyond expiration $cedola_factory = new Cedola(); $cedole = $cedola_factory->find_all(array('where_clause' => "`isin` = '{$bond->isin}' " . "AND `stacco` > '{$bond->scadenza}'")); if (count($cedole) > 0) { foreach ($cedole as $cedola) { // printf("Deleting yield of stock %s beyond expiration %s\n", $stock->isin, $cedola->stacco); $cedola->delete(); } } // $bonds = $bond_factory->find_all(array('where_clause' => "`stacco` != '0000-00-00' AND `cadenza` > 0 AND `scadenza` > '{$today}'")); $bonds = $bond_factory->find_all(array('where_clause' => '`cadenza` > 0 ' . "AND `isin` = '{$stock->isin}' " . "AND `zero_coupon` = 0 " . "AND `scadenza` > '{$today}'")); if (count($bonds) < 1) { // printf("No valid bond found with isin %s\n", $stock->isin); continue; } $bond = $bonds[0]; if ($bond->stacco != '0000-00-00') { $s = strtotime($bond->stacco); $k = 1; } else { $s = strtotime($bond->scadenza); $k = -1; } $d = $today = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $c = 0; while ($d >= $today && $d <= $s) { $m = date('n', $s) + $c; $d = mktime(0, 0, 0, $m, date('d', $s), date('Y')); $cedola = new Cedola(array('isin' => $bond->isin, 'stacco' => date('Y-m-d', $d), 'tasso' => $bond->tasso)); $cedola->_force_create = TRUE; $cedola->_ignore = TRUE; $cedola->save(); $c += $k * $bond->cadenza; $yield_count++; // printf("Creating new yield of stock %s on %s\n", $stock->isin, $cedola->stacco); } $bond_count++; } printf("%s updated %d yields for %d bonds\n", get_called_class(), $yield_count, $bond_count); }