From cbcb3677f533ee975b61c3d7daaa17f6f3cb36a7 Mon Sep 17 00:00:00 2001 From: 4Giedrius <4giedrius@gmail.com> Date: Wed, 18 Feb 2015 14:37:08 +0200 Subject: [PATCH] Tax rules update implemented --- src/bb-modules/Invoice/Api/Admin.php | 50 +++++++++++++++++- src/bb-modules/Invoice/Controller/Admin.php | 15 ++++-- src/bb-modules/Invoice/ServiceTax.php | 14 +++++ .../admin_default/html/mod_invoice_tax.phtml | 3 +- .../html/mod_invoice_taxupdate.phtml | 51 +++++++++++++++++++ 5 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 src/bb-themes/admin_default/html/mod_invoice_taxupdate.phtml diff --git a/src/bb-modules/Invoice/Api/Admin.php b/src/bb-modules/Invoice/Api/Admin.php index b06d7073b..4b6b84c9d 100644 --- a/src/bb-modules/Invoice/Api/Admin.php +++ b/src/bb-modules/Invoice/Api/Admin.php @@ -807,7 +807,7 @@ class Admin extends \Api_Abstract * @param int $id - tax id * * @return boolean - * @throws Box_Exception + * @throws \Box_Exception */ public function tax_delete($data) { @@ -825,7 +825,7 @@ class Admin extends \Api_Abstract * Create new tax rule * * @param string $name - tax name - * @param flaot $taxrate - tax rate + * @param float $taxrate - tax rate * * @return int - new tax id */ @@ -841,6 +841,52 @@ class Admin extends \Api_Abstract return $taxService->create($data); } + /** + * Update tax rule + * + * @param string $id - tax ID + * + * @return array + */ + public function tax_get($data) + { + $required = array( + 'id' => 'Tax id is missing' + ); + + $this->di['validator']->checkRequiredParamsForArray($required, $data); + + $tax = $this->di['db']->getExistingModelById('Tax', $data['id'], 'Tax rule not found'); + + $taxService = $this->di['mod_service']('Invoice', 'Tax'); + return $taxService->toApiArray($tax); + } + + /** + * Update tax rule + * + * @param int $id - tax ID + * @param string $name - tax name + * @param float $taxrate - tax rate + * + * @return boolean + */ + public function tax_update($data) + { + $required = array( + 'id' => 'Tax id is missing', + 'taxrate' => 'Tax rate is missing', + 'name' => 'Tax name is missing' + ); + + $tax = $this->di['db']->getExistingModelById('Tax', $data['id'], 'Tax rule not found'); + + $this->di['validator']->checkRequiredParamsForArray($required, $data); + $taxService = $this->di['mod_service']('Invoice', 'Tax'); + + return $taxService->update($tax, $data); + } + /** * Get list of taxes * diff --git a/src/bb-modules/Invoice/Controller/Admin.php b/src/bb-modules/Invoice/Controller/Admin.php index 2d01161b4..d073c46a2 100644 --- a/src/bb-modules/Invoice/Controller/Admin.php +++ b/src/bb-modules/Invoice/Controller/Admin.php @@ -96,7 +96,7 @@ class Admin implements \Box\InjectionAwareInterface ), ); } - + public function register(\Box_App &$app) { $app->get('/invoice', 'get_index', array(), get_class($this)); @@ -107,15 +107,24 @@ class Admin implements \Box\InjectionAwareInterface $app->get('/invoice/manage/:id','get_invoice', array('id'=>'[0-9]+'), get_class($this)); $app->get('/invoice/transaction/:id','get_transaction', array('id'=>'[0-9]+'), get_class($this)); $app->get('/invoice/subscription/:id','get_subscription', array('id'=>'[0-9]+'), get_class($this)); - $app->get('/invoice/tax', 'get_tax', array(), get_class($this)); + $app->get('/invoice/tax', 'get_taxes', array(), get_class($this)); + $app->get('/invoice/tax/:id', 'get_tax', array(), get_class($this)); $app->get('/invoice/pdf/:hash', 'get_pdf', array('hash'=>'[a-z0-9]+'), get_class($this)); } - public function get_tax(\Box_App $app) + public function get_taxes(\Box_App $app) { $api = $app->getApiAdmin(); return $app->render('mod_invoice_tax'); } + + public function get_tax(\Box_App $app, $id) + { + $api = $app->getApiAdmin(); + $tax = $api->invoice_tax_get(array('id' => $id)); + + return $app->render('mod_invoice_taxupdate', array('tax' => $tax)); + } public function get_index(\Box_App $app) { diff --git a/src/bb-modules/Invoice/ServiceTax.php b/src/bb-modules/Invoice/ServiceTax.php index ba166e3e2..433789d45 100644 --- a/src/bb-modules/Invoice/ServiceTax.php +++ b/src/bb-modules/Invoice/ServiceTax.php @@ -108,6 +108,20 @@ class ServiceTax implements InjectionAwareInterface return $newId ; } + public function update(\Model_Tax $model, array $data) + { + $model->name = $data['name']; + $model->country = (!isset($data['country']) || empty($data['country'])) ? NULL : $data['country']; + $model->state = (!isset($data['state']) || empty($data['state'])) ? NULL : $data['state']; + $model->taxrate = $data['taxrate']; + $model->created_at = date('c'); + $model->updated_at = date('c'); + $this->di['db']->store($model); + + $this->di['logger']->info('Created new tax rule %s', $model->name); + return true ; + } + public function getSearchQuery($data) { $sql = 'SELECT * diff --git a/src/bb-themes/admin_default/html/mod_invoice_tax.phtml b/src/bb-themes/admin_default/html/mod_invoice_tax.phtml index 69c0cb83a..6bd1ecbfa 100644 --- a/src/bb-themes/admin_default/html/mod_invoice_tax.phtml +++ b/src/bb-themes/admin_default/html/mod_invoice_tax.phtml @@ -47,7 +47,8 @@ {% endif %} {{ tax.taxrate }}% - + + diff --git a/src/bb-themes/admin_default/html/mod_invoice_taxupdate.phtml b/src/bb-themes/admin_default/html/mod_invoice_taxupdate.phtml new file mode 100644 index 000000000..af0c3c6ca --- /dev/null +++ b/src/bb-themes/admin_default/html/mod_invoice_taxupdate.phtml @@ -0,0 +1,51 @@ +{% extends "layout_default.phtml" %} +{% block meta_title %}Tax{% endblock %} +{% set active_menu = 'system' %} + +{% block content %} +
+
Update tax rule
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+ % +
+
+
+
+ +
+ {{ mf.selectbox('country', guest.system_countries, tax.country, 0, 'Apply to all countries') }} +
+
+
+
+ +
+ {# mf.selectbox('state', guest.system_states, tax.state, 0, 'Apply to all states') #} + +
+
+
+ + +
+
+
+
+ +{% endblock %}