public static function locate($address) { if (!self::$config) { self::$config = Kohana::$config->load('mapquest'); } if (!is_array($address)) { $address = array($address); } $address = array_map(function ($value) { return trim(preg_replace('/^.*unit [0-9]+,/i', '', str_replace("\n", ', ', $value))); }, $address); reset($address); $list = array(); $response = array(); while ($item = current($address)) { $list[$item] = key($address); $state = substr($item, strrpos($item, ',') + 1); $item = array('country' => 'AU', 'street' => $item); if ($state) { $item['state'] = $state; } $data[] = $item; next($address); if (!key($address) && $list || count($list) >= self::$config->get('batch_size')) { $curl = curl_init(); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $url = self::$config->get('service_url') . '?key=' . self::$config->get('api_key'); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('locations' => $data))); $result = curl_exec($curl); curl_close($curl); $result = json_decode($result, true); try { foreach ($result['results'] as $item) { $key = Arr::get($list, Arr::path($item, 'providedLocation.street')); if ($key === NULL) { continue; } $location = array_shift($item['locations']); $response[$key] = Arr::get($location, 'latLng'); } } catch (Exception $e) { } $list = array(); $data = array(); } } if (count($address) == 1 && isset($address[0])) { return $response[0]; } else { return $response; } }
public function action_index() { $id = $this->request->param('id'); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($id))); if (!$job) { throw new HTTP_Exception_404('Not found'); } if (!Group::current('show_all_jobs') && !in_array((int) User::current('company_id'), Arr::get($job, 'companies', array()), true) && !in_array((int) User::current('company_id'), Arr::get($job, 'ex', array()), true)) { throw new HTTP_Exception_403('Forbidden'); } if (!Group::current('show_all_jobs') && !in_array((int) User::current('company_id'), Arr::get($job, 'companies', array()), true)) { Columns::set_readonly(); } $job['attachments'] = DB::select()->from('attachments')->where('job_id', '=', $id)->and_where('uploaded', '>', '0')->order_by('folder', 'asc')->order_by('numbering', 'asc')->execute()->as_array('id'); if (isset($_GET['delete']) && Group::current('allow_assign')) { $file_id = intval($_GET['delete']); if (isset($job['attachments'][$file_id])) { if (file_exists(DOCROOT . 'storage/' . $file_id)) { unlink(DOCROOT . 'storage/' . $file_id); } $attachment = $job['attachments'][$file_id]; $data = array('filename' => $attachment['folder'] . ' / ' . $attachment['fda_id'] . ' / ' . $attachment['address'] . ' / ' . $attachment['filename'], 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'action' => 2); Database::instance()->begin(); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); DB::delete('attachments')->where('id', '=', $file_id)->execute(); Database::instance()->commit(); die(json_encode(array('success' => true))); } else { throw new HTTP_Exception_404('Not found'); } } $users = array(); $job['region'] = DB::select('name')->from('regions')->where('id', '=', $job['region'])->execute()->get('name'); $tabs = DB::select()->from('job_tabs')->execute()->as_array('id'); foreach ($job['attachments'] as $attachment) { $users[$attachment['user_id']] = 1; } $job_types = DB::select('id', 'name')->from('job_types')->order_by('name', 'asc')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->order_by('name', 'asc')->execute()->as_array('id', 'name'); $submissions = array(); if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->find(array('job_key' => $id))->sort(array('update_time' => -1, 'user_id' => 1, 'key' => 1)); } else { $u = DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'); $result = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'user_id' => array('$in' => $u)))->sort(array('update_time' => -1, 'user_id' => 1, 'key' => 1)); } foreach ($result as $submission) { $users[$submission['user_id']] = 1; $key = substr($submission['key'], 5); $data = array('id' => $submission['_id'], 'key' => str_replace('.', '-', $submission['key']), 'user_id' => $submission['user_id'], 'time' => $submission['update_time'], 'name' => Columns::get_name($key), 'type' => Columns::get_type($key), 'active' => Arr::get($submission, 'active'), 'value' => $submission['value']); if (isset($submission['version'])) { $data['version'] = $submission['version']; } if (Group::current('allow_assign') && Arr::get($submission, 'active') == 1) { $tabs[Columns::get_tab($key)]['submissions'][$key] = 1; $submissions[$submission['key']][] = $data; } $submissions['list'][(string) $data['id']] = $data; } foreach (Columns::get_visible() as $key => $name) { $tabs[Columns::get_tab($key)]['columns'][$key] = $name; } User::get(array_keys($users)); if ($_POST) { $update_time = time(); if (!Arr::path($_FILES, 'attachment.error', -1) && is_uploaded_file($_FILES['attachment']['tmp_name'])) { $data = array('filename' => $_FILES['attachment']['name'], 'mime' => $_FILES['attachment']['type'], 'uploaded' => $update_time, 'user_id' => User::current('id'), 'job_id' => $id); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); if ($image_id && move_uploaded_file($_FILES['attachment']['tmp_name'], DOCROOT . 'storage/' . $image_id)) { Database::instance()->commit(); Messages::save("File " . $data['filename'] . ' was successfully uploaded!', 'success'); } else { Database::instance()->rollback(); } } if (Group::current('allow_assign')) { $update = array(); $archive = array(); foreach (Arr::get($_POST, 'data', array()) as $key => $value) { $value = Columns::parse($value, Columns::get_type($key)); if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } } $list = Arr::get($submissions, 'list', array()); $pending = array(); $accepted = array(); $ignored = array(); foreach ($list as $submission) { $key = 'submission-' . str_replace('-', '.', $submission['key']); $value = Arr::path($_POST, $key); if ($value !== NULL) { $pending[] = $submission['id']; if ($value) { $accepted[] = $submission['id']; } else { $ignored[] = $submission['id']; } } } foreach (Arr::get($_POST, 'submission-data', array()) as $key => $value) { if ($value && isset($list[$value])) { $value = Arr::path($list, $value . '.value'); if ($key == 177 || $key == 205) { $value = $value ? 1 : 0; } if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; unset($update['$unset']['data.' . $key]); } else { $update['$unset']['data.' . $key] = 1; unset($update['$set']['data.' . $key]); } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } } } if ($pending) { Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => $ignored)), array('$set' => array('admin_id' => User::current('id'), 'process_time' => $update_time), '$unset' => array('active' => 1)), array('multiple' => 1)); Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => $accepted)), array('$set' => array('admin_id' => User::current('id'), 'process_time' => $update_time, 'active' => -1)), array('multiple' => 1)); } $ass = false; $values = array(); foreach (Arr::get($_POST, 'assigned', array()) as $key => $value) { if (Arr::path($job, 'assigned.' . $key) != $value) { if ($ass < 1) { $ass = $value ? 1 : -1; } $values[] = array('time' => $update_time, 'user_id' => User::current('id'), 'company_id' => max($value, 0), 'job_id' => $job['_id'], 'type' => $key); $unassigned = Arr::path($job, 'assigned.' . $key); if ($value) { $job['assigned'][$key] = $value; $users = DB::select('id')->from('users')->where('company_id', '=', $value)->execute()->as_array(NULL, 'id'); $update['$set']['data.266'] = $update_time; $message = '1 ticket was allocated on ' . date('d-m-Y H:i', $update_time) . '. <a href="' . URL::base() . 'search/view/' . $job['_id'] . '">View ticket</a>'; $insert = DB::insert('notifications', array('user_id', 'message')); foreach ($users as $user) { $insert->values(array($user, $message)); } $insert->execute(); } else { unset($job['assigned'][$key]); } if ($unassigned) { $users = DB::select('id')->from('users')->where('company_id', '=', $unassigned)->execute()->as_array(NULL, 'id'); $subs = Database_Mongo::collection('submissions')->findOne(array('job_key' => $job['_id'], 'active' => 1)); if ($subs) { Messages::save('Unable to unassign the job - please, approve or reject all submissions first!', 'danger'); } else { $subs = Database_Mongo::collection('submissions')->find(array('job_key' => $job['_id'], 'user_id' => array('$in' => $users), 'financial' => array('$exists' => 0), 'active' => -1))->sort(array('process_time' => 1)); $list = array(); foreach ($subs as $sub) { $list[$sub['key']] = $sub['_id']; } $financial = DB::select('id')->from('job_columns')->where('financial', '>', 0)->execute()->as_array(NULL, 'id'); $result = array(); foreach ($financial as $column) { if (isset($list['data.' . $column])) { $result[$column] = $list['data.' . $column]; } } Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => array_values($result))), array('$set' => array('financial_time' => 0)), array('multiple' => 1)); } } } } if ($ass) { $update['$set']['assigned'] = $job['assigned']; $insert = DB::insert('assign_log', array_keys($values[0])); foreach ($values as $value) { $insert->values($value); } $insert->execute(); $result = Database_Mongo::collection('submissions')->distinct('user_id', array('job_key' => $job['_id'])); if ($result) { $result = DB::select('company_id')->distinct(true)->from('users')->where('id', 'IN', $result)->and_where('company_id', '>', 0)->execute()->as_array(NULL, 'company_id'); } $companies = array_flip($result); foreach ($companies as $key => $value) { $companies[$key] = 1; } $update['$set']['ex'] = array_keys($companies); } if (!Arr::get($update, '$unset')) { unset($update['$unset']); } $companies = array(); if (isset($job['assigned'])) { foreach ($job['assigned'] as $company) { if ($company) { $companies[$company] = 1; } } } foreach (Columns::$settings as $key => $value) { if (!in_array($key, Columns::$settings_read_only, true)) { if (Arr::get($job, $key) != Arr::get($_POST, $key)) { $value = Arr::get($_POST, $key) ? 1 : 0; $update[$value ? '$set' : '$unset'][$key] = 1; $job[$key] = $value; } } } $discrepancies = Database_Mongo::collection('discrepancies')->find(array('job_key' => $job['_id']))->sort(array('update_time' => -1))->getNext(); if (Group::current('allow_reports') && $discrepancies) { $fl = 0; $set = array(); $ignores = Arr::get($_POST, 'ignore-discrepancy'); foreach ($discrepancies['data'] as $key => $values) { $val = Arr::get($values, 'ignore') ? 1 : 0; $new_val = Arr::get($ignores, $key) ? 1 : 0; if ($val != $new_val) { if ($new_val) { if (!$fl) { $fl = -1; } $set['$set']['data.' . $key . '.ignore'] = 1; $discrepancies['data'][$key]['ignore'] = 1; } else { if ($values['old_value'] != Arr::get($job['data'], $key, '')) { $fl = 1; } $set['$unset']['data.' . $key . '.ignore'] = 1; $discrepancies['data'][$key]['ignore'] = 0; } } } if ($set) { Database_Mongo::collection('discrepancies')->update(array('_id' => new MongoId($discrepancies['_id'])), $set); } if ($fl > 0 && !Arr::get($job, 'discrepancies')) { $update['$set']['discrepancies'] = 1; } elseif ($fl <= 0 && Arr::get($job, 'discrepancies')) { $fl = true; foreach ($discrepancies['data'] as $key => $values) { if (!Arr::get($values, 'ignore')) { $value = $values['old_value']; $new_value = Arr::get($job['data'], $key, ''); if ($key == 44) { $value = preg_replace('/[^a-z]/i', '', strtolower($value)); $new_value = preg_replace('/[^a-z]/i', '', strtolower($new_value)); } if ($value != $new_value) { $fl = false; } } } if ($fl) { $update['$unset']['discrepancies'] = 1; } } } if ($update) { Utils::calculate_financial($job); $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($update, array('$set', 'data.44'), ''))); if ($status == 'built' && !Arr::path($job, 'data.264')) { $update['$set']['data.264'] = $update_time; } if ($status == 'tested' && !Arr::path($job, 'data.265')) { $update['$set']['data.265'] = $update_time; if (!Arr::path($job, 'data.264')) { $update['$set']['data.264'] = $update_time; } } $update['$set']['companies'] = array_keys($companies); $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); if ($companies && $status == Enums::STATUS_UNALLOC) { $update['$set']['status'] = Enums::STATUS_ALLOC; } elseif (!$companies && $status == Enums::STATUS_ALLOC) { $update['$unset']['status'] = 1; } $update['$set']['last_update'] = $update_time; if (isset($update['$set']['data.8'])) { $update['$set']['address'] = MapQuest::parse($update['$set']['data.8']); } elseif (isset($update['$unset']['data.8'])) { $update['$unset']['address'] = 1; } Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = $update_time; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } Messages::save("Changes were succesfully saved!", 'success'); } else { Messages::save("No changes were submitted", 'warning'); } if ($pending) { $count = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => 1))->count(); $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); if ($status == Enums::STATUS_PENDING && !$count) { $update = array('$set' => array('last_update' => $update_time, 'status' => Enums::STATUS_COMPLETE)); Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } } elseif (in_array(Arr::get($job, 'status', Enums::STATUS_UNALLOC), array(Enums::STATUS_UNALLOC, Enums::STATUS_ALLOC))) { if ($companies) { $status = Enums::STATUS_ALLOC; } else { $status = Enums::STATUS_UNALLOC; } if ($status != Arr::get($job, 'status', Enums::STATUS_UNALLOC)) { $update = array('$set' => array('last_update' => $update_time)); if ($status == Enums::STATUS_UNALLOC) { $update['$unset']['status'] = 1; } else { $update['$set']['status'] = $status; } Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } } elseif ($ass == 1 && in_array(Arr::get($job, 'status', Enums::STATUS_UNALLOC), array(Enums::STATUS_COMPLETE, Enums::STATUS_ARCHIVE))) { $update = array('$set' => array('last_update' => $update_time, 'status' => Enums::STATUS_ALLOC)); Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } if ($submissions) { Messages::save(count($pending) . '/' . count($submissions['list']) . ' submission(s) were processed.', 'info'); } } else { $submissions = array(); foreach (Arr::get($_POST, 'data', array()) as $key => $value) { $value = Columns::parse($value, Columns::get_type($key)); if (Arr::path($job, 'data.' . $key) != $value) { $submissions[$key] = $value; } } if ($submissions) { $submission = array('job_key' => $id, 'user_id' => User::current('id'), 'active' => 1, 'update_time' => $update_time); if (Arr::get($_POST, 'location')) { $submission['location'] = $_POST['location']; } $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); $update = array(); $approval = false; $archive = array(); foreach ($submissions as $key => $value) { if (Columns::get_direct($key)) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = -1; $submission['process_time'] = $submission['update_time']; Database_Mongo::collection('submissions')->insert($submission); unset($submission['process_time']); unset($submission['_id']); } else { $approval = true; if ($status != Enums::STATUS_PENDING) { $status = $update['$set']['status'] = Enums::STATUS_PENDING; } $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } if ($update) { $update['$set']['last_update'] = $update_time; Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = $update_time; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } } Messages::save("Changes were succesfully submitted. " . ($approval ? 'Manager will review changes and confirm them.' : ''), 'success'); } else { Messages::save("No changes were submitted", 'warning'); } } $this->redirect('/search'); } $values = array(); if (!Group::current('allow_assign')) { $last = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => 1, 'user_id' => array('$in' => DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'))))->sort(array('update_time' => 1)); foreach ($last as $submission) { $values[str_replace('.', '', $submission['key'])] = $submission['value']; } } $job['discr'] = array(); if (Group::current('allow_reports')) { $job['discr'] = Database_Mongo::collection('discrepancies')->find(array('job_key' => $id))->sort(array('update_time' => -1))->getNext(); } if (Group::current('time_machine')) { $result = Database_Mongo::collection('archive')->find(array('job_key' => $job['_id']))->sort(array('update_time' => -1)); $archive = array(); $ids = array(); foreach ($result as $item) { $archive[] = $item; $ids[Arr::get($item, 'user_id', 0)] = 1; } User::get(array_keys($ids)); } if (Group::current('allow_finance')) { $query = DB::select('company_id', 'payment_time', 'admin_id', array('payments.amount', 'total'), array('payment_jobs.amount', 'amount'))->from('payment_jobs')->join('payments')->on('payment_jobs.payment_id', '=', 'payments.id')->where('job_key', '=', $job['_id'])->order_by('payment_time', 'desc'); if (!Group::current('show_all_jobs')) { $query->and_where('company_id', '=', User::current('company_id')); } $job['payments'] = $query->execute()->as_array(); $ids = array(); foreach ($job['payments'] as $payment) { $ids[$payment['admin_id']] = 1; } if ($ids) { User::get(array_keys($ids)); } } $forms = array(); if (Group::current('allow_custom_forms')) { $forms = array('job' => $job['_id']); if (!Group::current('show_all_jobs')) { $forms['company'] = User::current('company_id'); } $result = Database_Mongo::collection('forms-data')->find($forms, array('data' => 0))->sort(array('last_update' => -1)); $forms = array(); $ids = array(); foreach ($result as $form) { $ids[$form['form_id']] = 1; $forms[] = $form; } $list = array(); foreach ($ids as $key => $dummy) { $list[] = new MongoId($key); } $result = Database_Mongo::collection('forms')->find(array('_id' => array('$in' => $list)), array('name' => 1)); $list = array(); foreach ($result as $form) { $list[strval($form['_id'])] = $form['name']; } foreach ($forms as $key => $form) { if (isset($list[$form['form_id']])) { $forms[$key]['name'] = $list[$form['form_id']]; } } } $view = View::factory('Jobs/View')->bind('job', $job)->bind('tabs', $tabs)->bind('job_types', $job_types)->bind('companies', $companies)->bind('submissions', $submissions)->bind('values', $values)->bind('archive', $archive)->bind('forms', $forms); $this->response->body($view); }
public function action_process() { if (!isset($_POST['pos'])) { die('Error: file position not defined!'); } $pos = $_POST['pos']; $region = Arr::get($_POST, 'region', 0); $date = strtotime(Arr::get($_POST, 'date')); $tod = Arr::get($_POST, 'tod', 0); if ($tod) { $date += 18 * 60 * 60; } $time = $tod ? 'EOD' : 'SOD'; if ($region > 0) { $reg_name = DB::select('name')->from('regions')->where('id', '=', $region)->execute()->get('name'); } else { $reg_name = 'PARTIAL'; } ini_set("memory_limit", "512M"); $filename = $this->request->param('id'); if (!$filename || !file_exists(DOCROOT . 'files/' . $filename)) { die(json_encode(array('success' => true, 'error' => 'Warning: Import aborted - file not found!'))); } $file = fopen(DOCROOT . '/files/' . $filename, 'r'); $header = fgetcsv($file); $columns = array(-1 => 'Ticket Of Work', 0 => 'Please, select a column...', -2 => '<<< IGNORE >>>'); $result = DB::select('id', 'name')->from('job_columns')->execute()->as_array('id', 'name'); foreach ($result as $key => $value) { $columns[$key] = $value; } $map = array('ticketofwork' => -1); foreach ($result as $key => $name) { $id = preg_replace('/[^a-z0-9]/', '', strtolower($name)); if (isset($map[$id])) { if (is_array($map[$id])) { $map[$id][] = $key; } else { $map[$id] = array($map[$id], $key); } } else { $map[$id] = $key; } } foreach ($header as $value) { $id = str_replace("(YYYY-MM-DD)", '', $value); $id = str_replace("(in m)", '', $id); $id = preg_replace('/[^a-z0-9]/', '', strtolower($id)); foreach ($columns as $key => $name) { if (preg_replace('/[^a-z0-9]/', '', strtolower($name)) == $id) { unset($columns[$key]); break; } } } $keys = array(); $data = Arr::get($_POST, 'data', array()); foreach ($header as $key => $value) { $id = str_replace("(YYYY-MM-DD)", '', $value); $id = str_replace("(in m)", '', $id); $id = preg_replace('/[^a-z0-9]/', '', strtolower($id)); if (isset($map[$id])) { if (is_array($map[$id])) { $keys[$key] = array_shift($map[$id]); if (!$map[$id]) { unset($map[$id]); } } else { $keys[$key] = $map[$id]; unset($map[$id]); } } else { if (isset($data[$key]) && isset($columns[$data[$key]])) { $keys[$key] = $data[$key]; } } } if (count($keys) != count($header)) { die(json_encode(array('success' => true, 'error' => 'Error: Unable to map all columns!'))); } $primary_key_id = -1; foreach ($keys as $key => $id) { if ($id == -1) { $primary_key_id = $key; } } if ($primary_key_id < 0) { die(json_encode(array('success' => true, 'error' => 'Error: Unable to map Ticket ID!'))); } else { unset($keys[$primary_key_id]); } if ($pos) { fseek($file, $pos); } $types = DB::select('id', 'type')->from('job_columns')->where('csv', '=', 1)->execute()->as_array('id', 'type'); $static = DB::select('id', 'name')->from('job_columns')->where('csv', '=', 1)->and_where('show_reports', '=', 1)->execute()->as_array('id', 'name'); $start = microtime(true); $inserted = 0; $updated = 0; $skipped = 0; $deleted = 0; $update_time = time(); $jobs = Database_Mongo::collection('jobs'); $archive = Database_Mongo::collection('archive'); $ids = Session::instance()->get($filename); if ($region > 0 && $ids === NULL && !$pos) { $result = $jobs->find(array('region' => $region), array('_id')); $ids = array(); while ($row = $result->next()) { $ids[$row['_id']] = 1; } } while (true) { $row = fgetcsv($file); $id = Arr::get($row, $primary_key_id); if ($id && strlen($id) == 15 && substr($id, 0, 3) == 'T1W') { try { unset($row[$primary_key_id]); $data = array(); foreach ($row as $key => $value) { if ($keys[$key] > 0) { switch (Columns::get_type($keys[$key])) { case 'date': case 'datetime': $value = $value ? strtotime(str_replace('/', '-', $value)) : ''; break; case 'int': $value = intval($value); break; case 'float': $value = floatval($value); break; default: $value = iconv("CP1251", 'UTF-8//ignore', $value); break; } $data[$keys[$key]] = $value; } } $job = $jobs->findOne(array('_id' => $id)); if ($job) { $diff = array(); $new = array(); foreach ($data as $key => $value) { $old = Arr::get($job['data'], $key, ''); if (($old || $value) && $old != $value) { if (!Columns::get_persistent($key)) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } } $diff[$key] = array('old_value' => Arr::get($job['data'], $key), 'new_value' => $value ?: ''); } } if (Arr::get($job, 'removed')) { $new['$unset']['removed'] = 1; } if ($diff || $new) { if (isset($data[44])) { $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($diff, '44.old_value'))); $status2 = preg_replace('/[^a-z]/', '', strtolower(Arr::path($new, array('$set', 'data.44')))); $status_updated = isset($new['$set']['data.44']); $discrepancy = array(); if ($status_updated && ($status == 'tested' && $status2 != 'tested' || $status == 'built' && ($status2 != 'built' && $status2 != 'tested') || $status != $status2 && in_array($status2, array('deferred', 'dirty', 'heldnbn'), true))) { $discrepancy[44] = $diff[44]; } foreach (Columns::get_track() as $key) { if ($key != 44 && isset($diff[$key]) && !$diff[$key]['new_value'] && $diff[$key]['old_value']) { $discrepancy[$key] = $diff[$key]; } } if ($discrepancy) { $discrepancy = array('job_key' => $id, 'update_time' => $update_time, 'user_id' => User::current('id'), 'filename' => $filename, 'data' => $discrepancy, 'fields' => array_keys($discrepancy)); Database_Mongo::collection('discrepancies')->insert($discrepancy); Database_Mongo::collection('jobs')->update(array('_id' => $id), array('$set' => array('discrepancies' => $discrepancy['_id']))); } } if ($new) { $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($new, array('$set', 'data.44')))); if ($status == 'built' && !Arr::path($job, 'data.264')) { $new['$set']['data.264'] = $update_time; } if ($status == 'tested' && !Arr::path($job, 'data.265')) { $new['$set']['data.265'] = $update_time; if (!Arr::path($job, 'data.264')) { $new['$set']['data.264'] = $update_time; } } $new['$set']['last_update'] = $update_time; if (isset($new['$set']['data.8'])) { $new['$set']['address'] = MapQuest::parse($new['$set']['data.8']); } elseif (isset($new['$unset']['data.8'])) { $new['$unset']['address'] = 1; } $jobs->update(array('_id' => $id), $new); foreach (array_keys($diff) as $key) { if (Columns::get_persistent($key)) { unset($diff[$key]); } } if ($diff) { $archive->insert(array('job_key' => $id, 'update_time' => $update_time, 'update_type' => 2, 'user_id' => User::current('id'), 'filename' => $filename, 'static' => array_intersect_key($data, $static), 'data' => $diff, 'fields' => array_keys($diff))); } $updated++; } else { $skipped++; } } else { $skipped++; } } elseif ($region > 0) { $inserted++; $job = array('_id' => $id, 'region' => $region, 'created' => $update_time, 'last_update' => $update_time, 'data' => $data); if (isset($data[8])) { $job['address'] = MapQuest::parse($data[8]); } $jobs->insert($job); $archive->insert(array('job_key' => $id, 'update_time' => $update_time, 'update_type' => 1, 'user_id' => User::current('id'), 'filename' => $filename, 'static' => array_intersect_key($data, $static), 'data' => array(), 'fields' => array())); } unset($ids[$id]); } catch (Exception $e) { print_r($e); die; } } $pos = ftell($file); if (feof($file) || microtime(true) - $start > 1) { break; } } $finished = filesize(DOCROOT . '/files/' . $filename) == $pos ? 1 : 0; if ($finished) { if ($ids && $region > 0 && !isset($_POST['skip-deleted'])) { $jobs->update(array('_id' => array('$in' => array_keys($ids))), array('$set' => array('removed' => "1")), array('multiple' => 1)); $result = $archive->find(array('job_key' => array('$in' => array_keys($ids))), array('job_key', 'update_type', 'update_time')); $list = array(); while ($value = $result->next()) { if (!isset($list[$value['job_key']])) { $list[$value['job_key']] = array('date' => $value['update_time'], 'state' => $value['update_type']); } elseif ($list[$value['job_key']]['date'] < $value['update_time']) { $list[$value['job_key']]['state'] = $value['update_type']; } } $cnt = 0; foreach ($ids as $id => $v) { if (Arr::path($list, array($id, 'state'), 0) != 3) { $cnt++; $values = array('job_key' => $id, 'update_type' => 3, 'update_time' => $update_time, 'user_id' => User::current('id'), 'filename' => $filename, 'data' => array(), 'fields' => array()); $archive->insert($values); } } $deleted = $cnt; } if (Kohana::$environment == Kohana::PRODUCTION) { unlink(DOCROOT . 'files/' . $filename); } } else { Session::instance()->set($filename, $ids); } $result = array('success' => true, 'time' => microtime(true) - $start, 'memory' => memory_get_peak_usage(true), 'inserted' => $inserted, 'updated' => $updated, 'deleted' => $deleted, 'skipped' => $skipped, 'position' => $pos, 'progress' => $finished ? 100 : number_format($pos * 100 / filesize(DOCROOT . '/files/' . $filename)), 'done' => $finished); if ($finished) { $result['import_name'] = $reg_name . '_' . date('Ymd', $date) . '_' . $time; DB::update('regions')->set(array('last_update' => $date))->where('id', '=', $region)->execute(); } die(json_encode($result)); }
public function action_index() { $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $query = array(); if ($regs) { $query['region'] = array('$in' => array_values($regs)); } if (!Group::current('show_all_jobs')) { $query['$or'] = array(array('ex' => intval(User::current('company_id'))), array('companies' => intval(User::current('company_id')))); } $query['_id'] = array('$in' => $ids); $jobs = Database_Mongo::collection('jobs')->find($query); $static = array_flip(explode(',', Group::current('columns'))); $header = array('Ticket ID'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($ids) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } if (isset($static['last_update'])) { $header[] = 'Last update'; } if (isset($static['last_submit'])) { $header[] = 'Last submit'; } if (isset($static['status']) && Group::current('show_all_jobs')) { $header[] = 'Job status'; } if (isset($static['types'])) { $header[] = 'Assigned works'; } if (isset($static['companies'])) { $header[] = 'Assigned companies'; } if (isset($static['pending'])) { $header[] = 'Pending submissions'; } if (isset($static['attachments'])) { $header[] = 'Attachments'; } foreach (Columns::get_search() as $id => $type) { $header[] = Columns::get_name($id); } $list = array(); $request = array(); foreach ($jobs as $job) { if (!isset($job['lat'])) { $request[$job['_id']] = Arr::path($job, 'data.8'); } } if ($request) { $coords = MapQuest::locate($request); foreach ($coords as $key => $value) { Database_Mongo::collection('jobs')->update(array('_id' => $key), array('$set' => array('lng' => $value['lng'], 'lat' => $value['lat']))); } } else { $coords = array(); } foreach ($jobs as $ticket) { if (isset($coords[$ticket['_id']])) { $ticket['lng'] = $coords[$ticket['_id']]['lng']; $ticket['lat'] = $coords[$ticket['_id']]['lat']; } if (!isset($ticket['lat'])) { continue; } $row = array($ticket['_id']); if (isset($static['last_update'])) { $row[] = date('d-m-Y H:i', Arr::get($ticket, 'last_update', $ticket['created'])); } if (isset($static['last_submit'])) { $row[] = Arr::get($ticket, 'last_submit') ? date('d-m-Y H:i', $ticket['last_submit']) : ''; } if (isset($static['status']) && Group::current('show_all_jobs')) { $row[] = Arr::get(Enums::$statuses, Arr::get($ticket, 'status', 0), 'Unknown'); } if (isset($static['types'])) { if (Group::current('allow_assign')) { $row[] = implode(', ', array_intersect_key($types, Arr::get($ticket, 'assigned', array()))); } else { $row[] = implode(', ', array_intersect_key($types, array_filter(Arr::get($ticket, 'assigned', array()), function ($x) { return $x == User::current('company_id'); }))); } } if (isset($static['companies'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'assigned', array())))); } if (isset($static['pending'])) { $row[] = Arr::get($submissions, $ticket['_id']); } if (isset($static['attachments'])) { $row[] = Arr::get($attachments, $ticket['_id']); } foreach (Columns::get_search() as $id => $type) { $row[] = Arr::path($ticket, array('data', $id)) ? Columns::output($ticket['data'][$id], Columns::get_type($id), true) : ''; } $row = array_combine($header, $row); $row['lat'] = $ticket['lat']; $row['lng'] = $ticket['lng']; $list[] = $row; } if (!$list) { throw new HTTP_Exception_404('Not found'); } DB::delete('maps')->where('expire', '<', time())->execute(); $ids = DB::select('map_id')->distinct(true)->from('maps')->execute()->as_array('map_id', 'map_id'); do { $id = Text::random('alnum', 32); } while (isset($ids[$id])); $expire = time() + 60 * 60 * 24 * 7; $query = DB::insert('maps', array('map_id', 'job_key', 'lng', 'lat', 'info', 'expire')); foreach ($list as $values) { $lat = $values['lat']; $lng = $values['lng']; unset($values['lng']); unset($values['lat']); $query->values(array($id, $values['Ticket ID'], $lng, $lat, json_encode($values), $expire)); } $query->execute(); $this->redirect('/map.html#' . $id); }
public function action_index() { header('Content-type: text/plain'); /*$list = Database_Mongo::collection('archive')->distinct('job_key', array('update_time' => array('$gt' => strtotime('2015-09-01')), 'data.44.new_value' => array('$in' => array('TESTED', '[3] Tested')))); $list = Database_Mongo::collection('jobs')->distinct('_id', array('_id' => array('$in' => $list), 'data.12' => '4AAR')); echo implode("\n", $list); die();*/ set_time_limit(0); $jobs = Database_Mongo::collection('jobs')->find(); foreach ($jobs as $job) { Utils::calculate_financial($job); } die('Done'); $list = Database_Mongo::collection('jobs')->find(array('discrepancies' => array('$nin' => array(NULL, '', 0)))); $ids = array(); foreach ($list as $job) { $discr = Database_Mongo::collection('discrepancies')->find(array('job_key' => $job['_id']))->sort(array('update_time' => -1))->getNext(); $fl = true; foreach ($discr['data'] as $key => $value) { if ($value['old_value'] != Arr::get($job['data'], $key, '')) { $fl = false; continue; } } if ($fl) { $ids[] = $job['_id']; } } Database_Mongo::collection('jobs')->update(array('_id' => array('$in' => $ids)), array('$unset' => array('discrepancies' => 1)), array('multiple' => 1)); die('Done. Total ' . count($ids) . ' job(s)'); $jobs = Database_Mongo::collection('jobs')->find(); foreach ($jobs as $job) { $address = Arr::path($job, 'data.8'); $address = MapQuest::parse($address); Database_Mongo::collection('jobs')->update(array('_id' => $job['_id']), array('$set' => array('address' => $address))); } die('Done'); header('Content-type: application/json'); $result = Database_Mongo::collection('jobs')->find(array(), array('data.8' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = $job['data'][8]; } die(json_encode($jobs)); $start = microtime(true); $total = 0; $jobs = Database_Mongo::collection('jobs')->find(); foreach ($jobs as $job) { $new = array(); if (isset($job['assigned'])) { foreach ($job['assigned'] as $key => $company) { if ($company !== intval($company)) { $new['$set']['assigned.' . $key] = intval($company); } } } if (isset($job['ex'])) { foreach ($job['ex'] as $key => $company) { if ($company !== intval($company)) { $new['$set']['ex.' . $key] = intval($company); } } } if ($new) { Database_Mongo::collection('jobs')->update(array('_id' => $job['_id']), $new); $total++; } } echo $total; die; header("Content-type: text/plain"); $jobs = Database_Mongo::collection('jobs')->find(array(), array('data.245' => 1, 'data.43' => 1, 'data.190' => 1, 'data.191' => 1, 'data.192' => 1, 'created' => 1)); $archive = Database_Mongo::collection('archive'); $result = DB::select('id', 'company_id')->from('users')->where('company_id', '>', 0)->execute()->as_array('id', 'company_id'); $companies = array(); foreach ($result as $key => $value) { $companies[intval($key)] = $value; } $ids = array(); foreach ($jobs as $job) { if (Arr::path($job, 'data.245') == 'Yes') { $result = $archive->find(array('job_key' => $job['_id'], 'fields' => array('$in' => array(245, 43, 190, 191, 192))))->sort(array('update_time' => 1)); $indexes = array(); if (Arr::get($job['data'], 43)) { $indexes['t'] = 43; $types['t'][0] = $job['created']; } if (Arr::get($job['data'], 190)) { $indexes['a'] = 190; $types['a'][0] = $job['created']; } if (Arr::get($job['data'], 191)) { $indexes['b'] = 191; $types['b'][0] = $job['created']; } if (Arr::get($job['data'], 192)) { $indexes['c'] = 192; $types['c'][0] = $job['created']; } $finished = 0; $types = array(); foreach ($result as $item) { if (isset($companies[$item['user_id']])) { if (isset($item['data']['245'])) { $finished = $item['update_time']; } foreach ($indexes as $key => $value) { if (isset($item['data'][$value])) { $types[$key][$companies[$item['user_id']]] = $item['update_time']; } } } } if ($finished && $types) { $types['f'] = $finished; print_r($types); die; } } } die('Done in ' . round(microtime(true) - $start, 3) . 's.'); }