Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static function handleMaterialContributionDelete($op, $objectName, $objec
/**
*
*/
public static function updateUniqueContributorsCount(int $contributionId, string $activeFieldName, int $currentContactId) {
public static function updateUniqueContributorsCount(int $contributionId, string $activeFieldName, int $currentContactId, bool $countCurrentContact = TRUE) {
$materialContactIds = [];

if (!in_array($activeFieldName, ['Material_Contribution.Institution_Goonj_Activities', 'Material_Contribution.Goonj_Activities'], TRUE)) {
Expand Down Expand Up @@ -368,7 +368,7 @@ public static function updateUniqueContributorsCount(int $contributionId, string
$inMaterial = in_array($currentContactId, $materialContactIds, TRUE);
$inMoney = in_array($currentContactId, $monetaryContactIds, TRUE);

if (!$inMaterial && !$inMoney) {
if ($countCurrentContact && !$inMaterial && !$inMoney) {
$uniqueCount++;
}

Expand Down Expand Up @@ -474,22 +474,26 @@ public static function updateMonetaryContributorsCount(string $op, string $objec
}

$contributionId = $objectRef->id;
if (isset($processed[$contributionId])) {
return;
}
$processed[$contributionId] = TRUE;

$contribution = Contribution::get(FALSE)
->addSelect('contact_id', 'Contribution_Details.Source.id')
->addSelect('contact_id', 'Contribution_Details.Source.id', 'contribution_status_id:name')
->addWhere('id', '=', $contributionId)
->addWhere('contribution_status_id:name', '=', 'Completed')
->execute()
->first();

if (!$contribution || empty($contribution['Contribution_Details.Source.id'])) {
return;
}

// Process each status of a contribution once, not just the first save:
// a CSV import saves it as Pending and completes it in a second save, and
// a cancellation must bring the count back down.
$status = $contribution['contribution_status_id:name'];
if (isset($processed[$contributionId][$status])) {
return;
}
$processed[$contributionId][$status] = TRUE;

$contributionId = $contribution['Contribution_Details.Source.id'];

$collectionCamps = EckEntity::get('Collection_Camp', FALSE)
Expand All @@ -502,7 +506,7 @@ public static function updateMonetaryContributorsCount(string $op, string $objec

if ($subtype) {
$fieldName = "Material_Contribution.$subtype";
self::updateUniqueContributorsCount($contributionId, $fieldName, $contribution['contact_id']);
self::updateUniqueContributorsCount($contributionId, $fieldName, $contribution['contact_id'], $status === 'Completed');
}

$allContributions = Contribution::get(FALSE)
Expand Down Expand Up @@ -614,30 +618,36 @@ public static function handleMonetaryContributionDelete(string $op, string $obje
$existing = EckEntity::get('Collection_Camp')
->addSelect(
'Core_Contribution_Details.Total_online_monetary_contributions',
'Core_Contribution_Details.Total_cash_cheque_monetary_contributions'
'Core_Contribution_Details.Total_cash_cheque_monetary_contributions',
'Core_Contribution_Details.Total_Other_Monetary_Contributions'
)
->addWhere('id', '=', $contributionId)
->execute()
->first();

$onlineCurrent = (float) ($existing['Core_Contribution_Details.Total_online_monetary_contributions'] ?? 0);
$cashCurrent = (float) ($existing['Core_Contribution_Details.Total_cash_cheque_monetary_contributions'] ?? 0);
$otherCurrent = (float) ($existing['Core_Contribution_Details.Total_Other_Monetary_Contributions'] ?? 0);

$onlineNew = $onlineCurrent;
$cashNew = $cashCurrent;
$otherNew = $otherCurrent;

if ($paymentMethod === 'Credit Card') {
$onlineNew = max(0, $onlineCurrent - $amount);
}

if (in_array($paymentMethod, ['Cash', 'Check'], TRUE)) {
elseif (in_array($paymentMethod, ['Cash', 'Check'], TRUE)) {
$cashNew = max(0, $cashCurrent - $amount);
}
else {
$otherNew = max(0, $otherCurrent - $amount);
}

$update = EckEntity::update('Collection_Camp')
->addWhere('id', '=', $contributionId)
->addValue('Core_Contribution_Details.Total_online_monetary_contributions', $onlineNew)
->addValue('Core_Contribution_Details.Total_cash_cheque_monetary_contributions', $cashNew);
->addValue('Core_Contribution_Details.Total_cash_cheque_monetary_contributions', $cashNew)
->addValue('Core_Contribution_Details.Total_Other_Monetary_Contributions', $otherNew);

if (!$currentContactStillExists) {
$update->addValue('Core_Contribution_Details.Number_of_unique_monetary_contributors', $uniqueMonetaryCount);
Expand Down Expand Up @@ -665,10 +675,6 @@ public static function updateMonetaryContributionTotalAmount(string $op, string
}

$contributionId = $objectRef->id;
if (isset($processed[$contributionId])) {
return;
}
$processed[$contributionId] = TRUE;

$contribution = Contribution::get(FALSE)
->addSelect(
Expand All @@ -678,14 +684,22 @@ public static function updateMonetaryContributionTotalAmount(string $op, string
'payment_instrument_id:name'
)
->addWhere('id', '=', $contributionId)
->addWhere('contribution_status_id:name', '=', 'Completed')
->execute()
->first();

if (!$contribution || empty($contribution['Contribution_Details.Source.id'])) {
return;
}

// Process each status of a contribution once, not just the first save:
// a CSV import saves it as Pending and completes it in a second save, and
// a cancellation must bring the totals back down.
$status = $contribution['contribution_status_id:name'];
if (isset($processed[$contributionId][$status])) {
return;
}
$processed[$contributionId][$status] = TRUE;

$campId = (int) $contribution['Contribution_Details.Source.id'];

$allCampContributions = Contribution::get(FALSE)
Expand All @@ -696,6 +710,7 @@ public static function updateMonetaryContributionTotalAmount(string $op, string

$onlineTotal = 0.0;
$cashChequeTotal = 0.0;
$otherTotal = 0.0;

foreach ($allCampContributions as $item) {
$amount = (float) ($item['total_amount'] ?? 0);
Expand All @@ -707,13 +722,19 @@ public static function updateMonetaryContributionTotalAmount(string $op, string
elseif (in_array($method, ['Cash', 'Check'], TRUE)) {
$cashChequeTotal += $amount;
}
else {
// Any payment method that is not Razorpay or cash/cheque, including
// ones added in future (UPI, Wire Transfer, ...).
$otherTotal += $amount;
}
}

try {
EckEntity::update('Collection_Camp', FALSE)
->addWhere('id', '=', $campId)
->addValue('Core_Contribution_Details.Total_online_monetary_contributions', $onlineTotal)
->addValue('Core_Contribution_Details.Total_cash_cheque_monetary_contributions', $cashChequeTotal)
->addValue('Core_Contribution_Details.Total_Other_Monetary_Contributions', $otherTotal)
->execute();
}
catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ public static function handleEventMonetaryContributionDelete(string $op, string
$existing = Event::get(FALSE)
->addSelect(
'Goonj_Events_Outcome.Online_Monetary_Contribution',
'Goonj_Events_Outcome.Cash_Contribution'
'Goonj_Events_Outcome.Cash_Contribution',
'Goonj_Events_Outcome.Total_Other_Monetary_Contributions'
)
->addWhere('id', '=', $eventId)
->execute()
->first();

$onlineCurrent = (float) ($existing['Goonj_Events_Outcome.Online_Monetary_Contribution'] ?? 0);
$cashCurrent = (float) ($existing['Goonj_Events_Outcome.Cash_Contribution'] ?? 0);
$otherCurrent = (float) ($existing['Goonj_Events_Outcome.Total_Other_Monetary_Contributions'] ?? 0);

$update = Event::update()
->addWhere('id', '=', $eventId);
Expand All @@ -150,9 +152,12 @@ public static function handleEventMonetaryContributionDelete(string $op, string
$update->addValue('Goonj_Events_Outcome.Online_Monetary_Contribution', max(0, $onlineCurrent - $amount));
}

if (in_array($paymentMethod, ['Cash', 'Check'], TRUE)) {
elseif (in_array($paymentMethod, ['Cash', 'Check'], TRUE)) {
$update->addValue('Goonj_Events_Outcome.Cash_Contribution', max(0, $cashCurrent - $amount));
}
else {
$update->addValue('Goonj_Events_Outcome.Total_Other_Monetary_Contributions', max(0, $otherCurrent - $amount));
}

$update->execute();

Expand Down Expand Up @@ -240,7 +245,7 @@ public static function handleEventMaterialContributionDelete(string $op, string
/**
*
*/
public static function updateUniqueContributorsCount(int $eventId) {
public static function updateUniqueContributorsCount(int $eventId, ?int $currentContactId = NULL) {
$materialActivities = Activity::get(FALSE)
->addSelect('source_contact_id')
->addWhere('Material_Contribution.Event', '=', $eventId)
Expand All @@ -253,6 +258,12 @@ public static function updateUniqueContributorsCount(int $eventId) {
}
}

// The contact whose material contribution is being saved right now; see
// updateEventMatrialContributorsCount.
if ($currentContactId) {
$materialContactIds[] = $currentContactId;
}

$monetaryContributions = Contribution::get(FALSE)
->addSelect('contact_id')
->addWhere('Contribution_Details.Events.id', '=', $eventId)
Expand Down Expand Up @@ -320,9 +331,17 @@ public static function updateEventMatrialContributorsCount(string $op, string $o
}
}

// The form's submission is saved before its activity, so the material
// contribution being added is not in the list above yet; count its
// contact here (the camp hook does the same).
$currentContactId = (int) ($activityFields['source_contact_id'] ?? 0);
if ($currentContactId) {
$uniqueContactIds[$currentContactId] = TRUE;
}

$uniqueCount = count($uniqueContactIds);

self::updateUniqueContributorsCount($eventId);
self::updateUniqueContributorsCount($eventId, $currentContactId ?: NULL);

try {
Event::update()
Expand All @@ -345,22 +364,26 @@ public static function updateEventMonetaryContributorsCount(string $op, string $
}

$contributionId = $objectRef->id;
if (isset($processed[$contributionId])) {
return;
}
$processed[$contributionId] = TRUE;

$contribution = Contribution::get(FALSE)
->addSelect('contact_id', 'Contribution_Details.Events.id')
->addSelect('contact_id', 'Contribution_Details.Events.id', 'contribution_status_id:name')
->addWhere('id', '=', $contributionId)
->addWhere('contribution_status_id:name', '=', 'Completed')
->execute()
->first();

if (!$contribution || empty($contribution['Contribution_Details.Events.id'])) {
return;
}

// Process each status of a contribution once, not just the first save:
// a CSV import saves it as Pending and completes it in a second save, and
// a cancellation must bring the count back down.
$status = $contribution['contribution_status_id:name'];
if (isset($processed[$contributionId][$status])) {
return;
}
$processed[$contributionId][$status] = TRUE;

$eventId = $contribution['Contribution_Details.Events.id'];

if (!$eventId) {
Expand Down Expand Up @@ -401,10 +424,6 @@ public static function updateEventMonetaryContributionTotalAmount(string $op, st
}

$contributionId = $objectRef->id;
if (isset($processed[$contributionId])) {
return;
}
$processed[$contributionId] = TRUE;

$contribution = Contribution::get(FALSE)
->addSelect(
Expand All @@ -414,43 +433,57 @@ public static function updateEventMonetaryContributionTotalAmount(string $op, st
'payment_instrument_id:name'
)
->addWhere('id', '=', $contributionId)
->addWhere('contribution_status_id:name', '=', 'Completed')
->execute()
->first();

if (!$contribution || empty($contribution['Contribution_Details.Events.id'])) {
return;
}

$eventId = $contribution['Contribution_Details.Events.id'];
$totalAmount = (float) $contribution['total_amount'];
$paymentMethod = $contribution['payment_instrument_id:name'];
// Process each status of a contribution once, not just the first save:
// a CSV import saves it as Pending and completes it in a second save, and
// a cancellation must bring the totals back down.
$status = $contribution['contribution_status_id:name'];
if (isset($processed[$contributionId][$status])) {
return;
}
$processed[$contributionId][$status] = TRUE;

$existing = Event::get(FALSE)
->addSelect(
'Goonj_Events_Outcome.Online_Monetary_Contribution',
'Goonj_Events_Outcome.Cash_Contribution'
)
->addWhere('id', '=', $eventId)
->execute()
->first();
$eventId = (int) $contribution['Contribution_Details.Events.id'];

$onlineCurrent = (float) ($existing['Goonj_Events_Outcome.Online_Monetary_Contribution'] ?? 0);
$cashCurrent = (float) ($existing['Goonj_Events_Outcome.Cash_Contribution'] ?? 0);
$update = Event::update(FALSE)
->addWhere('id', '=', $eventId);
$allEventContributions = Contribution::get(FALSE)
->addSelect('total_amount', 'payment_instrument_id:name')
->addWhere('Contribution_Details.Events.id', '=', $eventId)
->addWhere('contribution_status_id:name', '=', 'Completed')
->execute();

if ($paymentMethod === 'Credit Card') {
$onlineNew = $onlineCurrent + $totalAmount;
$update->addValue('Goonj_Events_Outcome.Online_Monetary_Contribution', $onlineNew);
}
$onlineTotal = 0.0;
$cashChequeTotal = 0.0;
$otherTotal = 0.0;

foreach ($allEventContributions as $item) {
$amount = (float) ($item['total_amount'] ?? 0);
$method = $item['payment_instrument_id:name'] ?? '';

if (in_array($paymentMethod, ['Cash', 'Check'], TRUE)) {
$cashNew = $cashCurrent + $totalAmount;
$update->addValue('Goonj_Events_Outcome.Cash_Contribution', $cashNew);
if ($method === 'Credit Card') {
$onlineTotal += $amount;
}
elseif (in_array($method, ['Cash', 'Check'], TRUE)) {
$cashChequeTotal += $amount;
}
else {
// Any payment method that is not Razorpay or cash/cheque, including
// ones added in future (UPI, Wire Transfer, ...).
$otherTotal += $amount;
}
}

$update->execute();
Event::update(FALSE)
->addWhere('id', '=', $eventId)
->addValue('Goonj_Events_Outcome.Online_Monetary_Contribution', $onlineTotal)
->addValue('Goonj_Events_Outcome.Cash_Contribution', $cashChequeTotal)
->addValue('Goonj_Events_Outcome.Total_Other_Monetary_Contributions', $otherTotal)
->execute();
}

/**
Expand Down
Loading
Loading