function add() { if ($this->request->is_post()) { $item = new Dividend($_POST); $item->save(); $this->redirect_to(array('action' => 'index')); } else { $stock_factory = new Stock(); $stocks = $stock_factory->find_all(array('where_clause' => '`attivo` = 1', 'order_by' => '`title` ASC')); $this->stocks = array('-1' => 'Scegli...'); foreach ($stocks as $stock) { $this->stocks[$stock->isin] = $stock->title; } } }
<?php $dividend_factory = new Dividend(); $dividends = $dividend_factory->find_all(array('where_clause' => "`isin` = '{$this->stock->isin}'" . 'AND `stacco` > NOW()', 'order_by' => '`stacco` ASC', 'limit' => 1)); if (count($dividends) > 0) { $dividend = $dividends[0]; ?> <tr class="row1"> <th></th> <th>Evento</th> <th>Data</th> <th>Importo lordo</th> <th>Importo unitario</th> </tr> <tr class="row0"> <td class="cell10"><div class="vstrut17"></div></td> <td class="cell10">Dividendo</td> <td class="cell10"><b><?php print $dividend->pretty_date(); ?> </b></td> <td class="cell10"><?php $portfolio = new PortfolioStock(); $ports = $portfolio->find_all(array('where_clause' => "`isin` = '{$this->stock->isin}'")); if (count($ports) > 0) { $port = $ports[0]; $revenue = $port->quantita * $dividend->importo; printf('%.2f %s', $revenue, $this->stock->divisa); if ($this->stock->divisa != 'EUR') {
private function get_cedole() { $payments = array(); $month_from = strtotime($_REQUEST['month-from']); $month_to = strtotime($_REQUEST['month-to']); $conn = $this->get_connection(); $portfolio_stock_factory = new PortfolioStock(); $portfolio_stocks = $portfolio_stock_factory->find_by_query('SELECT `creso_portfolio`.`id`, `creso_portfolio`.`quantita`, `creso_portfolio`.`isin`, `creso_titoli`.`tipo`, `creso_titoli`.`title` ' . 'FROM `creso_portfolio` ' . 'LEFT JOIN `creso_titoli` ON `creso_portfolio`.`isin` = `creso_titoli`.`isin` ' . "WHERE `utente` = '{$conn->escape($_COOKIE['username'])}' " . (!empty($_REQUEST['mercato']) ? "AND `mercato` = '{$conn->escape($_REQUEST['mercato'])}' " : '') . (!empty($_REQUEST['tipo']) ? "AND `tipo` = '{$conn->escape($_REQUEST['tipo'])}' " : '') . (!empty($_REQUEST['divisa']) ? "AND `divisa` = '{$conn->escape($_REQUEST['divisa'])}' " : '') . (!empty($_REQUEST['isin']) ? "AND `creso_portfolio`.`isin` = '{$conn->escape($_REQUEST['isin'])}' " : '') . 'ORDER BY ' . sprintf('`%s` %s ', $this->get_sort('isin'), $this->get_dir()) . "LIMIT {$conn->escape($this->get_start())}, {$conn->escape($this->get_limit())}"); if (count($portfolio_stocks) == 0 && !empty($_REQUEST['isin'])) { $portfolio_stocks = $this->synthesize_portfolio_stocks(); } if (count($portfolio_stocks) > 0) { foreach ($portfolio_stocks as $portfolio_stock) { $stock = new Stock(); $stock->find_by_id($portfolio_stock->isin); if ($this->historiful) { $quantita_history = $this->backfill_with_historical_data($portfolio_stock); } switch ($stock->tipo) { case 'azione': $dividend_factory = new Dividend(); $dividend = $dividend_factory->find_all(array('where_clause' => "`isin` = '{$portfolio_stock->isin}'"))[0]; if ($portfolio_stock->quantita > 0) { $payment = new Payment(); $q = $portfolio_stock->quantita; foreach ($quantita_history as $date => $quantita) { if ($date < date('Y-m-d', $stacco)) { break; } $q = $quantita; } $payment->stock = $stock; $payment->portfolio_stock = $portfolio_stock; $payment->dividend = $dividend; $payment->transactions = $transactions; $payment->timestamp = strtotime($dividend->stacco); $payment->importo = $q * $dividend->importo; $payment->quantita = $q; $payment->tipo = 'dividendo'; $payments[] = $payment; } break; case 'obbligazione': $bond_factory = new Bond(); $bond = $bond_factory->find_all(array('where_clause' => "`isin` = '{$portfolio_stock->isin}'"))[0]; if (!empty($bond->stacco)) { $stacco = strtotime($bond->stacco); // FIXME: bug, se emissione è vuoto, il prossimo stacco utile può avvenire in una data precedente if ($bond->emissione == '0000-00-00') { $stacco_precedente = $stacco; do { $stacco = $stacco_precedente; $stacco_precedente = mktime(0, 0, 0, date('m', $stacco) - $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } while ($stacco_precedente >= $month_from); } while ($stacco < $month_from) { // Sommo i mesi di cadenza fino a giungere ad una data futura $stacco = mktime(0, 0, 0, date('m', $stacco) + $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } if ($stacco > strtotime($bond->scadenza) || $stacco > $month_to) { continue; } do { $payment = new Payment(); $q = $portfolio_stock->quantita; foreach ($quantita_history as $date => $quantita) { if ($date < date('Y-m-d', $stacco)) { break; } $q = $quantita; } if ($q > 0) { $payment->stock = $stock; $payment->portfolio_stock = $portfolio_stock; $payment->bond = $bond; $payment->transactions = $transactions; $payment->timestamp = $stacco; $payment->importo = $q * $bond->tasso / (100 * $bond->cedole_per_anno()); $payment->quantita = $q; $payment->tipo = 'cedola'; $payments[] = $payment; } $stacco = mktime(0, 0, 0, date('m', $stacco) + $bond->cadenza, date('d', $stacco), date('Y', $stacco)); } while ($stacco <= strtotime($bond->scadenza) && $stacco <= $month_to); } break; } } } return $payments; }
public static function yields($isin, $month_from, $month_to) { $payments = array(); $conn = Db::get_connection(); $stock = new Stock(); if (!$stock->find_by_id($isin)) { return; } switch ($stock->tipo) { case 'azione': $dividend_factory = new Dividend(); $dividend = $dividend_factory->find_all(array('where_clause' => "`isin` = '{$isin}'"))[0]; $payment = new Payment(); $payment->stock = $stock; $payment->dividend = $dividend; $payment->timestamp = strtotime($dividend->stacco); $payment->importo = $dividend->importo; $payment->tipo = 'dividendo'; $payments[] = $payment; break; case 'obbligazione': $bond = new Bond(); if (!$bond->find_by_id($isin)) { return; } if ($bond->zero_coupon == 1) { return; } if (!is_numeric($bond->cadenza)) { return; } if ($bond->tasso == 0) { return; } if ($bond->emissione == '0000-00-00' && $bond->stacco == '0000-00-00') { return; } if (!empty($bond->stacco)) { $stacco = strtotime($bond->stacco); // FIXME: bug, se emissione è vuoto, il prossimo stacco utile può avvenire in una data precedente if ($bond->emissione == '0000-00-00') { $stacco_precedente = $stacco; do { $stacco = $stacco_precedente; $stacco_precedente = mktime(0, 0, 0, date("m", $stacco) - $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } while ($stacco_precedente >= strtotime($month_from)); } while ($stacco < strtotime($month_from)) { // Sommo i mesi di cadenza fino a giungere ad una data futura $stacco = mktime(0, 0, 0, date("m", $stacco) + $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } if ($stacco > strtotime($bond->scadenza) || $stacco > strtotime($month_to)) { break; } do { $payment = new Payment(); $payment->stock = $stock; $payment->bond = $bond; $payment->timestamp = $stacco; $payment->importo = $bond->tasso / (100 * $bond->cedole_per_anno()); $payment->tipo = 'cedola'; $payments[] = $payment; $stacco = mktime(0, 0, 0, date("m", $stacco) + $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } while ($stacco <= strtotime($bond->scadenza) && $stacco <= strtotime($month_to)); } break; } Db::close_connection($conn); return $payments; }