Skip to content

Instantly share code, notes, and snippets.

@hipsterjazzbo
Last active December 17, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipsterjazzbo/5625724 to your computer and use it in GitHub Desktop.
Save hipsterjazzbo/5625724 to your computer and use it in GitHub Desktop.
<?php
function new_recurring_instance($job_template_id)
{
$job_template = Job_template_model::find($job_template_id);
$new_job_instance = Job_model::create();
// Copy some fields from template
$new_job_instance->company_id = $job_template->company_id;
$new_job_instance->customer_id = $job_template->customer_id;
$new_job_instance->job_type = $job_template->job_type;
$new_job_instance->notes = $job_template->notes;
$new_job_instance->brief_description = $job_template->brief_description;
$new_job_instance->long_description = $job_template->long_description;
$new_job_instance->job_contact_id = $job_template->job_contact_id;
$new_job_instance->created_by = $job_template->created_by;
$new_job_instance->customer_ref = $job_template->customer_ref;
$new_job_instance->customer_budget = $job_template->customer_budget;
$new_job_instance->accepted_terms = $job_template->accepted_terms;
$new_job_instance->terms_sent = $job_template->terms_sent;
$new_job_instance->default_invoice_due_days = $job_template->default_invoice_due_days;
// New values for some fields
$new_job_instance->internal_job_id = $this->calculate_new_internal_job_id($job_template->company_id);
$new_job_instance->created_at = date('Y-m-d H:i:s');
$new_job_instance->job_status = $this->get_job_status_id_by_status_name('Active');
// Save it
$new_job_instance->save();
// Add new site visit
$new_works_order = Works_order_model::create();
// Copy some fields from the new job
$new_works_order->company_id = $new_job_instance->company_id;
$new_works_order->job_id = $new_job_instance->job_id;
$new_works_order->brief_description = $new_job_instance->brief_description;
$new_works_order->long_description = $new_job_instance->long_description;
$new_works_order->created_by = $new_job_instance->created_by;
// New values for some fields
$new_works_order->job_status = $this->get_job_status_id_by_status_name('To Schedule');
$new_works_order->suffix = 'a';
$new_works_order->created_at = date('Y-m-d H:i:s');
$new_works_order->estimated_duration = 4;
$new_works_order->schedule_sent = 0;
// Save it so we have the id for potential assignments
$new_works_order->save();
// Figure out assignment and schedule form template options
if ( ! empty($job_template->assign_to))
{
$this->load->model('assign_model');
$assigned_employees = explode(',', $job_template->assign_to);
$event_type = $this->calendar_model->get_event_type_id_by_name('works_order');
$event_title = $this->calendar_model->get_event_title_for_works_order($new_works_order->works_order_id);
foreach ($assigned_employees as $assigned_employee_id)
{
$this->assign_model->assign_works_order($new_works_order->works_order_id, 0, $assigned_employee_id);
if ($job_template->auto_schedule)
{
$start_time = $job_template->recurring_object->start_date;
$end_time = $job_template->recurring_object->start_date->add(new DateInterval('PT1H'));
$this->calendar_model->schedule_new_event($assigned_employee_id, $start_time, $end_time, $event_type, NULL, $event_title);
}
}
}
return $new_job_instance->job_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment