Commit 667340da authored by Cédric Houbart's avatar Cédric Houbart
Browse files

Reworked taxes edit (single tax rate)

parent 8f755f5c
......@@ -75,14 +75,33 @@ class TaxesService {
$pdo = PDOBuilder::getPDO();
$id = md5(time() . rand());
$stmt = $pdo->prepare('INSERT INTO TAXCATEGORIES (ID, NAME) VALUES '
. '(:id, :name)');
return $stmt->execute(array(':id' => $id, ':name' => $cat->label));
. '(:id, :name)');
$stmt->bindParam(':id', $id);
$stmt->bindParam(':name', $cat->label);
if (!$stmt->execute()) {
return FALSE;
} else {
return $id;
}
}
static function deleteCat($id) {
$pdo = PDOBuilder::getPDO();
$pdo->beginTransaction();
$stmtTax = $pdo->prepare("DELETE FROM TAXES WHERE CATEGORY = :id");
$stmtTax->bindParam(':id', $id);
if ($stmtTax->execute() === FALSE) {
$pdo->rollback();
return FALSE;
}
$stmt = $pdo->prepare('DELETE FROM TAXCATEGORIES WHERE ID = :id');
return $stmt->execute(array(':id' => $id));
$stmt->bindParam(':id', $id);
if ($stmt->execute() === FALSE) {
$pdo->rollback();
return FALSE;
}
$pdo->commit();
return TRUE;
}
static function getTax($id) {
......@@ -119,11 +138,17 @@ class TaxesService {
. 'CATEGORY, RATE) VALUES '
. '(:id, :name, :valid, :cat, :rate)');
$date = strftime("%Y-%m-%d %H:%M:%S", $tax->start_date);
return $stmt->execute(array(':name' => $tax->label,
':valid' => $date,
':cat' => $tax->tax_cat_id,
':rate' => $tax->rate,
':id' => $id));
$stmt->bindParam(':name', $id);
$stmt->bindParam(':valid', $date);
$stmt->bindParam(':cat', $tax->tax_cat_id);
$stmt->bindParam(':rate', $tax->rate);
$stmt->bindParam(':id', $id);
if (!$stmt->execute()) {
var_dump($stmt->errorInfo());
return FALSE;
} else {
return $id;
}
}
static function deleteTax($id) {
......
......@@ -22,39 +22,50 @@
namespace BaseProducts;
$message = NULL;
$error = NULL;
// Check saves
if (isset($_POST['name'])) {
// Tax cat
$cat = \Pasteque\TaxCat::__form($_POST);
if (isset($_POST['id'])) {
\Pasteque\TaxesService::updateCat($cat);
} else {
\Pasteque\TaxesService::createCat($cat);
if (isset($_POST['label'])) {
if (isset($_POST['label']) && isset($_POST['rate'])) {
$rate = $_POST['rate'];
if (isset($_POST['id'])) {
$cat = \Pasteque\TaxCat::__build($_POST['id'], $_POST['label']);
\Pasteque\TaxesService::updateCat($cat);
$tax = new \Pasteque\Tax($cat->id, $cat->label, time(), $rate);
if (\Pasteque\TaxesService::createTax($tax)) {
$message = \i18n("Changes saved");
} else {
$error = \i18n("Unable to save changes");
}
} else {
$cat = new \Pasteque\TaxCat($_POST['label']);
$id = \Pasteque\TaxesService::createCat($cat);
$tax = new \Pasteque\Tax($id, $cat->label, time(), $rate);
if (\Pasteque\TaxesService::createTax($tax)) {
$message = \i18n("Changes saved");
} else {
$error = \i18n("Unable to save changes");
}
}
}
} else if (isset($_POST['rate'])) {
// Tax rate
$rate = \Pasteque\Tax::__form($_POST);
if (isset($_POST['id'])) {
\Pasteque\TaxesService::updateTax($rate);
} else {
\Pasteque\TaxesService::createTax($rate);
}
} else if (isset($_POST['delete-tax'])) {
\Pasteque\TaxesService::deleteTax($_POST['delete-tax']);
} else if (isset($_POST['delete-taxcat'])) {
}
$tax_cat = NULL;
$tax = NULL;
if (isset($_GET['id'])) {
$tax_cat = \Pasteque\TaxesService::get($_GET['id']);
$tax = $tax_cat->getCurrentTax();
}
?>
<h1><?php \pi18n("Edit tax", PLUGIN_NAME); ?></h1>
<?php \Pasteque\tpl_msg_box($message, $error); ?>
<!-- Tax category edit -->
<form class="edit" action="<?php echo \Pasteque\get_current_url(); ?>" method="post">
<?php \Pasteque\form_hidden("edit", $tax_cat, "id"); ?>
<?php \Pasteque\form_input("edit", "TaxCat", $tax_cat, "label", "string", array("required" => true)); ?>
<?php \Pasteque\form_input("edit", "Tax", $tax, "rate", "float", array("required" => true)); ?>
<div class="row actions">
<?php \Pasteque\form_save(); ?>
</div>
......@@ -64,35 +75,3 @@ if (isset($_GET['id'])) {
<?php \Pasteque\form_delete("taxcat", $tax_cat->id); ?>
</form>
<?php } ?>
<?php if ($tax_cat->taxes !== NULL) { ?>
<!-- Tax rates -->
<h2><?php \pi18n("Rates", PLUGIN_NAME); ?></h2>
<?php foreach ($tax_cat->taxes as $tax) { ?>
<form class="edit" action="<?php echo \Pasteque\get_current_url(); ?>" method="post">
<?php \Pasteque\form_hidden("rate$tax->id", $tax, "id"); ?>
<?php \Pasteque\form_hidden("rate$tax->id", $tax, "tax_cat_id"); ?>
<?php \Pasteque\form_input("rate$tax->id", "Tax", $tax, "rate", "float", array("required" => true, "step" => 0.001)); ?>
<?php \Pasteque\form_input("rate$tax->id", "Tax", $tax, "start_date", "date", array("required" => true)); ?>
<div class="row actions">
<?php \Pasteque\form_save(); ?>
</div>
</form>
<form action="<?php echo \Pasteque\get_current_url(); ?>" method="post">
<?php \Pasteque\form_delete("tax", $tax->id); ?>
</form>
<?php } ?>
<?php } ?>
<!-- New rate -->
<?php if ($tax_cat !== NULL) { ?>
<h2><?php \pi18n("New tax rate", PLUGIN_NAME); ?></h2>
<form class="edit" action="<?php echo \Pasteque\get_current_url(); ?>" method="post">
<?php \Pasteque\form_value_hidden("new_rate", "tax_cat_id", $tax_cat->id); ?>
<?php \Pasteque\form_input("new_rate", "Tax", NULL, "rate", "float", array("required" => true)); ?>
<?php \Pasteque\form_input("new_rate", "Tax", NULL, "start_date", "date", array("required" => true)); ?>
<div class="row actions">
<?php \Pasteque\form_save(); ?>
</div>
</form>
<?php } ?>
......@@ -22,20 +22,29 @@
namespace BaseProducts;
$message = NULL;
$error = NULL;
if (isset($_POST['delete-taxcat'])) {
\Pasteque\TaxesService::deleteCat($_POST['delete-taxcat']);
if (\Pasteque\TaxesService::deleteCat($_POST['delete-taxcat'])) {
$message = \i18n("Changes saved");
} else {
$error = \i18n("Unable to delete tax. Tax cannot be deleted when in use.", PLUGIN_NAME);
}
}
$taxes = \Pasteque\TaxesService::getAll();
?>
<h1><?php \pi18n("Taxes", PLUGIN_NAME); ?></h1>
<?php \Pasteque\tpl_msg_box($message, $error); ?>
<p><a class="btn" href="<?php echo \Pasteque\get_module_url_action(PLUGIN_NAME, 'tax_edit'); ?>"><img src="<?php echo \Pasteque\get_template_url(); ?>img/btn_add.png" /><?php \pi18n("Add a tax", PLUGIN_NAME); ?></a></p>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php \pi18n("TaxCat.label"); ?></th>
<th><?php \pi18n("Tax.rate"); ?></th>
<th></th>
</tr>
</thead>
......@@ -47,6 +56,7 @@ $par = !$par;
?>
<tr class="row-<?php echo $par ? 'par' : 'odd'; ?>">
<td><?php echo $tax->label; ?></td>
<td><?php echo $tax->getCurrentTax()->rate; ?>
<td class="edition">
<a href="<?php echo \Pasteque\get_module_url_action(PLUGIN_NAME, 'tax_edit', array('id' => $tax->id)); ?>"><img src="<?php echo \Pasteque\get_template_url(); ?>img/edit.png" alt="<?php \pi18n('Edit'); ?>" title="<?php \pi18n('Edit'); ?>"></a>
<form action="<?php echo \Pasteque\get_current_url(); ?>" method="post"><?php \Pasteque\form_delete("taxcat", $tax->id, \Pasteque\get_template_url() . 'img/delete.png') ?></form>
......
......@@ -26,5 +26,6 @@
"Add a tax": "Add a tax",
"Edit tax": "Edit tax",
"Rates": "Rates",
"New tax rate": "New rate"
"New tax rate": "New rate",
"Unable to delete tax. Tax cannot be deleted when in use.": "Unable to delete tax. Tax cannot be deleted when in use."
}
......@@ -26,5 +26,6 @@
"Add a tax": "Ajouter une taxe",
"Edit tax": "Édition de la taxe",
"Rates": "Taux",
"New tax rate": "Nouveau taux"
"New tax rate": "Nouveau taux",
"Unable to delete tax. Tax cannot be deleted when in use.": "Impossible de supprimer la taxe. Une taxe ne peut pas être supprimée lorsqu'elle est utilisée."
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment