diff --git a/src/bb-modules/Cart/Service.php b/src/bb-modules/Cart/Service.php index 799259a4c..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; @@ -460,18 +459,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.'); @@ -521,7 +514,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) { @@ -568,44 +561,58 @@ 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)); - $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)); - foreach ($orders as $oid) { - $o = $this->di['db']->load('ClientOrder', $oid); - $o->unpaid_invoice_id = $invoice_id; - $this->di['db']->store($o); - } + $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); + $balanceAmount = $clientBalanceService->getClientBalance($client); + $useCredits = $balanceAmount >= $ca['total']; - $result = array($master_order, $invoice_id, $orders); - } else { - $result = array($master_order, null, $orders); - } + $invoiceService->approveInvoice($invoiceModel, array('id' => $invoiceModel->id, 'use_credits' => $useCredits)); - //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) { - - $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)) { - try { - $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 ($invoiceModel->status == \Model_Invoice::STATUS_UNPAID) { + foreach ($orders as $order) { + $order->unpaid_invoice_id = $invoiceModel->id; + $this->di['db']->store($order); } } } - return $result; + //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']); + try { + if ($product->setup == \Model_ProductTable::SETUP_AFTER_ORDER){ + $orderService->activateOrder($order); + } + + 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()); + $status = 'error'; + $notes = 'Order could not be activated after checkout due to error: ' . $e->getMessage(); + $orderService->orderStatusAdd($order, $status, $notes); + } + } + + return array( + $master_order, + isset($invoiceModel) ? $invoiceModel : null, + $ids, + ); } public function usePromo(\Model_Promo $promo) 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/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/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml b/src/bb-modules/Orderbutton/html_client/mod_orderbutton_checkout.phtml index 03ac9643c..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,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 %}
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 %}
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)); 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 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()) 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() 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..b3d79402a 100644 --- a/tests/integration/bb-modules/mod_cart/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_cart/Api_ClientTest.php @@ -102,4 +102,86 @@ class Box_Mod_Cart_Api_ClientTest extends BBDbApiTestCase $this->assertEquals(9874, $e->getCode()); } } + + /** + * checkout cart with product with addon and client has insufficient funds to cover + */ + 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->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