public static function getMeminfo($card_no) { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $model = new MeminfoModel($dbc); $model->card_no($card_no); $model->load(); if ($model->load()) { return $model; } else { return false; } }
function process_file($linedata) { global $FANNIE_OP_DB, $FANNIE_TRANS_DB; $EMP_NO = $this->config->get('EMP_NO'); $LANE_NO = $this->config->get('REGISTER_NO'); $OFFSET_DEPT = $this->config->get('MISC_DEPT'); $card_no = $this->get_column_index('card_no'); $classA = $this->get_column_index('classA'); $classB = $this->get_column_index('classB'); $note = $this->get_column_index('note'); $dbc = FannieDB::get($FANNIE_OP_DB); $dtrans_table = $FANNIE_TRANS_DB . $dbc->sep() . 'dtransactions'; $prep = $dbc->prepare('SELECT MAX(trans_no) as tn FROM ' . $dtrans_table . ' WHERE emp_no=? AND register_no=?'); $result = $dbc->execute($prep, array($EMP_NO, $LANE_NO)); $trans = 1; if ($dbc->num_rows($result) > 0) { $row = $dbc->fetch_row($result); if ($row['tn'] != '') { $trans = $row['tn'] + 1; } } foreach ($linedata as $data) { if (!isset($data[$card_no])) { continue; } elseif (!is_numeric($data[$card_no])) { continue; } $cn = trim($data[$card_no]); $a_amt = trim($data[$classA], '$ '); $b_amt = trim($data[$classB], '$ '); $offset_amt = $a_amt + $b_amt; if ($offset_amt == 0) { continue; } $now = date('Y-m-d H:i:s'); $custdata = new CustdataModel($dbc); $custdata->CardNo($cn); $custdata->personNum(1); $custdata->load(); $meminfo = new MeminfoModel($dbc); $meminfo->card_no($cn); $meminfo->load(); $susp = new SuspensionsModel($dbc); $susp->cardno($cn); $susp->load(); $susp->type('T'); $susp->memtype1($custdata->memType()); $susp->memtype2($custdata->Type()); $susp->suspDate($now); $susp->discount($custdata->Discount()); $susp->chargelimit($custdata->ChargeLimit()); $susp->mailflag($meminfo->ads_OK()); $susp->reasoncode(64); $susp->save(); $suspHistory = new SuspensionHistoryModel($dbc); $suspHistory->username('abandon-import'); $suspHistory->postdate($now); $suspHistory->cardno($cn); $suspHistory->reasoncode(64); $suspHistory->save(); $meminfo->ads_OK(0); $meminfo->save(); $custdata->reset(); $custdata->CardNo($cn); foreach ($custdata->find() as $obj) { $obj->Type('TERM'); $obj->memType(0); $obj->Discount(0); $obj->ChargeLimit(0); $obj->MemDiscountLimit(0); $obj->save(); } if (isset($data[$note]) && !empty($data[$note])) { $memNote = new MemberNotesModel($dbc); $memNote->cardno($cn); $memNote->note($data[$note]); $memNote->stamp($now); $memNote->username('abandon-import'); $memNote->save(); } $trans_id = 1; if ($a_amt > 0) { $record = DTrans::$DEFAULTS; $record['register_no'] = $LANE_NO; $record['emp_no'] = $EMP_NO; $record['trans_no'] = $trans; $record['upc'] = $a_amt . 'DP992'; $record['description'] = 'Class A Equity'; $record['trans_type'] = 'D'; $record['department'] = 992; $record['unitPrice'] = -1 * $a_amt; $record['total'] = -1 * $a_amt; $record['regPrice'] = -1 * $a_amt; $record['card_no'] = $cn; $record['trans_id'] = $trans_id; $trans_id++; $info = DTrans::parameterize($record, 'datetime', $dbc->now()); $prep = $dbc->prepare("INSERT INTO {$dtrans_table} ({$info['columnString']}) VALUES ({$info['valueString']})"); $dbc->execute($prep, $info['arguments']); } if ($b_amt > 0) { $record = DTrans::$DEFAULTS; $record['register_no'] = $LANE_NO; $record['emp_no'] = $EMP_NO; $record['trans_no'] = $trans; $record['upc'] = $a_amt . 'DP991'; $record['description'] = 'Class B Equity'; $record['trans_type'] = 'D'; $record['department'] = 991; $record['unitPrice'] = -1 * $b_amt; $record['total'] = -1 * $b_amt; $record['regPrice'] = -1 * $b_amt; $record['card_no'] = $cn; $record['trans_id'] = $trans_id; $trans_id++; $info = DTrans::parameterize($record, 'datetime', $dbc->now()); $prep = $dbc->prepare("INSERT INTO {$dtrans_table} ({$info['columnString']}) VALUES ({$info['valueString']})"); $dbc->execute($prep, $info['arguments']); } $record = DTrans::$DEFAULTS; $record['register_no'] = $LANE_NO; $record['emp_no'] = $EMP_NO; $record['trans_no'] = $trans; $record['upc'] = $offset_amt . 'DP' . $OFFSET_DEPT; $record['description'] = 'Abandon Equity'; $record['trans_type'] = 'D'; $record['department'] = $OFFSET_DEPT; $record['unitPrice'] = $offset_amt; $record['total'] = $offset_amt; $record['regPrice'] = $offset_amt; $record['card_no'] = $cn; $record['trans_id'] = $trans_id; $trans_id++; $info = DTrans::parameterize($record, 'datetime', $dbc->now()); $prep = $dbc->prepare("INSERT INTO {$dtrans_table} ({$info['columnString']}) VALUES ({$info['valueString']})"); $dbc->execute($prep, $info['arguments']); $record = DTrans::$DEFAULTS; $record['register_no'] = $LANE_NO; $record['emp_no'] = $EMP_NO; $record['trans_no'] = $trans; $record['upc'] = '0'; $record['description'] = '63350'; $record['trans_type'] = 'C'; $record['trans_subtype'] = 'CM'; $record['card_no'] = $cn; $record['trans_id'] = $trans_id; $info = DTrans::parameterize($record, 'datetime', $dbc->now()); $prep = $dbc->prepare("INSERT INTO {$dtrans_table} ({$info['columnString']}) VALUES ({$info['valueString']})"); $dbc->execute($prep, $info['arguments']); $trans++; } return true; }
public function run() { global $FANNIE_OP_DB, $FANNIE_PLUGIN_SETTINGS, $argv; $dbc = FannieDB::get($FANNIE_OP_DB); $custdata = new CustdataModel($dbc); $meminfo = new MeminfoModel($dbc); $APIKEY = 'a92f83d3e5f7fe52d4579e7902c6491d-us8'; $LISTID = '54100d18af'; $APIKEY = $FANNIE_PLUGIN_SETTINGS['MailChimpApiKey']; $LISTID = $FANNIE_PLUGIN_SETTINGS['MailChimpListID']; if (empty($APIKEY) || empty($LISTID)) { $this->cronMsg('Missing API key or List ID', FannieLogger::NOTICE); return false; } if (!class_exists('Mailchimp')) { $this->cronMsg('MailChimp library is not installed', FannieLogger::NOTICE); return false; } $mc = new MailChimpEx($APIKEY); if ($FANNIE_PLUGIN_SETTINGS['MailChimpMergeVarField'] != 1) { $vars = $mc->lists->mergeVars(array($LISTID)); $field_id = false; if ($vars['data']) { $current = array_pop($vars['data']); foreach ($current['merge_vars'] as $index => $info) { if ($info['tag'] == 'CARDNO') { $field_id = $info['id']; break; } } if ($field_id !== false) { echo 'Found member# field' . "\n"; } else { echo 'Adding member# field' . "\n"; $new = $mc->lists->mergeVarAdd($LISTID, 'CARDNO', 'Owner Number', array('field_type' => 'number', 'public' => false)); $field_id = $new['id']; } } if ($field_id === false) { $this->cronMsg('Error: could not locate / create owner number field!', FannieLogger::NOTICE); return false; } } // end create owner number field if needed $statuses = array('subscribed', 'unsubscribed', 'cleaned'); $cleans = array(); $memlist = ''; /** Examine all list members */ foreach ($statuses as $status) { $this->cronMsg('==== Checking ' . $status . ' emails ====', FannieLogger::INFO); $full_list = $mc->lists->export($LISTID, $status); $headers = array_shift($full_list); $columns = array(); foreach ($headers as $index => $name) { $columns[strtoupper($name)] = $index; } $line_count = 1; foreach ($full_list as $record) { /** Print progress meter in verbose mode */ if (isset($argv[2]) && ($argv[2] == '-v' || $argv[2] == '--verbose')) { printf("Processing %d/%d\r", $line_count, count($full_list)); } $line_count++; $card_no = $record[$columns['OWNER NUMBER']]; $email = $record[$columns['EMAIL ADDRESS']]; $fn = $record[$columns['FIRST NAME']]; $ln = $record[$columns['LAST NAME']]; $changed = isset($columns['LAST_CHANGED']) && isset($record[$columns['LAST_CHANGED']]) ? $record[$columns['LAST_CHANGED']] : 0; /** MailChimp has a POS member number tag **/ if (!empty($card_no)) { switch ($status) { /** If subscribed list member has been tagged with a POS member number, compare MailChimp fields to POS fields. If name disagrees, use POS value for both. If email disagrees, use MailChimp value for both. */ case 'subscribed': $memlist .= sprintf('%d,', $card_no); $custdata->reset(); $custdata->CardNo($card_no); $custdata->personNum(1); $custdata->load(); $update = array(); $meminfo->reset(); $meminfo->card_no($card_no); $meminfo->load(); if ($meminfo->email_1() != $email && strtotime($changed) > strtotime($meminfo->modified())) { $this->cronMsg(sprintf("MISMATCH: POS says %s, MailChimp says %s, Mailchimp is newer", $meminfo->email_1(), $email), FannieLogger::INFO); $meminfo->email_1($email); $meminfo->save(); } elseif ($meminfo->email_1() != $email) { $update['EMAIL'] = $meminfo->email_1(); $this->cronMsg(sprintf("MISMATCH: POS says %s, MailChimp says %s, POS is newer", $meminfo->email_1(), $email), FannieLogger::INFO); } if (strtoupper(trim($custdata->FirstName())) != strtoupper($fn)) { $this->cronMsg(sprintf("MISMATCH: POS says %s, MailChimp says %s", $custdata->FirstName(), $fn), FannieLogger::INFO); $update['FNAME'] = trim($custdata->FirstName()); } if (strtoupper(trim($custdata->LastName())) != strtoupper($ln)) { $this->cronMsg(sprintf("MISMATCH: POS says %s, MailChimp says %s", $custdata->LastName(), $ln), FannieLogger::INFO); $update['LNAME'] = trim($custdata->LastName()); } if (count($update) > 0) { $email_struct = array('euid' => $record[$columns['EUID']], 'leid' => $record[$columns['LEID']]); $this->cronMsg(sprintf("Updating name field(s) for member #%d", $card_no), FannieLogger::INFO); try { $mc->lists->updateMember($LISTID, $email_struct, $update, '', false); } catch (Exception $ex) { echo $ex; var_dump($update); } sleep(1); } break; /** Just track the number to avoid re-adding them to the list */ /** Just track the number to avoid re-adding them to the list */ case 'unsubscribed': $memlist .= sprintf('%d,', $card_no); break; /** Cleaned in MailChimp means the address isn't deliverable In this situation, remove the bad address from POS and delete the address from MailChimp. The member can be re-added when a correct email is entered into POS. */ /** Cleaned in MailChimp means the address isn't deliverable In this situation, remove the bad address from POS and delete the address from MailChimp. The member can be re-added when a correct email is entered into POS. */ case 'cleaned': $meminfo->reset(); $meminfo->card_no($card_no); $meminfo->email_1(''); $meminfo->save(); $this->cronMsg(sprintf('CLEANING Member %d, email %s', $card_no, $email), FannieLogger::INFO); $cleans[] = $record; break; } /** If list member is not tagged with a POS member number, try to locate them in POS by name and/or email address. If found, tag them in MailChimp with the POS member number. This whole situation only occurs if the initial list is imported without member numbers. */ } else { switch ($status) { // subscribed users can be updated easily case 'subscribed': $update = array(); $meminfo->reset(); $meminfo->email_1($email); $matches = $meminfo->find(); if (count($matches) == 1) { $update['CARDNO'] = $matches[0]->card_no(); } else { $custdata->reset(); $custdata->FirstName($fn); $custdata->LastName($ln); $custdata->personNum(1); $custdata->Type('PC'); $matches = $custdata->find(); if (count($matches) == 1) { $update['CARDNO'] = $matches[0]->CardNo(); } } if (isset($update['CARDNO'])) { $email_struct = array('email' => $email, 'euid' => $record[$columns['EUID']], 'leid' => $record[$columns['LEID']]); $this->cronMsg("Assigning member # to account " . $email, FannieLogger::INFO); $mc->lists->updateMember($LISTID, $email_struct, $update, '', false); sleep(1); $memlist .= sprintf('%d,', $update['CARDNO']); } break; /** Unsubscribed are currently ignored. The can't be updated as is. They could be deleted entirely via unsubscribe, resubscribed with an owner number, and then unsubscribed again. That's not currently implemented. It does check for the email address on the POS side to prevent re-adding it. */ /** Unsubscribed are currently ignored. The can't be updated as is. They could be deleted entirely via unsubscribe, resubscribed with an owner number, and then unsubscribed again. That's not currently implemented. It does check for the email address on the POS side to prevent re-adding it. */ case 'unsubscribed': $meminfo->reset(); $this->cronMsg('Checking unsubscribed ' . $email, FannieLogger::INFO); $meminfo->email_1($email); $matches = $meminfo->find(); foreach ($matches as $opted_out) { $memlist .= sprintf('%d,', $opted_out->card_no()); $this->cronMsg('Excluding member ' . $opted_out->card_no(), FannieLogger::INFO); } break; /** Cleaned are bad addresses. Delete them from POS database then from Mail Chimp. */ /** Cleaned are bad addresses. Delete them from POS database then from Mail Chimp. */ case 'cleaned': $meminfo->reset(); $meminfo->email_1($email); foreach ($meminfo->find() as $bad_address) { $bad_address->email_1(''); $bad_address->save(); $this->cronMsg(sprintf('CLEANING untagged member %d, email %s', $bad_address->card_no(), $email), FannieLogger::INFO); } $cleans[] = $record; break; } } } // foreach list member record } // foreach list status /** Removed bounced from the MailChimp list now that POS has been updated */ $removal_batch = array(); foreach ($cleans as $record) { if (empty($record[$columns['EMAIL ADDRESS']])) { continue; } $email_struct = array('email' => $record[$columns['EMAIL ADDRESS']], 'euid' => $record[$columns['EUID']], 'leid' => $record[$columns['LEID']]); $removal_batch[] = $email_struct; } if (count($removal_batch) > 0) { $this->cronMsg(sprintf('Removing %d addresses with status "cleaned"', count($removal_batch)), FannieLogger::INFO); $result = $mc->lists->batchUnsubscribe($LISTID, $removal_batch, true, false, false); } /** Finally, find new members and add them to MailChimp */ if ($memlist === '') { $memlist = '-1,'; } $memlist = substr($memlist, 0, strlen($memlist) - 1); $query = 'SELECT m.card_no, m.email_1, c.FirstName, c.LastName FROM meminfo AS m INNER JOIN custdata AS c ON c.CardNo=m.card_no AND c.personNum=1 WHERE c.Type = \'PC\' AND m.email_1 IS NOT NULL AND m.email_1 <> \'\' AND m.card_no NOT IN (' . $memlist . ')'; $result = $dbc->query($query); $add_batch = array(); while ($row = $dbc->fetch_row($result)) { if (!filter_var($row['email_1'], FILTER_VALIDATE_EMAIL)) { continue; } $new = array('email' => array('email' => $row['email_1']), 'email_type' => 'html', 'merge_vars' => array('FNAME' => $row['FirstName'], 'LNAME' => $row['LastName'], 'CARDNO' => $row['card_no'])); $add_batch[] = $new; } if (count($add_batch) > 0) { $this->cronMsg(sprintf('Adding %d new members', count($add_batch)), FannieLogger::INFO); $added = $mc->lists->batchSubscribe($LISTID, $add_batch, false, true); } return true; }
public function post_handler() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $fy = FormLib::get('fy'); $per_page = FormLib::get('per_page'); $custdata = new CustdataModel($dbc); $meminfo = new MeminfoModel($dbc); $query = $dbc->prepare(' SELECT p.cardno, p.cash_pat, m.zip, p.check_number FROM patronage AS p INNER JOIN meminfo AS m ON p.cardno=m.card_no INNER JOIN custdata AS c ON p.cardno=c.CardNo AND c.personNum=1 WHERE p.FY=? ORDER BY m.zip, c.LastName, c.FirstName'); $result = $dbc->execute($query, array($fy)); $pdf = new FPDF('P', 'mm', 'Letter'); $pdf->SetMargins(6.35, 6.35, 6.35); // quarter-inch margins $pdf->SetAutoPageBreak(false); $filename = ''; $this->files = array(); $filenumber = 1; set_time_limit(0); while ($w = $dbc->fetch_row($result)) { if (empty($filename)) { $filename = $filenumber . '-' . substr($w['zip'], 0, 5); } $dbc = FannieDB::get($FANNIE_OP_DB); $custdata->CardNo($w['cardno']); $custdata->personNum(1); $custdata->load(); $meminfo->card_no($w['cardno']); $meminfo->load(); if ($w['check_number'] == '') { $patronage = new PatronageModel($dbc); $patronage->cardno($w['cardno']); $patronage->FY($fy); $number = GumLib::allocateCheck($patronage, false); $dbc = FannieDB::get($FANNIE_OP_DB); $patronage->check_number($number); $patronage->save(); $w['check_number'] = $number; } $pdf->AddPage(); $pdf->Image('rebate_body.png', 10, 0, 190); $check = new GumCheckTemplate($custdata, $meminfo, $w['cash_pat'], 'Rebate ' . $fy, $w['check_number']); $check->renderAsPDF($pdf); if ($pdf->PageNo() == $per_page) { $filename .= '-' . substr($w['zip'], 0, 5) . '.pdf'; $filenumber++; $pdf->Output('/tmp/' . $filename, 'F'); $this->files[] = $filename; $filename = ''; $pdf = new FPDF('P', 'mm', 'Letter'); $pdf->SetMargins(6.35, 6.35, 6.35); // quarter-inch margins $pdf->SetAutoPageBreak(false); } } $filename .= '-End.pdf'; $pdf->Output('/tmp/' . $filename, 'F'); $this->files[] = $filename; return true; }