Ejemplo n.º 1
0
 /**
  *
  * @param sfWebRequest $request
  * @param ImputationAccountTransactionForm $form
  */
 private function processAccountTransactionForm(sfWebRequest $request, ImputationAccountTransactionForm $form)
 {
     $parameters = $request->getParameter($form->getName());
     if ($parameters['imputation']['account_id'] == 0) {
         $parameters['imputation']['account_id'] = null;
         $this->error_account = true;
     }
     if ($parameters['quantity'] != '') {
         $parameters['sum'] = -$parameters['quantity'];
         $parameters['imputation']['total'] = 0;
         $parameters['imputation']['method_of_payment_id'] = ImputationDefaultValues::getFreeMethodId();
         unset($parameters['unitary_price']);
         unset($parameters['quantity']);
         $this->error_account = false;
     }
     $form->bind($parameters);
     if ($form->isValid()) {
         $transaction = $form->save();
         $account = Doctrine::getTable('Account')->find($parameters['imputation']['account_id']);
         $account->setValue($account->getValue() + $parameters['sum']);
         $account->save();
         if ($parameters['imputation']['method_of_payment_id'] == ImputationDefaultValues::getAccountMethodId()) {
             //If the method of payment is 'account':
             //Find the target account (the account used to pay):
             $target_account = Doctrine::getTable('Account')->find($parameters['account_to_pay_id']);
             $parameters_to_pay = $parameters;
             $parameters_to_pay['sum'] = $parameters['imputation']['total'];
             $parameters_to_pay['imputation']['total'] = $parameters_to_pay['sum'];
             $parameters_to_pay['imputation']['account_id'] = $parameters['account_to_pay_id'];
             $parameters_to_pay['imputation']['unity_id'] = $target_account->getAct()->getUnityId();
             //print_r($parameters_to_pay);exit(-1);
             //Duplicate the form:
             $transaction = new ImputationAccountTransaction();
             $transaction->getImputation()->setUserId($parameters['imputation']['user_id']);
             $form_to_pay = new ImputationAccountTransactionForm($transaction, array('culture' => ParametersConfiguration::getDefault('default_language')));
             //Bind it with the duplicate parameters:
             $form_to_pay->bind($parameters_to_pay);
             if ($form_to_pay->isValid()) {
                 //If the form is valid, change the target account value:
                 $target_account->setValue($target_account->getValue() - $parameters_to_pay['sum']);
                 $target_account->save();
                 //Eventually if the form is valid, save the duplicated form:
                 $form_to_pay->save();
                 $this->error_account = false;
             }
         } else {
             $this->error_account = true;
         }
         $this->redirect('use/index');
     }
 }
 /**
  * 
  * @param $parameters
  */
 public static function buildAndSaveTransactionFrom($parameters)
 {
     /* --- Duplicate the parameters: --- */
     $parameters_account_transaction = $parameters;
     /* --- Change the parameters: --- */
     /* --- Unset those who do not correspond to an account transaction: --- */
     $parameters_account_transaction = ImputationAccountTransaction::unsetFieldsByType($parameters_account_transaction, $parameters_account_transaction['imputation']['imputation_type']);
     /* --- Add the missing ones: --- */
     $parameters_account_transaction['sum'] = $parameters_account_transaction['imputation']['total'];
     /* --- Modify those who need to be modified --- */
     $parameters_account_transaction['imputation']['imputation_type'] = ImputationDefaultValues::ACCOUNT_TRANSACTION_TYPE;
     $parameters_account_transaction['imputation']['total'] = -$parameters_account_transaction['imputation']['total'];
     /* --- Create a form of type 'account transaction': --- */
     $transaction = new ImputationAccountTransaction();
     $transaction->getImputation()->setUserId($parameters_account_transaction['imputation']['user_id']);
     $form_account_transaction = new ImputationAccountTransactionForm($transaction, array('culture' => ParametersConfiguration::getDefault('default_language')));
     //Bind the form to the new parameters:
     $form_account_transaction->bind($parameters_account_transaction);
     //Save it if it's valid:
     if ($form_account_transaction->isValid() && $parameters['imputation']['account_id'] != 0) {
         /* --- Decrement the corresponding account: --- */
         $account = Doctrine::getTable('Account')->find($parameters['imputation']['account_id']);
         $account->setValue($account->getValue() - $parameters['imputation']['total']);
         $account->save();
         $form_account_transaction->save();
         return true;
     } else {
         return false;
     }
 }