Пример #1
0
function populate_cedole()
{
    $bond_factory = new Bond();
    $today = date('Y-m-d');
    // $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 `scadenza` > '{$today}'"));
    // print_r($bonds);
    foreach ($bonds as $bond) {
        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;
        }
    }
}
 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);
 }