Exemplo n.º 1
0
 /**
  * Change customer type to VIP if number of orders >= 5 (or value from config)
  * 
  * @param type $observer
  */
 public function salesOrderPlaceAfter($observer)
 {
     $order = $observer->getOrder();
     $customerId = $order->getCustomerId();
     $customer = Mage::getModel('customer/customer')->load($customerId);
     $numberOfOrders = $customer->getData('number_of_orders');
     if (!$numberOfOrders) {
         $numberOfOrders = 0;
     }
     $numberOfOrdersInConfig = Mage::getStoreConfig('inventoryplus/customer/minimum_orders_to_be_vip');
     if ($numberOfOrders >= $numberOfOrdersInConfig - 1) {
         $customer->setData('customer_satisfaction_type', 1);
     }
     $customer->setData('number_of_orders', $numberOfOrders + 1);
     try {
         $customer->save();
     } catch (Exception $ex) {
         Mage::log("salesOrderPlaceAfter error: " . $ex);
     }
 }
Exemplo n.º 2
0
 /**
  * Changed By Adam to solve the problem of partial refund 26/08/2014
  * @param type $creditmemo
  * @return \Magestore_Affiliateplus_Model_Transaction
  */
 public function reduce($creditmemo)
 {
     if ($this->getStatus() == 1) {
         if ($this->canRestore()) {
             return $this;
         }
         //        edit by viet
         if (!$this->getId() || !$creditmemo->getId()) {
             return $this;
         }
         $reducedIds = explode(',', $this->getCreditmemoIds());
         if (is_array($reducedIds) && in_array($creditmemo->getId(), $reducedIds)) {
             return $this;
         }
         $reducedIds[] = $creditmemo->getId();
         // calculate reduced commission
         $reduceCommission = 0;
         // Reduce commission for affiliate level 0
         $reduceTransactionCommission = 0;
         // Reduce commission for transaction (all affiliate + tier affiliate)
         $reduceTransactionDiscount = 0;
         foreach ($creditmemo->getAllItems() as $item) {
             if ($item->getOrderItem()->isDummy()) {
                 continue;
             }
             // Calculate the reduce commission for affiliate
             if (!$item->getAffiliateplusCommissionFlag()) {
                 $orderItem = $item->getOrderItem();
                 if ($orderItem->getAffiliateplusCommission()) {
                     // Calculate the reduce commission for affiliate
                     $affiliateplusCommissionItem = explode(",", $orderItem->getAffiliateplusCommissionItem());
                     $firstComs = $affiliateplusCommissionItem[0];
                     $reduceCommission += $firstComs * $item->getQty() / $orderItem->getQtyOrdered();
                     // Calculate the reduce commission for transaction
                     $orderItemQty = $orderItem->getQtyOrdered();
                     $orderItemCommission = (double) $orderItem->getAffiliateplusCommission();
                     if ($orderItemCommission && $orderItemQty) {
                         $reduceTransactionCommission += $orderItemCommission * $item->getQty() / $orderItemQty;
                     }
                     $reduceTransactionDiscount += $orderItem->getBaseAffiliateplusAmount() * $item->getQty() / $orderItemQty;
                     $item->setAffiliateplusCommissionFlag(1)->save();
                     Mage::dispatchEvent('update_tiercommission_to_tieraffiliate_partial_refund', array('transaction' => $this, 'creditmemo_item' => $item));
                 }
             }
         }
         if ($reduceCommission <= 0) {
             return $this;
         }
         // check reduced commission is over than total commission
         if ($reduceTransactionCommission > $this->getCommission()) {
             $reduceTransactionCommission = $this->getCommission();
         }
         $account = Mage::getModel('affiliateplus/account')->setStoreId($this->getStoreId())->load($this->getAccountId());
         try {
             //          $commission = $reduceCommission + $this->getCommissionPlus() * $reduceCommission / $this->getCommission() + $reduceCommission * $this->getPercentPlus() / 100;
             //
             $commission = $reduceCommission + $this->getCommissionPlus() * $reduceTransactionCommission / $this->getCommission() + $reduceTransactionCommission * $this->getPercentPlus() / 100;
             if ($commission) {
                 $account->setBalance($account->getData('balance') - $commission)->save();
                 //                $account->setBalance($account->getBalance() - $commission)
                 //                        ->save();
             }
             $creditMemoIds = implode(',', array_filter($reducedIds));
             $this->setCreditmemoIds($creditMemoIds);
             $this->setCommissionPlus($this->getCommissionPlus() - $this->getCommissionPlus() * $reduceTransactionCommission / $this->getCommission());
             $order = $creditmemo->getOrder();
             if ($reduceTransactionCommission) {
                 if ($this->getCommission() <= $reduceTransactionCommission && $order->getBaseSubtotal() == $order->getBaseSubtotalRefunded()) {
                     $this->setCommission(0)->setStatus(3);
                 } else {
                     $this->setCommission($this->getCommission() - $reduceTransactionCommission);
                 }
             }
             if ($reduceTransactionDiscount) {
                 if ($this->getDiscount() > $reduceTransactionDiscount) {
                     $this->setDiscount(0);
                 } else {
                     $this->setDiscount($this->getDiscount() + $reduceTransactionDiscount);
                 }
             }
             $this->save();
             if ($reduceTransactionCommission) {
                 // Update affiliateplusprogram transaction
                 Mage::dispatchEvent('update_affiliateplusprogram_transaction_partial_refund', array('transaction' => $this, 'creditmemo' => $creditmemo));
                 // update balance for tier transaction
                 $commissionObj = new Varien_Object(array('base_reduce' => $reduceTransactionCommission, 'total_reduce' => $commission));
                 Mage::dispatchEvent('affiliateplus_reduce_transaction', array('transaction' => $this, 'creditmemo' => $creditmemo, 'commission_obj' => $commissionObj));
                 $reduceCommission = $commissionObj->getBaseReduce();
                 // Tong commission se bi tru di o transaction
                 $commission = $commissionObj->getTotalReduce();
                 // Tong commission se bi tru di khoi tai khoan customer
                 // Send email for affiliate account
                 $this->sendMailReduceCommissionToAccount($reduceCommission, $commission);
             }
         } catch (Exception $e) {
             print_r($e->getMessage());
             die('zzz');
         }
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Konstruiert einen Schlüssel für die Sortierung der Personen Formulare.
  * 
  * Der Schlüssel hat folgende Struktur. 
  * 
  * SORTORDER_MODIFIED_OLDPOSITION
  * 
  * Zuerst kommt die gewünscht SortOrder, dann kommt ein Flag, ob die SortOrder modifiziert wurde, also nicht mehr
  * der aktuellen Position entspricht, und zum Schluss kommt die alte Position. 
  * 
  * Um beliebig viele Unterfomulare oder beliebig große SortOrder Werte zu unterstützen werden diese mit einer
  * festen Länge, gegebenfalls mit 0 aufgefüllt ausgegeben. Die Anzahl der Digits wird übergeben, damit die
  * Berechnung nicht für jeden Schlüssel erfolgen muss.
  * 
  * Wenn der SortOrder Wert leer ist wird er auf $maxSortOrder + 1 gesetzt, damit diese Unterformular nach ganz 
  * hinten kommen. Das muss bei der Berechnung der Digits berücksichtig werden (99 + 1 = 100).
  *
  * @param type $subform
  * @return type
  */
 public function getSortKey($subform, $maxSortOrder, $digitsSortOrder = 2, $digitsOrder = 2)
 {
     $sortOrder = $subform->getElement(Admin_Form_Document_Person::ELEMENT_SORT_ORDER)->getValue();
     $sortOrder = $sortOrder == null ? $maxSortOrder + 1 : $sortOrder;
     $order = $subform->getOrder() + 1;
     $modified = $sortOrder == $order ? 1 : 0;
     // NICHT MODIFIZIERT (1) : MODIFIZIERT (0)
     return sprintf('%1$0' . $digitsSortOrder . 'd_%2$d_%3$0' . $digitsOrder . 'd', $sortOrder, $modified, $order);
 }
Exemplo n.º 4
0
 /**
  * Builds request for DeliverInvoice as Credit for Invoice. Calls from
  * Magento Refund
  *
  * @param type $payment
  * @param type $auth
  * @param type $sveaOrderId
  * @return type
  */
 public function getRefundRequest($payment, $auth, $sveaOrderId)
 {
     $conf = new SveaMageConfigProvider($auth);
     $sveaObject = WebPay::deliverOrder($conf);
     $order = $payment->getOrder();
     $creditMemo = $payment->getCreditmemo();
     $storeId = $order->getStoreId();
     $countryCode = $order->getBillingAddress()->getCountryId();
     $taxCalculationModel = Mage::getSingleton('tax/calculation');
     $taxConfig = Mage::getSingleton('tax/config');
     $store = Mage::app()->getStore($storeId);
     foreach ($creditMemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
             continue;
         }
         // Default to the item price
         $name = $item->getName();
         $price = $orderItem->getPrice();
         $priceInclTax = $orderItem->getPriceInclTax();
         $taxPercent = $orderItem->getTaxPercent();
         if (!(int) $taxPercent) {
             $taxPercent = false;
         }
         $qty = $item->getQty();
         $parentItem = $orderItem->getParentItem();
         if ($parentItem) {
             switch ($parentItem->getProductType()) {
                 case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE:
                     $price = $parentItem->getPrice();
                     $priceInclTax = $parentItem->getPriceInclTax();
                     $taxPercent = $parentItem->getTaxPercent();
                     $qty = $parentItem->getQtyRefunded();
                     break;
                 case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE:
                     $taxPercent = $priceInclTax = $price = 0;
                     $name = '- ' . $name;
                     break;
             }
         }
         if ($taxPercent === false) {
             // If it's a bundle item we have to calculate the tax from
             // the including/excluding tax values
             $taxPercent = round(100 * ($priceInclTax / $price - 1));
         }
         $orderRow = WebPayItem::orderRow()->setArticleNumber($item->getSku())->setQuantity((int) $qty)->setName($name)->setUnit(Mage::helper('svea_webpay')->__('unit'))->setVatPercent((int) $taxPercent)->setAmountIncVat((double) $priceInclTax);
         $sveaObject->addOrderRow($orderRow);
     }
     $request = $taxCalculationModel->getRateRequest($order->getShippingAddress(), $order->getBillingAddress(), null, $store);
     // Shipping
     if ($creditMemo->getShippingAmount() > 0) {
         $shippingFee = WebPayItem::shippingFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName($order->getShippingDescription());
         // We require shipping tax to be set
         $shippingTaxClass = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $storeId);
         $rate = $taxCalculationModel->getRate($request->setProductClassId($shippingTaxClass));
         $shippingFee->setVatPercent((int) $rate);
         if ($taxConfig->shippingPriceIncludesTax($storeId)) {
             $shippingFee->setAmountIncVat($creditMemo->getShippingInclTax());
         } else {
             $shippingFee->setAmountExVat($creditMemo->getShippingAmount());
         }
         $sveaObject->addFee($shippingFee);
     }
     // Discount
     $discount = abs($creditMemo->getDiscountAmount());
     if ($discount > 0) {
         $discountRow = WebPayItem::fixedDiscount()->setAmountIncVat($discount)->setName(Mage::helper('svea_webpay')->__('discount'))->setUnit(Mage::helper('svea_webpay')->__('unit'));
         $sveaObject->addDiscount($discountRow);
     }
     // Gift cards
     if (abs($creditMemo->getGiftCardsAmount()) > 0) {
         $giftCardRow = WebPayItem::fixedDiscount()->setAmountIncVat(abs($creditMemo->getGiftCardsAmount()))->setUnit(Mage::helper('svea_webpay')->__('unit'));
         $sveaObject->addDiscount($giftCardRow);
     }
     // Invoice fee
     $paymentFee = $creditMemo->getSveaPaymentFeeAmount();
     $paymentFeeInclTax = $creditMemo->getSveaPaymentFeeInclTax();
     $refunded = $creditMemo->getOrder()->getSveaPaymentFeeRefunded();
     if ($paymentFee > 0 && $refunded == 0) {
         $invoiceFee = WebPayItem::invoiceFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName(Mage::helper('svea_webpay')->__('invoice_fee'))->setAmountExVat($paymentFee)->setAmountIncVat($paymentFeeInclTax);
         $sveaObject = $sveaObject->addFee($invoiceFee);
         $creditMemo->getOrder()->setSveaPaymentFeeRefunded($paymentFeeInclTax);
     }
     $adjustmentFee = $creditMemo->getAdjustmentPositive();
     if ($adjustmentFee > 0) {
         $invoiceAdjustment = WebPayItem::invoiceFee()->setVatPercent(0)->setAmountIncVat($adjustmentFee);
         $sveaObject->addFee($invoiceAdjustment);
     }
     $response = $sveaObject->setCountryCode($countryCode)->setOrderId($sveaOrderId)->setInvoiceDistributionType(Mage::getStoreConfig("payment/svea_invoice/deliver_method"));
     $order->setData('svea_refund_request', $response);
     return $order->getData('svea_refund_request');
 }
 /**
  * Sets volume_license flag in sales flat order item table when order is placed, if the oder item has volume_license as "Yes"
  * @param type $event
  */
 public function isVolumeLicense($event)
 {
     $order = $event->getOrder();
     $items = $order->getAllItems();
     foreach ($items as $number => $item) {
         $obj = Mage::getModel('catalog/product');
         $_product = $obj->load($item->getProductId());
         // Mage::log($_product->debug(),null,'hi.log');
         $volume_license = $_product->getResource()->getAttribute('volume_license')->getFrontend()->getValue($_product);
         if ($item->getProductType() == 'downloadable' && $item->getQtyOrdered() > 1 && $volume_license == 'Yes') {
             $orderItem = Mage::getModel('sales/order_item')->load($item->getItemId());
             $orderItem->setVolumeLicense(1);
             $orderItem->save();
         }
     }
     //foreach
 }
Exemplo n.º 6
0
 /**
  * Changed By Adam to solve the problem of invoice tung phan 22/08/2014
  * @param type $observer
  * @return type
  */
 public function update_tiercommission_to_transaction_partial_invoice($observer)
 {
     /* hainh edit 25-04-2014 */
     if (!Mage::helper('affiliatepluslevel')->isPluginEnabled()) {
         return;
     }
     $transaction = $observer->getTransaction();
     $order = $observer->getOrder();
     $tierTransactions = $this->_getTierTransactions($transaction);
     try {
         foreach ($tierTransactions as $tierTransaction) {
             $transactionTotal = 0;
             foreach ($order->getAllItems() as $item) {
                 if ($item->getAffiliateplusCommission()) {
                     $affiliateplusCommissionItem = explode(",", $item->getAffiliateplusCommissionItem());
                     $totalCommissionItemLevel = $affiliateplusCommissionItem[$tierTransaction->getLevel()];
                     $transactionTotal += $totalCommissionItemLevel * $item->getQtyInvoiced() / $item->getQtyOrdered();
                 }
             }
             if ($transactionTotal) {
                 $tierTransaction->setCommission($transactionTotal)->save();
             }
             //send mail Complete to tier
             if ($tierTransaction->getLevel() > 0) {
                 $tierTransaction->sendMailUpdatedTransactionToAccount($transaction, true);
             }
         }
     } catch (Exception $e) {
         print_r($e->getMessage());
         die('z');
     }
 }
 /**
  * Sets premium_access flag in sales flat order item table when order is placed, if the oder item has premium_access as "Yes"
  * @param type $event
  */
 public function isPremiumAccess($event)
 {
     $helper = Mage::helper('icc_premiumaccess');
     $premiumOptions = $helper->getPremiumAccessType();
     $order = $event->getOrder();
     $items = $order->getAllItems();
     $allbundle = array();
     foreach ($items as $number => $item) {
         $obj = Mage::getModel('catalog/product');
         $_product = $obj->load($item->getProductId());
         $expiration = date('m/d/y', $this->getExpriationFromDurationValue($_product->getAttributeText("subscription_duration")));
         if (in_array($_product->getData('item_type'), $premiumOptions)) {
             // echo $item->getProductType().'===';
             if ($item->getProductType() == 'bundle') {
                 $allbundle[$item->getId()] = $item->getId();
             }
             if (!in_array($item->getParentItemId(), $allbundle)) {
                 if (!$order->getPremiumAccess()) {
                     $order->setPremiumAccess(1);
                     $order->setExpirydate($expiration);
                     $order->save();
                     //echo "<pre>"; print_r($order->getData()); exit;
                 }
                 $orderItem = Mage::getModel('sales/order_item')->load($item->getItemId());
                 $orderItem->setPremiumAccess(1);
                 $orderItem->setExpirydate($expiration);
                 $orderItem->save();
             }
         }
     }
 }
Exemplo n.º 8
0
 /**
  * Refund order when using balance as store credit
  * @param type $creditmemo
  * @return type
  */
 public function creditmemoRefund($creditmemo)
 {
     // Changed By Adam 28/07/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return;
     }
     // $creditmemo = $observer->getCreditmemo();
     $order = $creditmemo->getOrder();
     $paymentMethod = Mage::getModel('affiliateplus/payment_credit')->load($order->getId(), 'order_id');
     if ($paymentMethod->getId() && $paymentMethod->getBasePaidAmount() - $paymentMethod->getBaseRefundAmount() > 0) {
         $payment = Mage::getModel('affiliateplus/payment')->load($paymentMethod->getPaymentId())->setData('payment', $paymentMethod);
         $account = $payment->getAffiliateplusAccount();
         if ($account && $account->getId() && $payment->getId()) {
             try {
                 $refundAmount = -$creditmemo->getBaseAffiliateCredit();
                 $account->setBalance($account->getBalance() + $refundAmount)->setTotalPaid($account->getTotalPaid() - $refundAmount)->setTotalCommissionReceived($account->getTotalCommissionReceived() - $refundAmount)->save();
                 $paymentMethod->setBaseRefundAmount($paymentMethod->getBaseRefundAmount() + $refundAmount)->setRefundAmount($paymentMethod->getRefundAmount() - $creditmemo->getAffiliateCredit())->save();
                 if (abs($paymentMethod->getBasePaidAmount() - $paymentMethod->getBaseRefundAmount()) < 0.0001) {
                     $payment->setStatus(4)->save();
                 }
             } catch (Exception $e) {
             }
         }
     }
 }