Exemple #1
0
 function exchange($val)
 {
     $date = sqlDate('now');
     $kes = $this->getDi()->currencyExchangeTable->getRate('KES', $date);
     $usd = $this->getDi()->currencyExchangeTable->getRate('USD', $date);
     return moneyRound($val * $kes / $usd);
 }
Exemple #2
0
 public function calculate(Invoice $invoiceBill)
 {
     $this->coupon = $invoiceBill->getCoupon();
     $this->user = $invoiceBill->getUser();
     $isFirstPayment = $invoiceBill->isFirstPayment();
     foreach ($invoiceBill->getItems() as $item) {
         $item->first_discount = $item->second_discount = 0;
         $item->_calculateTotal();
     }
     if (!$this->coupon) {
         return;
     }
     if ($this->coupon->getBatch()->discount_type == Coupon::DISCOUNT_PERCENT) {
         foreach ($invoiceBill->getItems() as $item) {
             if ($this->coupon->isApplicable($item->item_type, $item->item_id, $isFirstPayment)) {
                 $item->first_discount = moneyRound($item->first_total * $this->coupon->getBatch()->discount / 100);
             }
             if ($this->coupon->isApplicable($item->item_type, $item->item_id, false)) {
                 $item->second_discount = moneyRound($item->second_total * $this->coupon->getBatch()->discount / 100);
             }
         }
     } else {
         // absolute discount
         $discountFirst = $this->coupon->getBatch()->discount;
         $discountSecond = $this->coupon->getBatch()->discount;
         $first_discountable = $second_discountable = array();
         $first_total = $second_total = 0;
         $second_total = array_reduce($second_discountable, create_function('$s,$item', 'return $s+=$item->second_total;'), 0);
         foreach ($invoiceBill->getItems() as $item) {
             if ($this->coupon->isApplicable($item->item_type, $item->item_id, $isFirstPayment)) {
                 $first_total += $item->first_total;
                 $first_discountable[] = $item;
             }
             if ($this->coupon->isApplicable($item->item_type, $item->item_id, false)) {
                 $second_total += $item->second_total;
                 $second_discountable[] = $item;
             }
         }
         if ($first_total) {
             $k = max(0, min($discountFirst / $first_total, 1));
             // between 0 and 1!
             foreach ($first_discountable as $item) {
                 $item->first_discount = moneyRound($item->first_total * $k);
             }
         }
         if ($second_total) {
             $k = max(0, min($discountSecond / $second_total, 1));
             // between 0 and 1!
             foreach ($second_discountable as $item) {
                 $item->second_discount = moneyRound($item->second_total * $k);
             }
         }
     }
     foreach ($invoiceBill->getItems() as $item) {
         $item->_calculateTotal();
     }
 }
Exemple #3
0
 public function calculatePiece(stdClass $fields)
 {
     if (!$fields->no_tax) {
         $fields->tax = moneyRound($fields->total * $this->tax_rate / 100);
     } else {
         $fields->tax = 0.0;
     }
     $fields->total += $fields->tax;
 }
 public function assertAmount($expected, $got, $whereMsg = "amount")
 {
     if ($e = moneyRound($expected) != ($g = moneyRound($got))) {
         throw new Am_Exception_Paysystem_TransactionInvalid("Transaction {$whereMsg} [{$g}] does not match expected [{$e}]");
     }
 }
Exemple #5
0
 public function getAmount()
 {
     return moneyRound($this->request->get('price'));
 }
 /**
  * Refresh totals according to currently selected
  *   items, _coupon, user and so ons
  * Should be called on a fresh invoice only, because
  * it may break reporting later if called on a paid
  * invoice
  * @return Invoice provides fluent interface
  */
 function calculate()
 {
     foreach ($this->getCalculators() as $calc) {
         $calc->calculate($this);
     }
     // now summarize all items to invoice totals
     $priceFields = array('first_subtotal' => null, 'first_discount' => 'first_discount', 'first_tax' => 'first_tax', 'first_shipping' => 'first_shipping', 'first_total' => 'first_total', 'second_subtotal' => null, 'second_discount' => 'second_discount', 'second_tax' => 'second_tax', 'second_shipping' => 'second_shipping', 'second_total' => 'second_total');
     foreach ($priceFields as $k => $kk) {
         $this->{$k} = 0.0;
     }
     foreach ($this->getItems() as $item) {
         $this->first_subtotal += moneyRound($item->first_price * $item->qty);
         $this->second_subtotal += moneyRound($item->second_price * $item->qty);
         foreach ($priceFields as $k => $kk) {
             $this->{$k} += $kk ? $item->{$kk} : 0;
         }
     }
     foreach ($priceFields as $k => $kk) {
         $this->{$k} = moneyRound($this->{$k});
     }
     /// set periods, it has been checked for compatibility in @see add()
     foreach ($this->getItems() as $item) {
         $this->currency = $item->currency;
         if (!@$this->first_period) {
             $this->first_period = $item->first_period;
         }
         if (!@$this->second_period) {
             $this->second_period = $item->second_period;
         }
         $this->rebill_times = max(@$this->rebill_times, $item->rebill_times);
     }
     if ($this->currency == Am_Currency::getDefault()) {
         $this->base_currency_multi = 1.0;
     } else {
         $this->base_currency_multi = $this->getDi()->currencyExchangeTable->getRate($this->currency, sqlDate(!empty($this->tm_added) ? $this->tm_added : $this->getDi()->sqlDateTime));
         if (!$this->base_currency_multi) {
             $this->base_currency_multi = 1;
         }
     }
     $this->getDi()->hook->call(Am_Event::INVOICE_CALCULATE, array('invoice' => $this));
     return $this;
 }
Exemple #7
0
 public function calculatePiece(stdClass $fields)
 {
     $orig_price = $this->item->data()->get('orig_' . $this->currentPrefix . 'price');
     $fields->price = $orig_price ? $orig_price : $fields->price;
     $fields->tax = $fields->discount = $fields->shipping = 0.0;
     $fields->total = moneyRound($fields->price * $fields->qty);
 }
Exemple #8
0
 function _setExpressAmounts(Invoice $invoice)
 {
     $this->addPostParameter('PAYMENTREQUEST_0_AMT', $invoice->first_total);
     $this->addPostParameter('PAYMENTREQUEST_0_CURRENCYCODE', $invoice->currency);
     // @todo
     $this->addPostParameter('PAYMENTREQUEST_0_ITEMAMT', $invoice->first_total - $invoice->first_tax);
     //        $this->addPostParameter('PAYMENTREQUEST_0_SHIPPINGAMT', $invoice->first_shipping);
     $this->addPostParameter('PAYMENTREQUEST_0_TAXAMT', $invoice->first_tax);
     $this->addPostParameter('PAYMENTREQUEST_0_INVNUM', $invoice->getSecureId('paypal'));
     $this->addPostParameter('PAYMENTREQUEST_0_NOTIFYURL', $this->plugin->getPluginUrl('ipn'));
     $this->addPostParameter('PAYMENTREQUEST_0_PAYMENTACTION', 'Sale');
     $i = 0;
     foreach ($invoice->getItems() as $item) {
         /* @var $item InvoiceItem */
         $this->addPostParameter('L_PAYMENTREQUEST_0_NAME' . $i, $item->item_title);
         $this->addPostParameter('L_PAYMENTREQUEST_0_AMT' . $i, moneyRound(($item->first_total - $item->first_tax) / $item->qty));
         //            $this->addPostParameter('L_PAYMENTREQUEST_0_ITEMAMT'.$i, $item->getFirstSubtotal());
         //            $this->addPostParameter('L_PAYMENTREQUEST_0_NUMBER'.$i, $item->item_id);
         $this->addPostParameter('L_PAYMENTREQUEST_0_QTY' . $i, $item->qty);
         //            $this->addPostParameter('L_PAYMENTREQUEST_0_TAXAMT'.$i, $item->first_tax);
         /// The unique non-changing identifier for the seller at the marketplace site. This ID is not displayed.
         //$this->addPostParameter('L_PAYMENTREQUEST_0_SELLERID'.$i, );
         // PAYMENTREQUEST_n_SELLERPAYPALACCOUNTID
         $i++;
     }
     if ($invoice->rebill_times) {
         $this->addPostParameter('L_BILLINGTYPE0', 'RecurringPayments');
         $this->addPostParameter('L_BILLINGAGREEMENTDESCRIPTION0', $invoice->getTerms());
     }
 }
Exemple #9
0
 public function getAmount()
 {
     return moneyRound($this->request->get('trans_amount'));
 }
Exemple #10
0
 public function calculatePiece(stdClass $fields)
 {
     $fields->tax = $fields->discount = $fields->shipping = 0.0;
     $fields->total = moneyRound($fields->price * $fields->qty);
 }
Exemple #11
0
 public function getAmount()
 {
     return moneyRound($this->vars['Amount']);
 }
Exemple #12
0
 function setAmount($amount)
 {
     $this->amount = moneyRound($amount);
     return $this;
 }
 function getPlannedRebills($start, $stop)
 {
     $row = $this->getDi()->db->selectRow("\n            SELECT \n                COUNT(*) AS cnt, \n                SUM(second_total) AS total \n            FROM ?_invoice\n            WHERE rebill_date BETWEEN DATE(?) AND DATE(?)\n            ", sqlTime(strtotime($start)), sqlTime(strtotime($stop)));
     return array((int) $row['cnt'], moneyRound($row['total']));
 }
Exemple #14
0
 /**
  * Return unused amount for $item subscription
  * null will be returned if:
  *   - subscription is lifetime
  *   - subscription is for free product
  *   - subscription is expired
  * @param Invoice $invoice
  * @param InvoiceItem $item
  * @return float|null
  */
 function getUnusedAmount(Invoice $invoice, InvoiceItem $item)
 {
     $row = $this->getDi()->db->selectRow("\n            SELECT begin_date, expire_date \n            FROM ?_access\n            WHERE invoice_id=?d AND product_id=?\n            ORDER by expire_date desc LIMIT 1 \n            ", $invoice->pk(), $item->item_id);
     if (!$row) {
         return;
     }
     $maxExpire = $row['expire_date'];
     $maxBegin = $row['begin_date'];
     if ($maxExpire < $this->getDi()->sqlDate) {
         return null;
     }
     if ($maxExpire == Am_Period::MAX_SQL_DATE) {
         return null;
     }
     $daysTotal = $this->diffDays($maxBegin, $maxExpire);
     $daysUnused = $this->diffDays($this->getDi()->sqlDate, $maxExpire) - 1;
     // -1 as today date can be fully used
     $pc = $invoice->getPaymentsCount();
     $field = $pc == 1 && (double) $invoice->first_total || $pc == 0 ? 'first_total' : 'second_total';
     $paid = $item->get($field);
     return moneyRound($daysUnused * $paid / $daysTotal);
 }
Exemple #15
0
 public function getAmount()
 {
     return moneyRound($this->request->get('IPN_TOTALGENERAL'));
 }
    function getSaleCode(Invoice $invoice, InvoicePayment $payment)
    {
        if ($this->getDi()->config->get('analytics_version', 'google') == 'universal') {
            $out = <<<CUT
<script type="text/javascript">
    ga('create', '{$this->id}', 'auto');
    ga('send', 'pageview');
</script>
CUT;
        } else {
            $out = <<<CUT

<script type="text/javascript">
if (typeof(_gaq)=='object') { // sometimes google-analytics can be blocked and we will avoid error
    _gaq.push(['_setAccount', '{$this->id}']);
    _gaq.push(['_trackPageview']);
}
</script>
CUT;
        }
        if (empty($payment->amount) && !$this->getDi()->config->get('google_analytics_track_free_signups')) {
            return $out;
        } elseif (empty($payment->amount)) {
            $a = array($invoice->public_id, $this->getDi()->config->get('site_title'), 0, 0, 0, $invoice->getCity(), $invoice->getState(), $invoice->getCountry());
        } else {
            $a = array($payment->transaction_id, $this->getDi()->config->get('site_title'), $payment->amount - $payment->tax - $payment->shipping, (double) $payment->tax, (double) $payment->shipping, $invoice->getCity(), $invoice->getState(), $invoice->getCountry());
        }
        $a = implode(",\n", array_map('json_encode', $a));
        $items = "";
        foreach ($invoice->getItems() as $item) {
            if ($this->getDi()->config->get('analytics_version', 'google') == 'universal') {
                $it = json_encode(array('id' => $payment->transaction_id, 'name' => $item->item_title, 'sku' => $item->item_id, 'price' => moneyRound($item->first_total / $item->qty), 'quantity' => $item->qty));
                $items .= "ga('ecommerce:addItem', {$it});\n";
            } else {
                $items .= "['_addItem', '{$payment->transaction_id}', '{$item->item_id}', '{$item->item_title}','', {$item->first_total}, {$item->qty}],";
            }
        }
        if ($this->getDi()->config->get('analytics_version', 'google') == 'universal') {
            $tr = json_encode(array('id' => $payment->transaction_id, 'affiliation' => $this->getDi()->config->get("site_title"), 'revenue' => empty($payment->amount) ? 0 : $payment->amount - $payment->tax - $payment->shipping, 'shipping' => empty($payment->amount) ? 0 : $payment->shipping, 'tax' => empty($payment->amount) ? 0 : $payment->tax));
            return $out . <<<CUT
<script type="text/javascript">
    ga('require', 'ecommerce');
    ga('ecommerce:addTransaction', {$tr});
    {$items}
    ga('ecommerce:send');
</script>
<!-- end of GA code -->
CUT;
        }
        return $out . <<<CUT
<script type="text/javascript">
if (typeof(_gaq)=='object') { // sometimes google-analytics can be blocked and we will avoid error
    _gaq.push(
        ['_addTrans', {$a}],
        {$items}
        ['_trackTrans']
    );
}
</script>
<!-- end of GA code -->
CUT;
    }
 public function calculate(Invoice $invoice, InvoiceItem $item, User $aff, $paymentNumber = 0, $tier = 0, $paymentAmount = 0.0, $paymentDate = 'now', &$matchedRules = array())
 {
     // take aff.commission_days in account for 1-tier only
     if ($tier == 0 && ($commissionDays = $this->getDi()->config->get('aff.commission_days'))) {
         $signupDays = $this->getDi()->time - strtotime($invoice->getUser()->aff_added ? $invoice->getUser()->aff_added : $invoice->getUser()->added);
         $signupDays = intval($signupDays / (3600 * 24));
         // to days
         if ($commissionDays < $signupDays) {
             return;
         }
         // no commission for this case, affiliate<->user relation is expired
     }
     $multi = 1.0;
     $isFirst = $paymentNumber <= 1;
     $prefix = $isFirst && (double) $item->first_total ? 'first' : 'second';
     if ($tier == 0) {
         if ($invoice->get("{$prefix}_total") == 0) {
             $paidForItem = 0;
         } else {
             $paidForItem = $paymentAmount * $item->get("{$prefix}_total") / $invoice->get("{$prefix}_total");
         }
     } else {
         // for higher tier just take amount paid to previous tier
         $paidForItem = $paymentAmount;
     }
     $paidForItem = $tier ? $paidForItem : $paidForItem / $invoice->base_currency_multi;
     foreach ($this->findRules($invoice, $item, $aff, $paymentNumber, $tier, $paymentDate) as $rule) {
         array_push($matchedRules, $rule);
         // Second tier commission have to be calculated as percent from First tier commission.
         if ($tier > 0) {
             return moneyRound($rule->first_payment_c * $paidForItem / 100);
         }
         if ($rule->type == AffCommissionRule::TYPE_MULTI) {
             $multi *= $rule->multi;
         } else {
             if ($paidForItem == 0) {
                 // free signup?
                 if ($paymentNumber == 0 && $rule->free_signup_c) {
                     return moneyRound($multi * $rule->free_signup_c);
                 }
             } elseif ($isFirst) {
                 // first payment
                 if ($rule->first_payment_t == '%') {
                     return moneyRound($multi * $rule->first_payment_c * $paidForItem / 100);
                 } else {
                     return moneyRound($multi * $rule->first_payment_c);
                 }
             } else {
                 // recurring payment
                 if ($rule->recurring_t == '%') {
                     return moneyRound($multi * $rule->recurring_c * $paidForItem / 100);
                 } else {
                     return moneyRound($multi * $rule->recurring_c);
                 }
             }
         }
     }
 }
 public function calculate(Invoice $invoice, InvoiceItem $item, User $aff, $paymentNumber = 0, $tier = 0, $paymentAmount = 0.0, $paymentDate = 'now')
 {
     // take aff.commission_days in account for 1-tier only
     if ($tier == 0 && ($commissionDays = $this->getDi()->config->get('aff.commission_days'))) {
         $signupDays = $this->getDi()->time - strtotime($invoice->getUser()->added);
         $signupDays = intval($signupDays / 3600 * 24);
         // to days
         if ($commissionDays < $signupDays) {
             return;
         }
         // no commission for this case, affiliate<->user relation is expired
         // however, the relation still works for 2-level commissions
     }
     $multi = 1.0;
     $prefix = $paymentNumber == 0 ? 'first' : 'second';
     if ($tier == 0) {
         if ($invoice->get("{$prefix}_total") == 0) {
             $paidForItem = 0;
         } else {
             $paidForItem = $paymentAmount * $item->get("{$prefix}_total") / $invoice->get("{$prefix}_total");
         }
     } else {
         // for higher tier just take amount paid to previous tier
         $paidForItem = $paymentAmount;
     }
     foreach ($this->findRules($invoice, $item, $aff, $paymentNumber, $tier, $paymentDate) as $rule) {
         if ($rule->type == AffCommissionRule::TYPE_MULTI) {
             $multi *= $rule->multi;
         } else {
             if ($paidForItem == 0) {
                 // free signup?
                 if ($paymentNumber == 0 && $rule->free_signup_c) {
                     return moneyRound($multi * $rule->free_signup_c);
                 }
             } elseif ($paymentNumber == 0) {
                 // first payment
                 if ($rule->first_payment_t == '%') {
                     return moneyRound($multi * $rule->first_payment_c * $paidForItem / 100);
                 } else {
                     return moneyRound($multi * $rule->first_payment_c);
                 }
             } else {
                 // first payment
                 if ($rule->recurring_t == '%') {
                     return moneyRound($multi * $rule->recurring_c * $paidForItem / 100);
                 } else {
                     return moneyRound($multi * $rule->recurring_c);
                 }
             }
         }
     }
 }
Exemple #19
0
 public function getAmount()
 {
     return moneyRound($this->request->get('total_invoice_amount'));
 }
 function getSecondSubtotal()
 {
     return moneyRound($this->second_price * $this->qty);
 }
Exemple #21
0
 public function export(AffPayout $payout, Am_Query $details, Zend_Controller_Response_Http $response)
 {
     $q = $details->query();
     while ($d = $payout->getDi()->db->fetchRow($q)) {
         $d = $payout->getDi()->affPayoutDetailTable->createRecord($d);
         /* @var $d AffPayoutDetail */
         $aff = $d->getAff();
         $rows[] = array($aff->data()->get('aff_bitcoin_wallet'), moneyRound($d->amount), Am_Currency::getDefault(), $aff->user_id, "Affiliate commission to " . amDate($payout->thresehold_date));
     }
     $this->sendCsv("bitcoint-commission-" . $payout->payout_id . ".txt", $rows, $response);
 }