Commit 12f033a9 authored by thejoelinux's avatar thejoelinux

work in progress

parent ddac136f
<?php
/*
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8_bin NOT NULL,
`description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_street_nr` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_complement` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`addr_town_po` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`phone` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`account_type_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_uk` (`name`)
*/
class Account extends Record {
class Account {
public $id;
public $name, $description, $addr_name;
public $addr_street_nr, $addr_complement, $addr_town_po;
public $phone, $email;
public $account_type_id;
public $table = "accounts";
public function __construct($id = 0) {
if (!$this->id) {
......@@ -30,102 +9,33 @@ class Account extends Record {
}
}
public static function fetch($id) {
if(!is_numeric($id)) {
return false;
}
// SQL SELECT accounts
$sql = "SELECT a.id, a.name, a.description, a.addr_name,
a.addr_street_nr, a.addr_complement, a.addr_town_po,
a.phone, a.email, a.account_type_id, at.name as account_type_name
FROM accounts a, account_types at
WHERE
a.account_type_id = at.id
AND a.id = ".$id;
$GLOBALS["data"]->select($sql, $account, "Account");
return $account;
}
public static function fetch_by_type($type_id) {
if(!is_numeric($type_id)) {
return false;
}
$accounts = array();
// SQL SELECT accounts
$sql = "SELECT a.id, a.name
FROM accounts a
WHERE
a.account_type_id = $type_id
ORDER BY a.name";
$GLOBALS["data"]->select($sql, $accounts, "Account", 1);
return $accounts;
// call to the compta API
public static function create_for_member($name) {
$account = Saas_Service::call("compta", "accounts", "create",
array("name"=> $name,
"account_type_id" => 41)
);
return $account->id;
}
public static function fetch_first_bank() {
// SQL SELECT accounts
$sql = " SELECT a.id, a.name, a.description, a.addr_name,
a.addr_street_nr, a.addr_complement, a.addr_town_po,
a.phone, a.email, a.account_type_id, at.name as account_type_name
FROM accounts a, account_types at
WHERE
a.account_type_id = at.id
AND at.id = 51
ORDER BY a.id
LIMIT 0,1";
$GLOBALS["data"]->select($sql, $account, "Account");
return $account;
public static function ludotheque_new_subscription($name, $date, $account_id, $amount) {
Saas_Service::call("compta", "operations", "ludotheque_new_subscription",
array("name"=> $name,
"date" => $date,
"account_id" => $account_id,
"amount" => $amount)
);
return true;
}
public function render_json() {
echo json_encode($this);
public static function ludotheque_new_payment($name, $date, $account_id, $amount, $method) {
Saas_Service::call("compta", "operations", $method,
array("name"=> $name,
"date" => $date,
"account_id" => $account_id,
"amount" => $amount)
);
return true;
}
public static function fetch_all(&$accounts) {
$accounts = array();
// SQL SELECT accounts
$sql = " SELECT a.id, a.name, a.description, a.account_type_id,
at.name as account_type_name, sum(m.amount) AS balance
FROM accounts a
LEFT OUTER JOIN account_types at ON (a.account_type_id = at.id)
LEFT OUTER JOIN moves m ON (a.id = m.account_id)
GROUP BY a.name
ORDER BY at.id, a.name";
$GLOBALS["data"]->select($sql, $accounts, "Account");
return sizeof($accounts);
}
public function count_moves() {
// SQL SELECT moves
$sql = " SELECT count(*) AS num FROM moves
WHERE account_id = ".$this->id;
$GLOBALS["data"]->select($sql, $rset);
return $rset->value("num");
}
// this has nothing to do here
// this should be a call to the compta API
public static function create_for_member($id) {
// FIXME : something like this
// create the context for posting things
// maybe you should use some component
$saas_service = Saas_Service::fetch($api_key, "compta", "account", "create");
$saas_service->configure(
array("name"=> "",
"description" => "",
"account_type_id" => 0));
return $saas_service->execute();
// SQL INSERT accounts SELECT members
$sql = " INSERT INTO accounts
(name, description, account_type_id)
SELECT CONCAT(lastname, ' ', firstname),
CONCAT(firstname, ' ', lastname, ' - ', po_town), 41
FROM members
WHERE id = ".$id;
return $GLOBALS["data"]->insert($sql);
}
}
......@@ -44,6 +44,12 @@ class data {
return mysqli_real_escape_string($this->db_handle, $string);
}
public static function log($sql) {
error_log("-------------------");
error_log(preg_replace("/\n/", " ", $sql));
error_log("-------------------");
}
public function find($query, &$rset, $object) {
$this->select($query, $rset, $object);
if(!is_object($rset)) {
......
......@@ -89,6 +89,16 @@ class Member extends Record {
return $member;
}
public static function fetch_account_from_subscription($subscription_id) {
// SQL SELECT lud_members lud_subscriptions
$sql = " SELECT m.account_id
FROM ".Member::$table." m, ".Subscription::$table." s
WHERE m.id = s.member_id
AND s.id = $subscription_id ";
$GLOBALS["data"]->select($sql, $rset);
return $rset->value("account_id");
}
public static function fetch_birthdays() {
// from http://stackoverflow.com/a/28000048/1191256 - give credit where credit is due
// SQL SELECT lud_members lud_family_members
......@@ -140,6 +150,7 @@ class Member extends Record {
// here the thing begins to be really annoying
// must have a web REST service on the compta side OR you'll end up
// duplicating all the classes for operation, moves, etc.
}
public function update_subscription() {
......
<?php
class Operation {
public $id;
public function __construct($id = 0) {
if (!$this->id) {
$this->id = $id;
}
}
// call to the compta API
public static function create_operation_for_subscription($name, $account_id,
$amount) {
$operation = Saas_Service::call("compta", "operations", "create",
array("name" => $name, "account_id" => $account_id,
"amount" => $amount), "date" => date("Y-m-d"));
return $operation->id;
}
}
......@@ -2,8 +2,9 @@
class Payment extends Record {
public $id;
public $subscription_id, $payment_method_id;
public $amount, $comment;
public $subscription_id;
public $payment_method_name, $payment_method_id;
public $amount, $comments;
public $check_bank_name, $check_owner_name;
public $created_at;
......@@ -23,22 +24,44 @@ class Payment extends Record {
public static function fetch_all(&$payments, $subscription_id) {
$payments = array();
// SQL SELECT lud_payments
$sql = "SELECT id, payment_method_id, amount, comment,
check_bank_name, check_owner_name, created_at
FROM ".Payment::$table."
WHERE subscription_id = $subscription_id
ORDER BY id";
$sql = "SELECT p.id, p.payment_method_id, pm.name AS payment_method_name,
p.amount, p.comments,
p.check_bank_name, p.check_owner_name, p.created_at
FROM ".Payment::$table." p, ".Payment_Method::$table." pm
WHERE p.subscription_id = $subscription_id
AND p.payment_method_id = pm.id
ORDER BY p.id";
$GLOBALS["data"]->select($sql, $payments, "Payment", 1);
return sizeof($payments);
}
public static function fetch($id) {
// SQL SELECT lud_payments
$sql = "SELECT id, payment_method_id, amount, comment,
$sql = "SELECT id, payment_method_id, amount, comments,
check_bank_name, check_owner_name, created_at
FROM ".Payment::$table."
WHERE id = ".$id;
$GLOBALS["data"]->select($sql, $payment, "Payment");
return $payment;
}
public static function create_from_post($subscription_id, $payment_method_id,
$comments, $amount) {
$payment = new Payment();
$payment->subscription_id = $subscription_id;
$payment->payment_method_id = $payment_method_id;
$payment->comments = $comments;
$payment->amount = $amount;
$payment->save();
return $payment;
}
public function save() {
// SQL INSERT lud_payments
$sql = " INSERT INTO ".Payment::$table." (subscription_id, payment_method_id, amount, comments)
VALUES ('".$this->subscription_id."', '".$this->payment_method_id."',
'".$this->amount."', '".$this->comments."' )";
$this->id = $GLOBALS["data"]->insert($sql);
}
}
......@@ -2,27 +2,28 @@
class Saas_Service {
public static function fetch($api_key, $software, $object, $method) {
public static function call($software, $object, $method, $args) {
$postdata = http_build_query(
array(
'o' => 'exported_services',
'a' => 'fetch',
'i' => $api_key,
'software' => $software,
'service' => $object."@".$method
'a' => $software."%".$object."%".$method,
'api_key' => $GLOBALS["saas_key"],
'args' => $args,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'header' => "Content-type: application/x-www-form-urlencoded\r\n".
"Cookie: PHPSESSID=".$_COOKIE["PHPSESSID"],
'content' => $postdata
)
);
$context = stream_context_create($opts);
error_log("Calling ".$GLOBALS["saas_api_url"]." NOW");
return json_decode(file_get_contents($GLOBALS["saas_api_url"], false, $context));
}
......
<?php
class Subscription extends Record {
public $id, $start_date, $end_date, $member_id, $member_name, $membership_type_id, $payment_method_id;
public $price, $credit, $comments, $created_at, $updated_at;
public $id, $start_date, $end_date, $member_id, $member_name, $membership_type_id, $payment_method_id;
public $price, $credit, $comments, $created_at, $updated_at;
public static $table = "lud_subscriptions";
public static $table = "lud_subscriptions";
public function __construct($id = 0)
{
if (!$this->id) {
$this->id = $id;
public function __construct($id = 0)
{
if (!$this->id) {
$this->id = $id;
}
}
}
public function text(){
public function text(){
return "Du ".
date_format(date_create_from_format('Y-m-d',$this->start_date), 'd/m/Y').
" au ".
date_format(date_create_from_format('Y-m-d',$this->end_date), 'd/m/Y');
}
}
public static function fetch($id) {
// SQL SELECT lud_subscriptions lud_membership_types lud_members
public static function fetch($id) {
// SQL SELECT lud_subscriptions lud_membership_types lud_members
$sql = " SELECT ms.id, start_date, end_date, ms.member_id, CONCAT(m.lastname, ' ', m.firstname) as member_name,
ms.membership_type_id, mt.name as membership_type_name,
ms.membership_type_id, mt.name as membership_type_name,
COUNT(p.id) AS num_payment, sum(p.amount) AS total_payment,
ms.price, credit, ms.comments, ms.created_at, ms.updated_at,
DATEDIFF(end_date, curdate()) as remaining_days
ms.price, credit, ms.comments, ms.created_at, ms.updated_at,
DATEDIFF(end_date, curdate()) as remaining_days
FROM ".Subscription::$table." ms, ".Member::$table." m, ".Membership_Type::$table." mt, ".Payment::$table." p
WHERE ms.id = ".$id."
AND ms.member_id = m.id
AND ms.membership_type_id = mt.id
AND ms.id = p.subscription_id
";
AND ms.member_id = m.id
AND ms.membership_type_id = mt.id
AND ms.id = p.subscription_id
";
$GLOBALS["data"]->select($sql, $subscription, "Subscription");
return $subscription;
}
return $subscription;
}
/*
* function fetch_all
* fetches all subscriptions for a member in an array by ref
* @return the number of subscriptions
*/
public static function fetch_all(&$subscriptions, $member_id) {
public static function fetch_all(&$subscriptions, $member_id) {
$subscriptions = array();
// SQL SELECT lud_subscriptions lud_members lud_membership_types
// SQL SELECT lud_subscriptions lud_members lud_membership_types
$sql = " SELECT ms.id, start_date, end_date, ms.member_id, CONCAT(m.lastname, ' ', m.firstname) as member_name,
ms.membership_type_id, mt.name as membership_type_name,
COUNT(p.id) AS num_payment, sum(p.amount) AS total_payment,
ms.price, credit, ms.comments, ms.created_at, ms.updated_at,
DATEDIFF(end_date, curdate()) as remaining_days
FROM ".Subscription::$table." ms, ".Member::$table." m, ".Membership_Type::$table." mt, ".Payment::$table." p
WHERE member_id = ".$member_id."
AND ms.member_id = m.id
AND ms.membership_type_id = mt.id
AND ms.id = p.subscription_id
ORDER BY end_date DESC
";
ms.price, credit, ms.comments, ms.created_at, ms.updated_at,
DATEDIFF(end_date, curdate()) as remaining_days
FROM ".Subscription::$table." ms
LEFT JOIN ".Member::$table." m ON (ms.member_id = m.id)
LEFT JOIN ".Membership_Type::$table." mt ON ( ms.membership_type_id = mt.id)
LEFT JOIN ".Payment::$table." p ON (ms.id = p.subscription_id)
WHERE ms.member_id = ".$member_id."
GROUP BY ms.id HAVING ms.id IS NOT NULL
ORDER BY end_date DESC
";
$GLOBALS["data"]->select($sql, $subscriptions, "Subscription", true);
return sizeof($subscriptions);
}
public function delete() {
// SQL SELECT lud_subscriptions
$sql = " SELECT id
FROM ".Subscription::$table."
WHERE id = ".$this->id;
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE lud_subscription
$sql = " DELETE FROM ".Subscription::$table."
WHERE id = ".$this->id;
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
public function delete() {
// SQL SELECT lud_subscriptions
$sql = " SELECT id
FROM ".Subscription::$table."
WHERE id = ".$this->id;
$GLOBALS["data"]->select($sql, $rset);
if($rset->numrows) {
// SQL DELETE lud_subscription
$sql = " DELETE FROM ".Subscription::$table."
WHERE id = ".$this->id;
$GLOBALS["data"]->delete($sql);
return $rset->value("id");
}
return false;
}
}
......@@ -273,6 +273,7 @@ class AppController {
$objects = array();
$classname = $this->model;
$classname::$method($objects);
error_log("in the method _list of app");
if($this->format == "json") {
// send dt_format=1 in URL to send format for datatables
if(array_key_exists("dt_format", $_REQUEST)) {
......
......@@ -203,6 +203,10 @@ class MembersController extends AppController {
$member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($member->id != 0) {
$member->fetch_subscriptions();
if($this->format == "json") {
echo json_encode($member->subscriptions, JSON_PRETTY_PRINT);
exit();
}
$this->set("member", $member);
return "members/subscriptions";
} else {
......@@ -220,15 +224,16 @@ class MembersController extends AppController {
$member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
if($member->id != 0) {
$_REQUEST["member_id"] = $member->id;
// the user must first have an account in the accounting part
/*
if(!$member->account_id) {
$member->account_id = Account::create_for_member($member->id);
$member->account_id = Account::create_for_member("Adhérent #".$member->id.
" - ".$member->firstname." ".$member->lastname);
$member->save_account();
}
*/
$_REQUEST["credit"] = ($_REQUEST["credit"] == "on") ? 1 : 0;
$member->create_subscription();
$_REQUEST["credit"] = (array_key_exists("credit", $_REQUEST) && $_REQUEST["credit"] == "on") ? 1 : 0;
$member->create_subscription();
Account::ludotheque_new_subscription("Adhésion #".$member->id.
" - ".$member->firstname." ".$member->lastname,
$_REQUEST["start_date"], $member->account_id, $_REQUEST["price"]);
$member->fetch_subscriptions();
$this->set("member", $member);
return "members/subscriptions";
......
<?php
class PaymentsController extends AppController {
public $model = "Payment";
function PaymentsController() {
$this->AppController();
$function_name = "_".$_REQUEST["a"];
// to the view
$this->render($this->$function_name());
}
function _create() {
try {
// FIXME : check that the sum of existing amount for this subscription
// is not greater than its price
$payment = Payment::create_from_post(
$GLOBALS["data"]->db_escape_string($_REQUEST["subscription_id"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["payment_method_id"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["comments"]),
$GLOBALS["data"]->db_escape_string($_REQUEST["amount"]));
$payment_method = Payment_Method::fetch($payment->payment_method_id);
$payment->payment_method_name = $payment_method->name;
$this->set("payment", $payment);
Account::ludotheque_new_payment(
"Paiement adhésion #".$payment->subscription_id." - ".$payment->comments,
date("Y-m-d"),
Member::fetch_account_from_subscription($payment->subscription_id),
$payment->amount, $payment_method->description);
return "payments/single_payment";
} catch (data_exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit();
}
}
}
......@@ -24,12 +24,16 @@ class SubscriptionsController extends AppController {
function _payments() {
try {
$subscription = Subscription::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
$payments = array();
Payment::fetch_all($payments, $subscription->id);
$subscription->payments = array();
Payment::fetch_all($subscription->payments, $subscription->id);
if($this->format == "json") {
echo json_encode($payments);
echo json_encode($subscription->payments, JSON_PRETTY_PRINT);
exit();
} else {
$payment_methods = array();
Payment_Method::fetch_all($payment_methods);
$this->set("methods", $payment_methods);
$this->set("object", $subscription);
return "payments/list";
}
} catch(data_exception $e) {
......
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">
<span class="glyphicon glyphicon-list-alt" style="margin-right: 10px;" ></span>
Paiement(s) de l'adhésion {{ object.id }}
<input type="hidden" id="subscription_id" value="{{ object.id }}">
</h4>
</div>
<div class="modal-body">
<div id="existing_payments">
{% for key, payment in object.payments %}
{% include 'payments/single_payment.html' %}
{% endfor %}
</div>
<div id="new_payment">
<div class="form-group">
<label class="control-label col-md-2" for="payment_method_id">Méthode</label>
<div class="col-md-3">
<select id="payment_method_id" name="payment_method_id" class="form-control">
<option value="0" selected >Methode...</option>{% for key2, val2 in methods
%}<option value="{{ val2.id }}">{{ val2.name }}</option>
{% endfor %}<select>
</div>
<label class="control-label col-md-2" for="amount">Montant</label>
<div class="col-md-3">
<input style="text-align: right" type="text" class="form-control"
size="10" name="amount"
id="amount" align="right" value=""></div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="comments">Note</label>
<div class="col-md-7">
<input type="text" class="form-control"
name="comments" id="comments" align="right" value=""></div>
<div class="col-md-1 col-md-offset-1">
<button type="button" class="save_payment_button btn btn-primary btn-md" data-id="0">
<i class="glyphicon glyphicon-floppy-save"></i>
</button>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<script>
// buttons events
$('.save_payment_button').on('click', function(){
if($('#amount').val() == '') {
alert ("Vous n'avez pas saisi de montant !");
return false;
}
// save the operation
$.post('index.php', {
o: 'payments', a: 'create', i: '',
subscription_id: $('#subscription_id').val(),
payment_method_id: $('#payment_method_id').val(),
comments: $('#comments').val(),
amount: $('#amount').val()
})
.done(function( data ) {
$('#existing_payments').append(data);
// set the new_payment fields to default values !
$('#amount').val('');
$('#comments').val('');
$("#payment_method_id").prop('selectedIndex',0);
});
return true;
});
function delete_payment(id) {
var msg = 'Voulez-vous réellement supprimer ce paiement ?';
if(confirm(msg)) {
// save the operation
$.post('api.php', {
o: 'payments', a: 'delete', i: id
})
.done(function( data ) {
$(".payment_" + id).remove();
});
return true;
}
}
</script>
<div class="form-group payment_{{ payment.id }}" id="payment_{{ payment.id }}">
<div class="col-md-2">
{{ payment.payment_method_name }}
</div>
<div class="col-md-6">
{{ payment.comments }}
</div>
<div class="col-md-2" style="text-align: right">
{{ payment.amount | number_format(2, '.', ' ') }}
</div>
<div class="col-md-2" style="text-align: left">
<button type="button" id="delete_payment_button"
class="btn btn-danger btn-sm" onClick="delete_payment({{ payment.id }})">
<i class="glyphicon glyphicon-trash"></i>
</button>
</div>
</div>
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