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; }
function MarginFS($upc, $cost, $deptID) { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $price = 'None'; $prod = new ProductsModel($dbc); $prod->upc($upc); if ($prod->load()) { $price = $prod->normal_price(); } $dm = 'Unknown'; $dept = new DepartmentsModel($dbc); $dept->dept_no($deptID); if ($dept->load()) { $dm = $dept->margin(); } if ((empty($dm) || $dm == 'Unknown') && $dbc->tableExists('deptMargin')) { $prep = $dbc->prepare_statement("SELECT margin FROM deptMargin WHERE dept_ID=?"); $dm = $dbc->exec_statement($prep, array($deptID)); if ($dbc->num_rows($dm) > 0) { $row = $dbc->fetch_row($dm); $dm = $dm['margin']; } } $ret = "Desired margin on this department is "; if ($dm == "Unknown") { $ret .= $dm; } else { $ret .= sprintf("%.2f%%", $dm * 100); } $ret .= "<br />"; $actual = 0; if ($price != 0) { $actual = ($price - $cost) / $price; } if ($actual > $dm && is_numeric($dm) || !is_numeric($dm)) { $ret .= sprintf("<span style=\"color:green;\">Current margin on this item is %.2f%%<br />", $actual * 100); } else { if (!is_numeric($price)) { $ret .= "<span style=\"color:green;\">No price has been saved for this item<br />"; } else { $ret .= sprintf("<span style=\"color:red;\">Current margin on this item is %.2f%%</span><br />", $actual * 100); $srp = getSRP($cost, $dm); $ret .= sprintf("Suggested price: \$%.2f ", $srp); $ret .= sprintf("(<a href=\"\" onclick=\"setPrice(%.2f); return false;\">Use this price</a>)", $srp); } } echo $ret; }
protected function hookAddColumnmargin() { if ($this->connection->table_exists('deptMargin')) { $dataR = $this->connection->query('SELECT dept_ID, margin FROM deptMargin'); $tempModel = new DepartmentsModel($this->connection); while ($dataW = $this->connection->fetch_row($dataR)) { $tempModel->reset(); $tempModel->dept_no($dataW['dept_ID']); if ($tempModel->load()) { $tempModel->margin($dataW['margin']); $tempModel->save(); } } } }
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); }
/** Do whatever the service is supposed to do. Should override this. @param $args array of data @return an array of data */ public function run($args = array()) { $ret = array(); if (!property_exists($args, 'type')) { // missing required arguments $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } // validate additional arguments switch (strtolower($args->type)) { case 'settings': if (!property_exists($args, 'dept_no')) { // missing required arguments $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } break; case 'children': if (!property_exists($args, 'superID') && !property_exists($args, 'dept_no')) { // missing required arguments $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } if (property_exists($args, 'superID') && is_array($args->superID) && count($args->superID) != 2) { // range must specify exactly two superIDs $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } if (property_exists($args, 'dept_no') && is_array($args->dept_no) && count($args->dept_no) != 2) { // range must specify exactly two dept_nos $et['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } break; default: // unknown type argument $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } // lookup results $dbc = \FannieDB::getReadOnly(\FannieConfig::factory()->get('OP_DB')); switch (strtolower($args->type)) { case 'settings': $model = new DepartmentsModel($dbc); $model->dept_no($args->dept_no); $model->load(); $ret['tax'] = $model->dept_tax(); $ret['fs'] = $model->dept_fs(); $ret['discount'] = $model->dept_discount(); $ret['seeID'] = $model->dept_see_id(); $ret['margin'] = $model->margin(); return $ret; case 'children': $query = ''; $params = array(); if (property_exists($args, 'dept_no')) { $query = ' SELECT s.subdept_no AS id, s.subdept_name AS name FROM departments AS d INNER JOIN subdepts AS s ON d.dept_no=s.dept_ID '; if (property_exists($args, 'superID') && is_numeric($args->superID)) { $query .= ' INNER JOIN superdepts AS a ON d.dept_no=a.dept_ID '; } if (is_array($args->dept_no)) { $query .= ' WHERE d.dept_no BETWEEN ? AND ? '; $params[] = $args->dept_no[0]; $params[] = $args->dept_no[1]; } else { $query .= ' WHERE d.dept_no = ? '; $params[] = $args->dept_no; } if (property_exists($args, 'superID') && is_numeric($args->superID)) { $query .= ' AND a.superID = ? '; $params[] = $args->superID; } $query .= ' ORDER BY s.subdept_no'; } else { $query = ' SELECT d.dept_no AS id, d.dept_name AS name FROM superdepts AS s INNER JOIN departments AS d ON d.dept_no=s.dept_ID '; if (is_array($args->superID)) { $query .= ' WHERE s.superID BETWEEN ? AND ? '; $params[] = $args->superID[0]; $params[] = $args->superID[1]; } else { $query .= ' WHERE s.superID = ? '; $params[] = $args->superID; } $query .= ' ORDER BY d.dept_no'; // support meta-options for all departments if (!is_array($args->superID) && $args->superID < 0) { if ($args->superID == -1) { $query = ' SELECT d.dept_no AS id, d.dept_name AS name FROM departments AS d ORDER BY d.dept_no'; $params = array(); } elseif ($args->superID == -2) { $query = ' SELECT d.dept_no AS id, d.dept_name AS name FROM departments AS d INNER JOIN MasterSuperDepts AS m ON d.dept_no=m.dept_ID WHERE m.superID <> 0 ORDER BY d.dept_no'; $params = array(); } } } $prep = $dbc->prepare($query); $res = $dbc->execute($prep, $params); while ($w = $dbc->fetch_row($res)) { $ret[] = array('id' => $w['id'], 'name' => $w['name']); } return $ret; } }
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; }