diff --git a/CHANGELOG.md b/CHANGELOG.md index 0868ee482..e8c76d9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Release 4.15 +------------------------------------------------------------------------------ +* New module paidsupport + + Release 4.14 (2015-02-03) ------------------------------------------------------------------------------ diff --git a/src/bb-modules/Client/ServiceBalance.php b/src/bb-modules/Client/ServiceBalance.php index edaa62158..61f97c402 100644 --- a/src/bb-modules/Client/ServiceBalance.php +++ b/src/bb-modules/Client/ServiceBalance.php @@ -20,7 +20,7 @@ class ServiceBalance implements InjectionAwareInterface protected $di = null; /** - * @param Box_Di|null $di + * @param \Box_Di|null $di */ public function setDi($di) { @@ -28,7 +28,7 @@ class ServiceBalance implements InjectionAwareInterface } /** - * @return Box_Di|null + * @return \Box_Di|null */ public function getDi() { @@ -118,4 +118,35 @@ class ServiceBalance implements InjectionAwareInterface return array($q, $params); } + + /** + * @param \Model_Client $client + * @param double $amount + * @param string $description + * @param array $data + * @return \Model_ClientBalance + * @throws \Box_Exception + */ + public function deductFunds(\Model_Client $client, $amount, $description, array $data = null) + { + if(!is_numeric($amount)) { + throw new \Box_Exception('Funds amount is not valid'); + } + + if(strlen(trim($description)) == 0) { + throw new \Box_Exception('Funds description is not valid'); + } + + $credit = $this->di['db']->dispense('ClientBalance'); + $credit->client_id = $client->id; + $credit->type = isset($data['type']) ? $data['type'] : 'default'; + $credit->rel_id = isset($data['rel_id']) ? $data['rel_id'] : null; + $credit->description = $description; + $credit->amount = -$amount; + $credit->created_at = date('Y-m-d H:i:s'); + $credit->updated_at = date('Y-m-d H:i:s'); + $this->di['db']->store($credit); + + return $credit; + } } \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/Service.php b/src/bb-modules/Paidsupport/Service.php new file mode 100644 index 000000000..5c1745fb7 --- /dev/null +++ b/src/bb-modules/Paidsupport/Service.php @@ -0,0 +1,126 @@ +di = $di; + } + + /** + * @return mixed + */ + public function getDi() + { + return $this->di; + } + + public static function onBeforeClientOpenTicket(\Box_Event $event) + { + $di = $event->getDi(); + $params = $event->getParameters(); + + $client = $di['db']->load('Client', $params['client_id']); + + $paidSupportService = $di['mod_service']('Paidsupport'); + $paidSupportService->setDi($di); + $paidSupportService->enoughInBalanceToOpenTicket($client); + + return true; + } + + /** + * @param \Box_Event $event + * @return bool + */ + public static function onAfterClientOpenTicket(\Box_Event $event) + { + $di = $event->getDi(); + $params = $event->getParameters(); + + $supportTicket = $di['db']->load('SupportTicket', $params['id']); + $client = $di['db']->load('Client', $supportTicket->client_id); + + $paidSupportService = $di['mod_service']('Paidsupport'); + $paidSupportService->setDi($di); + $paidSupportService->enoughInBalanceToOpenTicket($client); + + $clientBalanceService = $di['mod_service']('Client', 'Balance'); + $clientBalanceService->setDi($di); + + $message = sprintf('Paid support ticket#%d "%s" opened', $supportTicket->id, $supportTicket->subject); + $extra = array( + 'rel_id' => $supportTicket->id, + 'type' => 'supportticket', + ); + $clientBalanceService->deductFunds($client, $paidSupportService->getTicketPrice(), $message, $extra); + + return true; + } + + /** + * @return float + */ + public function getTicketPrice() + { + $config = $this->di['mod_config']('Paidsupport'); + if (!isset($config['ticket_price'])){ + error_log('Paid Support ticket price is not set'); + return (float) 0; + } + return (float) $config['ticket_price']; + } + + /** + * @return string + */ + public function getErrorMessage() + { + $config = $this->di['mod_config']('Paidsupport'); + return isset($config['error_msg']) ? $config['error_msg'] : 'Configure paid support module!'; + } + + public function enoughInBalanceToOpenTicket(\Model_Client $client) + { + $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); + $clientBalance = $clientBalanceService->getClientBalance($client); + + if ($this->getTicketPrice() > $clientBalance){ + throw new \Box_Exception($this->getErrorMessage()); + } + + return true; + } + + +} \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml new file mode 100644 index 000000000..7906b7994 --- /dev/null +++ b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml @@ -0,0 +1,46 @@ +{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %} +{% import "macro_functions.phtml" as mf %} +{% block meta_title %}{% trans 'Paid support settings' %}{% endblock %} +{% set active_menu = 'system' %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} + +
+
+
{% trans 'Paid support settings' %}
+
+ + {% set params = admin.extension_config_get({"ext":"mod_paidsupport"}) %} +
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+ + + + +
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/icon.png b/src/bb-modules/Paidsupport/icon.png new file mode 100644 index 000000000..2af03dd46 Binary files /dev/null and b/src/bb-modules/Paidsupport/icon.png differ diff --git a/src/bb-modules/Paidsupport/manifest.json b/src/bb-modules/Paidsupport/manifest.json new file mode 100644 index 000000000..d743d3069 --- /dev/null +++ b/src/bb-modules/Paidsupport/manifest.json @@ -0,0 +1,12 @@ +{ + "id": "paidsupport", + "type": "mod", + "name": "Paid support module", + "description": "Module enables paid support service for your clients.", + "icon_url": "icon.png", + "homepage_url": "http://github.com/boxbilling/", + "author": "BoxBilling", + "author_url": "http://github.com/boxbilling/", + "license": "GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html", + "version": "1.0.0" +} \ No newline at end of file diff --git a/tests/bb-modules/Client/ServiceBalanceTest.php b/tests/bb-modules/Client/ServiceBalanceTest.php new file mode 100644 index 000000000..9e17ccfb0 --- /dev/null +++ b/tests/bb-modules/Client/ServiceBalanceTest.php @@ -0,0 +1,93 @@ +setDi($di); + $getDi = $service->getDi(); + $this->assertEquals($di, $getDi); + } + + public function testdeductFunds() + { + $di = new \Box_Di(); + + $clientBalance = new \Model_ClientBalance(); + $clientBalance->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('dispense') + ->with('ClientBalance') + ->willReturn($clientBalance); + $dbMock->expects($this->atLeastOnce()) + ->method('store') + ->with($clientBalance); + $di['db'] = $dbMock; + + $service = new \Box\Mod\Client\ServiceBalance(); + $service->setDi($di); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = 'Charged for product'; + $amount = 5.55; + + $extra = array( + 'rel_id' => 1, + ); + + $result = $service->deductFunds($clientModel, $amount, $description, $extra); + + $this->assertInstanceOf('\Model_ClientBalance', $result); + $this->assertEquals(-$amount, $result->amount); + $this->assertEquals($description, $result->description); + $this->assertEquals($extra['rel_id'], $result->rel_id); + $this->assertEquals('default', $result->type); + } + + public function testdeductFunds_InvalidDescription() + { + $service = new \Box\Mod\Client\ServiceBalance(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = ' '; + $amount = 5.55; + + $extra = array( + 'rel_id' => 1, + ); + + $this->setExpectedException('\Box_Exception', 'Funds description is not valid'); + $service->deductFunds($clientModel, $amount, $description, $extra); + } + + public function testdeductFunds_InvalidAmount() + { + $service = new \Box\Mod\Client\ServiceBalance(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = 'Charged'; + $amount = "5.5adadzxc"; + + $extra = array( + 'rel_id' => 1, + ); + + $this->setExpectedException('\Box_Exception', 'Funds amount is not valid'); + $service->deductFunds($clientModel, $amount, $description, $extra); + } +} + \ No newline at end of file diff --git a/tests/bb-modules/Paidsupport/ServiceTest.php b/tests/bb-modules/Paidsupport/ServiceTest.php new file mode 100644 index 000000000..fa8b04930 --- /dev/null +++ b/tests/bb-modules/Paidsupport/ServiceTest.php @@ -0,0 +1,324 @@ +service= new \Box\Mod\Paidsupport\Service(); + } + + public function testgetDi() + { + $di = new \Box_Di(); + $this->service->setDi($di); + $getDi = $this->service->getDi(); + $this->assertEquals($di, $getDi); + } + + public function testenoughInBalanceToOpenTicket() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 25; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => 2, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function test_NotenoughInBalanceToOpenTicket() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 0; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => 2, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $this->setExpectedException('\Box_Exception', $paidSupportConfig['error_msg']); + $this->service->enoughInBalanceToOpenTicket($clientModel); + } + + public function test_enoughInBalanceToOpenTicket_TicketPriceEqualsTotalAmount() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 4; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => $clientTotalAmount, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function test_enoughInBalanceToOpenTicket_TicketPriceIsNotSet() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 4; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array(); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function testonBeforeClientOpenTicket() + { + $di = new \Box_Di(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->with('Client') + ->willReturn($clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + $di['mod_service'] = $di->protect(function ($serviceName) use($paidSupportMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + }); + + $params = array( + 'client_id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onBeforeClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } + + public function testgetTicketPrice() + { + $di = new \Box_Di(); + $paidSupportConfig = array( + 'ticket_price' => 1, + ); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getTicketPrice(); + $this->assertEquals($paidSupportConfig['ticket_price'], $result); + } + + public function testgetTicketPrice_NotSet() + { + $di = new \Box_Di(); + $paidSupportConfig = array(); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getTicketPrice(); + $this->assertEquals(0, $result); + } + + public function testgetErrorMessage() + { + $di = new \Box_Di(); + $errorMessage = 'Not enough funds'; + $paidSupportConfig = array( + 'error_msg' => $errorMessage, + ); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getErrorMessage(); + $this->assertEquals($errorMessage, $result); + } + + public function testgetErrorMessage_NotSet() + { + $di = new \Box_Di(); + $errorMessage = 'Configure paid support module!'; + $paidSupportConfig = array(); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getErrorMessage(); + $this->assertEquals($errorMessage, $result); + } + + public function testonAfterClientOpenTicket() + { + $di = new \Box_Di(); + + $supportTicketModel = new \Model_SupportTicket(); + $supportTicketModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->withConsecutive(array('SupportTicket'), array('Client')) + ->willReturnOnConsecutiveCalls($supportTicketModel, $clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('deductFunds'); + + $di['mod_service'] = $di->protect(function ($serviceName, $sub ='') use($paidSupportMock, $clientBalanceMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + if ($serviceName == 'Client' && $sub == 'Balance'){ + return $clientBalanceMock; + } + }); + + $params = array( + 'id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onAfterClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } +} + \ No newline at end of file diff --git a/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php new file mode 100644 index 000000000..3199fa150 --- /dev/null +++ b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php @@ -0,0 +1,88 @@ + 'paidsupport', + 'type' => 'mod', + ); + $this->api_admin->extension_activate($data); + $ticketPrice = 5.5; + + $balance = $this->api_client->client_balance_get_total(); + + if ($balance < $ticketPrice){ + $this->api_admin->client_balance_add_funds(array('id' => 1, 'amount' => $ticketPrice, 'description' => 'Added from PHPUnit')); + } + + $beforeBalance = $this->api_client->client_balance_get_total(); + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => 'Insufficient amount in balance', + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => '1', + + 'order_id' => '1', + 'task' => 'cancel', + ); + $id = $this->api_client->support_ticket_create($data); + + $supportTicket = $this->di['db']->load('SupportTicket', $id); + $this->assertInstanceOf('\Model_SupportTicket', $supportTicket); + + $balance = $this->api_client->client_balance_get_total(); + $this->assertEquals($beforeBalance - $ticketPrice, $balance); + + $clientBalanceModel = $this->di['db']->findOne('ClientBalance', 'ORDER BY id desc'); + $this->assertEquals(-$ticketPrice, $clientBalanceModel->amount); + } + + public function testCreatePaidSupportTicket_insufficientFunds() + { + $data = array( + 'id' => 'paidsupport', + 'type' => 'mod', + ); + $this->api_admin->extension_activate($data); + + $balance = $this->api_client->client_balance_get_total(); + + $ticketPrice = 10 + $balance; + + $errorMessage = 'Insufficient amount in balance'; + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => $errorMessage, + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => '1', + + 'order_id' => '1', + 'task' => 'cancel', + ); + + $supportTickets = $this->di['db']->find('SupportTicket'); + + $this->setExpectedException('\Box_Exception', $errorMessage); + $this->api_client->support_ticket_create($data); + + $supportTicketsAfterCreate = $this->di['db']->find('SupportTicket'); + $this->assertEquals(count($supportTickets), count($supportTicketsAfterCreate)); + } + +} \ No newline at end of file