function process(Am_Form $f) { $vars = $f->getValue(); $user = Am_Di::getInstance()->userTable->findFirstByLogin($vars['user']); if (!$user) { list($el) = $f->getElementsByName('user'); $el->setError(___('User %s not found', $vars['user'])); return false; } $aff = Am_Di::getInstance()->userTable->findFirstByLogin($vars['aff']); if (!$aff) { list($el) = $f->getElementsByName('aff'); $el->setError(___('Affiliate %s not found', $vars['user'])); return false; } $couponAff = null; if ($vars['coupon']) { $coupon = Am_DI::getInstance()->couponTable->findFirstByCode($vars['coupon']); if ($coupon && ($coupon->aff_id || $coupon->getBatch()->aff_id)) { $couponAff = Am_Di::getInstance()->userTable->load($coupon->aff_id ? $coupon->aff_id : $coupon->getBatch()->aff_id, false); } } /* @var $invoice Invoice */ $invoice = Am_Di::getInstance()->invoiceTable->createRecord(); $invoice->setUser($user); if ($vars['coupon']) { $invoice->setCouponCode($vars['coupon']); $error = $invoice->validateCoupon(); if ($error) { throw new Am_Exception_InputError($error); } } $user->aff_id = $aff->pk(); foreach ($vars['product_id'] as $plan_id => $qty) { $p = Am_Di::getInstance()->billingPlanTable->load($plan_id); $pr = $p->getProduct(); $invoice->add($pr, $qty); } $invoice->calculate(); $invoice->setPaysystem($vars['paysys_id'], false); $invoice->invoice_id = '00000'; $invoice->public_id = 'TEST'; $invoice->tm_added = sqlTime('now'); echo "<pre>"; echo $invoice->render(); echo "\nBilling Terms: " . $invoice->getTerms() . "\n" . str_repeat("-", 70) . "\n"; $helper = new Am_View_Helper_UserUrl(); $helper->setView(new Am_View()); printf("User Ordering the subscription: <a target='_blank' class='link' href='%s'>%d/%s "%s" <%s></a>\n", $helper->userUrl($user->pk()), $user->pk(), Am_Controller::escape($user->login), Am_Controller::escape($user->name_f . ' ' . $user->name_l), Am_Controller::escape($user->email)); printf("Reffered Affiliate: <a target='_blank' class='link' href='%s'>%d/%s "%s" <%s></a>\n", $helper->userUrl($aff->pk()), $aff->pk(), Am_Controller::escape($aff->login), Am_Controller::escape($aff->name_f . ' ' . $aff->name_l), Am_Controller::escape($aff->email)); if ($couponAff) { printf("Affiliate Detected by Coupon (will get commision): <a target='_blank' class='link' href='%s'>%d/%s "%s" <%s></a>\n", $helper->userUrl($couponAff->pk()), $couponAff->pk(), Am_Controller::escape($couponAff->login), Am_Controller::escape($couponAff->name_f . ' ' . $couponAff->name_l), Am_Controller::escape($couponAff->email)); } $max_tier = Am_Di::getInstance()->affCommissionRuleTable->getMaxTier(); //COMMISSION FOR FREE SIGNUP if (!(double) $invoice->first_total && !(double) $invoice->second_total && $vars['is_first']) { echo "\n<strong>FREE SIGNUP</strong>:\n"; list($item, ) = $invoice->getItems(); echo sprintf("* ITEM: %s\n", Am_Controller::escape($item->item_title)); foreach (Am_Di::getInstance()->affCommissionRuleTable->findRules($invoice, $item, $aff, 0, 0) as $rule) { echo $rule->render('* '); } $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 0, 0); echo "* AFFILIATE WILL GET FOR THIS ITEM: " . Am_Currency::render($to_pay) . "\n"; for ($i = 1; $i <= $max_tier; $i++) { $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 0, $i, $to_pay); $tier = $i + 1; echo "* {$tier}-TIER AFFILIATE WILL GET FOR THIS ITEM: " . Am_Currency::render($to_pay) . "\n"; } echo str_repeat("-", 70) . "\n"; } //COMMISSION FOR FIRST PAYMENT $price_field = (double) $invoice->first_total ? 'first_total' : 'second_total'; if ((double) $invoice->{$price_field}) { echo "\n<strong>FIRST PAYMENT ({$invoice->currency} {$invoice->{$price_field}})</strong>:\n"; $payment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $payment->invoice_id = @$invoice->invoice_id; $payment->dattm = sqlTime('now'); $payment->amount = $invoice->{$price_field}; echo str_repeat("-", 70) . "\n"; foreach ($invoice->getItems() as $item) { if (!(double) $item->{$price_field}) { continue; } //do not calculate commission for free items within invoice echo sprintf("* ITEM: %s ({$invoice->currency} {$item->{$price_field}})\n", Am_Controller::escape($item->item_title)); foreach (Am_Di::getInstance()->affCommissionRuleTable->findRules($invoice, $item, $aff, 1, 0, $payment->dattm) as $rule) { echo $rule->render('* '); } $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 1, 0, $payment->amount, $payment->dattm); echo "* AFFILIATE WILL GET FOR THIS ITEM: <strong>" . Am_Currency::render($to_pay) . "</strong>\n"; for ($i = 1; $i <= $max_tier; $i++) { $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 1, $i, $to_pay, $payment->dattm); $tier = $i + 1; echo "* {$tier}-TIER AFFILIATE WILL GET FOR THIS ITEM: <strong>" . Am_Currency::render($to_pay) . "</strong>\n"; } echo str_repeat("-", 70) . "\n"; } } //COMMISSION FOR SECOND AND SUBSEQUENT PAYMENTS if ((double) $invoice->second_total) { echo "\n<strong>SECOND AND SUBSEQUENT PAYMENTS ({$invoice->second_total} {$invoice->currency})</strong>:\n"; $payment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $payment->invoice_id = @$invoice->invoice_id; $payment->dattm = sqlTime('now'); $payment->amount = $invoice->second_total; echo str_repeat("-", 70) . "\n"; foreach ($invoice->getItems() as $item) { if (!(double) $item->second_total) { continue; } //do not calculate commission for free items within invoice echo sprintf("* ITEM: %s ({$item->second_total} {$invoice->currency})\n", Am_Controller::escape($item->item_title)); foreach (Am_Di::getInstance()->affCommissionRuleTable->findRules($invoice, $item, $aff, 2, 0, $payment->dattm) as $rule) { echo $rule->render('* '); } $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 2, 0, $payment->amount, $payment->dattm); echo "* AFFILIATE WILL GET FOR THIS ITEM: <strong>" . Am_Currency::render($to_pay) . "</strong>\n"; for ($i = 1; $i <= $max_tier; $i++) { $to_pay = Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 2, $i, $to_pay, $payment->dattm); $tier = $i + 1; echo "* {$tier}-TIER AFFILIATE WILL GET FOR THIS ITEM: <strong>" . Am_Currency::render($to_pay) . "</strong>\n"; } echo str_repeat("-", 70) . "\n"; } } echo "</pre>"; return true; }
function process(array $vars) { $vars['user'] = filterId($vars['user']); $vars['aff'] = filterId($vars['aff']); $user = Am_Di::getInstance()->userTable->findFirstByLogin($vars['user']); if (!$user) { throw new Am_Exception_InputError("User {$vars['user']} not found"); } $aff = Am_Di::getInstance()->userTable->findFirstByLogin($vars['aff']); if (!$aff) { throw new Am_Exception_InputError("Affiliate {$vars['aff']} not found"); } $invoice = Am_Di::getInstance()->invoiceTable->createRecord(); $invoice->setUser($user); $user->aff_id = $aff->pk(); foreach (Am_Di::getInstance()->productTable->loadIds($vars['product_ids']) as $pr) { $invoice->add($pr); } $invoice->paysys_id = 'manual'; $invoice->calculate(); $firstPayment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $firstPayment->amount = $invoice->first_total; $firstPayment->currency = $invoice->currency; $firstPayment->dattm = sqlTime('now'); $firstPayment->discount = $invoice->first_discount; $firstPayment->paysys_id = $invoice->paysys_id; $firstPayment->shipping = $invoice->first_shipping; $firstPayment->tax = $invoice->first_tax; $firstPayment->_setInvoice($invoice); $secondPayment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $secondPayment->amount = $invoice->second_total; $secondPayment->currency = $invoice->currency; $secondPayment->dattm = sqlTime('tomorrow'); $secondPayment->discount = $invoice->second_discount; $secondPayment->paysys_id = $invoice->paysys_id; $secondPayment->shipping = $invoice->second_shipping; $secondPayment->tax = $invoice->second_tax; $secondPayment->_setInvoice($invoice); // Am_Di::getInstance()->affCommissionRuleTable->getRules($firstPayment); // Am_Di::getInstance()->affCommissionRuleTable->getRules($secondPayment); $invoice->invoice_id = '00000'; $invoice->public_id = 'TEST'; $invoice->tm_added = sqlTime('now'); echo "<pre>"; echo $invoice->render(); echo "\nBilling Terms: " . $invoice->getTerms() . "\n" . str_repeat("-", 70) . "\n"; $helper = new Am_View_Helper_UserUrl(); $helper->setView(new Am_View()); printf("User Ordering the subscription: <a target='_blank' href='%s'>%d/%s "%s" <%s></a>\n", $helper->userUrl($user->pk()), $user->pk(), Am_Controller::escape($user->login), Am_Controller::escape($user->name_f . ' ' . $user->name_l), Am_Controller::escape($user->email)); printf("Reffered Affiliate: <a target='_blank' href='%s'>%d/%s "%s" <%s></a>\n", $helper->userUrl($aff->pk()), $aff->pk(), Am_Controller::escape($aff->login), Am_Controller::escape($aff->name_f . ' ' . $aff->name_l), Am_Controller::escape($aff->email)); echo "\nFIRST PAYMENT ({$invoice->currency} {$invoice->first_total}):\n"; $payment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $payment->invoice_id = @$invoice->invoice_id; $payment->dattm = sqlTime('now'); $payment->amount = $invoice->first_total; echo str_repeat("-", 70) . "\n"; foreach ($invoice->getItems() as $item) { echo "* ITEM: {$item->item_title} ({$invoice->currency} {$item->first_total})\n"; foreach (Am_Di::getInstance()->affCommissionRuleTable->findRules($invoice, $item, $aff, 0, 0, $payment->dattm) as $rule) { echo $rule->render('* '); } echo "* AFFILIATE WILL GET FOR THIS ITEM: " . Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 0, 0, $payment->amount, $payment->dattm) . " {$invoice->currency} \n"; echo "* " . str_repeat("-", 70) . "\n"; } if ($invoice->second_total) { echo "\nSECOND AND THE FOLLOWING PAYMENTS ({$invoice->second_total} {$invoice->currency}):\n"; $payment = Am_Di::getInstance()->invoicePaymentTable->createRecord(); $payment->invoice_id = @$invoice->invoice_id; $payment->dattm = sqlTime('now'); $payment->amount = $invoice->second_total; echo str_repeat("-", 70) . "\n"; foreach ($invoice->getItems() as $item) { if (!$item->second_total) { continue; } echo "* ITEM: {$item->item_title} ({$item->second_total} {$invoice->currency})\n"; foreach (Am_Di::getInstance()->affCommissionRuleTable->findRules($invoice, $item, $aff, 1, 0, $payment->dattm) as $rule) { echo $rule->render('* '); } echo "* AFFILIATE WILL GET FOR THIS ITEM: " . Am_Di::getInstance()->affCommissionRuleTable->calculate($invoice, $item, $aff, 1, 0, $payment->amount, $payment->dattm) . " {$invoice->currency} \n"; echo "* " . str_repeat("-", 70) . "\n"; } } echo "</pre>"; return true; }