subscriptions.php 8.63 KB
Newer Older
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
1 2
<?php

3
class SubscriptionsController extends AppController {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
4

5
    public $model = "Subscription";
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
6

7 8 9 10 11 12 13
    function SubscriptionsController() {
        $this->AppController();
        
        $function_name = "_".$_REQUEST["a"];
        // to the view
        $this->render($this->$function_name());
    }
14 15 16

    function _new() {
        $classname = $this->model;
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
17
        $object = new $classname(0);
18 19
        $object->member_id = $_REQUEST["i"];
        $this->set("object", $object);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
20 21 22 23
        $_REQUEST["i"] = 0;
        return $_REQUEST["o"]."/edit";
    }

Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
24 25 26
    function _payments() {
        try {
            $subscription = Subscription::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
27 28
            $subscription->payments = array();
            Payment::fetch_all($subscription->payments, $subscription->id);
29 30 31 32 33 34 35 36 37 38
            $payment_methods = Payment_Method::fetch_all();
            while(list($key, $val) = each($payment_methods)) {
                $desc[$val->method] = $val->name;
            }
            reset($payment_methods);
            // complement the name by a more human readable description
            while(list($key, $val) = each($subscription->payments)) {
                $subscription->payments[$key]->description = $desc[
                    $subscription->payments[$key]->payment_method_name];
            }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
39
            if($this->format == "json") {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
40
                echo json_encode($subscription->payments, JSON_PRETTY_PRINT);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
41 42
                exit();
            } else {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
43
                $this->set("methods", $payment_methods);
44

Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
45
                $this->set("object", $subscription);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
46 47 48 49 50 51 52
                return "payments/list";
            }
        } catch(data_exception $e) {
            return "data_exception";
        }
    }

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    // API call from the list or the modal
    function _delete() {
        // $this->context["request"] = $_REQUEST;
		try {
            $member = Member::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["member_id"]));
			if($member->id != 0) {
                if(!$member->account_id) {
                    $member->account_id = Account::create_for_member("Adhérent #".$member->id.
                        " - ".$member->firstname." ".$member->lastname);
                    $member->save_account();
                }
                // check the subscription you want to delete
                // you can't delete a subscription if there are payments on it
		        $subscription = Subscription::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
                if($subscription->num_payment != 0) {
                    // do not FIXME : tell that to the user
                    AppController::http_error(422);
                    $subscription->error = "Impossible de supprimer une adhésion avec des payments associés";
                } else {
                    $subscription->delete();
73
                    Account::ludotheque_new_subscription("Annulation adhésion #".$subscription->id.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                        " - ".$member->firstname." ".$member->lastname,
                        date("Y-m-d"), $member->account_id, 0 - $subscription->price);
                }
                echo json_encode($subscription);
                exit();
            } else {
                AppController::http_error(404);
                exit();
			}
		} catch(data_exception $e) {
            AppController::http_error(500);
            exit();
		}
    }

Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    function _print() {
        try {
            $subscription = Subscription::fetch($GLOBALS["data"]->db_escape_string($_REQUEST["i"]));
            $member = Member::fetch($subscription->member_id);
        } catch(data_exception $e) {
            return "data_exception";
        }

        define('EURO', chr(128));

        
        $pdf = new FPDF();

        // the header
        $pdf->AddPage();
104
        $pdf->Image('../../ludotm_cazeres.png',10,6,60);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        $pdf->SetY(25);
        $pdf->SetFont('Arial','B',15);
        $pdf->Cell(40,10,utf8_decode('Association Ludot\'m - Cazères'));
        $pdf->SetFont('Arial','',10);
        $pdf->Ln(5);
        $pdf->Cell(40,10,utf8_decode('Impasse des Capucins'));
        $pdf->Ln(5);
        $pdf->Cell(40,10,'31220 CAZERES');
        $pdf->Ln(5);
        $pdf->Cell(40,10,'ludothequecazeres@gmail.com');
        $pdf->Ln(5);
        $pdf->Cell(40,10,'07 81 39 34 70');

        // the member name and address
        $pdf->SetXY(120,45);
        $pdf->SetFont('Arial','B', 12);
        $pdf->Cell(40, 10, utf8_decode($subscription->member_name));
        $pdf->Ln(5);
        $pdf->SetX(120);
        $pdf->SetFont('Arial','', 10);
        $pdf->Cell(40, 10, utf8_decode($member->address));
        $pdf->Ln(5);
        $pdf->SetX(120);
        $pdf->Cell(40, 10, utf8_decode($member->po_town));

        // subscription line
        $pdf->SetXY(10,85);
        $pdf->SetFont('Arial','B', 12);
        $pdf->Cell(40,10,utf8_decode('Adhésion N°'.$subscription->id));
        $pdf->Ln();
        $pdf->SetFont('Arial','', 10);
        $pdf->SetFillColor(200,200,200);
        $pdf->Cell(150,8, utf8_decode('Désignation'), 1, 0, 'L', true);
        $pdf->Cell(0,8, 'Prix', 1, 0, 'C', true);
        $pdf->Ln();
        $start_date = date_format(date_create_from_format('Y-m-d', $subscription->start_date),'d/m/Y');
        $end_date = date_format(date_create_from_format('Y-m-d', $subscription->end_date),'d/m/Y');
        $pdf->Cell(150,8, utf8_decode('Adhésion type '.$subscription->membership_type_name.' du '.
                $start_date.' au '.$end_date), 1);
        $pdf->Cell(0,8, $subscription->price." ".EURO, 1, 0, 'R');
        $pdf->Ln();

        // seal line
        $pdf->SetXY(120, 130);
        $pdf->Cell(40, 10, utf8_decode('À Cazères, le ').
            date_format(date_create_from_format('Y-m-d H:i:s', $subscription->created_at),'d/m/Y')
        );

        // member card
        $pdf->SetXY(18,221);
        $pdf->SetFillColor(245,245,189);
        $pdf->SetDrawColor(215,215,189);
        $pdf->Cell(85,50,'',1,0,'C',1);
        $pdf->Cell(85,50,'',1,0,'C',1);

        $pdf->SetXY(21,222);
161
        $pdf->Image('../../ludotm_cazeres.png',null, null, 50);
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        $pdf->SetXY(19,245);
        $pdf->SetFont('Arial','B', 12);
        $pdf->Cell(50, 2, utf8_decode($subscription->member_name),0,1);
        $pdf->SetFont('Arial','', 10);
        $pdf->SetX(19);
        $pdf->Cell(50, 10, utf8_decode($member->po_town), 0, 1);
        $pdf->SetXY(19, 263);
        $pdf->Cell(50, 6, utf8_decode('Adhésion '.$subscription->membership_type_name.' du '.
                $start_date.' au '.$end_date), 0, 1);


        $x = 106;
        $y = 225;
        $n = 3;
        $pdf->SetXY($x,$y);
        $pdf->SetFont('Arial','B', 8);
        $pdf->Cell(50, 2, utf8_decode("Périodes Scolaires"), 0, 1);
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->SetFont('Arial','', 8);
        $pdf->Cell(15, 2, "Mercredi", 0);
        $pdf->Cell(50, 2, utf8_decode("9h00 à 12h30 et 14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Jeudi", 0);
        $pdf->Cell(50, 2, utf8_decode("14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Vendredi", 0);
        $pdf->Cell(50, 2, utf8_decode("10h00 à 12h30"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Samedi", 0);
        $pdf->Cell(50, 2, utf8_decode("10h00 à 12h30 et 14h30 à 18h00"));

        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->SetFont('Arial','B', 8);
        $pdf->Cell(50, 2, utf8_decode("Vacances Scolaires"), 0, 1);
        $pdf->SetFont('Arial','', 8);
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Mardi", 0);
        $pdf->Cell(50, 2, utf8_decode("14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Mercredi", 0);
        $pdf->Cell(50, 2, utf8_decode("10h00 à 12h30 et 14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Jeudi", 0);
        $pdf->Cell(50, 2, utf8_decode("14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Vendredi", 0);
        $pdf->Cell(50, 2, utf8_decode("14h30 à 18h00"));
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(15, 2, "Samedi", 0);
        $pdf->Cell(50, 2, utf8_decode("10h00 à 12h30 et 14h30 à 18h00"));

        $pdf->SetFont('Arial','B', 8);
        $y += $n + 5;
        $pdf->SetXY($x,$y);
        $pdf->Cell(50, 2, "Contact : ludothequecazeres@gmail.com / 07 81 39 34 70");
        $y += $n;
        $pdf->SetXY($x,$y);
        $pdf->Cell(50, 2, "http://ludotheque-cazeres.fr");


        //$pdf->Output($file_name, ($_GET["a"] == "view" ? 'I' : 'D'));
        $pdf->Output("adhesion.pdf", 'I');
        exit();
235
    }
236
}