mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-11-23 13:23:00 +09:00
Finish improve-createFromCart
This commit is contained in:
commit
cf8eac837f
12 files changed
+187
-53
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -493,7 +493,7 @@ class Service implements InjectionAwareInterface
|
||||
}
|
||||
}
|
||||
|
||||
return $invoiceId;
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function setInvoiceDefaults(\Model_Invoice $model)
|
||||
|
||||
@@ -154,7 +154,8 @@
|
||||
|
||||
<form method="post" action="client/cart/checkout" class="form-horizontal" id="checkout-form" onsubmit="return false;">
|
||||
<fieldset>
|
||||
{% if cart.total %}
|
||||
{% set enough_in_balance = client.client_balance_get_total >= cart.total %}
|
||||
{% if cart.total and not enough_in_balance %}
|
||||
<div class="control-group">
|
||||
{% for gtw in guest.invoice_gateways %}
|
||||
{% if cart.currency.code in gtw.accepted_currencies %}
|
||||
@@ -168,6 +169,9 @@
|
||||
{% endif %}
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
{% if enough_in_balance %}
|
||||
<p>{% trans 'Total amount will be deducted from account balance' %}</p>
|
||||
{% endif %}
|
||||
<button class="btn btn-primary btn-large" type="submit">{% trans 'Checkout' %}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -89,7 +89,8 @@
|
||||
<p>{% trans 'Total:' %}</p>
|
||||
<div class="currency">{{ cart.total | money_convert }}</div>
|
||||
<form method="post" action="{{ 'api/client/cart/checkout'|link }}" class="api-form" data-api-jsonp="onAfterCartCheckout">
|
||||
{% if cart.total %}
|
||||
{% set enough_in_balance = client.client_balance_get_total >= cart.total %}
|
||||
{% if cart.total and not enough_in_balance %}
|
||||
<div style="text-align: left;">
|
||||
{% for gtw in guest.invoice_gateways %}
|
||||
{% if cart.currency.code in gtw.accepted_currencies %}
|
||||
@@ -103,6 +104,9 @@
|
||||
{% endif %}
|
||||
<br/>
|
||||
<button class="bb-button bb-button-submit bb-button-big" type="submit">{% trans 'Checkout' %}</button>
|
||||
{% if enough_in_balance %}
|
||||
<p>{% trans 'Total amount will be deducted from account balance' %}</p>
|
||||
{% endif %}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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()
|
||||
|
||||
Vendored
+1
@@ -76,6 +76,7 @@
|
||||
<service_downloadable />
|
||||
<service_hosting />
|
||||
<service_membership />
|
||||
<client_order_status />
|
||||
|
||||
<service_hosting id="1" client_id="1" service_hosting_hp_id="1" sld="exist" tld=".com" />
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user