Esempio n. 1
0
 /** 
  * Build and run the query to select all contributions
  * matching the criteria, and try to create a snapshot
  *
  * @return snapshot creation result/error
  */
 public static function createSnapshot($values)
 {
     // prepare timestamps
     $raw_from_ts = $values['donrec_contribution_horizon_from'];
     $raw_to_ts = $values['donrec_contribution_horizon_to'];
     $date_from = CRM_Utils_DonrecHelper::convertDate($raw_from_ts, -1);
     $date_to = CRM_Utils_DonrecHelper::convertDate($raw_to_ts, 1);
     $formatted_date_from = date('Y-m-d H:i:s', $date_from);
     $formatted_date_to = date('Y-m-d H:i:s', $date_to);
     $query_date_limit = "";
     if ($date_from) {
         $query_date_limit .= "AND `receive_date` >= '{$formatted_date_from}'";
     }
     if ($date_to) {
         $query_date_limit .= " AND `receive_date` <= '{$formatted_date_to}'";
     }
     // get table- and column name
     $table_query = "SELECT `cg`.`table_name`,\n                           `cf`.`column_name`\n                    FROM `civicrm_custom_group` AS cg,\n                         `civicrm_custom_field` AS cf\n                    WHERE `cg`.`name` = 'zwb_donation_receipt_item'\n                      AND `cf`.`custom_group_id` = `cg`.`id`\n                      AND `cf`.`name` = 'status'";
     $results = CRM_Core_DAO::executeQuery($table_query);
     $custom_group_table = NULL;
     $status_column = NULL;
     while ($results->fetch()) {
         $custom_group_table = $results->table_name;
         $status_column = $results->column_name;
     }
     if ($custom_group_table == NULL || $status_column == NULL) {
         // something went wrong here
         CRM_Core_Error::debug_log_message("de.systopia.donrec: error: custom_group_table or status_column is empty!");
         return array();
     }
     // calculate main selector clause
     if (!empty($values['contact_id'])) {
         $contact_id = (int) $values['contact_id'];
         $main_selector = "`contact_id` = {$contact_id}";
     } elseif (!empty($values['contact_ids'])) {
         $contact_ids = implode(',', $values['contact_ids']);
         $main_selector = "`contact_id` IN ({$contact_ids})";
     } elseif (!empty($values['contribution_ids'])) {
         $contribution_ids = implode(',', $values['contribution_ids']);
         $main_selector = "`civicrm_contribution`.`id` IN ({$contribution_ids})";
     } else {
         CRM_Core_Error::debug_log_message("de.systopia.donrec: error: no selector data found in params!");
         $main_selector = "FALSE";
     }
     // get financial type selector clause
     $profile = new CRM_Donrec_Logic_Profile($values['profile']);
     $financialTypeClause = $profile->getContributionTypesClause();
     // run the main query
     $query = "SELECT `civicrm_contribution`.`id`\n              FROM (`civicrm_contribution`)\n              LEFT JOIN `{$custom_group_table}` AS existing_receipt\n                  ON  `civicrm_contribution`.`id` = existing_receipt.`entity_id`\n                  AND existing_receipt.`{$status_column}` = 'ORIGINAL'\n              WHERE\n                  ({$main_selector})\n                  {$query_date_limit}\n                  AND {$financialTypeClause}\n                  AND (`non_deductible_amount` = 0 OR `non_deductible_amount` IS NULL)\n                  AND `contribution_status_id` = 1\n                  AND `is_test` = 0\n                  AND `currency` = 'EUR'\n                  AND existing_receipt.`entity_id` IS NULL;";
     // execute the query
     $result = CRM_Core_DAO::executeQuery($query);
     // build array
     $contributionIds = array();
     while ($result->fetch()) {
         $contributionIds[] = $result->id;
     }
     // finally, build the snapshot with it
     return CRM_Donrec_Logic_Snapshot::create($contributionIds, CRM_Donrec_Logic_Settings::getLoggedInContactID(), $formatted_date_from, $formatted_date_to, $values['profile']);
 }
 function testCopy()
 {
     // prerequisites
     $contributions = $this->generateContributions(3);
     $this->assertEquals(3, count($contributions));
     // generate a new snapshot
     $snapshot = CRM_Donrec_Logic_Snapshot::create($contributions, 1);
     $this->assertNotNull($snapshot, "CRM_Donrec_Logic_Snapshot::create() returned NULL");
     // create a receipt
     $snapshot_line_id = 1;
     $params = array();
     $result = CRM_Donrec_Logic_Receipt::createSingleFromSnapshot($snapshot, $snapshot_line_id, $params);
     $this->assertEquals(TRUE, $result, "CRM_Donrec_Logic_Receipt::createSingleFromSnapshot returned FALSE");
     // clone it
 }
 /**
  * 
  *
  * @author niko bochan
  */
 public function testSnapshotConflictMixed()
 {
     // prerequisites
     $contributions = $this->generateContributions(5);
     $this->assertEquals(5, count($contributions));
     // generate a snapshot
     $result = CRM_Donrec_Logic_Snapshot::create($contributions, 1);
     $snapshot1 = $result['snapshot'];
     $this->assertNotNull($snapshot1, "CRM_Donrec_Logic_Snapshot::create() returned NULL");
     $this->assertDBQuery(5, "SELECT count(*) FROM `donrec_snapshot`");
     // generate a second, expired snapshot
     $result = CRM_Donrec_Logic_Snapshot::create($contributions, 2, -10);
     $snapshot2 = $result['snapshot'];
     $this->assertNotNull($snapshot2, "CRM_Donrec_Logic_Snapshot::create() returned NULL");
     $this->assertDBQuery(10, "SELECT count(*) FROM `donrec_snapshot`");
     // check if there are intersections between the two snapshots
     error_log("\n--- the following warnings are expected ---");
     $result = CRM_Donrec_Logic_Snapshot::hasIntersections();
     $this->assertEquals(5, $result);
     error_log("--- end of expected warnings ---");
 }
Esempio n. 4
0
 public function testEngineMultiExport()
 {
     // create a new snapshot
     $contributions = $this->generateContributions(6);
     $result = CRM_Donrec_Logic_Snapshot::create($contributions, 1);
     $snapshot = $result['snapshot'];
     // engine setup parameters
     $sid = $snapshot->getId();
     $parameters = array();
     $parameters['test'] = 1;
     $parameters['bulk'] = 0;
     $parameters['exporters'] = 'Dummy,SecondDummy';
     // let's try to start it
     $engine = new CRM_Donrec_Logic_Engine();
     $engine_error = $engine->init($sid, $parameters, TRUE);
     $this->assertEquals(FALSE, $engine_error);
     $ctr = 0;
     foreach ($contributions as $id) {
         $stats = $engine->nextStep();
         $ctr++;
         $this->assertDBQuery('TEST', sprintf("SELECT `status` FROM `donrec_snapshot` WHERE `contribution_id` = %d;", $id));
         $this->assertDBQuery('{"Dummy":{"test":"Dummy was here!"},"SecondDummy":{"test":"2nd Dummy was here!"}}', sprintf("SELECT `process_data` FROM `donrec_snapshot` WHERE `contribution_id` = %d;", $id));
         $this->assertEquals($stats['count'], 6);
         $this->assertEquals($stats['completed_test'], $ctr);
     }
 }