public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { Messages::save('Please, select at least one job!'); } else { $jobs = Database_Mongo::collection('jobs'); $submissions = Database_Mongo::collection('submissions')->distinct('job_key', array('job_key' => array('$in' => $ids), 'active' => 1)); $ids = array_values(array_diff($ids, $submissions)); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => $ids))); print_r($result->explain()); $count = 0; foreach ($result as $job) { if (!in_array(Arr::get($job, 'status'), array(Enums::STATUS_ARCHIVE, Enums::STATUS_COMPLETE))) { $jobs->update(array('_id' => $job['_id']), array('$set' => array('last_update' => time(), 'status' => Enums::STATUS_COMPLETE))); $count++; } } Messages::save($count . ' jobs were succesfully completed', 'success'); } $this->redirect('/search'); }
public function action_index() { $id = intval($this->request->param('id')); $attachment = DB::select()->from('attachments')->where('id', '=', $id)->execute()->current(); if (!preg_match('/^image\\/.*$/i', $attachment['mime']) || $attachment['folder'] == 'Signatures') { throw new HTTP_Exception_403('Forbidden'); } $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($attachment['job_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 (!file_exists(DOCROOT . 'storage/' . $id)) { throw new HTTP_Exception_404('Not found'); } list(, $data) = explode(',', file_get_contents('php://input'), 2); $src = imagecreatefromstring(file_get_contents(DOCROOT . 'storage/' . $id)); $image = imagecreatefromstring(base64_decode($data)); imagealphablending($src, true); imagesavealpha($src, true); imagecopyresampled($src, $image, 0, 0, 0, 0, imagesx($src), imagesy($src), imagesx($image), imagesy($image)); if ($attachment['mime'] == 'image/png') { imagepng($src, DOCROOT . 'storage/' . $id, 9); } else { imagejpeg($src, DOCROOT . 'storage/' . $id, 90); } if (file_exists(DOCROOT . 'storage/' . $id . '.thumb')) { unlink(DOCROOT . 'storage/' . $id . '.thumb'); } imagedestroy($src); imagedestroy($image); die(json_encode(array('success' => true))); }
public function action_fda() { $query = array(); if (!Group::current('allow_assign')) { $query['$or'] = array(array('companies' => intval(User::current('company_id'))), array('ex' => intval(User::current('company_id')))); } else { if (Arr::get($_GET, 'company')) { $company = is_array($_GET['company']) ? $_GET['company'] : explode(',', $_GET['company']); $company = array_map('intval', $company); if (count($company) == 1) { $company = array_shift($company); } $query['$or'] = array(array('companies' => is_array($company) ? array('$in' => $company) : $company), array('ex' => is_array($company) ? array('$in' => $company) : $company)); } if (Arr::get($_GET, 'region')) { $query['region'] = strval($_GET['region']); } } if (Arr::get($_GET, 'fsam')) { $fsam = is_array($_GET['fsam']) ? array_map('strval', $_GET['fsam']) : explode(',', $_GET['fsam']); $query['data.13'] = count($fsam) == 1 ? array_shift($fsam) : array('$in' => $fsam); } $list = Database_Mongo::collection('jobs')->distinct('data.14', $query ?: NULL); sort($list); die(json_encode($list)); }
public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { Messages::save('Please, select at least one job!'); } else { $jobs = Database_Mongo::collection('jobs'); $result = $jobs->find(array('_id' => array('$in' => $ids))); $count = 0; foreach ($result as $job) { if (Arr::get($job, 'status') == Enums::STATUS_ARCHIVE || Arr::get($job, 'status') == Enums::STATUS_COMPLETE || Arr::get($job, 'status') == Enums::STATUS_PENDING) { if ($job['status'] == Enums::STATUS_PENDING) { $submissions = Database_Mongo::collection('submissions')->find(array('job_key' => $job['_id'], 'active' => 1))->count(); if ($submissions) { continue; } } $update = array('$set' => array('last_update' => time())); if (Arr::get($job, 'assigned')) { $update['$set']['status'] = Enums::STATUS_ALLOC; } else { $update['$unset']['status'] = 1; } $jobs->update(array('_id' => $job['_id']), $update); $count++; } } Messages::save($count . ' jobs were succesfully reset to initial state', 'success'); } $this->redirect('/search'); }
private function get_results($all = false) { $archive = Database_Mongo::collection('discrepancies'); $filters = array(); if (Arr::get($_GET, 'ticket')) { $tickets = array(); foreach (explode(',', $_GET['ticket']) as $ticket) { if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $tickets[] = $ticket; } else { $tickets[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } $filters['job_key']['$in'] = $tickets; } if (Arr::get($_GET, 'file')) { $filters['filename'] = $_GET['file']; } if (Arr::get($_GET, 'start')) { $start = strtotime($_GET['start']); $filters['update_time']['$gt'] = $start; } if (isset($filters['end'])) { $end = strtotime($_GET['end']); $filters['update_time']['$lt'] = $end; } $result = $archive->find($filters)->sort(array('update_time' => -1, 'job_key' => 1)); if (!$all) { Pager::$count = $result->count(); $result->skip(Pager::offset())->limit(Pager::limit()); } $list = array(); $ids = array(); foreach ($result as $row) { $list[] = $row; $ids[$row['job_key']] = 1; } $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($ids)))); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = Arr::get($job, 'data'); } $items = array(); foreach ($list as $row) { $row['data'] = array_intersect_key($row['data'], Columns::get_visible()); $row['current'] = array(); $row['discr'] = 1; foreach ($row['data'] as $key => $value) { $row['current'][$key] = Arr::path($jobs, array($row['job_key'], $key)); if ($row['current'][$key] != $value['old_value']) { $row['discr'] = 0; } } foreach (Columns::get_static() as $static => $show) { $row['static'][$static] = Arr::path($jobs, array($row['job_key'], $static)); } $items[] = $row; } return $items; }
public function action_update() { $id = Arr::get($_REQUEST, 'id'); $name = Arr::get($_REQUEST, 'name'); $data = Arr::get($_REQUEST, 'data'); Database::instance()->begin(); if ($id) { $report = DB::select()->from('reports')->where('id', '=', $id)->execute()->current(); if (!$report) { throw new HTTP_Exception_404('Not found'); } DB::update('reports')->set(array('name' => $name))->where('id', '=', $id)->execute(); DB::delete('report_columns')->where('report_id', '=', $id)->execute(); } else { $id = Arr::get(DB::insert('reports', array('name'))->values(array($name))->execute(), 0); } if ($data) { $query = DB::insert('report_columns', array('report_id', 'id', 'name', 'type', 'visible')); Database_Mongo::collection('api')->insert($data); unset($data['_id']); foreach ($data as $key => $value) { $query->values(array($id, $key, Arr::get($value, 'name', ''), Arr::get($value, 'type', ''), Arr::get($value, 'visible', 'read'))); } $query->execute(); } Database::instance()->commit(); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $id))); }
public function action_index() { $id = strval(Arr::get($_GET, 'id')); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => $id), array('data' => 1)); if (!$job) { throw new HTTP_Exception_404('Not found'); } $point = strval(Arr::get($_GET, 'point')); $archive = Database_Mongo::collection('archive')->findOne(array('_id' => new MongoId($point)), array('update_time' => 1)); if (!$archive) { throw new HTTP_Exception_404('Not found'); } $result = Database_Mongo::collection('archive')->find(array('job_key' => $id, 'update_time' => array('$gt' => $archive['update_time'])))->sort(array('update_time' => -1)); $ids = array(); $values = array(); foreach ($result as $item) { $ids[] = $item['_id']; foreach (Arr::get($item, 'data', array()) as $key => $data) { $job['data'][$key] = $values[$key] = $data['old_value']; } } foreach ($values as $key => $value) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } } if ($new) { $new['$set']['last_update'] = $archive['update_time']; } $submissions = array(); $result = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'process_time' => array('$gt' => $archive['update_time']))); foreach ($result as $item) { if (Arr::path($job, $item['key'], '') != $item['value']) { $submissions[] = $item['_id']; } } if ($new) { Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => $submissions)), array('$unset' => array('process_time' => 1), '$set' => array('active' => 1))); Database_Mongo::collection('jobs')->update(array('_id' => $id), $new); Database_Mongo::collection('archive')->remove(array('_id' => array('$in' => $ids))); } $message = ''; if (count($ids)) { $message .= count($ids) . ' changes were removed. '; } if (count($submissions)) { $message .= count($submissions) . ' submissions were unapproved. '; } Messages::save('Ticket ' . $id . ' successfully rolled back to ' . date('d-m-Y H:i', $archive['update_time']) . '. ' . $message); die(json_encode(array('success' => true))); }
public function action_index() { $id = $this->request->param('id'); $location = Arr::get($_GET, 'location', ''); $type = Arr::get($_GET, 'type', 'other'); $title = Arr::get($_GET, 'title', 'other'); $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'); } switch ($type) { case 'photo-before': $type = 'Photos'; $filename = $id . '.' . Arr::path($job, 'data.9') . '.' . Arr::path($job, 'data.14') . '.before.%NUM%'; $title = ''; break; case 'photo-after': $type = 'Photos'; $filename = $id . '.' . Arr::path($job, 'data.9') . '.' . Arr::path($job, 'data.14') . '.after.%NUM%'; $title = ''; break; case 'jsa': $type = 'JSA-forms'; $filename = $id . '.' . Arr::path($job, 'data.9') . '.' . Arr::path($job, 'data.14') . '.JSA.%NUM%'; $title = ''; break; case 'waiver': $type = 'Waiver'; $filename = $id . '.' . Arr::path($job, 'data.9') . '.' . Arr::path($job, 'data.14') . '.Waiver.%NUM%'; $title = ''; break; case 'odtr': $title = ''; $type = 'otdr-traces'; $filename = ''; break; default: $type = 'Other'; $filename = ''; break; } $number = DB::select('numbering')->from('attachments')->where('job_id', '=', $id)->and_where('folder', '=', $type)->order_by('numbering', 'desc')->limit(1)->execute()->get('numbering'); $data = array('filename' => $filename, 'mime' => '', 'uploaded' => 0, 'user_id' => User::current('id'), 'job_id' => $id, 'folder' => $type, 'fda_id' => Arr::path($job, 'data.14'), 'address' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', Arr::path($job, 'data.8'))), '-'), 'title' => $title, 'numbering' => intval($number) + 1); $result = Arr::get(DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(), 0); if (file_exists(DOCROOT . 'storage/' . $result)) { unlink(DOCROOT . 'storage/' . $result); } die(json_encode(array('success' => true, 'id' => $result))); }
public function action_financial() { if (!Group::current('is_admin') && !(Group::current('allow_finance') && Group::current('show_all_jobs'))) { throw new HTTP_Exception_403('Forbidden'); } $jobs = Database_Mongo::collection('jobs')->find(); foreach ($jobs as $job) { set_time_limit(30); Utils::calculate_financial($job); } header('Content-type: application/json'); die(json_encode(array('success' => true))); }
public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { Messages::save('Please, select at least one job!'); } else { $jobs = Database_Mongo::collection('jobs'); $result = $jobs->find(array('_id' => array('$in' => $ids))); $count = 0; foreach ($result as $job) { if (Arr::get($job, 'status') != Enums::STATUS_PENDING && Arr::get($job, 'status') != Enums::STATUS_ARCHIVE) { $jobs->update(array('_id' => $job['_id']), array('$set' => array('last_update' => time(), 'status' => Enums::STATUS_ARCHIVE))); $count++; } } Messages::save($count . ' jobs were succesfully archived', 'success'); } $this->redirect('/search'); }
public function action_search() { $id = Arr::get($_GET, 'id'); if (!DB::select('id')->from('job_columns')->where('id', '=', $id)->execute()->get('id')) { throw new HTTP_Exception_403(); } $group = Arr::get($_GET, 'group'); if (!Group::get($group)) { throw new HTTP_Exception_403(); } $state = intval(Arr::get($_GET, 'state')); DB::query(Database::INSERT, DB::expr("INSERT INTO `group_columns` (`group_id`, `column_id`, `search`) VALUES (:group, :id, :state) ON DUPLICATE KEY UPDATE `search` = :state", array(':group' => $group, ':id' => $id, ':state' => $state))->compile())->execute(); if ($state == 2) { if (Columns::get_type($id) != 'text') { Database_Mongo::collection('jobs')->ensureIndex(array('data.' . $id => true), array('sparse' => true)); } } else { if (!DB::select('group_id')->from('group_columns')->where('column_id', '=', $id)->and_where('search', '=', 2)->execute()->get('group_id')) { Database_Mongo::collection('jobs')->deleteIndex(array('data.' . $id => true)); } } die(json_encode(array('success' => true))); }
public function action_remove() { $id = $this->request->param('id'); $form_data = Database_Mongo::collection('forms-data')->findOne(array('_id' => new MongoId($id))); if (!$form_data) { throw new HTTP_Exception_404('Not found'); } $image_id = intval($_GET['id']); if (!in_array($image_id, Arr::get($form_data, 'attachments', array()), true)) { throw new HTTP_Exception_404('Not found'); } $attachments = array_diff($form_data['attachments'], array($image_id)); $update_time = time(); Database_Mongo::collection('forms-data')->update(array('_id' => new MongoId($id)), array('$set' => array('last_update' => $update_time, 'attachments' => $attachments))); @unlink(DOCROOT . 'storage/' . $image_id); @unlink(DOCROOT . 'storage/' . $image_id . '.thumb'); die(json_encode(array('success' => true))); }
public function action_tickets() { $id = $this->request->param('id'); $id = preg_replace('/[^a-z0-9]/i', '', $id); $result = Database_Mongo::collection('archive')->aggregate(array(array('$match' => array('job_key' => array('$regex' => '.*' . $id . '.*', '$options' => 'i'))), array('$group' => array('_id' => '$job_key')), array('$sort' => array('_id' => 1)), array('$limit' => 10))); $tickets = array(); foreach ($result['result'] as $row) { $tickets[] = $row['_id']; } die(json_encode($tickets)); }
public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } $ids = array_keys(Arr::get($_POST, 'job', array())); $type = intval(Arr::get($_POST, 'type')); $company = intval(Arr::get($_POST, 'company')); if (!$ids) { Messages::save('Please, select at least one job!'); } elseif (!$type) { Messages::save('Please, select works type!'); } elseif (!$company) { Messages::save('Please, select company!'); } elseif (DB::select('id')->from('job_types')->where('id', '=', $type)->execute()->get('id') && ($company == -1 || DB::select('id')->from('companies')->where('id', '=', $company)->execute()->get('id'))) { $assign_time = time(); $jobs = Database_Mongo::collection('jobs'); $result = $jobs->find(array('_id' => array('$in' => $ids))); $count = 0; $values = array(); $users = array(); while ($job = $result->next()) { if (Arr::path($job, 'assigned.' . $type) != $company) { $update = array(); if (Arr::path($job, 'assigned.' . $type)) { $users = DB::select('id')->from('users')->where('company_id', '=', $job['assigned'][$type])->execute()->as_array(NULL, 'id'); $subs = Database_Mongo::collection('submissions')->findOne(array('job_key' => $job['_id'], 'active' => 1, 'user_id' => array('$in' => $users))); if ($subs) { continue; } else { $subs = Database_Mongo::collection('submissions')->find(array('job_key' => $job['_id'], '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'); $res = array(); foreach ($financial as $column) { if (isset($list['data.' . $column])) { $res[$column] = $list['data.' . $column]; } } Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => array_values($res))), array('$set' => array('financial_time' => 0)), array('multiple' => 1)); if (!in_array($job['assigned'][$type], Arr::get($job, 'ex', array()), true)) { $job['ex'][] = intval($job['assigned'][$type]); $update['$set']['ex'] = $job['ex']; } } } if ($company == -1) { unset($job['assigned'][$type]); } else { $job['assigned'][$type] = $company; $update['$set']['data.266'] = $assign_time; } if (Arr::get($job, 'assigned')) { $update['$set']['assigned'] = $job['assigned']; } else { $update['$unset']['assigned'] = 1; } $update['$set']['last_update'] = $assign_time; $companies = array(); if (Arr::get($job, 'assigned')) { foreach ($job['assigned'] as $cmp) { if ($cmp) { $companies[$cmp] = 1; } } } $update['$set']['companies'] = array_keys($companies); $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); if (in_array($status, array(Enums::STATUS_UNALLOC, Enums::STATUS_COMPLETE, Enums::STATUS_ARCHIVE))) { if ($companies) { $update['$set']['status'] = Enums::STATUS_ALLOC; } } elseif ($status == Enums::STATUS_ALLOC) { if (!$companies) { $update['$unset']['status'] = 1; } } else { $update['$set']['status'] = Enums::STATUS_COMPLETE; } $count++; $jobs->update(array('_id' => $job['_id']), $update); $values[] = array('time' => $assign_time, 'user_id' => User::current('id'), 'company_id' => max($company, 0), 'job_id' => $job['_id'], 'type' => $type); } } if ($values) { $insert = DB::insert('assign_log', array_keys($values[0])); foreach ($values as $value) { $insert->values($value); } $insert->execute(); } if ($company == -1) { Messages::save($count . ' jobs were succesfully unassigned', 'success'); } else { Messages::save($count . ' jobs were succesfully assigned to ' . Company::get($company, 'name'), 'success'); $users = DB::select('id')->from('users')->where('company_id', '=', $company)->execute()->as_array(NULL, 'id'); $message = $count . ' tickets were allocated on ' . date('d-m-Y H:i', $assign_time) . '. <a href="javascript:;" class="tickets-search-assign" data-date="' . date('d-m-Y H:i:s', $assign_time) . '">View ticket(s)</a>'; $insert = DB::insert('notifications', array('user_id', 'message')); foreach ($users as $user) { $insert->values(array($user, $message)); } $insert->execute(); } } $this->redirect('/search'); }
public function action_index() { if (!Group::current('allow_reports')) { throw new HTTP_Exception_403('Forbidden'); } $regions = DB::select('id', 'name')->from('regions')->execute()->as_array('id', 'name'); $groups = DB::select('id', 'name')->from('groups')->execute()->as_array('id', 'name'); $form = new Form(); $form->add('region', 'Region', Form::SELECT, array('' => 'Please, select region') + $regions, array('not_empty')); if (isset($_POST['group']) && isset($_POST['csv'])) { if (isset($_POST['job'])) { $query = array('_id' => array('$in' => array_keys($_POST['job']))); $region = array('name' => 'PARTIAL'); } else { if (!Arr::get($_POST, 'region')) { throw new HTTP_Exception_404('Not found'); } $region = DB::select('id', 'name')->from('regions')->where('id', '=', $_POST['region'])->execute()->current(); $query = array('region' => $region['id']); } $jobs = Database_Mongo::collection('jobs')->find($query); $columns = array(); $csv = $_POST['csv']; if ($_POST['group']) { if (Group::get($_POST['group'], 'is_admin')) { $result = DB::select('id')->from('job_columns')->execute()->as_array(NULL, 'id'); } else { $result = DB::select('column_id')->from('group_columns')->where('group_id', '=', $_POST['group'])->and_where('permissions', '>', 0)->execute()->as_array(NULL, 'column_id'); } if ($csv != 'none') { foreach ($result as $column) { if ($csv == 'all' || !($csv == 'csv' xor Columns::get_csv($column))) { $columns[$column] = Columns::get_name($column); } } } } else { foreach (Arr::get($_POST, 'columns', array()) as $column => $value) { $columns[$column] = Columns::get_name($column); } } header("Content-type: text/csv"); header('Content-disposition: filename="' . date('Ymd') . '_EXEL_' . $region['name'] . '_EOD.csv"'); $file = tmpfile(); fputcsv($file, array(0 => 'Ticket Of Work') + $columns); while ($job = $jobs->next()) { $data[0] = $job['_id']; $i = 1; foreach ($columns as $key => $value) { $data[$i++] = iconv("CP1251", 'CP1251//ignore', Columns::output(Arr::get($job['data'], $key, ''), Columns::get_type($key), true)); } fputcsv($file, $data); } rewind($file); fpassthru($file); fclose($file); die; } $view = View::factory('Jobs/Export')->bind('regions', $regions)->bind('groups', $groups); $this->response->body($view); }
public static function calculate_financial($job) { $columns = array_map(function ($val) { return 'data.' . $val; }, array_keys(Columns::get_financial())); $submissions = Database_Mongo::collection('submissions')->find(array('job_key' => $job['_id'], 'key' => array('$in' => $columns)))->sort(array('update_time' => -1)); $columns = Columns::get_financial(); $data = array('rev' => 0, 'ecost' => 0, 'acost' => 0); $rates = array(); $result = DB::select()->from('rates')->execute(); foreach ($result as $row) { $rates[$row['company_id']][$row['region_id']][$row['column_id']] = $row['rate']; } $companies = DB::select('id', 'company_id')->from('users')->execute()->as_array('id', 'company_id'); $client = array(); foreach ($columns as $column => $max) { $client[$column] = isset($rates[0][$job['region']][$column]) ? $rates[0][$job['region']][$column] : (isset($rates[0][0][$column]) ? $rates[0][0][$column] : 0); } foreach (Columns::get_financial() as $column => $max) { $data['rev'] += Arr::path($job, array('data', $column), 0) * $client[$column]; } $used = array(); foreach ($submissions as $submission) { $company = Arr::get($companies, $submission['user_id']); if (!$company || !isset($rates[$company])) { continue; } $rate = $rates[$company]; $key = substr($submission['key'], 5); if (isset($used[$key])) { continue; } $used[$key] = true; if (isset($submission['paid'])) { $data['acost'] += round($submission['paid'] * $submission['rate'], 2); $data['ecost'] += round($submission['paid'] * $submission['rate'], 2); continue; } $rate = isset($rate[$job['region']][$key]) ? $rate[$job['region']][$key] : (isset($rate[0][$key]) ? $rate[0][$key] : 0); if (!$rate) { continue; } $value = min(floatval($submission['value']), $columns[$key]); $data['ecost'] += round($value * $rate, 2); } $data['apcost'] = $data['ecost']; if (!isset($used['205']) && Arr::get($job['data'], '205')) { $max = 0; $list = array_merge(Arr::get($job, 'companies', array()), Arr::get($job, 'ex', array())); foreach ($list as $company) { if (!isset($rates[$company])) { continue; } $rate = $rates[$company]; $max = max($max, isset($rate[$job['region']]['205']) ? $rate[$job['region']]['205'] : (isset($rate[0]['205']) ? $rate[0]['205'] : 0)); } $data['apcost'] += round(0.85 * $max, 2); } $data['egp'] = $data['rev'] - $data['ecost']; $data['gp'] = $data['rev'] - $data['acost']; $result = array(); if (Arr::get($job['data'], 258, 0) != $data['rev']) { $result['$set']['data.258'] = $data['rev']; } if (Arr::get($job['data'], 259, 0) != $data['ecost']) { $result['$set']['data.259'] = $data['ecost']; } if (Arr::get($job['data'], 260, 0) != $data['acost']) { $result['$set']['data.260'] = $data['acost']; } if (Arr::get($job['data'], 261, 0) != $data['egp']) { $result['$set']['data.261'] = $data['egp']; } if (Arr::get($job['data'], 262, 0) != $data['gp']) { $result['$set']['data.262'] = $data['gp']; } if (Arr::get($job['data'], 267, 0) != $data['apcost']) { $result['$set']['data.267'] = $data['apcost']; } $target = DB::select(DB::expr('SUM(`amount`) as total'))->from('payment_jobs')->where('job_key', '=', $job['_id'])->execute()->get('total'); if ($target) { if ($data['acost'] > $target) { if (!isset($job['partial'])) { $result['$set']['partial'] = 1; } if (isset($job['paid'])) { $result['$unset']['paid'] = 1; } } else { if (isset($job['partial'])) { $result['$unset']['partial'] = 1; } if (!isset($job['paid'])) { $result['$set']['paid'] = 1; } } } else { if (isset($job['partial'])) { $result['$unset']['partial'] = 1; } if (isset($job['paid'])) { $result['$unset']['paid'] = 1; } } if ($result) { Database_Mongo::collection('jobs')->update(array('_id' => $job['_id']), $result); } }
public function action_index() { $id = intval($this->request->param('id')); $attachment = DB::select()->from('attachments')->where('id', '=', $id)->execute()->current(); if (!$attachment) { throw new HTTP_Exception_404('Not found'); } if (Arr::get($attachment, 'uploaded')) { throw new HTTP_Exception_403('Forbidden'); } $job_id = Arr::get($attachment, 'job_id'); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($job_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 ($_FILES) { try { $file = Arr::get($_FILES, 'attachment', array()); $file['name'] = trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-\\.]/i', '-', Arr::get($file, 'name', ''))), '-'); preg_match_all("/([0-9]+)/", Arr::get($_SERVER, 'HTTP_CONTENT_RANGE', ''), $matches); $range = Arr::get($matches, 0); $size = Arr::get($range, 2, filesize($file['tmp_name'])); if (!is_uploaded_file($file['tmp_name'])) { die(json_encode(array('attachment' => array('name' => $file['name'], 'size' => $size, 'error' => 'Error!')))); } $src = fopen($file['tmp_name'], 'r'); $dest = fopen(DOCROOT . 'storage/' . $id, 'c'); fseek($dest, Arr::get($range, 0, 0)); $buf = fread($src, $size); fwrite($dest, $buf); fclose($dest); fclose($src); unlink($file['tmp_name']); if (!$range || Arr::get($range, 1) + 1 == Arr::get($range, 2)) { $data = array('filename' => $file['name'], 'mime' => $file['type'], 'uploaded' => time()); if ($attachment['filename']) { $data['filename'] = $attachment['filename']; $pos = strrpos($file['name'], '.'); if ($pos !== false) { $data['filename'] .= substr($file['name'], $pos); } } else { $data['filename'] = ($attachment['folder'] == 'Other' ? $attachment['title'] : '') . $file['name']; } $data['filename'] = str_replace('%NUM%', $attachment['numbering'], $data['filename']); Database::instance()->begin(); DB::update('attachments')->set($data)->where('id', '=', $id)->execute(); $filename = $data['filename']; $data = array('user_id' => User::current('id'), 'job_id' => $attachment['job_id'], 'uploaded' => $data['uploaded'], 'location' => $attachment['location'], 'filename' => $attachment['folder'] . ' / ' . $attachment['fda_id'] . ' / ' . $attachment['address'] . ' / ' . $data['filename'], 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); Database_Mongo::collection('jobs')->update(array('_id' => $attachment['job_id']), array('$unset' => array('downloaded' => 1), '$set' => array('last_update' => time()))); Messages::save("File " . $file['name'] . ' was successfully uploaded!', 'success'); $is_image = preg_match('/^image\\/.*$/i', $file['type']); die(json_encode(array('attachment' => array('name' => $file['name'], 'size' => $size, 'content' => '<table><tr>' . (Group::current('allow_assign') ? '<td><a href="' . URL::base() . 'search/view/' . $id . '?delete=' . $id . '" confirm="Do you really want to delete this attachment? This action can\'t be undone!!!" class="text-danger glyphicon glyphicon-remove remove-link"></a></td>' : '') . '<td><div class="td-image-center">' . ($is_image ? '<img src="' . URL::base() . 'download/thumb/' . $id . '" alt="Thumbnail" />' : '<img src="http://stdicon.com/' . $file['type'] . '?size=96&default=http://stdicon.com/text" />') . '</div></td><td><a data-id="' . $id . '" class="' . ($is_image && $attachment['folder'] != 'Signatures' ? 'image-attachments' : '') . '" href="' . URL::base() . 'download/attachment/' . $id . '">' . HTML::chars($attachment['folder']) . '<br/>' . HTML::chars($attachment['fda_id']) . '<br/>' . HTML::chars($attachment['address']) . '<br/>' . HTML::chars($filename) . '</a><br/> - Uploaded ' . date('d-m-Y H:i', $data['uploaded']) . ' by ' . User::current('login') . '</td></tr></table>', 'message' => Messages::render())))); } } catch (Exception $e) { die($e->getMessage()); } die(json_encode(array('attachment' => array('name' => $file['name'], 'size' => $size)))); } $view = View::factory("Jobs/UploadFile"); $this->response->body($view); }
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_mixed() { $query = array(); $key = false; if (Arr::get($_GET, 'fda')) { $key = 'data.14'; $values = $_GET['fda']; } elseif (Arr::get($_GET, 'fsam')) { $key = 'data.13'; $values = $_GET['fsam']; } elseif (Arr::get($_GET, 'fsa')) { $key = 'data.12'; $values = $_GET['fsa']; } if ($key) { if (!is_array($values)) { $values = explode(',', $values); } $values = array_map('strval', $values); $query[$key] = count($values) > 1 ? array('$in' => $values) : array_shift($values); } if (Arr::get($_GET, 'region')) { $query['region'] = $_GET['region']; } if (Arr::get($_GET, 'company')) { $company = $_GET['company']; if (!is_array($company)) { $company = explode(',', $company); } $company = array_map('intval', $company); $query['$or'] = array(array('companies' => count($company) > 1 ? array('$in' => $company) : current($company)), array('ex' => count($company) > 1 ? array('$in' => $company) : current($company))); } $types = array(); $result = Database_Mongo::collection('jobs')->find($query, array('data.44' => 1, 'created' => 1, 'data.190' => 1, 'data.191' => 1, 'data.192' => 1)); $start = Arr::get($_GET, 'start', 0) ? strtotime($_GET['start']) : 0; $end = Arr::get($_GET, 'end', 0) ? strtotime($_GET['end']) : 0; $jobs = array(); foreach ($result as $job) { $status = strtolower(preg_replace('/[^a-z]/i', '', Arr::path($job, 'data.44'))); $jobs[$job['_id']] = $status; if ($job['created'] >= $start && (!$end || $job['created'] <= $end) && in_array($status, array('built', 'tested'), true)) { if (Arr::path($job, 'data.192')) { $types[$job['_id']] = 3; } elseif (Arr::path($job, 'data.191')) { $types[$job['_id']] = 2; } elseif (Arr::path($job, 'data.190')) { $types[$job['_id']] = 1; } } } $query = array('job_key' => array('$in' => array_keys($jobs)), 'key' => array('$in' => array('data.190', 'data.191', 'data.192'))); if ($start) { $query['update_time']['$gte'] = $start; } if ($end) { $query['update_time']['$lte'] = $end; } $items = Database_Mongo::collection('submissions')->find($query)->sort(array('update_time' => 1)); foreach ($items as $item) { if ($jobs[$item['job_key']] == 'built' || $jobs[$item['job_key']] == 'tested') { switch ($item['key']) { case 'data.190': $types[$item['job_key']] = 1; break; case 'data.191': $types[$item['job_key']] = 2; break; case 'data.192': $types[$item['job_key']] = 3; break; } } } $jobs = array_diff_key($jobs, $types); $result = array('Type A' => 0, 'Type B' => 0, 'Type C' => 0, 'Not Buildable' => 0, 'Tickets Left' => 0); foreach ($types as $type) { switch ($type) { case 1: $result['Type A']++; break; case 2: $result['Type B']++; break; case 3: $result['Type C']++; break; } } foreach ($jobs as $status) { if (in_array($status, array('dirty', 'heldnbn', 'deferred'), true)) { $result['Not Buildable']++; } elseif (in_array($status, array('scheduled', 'inprogress', 'heldcontractor'), true)) { $result['Tickets Left']++; } } header('Content-type: application/json'); die(json_encode($result)); }
public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } header('Content-type: application/json'); $action = $this->request->param('id'); $result = array(); $columns = DB::select('id')->from('job_columns')->where('editable', '=', 1)->execute()->as_array('id', 'id'); switch ($action) { case 'get': $data = array(); $result['columns'] = array(); foreach ($columns as $column) { $value = array('id' => intval($column), 'name' => Columns::get_name($column)); $type = Columns::get_type($column); if (substr($type, 0, 4) == 'enum') { $id = substr($type, 5); $type = Enums::is_multi($id) ? 'multi' : 'list'; $value['values'] = array_values(Enums::get_values($id)); } $value['type'] = $type ?: 'string'; $result['columns'][] = $value; $data[] = 'data.' . $column; } $ids = explode(',', strval(Arr::get($_GET, 'id', ''))); $values = array(); if ($ids) { $query = array('_id' => array('$in' => $ids)); if (!Group::current('show_all_jobs')) { $query['companies'] = intval(User::current('company_id')); } $jobs = Database_Mongo::collection('jobs')->find($query, $data); foreach ($jobs as $job) { foreach ($job['data'] as $key => $value) { $job['data'][$key] = Columns::output($value, Columns::get_type($key), true); } $job['id'] = $job['_id']; unset($job['_id']); $values[] = $job; } } $result['jobs'] = $values; break; case 'set': if (User::current('login') !== Arr::get($_POST, 'username')) { die('Wrong username! Please, check it and submit data again.'); } $data = Arr::get($_POST, 'jobs'); $values = array(); foreach ($data as $job) { $id = strval(Arr::get($job, 'id', '')); foreach (Arr::get($job, 'data') as $key => $value) { if (isset($columns[$key])) { $values[$id][$key] = strval($value); } } } $query = array('_id' => array('$in' => array_keys($values))); if (!Group::current('show_all_jobs')) { $query['companies'] = intval(User::current('company_id')); } $data = array(); foreach ($columns as $column) { $data['data.' . $column] = 1; } $count = 0; $jobs = Database_Mongo::collection('jobs')->find($query, $data); foreach ($jobs as $job) { if (isset($values[$job['_id']])) { $id = $job['_id']; $new = array(); $archive = array(); foreach ($values[$id] as $key => $value) { $value = $value ? Columns::parse($value, Columns::get_type($key)) : ''; $old = Arr::path($job, 'data.' . $key); if (($value || $old) && $value != $old) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => $old, 'new_value' => $value); } } if ($new) { $new['$set']['last_update'] = time(); Database_Mongo::collection('jobs')->update(array('_id' => $id), $new); $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = time(); $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); $count++; } } } $result = array('success' => true, 'count' => $count); break; } die(json_encode($result)); }
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_upload() { $token = Arr::get($_GET, 'token'); $id = intval(Arr::get($_GET, 'id')); $pos = intval(Arr::get($_GET, 'pos')); if (!API::check($token)) { die(json_encode(array('success' => false, 'error' => 'forbidden'))); } $attachment = DB::select()->from('attachments')->where('id', '=', $id)->execute()->current(); if (!$attachment) { die(json_encode(array('success' => false, 'error' => 'not found'))); } if (Arr::get($attachment, 'uploaded')) { die(json_encode(array('success' => false, 'error' => 'not found'))); } $job_id = Arr::get($attachment, 'job_id'); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($job_id))); if (!$job) { die(json_encode(array('success' => false, 'error' => 'not found'))); } if (!Group::current('show_all_jobs') && !in_array((int) User::current('company_id'), Arr::get($job, 'companies', array()), true)) { die(json_encode(array('success' => false, 'error' => 'forbidden'))); } try { $filedata = base64_decode(file_get_contents('php://input')); $dest = fopen(DOCROOT . 'storage/' . $id, 'c'); fseek($dest, $pos); fwrite($dest, $filedata); $pos = ftell($dest); fclose($dest); if (isset($_GET['last'])) { $data = array('mime' => File::mime(DOCROOT . 'storage/' . $id), 'uploaded' => time()); Database::instance()->begin(); DB::update('attachments')->set($data)->where('id', '=', $id)->execute(); $data = array('user_id' => User::current('id'), 'job_id' => $attachment['job_id'], 'uploaded' => $data['uploaded'], 'location' => $attachment['location'], 'filename' => $attachment['folder'] . ' / ' . $attachment['fda_id'] . ' / ' . $attachment['address'] . ' / ' . $attachment['filename'], 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); Database_Mongo::collection('jobs')->update(array('_id' => $attachment['job_id']), array('$unset' => array('downloaded' => 1), '$set' => array('last_update' => time()))); die(json_encode(array('success' => true, 'time' => $data['uploaded']))); } } catch (Exception $e) { die(json_encode(array('success' => false, 'error' => 'exception'))); } die(json_encode(array('success' => true, 'position' => $pos))); }
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.'); }
public function action_tickets() { $ids = explode(',', Arr::get($_GET, 'id')); if (!$ids) { throw new HTTP_Exception_404('Not found!'); } //$zip_name = implode('-', $ids); $files = DB::select('id', 'filename', 'folder', 'fda_id', 'address')->from('attachments')->where('uploaded', '>', 0); if (count($ids) > 1) { $files->and_where('job_id', 'IN', $ids); } else { $ids = array_shift($ids); $files->and_where('job_id', '=', $ids); } $files = $files->order_by('filename', 'asc')->execute()->as_array(); $expire = time() + 600; $download_id = Text::random('alnum', 32); $list = array(); foreach ($files as $file) { if (file_exists(DOCROOT . 'storage/' . $file['id'])) { $pos = strrpos($file['filename'], '.'); if ($pos) { $ext = substr($file['filename'], $pos); $name = $file['folder'] . '/' . $file['fda_id'] . '/' . $file['address'] . '/' . substr($file['filename'], 0, $pos); } else { $ext = ''; $name = $file['folder'] . '/' . $file['fda_id'] . '/' . $file['address'] . '/' . $file['filename']; } if (isset($list[$name . $ext])) { $i = 1; while (isset($list[$name . '(' . $i . ')' . $ext])) { $i++; } $name = $name . '(' . $i . ')'; } $list[$name . $ext] = array('path' => $name . $ext, 'attachment_id' => $file['id'], 'size' => filesize(DOCROOT . 'storage/' . $file['id']), 'user_id' => User::current('id'), 'download_id' => $download_id, 'expires' => $expire); } } $total = 0; $files = array(); while (current($list)) { $file = current($list); $total += $file['size']; $files[] = $file; next($list); if (count($files) > 500 || !current($list) && $files) { $query = DB::insert('downloads', array_keys($file)); foreach ($files as $file) { $query->values(array_values($file)); } $query->execute(); $files = array(); } } if (is_array($ids)) { $query = array('_id' => array('$in' => $ids)); } else { $query = array('_id' => $ids); } Database_Mongo::collection('jobs')->update($query, array('$set' => array('downloaded' => '1', 'download-by' => User::current('id'), 'download-at' => time())), array('multiple' => 1)); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $download_id, 'count' => count($list), 'total' => $total))); }
public function action_index() { $start = Arr::get($_GET, 'start') ? strtotime($_GET['start']) : strtotime('this week', strtotime('this week') > time() ? strtotime('yesterday') : time()); $end = Arr::get($_GET, 'end') ? strtotime($_GET['end']) + 86399 : time(); $query = array('update_time' => array('$gt' => intval($start), '$lt' => intval($end))); if (Arr::get($_GET, 'app-start')) { $query['process_time']['$gt'] = strtotime($_GET['app-start']); } if (Arr::get($_GET, 'app-end')) { $query['process_time']['$lt'] = strtotime($_GET['app-end']) + 86399; } if (Group::current('allow_assign')) { $companies = DB::select('id', 'name')->from('companies')->order_by('name', 'asc')->execute()->as_array('id', 'name'); } if (!Group::current('allow_assign') || Arr::get($_GET, 'company')) { $query['user_id'] = array('$in' => DB::select('id')->from('users')->where('company_id', '=', Group::current('allow_assign') ? $_GET['company'] : User::current('company_id'))->execute()->as_array(NULL, 'id')); } $jobs = array(); if (Arr::get($_GET, 'finished')) { $jobs['data.245'] = $_GET['finished']; } if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $jobs['_id'] = array('$in' => $q); } elseif ($q) { $jobs['_id'] = $q[0]; } } if (Arr::get($_GET, 'fsa')) { $values = is_array($_GET['fsa']) ? $_GET['fsa'] : explode(',', $_GET['fsa']); $jobs['data.12'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fsam')) { $values = is_array($_GET['fsam']) ? $_GET['fsam'] : explode(',', $_GET['fsam']); $jobs['data.13'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fda')) { $values = is_array($_GET['fda']) ? $_GET['fda'] : explode(',', $_GET['fda']); $jobs['data.14'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'address')) { $jobs['data.8'] = new MongoRegex('/.*' . strval($_GET['address']) . '.*/mi'); } if ($jobs) { if (count($jobs) == 1 && isset($jobs['_id'])) { $query['job_key'] = $jobs['_id']; } else { $query['job_key'] = array('$in' => Database_Mongo::collection('jobs')->distinct('_id', $jobs)); } } $sort = array('job_key' => 1); if (!Arr::get($_GET, 'sort')) { $_GET['sort'] = array('-submission'); } foreach ($_GET['sort'] as $s) { $dir = substr($s, 0, 1) == '-' ? -1 : 1; $s = substr($s, 1); switch ($s) { case 'submission': $sort['update_time'] = $dir; break; case 'approval': $sort['process_time'] = $dir; break; } } $result = Database_Mongo::collection('submissions')->find($query)->sort($sort); $submissions = array(); $users = array(); foreach ($result as $submission) { $submissions[$submission['job_key']][] = $submission; $users[$submission['user_id']] = 1; } if ($users) { User::get(array_keys($users)); } if (isset($_GET['export'])) { header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $file = tmpfile(); $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'Approval Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } $headers[] = 'Column'; $headers[] = 'Value'; fputcsv($file, $headers); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('a' => Arr::path($job, 'data.8', ''), 'f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', '')); } foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $data = array($job, Arr::path($jobs, array($job, 'f')), Arr::path($jobs, array($job, 'l')), Arr::path($jobs, array($job, 'a')), date('d-m-Y H:i', $submission['update_time']), Arr::get($submission, 'process_time') ? date('d-m-Y H:i', $submission['process_time']) : '', User::get($submission['user_id'], 'login')); if (Group::current('allow_assign')) { $data[] = Arr::get($companies, User::get($submission['user_id'], 'company_id'), 'Unknown'); } $data[] = Columns::get_name($key); $data[] = Columns::output($submission['value'], Columns::get_type($key), true); fputcsv($file, $data); } } fseek($file, 0); fpassthru($file); fclose($file); die; } elseif (isset($_GET['export2'])) { //header('Content-type: text/plain'); header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $result = array(); $columns = array(); foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $result[$job][$submission['update_time']][$submission['user_id']][$key] = $submission['value']; $columns[$key] = Columns::get_name($key); } } $submissions = $result; ksort($columns); $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } /*$columns = Columns::get_visible();*/ foreach ($columns as $column) { $headers[] = $column; } $columns = array_keys($columns); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $file = tmpfile(); fputcsv($file, $headers); foreach ($submissions as $job => $list) { foreach ($list as $time => $values) { foreach ($values as $user => $submission) { $row = array($job, Arr::path($jobs, $job . '.f'), Arr::path($jobs, $job . '.l'), Arr::path($jobs, $job . '.a'), date('d-m-Y H:i', $time), User::get($user, 'login')); if (Group::current('allow_assign')) { $row[] = Arr::get($companies, User::get($user, 'company_id')); } foreach ($columns as $column) { $row[] = Columns::output(Arr::get($submission, $column, ''), Columns::get_type($column), true); } fputcsv($file, $row); } } } fseek($file, 0); fpassthru($file); fclose($file); //print_r($result); die; } elseif (isset($_GET['export3'])) { //header('Content-type: text/plain'); header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $result = array(); foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $result[$job][$submission['update_time']][$submission['user_id']][$key] = $submission['value']; } } $submissions = $result; $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } $columns = Columns::get_visible(); foreach ($columns as $column) { $headers[] = $column; } $columns = array_keys($columns); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $file = tmpfile(); fputcsv($file, $headers); foreach ($submissions as $job => $list) { foreach ($list as $time => $values) { foreach ($values as $user => $submission) { $row = array($job, Arr::path($jobs, $job . '.f'), Arr::path($jobs, $job . '.l'), Arr::path($jobs, $job . '.a'), date('d-m-Y H:i', $time), User::get($user, 'login')); if (Group::current('allow_assign')) { $row[] = Arr::get($companies, User::get($user, 'company_id')); } foreach ($columns as $column) { $row[] = Arr::get($submission, $column) ? Columns::output($submission[$column], Columns::get_type($column), true) : ''; } fputcsv($file, $row); } } } fseek($file, 0); fpassthru($file); fclose($file); //print_r($result); die; } $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $view = View::factory("Reports/Submissions")->bind('companies', $companies)->bind('submissions', $submissions)->bind('jobs', $jobs); $this->response->body($view); }
public function action_index() { $ids = array_keys(Arr::get($_POST, 'job', array())); $ids = array_filter($ids, function ($st) { return preg_match('/^T1W[0-9]{12}$/i', $st); }); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $users = DB::select('id', 'company_id')->from('users')->execute()->as_array('id', 'company_id'); $companies = array(); $result = Database_Mongo::collection('submissions')->find(array('job_key' => array('$in' => $ids), 'financial_time' => array('$gt' => 0)), array('user_id' => 1, 'job_key' => 1, 'paid' => 1, 'rate' => 1)); foreach ($result as $submission) { if (Arr::get($users, $submission['user_id'])) { $path = array($users[$submission['user_id']], $submission['job_key']); Arr::set_path($companies, $path, Arr::path($companies, $path, 0) + $submission['paid'] * $submission['rate']); } } $paid = array(); if ($companies) { $result = DB::select('job_key', 'company_id', DB::expr('SUM(`payment_jobs`.`amount`) as value'))->from('payment_jobs')->join('payments')->on('payment_id', '=', 'id')->where('job_key', 'IN', $ids)->group_by('job_key', 'company_id')->execute()->as_array(); foreach ($result as $payment) { $paid[$payment['company_id']][$payment['job_key']] = $payment['value']; } } $jobs = array_fill_keys($ids, array('c' => array(), 'p' => array())); foreach ($companies as $company => $list) { foreach ($list as $key => $value) { $jobs[$key]['c'][$company] = $value; } } foreach ($paid as $company => $list) { foreach ($list as $key => $value) { $jobs[$key]['p'][$company] = $value; } } if (Arr::get($_POST, 'company') && Arr::get($_POST, 'payment')) { $company = Arr::get($companies, $_POST['company']); if (!$company) { die(json_encode(array('success' => false))); } $payments = array_map('floatval', array_intersect_key($_POST['payment'], $company)); $payment = array('company_id' => intval($_POST['company']), 'admin_id' => User::current('id'), 'payment_time' => time(), 'amount' => array_sum($payments)); Database::instance()->begin(); $id = Arr::get(DB::insert('payments', array_keys($payment))->values(array_values($payment))->execute(), 0); $query = DB::insert('payment_jobs', array('payment_id', 'job_key', 'amount')); foreach ($company as $job => $value) { if (Arr::get($payments, $job)) { $query->values(array($id, $job, $payments[$job])); } } $query->execute(); $partial = array(); $paid = array(); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => $ids))); foreach ($result as $job) { $data = Utils::calculate_financial($job); if (isset($data['$set']['partial'])) { $partial[] = $job['_id']; } elseif (isset($data['$set']['paid'])) { $paid[] = $job['_id']; } } if ($partial) { Database_Mongo::collection('jobs')->update(array('_id' => array('$in' => $partial)), array('$unset' => array('paid' => 1), '$set' => array('partial' => 1)), array('multiple' => 1)); } if ($paid) { Database_Mongo::collection('jobs')->update(array('_id' => array('$in' => $paid)), array('$unset' => array('partial' => 1), '$set' => array('paid' => 1)), array('multiple' => 1)); } Database::instance()->commit(); Messages::save('Payment successfully added!', 'success'); die(json_encode(array('success' => true))); } ksort($jobs); if ($companies) { $companies = DB::select('id', 'name')->from('companies')->where('id', 'IN', array_keys($companies))->order_by('name', 'ASC')->execute()->as_array('id', 'name'); } else { $companies = array(); } $view = View::factory('Jobs/Payment')->bind('jobs', $jobs)->bind('companies', $companies); $this->response->body($view); }
public function action_remove() { if (!Group::current('is_admin')) { return false; } $id = Arr::get($_POST, 'id'); $ids = array_map('intval', explode(',', Arr::get($_POST, 'ids', ''))); if (!$id || !$ids) { throw new HTTP_Exception_404('Not found'); } $result = Database_Mongo::collection('reports')->remove(array('attachment_id' => array('$in' => $ids))); foreach ($ids as $id) { if (file_exists(DOCROOT . 'storage/' . $id)) { unlink(DOCROOT . 'storage/' . $id); } if (file_exists(DOCROOT . 'storage/' . $id . '.thumb')) { unlink(DOCROOT . 'storage/' . $id . '.thumb'); } } DB::delete('attachments')->where('id', 'IN', $ids)->execute(); die(json_encode(array('success' => true))); }
public function action_index() { define('JOB_STATUS_COLUMN', 'data.44'); if (!rand(0, 1000)) { Database_Mongo::collection('search')->remove(array('expires' => array('$lt' => time()))); } if ($_POST) { $data = array('request' => $_POST, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $search_id))); } elseif (!$_GET && Session::instance()->get('search-settings')) { $params = Session::instance()->get('search-settings'); if (!is_array($params)) { $params = json_decode($params); } if ($params !== NULL) { $data = array('request' => $params, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); } else { $search_id = Session::instance()->get('search-settings'); } if ($search_id) { $this->redirect('search?id=' . $search_id); } } elseif (isset($_GET['clear'])) { Session::instance()->delete('search-settings'); $_GET = array(); } elseif (isset($_GET['id'])) { $data = Database_Mongo::collection('search')->findOne(array('_id' => new MongoId($_GET['id']))); if ($data) { Session::instance()->set('search-settings', $_GET['id']); $_GET = $data['request']; } else { Session::instance()->delete('search-settings'); } } $actions = array('contain', 'does not contain', '=', '<', '<=', '>', '>=', '<>', 'empty', 'not empty'); $actions_mongo = array('=' => '$eq', '<' => '$lt', '<=' => '$lte', '>' => '$gt', '>=' => '$gte', '<>' => '$ne'); $columns = Columns::get_visible(); $reports = Columns::get_static(); $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $regions = DB::select('id', 'name')->from('regions'); if ($regs) { $regions->where('id', 'IN', $regs); } $regions = $regions->execute()->as_array('id', 'name'); $query = array(); $list_query = array(); if (Arr::get($_GET, 'submissions')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('active' => 1)), array('$group' => array('_id' => '$job_key')))); $result = array_column($result['result'], '_id'); $query['_id'] = array('$in' => $result); } if (Group::current('show_all_jobs')) { if (Arr::get($_GET, 'company')) { $companies = explode(',', $_GET['company']); if (count($companies) > 1) { $companies = array('$in' => array_map("intval", $companies)); } else { $companies = intval($_GET['company']); } if (Arr::get($_GET, 'ex')) { $list_query['$or'] = $query['$or'] = array(array('companies' => $companies), array('ex' => $companies)); } else { $list_query['companies'] = $query['companies'] = $companies; } } } else { if (Arr::get($_GET, 'status', -1) == 0) { $list_query['ex'] = $query['ex'] = intval(User::current('company_id')); } else { $list_query['companies'] = $query['companies'] = intval(User::current('company_id')); } } if (Group::current('allow_assign')) { foreach (Columns::$settings as $key => $value) { if (isset($_GET['settings'][$key])) { if ($_GET['settings'][$key]) { $query[$key] = array('$exists' => 1); } else { $query[$key] = array('$exists' => 0); } } } } if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $query['_id'] = array('$in' => $q); } elseif ($q) { $query['_id'] = $q[0]; } } if (Arr::get($_GET, 'start')) { $query['last_update']['$gt'] = Columns::parse($_GET['start'], 'date'); } if (Arr::get($_GET, 'end')) { $query['last_update']['$lt'] = Columns::parse($_GET['end'], 'date'); } if (Arr::get($_GET, 'submit-start')) { $query['last_submit']['$gt'] = Columns::parse($_GET['submit-start'], 'date'); } if (Arr::get($_GET, 'submit-end')) { $query['last_submit']['$lt'] = Columns::parse($_GET['submit-end'], 'date'); } $status = Arr::get($_GET, 'status', Group::current('allow_assign') ? -1 : Enums::STATUS_ALLOC); $_GET['status'] = intval($status); if ($status != -1 && (Group::current('show_all_jobs') || $status)) { $query['status'] = intval($status) ?: array('$exists' => 0); } if (!isset($_GET['region'])) { $_GET['region'] = User::current('default_region'); } if (Arr::get($_GET, 'region') && (!$regs || isset($regs[$_GET['region']]))) { $list_query['region'] = $query['region'] = strval($_GET['region']); } elseif ($regs) { $list_query['region'] = $query['region'] = array('$in' => array_values($regs)); } if (Arr::get($_GET, 'type')) { $query['assigned.' . $_GET['type']] = array('$exists' => 1); } foreach (Arr::get($_GET, 'columns', array()) as $id => $column) { if ($column) { $op = Arr::get($actions, Arr::path($_GET, 'actions.' . $id), 0); $value = Arr::path($_GET, 'values.' . $id, ''); $value = Columns::parse($value, Columns::get_type($column)); if (substr(Columns::get_type($column), 0, 4) == 'enum' && Enums::is_multi(substr(Columns::get_type($column), 5)) && $op == '=' && $value) { $op = 'contain'; } if (Columns::get_type($column) == 'date') { if ($op === 'contain') { $op = '='; } elseif ($op === 'does not contain') { $op = '<>'; } elseif ($op == '<=') { $value += 86399; } if ($op == '=' && date('His', $value) === '000000') { $query['data.' . $column]['$lte'] = $value + 86399; $op = '>='; } } if ($op === 'contain') { $op = '$eq'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'does not contain') { $op = '$not'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'empty') { $op = '$in'; $value = array(NULL, '', 0); } elseif ($op === 'not empty') { $op = '$nin'; $value = array(NULL, '', 0); } else { $op = Arr::get($actions_mongo, $op, '$eq'); } if ($op == '$eq' && !$value) { $value = NULL; } if (isset($query['data.' . $column]) && $op == '$eq') { if (isset($query['data.' . $column]['$in'])) { $query['data.' . $column]['$in'][] = $value; } elseif (isset($query['data.' . $column]['$eq'])) { $query['data.' . $column] = array('$in' => array($query['data.' . $column]['$eq'], $value)); } else { $query['data.' . $column]['$in'] = array($value); } } else { $query['data.' . $column][$op] = $value; } } } foreach ($query as $key => $ops) { if (substr($key, 0, 5) == 'data.' && count($ops) == 1 && key($ops) == '$eq') { $query[$key] = array_shift($ops); } } $jobs = Database_Mongo::collection('jobs'); $list_values = array(); foreach (Columns::get_search() as $key => $value) { if ($value == 2) { $list_values[$key] = $jobs->distinct('data.' . $key, $query ?: NULL) ?: array(); if (substr(Columns::get_type($key), 0, 4) == 'enum') { $list = array(); foreach ($list_values[$key] as $values) { if ($values) { $values = explode(', ', $values); foreach ($values as $value) { $list[$value] = 1; } } else { $list[''] = 1; } } $list_values[$key] = array_keys($list); } if ($list_values[$key]) { sort($list_values[$key]); } } } Pager::$count = $jobs->count($query); $show = array('region' => 1, 'status' => 1, 'created' => 1, 'last_update' => 1, 'last_submit' => 1, 'assigned' => 1, 'companies' => 1, 'ex' => 1); foreach (Columns::$settings as $key => $value) { $show[$key] = 1; } foreach (Columns::get_search() as $column => $type) { $show['data.' . $column] = 1; } $result = $jobs->find($query, $show); $sort = Arr::get($_GET, 'sort'); if (is_array($sort)) { $save = implode('|', $sort); DB::update('users')->set(array('default_sort' => $save))->where('id', '=', User::current('id'))->execute(); } else { $sort = explode('|', User::current('default_sort') ?: '-update'); $_GET['sort'] = $sort; } $sorting = array(); foreach ($sort as $order) { $dir = substr($order, 0, 1) == '-' ? -1 : 1; $order = substr($order, 1); switch ($order) { case 'id': $order = '_id'; break; case 'update': $order = 'last_update'; break; case 'submit': $order = 'last_submit'; break; case 'status': $order = 'status'; break; case 'data-8': $sorting['address'] = $dir; $order = false; break; default: if (substr($order, 0, 5) == 'data-') { $order = 'data.' . intval(substr($order, 5)); } else { $order = false; } } if ($order) { $sorting[$order] = $dir; } } $result->sort($sorting)->limit(Pager::limit())->skip(Pager::offset()); $tickets = array(); while ($row = $result->next()) { $tickets[] = $row; } $ids = array_column($tickets, '_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)->and_where('folder', '<>', 'Signatures')->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']; } } $result = Database_Mongo::collection('forms')->find(array(), array('_id' => 1, 'type' => 1, 'name' => 1)); $forms = array(); foreach ($result as $form) { $forms[$form['type']][strval($form['_id'])] = $form['name']; } $view = View::factory('Jobs/Search')->bind('reports', $reports)->bind('regions', $regions)->bind('columns', $columns)->bind('actions', $actions)->bind('tickets', $tickets)->bind('submissions', $submissions)->bind('attachments', $attachments)->bind('list_values', $list_values)->bind('types', $types)->bind('sort', $sort)->bind('companies', $companies)->bind('forms', $forms)->bind('query', $query); $this->response->body($view); }
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 (Arr::get($job, 'locked')) { throw new HTTP_Exception_403('Forbidden'); } $job['region'] = DB::select('name')->from('regions')->where('id', '=', $job['region'])->execute()->get('name'); $types = array(); foreach (Arr::get($job, 'assigned', array()) as $type => $company) { if ($company == User::current('company_id')) { $types[] = $type; } } if (!$types) { throw new HTTP_Exception_403('Forbidden'); } $columns_sets = Form::$columns; if (Arr::get($_POST, 'signature') && Arr::get($_POST, 'signed')) { $completed = Arr::get($_POST, 'completed'); $data = Arr::get($_POST, 'data-' . $completed); $submissions = array(); foreach ($columns_sets as $title => $columns) { if ($completed == crc32($title)) { foreach ($columns as $map => $value) { $value = Arr::get($data, $map, ''); if ($value === '') { continue; } $map = explode(',', $map); foreach ($map as $key) { if (is_numeric($key)) { $val = Columns::parse($value, Columns::get_type($key)); $submissions[$key] = $val; } } } if (Arr::get($data, 'ot1') || Arr::get($data, 'ot2')) { $submissions['43'] = '1310-' . Arr::get($data, 'ot1', '') . ' 1625-' . Arr::get($data, 'ot2', ''); } if ($submissions) { $submissions['245'] = strpos($title, 'Yes') === 0 ? 'Yes' : 'No'; } break; } } if (Arr::get($_POST, 'upload-count')) { Messages::save(intval($_POST['upload-count']) . ' file(s) were successfully uploaded', 'success'); } if ($submissions) { $data = array('filename' => 'Submission-' . date('dmY-His') . '-signature.png', 'mime' => 'image/png', 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'folder' => 'Signatures', 'fda_id' => Arr::path($job, 'data.14'), 'address' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', Arr::path($job, 'data.8'))), '-'), 'title' => ''); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); $content = explode(',', Arr::get($_POST, 'signature')); if ($image_id && file_put_contents(DOCROOT . 'storage/' . $image_id, base64_decode(Arr::get($content, 1, '')))) { unset($data['mime']); $data = array('filename' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', 'Signatures / ' . Arr::path($job, 'data.14') . ' / ' . Arr::path($job, 'data.8') . ' / Submission-' . date('dmY-His') . '-signature.png')), '-'), 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); $submission = array('job_key' => $id, 'user_id' => User::current('id'), 'update_time' => 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 (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; } $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 { $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } if ($status != Enums::STATUS_PENDING) { $status = $update['$set']['status'] = Enums::STATUS_PENDING; } if ($update) { $update['$set']['last_update'] = time(); $update['$set']['last_submit'] = 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'] = 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 { Database::instance()->rollback(); Messages::save("Unable to save signature image! Please, try again in few minutes", 'danger'); } } else { Messages::save("No changes were submitted", 'warning'); } $this->redirect('search'); } $last = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => array('$exists' => 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)); $values = array(); foreach ($last as $submission) { $values[str_replace('.', '', $submission['key'])] = array('status' => Arr::get($submission, 'active', 0), 'value' => $submission['value']); } $view = View::factory('Jobs/Form')->bind('job', $job)->bind('job_values', $values)->bind('columns', $columns_sets); $this->response->body($view); }
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); }