Commit 6a19f5cf authored by thejoelinux's avatar thejoelinux

annulation, verification montant du paiement

parent 9e23c458
......@@ -24,7 +24,7 @@ class Payment extends Record {
public static function fetch_all(&$payments, $subscription_id) {
$payments = array();
// SQL SELECT lud_payments
$sql = "SELECT p.id, p.payment_method_name,
$sql = "SELECT p.id, p.subscription_id, p.payment_method_name,
p.amount, p.comments,
p.check_bank_name, p.check_owner_name, p.created_at
FROM ".Payment::$table." p
......@@ -36,7 +36,7 @@ class Payment extends Record {
public static function fetch($id) {
// SQL SELECT lud_payments
$sql = "SELECT id, payment_method_name, amount, comments,
$sql = "SELECT id, subscription_id, payment_method_name, amount, comments,
check_bank_name, check_owner_name, created_at
FROM ".Payment::$table."
WHERE id = ".$id;
......@@ -47,6 +47,18 @@ class Payment extends Record {
public static function create_from_post($subscription_id, $payment_method_name,
$comments, $amount) {
$payment = new Payment();
// SQL SELECT lud_payments lud_subscriptions
$sql = " SELECT s.price - sum(p.amount) AS amount_left
FROM ".Subscription::$table." s
LEFT JOIN ".Payment::$table." p ON p.subscription_id = s.id
WHERE s.id = ".$subscription_id;
$GLOBALS["data"]->select($sql, $rset);
if($amount > $rset->value("amount_left")) {
$payment->error = "La somme des paiements est supérieure au prix de l'adhésion";
return $payment;
}
$payment = new Payment();
$payment->subscription_id = $subscription_id;
$payment->payment_method_name = $payment_method_name;
......
......@@ -5,8 +5,13 @@ class Payment_Method {
// call to the compta API
public static function fetch_all() {
return Saas_Service::call("compta", "payment_methods", "list",
return Saas_Service::call("compta", "payment_methods", "list",
array());
}
public static function fetch_by_name($i) {
return Saas_Service::call("compta", "payment_methods", "by_name",
array("i" => $i));
}
}
......@@ -20,8 +20,15 @@ class PaymentsController extends AppController {
$GLOBALS["data"]->db_escape_string($_REQUEST["payment_method_name"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["comments"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["amount"]));
if(isset($payment->error)) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
header("Content-Type: application/json");
echo json_encode($payment);
exit();
}
$payment_method = Payment_Method::fetch_by_name($payment->payment_method_name);
$payment->description = $payment_method->name;
$this->set("payment", $payment);
Account::ludotheque_new_payment(
"Paiement adhésion #".$payment->subscription_id." - ".$payment->comments,
date("Y-m-d"),
......@@ -35,4 +42,23 @@ class PaymentsController extends AppController {
}
}
// API call
function _delete() {
try {
$payment = Payment::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
Account::ludotheque_new_payment(
"Annulation paiement adhésion #".$payment->subscription_id." - ".$payment->comments,
date("Y-m-d"),
Member::fetch_account_from_subscription($payment->subscription_id),
0 - $payment->amount, $payment->payment_method_name);
$payment->delete();
echo json_encode($payment);
exit();
} catch (data_exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
}
}
}
......@@ -23,28 +23,6 @@
{% for key2, val2 in methods %}<option value="{{ val2.method }}">{{ val2.name }}</option>
{% endfor %}
<select>
<!--
<select id="payment_method_name" name="payment_method_name" class="form-control">
</select>
<script>
$('#payment_method_name').html('<option value="">Loading...</option>');
// FIXME : do not call compta directly, use the saas !!!!
$.ajax({url: '/ludotm/compta/webroot/rest/payment_methods',
success: function(output) {
var html = '';
$.each(output, function(key, val){
html = html + '<option value="' + val.method + '">'
+ val.description + '</option>';
});
$('#payment_method_name').html(html);
},
error: function (xhr, ajaxOptions, thrownError) {
// well, that's weird, ok :)
$('#payment_method_name').html('<option value="">' + xhr.status + ' ' + thrownError + '</option>');
// alert(xhr.status + " " + thrownError);
}});
</script>
-->
</div>
<label class="control-label col-md-2" for="amount">Montant</label>
<div class="col-md-2">
......@@ -87,11 +65,18 @@
amount: $('#amount').val()
})
.done(function( data ) {
// FIXME : check the return value is there is something left to pay !!!
$('#existing_payments').append(data);
// set the new_payment fields to default values !
$('#amount').val('');
$('#comments').val('');
$("#payment_method_name").prop('selectedIndex',0);
})
.fail(function( data ) {
error = jQuery.parseJSON(data.responseText);
// console.table(error);
alert(error.error);
return false;
});
return true;
});
......
Markdown is supported
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