コード例 #1
0
ファイル: payments_helper.php プロジェクト: emeraldion/creso
 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;
 }