/** * * * @author niko bochan */ public function testSnapshotDeletion() { // prerequisites $contributions = $this->generateContributions(1); $this->assertEquals(1, count($contributions)); // generate a new snapshot $result = CRM_Donrec_Logic_Snapshot::create($contributions, 1); $snapshot = $result['snapshot']; $this->assertNotNull($snapshot, "CRM_Donrec_Logic_Snapshot::create() returned NULL"); // check if a contribution is part of the snapshot $result = CRM_Donrec_Logic_Snapshot::isInOpenSnapshot($contributions[0]); $this->assertEquals(TRUE, $result); // delete it $snapshot->delete(); // check if a contribution is still part of a snapshot $result = CRM_Donrec_Logic_Snapshot::isInOpenSnapshot($contributions[0]); $this->assertEquals(FALSE, $result); // check via sql whether the snapshot has been removed completely $this->assertDBQuery(0, "SELECT count(*) FROM `donrec_snapshot`"); }
function donrec_civicrm_pre($op, $objectName, $id, &$params) { if ($objectName == 'Contribution' && ($op == 'edit' || $op == 'delete')) { $has_item = CRM_Donrec_Logic_ReceiptItem::hasValidReceiptItem($id); $in_snapshot = CRM_Donrec_Logic_Snapshot::isInOpenSnapshot($id); if ($has_item || $in_snapshot) { if ($op == 'edit') { // columns that must not be changed $forbidden = array('financial_type_id', 'total_amount', 'receive_date', 'currency', 'contribution_status_id', 'payment_instrument_id'); // get the contribution $query = "\n SELECT *\n FROM civicrm_contribution\n WHERE id = {$id}"; $result = CRM_Core_DAO::executeQuery($query); $result->fetch(); // check if forbidden values are going to be changed. foreach ($forbidden as $col) { if (isset($params[$col]) && $result->{$col} != $params[$col]) { // we need a extra-check for dates (which are not in the same format) if (strpos($col, 'date')) { if ($col == 'receive_date' && substr(preg_replace('/[-: ]/', '', $result->{$col}), 0, -2) . "00" == $params[$col]) { continue; } } $message = sprintf(ts("The column '%s' of this contribution [%d] must not be changed because it has a receipt or is going to be receipted!", array('domain' => 'de.systopia.donrec')), $col, $id); CRM_Utils_DonrecHelper::exitWithMessage($message); } } } elseif ($op == 'delete') { $message = sprintf(ts("This contribution [%d] must not be deleted because it has a receipt or is going to be receipted!", array('domain' => 'de.systopia.donrec')), $id); CRM_Utils_DonrecHelper::exitWithMessage($message); } } } return; }