/**
  *
  * @service affiliate_invoice read
  * @param $fields
  */
 public function previewInvoice(Gpf_Rpc_Params $params) {
     $form = new Gpf_Rpc_Form($params);
     
     $user = new Pap_Affiliates_User();
     
     $applyVat = $form->getFieldValue('applyVat');
     $payoutInvoice = $form->getFieldValue('payoutInvoice');
     
     
     $user->setId($form->getFieldValue('userid'));
     try {
         $user->load();
     } catch (Gpf_Exception $e) {
         $this->invoiceHtml = $this->_("You have to select user");
         return $this;
     }
     
     $currency = Pap_Common_Utils_CurrencyUtils::getDefaultCurrency();
     $payout = new Pap_Common_Payout($user, $currency, 1, 123456);
     $payout->setApplyVat($applyVat);
     $payout->generateInvoice($payoutInvoice);
     $this->invoiceHtml = $payout->getInvoice(); 
     return $this;
 }
    /**
     * @service pay_affiliate write
     * @return Gpf_Rpc_Form
     */
    public function payAffiliates(Gpf_Rpc_Params $params) {
    	$form = new Gpf_Rpc_Form($params);    	    	        

    	$this->saveNotificationSettings($form);       	       
        $currency = Pap_Common_Utils_CurrencyUtils::getDefaultCurrency();
        $payoutHistory = $this->createPayoutHistoryItem($form);
        
        foreach ($this->getAffiliatesToPaySelect()->getAllRowsIterator() as $row) {
            $user = new Pap_Common_User();
            $user->fillFromRecord($row);
            $payout = new Pap_Common_Payout($user, $currency, $row->get('amountRaw'), $this->generateInvoiceNumber());
            $payout->setPayoutHistory($payoutHistory);
            $payout->generateInvoice();
            try {
                $payout->save();
            } catch (Gpf_DbEngine_Row_ConstraintException $e) {
            	$form->setErrorMessage($this->_('Failed to pay affiliates %s', $e->getMessage()));            	
            	continue;
            }
            $this->markTransactionsAsPaid($payout);
            $this->sendEmails($payout);

            Gpf_Plugins_Engine::extensionPoint('PostAffiliate.Pap_Merchants_Payout_PayAffiliatesForm.payAffiliates', $user);
        }           
             
        $form->setInfoMessage($this->_('Affiliates successfully paid'));        
        
        return $form;
    }