private function ajax_save_program() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['CoopCredDatabase']); $OP = $FANNIE_OP_DB . $dbc->sep(); /* Place to accumulate not-immediately-fatal messages * that will be displayed in the ajax-return-string. * Maybe better as array. */ $returnMessage = ""; /* These are from the query (parameters) string in the AJAX request * and are the same as the input form id= values. */ $configno = FormLib::get_form_value('configno', 0); $dummytender = FormLib::get_form_value('dummytender', ''); $dummydept = FormLib::get_form_value('dummydept', 0); $deptmin = FormLib::get_form_value('deptmin', 0); $deptmax = FormLib::get_form_value('deptmax', 0); $dummybanker = FormLib::get_form_value('dummybanker', 0); $bankermin = FormLib::get_form_value('bankermin', 0); $bankermax = FormLib::get_form_value('bankermax', 0); $membermin = FormLib::get_form_value('membermin', 0); $membermax = FormLib::get_form_value('membermax', 0); $isnew = FormLib::get_form_value('isnew', 9); /* Check for problems. * See CoopCredProgramEditor for examples. */ $sMessage = ""; /* Each check */ /* After all checks done. */ if ($sMessage) { $sMessage = preg_replace("/^\n+/", "", $sMessage); $sMessage .= "\n\nNo current changes have been Saved."; echo $sMessage; return; } /* Save changes to or Create the Config proper. */ $config = new CCredConfigModel($dbc); $config->configID($configno); $config->dummyTenderCode($dummytender); $config->dummyDepartment($dummydept); $config->deptMin($deptmin); $config->deptMax($deptmax); // $config->dummyBanker($dummybanker); $config->bankerMin($bankermin); $config->bankerMax($bankermax); // $config->regularMemberMin($membermin); $config->regularMemberMax($membermax); // $config->modifiedBy($this->authUserNumber); $config->modified(date('Y-m-d H:i:s')); /* save() decides UPDATE vs INSERT based on whether configID already * exists. */ $saved = $config->save(); if ($isnew == 1) { if ($saved === False) { echo 'Error: could not create Configuration'; return; } } else { if ($saved === False) { echo 'Error: could not save the changes to the Configuration'; return; } else { $returnMessage .= sprintf("\nSaved Configuration (#%d)", $configno); } } $returnMessage = preg_replace("/^\n+/", "", $returnMessage); echo $returnMessage; // ajax_save_program() }
function ShowEditForm($memNum, $country = "US", $inProgramID = -1) { global $FANNIE_URL; global $FANNIE_PLUGIN_LIST, $FANNIE_PLUGIN_SETTINGS; if (!isset($FANNIE_PLUGIN_LIST) || !in_array('CoopCred', $FANNIE_PLUGIN_LIST)) { $msg = "Problem: The '" . $this->pluginName . "' plugin is not enabled."; return $this->FormatReturnMessage($msg); } if (array_key_exists('CoopCredDatabase', $FANNIE_PLUGIN_SETTINGS) && $FANNIE_PLUGIN_SETTINGS['CoopCredDatabase'] != "") { $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['CoopCredDatabase']); } else { $msg = "Problem: Connection to the database for the '" . $this->pluginName . "' plugin failed."; return $this->FormatReturnMessage($msg); } $config = new CCredConfigModel($dbc); $config->configID(1); $loadOK = $config->load(); if (!$loadOK) { $msg = "Problem: Please 'Configure Coop Cred' from the Coop Cred Admin menu."; return $this->FormatReturnMessage($msg); } else { $this->regularMemberMin = $config->regularMemberMin(); $this->regularMemberMax = $config->regularMemberMax(); } /* Needs to get: * -all programs or * -all active programs * regardless whether member is in them * distinguish the ones he/does belong to by CAPS or colour or something * set SELECTED for the first one he/she belongs to, else Choose * and show the current balance for that * and set the default params for: * -transfer * -fix * -Activity Report * to that. */ $programID = 0; $programBankNumber = 0; /* Get all programs first, then see if the member is in them. */ $progQ = "SELECT programID, programName, bankID\n FROM CCredPrograms\n WHERE active =1\n ORDER BY programName ASC"; $progS = $dbc->prepare_statement($progQ); $progR = $dbc->exec_statement($progS, array()); $ccred_programs = array(); $pID = ''; // format: P01 $firstMpID = ''; // a $pID $programCount = 0; while ($progW = $dbc->fetch_row($progR)) { $programCount++; $pID = sprintf("P%02d", $progW['programID']); if ($programCount == 1) { $firstMpID = $pID; } $ccred_programs["{$pID}"] = array('programID' => $progW['programID'], 'programName' => $progW['programName'], 'bankID' => $progW['bankID'], 'creditOK' => 0, 'membershipID' => 0, 'balance' => NULL, 'maxCreditBalance' => 0, 'isDefault' => 0); } /* Then get balance for the ones the member belongs to. * Decide which one will be displayed, the default, * in order of preference. * 1. The first one the member belongs to where creditOK is True, * i.e. that he/she can purchase with. * 2. The first one he/she belongs to. * 3. The first one that exists. * 4. Override with parameter $inProgramID. * */ $infoQ = "SELECT m.programID, m.cardNo, m.creditOK, m.membershipID,\n m.maxCreditBalance,\n b.balance\n FROM CCredMemberships AS m\n JOIN CCredLiveBalance AS b ON m.cardNo = b.cardNo\n AND m.programID = b.programID\n JOIN CCredPrograms AS p ON m.programID = p.programID\n WHERE m.cardNo =?\n AND p.active =1"; $infoS = $dbc->prepare_statement($infoQ); $infoR = $dbc->exec_statement($infoS, array($memNum)); $membershipCount = 0; $firstCreditOK = $inProgramID > -1 ? 1 : 0; // This may not be getting a real value if Member does not belong to any yet. if ($firstMpID == '') { // Means there are no Programs $pID = sprintf("P%02d", 0); $ccred_programs["{$pID}"] = array('programID' => 0, 'programName' => "No Programs Exist", 'bankID' => 0, 'creditOK' => 0, 'membershipID' => 0, 'balance' => NULL, 'maxCreditBalance' => 0, 'isDefault' => 1); } else { $pID = $firstMpID; // format: P01 } $defaultMembership = $inProgramID > -1 ? sprintf("P%02d", $inProgramID) : $pID; $firstPID = ''; // a $pID while ($infoW = $dbc->fetch_row($infoR)) { $membershipCount++; $pID = sprintf("P%02d", $infoW['programID']); if ($membershipCount == 1) { $firstPID = $pID; } /* ?Details for programs other than the default were gathered * for when I was going to compose JS to do the program change. */ $ccred_programs["{$pID}"]['balance'] = $infoW['balance']; $ccred_programs["{$pID}"]['creditOK'] = $infoW['creditOK']; $ccred_programs["{$pID}"]['maxCreditBalance'] = $infoW['maxCreditBalance']; $ccred_programs["{$pID}"]['membershipID'] = $infoW['membershipID']; if ($inProgramID > -1) { if ($infoW['programID'] == $inProgramID) { $ccred_programs["{$pID}"]['isDefault'] = 1; $defaultMembership = $pID; $programID = $ccred_programs["{$pID}"]['programID']; $programName = $ccred_programs["{$pID}"]['programName']; $programBankNumber = $ccred_programs["{$pID}"]['bankID']; $firstCreditOK = 1; continue; } } else { if ($ccred_programs["{$pID}"]['creditOK'] && $firstCreditOK == 0) { $ccred_programs["{$pID}"]['isDefault'] = 1; $defaultMembership = $pID; $programID = $ccred_programs["{$pID}"]['programID']; $programName = $ccred_programs["{$pID}"]['programName']; $programBankNumber = $ccred_programs["{$pID}"]['bankID']; $firstCreditOK = 1; } } } /* If Member belongs to at least one but none is inputOK yet * default to the first one belonged to. */ if ($membershipCount > 0 && $firstCreditOK == 0) { $ccred_programs["{$firstPID}"]['isDefault'] = 1; $defaultMembership = $firstPID; $programID = $ccred_programs["{$firstPID}"]['programID']; $programName = $ccred_programs["{$firstPID}"]['programName']; $programBankNumber = $ccred_programs["{$firstPID}"]['bankID']; } /* If Member belongs to none $defaultMembership is still "". * The formlet can refer to Programs that Member doesn't belong to * but not to values that come from CCredMemberships. * The membership values are dummy. */ $programMemberType = $memNum == $programBankNumber ? "{$programName}" : "Member"; $ret = ""; /* Later, AJAX-driven, calls ($inProgramID > -1) re-populate this <div> * but must not write it again. * CSS is temporary until boostrap coding is done. */ if ($inProgramID == -1) { $ret .= "<style type='text/css'>\n.MemFormTable th {\n font-size: 75%;\n text-align: right;\n color: #fff;\n padding: 0 5px 0 2px;\n border: solid white 2px;\n}\n.MemFormTable td {\n padding: 2px 2px 2px 2px;\n}\n/* The interveneing ccredmemdiv prevents bootstrap's\n * gradient from appearing.\n */\n.ccredpanelhead {\n background-color: #f5f5f5;\n border-bottom: 1px solid #ddd;\n}\n</style>"; $ret .= "<div class='panel panel-default'>"; $ret .= "<div id='ccredmemdiv'>"; } $ret .= "<div class=\"panel-heading ccredpanelhead\">Coop Cred - {$programMemberType}"; //</div>"; $ret .= " <a onclick=\"\$('#_fhtt14102004').toggle();return false;\" href=''>" . "<img title='Let the Member make purchases against money in an account " . "(Click for more)' src='{$FANNIE_URL}src/img/buttons/help16.png'>" . "</a>"; $ret .= "</div><!-- /.panel-heading -->"; $ret .= "<div class=\"panel-body\">"; /* For bootstrapped v.1 retain the table coding. */ $ret .= "<table class='MemFormTable' border='0' width='100%'>"; if ($memNum != $programBankNumber) { $ret .= "<tr>"; $ret .= "<td colspan=3>"; if ($memNum >= $this->regularMemberMin && $memNum <= $this->regularMemberMax) { $ret .= "<select id='ccred_program' name='ccred_program' " . "onchange='memProgramChange();' >"; $ret .= "<option value='0'>Choose Program</option>"; foreach ($ccred_programs as $pID => $prog) { $p_sel = ($membershipCount > 0 || $inProgramID > -1) && $pID == $defaultMembership ? ' selected="1" ' : ''; $o_style = $prog['creditOK'] == 1 ? "style='font-weight:bold;'" : ""; $ret .= "<option value='{$prog['programID']}'{$p_sel}{$o_style}>" . "{$prog['programName']}</option>"; } $ret .= "</select>"; } else { $ret .= " "; } $ret .= "</td>"; $ret .= "<th title='The amount of Coop Cred the member has to use.'>Current Balance</th>"; $ret .= "<th title='The maximum Coop Cred the member may accumulate.'>Max Balance</th>"; $ret .= "</tr>"; } $ret .= "<tr>"; $ucc = $ccred_programs["{$defaultMembership}"]['creditOK'] ? ' checked="" ' : ''; $uccDisabled = $memNum == $programBankNumber ? ' disabled ' : ""; $ret .= "<th title='Allow/prevent the member's use of the selected type of Coop Cred.'>Use Coop Cred</th>"; $ret .= sprintf('<td><input type="checkbox" name="use_coop_cred" %s %s/> </td>', $ucc, $uccDisabled); // In Coop Cred $limit is always 0. $limit = 0; $ret .= "<input type='hidden' name='CC_limit' value='{$limit}'>"; if ($memNum == $programBankNumber) { $ret .= "<th title='The amount of Coop Cred the member has to use.'>Current Balance</th>"; } else { $ret .= "<td> </td>"; } $ret .= sprintf('<td id="cbal" name="cbal">%.2f</td>', $ccred_programs["{$defaultMembership}"]['balance'] * -1); if ($memNum == $programBankNumber) { $today = date('Y-m-d'); $cellContent = "<p style='margin:0em; font-family:Arial;line-height:1.0em;'>\n <a href=\"{$FANNIE_URL}{$this->pluginHome}reports/ProgramEvents/" . "ProgramEventsReport.php?date1=&date2=&card_no={$memNum}" . "&sortable=on" . "&programID={$programID}\"\n title='List inputs to and payments from the program before today'\n target='_coop_cred_events'\n >Event History</a>\n <br />\n <a href=\"{$FANNIE_URL}{$this->pluginHome}reports/ProgramEvents/" . "ProgramEventsReport.php?date1={$today}&date2={$today}" . "&sortable=on" . "&other_dates=on&submit=Submit&card_no={$memNum}" . "&programID={$programID}\"\n title='List inputs to and payments from the program today'\n target='_coop_cred_events'\n >Events Today</a>\n </p>\n "; } else { $template = '<input type="text" size=8 maxlength=10 id="maxbal" ' . 'name="maxbal" value="%.2f" >'; $cellContent = sprintf("{$template}", $ccred_programs["{$defaultMembership}"]['maxCreditBalance'] * -1); } $ret .= "<td>" . $cellContent; $ret .= "</td>"; $ret .= "</tr>"; $ret .= "<tr>"; if ($memNum == $programBankNumber) { $transferTitle = 'Move funds to a member account or return them to ' . 'the Program Account.'; } else { $transferTitle = 'Get funds for the member from, or return the ' . 'member's funds to, the Program Account or another member.'; } if ($programID == 0) { $transferTitle .= "\nAdd the member to a Program and Save before " . "doing a Transfer."; } $args1 = "memIN={$memNum}&memEDIT={$memNum}&" . "programID={$programID}"; $ret .= "<td colspan=\"2\"><a id='tlink' name='tlink'\n href=\"{$FANNIE_URL}{$this->pluginHome}membership/CoopCredTransferTool.php?{$args1}\"\n title='{$transferTitle}'\n >Transfer Coop Cred</a></td>"; $args2 = "memIN={$memNum}&memEDIT={$memNum}&" . "programID={$programID}"; $ret .= "<td><a id='flink' name='flink'\n href=\"{$FANNIE_URL}{$this->pluginHome}membership/CoopCredJiggerTool.php?{$args2}\"\n title='Fix errors and problems'\n >Fix Coop Cred</a></td>"; /* In this program only the Program Account may accept inputs. * -> Needs to come from Program */ $ret .= "<td colspan='2' style='text-align:center;'>"; if ($memNum == $programBankNumber) { $args3 = $args2; $ret .= "<a id='ilink' name='ilink'\n href=\"{$FANNIE_URL}{$this->pluginHome}membership/CoopCredInputTool.php?{$args3}\"\n title='Input (deposit) external funds to the Program Account'\n >Input Coop Cred</a>"; } elseif ($memNum >= $this->regularMemberMin && $memNum <= $this->regularMemberMax) { $reportLink = "<a id='arlink' name='arlink' " . "href=\"{$FANNIE_URL}{$this->pluginHome}reports/" . "Activity/ActivityReport.php?" . "memNum={$memNum}&programID={$programID}\"" . " title='List earnings and purchases for this member'" . " target='_blank'" . "><p style='margin:0em; font-family:Arial;line-height:1.0em;'>" . "Activity Report</p></a>"; $ret .= $reportLink; } else { $ret .= " "; } $ret .= "</td>"; $ret .= "</tr>"; $ret .= "</table>"; //$ret .= "</fieldset>"; $ret .= "</div><!-- /.panel-body -->"; if ($inProgramID == -1) { /* The fieldset goes in the ccremdiv container. */ $ret .= "</div> <!-- /ccredmemdiv -->"; $ret .= '<fieldset id="_fhtt14102004" style="display:none; width:440px;">' . "Let the Member make purchases against money in an account. " . "<br />A Member may have an account in (be a member of) more than one Program." . "<br />'Balance' shows how much is left in the account." . "<br />Un-ticking 'OK' will suspend the account when member data is refreshed on lanes. " . "<br />Use the 'Transfer' link to add money to the account from the Program Bank." . "<br />The Member is allowed to use all accounts in the Program dropdown " . "that are in <b>bold</b>." . "<br />To add a Member to a Program (allow him/her to use it) " . "select the Program, tick 'Use Coop Cred' and click 'Save'" . "<!-- br />The Transfer, Fix and Activity links are for the Program that " . "is selected when the form initially displays. " . "To use Transfer etc. for another Program, temporarily untick 'Use Coop Cred', " . "click Save, and Edit the same Member again." . "This will expose the next Program the Member belongs to." . "<br />After making and Saving the changes, select the initial Program again, " . "tick 'Use Coop Cred' and click Save." . " -->" . "</fieldset>"; $ret .= sprintf("<input type='hidden' id='memNum' name='memNum' value='%d' />", $memNum); $ret .= sprintf("<input type='hidden' id='pathTo' name='pathTo' value='%s' />", $FANNIE_URL . $this->pluginHome . 'membership/'); $ret .= "</div><!-- /.panel .panel-default -->"; $ret .= $this->getEditJavascript(); } return $ret; // ShowEditForm() }