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); }
/** 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; } }