| 1 |
<?php
|
| 2 |
// $Id: erp.inc,v 1.22 2007/06/24 10:55:00 simon Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* PC Shop common helper module
|
| 6 |
*/
|
| 7 |
|
| 8 |
// Works for 4.7 & 5.0
|
| 9 |
if (function_exists('theme_add_style')) {
|
| 10 |
theme_add_style(drupal_get_path('module', 'erp') .'/erp.css');
|
| 11 |
}
|
| 12 |
else {
|
| 13 |
drupal_add_css(drupal_get_path('module', 'erp') .'/erp.css');
|
| 14 |
}
|
| 15 |
|
| 16 |
function shrink($text, $shrink = 0) {
|
| 17 |
if (strlen($text) > $shrink + 2) {
|
| 18 |
return substr($text, 0, $shrink) .'..';
|
| 19 |
}
|
| 20 |
else {
|
| 21 |
return ($text ? $text : '');
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Validate a job/timesheet/whatever's owner
|
| 28 |
*
|
| 29 |
* @param unknown_type $node
|
| 30 |
* @return unknown
|
| 31 |
*/
|
| 32 |
function erp_owner_validate($name) {
|
| 33 |
// Try and retrieve the uid for the job owner based on the name
|
| 34 |
$owner = user_load(array('name' => $name));
|
| 35 |
|
| 36 |
return ($owner->uid ? $owner->uid : 0);
|
| 37 |
}
|
| 38 |
|
| 39 |
function erp_oid_validate($oid) {
|
| 40 |
// Try and retrieve the owner name from the uid
|
| 41 |
$owner = user_load(array('uid' => $oid));
|
| 42 |
|
| 43 |
return ($owner->name ? $owner->name : '');
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Debugging function to list variables
|
| 48 |
*
|
| 49 |
* @param unknown_type $var
|
| 50 |
* @return unknown
|
| 51 |
*/
|
| 52 |
function erp_var_list($var, $html = true) {
|
| 53 |
$string = '';
|
| 54 |
|
| 55 |
if (is_string($var)) {
|
| 56 |
$string = $var;
|
| 57 |
}
|
| 58 |
elseif (is_array($var)) {
|
| 59 |
foreach ($var as $key => $value) {
|
| 60 |
$string .= ($html ? '<li>' : '') . $key . ' : ';
|
| 61 |
|
| 62 |
if (is_array($value) or is_object($value)) {
|
| 63 |
$string .= ($html ? '<ul>' : '') .
|
| 64 |
erp_var_list($value, $html) .
|
| 65 |
($html ? '</ul></li>' : "\n");
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$string .= $value . ($html ? '</li>' : "\n");
|
| 69 |
}
|
| 70 |
}
|
| 71 |
}
|
| 72 |
elseif (is_object($var)) {
|
| 73 |
foreach (get_object_vars($var) as $key => $value) {
|
| 74 |
$string .= ($html ? '<li>' : '') . $key . ($html ? ' : <ul>' : '');
|
| 75 |
$string .= erp_var_list($value, $html) . ($html ? '</ul></li>' : '');
|
| 76 |
}
|
| 77 |
}
|
| 78 |
else {
|
| 79 |
$string .= "\n" . ($html ? '<BR>' : '');
|
| 80 |
}
|
| 81 |
return $string;
|
| 82 |
}
|
| 83 |
|
| 84 |
function erp_find_path_query($url) {
|
| 85 |
global $base_url, $base_path;
|
| 86 |
|
| 87 |
// TODO: This doesn't work properly unless using clean URL's
|
| 88 |
$source = parse_url($url);
|
| 89 |
$path = $source['path'];
|
| 90 |
$new_base = substr(substr($base_path, 1), 0, -1);
|
| 91 |
|
| 92 |
if ($base_path != "/") {
|
| 93 |
$path = str_replace($base_path, '', $path);
|
| 94 |
}
|
| 95 |
|
| 96 |
$query = $source['query'];
|
| 97 |
return array($path, $query);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Function used to both display and set the next invoice number,
|
| 102 |
* po number, etc. see invoice_settings, purchase_order_settings
|
| 103 |
* Defaults to collapsed as its not common
|
| 104 |
*
|
| 105 |
* TODO: I dont think this code will work with postgresql as it uses "REPLACE".
|
| 106 |
*
|
| 107 |
* @param unknown_type $type
|
| 108 |
* @param unknown_type $desc
|
| 109 |
* @return unknown
|
| 110 |
*/
|
| 111 |
function erp_set_next_id_form($form, $type, $desc) {
|
| 112 |
$op = $form_values['op'];
|
| 113 |
|
| 114 |
if ($op == t('Set number')) {
|
| 115 |
db_query("REPLACE INTO {sequences} (name, id) VALUES ('%s', %d)", $type, $_POST['edit'][$type .'_number']);
|
| 116 |
}
|
| 117 |
|
| 118 |
$result = db_query("SELECT id FROM {sequences} WHERE name = '%s'", $type);
|
| 119 |
if (db_num_rows($result)) {
|
| 120 |
$invoice_id = array_pop(db_fetch_array($result));
|
| 121 |
}
|
| 122 |
|
| 123 |
$form[$type .'_settings']['set_next'] =
|
| 124 |
array('#type' => 'fieldset',
|
| 125 |
'#title' => t('Next ') . $desc . t(' number'),
|
| 126 |
'#collapsible' => TRUE,
|
| 127 |
'#collapsed' => TRUE,
|
| 128 |
'#description' => t('You can set the number for the next created ') . $desc . t(' here.'));
|
| 129 |
|
| 130 |
$form[$type .'_settings']['set_next'][$type .'_number'] =
|
| 131 |
array('#type' => 'textfield',
|
| 132 |
'#size' => 10,
|
| 133 |
'#maxlength' => 10,
|
| 134 |
'#title' => t('Next ') . $desc . t(' number'),
|
| 135 |
'#default_value' => $invoice_id);
|
| 136 |
|
| 137 |
$form[$type .'_settings']['set_next'][$type .'_button'] =
|
| 138 |
array('#type' => 'button',
|
| 139 |
'#value' => t('Set number'));
|
| 140 |
|
| 141 |
return $form;
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* This is is common function used by quote, invoice, cash_sale, purchase_order, and so is
|
| 146 |
* placed here. It does depend on "erp_item" being enabled however, meaning all those depend
|
| 147 |
* on erp_item as well.
|
| 148 |
*
|
| 149 |
*/
|
| 150 |
function erp_add_newline(&$node) {
|
| 151 |
if ($_POST['addline'] == t('Add line')) {
|
| 152 |
$newline = $_POST['newline'];
|
| 153 |
|
| 154 |
if ($newline['item_desc']) {
|
| 155 |
list($supplier_code, $stock_code, $desc, $price) = erp_item_desc_breakup($newline['item_desc']);
|
| 156 |
$item_nid = erp_item_locate($supplier_code, $stock_code, FALSE);
|
| 157 |
if ($item_nid) {
|
| 158 |
$item_node = node_load($item_nid);
|
| 159 |
|
| 160 |
erp_set_entries_form_value($node, 'item', $item_nid);
|
| 161 |
erp_set_entries_form_value($node, 'item_desc', erp_item_desc_build($item_node));
|
| 162 |
erp_set_entries_form_value($node, 'qty', $newline['qty']);
|
| 163 |
erp_set_entries_form_value($node, 'completed_date', $newline['completed_date']);
|
| 164 |
erp_set_entries_form_value($node, 'extra', $newline['extra']);
|
| 165 |
erp_set_entries_form_value($node, 'serials', $newline['serials']);
|
| 166 |
erp_set_entries_form_value($node, 'price', $newline['price']);
|
| 167 |
}
|
| 168 |
else {
|
| 169 |
form_set_error('newline][item_desc', t('Invalid stock code'));
|
| 170 |
}
|
| 171 |
}
|
| 172 |
}
|
| 173 |
|
| 174 |
for ($i = 0; $i < count($_POST['entries']['qty']); $i++) {
|
| 175 |
if ($_POST['delete-'. $i] == 'X') {
|
| 176 |
foreach ($node->entries as $value => $key) {
|
| 177 |
if (is_array($node->entries[$value])) {
|
| 178 |
erp_delete_array_key($node->entries[$value], $i);
|
| 179 |
erp_delete_array_key($_POST['entries'][$value], $i);
|
| 180 |
}
|
| 181 |
}
|
| 182 |
}
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|
| 186 |
function erp_set_entries_form_value(&$node, $field, $value) {
|
| 187 |
$x['#parents'] = array('entries', $field, $i, '#default_value');
|
| 188 |
form_set_value($x, $value);
|
| 189 |
unset($x);
|
| 190 |
$node->entries[$field][] = $value;
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Given an array, remove one item, array_splice renumbers as well, which is nice.
|
| 195 |
*
|
| 196 |
* @param array $array
|
| 197 |
* @param int $key
|
| 198 |
*/
|
| 199 |
function erp_delete_array_key(&$array, $key) {
|
| 200 |
array_splice($array, $key, 1);
|
| 201 |
$temp = $array;
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Helper function to display invoice entries.
|
| 206 |
*
|
| 207 |
* @param unknown_type $form
|
| 208 |
* @return unknown
|
| 209 |
*/
|
| 210 |
function theme_entries($form) {
|
| 211 |
$rows = array();
|
| 212 |
|
| 213 |
$header = build_entries_header($form);
|
| 214 |
|
| 215 |
foreach (element_children($form['qty']) as $key) {
|
| 216 |
$row_data[] = drupal_render($form['line'][$key]) . drupal_render($form['qty'][$key]);
|
| 217 |
$row_data[] = drupal_render($form['item_desc'][$key]) . drupal_render($form['item'][$key]); //hidden
|
| 218 |
|
| 219 |
if (isset($form['completed_date'][0])) {
|
| 220 |
$row_data[] = drupal_render($form['completed_date'][$key]);
|
| 221 |
}
|
| 222 |
|
| 223 |
if (isset($form['extra'][0])) {
|
| 224 |
$row_data[] = drupal_render($form['extra'][$key]);
|
| 225 |
}
|
| 226 |
|
| 227 |
// Invoices etc.
|
| 228 |
if (isset($form['serials'][0])) {
|
| 229 |
$row_data[] = drupal_render($form['serials'][$key]);
|
| 230 |
}
|
| 231 |
|
| 232 |
// Goods receive has individual serials
|
| 233 |
if (isset($form['serial'][0])) {
|
| 234 |
$row_data[] = drupal_render($form['serial'][$key]);
|
| 235 |
}
|
| 236 |
|
| 237 |
$row_data[] = drupal_render($form['price'][$key]);
|
| 238 |
|
| 239 |
if (isset($form['delete'][0])) {
|
| 240 |
$row_data[] = drupal_render($form['delete'][$key]);
|
| 241 |
}
|
| 242 |
|
| 243 |
$rows[] = $row_data;
|
| 244 |
$row_data = array();
|
| 245 |
}
|
| 246 |
|
| 247 |
if (in_array(t('Price'), $header)) {
|
| 248 |
$row_data = array_fill(0, count($header) - 1, array());
|
| 249 |
$row_data[count($header) - 1] = drupal_render($form['total']);
|
| 250 |
$rows[] = $row_data;
|
| 251 |
}
|
| 252 |
|
| 253 |
$output .= '<div class="entries">'. theme('table', $header, $rows) . '</div>';
|
| 254 |
|
| 255 |
$output .= drupal_render($form);
|
| 256 |
|
| 257 |
return $output;
|
| 258 |
}
|
| 259 |
|
| 260 |
function build_entries_header($form, $item = 0) {
|
| 261 |
$header = array();
|
| 262 |
if (isset($form['qty'][$item])) {
|
| 263 |
$header[] = t('Qty');
|
| 264 |
}
|
| 265 |
if (isset($form['item_desc'][$item])) {
|
| 266 |
$header[] = t('Stock Code');
|
| 267 |
}
|
| 268 |
if (isset($form['completed_date'][$item])) {
|
| 269 |
$header[] = t('Date');
|
| 270 |
}
|
| 271 |
if (isset($form['extra'][$item])) {
|
| 272 |
$header[] = t('Extra Text');
|
| 273 |
}
|
| 274 |
if (isset($form['serials'][$item]) or isset($form['serial'][$item])) {
|
| 275 |
$header[] = t('Serial');
|
| 276 |
}
|
| 277 |
if (isset($form['price'][$item])) {
|
| 278 |
$header[] = t('Price');
|
| 279 |
}
|
| 280 |
if (isset($form['delete'][$item])) {
|
| 281 |
$header[] = t('Delete');
|
| 282 |
}
|
| 283 |
return $header;
|
| 284 |
}
|
| 285 |
|
| 286 |
/**
|
| 287 |
* Theme newline entries
|
| 288 |
*/
|
| 289 |
function theme_newline($form) {
|
| 290 |
$rows = array();
|
| 291 |
|
| 292 |
$header = build_entries_header($form, '#id');
|
| 293 |
|
| 294 |
if (isset($form['qty']['#id'])) {
|
| 295 |
$row_data[] = drupal_render($form['qty']);
|
| 296 |
}
|
| 297 |
if (isset($form['item_desc']['#id'])) {
|
| 298 |
$row_data[] = drupal_render($form['item_desc']);
|
| 299 |
}
|
| 300 |
if (isset($form['completed_date']['#id'])) {
|
| 301 |
$row_data[] = drupal_render($form['completed_date']);
|
| 302 |
}
|
| 303 |
if (isset($form['extra']['#id'])) {
|
| 304 |
$row_data[] = drupal_render($form['extra']);
|
| 305 |
}
|
| 306 |
if (isset($form['serials']['#id'])) {
|
| 307 |
$row_data[] = drupal_render($form['serials']);
|
| 308 |
}
|
| 309 |
if (isset($form['serial']['#id'])) {
|
| 310 |
$row_data[] = drupal_render($form['serial']);
|
| 311 |
}
|
| 312 |
if (isset($form['price']['#id'])) {
|
| 313 |
$row_data[] = drupal_render($form['price']);
|
| 314 |
}
|
| 315 |
|
| 316 |
$rows[] = $row_data;
|
| 317 |
|
| 318 |
$output .= '<div class="newline">'. theme('table', $header, $rows) . '</div>';
|
| 319 |
|
| 320 |
$output .= drupal_render($form);
|
| 321 |
|
| 322 |
return $output;
|
| 323 |
}
|
| 324 |
|
| 325 |
function theme_erp_amount($amount) {
|
| 326 |
return sprintf("$%-12.2f", $amount);
|
| 327 |
}
|
| 328 |
|
| 329 |
function erp_get_total($node) {
|
| 330 |
$total = 0;
|
| 331 |
if ($node->entries) {
|
| 332 |
for ($i=0; $i < count($node->entries['qty']); $i++) {
|
| 333 |
$total += $node->entries['qty'][$i] * $node->entries['price'][$i];
|
| 334 |
}
|
| 335 |
}
|
| 336 |
return $total;
|
| 337 |
}
|
| 338 |
|
| 339 |
function erp_cmp_by_weight($a, $b) {
|
| 340 |
$a = (isset($a['#weight']) ? $a['#weight'] : 0);
|
| 341 |
$b = (isset($b['#weight']) ? $b['#weight'] : 0);
|
| 342 |
return ($a < $b ? -1 : ($a == $b ? 0 : +1));
|
| 343 |
}
|
| 344 |
|
| 345 |
function erp_set_customer(&$node) {
|
| 346 |
if (!$node->customer_id) {
|
| 347 |
if ($_GET['customer']) {
|
| 348 |
$node->customer_id = $_GET['customer'];
|
| 349 |
}
|
| 350 |
elseif ($_SESSION['curr_customer']) {
|
| 351 |
$node->customer_id = $_SESSION['curr_customer'];
|
| 352 |
}
|
| 353 |
}
|
| 354 |
|
| 355 |
if ($node->customer_id && !isset($node->customer)) {
|
| 356 |
$customer = node_load($node->customer_id);
|
| 357 |
$node->customer = $customer->title;
|
| 358 |
}
|
| 359 |
}
|