/** Add an open ring record to dtransactions on the backend @param $connection [SQLManager] database connection @param $department [integer] department number $param $amount [number] ring amount @param $trans_no [integer] transaction number (dtransactions.trans_no) @param $params [array] of column_name => value If emp_no and register_no values are not specified, the defaults are the configuration settings FANNIE_EMP_NO and FANNIE_REGISTER_NO. The following columns are automatically calculated based on department number and amount: - upc - description - trans_type - trans_status - unitPrice - total - regPrice - quantity - ItemQtty Negative amounts result in a refund trans_status This method calls DTrans::addItem() so columns datetime and trans_id are also automatically assigned. */ public static function addOpenRing(SQLManager $connection, $department, $amount, $trans_no, $params = array()) { $config = FannieConfig::factory(); $model = new DepartmentsModel($connection); $model->whichDB($config->get('OP_DB')); $model->dept_no($department); $model->load(); $params['trans_type'] = 'D'; $params['department'] = $department; $params['unitPrice'] = $amount; $params['total'] = $amount; $params['regPrice'] = $amount; $params['quantity'] = 1; $params['ItemQtty'] = 1; if ($amount < 0) { $params['quantity'] = -1; $params['trans_status'] = 'R'; } $params['description'] = $model->dept_name(); $params['upc'] = abs($amount) . 'DP' . $department; return self::addItem($connection, $trans_no, $params); }
public function run() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_AR_DEPARTMENTS, $FANNIE_TRANS_DB, $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['StaffArPayrollDB']); $chkQ = 'SELECT staffArDateID FROM StaffArDates WHERE ' . $dbc->datediff($dbc->now(), 'tdate') . ' = 0 '; $chkR = $dbc->query($chkQ); if ($dbc->num_rows($chkR) == 0) { // not scheduled for today return true; } /** Update plugin's table from legacy table, if present. Can go away once WFC transistions away from legacy table. */ $legacy_table = $FANNIE_TRANS_DB . $dbc->sep() . 'staffAR'; if ($dbc->tableExists($legacy_table)) { $query = 'SELECT cardNo, adjust FROM ' . $legacy_table; $result = $dbc->query($query); $cards = ''; $args = array(); while ($row = $dbc->fetch_row($result)) { $model = new StaffArAccountsModel($dbc); $model->card_no($row['cardNo']); $model->nextPayment($row['adjust']); $model->save(); $cards .= '?,'; $args[] = $row['cardNo']; } // remove records that aren't in legacy table if (count($args) > 0) { $cards = substr($cards, 0, strlen($cards) - 1); $query = "DELETE FROM StaffArAccounts WHERE card_no NOT IN ({$cards})"; $prep = $dbc->prepare($query); $dbc->execute($prep, $args); } } // end legacy table handling // build department list $ar_dept = 0; $ret = preg_match_all("/[0-9]+/", $FANNIE_AR_DEPARTMENTS, $depts); $depts = array_pop($depts); if (!is_array($depts) || count($depts) == 0) { $this->cronMsg('Could not locate any AR departments in Fannie configuration', FannieLogger::NOTICE); return false; } else { $ar_dept = $depts[0]; } $dept_desc = ''; $dept_model = new DepartmentsModel($dbc); $dept_model->whichDB($FANNIE_OP_DB); $dept_model->dept_no($ar_dept); if ($dept_model->load()) { $dept_desc = $dept_model->dept_name(); } $dtrans = $FANNIE_TRANS_DB . $dbc->sep() . 'dtransactions'; $emp = isset($FANNIE_PLUGIN_SETTINGS['StaffArPayrollEmpNo']) ? $FANNIE_PLUGIN_SETTINGS['StaffArPayrollEmpNo'] : 1001; $reg = isset($FANNIE_PLUGIN_SETTINGS['StaffArPayrollRegNo']) ? $FANNIE_PLUGIN_SETTINGS['StaffArPayrollRegNo'] : 20; $query = 'SELECT MAX(trans_no) as maxt FROM ' . $dtrans . ' WHERE emp_no=? AND register_no=?'; $prep = $dbc->prepare($query); $result = $dbc->execute($prep, array($emp, $reg)); $trans_no = 1; if ($dbc->num_rows($result) > 0) { $row = $dbc->fetch_row($result); if ($row['maxt'] != '') { $trans_no = $row['maxt'] + 1; } } $model = new StaffArAccountsModel($dbc); foreach ($model->find() as $obj) { if ($obj->nextPayment() == 0) { // no need to write empty records continue; } $record = DTrans::$DEFAULTS; $record['emp_no'] = $emp; $record['register_no'] = $reg; $record['trans_no'] = $trans_no; $record['trans_id'] = 1; $record['trans_type'] = 'D'; $record['card_no'] = $obj->card_no(); $record['department'] = $ar_dept; $record['description'] = $dept_desc; $record['upc'] = sprintf('%.2fDP%d', $obj->nextPayment(), $ar_dept); $record['total'] = sprintf('%.2f', $obj->nextPayment()); $record['unitPrice'] = sprintf('%.2f', $obj->nextPayment()); $record['regPrice'] = sprintf('%.2f', $obj->nextPayment()); $p = DTrans::parameterize($record, 'datetime', date("'Y-m-d 23:59:59'", strtotime('yesterday'))); $query = "INSERT INTO {$dtrans} ({$p['columnString']}) VALUES ({$p['valueString']})"; $prep = $dbc->prepare($query); $write = $dbc->execute($prep, $p['arguments']); if ($write === false) { $this->cronMsg('Error making staff AR deduction for #' . $obj->card_no(), FannieLogger::ERROR); } $trans_no++; } }