function process_file($linedata) { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $dn_index = $this->get_column_index('dept_no'); $desc_index = $this->get_column_index('desc'); $margin_index = $this->get_column_index('margin'); $tax_index = $this->get_column_index('tax'); $fs_index = $this->get_column_index('fs'); // prepare statements $marP = $dbc->prepare_statement("INSERT INTO deptMargin (dept_ID,margin) VALUES (?,?)"); $scP = $dbc->prepare_statement("INSERT INTO deptSalesCodes (dept_ID,salesCode) VALUES (?,?)"); $model = new DepartmentsModel($dbc); foreach ($linedata as $line) { // get info from file and member-type default settings // if applicable $dept_no = $line[$dn_index]; $desc = $line[$desc_index]; $margin = $margin_index !== False ? $line[$margin_index] : 0; if ($margin > 1) { $margin /= 100.0; } $tax = $tax_index !== False ? $line[$tax_index] : 0; $fs = $fs_index !== False ? $line[$fs_index] : 0; if (!is_numeric($dept_no)) { continue; } // skip header/blank rows if (strlen($desc) > 30) { $desc = substr($desc, 0, 30); } $model->reset(); $model->dept_no($dept_no); $model->dept_name($desc); $model->dept_tax($tax); $model->dept_fs($fs); $model->dept_limit(50); $model->dept_minimum(0.01); $model->dept_discount(1); $model->dept_see_id(0); $model->modified(date('Y-m-d H:i:s')); $model->modifiedby(1); $model->margin($margin); $model->salesCode($dept_no); $imported = $model->save(); if ($imported) { $this->stats['imported']++; } else { $this->stats['errors'][] = 'Error imported department #' . $dept_no; } if ($dbc->tableExists('deptMargin')) { $insR = $dbc->exec_statement($marP, array($dept_no, $margin)); } if ($dbc->tableExists('deptSalesCodes')) { $insR = $dbc->exec_statement($scP, array($dept_no, $dept_no)); } } return true; }
private function ajax_save_dept() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $id = FormLib::get_form_value('did', 0); $name = FormLib::get_form_value('name', ''); $tax = FormLib::get_form_value('tax', 0); $fs = FormLib::get_form_value('fs', 0); $disc = FormLib::get_form_value('disc', 1); $min = FormLib::get_form_value('min', 0.01); $max = FormLib::get_form_value('max', 50.0); $margin = FormLib::get_form_value('margin', 0); $margin = (double) $margin / 100.0; $pcode = FormLib::get_form_value('pcode', $id); if (!is_numeric($pcode)) { $pcode = (int) $id; } $new = FormLib::get_form_value('new', 0); $model = new DepartmentsModel($dbc); $model->dept_no($id); $model->dept_name($name); $model->dept_tax($tax); $model->dept_fs($fs); $model->dept_discount($disc); $model->dept_minimum($min); $model->dept_limit($max); $model->modified(date('Y-m-d H:i:s')); $model->margin($margin); $model->salesCode($pcode); if ($new == 1) { $model->modifiedby(1); $model->dept_see_id(0); } $saved = $model->save(); if ($new == 1) { if ($saved === False) { echo 'Error: could not create department'; return; } $superP = $dbc->prepare_statement('INSERT INTO superdepts (superID,dept_ID) VALUES (0,?)'); $superR = $dbc->exec_statement($superP, array($id)); } else { if ($saved === False) { echo 'Error: could not save changes'; return; } } if ($dbc->tableExists('deptMargin')) { $chkM = $dbc->prepare_statement('SELECT dept_ID FROM deptMargin WHERE dept_ID=?'); $mR = $dbc->exec_statement($chkM, array($id)); if ($dbc->num_rows($mR) > 0) { $up = $dbc->prepare_statement('UPDATE deptMargin SET margin=? WHERE dept_ID=?'); $dbc->exec_statement($up, array($margin, $id)); } else { $ins = $dbc->prepare_statement('INSERT INTO deptMargin (dept_ID,margin) VALUES (?,?)'); $dbc->exec_statement($ins, array($id, $margin)); } } if ($dbc->tableExists('deptSalesCodes')) { $chkS = $dbc->prepare_statement('SELECT dept_ID FROM deptSalesCodes WHERE dept_ID=?'); $rS = $dbc->exec_statement($chkS, array($id)); if ($dbc->num_rows($rS) > 0) { $up = $dbc->prepare_statement('UPDATE deptSalesCodes SET salesCode=? WHERE dept_ID=?'); $dbc->exec_statement($up, array($pcode, $id)); } else { $ins = $dbc->prepare_statement('INSERT INTO deptSalesCodes (dept_ID,salesCode) VALUES (?,?)'); $dbc->exec_statement($ins, array($id, $pcode)); } } $json = array(); $json['did'] = $id; $json['msg'] = 'Department ' . $id . ' - ' . $name . ' Saved'; echo json_encode($json); }
/** 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); }
private function calculateMargin($price, $cost, $deptID, $upc) { $dbc = $this->db(); $desired_margin = 'Unknown'; $dept = new DepartmentsModel($dbc); $dept->dept_no($deptID); $dept_name = 'n/a'; if ($dept->load()) { $desired_margin = $dept->margin() * 100; $dept_name = $dept->dept_name(); } $ret = "Desired margin on this department (" . $dept_name . ") is " . $desired_margin . "%"; $ret .= "<br />"; $vendorP = $dbc->prepare(' SELECT d.margin, n.vendorName, d.name, s.margin AS specialMargin FROM products AS p INNER JOIN vendorItems AS v ON p.upc=v.upc AND v.vendorID=p.default_vendor_id INNER JOIN vendorDepartments AS d ON v.vendorID=d.vendorID AND v.vendorDept=d.deptID INNER JOIN vendors AS n ON v.vendorID=n.vendorID LEFT JOIN VendorSpecificMargins AS s ON p.department=s.deptID AND p.default_vendor_id=s.vendorID WHERE p.upc = ? '); $vendorR = $dbc->execute($vendorP, array($upc)); if ($vendorR && $dbc->num_rows($vendorR) > 0) { $w = $dbc->fetch_row($vendorR); $desired_margin = $w['margin'] * 100; if ($w['specialMargin']) { $desired_margin = 100 * $w['specialMargin']; $w['name'] = 'Custom'; } $ret .= sprintf('Desired margin for this vendor category (%s) is %.2f%%<br />', $w['vendorName'] . ':' . $w['name'], $desired_margin); } $shippingP = $dbc->prepare(' SELECT v.shippingMarkup, v.discountRate, v.vendorName FROM products AS p INNER JOIN vendors AS v ON p.default_vendor_id = v.vendorID WHERE p.upc=?'); $shippingR = $dbc->execute($shippingP, array($upc)); $shipping_markup = 0.0; $vendor_discount = 0.0; if ($shippingR && $dbc->numRows($shippingR) > 0) { $w = $dbc->fetchRow($shippingR); if ($w['discountRate'] > 0) { $ret .= sprintf('Discount rate for this vendor (%s) is %.2f%%<br />', $w['vendorName'], $w['discountRate'] * 100); $vendor_discount = $w['discountRate']; } if ($w['shippingMarkup'] > 0) { $ret .= sprintf('Shipping markup for this vendor (%s) is %.2f%%<br />', $w['vendorName'], $w['shippingMarkup'] * 100); $shipping_markup = $w['discountRate']; } } $cost = Margin::adjustedCost($cost, $vendor_discount, $shipping_markup); $actual_margin = Margin::toMargin($cost, $price, array(100, 2)); if ($actual_margin > $desired_margin && is_numeric($desired_margin)) { $ret .= sprintf("<span class=\"alert-success\">Current margin on this item is %.2f%%</span><br />", $actual_margin); } elseif (!is_numeric($price)) { $ret .= "<span class=\"alert-danger\">No price has been saved for this item</span><br />"; } else { $ret .= sprintf("<span class=\"alert-danger\">Current margin on this item is %.2f%%</span><br />", $actual_margin); $srp = $this->getSRP($cost, $desired_margin / 100.0); $ret .= sprintf("Suggested price: \$%.2f ", $srp); $ret .= sprintf("(<a href=\"\" onclick=\"\$('.price-input').val(%.2f); \$('.price-input:visible').trigger('change'); return false;\">Use this price</a>)", $srp); } return $ret; }
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++; } }