function renderInvoiceCommissions(Invoice $invoice, Am_View $view) { $query = new Am_Query($this->getDi()->affCommissionTable); $query->leftJoin('?_invoice', 'i', 'i.invoice_id=t.invoice_id')->leftJoin('?_user', 'a', 't.aff_id=a.user_id')->leftJoin('?_product', 'p', 't.product_id=p.product_id')->addField('CONCAT(a.login, \' (\', a.name_f, \' \', a.name_l,\') #\', a.user_id)', 'aff_name')->addField('p.title', 'product_title')->addWhere('t.invoice_id=?', $invoice->pk())->leftJoin('?_aff_payout_detail', 'apd', 't.payout_detail_id=apd.payout_detail_id')->leftJoin('?_aff_payout', 'ap', 'ap.payout_id=apd.payout_id')->addField('ap.date', 'payout_date')->addField('ap.payout_id')->addField('apd.is_paid')->setOrder('commission_id', 'desc'); $items = $query->selectAllRecords(); $view->comm_items = $items; $view->invoice = $invoice; $view->has_tiers = $this->getDi()->affCommissionRuleTable->getMaxTier(); return $view->render('blocks/admin-user-invoice-details.phtml'); }
/** * Return a link to stop recurring subscription * @param Invoice $invoice * @return string|null */ public function getAdminCancelUrl(Invoice $invoice) { $m = new ReflectionMethod($this, 'cancelAction'); if ($m->getDeclaringClass()->getName() == __CLASS__) { return; } return REL_ROOT_URL . '/admin-user-payments/stop-recurring/user_id/' . $invoice->user_id . '?invoice_id=' . $invoice->pk(); }
function findInitialTransactionNumber(Invoice $invoice) { return $this->getDi()->db->selectCell("SELECT receipt_id \n FROM ?_invoice_payment\n WHERE invoice_id=?d \n ORDER BY dattm \n LIMIT 1", $invoice->pk()); }
function yesOto(Am_Controller $controller, Invoice $invoice, Oto $oto) { $inv = $this->getDi()->invoiceTable->createRecord(); /* @var $inv Invoice */ $inv->data()->set('oto_parent', $invoice->pk()); $inv->user_id = $invoice->user_id; $inv->add($oto->getProduct()); $coupon = $oto->getCoupon(); if ($coupon) { $inv->setCoupon($coupon); } $inv->calculate(); if ($inv->isZero()) { // free invoice $inv->paysys_id = 'free'; } elseif ($oto->getPaysysId()) { // configured $inv->paysys_id = $oto->getPaysysId(); } elseif ($invoice->paysys_id != 'free') { // not free? take from invoice $inv->paysys_id = $invoice->paysys_id; } else { // was free, now paid, take first public $paysystems = Am_Di::getInstance()->paysystemList->getAllPublic(); $inv->paysys_id = $paysystems[0]->paysys_id; } $inv->insert(); $payProcess = new Am_Paysystem_PayProcessMediator($controller, $inv); $result = $payProcess->process(); // we decided to ignore failures here... }
/** * Process invoice and insert necessary commissions for it * * External code should guarantee that this method with $payment = null will be called * only once for each user for First user invoice */ public function processPayment(Invoice $invoice, InvoicePayment $payment = null) { $aff_id = $invoice->aff_id; /* @var $coupon Coupon */ try { if (!$aff_id && ($coupon = $invoice->getCoupon())) { // try to find affiliate by coupon $aff_id = $coupon->aff_id ? $coupon->aff_id : $coupon->getBatch()->aff_id; } } catch (Am_Exception_Db_NotFound $e) { //coupon not found } if (empty($aff_id)) { $aff_id = $invoice->getUser()->aff_id; } if ($aff_id && empty($invoice->aff_id)) { // set aff_id to invoice for quick access next time $invoice->updateQuick('aff_id', $aff_id); } // run event to get plugins chance choose another affiliate $event = new Am_Event(Bootstrap_Aff::AFF_FIND_AFFILIATE, array('invoice' => $invoice, 'payment' => $payment)); $event->setReturn($aff_id); $this->getDi()->hook->call($event); $aff_id = $event->getReturn(); if (empty($aff_id)) { return; } // no affiliate id registered if ($aff_id == $invoice->getUser()->pk()) { return; } //strange situation // load affiliate and continue $aff = $this->getDi()->userTable->load($aff_id, false); if (!$aff || !$aff->is_affiliate) { return; } // affiliate not found $user = $invoice->getUser(); if (!$user->aff_id) { $user->aff_id = $aff->pk(); $user->aff_added = sqlTime('now'); $user->data()->set('aff-source', 'invoice-' . $invoice->pk()); $user->save(); } // try to load other tier affiliate $aff_tier = $aff; $aff_tiers = array(); $aff_tiers_exists = array($aff->pk()); for ($tier = 1; $tier <= $this->getMaxTier(); $tier++) { if (!$aff_tier->aff_id || $aff_tier->pk() == $invoice->getUser()->pk()) { break; } $aff_tier = $this->getDi()->userTable->load($aff_tier->aff_id, false); if (!$aff_tier || !$aff_tier->is_affiliate || $aff_tier->pk() == $invoice->getUser()->pk() || in_array($aff_tier->pk(), $aff_tiers_exists)) { //already in chain break; } $aff_tiers[$tier] = $aff_tier; $aff_tiers_exists[] = $aff_tier->pk(); } $isFirst = !$payment || $payment->isFirst(); //to define price field $paymentNumber = is_null($payment) ? 0 : $invoice->getPaymentsCount(); if (!$payment) { $tax = 0; } else { $tax = $this->getDi()->config->get('aff.commission_include_tax', false) ? 0 : doubleval($payment->tax); } $amount = $payment ? $payment->amount - $tax : 0; $date = $payment ? $payment->dattm : 'now'; // now calculate commissions $items = is_null($payment) ? array_slice($invoice->getItems(), 0, 1) : $invoice->getItems(); foreach ($items as $item) { //we do not calculate commission for free items in invoice $prefix = $isFirst ? 'first' : 'second'; if (!is_null($payment) && !(double) $item->get("{$prefix}_total")) { continue; } $comm = $this->getDi()->affCommissionRecord; $comm->date = sqlDate($date); $comm->record_type = AffCommission::COMMISSION; $comm->invoice_id = $invoice->invoice_id; $comm->invoice_item_id = $item->invoice_item_id; $comm->invoice_payment_id = $payment ? $payment->pk() : null; $comm->receipt_id = $payment ? $payment->receipt_id : null; $comm->product_id = $item->item_id; $comm->is_first = $paymentNumber <= 1; $comm->_setPayment($payment); $comm->_setInvoice($invoice); $comm_tier = clone $comm; $rules = array(); $topay_this = $topay = $this->calculate($invoice, $item, $aff, $paymentNumber, 0, $amount, $date, $rules); if ($topay > 0) { $comm->aff_id = $aff->pk(); $comm->amount = $topay; $comm->tier = 0; $comm->_setAff($aff); $comm->insert(); $comm->setCommissionRules(array_map(create_function('$el', 'return $el->pk();'), $rules)); } foreach ($aff_tiers as $tier => $aff_tier) { $rules = array(); $topay_this = $this->calculate($invoice, $item, $aff_tier, $paymentNumber, $tier, $topay_this, $date, $rules); if ($topay_this > 0) { $comm_this = clone $comm_tier; $comm_this->aff_id = $aff_tier->pk(); $comm_this->amount = $topay_this; $comm_this->tier = $tier; $comm_this->_setAff($aff_tier); $comm_this->insert(); $comm_this->setCommissionRules(array_map(create_function('$el', 'return $el->pk();'), $rules)); } } } }
function getAdminCancelUrl(Invoice $invoice) { return REL_ROOT_URL . '/admin-user-payments/stop-recurring/user_id/' . $invoice->user_id . '?invoice_id=' . $invoice->pk(); }
public function getInvoiceRecordTitle(Invoice $invoice = null) { return $invoice ? sprintf('%s (%s/%s, %s: %s)', ___('Invoice'), $invoice->pk(), $invoice->public_id, ___('Billing Terms'), new Am_TermsText($invoice)) : ___('Invoice'); }
public function cancelInvoice(Invoice $invoice) { if (!($rbAccId = $invoice->data()->get(self::RB_ACCOUNT_ID_KEY))) { throw new Am_Exception_InputError("Subscription can not be cancelled"); } $request = new Am_HttpRequest(self::URL_RB, Am_HttpRequest::METHOD_POST); $request->addPostParameter(array('serviceVersion' => '1.0', 'operationType' => 'C', 'merchantId' => $this->getConfig('merchant_id'), 'passCode' => $this->getConfig('passcode'), 'rbAccountId' => $rbAccId)); $response = $request->send()->getBody(); $xml = simplexml_load_string($response); if ((string) $xml->code != 1) { throw new Am_Exception_InternalError("Cancel subscription[{$invoice->pk()}/{$invoice->public_id}] ERROR: " . (string) $xml->message); } $invoice->data()->set(self::RB_ACCOUNT_ID_KEY, null)->update(); return true; }
/** * 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); }
public function cancelAction(Invoice $invoice, $actionName, Am_Paysystem_Result $result) { $request = $this->createHttpRequest(); $ps = new stdclass(); $ps->type = 'cncl'; $ps->reason = 'ticket.type.cancel.7'; $ps->comment = 'cancellation request from aMember user (' . $invoice->getLogin() . ')'; $get_params = http_build_query((array) $ps, '', '&'); $payment = current($invoice->getPaymentRecords()); $request->setUrl($s = 'https://api.clickbank.com/rest/1.3/tickets/' . Am_Di::getInstance()->invoicePaymentTable->getLastReceiptId($invoice->pk()) . "?{$get_params}"); $request->setHeader(array('Content-Length' => '0', 'Accept' => 'application/xml', 'Authorization' => $this->getConfig('dev_key') . ':' . $this->getConfig('clerk_key'))); $request->setMethod(Am_HttpRequest::METHOD_POST); $this->logRequest($request); $request->setMethod('POST'); $response = $request->send(); $this->logResponse($response); if ($response->getStatus() != 200 && $response->getBody() != 'Subscription already canceled') { throw new Am_Exception_InputError("An error occurred while cancellation request"); } }
public function getAdminCancelUrl(Invoice $invoice) { return REL_ROOT_URL . '/payment/twocheckout/admin-cancel?invoice_id=' . $invoice->pk(); }