From d9061bd3f9f0297396464986774b13a8c9ac3191 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Mon, 26 Jan 2015 17:52:12 +0200 Subject: [PATCH 01/16] increase test fixture's group_id values. old values were mixing with mysql's incremental initial value of table --- tests/fixtures/initial.xml | 12 +++++----- tests/fixtures/mod_cart.xml | 24 +++++++++---------- tests/fixtures/orders.xml | 22 ++++++++--------- tests/fixtures/transactions.xml | 6 ++--- .../bb-modules/mod_order/Api_AdminTest.php | 2 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/fixtures/initial.xml b/tests/fixtures/initial.xml index 00dcffe04..e8212c9db 100644 --- a/tests/fixtures/initial.xml +++ b/tests/fixtures/initial.xml @@ -7,12 +7,12 @@ - - - - - - + + + + + + diff --git a/tests/fixtures/mod_cart.xml b/tests/fixtures/mod_cart.xml index 8e669fd44..2f90634ed 100644 --- a/tests/fixtures/mod_cart.xml +++ b/tests/fixtures/mod_cart.xml @@ -52,20 +52,20 @@ - - + + - - - - - - + + + + + + - - - - + + + + diff --git a/tests/fixtures/orders.xml b/tests/fixtures/orders.xml index 59b0a99f1..eed9dcf26 100644 --- a/tests/fixtures/orders.xml +++ b/tests/fixtures/orders.xml @@ -50,18 +50,18 @@ - - - - - - + + + + + + - - - - - + + + + + diff --git a/tests/fixtures/transactions.xml b/tests/fixtures/transactions.xml index 426285822..117fe3f43 100644 --- a/tests/fixtures/transactions.xml +++ b/tests/fixtures/transactions.xml @@ -8,9 +8,9 @@ - - - + + + diff --git a/tests/integration/bb-modules/mod_order/Api_AdminTest.php b/tests/integration/bb-modules/mod_order/Api_AdminTest.php index 60960fb88..8ad4487bb 100644 --- a/tests/integration/bb-modules/mod_order/Api_AdminTest.php +++ b/tests/integration/bb-modules/mod_order/Api_AdminTest.php @@ -210,7 +210,7 @@ class Api_Admin_OrderTest extends BBDbApiTestCase $data['client_id'] = 1; $data['product_id'] = $pid; $data['period'] = '1M'; - $data['group_id'] = 2; + $data['group_id'] = 200; // $data['currency'] = 'EUR'; // $data['invoice_option'] = 'issue-invoice'; $data['invoice_option'] = 'no-invoice'; From 47dff4160ea53ba3d3a9e588faae4d6ffea43f75 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 10:10:34 +0200 Subject: [PATCH 02/16] do not activate order addon while invoice is not paid --- src/bb-modules/Cart/Service.php | 33 +++++++++++++++- tests/fixtures/mod_cart.xml | 1 + .../bb-modules/mod_cart/Api_ClientTest.php | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 799259a4c..dfae402e2 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -593,7 +593,7 @@ class Service implements InjectionAwareInterface $order = $this->di['db']->load('ClientOrder', $oid); $oa = $orderService->toApiArray($order, false, $client); $product = $this->di['db']->getExistingModelById('Product', $oa['product_id']); - if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0)) { + if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($this->canActivateOrder($product, $oa))) { try { $orderService->activateOrder($order); } catch (\Exception $e) { @@ -608,6 +608,37 @@ class Service implements InjectionAwareInterface return $result; } + /** + * Activate order rules: + * - product setup == SETUP_AFTER_PAYMENT + * - order amount must be > 0 + * - group invoice must be paid to activate zero amount and addon order + * + * @param \Model_Product $product + * @param array $orderArray + * @return bool + */ + public function canActivateOrder(\Model_Product $product, array $orderArray) + { + if ($product->setup != \Model_ProductTable::SETUP_AFTER_PAYMENT){ + return false; + } + + if ($orderArray['total'] - $orderArray['discount'] > 0){ + return false; + } + + if ($orderArray['group_master'] == 0 && $orderArray['group_id'] > 0){ + $invoice = $this->di['db']->load('Invoice', $orderArray['unpaid_invoice_id']); + if ($invoice->status != \Model_Invoice::STATUS_PAID){ + return false; + } + + } + + return true; + } + public function usePromo(\Model_Promo $promo) { $promo->used++; diff --git a/tests/fixtures/mod_cart.xml b/tests/fixtures/mod_cart.xml index 2f90634ed..dec4dd9e0 100644 --- a/tests/fixtures/mod_cart.xml +++ b/tests/fixtures/mod_cart.xml @@ -76,6 +76,7 @@ + diff --git a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php index 3ed9afd11..33b35f267 100644 --- a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php @@ -102,4 +102,42 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase $this->assertEquals(9874, $e->getCode()); } } + + /** + * addon activated while order Invoice Unpaid + */ + public function testAddonActivatedWhileOrderInvoiceUnpaid() + { + + $this->api_guest->cart_reset(); + $data = array( + 'id' => 11, + 'period' => '6M', + 'domain' => array( + 'action' => "register", + 'owndomain_sld' =>"", + 'owndomain_tld' =>".com", + 'register_sld' =>"newdomainregister", + 'register_tld' =>".com", + 'register_years' => "1", + ), + 'multiple' => 1, + + ); + $this->api_guest->cart_add_item($data); + $this->api_client->cart_checkout(); + + $masterOrder = $this->di['db']->findOne('ClientOrder', 'group_master = 1 ORDER BY id desc'); + $addonOrder = $this->di['db']->findOne('ClientOrder', 'group_id = ? and group_master = 0', array($masterOrder->group_id)); + + $invoiceModel = $this->di['db']->load('Invoice', $masterOrder->unpaid_invoice_id); + + $this->assertInstanceOf('Model_ClientOrder', $masterOrder); + $this->assertInstanceOf('Model_ClientOrder', $addonOrder); + $this->assertInstanceOf('Model_Invoice', $invoiceModel); + + $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $masterOrder->status); + $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $addonOrder->status); + $this->assertEquals(Model_Invoice::STATUS_UNPAID, $invoiceModel->status); + } } \ No newline at end of file From 95a435456b2ecdd24be63a83418e9ff2db1b1f3d Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 10:28:27 +0200 Subject: [PATCH 03/16] order models are stored in array, not it ids --- src/bb-modules/Cart/Service.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index dfae402e2..b3c1fcd43 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -521,7 +521,7 @@ class Service implements InjectionAwareInterface $order->updated_at = date('c'); $this->di['db']->store($order); - $orders[] = $order->id; + $orders[] = $order; // mark promo as used if ($cart->promo_id) { @@ -575,10 +575,9 @@ class Service implements InjectionAwareInterface $invoiceModel = $this->di['db']->load('Invoice', $invoice_id); $invoiceService->approveInvoice($invoiceModel, array('id' => $invoice_id, 'use_credits' => true)); - foreach ($orders as $oid) { - $o = $this->di['db']->load('ClientOrder', $oid); - $o->unpaid_invoice_id = $invoice_id; - $this->di['db']->store($o); + foreach ($orders as $order) { + $order->unpaid_invoice_id = $invoice_id; + $this->di['db']->store($order); } $result = array($master_order, $invoice_id, $orders); @@ -588,9 +587,8 @@ class Service implements InjectionAwareInterface //activate orders if product is setup to be activated after order place or order total is $0 $orderService = $this->di['mod_service']('Order'); - foreach ($orders as $oid) { + foreach ($orders as $order) { - $order = $this->di['db']->load('ClientOrder', $oid); $oa = $orderService->toApiArray($order, false, $client); $product = $this->di['db']->getExistingModelById('Product', $oa['product_id']); if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($this->canActivateOrder($product, $oa))) { From 16367a2a5cd466a48ccba09f98a059b8d87f3d27 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 10:39:52 +0200 Subject: [PATCH 04/16] remove redundant cart query --- src/bb-modules/Cart/Service.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index b3c1fcd43..9270b5c12 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -460,18 +460,12 @@ class Service implements InjectionAwareInterface public function createFromCart(\Model_Client $client, $gateway_id = null) { - $cart = $this->getSessionCart(); - $cartProducts = $this->di['db']->find('CartProduct', 'cart_id = :cart_id', array(':cart_id' => $cart->id)); - if (count($cartProducts) == 0) { + $cart = $this->getSessionCart(); + $ca = $this->toApiArray($cart); + if (count($ca['items']) == 0) { throw new \Box_Exception('Can not checkout empty cart.'); } - $ca = $this->toApiArray($cart); - - $create_invoice = true; - if ($ca['total'] == 0) { - $create_invoice = false; - } $currency = $this->di['db']->getExistingModelById('Currency', $cart->currency_id, 'Currency not found.'); @@ -568,7 +562,7 @@ class Service implements InjectionAwareInterface $i++; } - if ($create_invoice) { + if ($ca['total'] > 0) { //crete invoice if order total > 0 $invoiceService = $this->di['mod_service']('Invoice'); $invoice_id = $invoiceService->prepareInvoice($client, array('client_id' => $client->id, 'items' => $invoice_items, 'gateway_id' => $gateway_id)); From 62ee45c96fc322e917bbeed1dd9a216ff7c872de Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 11:09:48 +0200 Subject: [PATCH 05/16] prepareInvoice() will return Model_Invoice object instead of invoice id --- src/bb-modules/Invoice/Api/Admin.php | 3 ++- src/bb-modules/Invoice/Service.php | 2 +- tests/bb-modules/Invoice/Api/AdminTest.php | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/bb-modules/Invoice/Api/Admin.php b/src/bb-modules/Invoice/Api/Admin.php index 21f7e295d..b06d7073b 100644 --- a/src/bb-modules/Invoice/Api/Admin.php +++ b/src/bb-modules/Invoice/Api/Admin.php @@ -93,7 +93,8 @@ class Admin extends \Api_Abstract $client = $this->di['db']->getExistingModelById('Client', $data['client_id'], 'Client not found'); - return $this->getService()->prepareInvoice($client, $data); + $invoice = $this->getService()->prepareInvoice($client, $data); + return $invoice->id; } /** diff --git a/src/bb-modules/Invoice/Service.php b/src/bb-modules/Invoice/Service.php index c4bd5f2f0..f2241ceaf 100644 --- a/src/bb-modules/Invoice/Service.php +++ b/src/bb-modules/Invoice/Service.php @@ -493,7 +493,7 @@ class Service implements InjectionAwareInterface } } - return $invoiceId; + return $model; } public function setInvoiceDefaults(\Model_Invoice $model) diff --git a/tests/bb-modules/Invoice/Api/AdminTest.php b/tests/bb-modules/Invoice/Api/AdminTest.php index aaee5f655..69dfcbc5c 100644 --- a/tests/bb-modules/Invoice/Api/AdminTest.php +++ b/tests/bb-modules/Invoice/Api/AdminTest.php @@ -117,10 +117,14 @@ class AdminTest extends \PHPUnit_Framework_TestCase { ); $newInvoiceId = 1; + $invoiceModel = new \Model_Invoice(); + $invoiceModel->loadBean(new \RedBeanPHP\OODBBean()); + $invoiceModel->id = $newInvoiceId; + $serviceMock = $this->getMockBuilder('\Box\Mod\Invoice\Service')->getMock(); $serviceMock->expects($this->atLeastOnce()) ->method('prepareInvoice') - ->will($this->returnValue($newInvoiceId)); + ->will($this->returnValue($invoiceModel)); $validatorMock = $this->getMockBuilder('\Box_Validate')->getMock(); $validatorMock->expects($this->atLeastOnce()) From 6daa231085142ae13f5156b4e5ef32cbab2a2dab Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 11:34:56 +0200 Subject: [PATCH 06/16] REVERT do not activate order addon while invoice is not paid (reverted from commit 47dff4160ea53ba3d3a9e588faae4d6ffea43f75) --- src/bb-modules/Cart/Service.php | 33 +--------------- tests/fixtures/mod_cart.xml | 1 - .../bb-modules/mod_cart/Api_ClientTest.php | 38 ------------------- 3 files changed, 1 insertion(+), 71 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 9270b5c12..0b1a8518d 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -585,7 +585,7 @@ class Service implements InjectionAwareInterface $oa = $orderService->toApiArray($order, false, $client); $product = $this->di['db']->getExistingModelById('Product', $oa['product_id']); - if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($this->canActivateOrder($product, $oa))) { + if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0)) { try { $orderService->activateOrder($order); } catch (\Exception $e) { @@ -600,37 +600,6 @@ class Service implements InjectionAwareInterface return $result; } - /** - * Activate order rules: - * - product setup == SETUP_AFTER_PAYMENT - * - order amount must be > 0 - * - group invoice must be paid to activate zero amount and addon order - * - * @param \Model_Product $product - * @param array $orderArray - * @return bool - */ - public function canActivateOrder(\Model_Product $product, array $orderArray) - { - if ($product->setup != \Model_ProductTable::SETUP_AFTER_PAYMENT){ - return false; - } - - if ($orderArray['total'] - $orderArray['discount'] > 0){ - return false; - } - - if ($orderArray['group_master'] == 0 && $orderArray['group_id'] > 0){ - $invoice = $this->di['db']->load('Invoice', $orderArray['unpaid_invoice_id']); - if ($invoice->status != \Model_Invoice::STATUS_PAID){ - return false; - } - - } - - return true; - } - public function usePromo(\Model_Promo $promo) { $promo->used++; diff --git a/tests/fixtures/mod_cart.xml b/tests/fixtures/mod_cart.xml index dec4dd9e0..2f90634ed 100644 --- a/tests/fixtures/mod_cart.xml +++ b/tests/fixtures/mod_cart.xml @@ -76,7 +76,6 @@ - diff --git a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php index 33b35f267..3ed9afd11 100644 --- a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php @@ -102,42 +102,4 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase $this->assertEquals(9874, $e->getCode()); } } - - /** - * addon activated while order Invoice Unpaid - */ - public function testAddonActivatedWhileOrderInvoiceUnpaid() - { - - $this->api_guest->cart_reset(); - $data = array( - 'id' => 11, - 'period' => '6M', - 'domain' => array( - 'action' => "register", - 'owndomain_sld' =>"", - 'owndomain_tld' =>".com", - 'register_sld' =>"newdomainregister", - 'register_tld' =>".com", - 'register_years' => "1", - ), - 'multiple' => 1, - - ); - $this->api_guest->cart_add_item($data); - $this->api_client->cart_checkout(); - - $masterOrder = $this->di['db']->findOne('ClientOrder', 'group_master = 1 ORDER BY id desc'); - $addonOrder = $this->di['db']->findOne('ClientOrder', 'group_id = ? and group_master = 0', array($masterOrder->group_id)); - - $invoiceModel = $this->di['db']->load('Invoice', $masterOrder->unpaid_invoice_id); - - $this->assertInstanceOf('Model_ClientOrder', $masterOrder); - $this->assertInstanceOf('Model_ClientOrder', $addonOrder); - $this->assertInstanceOf('Model_Invoice', $invoiceModel); - - $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $masterOrder->status); - $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $addonOrder->status); - $this->assertEquals(Model_Invoice::STATUS_UNPAID, $invoiceModel->status); - } } \ No newline at end of file From 9847d83f93879f09fe54f9ee1361d42488e384fc Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 12:06:55 +0200 Subject: [PATCH 07/16] do not set order unpaid_invoice_id if it was covered from client balance --- src/bb-modules/Cart/Service.php | 15 ++++++++------- tests/fixtures/mod_cart.xml | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 0b1a8518d..32fd335f7 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -565,16 +565,17 @@ class Service implements InjectionAwareInterface if ($ca['total'] > 0) { //crete invoice if order total > 0 $invoiceService = $this->di['mod_service']('Invoice'); - $invoice_id = $invoiceService->prepareInvoice($client, array('client_id' => $client->id, 'items' => $invoice_items, 'gateway_id' => $gateway_id)); - $invoiceModel = $this->di['db']->load('Invoice', $invoice_id); - $invoiceService->approveInvoice($invoiceModel, array('id' => $invoice_id, 'use_credits' => true)); + $invoiceModel = $invoiceService->prepareInvoice($client, array('client_id' => $client->id, 'items' => $invoice_items, 'gateway_id' => $gateway_id)); + $invoiceService->approveInvoice($invoiceModel, array('id' => $invoiceModel->id, 'use_credits' => true)); - foreach ($orders as $order) { - $order->unpaid_invoice_id = $invoice_id; - $this->di['db']->store($order); + if ($invoiceModel->status == \Model_Invoice::STATUS_UNPAID) { + foreach ($orders as $order) { + $order->unpaid_invoice_id = $invoiceModel->id; + $this->di['db']->store($order); + } } - $result = array($master_order, $invoice_id, $orders); + $result = array($master_order, $invoiceModel->id, $orders); } else { $result = array($master_order, null, $orders); } diff --git a/tests/fixtures/mod_cart.xml b/tests/fixtures/mod_cart.xml index 2f90634ed..dec4dd9e0 100644 --- a/tests/fixtures/mod_cart.xml +++ b/tests/fixtures/mod_cart.xml @@ -76,6 +76,7 @@ + From 9c1cab3ffae8010459811f06adc5959f9d65a00f Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 14:01:55 +0200 Subject: [PATCH 08/16] fix related to 95a435456b2ecdd24be63a83418e9ff2db1b1f3d return array with order ids --- src/bb-modules/Cart/Service.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 32fd335f7..43c1a3592 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -582,8 +582,9 @@ class Service implements InjectionAwareInterface //activate orders if product is setup to be activated after order place or order total is $0 $orderService = $this->di['mod_service']('Order'); + $ids = array(); foreach ($orders as $order) { - + $ids[] = $order->id; $oa = $orderService->toApiArray($order, false, $client); $product = $this->di['db']->getExistingModelById('Product', $oa['product_id']); if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0)) { @@ -598,6 +599,7 @@ class Service implements InjectionAwareInterface } } + $result[2] = $ids; return $result; } From 9eafbfd167793a5629b47390d3f9db136f35e2cf Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 14:36:53 +0200 Subject: [PATCH 09/16] do not activate addon order while master order is not paid --- src/bb-modules/Cart/Service.php | 19 +++++---- .../bb-modules/mod_cart/Api_ClientTest.php | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 43c1a3592..931210a71 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -587,15 +587,20 @@ class Service implements InjectionAwareInterface $ids[] = $order->id; $oa = $orderService->toApiArray($order, false, $client); $product = $this->di['db']->getExistingModelById('Product', $oa['product_id']); - if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER || ($product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0)) { - try { + try { + if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER){ $orderService->activateOrder($order); - } catch (\Exception $e) { - error_log($e->getMessage()); - $status = 'error'; - $notes = 'Order could not be activated after checkout due to error: ' . $e->getMessage(); - $orderService->orderStatusAdd($order, $status, $notes); } + + if ($ca['total'] <= 0 && $product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0){ + $orderService->activateOrder($order); + } + } + catch (\Exception $e) { + error_log($e->getMessage()); + $status = 'error'; + $notes = 'Order could not be activated after checkout due to error: ' . $e->getMessage(); + $orderService->orderStatusAdd($order, $status, $notes); } } diff --git a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php index 3ed9afd11..5816e4763 100644 --- a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php @@ -102,4 +102,43 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase $this->assertEquals(9874, $e->getCode()); } } + + /** + * addon activated while order Invoice Unpaid + */ + public function testAddonActivatedWhileOrderInvoiceUnpaid() + { + + $this->api_guest->cart_reset(); + $data = array( + 'id' => 11, + 'period' => '6M', + 'domain' => array( + 'action' => "register", + 'owndomain_sld' =>"", + 'owndomain_tld' =>".com", + 'register_sld' =>"newdomainregister", + 'register_tld' =>".com", + 'register_years' => "1", + ), + 'multiple' => 1, + + ); + $this->api_guest->cart_add_item($data); + $this->api_client->cart_checkout(); + + $masterOrder = $this->di['db']->findOne('ClientOrder', 'group_master = 1 ORDER BY id desc'); + $addonOrder = $this->di['db']->findOne('ClientOrder', 'group_id = ? and group_master = 0', array($masterOrder->group_id)); + + $invoiceModel = $this->di['db']->load('Invoice', $masterOrder->unpaid_invoice_id); + + $this->assertInstanceOf('Model_ClientOrder', $masterOrder); + $this->assertInstanceOf('Model_ClientOrder', $addonOrder); + $this->assertInstanceOf('Model_Invoice', $invoiceModel); + + $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $masterOrder->status); + $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $addonOrder->status); + $this->assertEquals(Model_Invoice::STATUS_UNPAID, $invoiceModel->status); + } + } \ No newline at end of file From f14fe0785b52d4f7b660e3ebd894adc53ed4e822 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 14:39:26 +0200 Subject: [PATCH 10/16] unit test fix related 62ee45c96fc322e917bbeed1dd9a216ff7c872de --- tests/bb-modules/Invoice/ServiceTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/bb-modules/Invoice/ServiceTest.php b/tests/bb-modules/Invoice/ServiceTest.php index 490bf7e90..00e5994f7 100644 --- a/tests/bb-modules/Invoice/ServiceTest.php +++ b/tests/bb-modules/Invoice/ServiceTest.php @@ -628,8 +628,7 @@ class ServiceTest extends \PHPUnit_Framework_TestCase $serviceMock->setDi($di); $result = $serviceMock->prepareInvoice($clientModel, $data); - $this->assertInternalType('int', $result); - $this->assertEquals($newRecordId, $result); + $this->assertInstanceOf('Model_Invoice', $result); } public function testsetInvoiceDefaults() From 3c82310d3585af96b7aada52506cb04cfda988a0 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 16:29:13 +0200 Subject: [PATCH 11/16] Client\Api\Client balance_get_total() created returns client balance amount --- src/bb-modules/Client/Api/Client.php | 10 +++++++++ tests/bb-modules/Client/Api/ClientTest.php | 25 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/bb-modules/Client/Api/Client.php b/src/bb-modules/Client/Api/Client.php index 4558c2b97..20f5cecdc 100644 --- a/src/bb-modules/Client/Api/Client.php +++ b/src/bb-modules/Client/Api/Client.php @@ -309,4 +309,14 @@ class Client extends \Api_Abstract return $pager; } + /** + * Get client balance + * @return float + */ + public function balance_get_total() + { + $service = $this->di['mod_service']('Client', 'Balance'); + return $service->getClientBalance($this->identity); + } + } \ No newline at end of file diff --git a/tests/bb-modules/Client/Api/ClientTest.php b/tests/bb-modules/Client/Api/ClientTest.php index 14860d034..e63912588 100644 --- a/tests/bb-modules/Client/Api/ClientTest.php +++ b/tests/bb-modules/Client/Api/ClientTest.php @@ -175,5 +175,30 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $result = $api->update($data); $this->assertTrue($result); } + + public function testbalance_get_total() + { + $balanceAmount = 0.00; + $model = new \Model_Client(); + $model->loadBean(new \RedBeanPHP\OODBBean()); + + $serviceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $serviceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->will($this->returnValue($balanceAmount)); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function ($name, $sub) use($serviceMock) {return $serviceMock;}); + + $api = new \Box\Mod\Client\Api\Client(); + $api->setDi($di); + $api->setIdentity($model); + + $result = $api->balance_get_total(); + + $this->assertInternalType('float', $result); + $this->assertEquals($balanceAmount, $result); + + } } \ No newline at end of file From 9612b6cd8205f99dc212b8db07765c14fd63ac66 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 16:40:51 +0200 Subject: [PATCH 12/16] cover invoice if client has enought money in balance and activate orders in checkout --- src/bb-modules/Cart/Service.php | 11 ++++- .../bb-modules/mod_cart/Api_ClientTest.php | 45 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 931210a71..f1dc86d5f 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -566,7 +566,12 @@ class Service implements InjectionAwareInterface $invoiceService = $this->di['mod_service']('Invoice'); $invoiceModel = $invoiceService->prepareInvoice($client, array('client_id' => $client->id, 'items' => $invoice_items, 'gateway_id' => $gateway_id)); - $invoiceService->approveInvoice($invoiceModel, array('id' => $invoiceModel->id, 'use_credits' => true)); + + $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); + $balanceAmount = $clientBalanceService->getClientBalance($client); + $useCredits = $balanceAmount > $ca['total']; + + $invoiceService->approveInvoice($invoiceModel, array('id' => $invoiceModel->id, 'use_credits' => $useCredits)); if ($invoiceModel->status == \Model_Invoice::STATUS_UNPAID) { foreach ($orders as $order) { @@ -595,6 +600,10 @@ class Service implements InjectionAwareInterface if ($ca['total'] <= 0 && $product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $oa['total'] - $oa['discount'] <= 0){ $orderService->activateOrder($order); } + + if ($ca['total'] > 0 && $product->setup == \Model_ProductTable::SETUP_AFTER_PAYMENT && $invoiceModel->status == \Model_Invoice::STATUS_PAID ){ + $orderService->activateOrder($order); + } } catch (\Exception $e) { error_log($e->getMessage()); diff --git a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php index 5816e4763..b3d79402a 100644 --- a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php @@ -104,7 +104,7 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase } /** - * addon activated while order Invoice Unpaid + * checkout cart with product with addon and client has insufficient funds to cover */ public function testAddonActivatedWhileOrderInvoiceUnpaid() { @@ -136,9 +136,52 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase $this->assertInstanceOf('Model_ClientOrder', $addonOrder); $this->assertInstanceOf('Model_Invoice', $invoiceModel); + $this->assertNotNull($masterOrder->unpaid_invoice_id); $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $masterOrder->status); $this->assertEquals(Model_ClientOrder::STATUS_PENDING_SETUP, $addonOrder->status); $this->assertEquals(Model_Invoice::STATUS_UNPAID, $invoiceModel->status); } + + /** + * checkout cart with product with addon and client has sufficient funds to cover + */ + public function testOrderWithAddonCoverFromAccountBalance() + { + $this->api_guest->cart_reset(); + $data = array( + 'id' => 11, + 'period' => '6M', + 'domain' => array( + 'action' => "register", + 'owndomain_sld' =>"", + 'owndomain_tld' =>".com", + 'register_sld' =>"newdomainregister2", + 'register_tld' =>".com", + 'register_years' => "1", + ), + 'multiple' => 1, + + ); + + $this->api_admin->client_balance_add_funds(array('id' => 1, 'amount' => 100, 'description' => 'Added from PHPUnit')); + $this->api_guest->cart_add_item($data); + $this->api_client->cart_checkout(); + + $masterOrder = $this->di['db']->findOne('ClientOrder', 'group_master = 1 ORDER BY id desc'); + $addonOrder = $this->di['db']->findOne('ClientOrder', 'group_id = ? and group_master = 0', array($masterOrder->group_id)); + + $invoiceItemModel = $this->di['db']->findOne('InvoiceItem', "type = 'order' and rel_id = ?", array($masterOrder->id)); + $invoiceModel = $this->di['db']->load('Invoice', $invoiceItemModel->invoice_id); + + $this->assertInstanceOf('Model_ClientOrder', $masterOrder); + $this->assertInstanceOf('Model_ClientOrder', $addonOrder); + $this->assertInstanceOf('Model_Invoice', $invoiceModel); + + $this->assertNull($masterOrder->unpaid_invoice_id); + $this->assertEquals(Model_ClientOrder::STATUS_ACTIVE, $masterOrder->status); + $this->assertEquals(Model_ClientOrder::STATUS_ACTIVE, $addonOrder->status); + $this->assertEquals(Model_Invoice::STATUS_PAID, $invoiceModel->status); + } + } \ No newline at end of file From 88d414360d80d49fd606b082ceebca5076073c34 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 16:53:16 +0200 Subject: [PATCH 13/16] Do not show payment gateways when client has enough in account balance --- .../Orderbutton/html_client/mod_orderbutton_checkout.phtml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml b/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml index 03ac9643c..447e4d517 100644 --- a/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml +++ b/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml @@ -154,7 +154,8 @@
- {% if cart.total %} + {% set enough_in_balance = client.client_balance_get_total > cart.total %} + {% if cart.total and not enough_in_balance %}
{% for gtw in guest.invoice_gateways %} {% if cart.currency.code in gtw.accepted_currencies %} @@ -168,6 +169,9 @@ {% endif %}
+ {% if enough_in_balance %} +

{% trans 'Total amount will be deducted from account balance' %}

+ {% endif %}
From bcacd3175830f4724d6ff6d2e682d013d2fb4a5b Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 16:57:56 +0200 Subject: [PATCH 14/16] update $result array --- src/bb-modules/Cart/Service.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index f1dc86d5f..8b79483c5 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -579,10 +579,6 @@ class Service implements InjectionAwareInterface $this->di['db']->store($order); } } - - $result = array($master_order, $invoiceModel->id, $orders); - } else { - $result = array($master_order, null, $orders); } //activate orders if product is setup to be activated after order place or order total is $0 @@ -613,8 +609,11 @@ class Service implements InjectionAwareInterface } } - $result[2] = $ids; - return $result; + return array( + $master_order, + isset($invoiceModel) ? $invoiceModel->id : null, + $ids, + ); } public function usePromo(\Model_Promo $promo) From 1211851d3acc050c11c2f835a9302af739b65fa5 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 27 Jan 2015 17:30:13 +0200 Subject: [PATCH 15/16] update logical operator for balance compare. --- src/bb-modules/Cart/Service.php | 11 +++++------ .../html_client/mod_orderbutton_checkout.phtml | 2 +- tests/bb-modules/Cart/ServiceTest.php | 3 --- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 8b79483c5..84ef27301 100644 --- a/src/bb-modules/Cart/Service.php +++ b/src/bb-modules/Cart/Service.php @@ -425,7 +425,7 @@ class Service implements InjectionAwareInterface ) ); - list($order, $invoice_id, $orders) = $this->createFromCart($client, $gateway_id); + list($order, $invoice, $orders) = $this->createFromCart($client, $gateway_id); $this->rm($cart); @@ -450,9 +450,8 @@ class Service implements InjectionAwareInterface ); // invoice may not be created if total is 0 - if ($invoice_id) { - $idata = $this->di['db']->getExistingModelById('Invoice', $invoice_id, 'Invoice not found'); - $result['invoice_hash'] = $idata->hash; + if ($invoice instanceof \Model_Invoice && $invoice->status == \Model_Invoice::STATUS_UNPAID) { + $result['invoice_hash'] = $invoice->hash; } return $result; @@ -569,7 +568,7 @@ class Service implements InjectionAwareInterface $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); $balanceAmount = $clientBalanceService->getClientBalance($client); - $useCredits = $balanceAmount > $ca['total']; + $useCredits = $balanceAmount >= $ca['total']; $invoiceService->approveInvoice($invoiceModel, array('id' => $invoiceModel->id, 'use_credits' => $useCredits)); @@ -611,7 +610,7 @@ class Service implements InjectionAwareInterface return array( $master_order, - isset($invoiceModel) ? $invoiceModel->id : null, + isset($invoiceModel) ? $invoiceModel : null, $ids, ); } diff --git a/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml b/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml index 447e4d517..900e2f7cb 100644 --- a/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml +++ b/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml @@ -154,7 +154,7 @@
- {% set enough_in_balance = client.client_balance_get_total > cart.total %} + {% set enough_in_balance = client.client_balance_get_total >= cart.total %} {% if cart.total and not enough_in_balance %}
{% for gtw in guest.invoice_gateways %} diff --git a/tests/bb-modules/Cart/ServiceTest.php b/tests/bb-modules/Cart/ServiceTest.php index a3124f10a..871167b16 100644 --- a/tests/bb-modules/Cart/ServiceTest.php +++ b/tests/bb-modules/Cart/ServiceTest.php @@ -678,9 +678,6 @@ class ServiceTest extends \PHPUnit_Framework_TestCase $promo->loadBean(new \RedBeanPHP\OODBBean()); $dbMock = $this->getMockBuilder('Box_Database')->getMock(); - $dbMock->expects($this->atLeastOnce()) - ->method('getExistingModelById') - ->will($this->returnValue($invoice)); $dbMock->expects($this->atLeastOnce()) ->method('load') ->will($this->returnValue($promo)); From 6e4246ab5ac4982a8a8d3a0030cba2ff4cbcb799 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Wed, 28 Jan 2015 11:27:11 +0200 Subject: [PATCH 16/16] update checkout page in boxbilling theme do not show payment gateways if there is enough in client balance --- src/bb-themes/boxbilling/html/mod_cart_index.phtml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bb-themes/boxbilling/html/mod_cart_index.phtml b/src/bb-themes/boxbilling/html/mod_cart_index.phtml index 99eb5d628..c70322d90 100644 --- a/src/bb-themes/boxbilling/html/mod_cart_index.phtml +++ b/src/bb-themes/boxbilling/html/mod_cart_index.phtml @@ -89,7 +89,8 @@

{% trans 'Total:' %}

{{ cart.total | money_convert }}
- {% if cart.total %} + {% set enough_in_balance = client.client_balance_get_total >= cart.total %} + {% if cart.total and not enough_in_balance %}
{% for gtw in guest.invoice_gateways %} {% if cart.currency.code in gtw.accepted_currencies %} @@ -103,6 +104,9 @@ {% endif %}
+ {% if enough_in_balance %} +

{% trans 'Total amount will be deducted from account balance' %}

+ {% endif %}