Finish paid-support-module

This commit is contained in:
Vaidas Lungis committed 2015-02-12 09:34:53 +02:00
commit 0ece8d0f96
9 files changed
+727 -2

No files matched your search

+5
View File
@@ -1,3 +1,8 @@
Release 4.15
------------------------------------------------------------------------------
* New module paidsupport
Release 4.14 (2015-02-03)
------------------------------------------------------------------------------
+33 -2
View File
@@ -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;
}
}
+126
View File
@@ -0,0 +1,126 @@
<?php
/**
* BoxBilling
*
* @copyright BoxBilling, Inc (http://www.boxbilling.com)
* @license Apache-2.0
*
* Copyright BoxBilling, Inc
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
/**
* This file is a delegate for module. Class does not extend any other class
*
* All methods provided in this example are optional, but function names are
* still reserved.
*
*/
namespace Box\Mod\Paidsupport;
use Box\InjectionAwareInterface;
class Service implements InjectionAwareInterface
{
/**
* @var \Box_Di
*/
protected $di;
/**
* @param \Box_Di $di
*/
public function setDi($di)
{
$this->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;
}
}
@@ -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 %}
<ul>
<li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
<li><a href="{{ 'system'|alink }}">{% trans 'Settings' %}</a></li>
<li class="lastB">{% trans 'Paid support settings' %}</li>
</ul>
{% endblock %}
{% block content %}
<div class="widget">
<div class="head">
<h5 class="iCog">{% trans 'Paid support settings' %}</h5>
</div>
{% set params = admin.extension_config_get({"ext":"mod_paidsupport"}) %}
<form method="post" action="{{ 'api/admin/extension/config_save'|link }}" class="mainForm api-form" data-api-msg="Settings updated">
<fieldset>
<div class="rowElem">
<label>{% trans 'Ticket price:' %}</label>
<div class="formRight">
<input type="text" name="ticket_price" value="{{ params.ticket_price }}" />
</div>
<div class="fix"></div>
</div>
<div class="rowElem">
<label>{% trans 'Error message:' %}</label>
<div class="formRight">
<input type="text" name="error_msg" value="{{ params.error_msg }}" />
</div>
<div class="fix"></div>
</div>
<input type="hidden" name="ext" value="mod_paidsupport" />
<input type="submit" value="{% trans 'Update settings' %}" class="greyishBtn submitForm" />
</fieldset>
</form>
</div>
{% endblock %}
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

+12
View File
@@ -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"
}
@@ -0,0 +1,93 @@
<?php
namespace Box\Tests\Mod\Client;
class ServiceBalanceTest extends \PHPUnit_Framework_TestCase
{
public function testgetDi()
{
$di = new \Box_Di();
$service = new \Box\Mod\Client\ServiceBalance();
$service->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);
}
}
@@ -0,0 +1,324 @@
<?php
namespace Box\Mod\Paidsupport;
class ServiceTest extends \PHPUnit_Framework_TestCase {
/**
* @var \Box\Mod\Paidsupport\Service
*/
protected $service = null;
public function setup()
{
$this->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);
}
}
@@ -0,0 +1,88 @@
<?php
class Box_Mod_Paidsupport_ServiceTest extends ApiTestCase
{
public function testCreatePaidSupportTicket()
{
$data = array(
'id' => '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));
}
}