Skip to content

Commit

Permalink
Issue techjoomla#11: [Vendor payouts] Refactor code to get pending pa…
Browse files Browse the repository at this point in the history
…youts as per clients and currencies
  • Loading branch information
deepa-g committed Nov 2, 2018
1 parent 4f1516e commit 332bac2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/com_tjvendors/admin/helpers/tjvendors.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static function bulkPendingAmount($vendor_id, $currency)

if (!empty($pendingAmount))
{
$bulkPendingAmount = $bulkPendingAmount + $pendingAmount[$currency]['amount'];
$bulkPendingAmount = $bulkPendingAmount + $pendingAmount[$client['client']][$currency];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/com_tjvendors/admin/models/payout.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function loadFormData()

if (!empty($payableAmount))
{
$this->item->total = $payableAmount[$this->item->currency]['amount'];
$this->item->total = $payableAmount[$this->item->client][$this->item->currency];
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/com_tjvendors/admin/views/payouts/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@

<td>
<?php
if($this->bulkPayoutStatus==0)
if ($this->bulkPayoutStatus==0)
{
$result = $tjvendorsModelVendor->getPayableAmount($item->vendor_id, $item->client, $item->currency);

if (!empty($result))
{
echo $result[$item->currency]['amount'];
echo $result[$item->client][$item->currency];
}
}
else
Expand All @@ -245,7 +245,14 @@

if (!empty($result))
{
echo $result[$item->currency]['amount'];
$totalPayableAmount = 0;

foreach( $result as $payment)
{
$totalPayableAmount = $totalPayableAmount + $payment[$item->currency];
}

echo $totalPayableAmount;
}
}

Expand Down
90 changes: 22 additions & 68 deletions src/com_tjvendors/site/models/vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,22 @@ public function formatPaymentStructure($data, $paymentDetails)
* @param string $currency string like USD, EUR
*
* @return Array
*
* Array
(
[com_jgive] => Array
(
[EUR] => 2
[USD] => 20
)
[com_jticketing] => Array
(
[EUR] => 4
[USD] => 2
)
)
*/
public static function getPayableAmount($vendorId, $client = '', $currency = '')
{
Expand All @@ -547,6 +563,7 @@ public static function getPayableAmount($vendorId, $client = '', $currency = '')
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('SUM(credit) as credit');
$query->select('SUM(debit) as debit');
$query->select($db->quoteName('currency'));
$query->select($db->quoteName('client'));
$query->from($db->quoteName('#__tjvendors_passbook'));
Expand All @@ -564,84 +581,21 @@ public static function getPayableAmount($vendorId, $client = '', $currency = '')
}

$query->group($db->quoteName('currency'));
$query->group($db->quoteName('client'));
$db->setQuery($query);
$credit = $db->loadAssocList();

// Query to get debit data
$query = $db->getQuery(true);
$query->select('SUM(debit) as debit');
$query->select($db->quoteName('currency'));
$query->select($db->quoteName('client'));
$query->from($db->quoteName('#__tjvendors_passbook'));
$query->where($db->quoteName('vendor_id') . ' = ' . $db->quote($vendorId));

if (!empty($currency))
{
$query->where($db->quoteName('currency') . ' = ' . $db->quote($currency));
}

if (!empty($client))
{
$query->where($db->quoteName('client') . ' = ' . $db->quote($client));
}

$query->where($db->quoteName('status') . ' = ' . $db->quote(1));
$query->group($db->quoteName('currency'));
$db->setQuery($query);
$debit = $db->loadAssocList();

$payableAmount = array();

if (empty($credit))
{
return $payableAmount;
}
// Total credit amount against the vendor for e.g. 100 ($50 + €50)

foreach ($credit as $creditAmount)
{
// Total debit amount against the vendor for e.g out 100 ($50 + €50) $20 is paid
if (!empty($debit))
{
foreach ($debit as $debitAmount)
{
/* Here we are checking credit - debit amount as per currency */
/* For e.g. $50 - $20 = $30*/
if ($creditAmount['currency'] == $debitAmount['currency'] && $creditAmount['client'] == $debitAmount['client'])
{
$payableAmt['amount'] = $creditAmount["credit"] - $debitAmount["debit"];
$payableAmt['currency'] = $creditAmount['currency'];
$payableAmt['client'] = $creditAmount['client'];
}
/* For e.g. €50 - + €0 = $50*/
else
{
$payableAmt['amount'] = $creditAmount["credit"];
$payableAmt['currency'] = $creditAmount['currency'];
$payableAmt['client'] = $creditAmount['client'];
}
}
}
// If there is no amount has been debited for a vendor for e.g out $100 $0 amount is paid
else
{
$payableAmt['amount'] = $creditAmount["credit"];
$payableAmt['currency'] = $creditAmount['currency'];
$payableAmt['client'] = $creditAmount['client'];
}

$payableAmount[$creditAmount['currency']] = $payableAmt;
{
$payableAmount[$creditAmount['client']][$creditAmount['currency']] = $creditAmount['credit'] - $creditAmount['debit'];
}

/*
* Array
(
[USD] => Array
(
[amount] => 96.00
[currency] => USD
[client] => com_jticketing
)
)*/

return $payableAmount;
}
}

0 comments on commit 332bac2

Please sign in to comment.