public function run() { global $FANNIE_LANES; global $FANNIE_PLUGIN_LIST; global $FANNIE_PLUGIN_SETTINGS; if (!FanniePlugin::isEnabled($this->pluginName)) { echo $this->cronMsg("Plugin '{$this->pluginName}' is not enabled."); return False; } if (!array_key_exists("{$this->pluginName}Database", $FANNIE_PLUGIN_SETTINGS) || empty($FANNIE_PLUGIN_SETTINGS["{$this->pluginName}Database"])) { echo $this->cronMsg("Setting: '{$this->pluginName}Database' is not set."); return False; } $server_db = $FANNIE_PLUGIN_SETTINGS["{$this->pluginName}Database"]; $dbc = FannieDB::get($server_db); if ($dbc === False) { echo $this->cronMsg("Unable to connect to {$server_db}."); return False; } // get balances that changed today $balanceData = array(); $fetchQ = "SELECT programID, cardNo, balance\n FROM CCredMemCreditBalance\n WHERE mark=1"; //orig: $fetchQ = "SELECT CardNo, balance FROM memChargeBalance WHERE mark=1"; $fetchR = $dbc->query($fetchQ); if ($fetchR === False) { echo $this->cronMsg("Fatal: {$fetchQ}"); flush(); return; } // Make a list of updates. while ($fetchW = $dbc->fetch_row($fetchR)) { $key = $fetchW['programID'] . '|' . $fetchW['cardNo']; // The order of elements as needed in the UPDATE statement. $balanceData["{$key}"] = array($fetchW['balance'], $fetchW['programID'], $fetchW['cardNo']); } /* Debug echo $this->cronMsg("Balance updates to do: " . count($balanceData)); */ /* Get CCredMemberships that have changed today. * Does not handle Members added today. */ $memberData = array(); $memberQ = "SELECT programID, cardNo, creditLimit, maxCreditBalance,\n creditOK, inputOK, transferOK, modified\n FROM CCredMemberships\n WHERE date(modified) = date(" . $dbc->now() . ")"; $memberR = $dbc->query($memberQ); if ($memberR === False) { echo $this->cronMsg("Failed: {$memberQ}"); $errors = True; } else { while ($memberW = $dbc->fetch_row($memberR)) { $key = $memberW['programID'] . '|' . $memberW['cardNo']; // The order of elements as needed in the UPDATE statement. $memberData["{$key}"] = array($memberW['creditLimit'], $memberW['maxCreditBalance'], $memberW['creditOK'], $memberW['inputOK'], $memberW['transferOK'], $memberW['modified'], $memberW['programID'], $memberW['cardNo']); } } /* Debug echo $this->cronMsg("Member updates to do: " . count($memberData)); */ /* Get CCredPrograms that have changed today. * Does not handle Programs added today. */ $programData = array(); $programQ = "SELECT programID, active, startDate, endDate,\n creditOK, inputOK, transferOK, maxCreditBalance,\n modified\n FROM CCredPrograms\n WHERE date(modified) = date(" . $dbc->now() . ")"; $programR = $dbc->query($programQ); if ($programR === False) { echo $this->cronMsg("Failed: {$programQ}"); $errors = True; } else { while ($programW = $dbc->fetch_row($programR)) { $key = $programW['programID']; // The order of elements as needed in the UPDATE statement. $programData["{$key}"] = array($programW['active'], $programW['startDate'], $programW['endDate'], $programW['creditOK'], $programW['inputOK'], $programW['transferOK'], $programW['maxCreditBalance'], $programW['modified'], $programW['programID']); } } /* Debug echo $this->cronMsg("Program updates to do: " . count($programData)); */ $errors = False; // connect to each lane and update balances foreach ($FANNIE_LANES as $lane) { $dbL = new SQLManager($lane['host'], $lane['type'], $lane['op'], $lane['user'], $lane['pw']); if ($dbL === False) { echo $this->cronMsg("Can't connect to lane: " . $lane['host'] . " db: {$lane['op']} ."); $errors = True; continue; } /* Find the name of the CoopCred db on the lane. * Why is PluginList in opdata.lane_config but PluginSettings isn't? * opdata.parameters has PluginList, CoopCredLaneDatabase */ $coopCredEnabled = 0; $laneDB = ""; $laneQ = "SELECT * FROM parameters\n WHERE param_key IN ('PluginList', 'CoopCredLaneDatabase')\n ORDER BY param_key, store_id, lane_id"; $laneR = $dbL->query($laneQ); if ($laneR === False) { echo $this->cronMsg("Failed query on: " . $lane['host'] . " query: {$query}"); $errors = True; continue; } /* Local values will override global. */ while ($laneP = $dbL->fetch_row($laneR)) { if ($laneP['param_key'] == 'PluginList') { $paramList = explode(',', $laneP['param_value']); if (in_array($this->pluginName, $paramList)) { $coopCredEnabled = 1; } } if ($laneP['param_key'] == 'CoopCredLaneDatabase') { $laneDB = $laneP['param_value']; } } if (!$coopCredEnabled) { echo $this->cronMsg("{$this->pluginName} is not enabled on: " . $lane['host']); continue; } if ($laneDB == '') { echo $this->cronMsg("No CoopCredDatabase named on: " . $lane['host']); continue; } // Change db on connection to the ccred db. $ccDB = $dbL->addConnection($lane['host'], $lane['type'], $laneDB, $lane['user'], $lane['pw']); if ($ccDB === False) { echo $this->cronMsg("Can't add connection to {$laneDB} on: " . $lane['host']); $errors = True; continue; } $dbL->default_db = $laneDB; foreach ($balanceData as $dt) { $upQ = "UPDATE CCredMemberships\n SET creditBalance=?,\n modified=" . $dbc->now() . ",\n modifiedBy=9998\n WHERE programID=? AND cardNo=?"; $upS = $dbL->prepare($upQ); $upR = $dbL->execute($upS, $dt); if ($upR === False) { echo $this->cronMsg("Balance update failed: member: {$dt[2]} " . "in program {$dt[1]} on lane: {$lane['host']}"); $errors = True; } /* Debug else { echo $this->cronMsg("Balance update OK: member: {$dt[2]} in program {$dt[1]} on lane: {$lane['host']}"); } */ } foreach ($memberData as $dt) { $upQ = "UPDATE CCredMemberships\n SET creditLimit=?,\n maxCreditBalance=?,\n creditOK=?,\n inputOK=?,\n transferOK=?,\n modified=?,\n modifiedBy=9999\n WHERE programID=? AND cardNo=?"; $upS = $dbL->prepare($upQ); $upR = $dbL->execute($upS, $dt); if ($upR === False) { echo $this->cronMsg("Member update failed: member: {$dt[7]} in program {$dt[6]} on lane: {$lane['host']}"); $errors = True; } /* Debug else { echo $this->cronMsg("Member update OK: member: {$dt[7]} ". "in program {$dt[6]} on lane: {$lane['host']}"); } */ } foreach ($programData as $dt) { $upQ = "UPDATE CCredPrograms\n SET active=?,\n startDate=?,\n endDate=?,\n creditOK=?,\n inputOK=?,\n transferOK=?,\n maxCreditBalance=?,\n modified=?,\n modifiedBy=9999\n WHERE programID=?"; $upS = $dbL->prepare($upQ); $upR = $dbL->execute($upS, $dt); if ($upR === False) { echo $this->cronMsg("Program update failed: program {$dt[8]} " . "on lane: {$lane['host']}"); $errors = True; } /* Debug else { echo $this->cronMsg("Program update OK: program {$dt[8]} ". "on lane: {$lane['host']}"); } */ } // each lane } if ($errors) { echo $this->cronMsg("There was an error pushing balances to the lanes."); flush(); } else { /* Debug echo $this->cronMsg("All OK."); */ $noop = 0; } // /run }