-
Notifications
You must be signed in to change notification settings - Fork 0
/
iof_tickets.orders.inc
684 lines (583 loc) · 20.8 KB
/
iof_tickets.orders.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
161
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
// $Id$
/**
* Order handling for the IO-Festival tickets store.
*
*/
/**
* This form is used to lookup orders for a given identifier.
*
* The identifier can be one of the following:
* - A student number.
* - An i.d-card number.
* - A campus card number.
*/
function iof_tickets_customer_lookup_form(&$form_state, $customer = NULL, $inline = FALSE) {
$form = array(
'lookup' => array(
'#type' => 'fieldset',
'#title' => t('Find customer'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'scan' => array(
'#title' => t('Identifier'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => ($customer->cardnumber !== NULL) ? $customer->cardnumber : $customer->studentnumber,
),
'submit' => array(
'#value' => t('Track orders'),
'#type' => 'submit',
),
)
);
return ($inline) ? $form : $form['lookup'];
}
/**
* Validate the identifier that was given and determine the type.
* Then proceed to lookup possible connected user information.
*
* If no system user is attached to the identifier, set uid to 0.
* Store the known identifier.
*/
function iof_tickets_customer_lookup_form_validate($form, &$form_state) {
$cardnumber = $studentnumber = NULL;
// Parse scan input. Can be eithe campus card number or student number
// i.d card scan: 36708XXXXXXXx
if (preg_match('#^36708([0-9]{7})[0-9]$#', $form_state['values']['scan'], $matches)) {
$studentnumber = $matches[1];
}
// campus card scan: xxxxxXXXXXXXXX
elseif (preg_match('#[0-9]*([0-9]{9})$#', $form_state['values']['scan'], $matches)) {
$cardnumber = $matches[1];
}
// student number, direct input
elseif (preg_match('#([0-9]{7})#', $form_state['values']['scan'])) {
$studentnumber = $form_state['values']['scan'];
}
// campus card number, direct input
elseif (preg_match('#([0-9]{9})#', $form_state['values']['scan'])) {
$cardnumber = $form_state['values']['scan'];
}
else {
form_set_error('scan', 'The student number entered could not be parsed. Please check your input');
return;
}
if ($studentnumber !== NULL) {
$result = db_query('SELECT uid FROM {svid_leden} WHERE studentnumber = %d', $studentnumber);
if (($form_state['values']['uid'] = db_result($result)) === FALSE) {
form_set_error('scan', t('The student number entered could not be matched to a member. Please use the campus card instead.'));
}
}
else {
$form_state['values']['uid'] = variable_get('aselect_fallback_uid', 0);
$form_state['values']['cardnumber'] = $cardnumber;
}
}
function iof_tickets_customer_lookup_form_submit($form, &$form_state) {
if ($form_state['values']['uid'] === variable_get('aselect_fallback_uid', 0)) {
drupal_goto(sprintf('admin/store/iof-tickets/view/%d/%d', $form_state['values']['uid'], $form_state['values']['cardnumber']));
}
else {
drupal_goto('admin/store/iof-tickets/view/' . $form_state['values']['uid']);
}
}
/**
* Retreive the orders for the given user.
*
* Also provide a form to add a custom order to the given user.
*/
function iof_tickets_customer_lookup_form_results($customer) {
$output = drupal_get_form('iof_tickets_customer_lookup_form', $customer, TRUE);
$output .= drupal_get_form('iof_tickets_order_list', $customer);
$output .= drupal_get_form('iof_tickets_order_form', $customer);
return $output;
}
function theme_iof_tickets_order_create_fieldset($form) {
$fields = array('default_ticket', 'intro_ticket', 'tokens');
$header = array(
t('Product'),
t('Price'),
t('Count'),
);
$rows = array();
foreach ($fields as $field) {
$rows[] = array(
drupal_render($form[$field]['title']),
drupal_render($form[$field]['price']),
drupal_render($form[$field][$field.'_qty']),
);
}
$output = theme_table($header, $rows);
$output .= drupal_render($form);
return $output;
}
function iof_tickets_order_form(&$form_state, $customer, $inline = TRUE) {
$default_ticket_nid = variable_get('iof_tickets_default_ticket_nid', 0);
$intro_ticket_nid = variable_get('iof_tickets_intro_ticket_nid', 0);
$tokens_nid = variable_get('iof_tickets_tokens_nid', 0);
if ($default_ticket_nid === 0 || $intro_ticket_nid == 0 || $tokens_nid === 0) {
drupal_set_message(t('You need to <a href="@config-link">configure</a> this module before using this functionality.', array('@config-link' => url('admin/store/settings/iof-tickets'))), 'warning');
return array();
}
$default_ticket = node_load($default_ticket_nid);
$intro_ticket = node_load($intro_ticket_nid);
$tokens = node_load($tokens_nid);
$fieldset = array();
$fieldset = array(
'customer' => array(
'#type' => 'value',
'#value' => $customer,
),
);
/**
* If uc_price_per_role module is available, run the following code.
*
* This code is an adaptation of uc_price_per_role_find_price(), fitted
* for use with a given customer, instead of the current user.
*/
if (module_exists('uc_price_per_role')) {
$prices = $default_ticket->role_prices;
$default_ticket_price = $default_ticket->roleindependent_sell_price;
$enabled = variable_get('uc_price_per_role_enabled', array());
$weights = variable_get('uc_price_per_role_weights', array());
asort($weights);
foreach ($weights as $rid => $weight) {
if (isset($customer->roles[$rid]) && $enabled[$rid] && isset($prices[$rid])) {
$default_ticket_price = $prices[$rid];
}
}
}
else {
$default_ticket_price = $default_ticket->sell_price;
}
$fieldset['default_ticket']['title']['#value'] = l(node_page_title($default_ticket), 'node/' . $default_ticket_nid);
$fieldset['default_ticket']['price']['#value'] = uc_currency_format($default_ticket_price);
$fieldset['default_ticket']['default_ticket_qty'] = array(
'#type' => 'textfield',
'#size' => 5,
);
$fieldset['intro_ticket']['title']['#value'] = l(node_page_title($intro_ticket), 'node/' . $intro_ticket_nid);
$fieldset['intro_ticket']['price']['#value'] = uc_currency_format($intro_ticket->sell_price);
$fieldset['intro_ticket']['intro_ticket_qty'] = array(
'#type' => 'textfield',
'#size' => 5,
);
$fieldset['tokens']['title']['#value'] = l(node_page_title($tokens), 'node/' . $tokens_nid);
$fieldset['tokens']['price']['#value'] = uc_currency_format($tokens->sell_price);
$fieldset['tokens']['tokens_qty'] = array(
'#type' => 'textfield',
'#size' => 5,
);
$fieldset['submit'] = array(
'#type' => 'submit',
'#value' => t('Create'),
);
$form = array(
'order' => array(
'#type' => 'fieldset',
'#title' => t('Create order'),
'#theme' => 'iof_tickets_order_create_fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
)
);
$form['order'] += $fieldset;
return ($inline) ? $form : $fieldset;
}
function iof_tickets_order_form_submit($form, &$form_state) {
$default_ticket_nid = variable_get('iof_tickets_default_ticket_nid', 0);
$intro_ticket_nid = variable_get('iof_tickets_intro_ticket_nid', 0);
$tokens_nid = variable_get('iof_tickets_tokens_nid', 0);
$default_ticket = node_load($default_ticket_nid);
$intro_ticket = node_load($intro_ticket_nid);
$tokens = node_load($tokens_nid);
$customer = $form_state['values']['customer'];
if (module_exists('uc_price_per_role')) {
$prices = $default_ticket->role_prices;
$default_ticket_price = $default_ticket->roleindependent_sell_price;
$enabled = variable_get('uc_price_per_role_enabled', array());
$weights = variable_get('uc_price_per_role_weights', array());
asort($weights);
foreach ($weights as $rid => $weight) {
if (isset($customer->roles[$rid]) && $enabled[$rid] && isset($prices[$rid])) {
$default_ticket_price = $prices[$rid];
}
}
}
else {
$default_ticket_price = $default_ticket->sell_price;
}
$order = uc_order_new($customer->uid, 'processing');
$order->payment_method = 'none';
$order->customer_cardnumber = $customer->cardnumber;
$order->delivery_method = 'pickup';
if ($customer->uid !== variable_get('aselect_fallback_uid', 0)) {
$order->delivery_first_name = $customer->firstname;
$order->delivery_last_name = $customer->lastname;
$order->delivery_street1 = $customer->address;
$order->delivery_postal_code = $customer->zipcode;
$order->delivery_city = $customer->city;
}
$order->products = array();
// Default ticket
if ($form_state['values']['default_ticket_qty'] > 0) {
$order->products[] = (object) array(
'nid' => $default_ticket_nid,
'title' => $default_ticket->title,
'model' => $default_ticket->model,
'qty' => $form_state['values']['default_ticket_qty'],
'price' => $default_ticket_price,
);
}
// Introduction ticket
if ($form_state['values']['intro_ticket_qty'] > 0) {
$order->products[] = (object) array(
'nid' => $intro_ticket_nid,
'title' => $intro_ticket->title,
'model' => $intro_ticket->model,
'qty' => $form_state['values']['intro_ticket_qty'],
'price' => $intro_ticket->sell_price,
);
}
// Tokens
if ($form_state['values']['tokens_qty'] > 0) {
$order->products[] = (object) array(
'nid' => $tokens_nid,
'title' => $tokens->title,
'model' => $tokens->model,
'qty' => $form_state['values']['tokens_qty'],
'price' => $tokens->sell_price,
);
}
uc_order_save($order);
uc_order_update_status($order->order_id, 'pickup');
drupal_set_message(t('Order created. Proceed with payment.'));
}
/**
* List of orders for a given customer.
*/
function iof_tickets_order_list(&$form_state, $customer) {
$default_ticket_nid = variable_get('iof_tickets_default_ticket_nid', 0);
$intro_ticket_nid = variable_get('iof_tickets_intro_ticket_nid', 0);
$tokens_nid = variable_get('iof_tickets_tokens_nid', 0);
if ($default_ticket_nid === 0 || $intro_ticket_nid == 0 || $tokens_nid === 0) {
drupal_set_message(t('You need to <a href="@config-link">configure</a> this module before using this functionality.', array('@config-link' => url('admin/store/settings/iof-tickets'))), 'warning');
return array();
}
$default_ticket = node_load($default_ticket_nid);
$intro_ticket = node_load($intro_ticket_nid);
$tokens = node_load($tokens_nid);
$form = array();
$container = array();
$form['customer'] = array(
'#type' => 'value',
'#value' => $customer,
);
$default_ticket_total = 0;
$intro_ticket_total = 0;
$tokens_total = 0;
$orders = array();
if (isset($customer->orders) && is_array($customer->orders)) {
foreach ($customer->orders as $order_id) {
$default_ticket_qty = $intro_ticket_qty = $tokens_qty = 0;
$order = uc_order_load($order_id);
if ($order->order_status == 'in_checkout') {
continue;
}
// Lookup product quantities.
foreach ($order->products as $product) {
switch($product->nid) {
case $default_ticket_nid:
$default_ticket_qty = $product->qty;
$default_ticket_total += $default_ticket_qty;
break;
case $intro_ticket_nid:
$intro_ticket_qty = $product->qty;
$intro_ticket_total += $intro_ticket_qty;
break;
case $tokens_nid:
$tokens_qty = $product->qty;
$tokens_total += $tokens_qty;
break;
}
}
// If order is waiting for pickup, show. Otherwise, discard.
if ($order->delivery_method != 'pickup' || $order->order_status == 'picked_up') {
continue;
}
// Place order in form.
$orders[$order_id] = '';
$form['default_ticket_qty'][$order_id] = array('#value' => $default_ticket_qty);
$form['intro_ticket_qty'][$order_id] = array('#value' => $intro_ticket_qty);
$form['tokens_qty'][$order_id] = array('#value' => $tokens_qty);
$form['order_total'][$order_id] = array('#value' => uc_currency_format($order->order_total));
$form['payment_received'][$order_id] = array('#value' => ($order->payment_method == 'none') ? t('No') : t('Yes'));
}
}
$form['order_complete'] = array(
'#type' => 'checkboxes',
'#options' => $orders,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Order picked up'),
);
$container['pending_orders'] = array(
'#type' => 'fieldset',
'#title' => t('Orders waiting for pickup'),
'#theme' => 'iof_tickets_order_list_fieldset',
'#weight' => -40,
);
$container['pending_orders'] += $form;
$container['total_orders'] = array(
'#type' => 'fieldset',
'#title' => t('Total orders from customer.'),
'#weight' => -50,
);
$container['total_orders']['table'] = array(
'#value' => theme('table',
array(
$default_ticket->title,
$intro_ticket->title,
$tokens->title,
),
array(array(
$default_ticket_total,
$intro_ticket_total,
$tokens_total,
))
),
);
return $container;
}
function iof_tickets_order_list_validate($form, &$form_state) {
$form_state['values']['order_complete'] = array_filter($form_state['values']['order_complete']);
if (count($form_state['values']['order_complete']) == 0) {
form_set_error('', t('No orders selected'));
}
}
function iof_tickets_order_list_submit($form, &$form_state) {
foreach ($form_state['values']['order_complete'] as $order_id) {
if (uc_order_update_status($order_id, 'picked_up') === FALSE) {
drupal_set_message(t('Could not update order %order_id. Status update was refused.', array('%order_id' => $order_id)), 'error');
}
}
drupal_set_message(t('Updated order queue.'));
}
/**
* Theme the list of orders for a selected customer.
*
* @param $form The unthemed Forms API data.
* @return Themed output of the order list form.
*/
function theme_iof_tickets_order_list_fieldset($form) {
$default_ticket_nid = variable_get('iof_tickets_default_ticket_nid', 0);
$intro_ticket_nid = variable_get('iof_tickets_intro_ticket_nid', 0);
$tokens_nid = variable_get('iof_tickets_tokens_nid', 0);
if ($default_ticket_nid === 0 || $intro_ticket_nid == 0 || $tokens_nid === 0) {
drupal_set_message(t('You need to <a href="@config-link">configure</a> this module before using this functionality.', array('@config-link' => url('admin/store/settings/iof-tickets'))), 'warning');
return array();
}
$default_ticket = node_load($default_ticket_nid);
$intro_ticket = node_load($intro_ticket_nid);
$tokens = node_load($tokens_nid);
$header = array(
theme('table_select_header_cell'),
$default_ticket->title,
$intro_ticket->title,
$tokens->title,
t('Total'),
t('Betaald'),
);
$rows = array();
if (isset($form['order_total']) && is_array($form['order_total'])) {
foreach(element_children($form['order_total']) as $order_id) {
$rows[] = array(
drupal_render($form['order_complete'][$order_id]),
drupal_render($form['default_ticket_qty'][$order_id]),
drupal_render($form['intro_ticket_qty'][$order_id]),
drupal_render($form['tokens_qty'][$order_id]),
drupal_render($form['order_total'][$order_id]),
drupal_render($form['payment_received'][$order_id]),
);
}
}
else {
$rows[] = array(array('data' => t('No orders found.'), 'colspan' => 6));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Ticket export form.
*
* Export a list of addresses and the orders connected to them.
*/
function iof_tickets_order_export_form() {
$form = array();
$form['export_type'] = array(
'#title' => t('Scope'),
'#type' => 'radios',
'#options' => array(
'all' => t('All orders'),
'online' => t('Orders purchased online'),
'mail' => t('Shippable orders'),
'picking' => t('Orders waiting to be shipped'),
'mailed' => t('Shipped orders'),
),
);
$form['update_status'] = array(
'#title' => t('Set exported orders to shipped.'),
'#type' => 'checkbox',
'#default_value' => FALSE,
'#description' => t('If set, exported orders that are marked as waiting to be shipped will be updated to shipped orders.'),
);
$form['export'] = array(
'#type' => 'submit',
'#value' => t('Export'),
);
return $form;
}
function iof_tickets_order_export_form_submit($form, &$form_state) {
if ($form_state['values']['update_status']) {
$form_state['values']['export_type'] .= '-update';
}
drupal_goto('admin/store/iof-tickets/export/' . $form_state['values']['export_type']);
}
function iof_tickets_order_export($action) {
$default_ticket_nid = variable_get('iof_tickets_default_ticket_nid', 0);
$intro_ticket_nid = variable_get('iof_tickets_intro_ticket_nid', 0);
$tokens_nid = variable_get('iof_tickets_tokens_nid', 0);
if ($default_ticket_nid === 0 || $intro_ticket_nid == 0 || $tokens_nid === 0) {
drupal_set_message(t('You need to <a href="@config-link">configure</a> this module before using this functionality.', array('@config-link' => url('admin/store/settings/iof-tickets'))), 'warning');
drupal_goto('admin/store/iof-tickets/export');
}
$default_ticket = node_load($default_ticket_nid);
$intro_ticket = node_load($intro_ticket_nid);
$tokens = node_load($tokens_nid);
$header = array(
t('Order ID'),
t('Order date'),
t('Order status'),
t('First name'),
t('Last name'),
t('Address'),
t('Postal code'),
t('City'),
t('Delivery method'),
t('Number of tickets (member)'),
t('Number of tickets (intro)'),
t('Number of tokens'),
t('Order total'),
t('Payment method'),
);
$rows = array();
$actions = explode('-', $action);
$scope = $actions[0];
$update_status = isset($actions[1]);
switch ($scope) {
case 'all':
$query = 'SELECT order_id
FROM {uc_orders}';
break;
case 'online':
$query = "SELECT order_id
FROM {uc_orders}
WHERE payment_method = 'ideal_lite_payment'";
break;
case 'mail':
$query = "SELECT order_id
FROM {uc_order_iof_tickets}
WHERE delivery_method = 'mail'";
break;
case 'picking':
$query = "SELECT o.order_id
FROM {uc_orders} o, {uc_order_iof_tickets} t
WHERE o.order_id = t.order_id
AND t.delivery_method = 'mail'
AND o.order_status = 'processing'";
break;
case 'mailed':
$query = "SELECT o.order_id
FROM {uc_orders} o, {uc_order_iof_tickets} t
WHERE o.order_id = t.order_id
AND t.delivery_method = 'mail'
AND o.order_status = 'shipped'";
break;
default:
drupal_set_message(t('Unknown scope.'), 'warning');
drupal_goto('admin/store/iof-tickets/export');
break;
}
$result = db_query($query);
if ($result === FALSE) {
drupal_set_message(t('Could not load list of order_id\'s'), 'error');
drupal_goto('admin/store/iof-tickets/export');
}
while (($order = db_fetch_object($result)) !== FALSE) {
$default_ticket_qty = $intro_ticket_qty = $tokens_qty = 0;
$order = uc_order_load($order->order_id);
// Lookup product quantities.
foreach ($order->products as $product) {
switch($product->nid) {
case $default_ticket_nid:
$default_ticket_qty += $product->qty;
break;
case $intro_ticket_nid:
$intro_ticket_qty += $product->qty;
break;
case $tokens_nid:
$tokens_qty = $product->qty;
break;
}
}
$rows[] = array(
$order->order_id,
format_date($order->created, 'small'),
$order->order_status,
// Delivery information.
$order->delivery_first_name,
$order->delivery_last_name,
$order->delivery_street1,
$order->delivery_postal_code,
$order->delivery_city,
$order->delivery_method,
// Order information
$default_ticket_qty,
$intro_ticket_qty,
$tokens_qty,
$order->order_total,
$order->payment_method,
);
if ($update_status) {
uc_order_update_status($order->order_id, 'shipped');
}
}
if (empty($rows)) {
drupal_set_message(t('Could not find orders matching the selected criteria'));
drupal_goto('admin/store/iof-tickets/export');
}
$filename = 'order-list-' . $scope . '.csv';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=' . $filename);
echo str_putcsv($header, ';');
foreach ($rows as $line) {
echo str_putcsv($line, ';');
}
exit();
}
/**
* Utility function for placing array data in CSV format.
*/
if (!function_exists('str_putcsv')) {
function str_putcsv($input, $delimiter = ',', $enclosure = '"') {
$fp = fopen('php://temp', 'w+');
fputcsv($fp, $input, $delimiter, $enclosure);
rewind($fp);
$data = fread($fp, 1048576);
fclose($fp);
return $data;
}
}