示例#1
0
 public function save()
 {
     $flash = Flash::Instance();
     if (strtolower($this->_data['saveform']) == 'cancel') {
         $flash->addMessage('Action cancelled');
         sendTo($this->name, 'index', $this->_modules);
     }
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $header_data = $this->_data['WHTransfer'];
     $lines_data = array();
     if (isset($this->_data['WHTransferLine'])) {
         $lines_data = $this->_data['WHTransferLine'];
         $lines_data = DataObjectCollection::joinArray($lines_data);
     } else {
         $errors[] = 'No Transfer Lines entered';
     }
     if (isset($header_data['id']) && $header_data['id'] != '') {
         $action = 'updated';
         // delete any lines not submitted
         $update = array();
         foreach ($lines_data as $line) {
             $update[$line['id']] = $line['id'];
         }
         $whtransfer = new WHTransfer();
         $whtransfer->load($header_data['id']);
         if ($whtransfer) {
             foreach ($whtransfer->transfer_lines as $line) {
                 if (!isset($update[$line->id])) {
                     $whtransferline = new WHTransferline();
                     $whtransferline->delete($line->id);
                 }
             }
         }
     } else {
         $action = 'added';
     }
     $whtransfer = WHTransfer::Factory($header_data, $lines_data, $errors);
     if ($whtransfer && count($errors) == 0) {
         $whtransfer->save($errors);
     }
     if (count($errors) == 0 && $db->CompleteTrans()) {
         $flash->addMessage('Transfer Number ' . $whtransfer->transfer_number . ' ' . $action . ' successfully');
         sendTo($this->name, 'index', $this->_modules);
     } else {
         $db->FailTrans();
         $flash->addErrors($errors);
         $this->_new();
         $this->_templateName = $this->getTemplateName('new');
     }
 }
示例#2
0
 public function save_model($data)
 {
     // Used to save Invoice Header and Invoice Lines from import or copy of existing
     $flash = Flash::Instance();
     if (empty($data['PInvoice']) || empty($data['PInvoiceLine'])) {
         $flash->addError('Error trying to save invoice');
         return false;
     }
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $header = $data['PInvoice'];
     $lines_data = DataObjectCollection::joinArray($data['PInvoiceLine'], 0);
     if (!$lines_data || empty($lines_data)) {
         $lines_data[] = $data['PInvoiceLine'];
     }
     $invoice = PInvoice::Factory($header, $errors);
     if (!$invoice || count($errors) > 0) {
         $errors[] = 'Invoice validation failed';
     } elseif (!$invoice->save()) {
         $errors[] = 'Invoice creation failed';
     }
     if ($invoice) {
         foreach ($lines_data as $line) {
             $line['invoice_id'] = $invoice->{$invoice->idField};
             $invoiceline = PInvoiceLine::Factory($invoice, $line, $errors);
             if (!$invoiceline || count($errors) > 0) {
                 $errors[] = 'Invoice Line validation failed for line ' . $line['line_number'];
             } elseif (!$invoiceline->save()) {
                 $errors[] = 'Invoice Line creation failed for line ' . $line['line_number'];
             }
         }
     }
     if (count($errors) === 0) {
         if (!$invoice->save()) {
             $errors[] = 'Error updating Invoice totals';
         } else {
             $result = array('internal_id' => $invoice->{$invoice->idField}, 'internal_identifier_field' => $invoice->identifierField, 'internal_identifier_value' => $invoice->getidentifierValue());
         }
     }
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $db->FailTrans();
         $result = false;
     }
     $db->CompleteTrans();
     return $result;
 }
 public function save()
 {
     /**
      * to ensure the redirect htis the right page, we must check the 
      * original action if it is day, week or month view we can construct 
      * a sentTo, if it is anything else we must fire a sendBack
      */
     switch ($this->_data['original_action']) {
         case "dayview":
         case "weekview":
         case "monthview":
             $original_action = $this->_data['original_action'];
             break;
         default:
             $original_action = FALSE;
     }
     $flash = Flash::Instance();
     $calendar = new Calendar();
     if (!isset($this->_data['CalendarEvent']['calendar_id']) || $this->_data['CalendarEvent']['calendar_id'] == '') {
         $flash->addError("You must specify a calendar");
         if ($original_action != FALSE) {
             sendTo('index', $original_action, 'calendar');
         } else {
             sendBack();
         }
     }
     if (!array_key_exists($this->_data['CalendarEvent']['calendar_id'], $calendar->getWritableCalendars())) {
         $flash->addError("You haven't got permission to write to this calendar");
         if ($original_action != FALSE) {
             sendTo('index', $original_action, 'calendar');
         } else {
             sendBack();
         }
     }
     if (isset($this->_data['CalendarEventAttendee'])) {
         $attendees = DataObjectCollection::joinArray($this->_data['CalendarEventAttendee'], 1);
         foreach ($attendees as $key => $attendee) {
             foreach ($attendees as $s_key => $s_attendee) {
                 if ($attendee['person_id'] == $s_attendee['person_id'] && $key != $s_key) {
                     $this->_new();
                     $this->_templateName = $this->getTemplateName('new');
                     $flash->addError('Please ensure to only add any attendee once');
                     return;
                 }
             }
         }
     }
     if (!isset($this->_data['book'])) {
         if ($this->_data['CalendarEvent']['start_time'] != $this->_data['CalendarEvent']['end_time']) {
             $this->_data['CalendarEvent']['all_day'] = true;
         }
         if (isset($this->_data['CalendarEvent']['start_time'])) {
             $this->_data['CalendarEvent']['start_time'] .= !empty($this->_data['CalendarEvent']['start_time_hours']) ? ' ' . $this->_data['CalendarEvent']['start_time_hours'] : ' 00';
             $this->_data['CalendarEvent']['start_time'] .= ':';
             $this->_data['CalendarEvent']['start_time'] .= !empty($this->_data['CalendarEvent']['start_time_minutes']) ? $this->_data['CalendarEvent']['start_time_minutes'] : '00';
         }
         if (isset($this->_data['CalendarEvent']['end_time'])) {
             $this->_data['CalendarEvent']['end_time'] .= !empty($this->_data['CalendarEvent']['end_time_hours']) ? ' ' . $this->_data['CalendarEvent']['end_time_hours'] : ' 00';
             $this->_data['CalendarEvent']['end_time'] .= ':';
             $this->_data['CalendarEvent']['end_time'] .= !empty($this->_data['CalendarEvent']['end_time_minutes']) ? $this->_data['CalendarEvent']['end_time_minutes'] : '00';
         }
         $temp_start_time = $this->_data['CalendarEvent']['start_time_hours'] . ":" . $this->_data['CalendarEvent']['start_time_minutes'];
         $temp_end_time = $this->_data['CalendarEvent']['end_time_hours'] . ":" . $this->_data['CalendarEvent']['end_time_minutes'];
         if ($temp_start_time == '00:00' || $temp_start_time == ':' || $temp_end_time == '00:00' || $temp_end_time == ':') {
             $this->_data['CalendarEvent']['all_day'] = true;
         }
         if ($this->check_collisions($this->_data['CalendarEvent']['start_time'], $this->_data['CalendarEvent']['end_time']) !== false) {
             $this->confirm_collision();
             $this->_templateName = $this->getTemplateName('confirm_collision');
             return;
         }
     } elseif (isset($this_data['dont_book'])) {
         $this->_new();
         $this->_templateName = $this->getTemplateName('new');
         return;
     }
     if (parent::save('CalendarEvent')) {
         if (isset($this->_data['CalendarEventAttendee'])) {
             foreach ($attendees as $attendee) {
                 $attendee['calendar_event_id'] = $this->_data['id'];
                 $model = call_user_func(array('CalendarEventAttendee', "Factory"), $attendee, array(), 'CalendarEventAttendee');
                 if (is_a($model, 'CalendarEventAttendee')) {
                     $model->save();
                 }
             }
         }
         if ($original_action != FALSE) {
             sendTo('index', $original_action, 'calendar');
         } else {
             sendBack();
         }
     } else {
         $this->_new();
         $this->_templateName = $this->getTemplateName('new');
     }
 }