public function fetch_report_data() { global $FANNIE_PLUGIN_SETTINGS; $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); // compound interest calculation is MySQL-specific $query = 'SELECT CASE WHEN shares > 0 THEN \'PURCHASE\' ELSE \'PAYOFF\' END as stype, SUM(shares) AS totalS, SUM(value) AS totalV FROM GumEquityShares GROUP BY CASE WHEN shares > 0 THEN \'PURCHASE\' ELSE \'PAYOFF\' END ORDER BY CASE WHEN shares > 0 THEN \'PURCHASE\' ELSE \'PAYOFF\' END DESC'; $result = $dbc->query($query); $reports = array(); $data = array(); while ($row = $dbc->fetch_row($result)) { $record = array($row['stype'], sprintf('%d', $row['totalS']), sprintf('%.2f', $row['totalV'])); $data[] = $record; } $reports[] = $data; $data = array(); $model = new GumEquitySharesModel($dbc); foreach ($model->find('tdate') as $obj) { $record = array(date('Y-m-d', strtotime($obj->tdate())), $obj->trans_num(), $obj->card_no(), $obj->shares(), sprintf('%.2f', $obj->value())); $data[] = $record; } $reports[] = $data; return $reports; }
public function fetch_report_data() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); $dt = FormLib::get('endDate', date('Y-m-d')); $end = strtotime($dt); $endDT = new DateTime(date('Y-m-d', $end)); $start = mktime(0, 0, 0, date('n', $end), date('j', $end) + 1, date('Y', $end) - 1); $startDT = new DateTime(date('Y-m-d', $start)); $num_days = $startDT->diff($endDT)->format('%r%a') + 1; $shares = new GumEquitySharesModel($dbc); // accumulate per-member purchases and payoffs // in date order $buys = array(); $sells = array(); foreach ($shares->find(array('card_no', 'tdate')) as $share) { $cn = $share->card_no(); if (!isset($buys[$cn])) { $buys[$cn] = array(); } if (!isset($sells[$cn])) { $sells[$cn] = array(); } if ($share->shares() > 0) { $buys[$cn][] = $share; } else { $sells[$cn][] = $share; } } // go through payoffs and decrement oldest // purchases foreach ($sells as $cn => $sales) { foreach ($sales as $sale) { $num_sold = abs($sale->shares()); $amt_sold = abs($sale->value()); while ($num_sold > 0) { for ($i = 0; $i < count($buys[$cn]); $i++) { if ($num_sold > $buys[$cn][$i]->shares()) { $num_sold -= $buys[$cn][$i]->shares(); $buys[$cn][$i]->shares(0); $amt_sold -= $buys[$cn][$i]->value(); $buys[$cn][$i]->value(0); } else { $buys[$cn][$i]->shares($buys[$cn][$i]->shares() - $num_sold); $num_sold = 0; $buys[$cn][$i]->value($buys[$cn][$i]->value() - $amt_sold); $amt_sold = 0; break; } } // if number sold isn't zero after // examining all purchases, there's a data // problem. force while loop to end. $num_sold = 0; } } } $data = array(); foreach ($buys as $cn => $shares) { foreach ($shares as $share) { $record = array($cn); $purchased = strtotime($share->tdate()); $record[] = date('Y-m-d', $purchased); $record[] = $share->shares(); $record[] = number_format($share->value(), 2); $tempDT = new DateTime(date('Y-m-d', $purchased)); $held = $num_days; if ($startDT->diff($tempDT)->format('%r%a') > 0) { $held = $tempDT->diff($endDT)->format('%r%a') + 1; if ($held <= 0) { continue; } } $record[] = $held; for ($i = 1; $i <= 5; $i++) { $dividend = $share->value() * ($i / 100.0) * ($held / (double) $num_days); $record[] = number_format($dividend, 2); } $data[] = $record; } } return $data; }
public function get_id_creceipt_cid_handler() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB; $bridge = GumLib::getSetting('posLayer'); $this->custdata = $bridge::getCustdata($this->id); $this->meminfo = $bridge::getMeminfo($this->id); $uid = FannieAuth::getUID($this->current_user); // bridge may change selected database $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); $msg = 'Dear ' . $this->custdata->FirstName() . ' ' . $this->custdata->LastName() . ',' . "\n"; $msg .= "\n"; $msg .= 'Class C Stock Purchase Receipt:' . "\n"; $msg .= "\n"; $model = new GumEquitySharesModel($dbc); $model->gumEquityShareID($this->cid); $model->load(); $spacer = str_repeat(' ', 6); $msg .= $spacer . 'Owner Number: ' . $this->id . "\n"; $msg .= $spacer . 'Date/Time: ' . $model->tdate() . "\n"; $msg .= $spacer . 'No. of Shares: ' . $model->shares() . "\n"; $msg .= $spacer . 'Purchase Amount: $' . number_format($model->value(), 2) . "\n"; $msg .= "\n"; $model->reset(); $model->card_no($this->id); $shares = 0; $value = 0; $purchases = 0; foreach ($model->find() as $obj) { $shares += $obj->shares(); $value += $obj->value(); if ($shares > 0) { $purchases++; } } if ($purchases > 1) { $msg .= 'Total Class C Stock Owned:' . "\n"; $msg .= $spacer . 'No. of Shares: ' . $shares . "\n"; $msg .= $spacer . 'Value of Shares: $' . number_format($value, 2) . "\n"; $msg .= "\n"; } $msg .= wordwrap('Whole Foods Co-op recognizes and thanks you for your support and purchase of Class C Stock. It is important that we maintain your current contact information so that we can deliver any dividends you may earn. Please reply to this email or to finance@wholefoods.coop with any questions or concerns. Or you may also call 218-728-0884, ask for Finance, and we will gladly assist you.') . "\n"; $msg .= "\n"; $msg .= 'Dale Maiers' . "\n"; $msg .= 'Finance Manager' . "\n"; $subject = 'WFC Owner Financing: Class C Stock Receipt'; $to = $this->meminfo->email_1(); $headers = 'From: Whole Foods Co-op <*****@*****.**>' . "\r\n" . 'Reply-To: finance@wholefoods.coop' . "\r\n"; $log = new GumEmailLogModel($dbc); $log->card_no($this->id); $log->tdate(date('Y-m-d H:i:s')); $log->uid($uid); $log->messageType('Equity Receipt (' . $this->cid . ')'); if (FormLib::get('sendAs') == 'print') { echo '<pre>' . $msg . '</pre>'; return false; } else { if (mail($to, $subject, $msg, $headers)) { $log->save(); header('Location: GumEmailPage.php?id=' . $this->id); } else { echo 'Error: unable to send email. Notify IT'; } } return false; }
public function post_id_shares_type_handler() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB, $FANNIE_URL; $bridge = GumLib::getSetting('posLayer'); $meminfo = $bridge::getMeminfo($this->id); $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); $settings = new GumSettingsModel($dbc); $settings->key('equityShareSize'); $settings->load(); if ($this->shares != 0) { $model = new GumEquitySharesModel($dbc); $model->card_no($this->id); $bal = 0.0; foreach ($model->find() as $obj) { $bal += $obj->value(); } if (strtolower($this->type) == 'payoff') { $this->shares *= -1; } $model->shares($this->shares); $model->value($this->shares * $settings->value()); $model->tdate(date('Y-m-d H:i:s')); // payoff cannot exceed balance if ($model->value() > 0 || $model->value() < 0 && abs($model->value()) <= $bal) { $newid = $model->save(); // share purchase & email exists // use curl to call email page's request handler if ($this->shares > 0 && $meminfo->email_1() != '') { $url = 'http://localhost' . $FANNIE_URL . 'modules/plugins2.0/GiveUsMoneyPlugin/GumEmailPage.php?id=' . $this->id . '&creceipt=1&cid=' . $newid; $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $res = curl_exec($handle); curl_close($handle); } $model->gumEquityShareID($newid); $model->load(); $emp = GumLib::getSetting('emp_no', 1001); $reg = GumLib::getSetting('register_no', 30); $dept = GumLib::getSetting('equityPosDept', 993); $desc = GumLib::getSetting('equityDescription', 'Class C Stock'); $offset = GumLib::getSetting('offsetPosDept', 800); $bridge = GumLib::getSetting('posLayer', 'GumCoreLayer'); if (class_exists($bridge)) { $line1 = array('department' => $dept, 'description' => $desc, 'amount' => $model->value(), 'card_no' => $model->card_no()); $line2 = array('department' => $offset, 'description' => 'OFFSET ' . $desc, 'amount' => -1 * $model->value(), 'card_no' => $model->card_no()); $trans_identifier = $bridge::writeTransaction($emp, $reg, array($line1, $line2)); if ($trans_identifier !== true && $trans_identifier !== false) { $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); $model->trans_num($trans_identifier); $model->save(); } } } } header('Location: GumMainPage.php?id=' . $this->id); return false; }
/** Determine how long shares were held @param $endDate [string] end of fiscal year @return [keyed array] card_no => [keyed array] days_held => share value */ private function calculateHoldings($endDate) { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['GiveUsMoneyDB']); $end = strtotime($endDate); $endDT = new DateTime(date('Y-m-d', $end)); $start = mktime(0, 0, 0, date('n', $end), date('j', $end) + 1, date('Y', $end) - 1); $startDT = new DateTime(date('Y-m-d', $start)); $num_days = $this->fyLength($endDate); $shares = new GumEquitySharesModel($dbc); // accumulate per-member purchases and payoffs // in date order $buys = array(); $sells = array(); foreach ($shares->find(array('card_no', 'tdate')) as $share) { $cn = $share->card_no(); if (!isset($buys[$cn])) { $buys[$cn] = array(); } if (!isset($sells[$cn])) { $sells[$cn] = array(); } if ($share->shares() > 0) { $buys[$cn][] = $share; } else { $sells[$cn][] = $share; } } // go through payoffs and decrement oldest // purchases foreach ($sells as $cn => $sales) { foreach ($sales as $sale) { $num_sold = abs($sale->shares()); $amt_sold = abs($sale->value()); while ($num_sold > 0) { for ($i = 0; $i < count($buys[$cn]); $i++) { if ($num_sold > $buys[$cn][$i]->shares()) { $num_sold -= $buys[$cn][$i]->shares(); $buys[$cn][$i]->shares(0); $amt_sold -= $buys[$cn][$i]->value(); $buys[$cn][$i]->value(0); } else { $buys[$cn][$i]->shares($buys[$cn][$i]->shares() - $num_sold); $num_sold = 0; $buys[$cn][$i]->value($buys[$cn][$i]->value() - $amt_sold); $amt_sold = 0; break; } } // if number sold isn't zero after // examining all purchases, there's a data // problem. force while loop to end. $num_sold = 0; } } } $holdings = array(); // calculate days held for each share // combine shares held an equal number of days // (e.g., a full year) into one record foreach ($buys as $cn => $shares) { $holdings[$cn] = array(); foreach ($shares as $share) { $purchased = strtotime($share->tdate()); $tempDT = new DateTime(date('Y-m-d', $purchased)); $held = $num_days; if ($startDT->diff($tempDT)->format('%r%a') > 0) { $held = $tempDT->diff($endDT)->format('%r%a') + 1; if ($held <= 0) { continue; } } if (!isset($holdings[$cn][$held])) { $holdings[$cn][$held] = 0.0; } $holdings[$cn][$held] += $share->value(); } } return $holdings; }