From 1f686b4e7a41544ad5268e528da6070f079fdfbc Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Thu, 12 Mar 2015 14:21:05 +0200 Subject: [PATCH] update findAllUnpaid instead of comparing total sums of client balance and total unpaid invoice amount, compare client balance and invoice item price. Find all unpaid invoices which can be covered from client balance. --- src/bb-modules/Invoice/Service.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bb-modules/Invoice/Service.php b/src/bb-modules/Invoice/Service.php index 135c6a731..7a8573299 100644 --- a/src/bb-modules/Invoice/Service.php +++ b/src/bb-modules/Invoice/Service.php @@ -1414,6 +1414,11 @@ class Service implements InjectionAwareInterface return true; } + /** + * Return list of unpaid invoices which can be covered from client balance + * @param array $filter + * @return array + */ public function findAllUnpaid(array $filter = null) { $sql = 'SELECT m.* @@ -1422,7 +1427,8 @@ class Service implements InjectionAwareInterface LEFT JOIN client_balance as cb on m.client_id = cb.client_id LEFT JOIN invoice_item as pi on pi.invoice_id = m.id WHERE m.status = :status - AND m.approved = 1'; + AND m.approved = 1 + AND cb.amount >= pi.price'; $params = array('status' => \Model_Invoice::STATUS_UNPAID); $client_id = isset($filter['client_id']) ? (int)$filter['client_id'] : null; @@ -1433,7 +1439,6 @@ class Service implements InjectionAwareInterface } $sql .= ' GROUP BY m.id, cl.id - HAVING SUM(cb.amount) >= SUM(pi.price) ORDER BY m.id DESC'; $records = $this->di['db']->getAll($sql, $params);