-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoopCredLib.php
399 lines (341 loc) · 14.3 KB
/
CoopCredLib.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
<?php
/*******************************************************************************
Copyright 2012 Whole Foods Co-op
Copyright 2014 West End Food Co-op, Toronto
This file is part of IT CORE.
IT CORE 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 2 of the License, or
(at your option) any later version.
IT CORE 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
in the file license.txt along with IT CORE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*********************************************************************************/
/**
@class CoopCredLib
Functions for the Coop Cred plugin.
*/
class CoopCredLib extends LibraryClass {
static private $SQL_CONNECTION = null;
static public function cclibtest ()
{
return 'OK';
}
/**
Whether OK to use Coop Cred. Assign balance and availBal.
@return 1 if OK, 0 if not.
Never used, may never be and is therefore deprecated.
Originally intended for making usage more like other tenders.
At this point is not program-specific so hard to see its use.
The assignments are in getCCredSubtotals().
$tender isn't needed.
*/
static public function chargeOK($tender='')
{
global $CORE_LOCAL;
$availBal = $this->availCreditBalance + CoreLocal::get("memChargeTotal");
CoreLocal::set("balance",$this->creditBalance);
CoreLocal::set("availBal",number_format($availBal,2,'.',''));
$chargeOk = 1;
/* This check has already been done.
if ($num_rows == 0 || !$row["ChargeOk"]) {
$chargeOk = 0;
} elseif ( $row["ChargeOk"] == 0 ) {
$chargeOk = 0;
}
*/
return $chargeOk;
// chargeOK()
}
/**
OK to use the tender or make the input?
@return True if all clear to charge or input to the program
message if not
@param $pKey - a tenderType or paymentDepartment
Note: Transfer-to-another member part is not done.
*
* Knowing tenderType or paymentDepartment and memberID
* Step 1:
* - Find the program
* Step 2:
* - Is the program active?
* Step 3:
* - Is the member in it? CCredMemberships record exists.
* - Is the membership in the program active?
* x Is there enough to cover the amount?
*/
static public function programOK($pKey='', $conn='')
{
global $CORE_LOCAL;
if (!CoreLocal::get("memberID")) {
return _("Please enter the Member ID");
}
if ($conn == '') {
$conn = self::ccDataConnect();
if (!is_object($conn)){
return _("Coop Cred database connection failed:") . " {$conn}";
}
}
$pKeyType = (preg_match('/^\d+$/',$pKey)) ? 'Department' : 'Tender';
$ccpModel = new CCredProgramsModel($conn);
if ($pKeyType == 'Department') {
$ccpModel->paymentDepartment($pKey);
} else {
$ccpModel->tenderType($pKey);
}
$pCount = 0;
foreach($ccpModel->find() as $pgm) {
$pCount++;
//$limit = $cdModel->ChargeLimit();
//$prog['inputOK'] = $pgm->inputOK();
}
if ($pCount == 0) {
return _("Error:") ." {$pKeyType}" . " '{$pKey}' " .
_("is not used in any Coop Cred Program.");
}
if ($pCount > 1) {
return _("Error:") ." {$pKeyType}" . " '{$pKey}' " .
_("is used in more than one Coop Cred Program.");
}
$today = date('Y-m-d');
if ($today < $pgm->startDate()) {
return $pgm->programName() . _(" hasn't started yet.");
}
if ($today < $pgm->endDate() != '' && $today > $pgm->endDate()) {
return $pgm->programName() . _(" is no longer operating.");
}
if (!$pgm->active()) {
return $pgm->programName() . _(" is not active.");
}
if (!$pgm->creditOK()) {
return $pgm->programName() . _(" is not accepting purchases at this time.");
}
/* These are only valid globally for calculations involving the
* current tenderType or paymentDepartment.
*/
CoreLocal::set("CCredProgramID",$pgm->programID());
$programCode = "CCred{$pgm->programID()}";
CoreLocal::set("CCredProgramCode",$programCode);
/* Reset these in CoopCred::plugin_transaction_reset()
*/
CoreLocal::set("{$programCode}programID",$pgm->programID());
CoreLocal::set("{$programCode}programName",$pgm->programName());
CoreLocal::set("{$programCode}paymentDepartment",$pgm->paymentDepartment());
CoreLocal::set("{$programCode}paymentName",$pgm->paymentName());
CoreLocal::set("{$programCode}paymentKeyCap",$pgm->paymentKeyCap());
CoreLocal::set("{$programCode}tenderType",$pgm->tenderType());
CoreLocal::set("{$programCode}tenderName",$pgm->tenderName());
CoreLocal::set("{$programCode}tenderKeyCap",$pgm->tenderKeyCap());
/* Membership info.
* (At the moment members's name is not used, so a model query could be used.)
*/
$query = "SELECT m.creditBalance, m.creditLimit, m.creditOK, m.inputOK,
m.transferOK, m.isBank
,(m.creditLimit - m.creditBalance) as availCreditBalance
,c.FirstName, c.LastName
FROM CCredMemberships m
JOIN {CoreLocal::get('pDatabase')}.custdata c
ON m.cardNo = c.CardNo
WHERE m.cardNo = ? AND m.programID = ? AND c.personNum=1";
$statement = $conn->prepare($query);
if ($statement === False) {
return "Error: prepare() failed for query: $query";
}
$args=array(
(int)CoreLocal::get("memberID"),
(int)$pgm->programID()
);
$result = $conn->execute($statement,$args);
if ($result === False) {
return "Error: execute() failed for query: $query args:" .
implode('|',$args);
}
$num_rows = $conn->num_rows($result);
if ($num_rows == 0) {
return _("Member ") .
CoreLocal::get("memberID") .
_(" is not registered for ") .
CoreLocal::get("{$programCode}programName") .
".";
}
$mem = $conn->fetchRow($result);
/* Suspended or not activated for either purchasing or input.
*/
if (!$mem['creditOK']) {
return _("Member #") .
CoreLocal::get("memberID") .
_(" is registered for ") .
'<b>'. CoreLocal::get("{$programCode}programName"). '</b>' .
_(" but may not use it") .
_(" at this time") . ".";
}
/* May not put money into the program.
*/
if ($pKeyType == "Department" && !$mem['inputOK']) {
return _("Member #") .
CoreLocal::get("memberID") .
_(" may not pay into ") .
CoreLocal::get("{$programCode}programName") .
".";
}
/* May not transfer to another member.
* I can't think how this could be done at cash without a special popup.
* Transfer among one's own accounts: input to one, tender from another.
* Test would be at point of tender and need a scan of localtemptrans
* for CoopCredDepartments, $CoopCredDepartmentsUsed.
* "Payments" better, "Inputs" even better.
* AND trans_status in ('','0') - not cancel, void, refund - what would that be?
*/
$isTransfer = 0;
if (!$mem['transferOK'] && $isTransfer) {
return _("Member #") .
CoreLocal::get("memberID") .
_(" may not transfer Coop Cred to another Member") .
".";
}
CoreLocal::set("{$programCode}availCreditBalance",$mem['availCreditBalance']);
CoreLocal::set("{$programCode}creditBalance",$mem['creditBalance']);
return True;
// programOK()
}
/**
Calculate Coop Cred-related subtotals for the named tender
in the current transaction.
@param $tender
@param $programCode
@param $table, default to localtemptrans for during-transaction,
but localtranstoday for end of transaction.
@param $ref emp-lane-trans, if needed for end of transaction
@return True or error message string.
*/
static public function getCCredSubtotals($tender='', $conn,
$programCode='', $table='', $ref='')
{
global $CORE_LOCAL;
$pc = ($programCode != '') ? $programCode : CoreLocal::get("CCredProgramCode");
//$pc = ($programCode != '') ? $programCode : CoreLocal::get("programCode");
if ($table == '') {
$table = 'localtemptrans';
$refSQL = '';
} else {
if ($ref == '') {
$ref = ReceiptLib::mostRecentReceipt();
if ($ref === false) {
return "Cannot find most recent receipt";
}
}
$refs = explode('-',$ref);
$refSQL =
' AND emp_no =' . $refs[0] .
' AND register_no =' . $refs[1] .
' AND trans_no =' . $refs[2];
}
$subsQ = "SELECT
SUM(CASE
WHEN trans_subtype = '{$tender}'
THEN total
ELSE 0 END) AS chargeTotal,
SUM(CASE
WHEN department=" . CoreLocal::get("{$pc}paymentDepartment") .
" THEN total
ELSE 0 END) as paymentTotal
FROM " . CoreLocal::get("tDatabase") . ".$table
WHERE trans_type <> 'L'{$refSQL}";
$subsR = $conn->query("$subsQ");
if ($subsR === False) {
return "Error: query() failed for query: $subsQ";
} else {
$row = $conn->fetchRow($subsR);
CoreLocal::set("{$pc}chargeTotal",
(!$row || !isset($row['chargeTotal']))
? 0 : (double)$row["chargeTotal"] );
CoreLocal::set("{$pc}paymentTotal",
(!$row || !isset($row['paymentTotal']))
? 0 : (double)$row["paymentTotal"] );
CoreLocal::set("{$pc}memChargeTotal",
CoreLocal::get("{$pc}chargeTotal") +
CoreLocal::get("{$pc}paymentTotal") );
}
$availBal = ((CoreLocal::get("{$pc}availCreditBalance") == '')
? 0 : CoreLocal::get("{$pc}availCreditBalance")) +
CoreLocal::get("{$pc}memChargeTotal");
CoreLocal::set("{$pc}availBal",number_format($availBal,2,'.',''));
CoreLocal::set("{$pc}balance",
((CoreLocal::get("{$pc}creditBalance") == '')
? 0 : CoreLocal::get("{$pc}creditBalance"))
);
return True;
// getCCredSubtotals()
}
/**
Connect to the coop cred database (local)
@return a SQLManager object
*/
static public function ccDataConnect()
{
global $CORE_LOCAL;
if (self::$SQL_CONNECTION === null){
/**
Create the connection object and add all local databases to it.
*/
self::$SQL_CONNECTION = new \COREPOS\pos\lib\SQLManager(CoreLocal::get("localhost"),
CoreLocal::get("DBMS"),
CoreLocal::get("tDatabase"),
CoreLocal::get("localUser"),CoreLocal::get("localPass"),
False);
self::$SQL_CONNECTION->db_types[CoreLocal::get('pDatabase')] =
strtoupper(CoreLocal::get('DBMS'));
if (isset(self::$SQL_CONNECTION->connections[CoreLocal::get('tDatabase')])) {
self::$SQL_CONNECTION->connections[CoreLocal::get('pDatabase')] =
self::$SQL_CONNECTION->connections[CoreLocal::get('tDatabase')];
}
self::$SQL_CONNECTION->db_types[CoreLocal::get('CoopCredLaneDatabase')] =
strtoupper(CoreLocal::get('DBMS'));
if (isset(self::$SQL_CONNECTION->connections[CoreLocal::get('CoopCredLaneDatabase')])) {
self::$SQL_CONNECTION->connections[CoreLocal::get('CoopCredLaneDatabase')] =
self::$SQL_CONNECTION->connections[CoreLocal::get('tDatabase')];
}
self::$SQL_CONNECTION->query('use '.CoreLocal::get('CoopCredLaneDatabase'));
self::$SQL_CONNECTION->default_db = CoreLocal::get('CoopCredLaneDatabase');
} else {
self::$SQL_CONNECTION->query('use '.CoreLocal::get('CoopCredLaneDatabase'));
self::$SQL_CONNECTION->default_db = CoreLocal::get('CoopCredLaneDatabase');
}
return self::$SQL_CONNECTION;
// ccDataConnect()
}
/**
Add the department to the list of payment departments in the current transaction.
@return True or False
*/
static public function addDepartmentUsed($paymentDepartment=0,$programID=0)
{
global $CORE_LOCAL;
/* I think it could default to using the current ones set by programOK()
*/
if (!$paymentDepartment || !$programID) {
return False;
}
/* Add the department to those input to in this transaction.
*/
if (CoreLocal::get('CCredDepartmentsUsed') == '') {
CoreLocal::set('CCredDepartmentsUsed', array(
"$paymentDepartment" => $programID
));
} else {
// Is there a push() or append()?
$du = CoreLocal::get('CCredDepartmentsUsed');
if (!array_key_exists("$paymentDepartment", $du)) {
$du["$paymentDepartment"] = $programID;
CoreLocal::set('CCredDepartmentsUsed',$du);
}
}
return True;
// addDepartmentUsed()
}
// CoopCredLib class
}