-
Notifications
You must be signed in to change notification settings - Fork 3
/
bills.controller.php
417 lines (350 loc) · 14.2 KB
/
bills.controller.php
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form for editing HTML block instances.
*
* @package local_shop
* @categroy local
* @author Valery Fremaux <valery.fremaux@gmail.com>
* @copyright Valery Fremaux <valery.fremaux@gmail.com> (MyLearningFactory.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_shop\backoffice;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/local/shop/classes/Bill.class.php');
require_once($CFG->dirroot.'/local/shop/classes/BillItem.class.php');
use local_shop\Bill;
use local_shop\BillItem;
class bill_controller {
protected $data;
protected $received;
protected $theshop;
protected $thecatalog;
protected $theblock;
protected $mform;
public function __construct(&$theshop, &$thecatalog, $theblock) {
$this->theshop = $theshop;
$this->thecatalog = $thecatalog;
$this->theblock = $theblock;
}
public function receive($cmd, $data = array()) {
if (!empty($data)) {
// Data is fed from outside.
$this->data = (object)$data;
$this->received = true;
return;
} else {
$this->data = new \StdClass;
}
switch ($cmd) {
case 'deletebill':
$this->data->billids = required_param_array('billid[]', PARAM_INT);
break;
case 'deletemillitems':
$this->data->items = required_param_array('billitem[]', PARAM_INT);
break;
case 'changestate':
$this->data->billid = required_param('billid', PARAM_INT);
$this->data->status = required_param('status', PARAM_TEXT);
break;
case 'ignoretax':
case 'restoretax':
case 'recalculate':
case 'generatecode':
$this->data->billid = required_param('billid', PARAM_INT);
break;
case 'relocate':
$this->data->relocated = required_param('relocated', PARAM_INT);
$this->data->z = required_param('z', PARAM_INT);
$this->data->where = required_param('at', PARAM_INT);
break;
case 'flowchange':
$this->data->billid = required_param('billid', PARAM_INT);
$this->data->status = required_param('status', PARAM_TEXT);
break;
case 'unattachall':
$this->data->billid = required_param('billid', PARAM_INT);
break;
case 'unattach':
$this->data->billid = required_param('billid', PARAM_INT);
$this->data->filepath = required_param('filepath', PARAM_INT);
$this->data->filename = required_param('filename', PARAM_INT);
break;
case 'reclettering':
$this->data->billid = required_param('billid', PARAM_INT);
$this->data->lettering = required_param('idnumber', PARAM_TEXT);
break;
case 'edit':
// Let data come from $data attributes.
break;
}
$this->received = true;
}
public function process($cmd) {
global $DB;
if (!$this->received) {
throw new \Exception('Bill Controller triggered without data');
}
$null = null;
// Delete a full bill ****************************** **.
if ($cmd == 'deletebill') {
if (!empty($this->data->billids)) {
foreach ($this->data->billids as $billid) {
try {
$bill = new Bill($billid);
$bill->delete();
} catch (\Exception $e) {
print_error('objecterror', 'local_shop', $e->message);
}
}
}
// Todo : delete filearea for the bill.
}
// Delete a set of billitems inside a bill ******************** **.
if ($cmd == 'deletebillitems') {
if (!empty($this->data->items)) {
foreach ($this->data->items as $bid) {
$billitem = new BillItem($bid);
$billitem->delete();
}
}
}
// Change bill state without triggering transition ******************** **.
if ($cmd == 'changestate') {
$bill = new Bill($this->data->billid, $null, $null, $null, true); // Get a lightweight version.
$bill->status = $this->data->status;
$bill->save();
}
if ($cmd == 'ignoretax') {
$bill = new Bill($this->data->billid); // Get a lightweight version.
$bill->ignoretax = 1;
$bill->save();
}
if ($cmd == 'restoretax') {
$bill = new Bill($this->data->billid, $null, $null, $null, true); // Get a lightweight version.
$bill->ignoretax = 0;
$bill->save();
}
if ($cmd == 'recalculate') {
$bill = new Bill($this->data->billid, $null, $null, $null, false); // Get a lightweight version.
$bill->recalculate();
}
// Generate bill code ************************* **.
if ($cmd == 'generatecode') {
$bill = new Bill($this->data->billid, $null, $null, $null, true); // Get a lightweight version.
$bill->transactionid = md5(session_id() . time());
$bill->save(true);
}
// Delete Items ************************* **.
/* Probably obsolete, calls undefined functions */
if ($cmd == 'deleteitems') {
$items = required_param('items', PARAM_INT);
$itemlist = str_replace(',', "','", $items);
// Fetches item to reorder (above the smaller deleted ordering).
$sql = "
SELECT
id, ordering as ordering
FROM
{local_shop_billitem}
WHERE
billid = '$billid' AND
ordering >= MIN($items) AND
ordering NOT IN ($items)
ORDER BY
ordering ASC
";
if ($moveditems = $DB->get_records_sql($sql)) {
$minordering = $moveditems[0]->ordering; // Catch the min.
}
// Delete other records.
$DB->delete_records_select('local_shop_billitem', " id IN ($itemlist) ");
// Reorder records.
$i = $minordering;
foreach ($moveditems as $moveditem) {
$moveditem->ordering = $i;
$DB->update_record('local_shop_billitem', $moveditem);
$i++;
}
$bill->recalculate();
}
// Relocates.
if ($cmd == 'relocate') {
/*
* Unlocks constraint
* not safe,find better algorithm
*/
$sql = "
ALTER TABLE
{local_shop_billitem}
DROP INDEX
unique_ordering
";
$DB->execute($sql);
// Relocates.
$relocated = $this->data->relocated;
$z = $thie->data->z;
$where = $this->data->where;
if ($z > $where) {
$gap = $z - $where;
for ($i = $z - 1; $i >= $where; $i--) {
moveRecord(1, $i, $this->data->billid);
}
$sql = "
UPDATE
{local_shop_billitem}
SET
ordering = ordering - $gap
WHERE
id = $relocated
";
$DB->execute($sql);
} else if ($z < $where) {
for ($i = $z + 1; $i <= $where; $i++) {
moveRecord(-1, $i, $billid);
}
$gap = $where - $z;
$sql = "
UPDATE
{local_shop_billitem}
SET
ordering = ordering + $gap
WHERE
id = '$relocated'
";
$DB->execute($sql);
}
/*
* Locks constraints back
* remove this : cannot support concurrent operations
*/
$sql = "
ALTER TABLE
{local_shop_billitem}
ADD INDEX
unique_ordering (billid, ordering)
";
$DB->execute($sql);
}
// Unattach attachements.
if ($cmd == 'unattachall') {
$fs = get_file_storage();
$context = context_system::instance();
$fs->delete_area_files($context->id, 'local_shop', 'billattachments', $this->data->billid);
}
// Unattach attachements.
if ($cmd == 'unattach') {
$fs = get_file_storage();
$context = context_system::instance();
$fs->delete_area_files($context->id, 'local_shop', 'billattachments', $this->data->billid,
$this->data->filepath, $this->data->filename);
}
// Registers accountance lettering **************************************.
if ($cmd == 'reclettering') {
if ($billrec = $DB->get_record('local_shop_bill', array('idnumber' => $this->data->lettering))) {
if ($billrec->id != $this->data->billid) {
$params = array('view' => 'viewBill', 'billid' => $billrec->id);
$badbillurl = new \moodle_url('/local/shop/bills/view.php', $params);
$errorline = get_string('uniqueletteringfailure', 'local_shop', $badbillurl);
return '<div class="bill_error">'.$errorline.'</div>';
}
$bill = new Bill($billrec);
} else {
$bill = new Bill($this->data->billid);
}
$bill->idnumber = $this->data->lettering;
$bill->save(true); // Light save.
}
// Change bill state and triggers transition ******************** **.
if ($cmd == 'flowchange') {
/*
* this implements a statefull automaton on bills
* trigers a state change handler if needed.
* Typical resolution is a manual SOLDOUT order
* for realizing production action when payed out.
*/
$bill = new Bill($this->data->billid);
$bill->work($this->data->status);
}
if ($cmd == 'edit') {
$billrec = $this->data;
if (!empty($billrec->billid)) {
$bill = new Bill($billrec->billid);
$bill->lastactiondate = $now;
} else {
$bill = new Bill(null, false, $this->theshop, $this->thecatalog, $this->theblock);
}
if (empty($billrec->currency)) {
$billrec->currency = $theshop->defaultcurrency;
}
$shipping = new \StdClass;
if (!empty($config->useshipping)) {
$shipping->value = 0;
// TODO : Call shipping calculation.
} else {
$shipping->value = 0;
}
// Creating a customer account for a user if missing.
if ($billrec->useraccountid != 0) {
$user = $DB->get_record('user', array('id' => $billrec->useraccountid));
if (!$potcustomers = $DB->get_records('local_shop_customer', array('hasaccount' => $user->id))) {
$customer = new Customer(null);
$customer->firstname = $user->firstname;
$customer->lastname = $user->lastname;
$customer->email = $user->email;
$customer->address = $user->address;
$customer->city = $user->city;
$customer->zip = '';
$customer->country = $user->country;
$customer->hasaccount = $user->id;
$customer->save();
$billrec->customerid = $customer->id; // Will be transfered to bill a bit later.
}
} else {
$bill->customerid = $billrec->userid;
}
unset($bill->userid);
unset($bill->useraccountid);
// Transfer all billrec attributes to the bill object.
foreach ($billrec as $key => $value) {
$bill->$key = $value;
}
$lastordering = Bill::last_ordering($this->theshop->id);
$bill->lastordering = $lastordering + 1;
$bill->save();
return $bill;
}
if ($cmd == 'edititem') {
/*
* TODO : change form and add a way to select a product in catalog,
* in which case the billitem type is BILLING.
*/
$billitemrec = $this->data;
$billitemrec->type = 'MANUAL';
if (empty($billitemrec->billitemid)) {
$bill = new Bill($billitemrec->billid, false, $this->theshop);
$bill->add_item_data($billitemrec, -1);
} else {
$billitem = new BillItem($billitemrec->billitemid);
unset($billitemrec->id);
unset($billitemrec->billitemid);
foreach ($billitemrec as $k => $v) {
$billitem->$k = $v;
}
$billitem->save();
}
}
}
}