Пример #1
0
 public static function addTransaction($invoiceId, $customer, $amount, $currency_code, $description = '')
 {
     if (is_numeric($customer)) {
         /// customer ID is passed. Need to get customer object
         $customer = CustomerDAO::getInstance()->getCustomer($customer);
     }
     /// Now need to convert transaction amount to customer base currency
     $currency = Transaction::$instance->getRegistry()->get('currency');
     $amountInCustomerCurrency = (double) $currency->convert($amount, $currency_code, $customer['base_currency_code']);
     $newCustomerBalance = $customer['balance'] - $amountInCustomerCurrency;
     Transaction::$instance->getDb()->query("\r\n            INSERT INTO customer_transaction\r\n            SET\r\n                customer_id = " . (int) $customer['customer_id'] . ",\r\n                invoice_id = " . (int) $invoiceId . ",\r\n                description = '" . Transaction::$instance->getDb()->escape($description) . "',\r\n                amount = {$amountInCustomerCurrency},\r\n                currency_code = '" . $customer['base_currency_code'] . "',\r\n                date_added = NOW(),\r\n                balance = {$newCustomerBalance},\r\n                balance_currency = '" . Transaction::$instance->getDb()->escape($customer['base_currency_code']) . "'\r\n        ");
     //$transactionId = Transaction::$instance->getDb()->getLastId();
     /// Update customer's balance
     Transaction::$instance->getDb()->query("\r\n                UPDATE customer\r\n                SET\r\n                    balance = {$newCustomerBalance}\r\n                WHERE customer_id = " . $customer['customer_id']);
     Transaction::$instance->getCache()->delete('customer.' . $customer['customer_id']);
     if (Transaction::$instance->user->isLogged()) {
         Audit::getInstance(Transaction::$instance->getRegistry())->addAdminEntry(Transaction::$instance->user->getId(), AUDIT_ADMIN_TRANSACTION_ADD, $_REQUEST);
     } elseif (Transaction::$instance->customer->isLogged()) {
         Audit::getInstance(Transaction::$instance->getRegistry())->addUserEntry(Transaction::$instance->customer->getId(), AUDIT_ADMIN_TRANSACTION_ADD, $_REQUEST);
     }
 }
 /**
  * Applies specified update to system
  *
  * @param int $pn_version Version number of update to apply
  * @param array $pa_options Array of options. Supported options are:
  *		cleanCache = Remove all application caches after database update if true. Default is false.
  * @return bool
  */
 static function performDatabaseUpdate($pn_version, $pa_options = null)
 {
     if (($pn_version = (int) $pn_version) <= 0) {
         return null;
     }
     $va_messages = array();
     $t = new Transaction();
     $o_db = $t->getDb();
     if (!file_exists(__CA_BASE_DIR__ . "/support/sql/migrations/{$pn_version}.sql")) {
         return null;
     }
     $o_handle = fopen(__CA_BASE_DIR__ . "/support/sql/migrations/{$pn_version}.sql", "r");
     $vn_query_total_nb = 0;
     $vs_query = '';
     while (!feof($o_handle)) {
         $vs_line = fgets($o_handle, 8128);
         // $vs_query gets a concat of lines while a ; is not met
         $vs_query .= $vs_line;
         // Reading 1 character before the EOL
         $vs_before_eol = substr(rtrim($vs_line), -1);
         if ($vs_before_eol == ';') {
             //If the line ends with a ; then we will take all what's before and execute it
             //Counts the number of requests contained in the SQL file
             $vn_query_total_nb++;
             //Launching the query
             $o_db->query($vs_query);
             if ($o_db->numErrors() > 0) {
                 // temp array to catch the errors
                 $va_error_array_for_printing = $o_db->getErrors();
                 $va_messages['error_' . $pn_version] = _t("Error applying database migration %1: %2; error was at query %3; query was %4", $pn_version, join('; ', $va_error_array_for_printing), $vn_query_total_nb, $vs_query);
                 $o_db->clearErrors();
                 $t->rollback();
                 return $va_messages;
             }
             // Reset variable
             $vs_query = '';
         }
         $t->commit();
     }
     if (isset($pa_options['cleanCache']) && $pa_options['cleanCache']) {
         // Clean cache
         caRemoveDirectory(__CA_APP_DIR__ . '/tmp', false);
     }
     return $va_messages;
 }
 public function update($pa_options = null)
 {
     $o_trans = new Transaction();
     $o_db = $o_trans->getDb();
     $this->setTransaction($o_trans);
     list($vs_target_table, $vs_target_key) = $this->_getTarget();
     $vs_rel_table = $this->tableName();
     $t_target = $this->getAppDatamodel()->getInstanceByTableName($vs_target_table);
     $vn_target_id = $this->get($vs_target_key);
     if (!$t_target->load($vn_target_id)) {
         // invalid target
         $this->postError(720, _t("Related %1 does not exist", $t_target->getProperty('NAME_SINGULAR')), "BaseRepresentationRelationship->update()");
         return false;
     }
     if ($this->changed('is_primary')) {
         if (!$this->get('is_primary')) {
             // force is_primary to be set if no other represention is so marked
             // is there another rep for this object marked is_primary?
             $qr_res = $o_db->query("\n\t\t\t\t\t\tSELECT oxor.relation_id\n\t\t\t\t\t\tFROM {$vs_rel_table} oxor\n\t\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = oxor.representation_id\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\toxor.{$vs_target_key} = ? AND oxor.is_primary = 1 AND o_r.deleted = 0 AND oxor.relation_id <> ?\n\t\t\t\t\t", (int) $vn_target_id, (int) $this->getPrimaryKey());
             if (!$qr_res->nextRow()) {
                 // nope - force one to be primary
                 //$this->set('is_primary', 1);
                 $qr_res = $o_db->query("\n\t\t\t\t\t\t\tSELECT oxor.relation_id\n\t\t\t\t\t\t\tFROM {$vs_rel_table} oxor\n\t\t\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = oxor.representation_id\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\toxor.{$vs_target_key} = ? AND oxor.is_primary = 0 AND o_r.deleted = 0 AND oxor.relation_id <> ?\n\t\t\t\t\t\t\tORDER BY oxor.rank, oxor.relation_id\n\t\t\t\t\t\t", (int) $vn_target_id, (int) $this->getPrimaryKey());
                 if ($qr_res->nextRow()) {
                     $o_db->query("\n\t\t\t\t\t\t\t\tUPDATE {$vs_rel_table}\n\t\t\t\t\t\t\t\tSET is_primary = 1\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\trelation_id = ?\n\t\t\t\t\t\t\t", (int) $qr_res->get('relation_id'));
                     if (!($vb_rc = parent::update($pa_options))) {
                         $o_trans->rollbackTransaction();
                     } else {
                         $o_trans->commitTransaction();
                     }
                 }
             }
             return parent::update($pa_options);
         } else {
             // unset other reps is_primary field
             $o_db->query("\n\t\t\t\t\t\tUPDATE {$vs_rel_table}\n\t\t\t\t\t\tSET is_primary = 0\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t{$vs_target_key} = ?\n\t\t\t\t\t", (int) $vn_target_id);
             if (!($vb_rc = parent::update($pa_options))) {
                 $o_trans->rollbackTransaction();
             } else {
                 $o_trans->commitTransaction();
             }
             return $vb_rc;
         }
     } else {
         $vb_rc = parent::update($pa_options);
         $o_trans->commitTransaction();
         return $vb_rc;
     }
 }