Example #1
0
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     if (empty($this->_data[$this->modeltype]['id'])) {
         $create = true;
     }
     $this->_data[$this->modeltype]['name'] = str_replace(' ', '_', strtolower($this->_data[$this->modeltype]['name']));
     $db = DB::Instance();
     $db->StartTrans();
     if (parent::save('Dataset', null, $errors)) {
         if (!$create || $this->create_table($this->_data[$this->modeltype]) == 2) {
             $db->CompleteTrans();
             sendTo($this->name, 'index', $this->_modules);
         }
         $message = $db->ErrorMsg();
         if (!empty($message)) {
             $errors[] = $message;
         }
     }
     $flash->addErrors($errors);
     $db->FailTrans();
     $db->CompleteTrans();
     $this->refresh();
 }
 public function save()
 {
     $flash = Flash::Instance();
     if (parent::save('SODespatchLine')) {
         sendTo($this->name, 'index', $this->_modules);
     } else {
         $this->refresh();
     }
 }
 public function save()
 {
     $flash = Flash::Instance();
     if (parent::save('POReceivedLine')) {
         sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null);
     } else {
         $this->refresh();
     }
 }
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     $save = parent::save('CRMCalendarEvent', array(), $errors);
     if (is_ajax()) {
         echo json_encode(array('status' => $save));
         exit;
     } else {
         sendBack();
     }
 }
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     if (parent::save($this->modeltype, $this->_data['WorkSchedule'], $errors)) {
         $idField = $this->saved_model->idField;
     }
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $this->refresh();
     } else {
         sendTo($this->name, 'view', $this->_modules, array($idField => $this->saved_model->{$idField}));
     }
 }
Example #6
0
 public function save_model($modelName, $dataIn = array(), &$errors = array(), &$warnings = array(), $duplicates = '')
 {
     // Need to define error array here due to too many nested levels
     $flash = Flash::Instance();
     $edi_errors = array();
     if ($duplicates != 'R') {
         // Do not reject duplicates - so check if record exists
         $model = new $modelName();
         $identifierFields = $model->getIdentifierFields();
         $identifierValues = array();
         foreach ($identifierFields as $key => $field) {
             if (isset($dataIn[$modelName][$field]) && $model->checkUniqueness($field)) {
                 $identifier_string = $field . ' : ' . $dataIn[$modelName][$field] . ',';
                 $identifierValues[$key] = $dataIn[$modelName][$field];
             } else {
                 unset($identifierFields[$key]);
             }
         }
         if (count($identifierFields) > 0 && count($identifierFields) == count($identifierValues)) {
             $model->loadBy($identifierFields, $identifierValues);
             if ($model->isLoaded()) {
                 if ($duplicates == 'I') {
                     // Ignore duplicates so return true
                     $warnings[] = 'Duplicate  ' . $identifier_string . ' Ignored';
                     return true;
                 } else {
                     // replace/update duplicate so set id field value
                     $dataIn[$modelName][$model->idField] = $model->{$model->idField};
                 }
             }
         }
     }
     $result = parent::save($modelName, $dataIn, $edi_errors);
     if (count($edi_errors) > 0) {
         // Add the Identifier Values to the errors to identify the data in error
         // Assumes the $dataIn array is an array of [ModelName][ModelData]
         // and that the input $modelName is the identifying model for the data
         $model = new $modelName();
         foreach ($model->getIdentifierFields() as $field) {
             $errors[] = prettify($field) . ' : ' . $dataIn[$modelName][$field];
         }
         $flash->addErrors($edi_errors);
         foreach ($edi_errors as $error) {
             $errors[] = $error;
         }
     }
     return $result;
 }
Example #7
0
 function save()
 {
     $flash = Flash::Instance();
     $calendar = new Calendar();
     if (isset($this->_data['Calendar']['id']) && !$calendar->isOwner($this->_data['Calendar']['id'])) {
         $flash->addError("You cannot save a calendar that belongs to someone else");
         sendTo('calendars', 'index', 'calendar');
     }
     // if we dont specify any shared users, pass an empty array
     if (isset($this->_data['CalendarShareCollection'])) {
         if (!$this->checkParams(array('Calendar', 'CalendarShareCollection'))) {
             sendBack();
         }
     }
     if ($this->_data['Calendar']['type'] == 'gcal' && (!isset($this->_data['Calendar']['gcal_url']) || empty($this->_data['Calendar']['gcal_url']))) {
         $flash->addError("You haven't specified a feed URL");
         sendBack();
     }
     if (!isset($this->_data['Calendar']['colour'])) {
         $colours = $calendar->getEnumOptions('colour');
         $this->_data['Calendar']['colour'] = $colours[array_rand($colours)];
     }
     if (isset($this->_data['Calendar']['id'])) {
         $calendarshare = new CalendarShareCollection(new CalendarShare());
         $sh = new SearchHandler($calendarshare, false);
         $sh->addConstraint(new Constraint('calendar_id', '=', $this->_data['Calendar']['id']));
         $calendarshare->delete($sh);
     }
     // apply calendar_id to CalendarShareCollection
     if (isset($this->_data['CalendarShareCollection']) && !empty($this->_data['CalendarShareCollection']['username'])) {
         foreach ($this->_data['CalendarShareCollection']['username'] as $key => $value) {
             $this->_data['CalendarShareCollection']['calendar_id'][$key] = '';
         }
     }
     $errors = array();
     if (parent::save('Calendar', '', $errors)) {
         sendTo('index', 'index', 'calendar');
     } else {
         sendBack();
     }
 }
Example #8
0
 function do_import()
 {
     $filename = $_FILES['file']['tmp_name'];
     $address_fields = array('street1', 'street2', 'street3', 'town', 'county', 'postcode');
     $req_address_fields = array('street1', 'town', 'county', 'postcode');
     $db =& DB::Instance();
     $flash = Flash::Instance();
     $db->StartTrans();
     $columnheadings = false;
     if (isset($this->_data['contains_headings'])) {
         $columnheadings = true;
     } else {
         if (count($this->_data['headings']) > 0) {
             $columnheadings = $this->_data['headings'];
         }
     }
     $data = parse_csv_file($filename, $columnheadings);
     $co_loaded = false;
     $errors = array();
     $try_address = false;
     if (in_array_all($req_address_fields, $columnheadings)) {
         $try_address = true;
     }
     foreach ($data as $person_data) {
         $company = DataObjectFactory::Factory('Company');
         if (in_array('company', $columnheadings)) {
             if (isset($this->_data['unique_companies'])) {
                 $co_loaded = $company->loadBy('name', $person_data['company']);
             }
             if ($co_loaded === false) {
                 $co_data = array('name' => $person_data['company']);
                 $company = DataObject::Factory($co_data, $errors, 'Company');
                 if ($company !== false) {
                     $company->save();
                 }
             }
             if ($company !== false) {
                 $person_data['company_id'] = $company->id;
             }
         }
         parent::save('Person', $person_data);
         if ($try_address && in_array_all($req_address_fields, array_keys($person_data))) {
             $address_data = array();
             foreach ($address_fields as $fieldname) {
                 if (isset($person_data[$fieldname])) {
                     $address_data[$fieldname] = $person_data[$fieldname];
                 }
             }
             $address_data['main'] = true;
             $address_data['name'] = 'Main';
             $address_data['person_id'] = $this->_data['id'];
             $address_data['countrycode'] = 'GB';
             $address = DataObject::Factory($address_data, $errors, 'Personaddress');
             if ($address !== false) {
                 $address->save();
             }
         }
     }
     $success = $db->CompleteTrans();
     if ($success) {
         $flash->clearMessages();
         $flash->addMessage(count($data) . ' contacts imported');
         $this->import();
         $this->setTemplateName('import');
     } else {
         $flash->addErrors($errors);
         $this->import();
         $this->setTemplateName('import');
     }
 }
 public function updateEvent()
 {
     $event = $this->_uses['SODespatchEvent'];
     $event->load($this->_data['id']);
     $data = $event->_data;
     // I'm sure there's a better way to format these dates...
     switch ($this->_data['type']) {
         case "drop":
             $data['start_time'] = un_fix_date(date('o-m-d H:i:s', strtotime("+" . $this->_data['day'] . " day", strtotime("+" . $this->_data['minute'] . " minute", strtotime($data['start_time'])))), true);
             $data['end_time'] = un_fix_date(date('o-m-d H:i:s', strtotime("+" . $this->_data['day'] . " day", strtotime("+" . $this->_data['minute'] . " minute", strtotime($data['end_time'])))), true);
             break;
         case "resize":
             $data['start_time'] = un_fix_date($data['start_time'], true);
             $data['end_time'] = un_fix_date(date('o-m-d H:i:s', strtotime("+" . $this->_data['day'] . " day", strtotime("+" . $this->_data['minute'] . " minute", strtotime($data['end_time'])))), true);
             break;
     }
     $errors = array();
     if (parent::save('SODespatchEvent', $data, $errors)) {
         json_reply(array('success' => TRUE));
     } else {
         json_reply(array('success' => FALSE));
     }
 }
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     if (!$this->checkParams($this->modeltype)) {
         sendBack();
     }
     $this->loadData();
     $header = $this->_uses[$this->modeltype];
     $stitem = DataObjectFactory::Factory('STItem');
     if (!empty($this->_data[$this->modeltype]['stitem_id'])) {
         $stitem->load($this->_data[$this->modeltype]['stitem_id']);
         $end_date = un_fix_date($stitem->obsolete_date);
         if (!empty($end_date) && $end_date != $this->_data[$this->modeltype]['end_date']) {
             $this->_data[$this->modeltype]['end_date'] = $end_date;
             $flash->addWarning('Item has obsolete date - setting end date on product');
         }
     }
     // If there is no description, use the description item code description
     if (empty($this->_data[$this->modeltype]['description']) && $stitem->isLoaded()) {
         $this->_data[$this->modeltype]['description'] = $stitem->getIdentifierValue();
     }
     // If there is no description, then no supplier or item has been selected
     if (empty($this->_data[$this->modeltype]['description'])) {
         $errors[] = 'You must select an item &/or enter a description';
     }
     if (count($errors) == 0) {
         if (parent::save($this->modeltype, null, $errors)) {
             if (isset($this->_data['saveform'])) {
                 sendTo($this->name, 'view', $this->_modules, array('id' => $this->saved_model->id));
             } else {
                 sendTo($this->name, 'new', $this->_modules);
             }
         } else {
             $errors[] = 'Failed to save Product';
         }
     }
     $flash->addErrors($errors);
     if (isset($this->_data[$this->modeltype]['id'])) {
         $this->_data['id'] = $this->_data[$this->modeltype]['id'];
     }
     $this->refresh();
 }
 protected function save_model($modelName, $dataIn = array(), &$errors = array())
 {
     return parent::save($modelName, $dataIn, $errors);
 }
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     if (!$this->checkParams($this->modeltype)) {
         sendBack();
     }
     $productline_data = $this->_data[$this->modeltype];
     $header = DataObjectFactory::Factory('SOProductlineHeader');
     $header->load($productline_data['productline_header_id']);
     if (!$header->isLoaded()) {
         $errors[] = 'Error loading header';
     }
     // If the customer is specified, the price type must match to the customer
     if (!empty($productline_data['slmaster_id'])) {
         // Load the customer
         $customer = DataObjectFactory::Factory('SLCustomer');
         $customer->load($productline_data['slmaster_id']);
         if (!$customer->isLoaded()) {
             $errors[] = 'Error loading customer';
         } else {
             if (!is_null($customer->so_price_type_id) && (empty($productline_data['so_price_type_id']) || $productline_data['so_price_type_id'] != $customer->so_price_type_id) || is_null($customer->so_price_type_id) && !empty($productline_data['so_price_type_id'])) {
                 $errors[] = 'Price Type invalid for this Customer';
             }
         }
     }
     // If no description is entered, use the supplier product code
     if (empty($productline_data['slmaster_id']) && !empty($productline_data['customer_product_code'])) {
         $productline_data['customer_product_code'] = '';
         $flash->addMessage('Customer Code ignored as no Customer selected');
     }
     // If there is no description, use the supplier code
     if (empty($productline_data['description']) && !empty($productline_data['customer_product_code'])) {
         $productline_data['description'] = $productline_data['customer_product_code'];
     }
     // If there is no description, use the header description
     if (empty($productline_data['description'])) {
         $productline_data['description'] = $header->description;
     }
     // Price is either entered directly or comes from the item
     if (empty($productline_data['price'])) {
         $errors[] = 'You must enter a price';
     }
     // If there is no description, then no customer or header has been selected
     if (empty($productline_data['description'])) {
         $errors[] = 'You must select a customer &/or enter a description';
     }
     // If the header has an end date, make sure the line end date is set
     // and not later than the header
     if (!is_null($header->end_date) && (empty($productline_data['end_date']) || fix_date($productline_data['end_date']) > $header->end_date)) {
         $productline_data['end_date'] = un_fix_date($header->end_date);
         $flash->addMessage('End date set to header end date');
     }
     // Check for overlapping lines
     $cc = new ConstraintChain();
     $cc->add(new Constraint('productline_header_id', '=', $productline_data['productline_header_id']));
     if (!empty($productline_data['id'])) {
         $cc->add(new Constraint('id', '!=', $productline_data['id']));
     }
     if (empty($productline_data['slmaster_id'])) {
         $cc->add(new Constraint('slmaster_id', 'is', 'NULL'));
     } else {
         $cc->add(new Constraint('slmaster_id', '=', $productline_data['slmaster_id']));
     }
     if (empty($productline_data['so_price_type_id'])) {
         $cc->add(new Constraint('so_price_type_id', 'is', 'NULL'));
     } else {
         $cc->add(new Constraint('so_price_type_id', '=', $productline_data['so_price_type_id']));
     }
     $db = DB::Instance();
     $cc1 = new ConstraintChain();
     $cc1->add(new Constraint('start_date', 'between', $db->qstr(fix_date($productline_data['start_date'])) . ' and ' . $db->qstr(empty($productline_data['end_date']) ? fix_date(date(DATE_FORMAT)) : fix_date($productline_data['end_date']))));
     $cc2 = new ConstraintChain();
     $cc2->add(new Constraint('start_date', '<', fix_date($productline_data['start_date'])));
     $cc3 = new ConstraintChain();
     $cc3->add(new Constraint('end_date', '>=', fix_date($productline_data['start_date'])));
     $cc3->add(new Constraint('end_date', 'is', 'NULL'), 'OR');
     $cc2->add($cc3);
     $cc1->add($cc2, 'OR');
     $cc->add($cc1);
     $productline = DataObjectFactory::Factory('SOProductLine');
     $overlap = $productline->getAll($cc);
     if (count($overlap) > 0) {
         $errors[] = 'Current product price already exists';
     }
     if (count($errors) == 0) {
         if (parent::save($this->modeltype, $productline_data, $errors)) {
             if (isset($this->_data['saveform'])) {
                 sendTo($this->name, 'view_so_product', $this->_modules, array('productline_header_id' => $productline_data['productline_header_id']));
             } else {
                 sendTo($this->name, 'new', $this->_modules, array('productline_header_id' => $productline_data['productline_header_id']));
             }
         } else {
             $errors[] = 'Failed to save Product Line';
         }
     }
     $flash->addErrors($errors);
     if (isset($this->_data['SOProductline']['id'])) {
         $this->_data['id'] = $this->_data['SOProductline']['id'];
         $this->_data['productline_header_id'] = $this->_data['SOProductline']['productline_header_id'];
     }
     $this->refresh();
 }
Example #13
0
 public function save_confirmsale()
 {
     if (!isset($this->_data) || !$this->loadData()) {
         $this->dataError();
         sendBack();
     }
     $sorder = $this->_uses[$this->modeltype];
     $sorder_data = $this->_data[$this->modeltype];
     $flash = Flash::Instance();
     $errors = array();
     $db = DB::Instance();
     $db->startTrans();
     $slcustomer = DataObjectFactory::Factory('SLCustomer');
     $slcustomer->load($sorder->slmaster_id);
     // save the customer details
     // Existing or new person?
     $person_data = array();
     if (!empty($sorder_data['person_id']) && $sorder_data['person_id'] > 0) {
         // Existing person
         $sorder->person_id = $sorder_data['person_id'];
         $person = DataObjectFactory::Factory('Person');
         $person->load($sorder_data['person_id']);
         if ($person->isLoaded()) {
             $sorder_data['party_id'] = $person->party_id;
         }
     } elseif (!empty($sorder_data['surname'])) {
         // TODO: Saving of person and party data needs to move to appropriate model
         // See also EmployeesController, CompanysController and PersonsController
         // new person
         $sorder_data['party_id'] = '';
         $person_data['Party']['id'] = '';
         $person_data['Party']['type'] = 'Person';
         $person_data['Person']['title'] = $sorder_data['title'];
         $person_data['Person']['firstname'] = $sorder_data['firstname'];
         $person_data['Person']['surname'] = $sorder_data['surname'];
         $person_data['Person']['id'] = '';
         $person_data['Person']['party_id'] = '';
         $person_data['Person']['company_id'] = $slcustomer->company_id;
         $index = 0;
         foreach (array('phone', 'email') as $contact_type) {
             if (!empty($sorder_data[$contact_type])) {
                 $cm = DataObjectFactory::Factory('PartyContactMethod');
                 $person_data[$index]['Contactmethod']['id'] = '';
                 $person_data[$index]['Contactmethod']['contact'] = $sorder_data[$contact_type];
                 $person_data[$index]['PartyContactMethod']['id'] = '';
                 $person_data[$index]['PartyContactMethod']['contactmethod_id'] = '';
                 $person_data[$index]['PartyContactMethod']['party_id'] = '';
                 $person_data[$index]['PartyContactMethod']['name'] = 'MAIN';
                 $person_data[$index]['PartyContactMethod']['type'] = $cm->getType($contact_type);
                 $person_data[$index]['PartyContactMethod']['main'] = 't';
                 $person_data[$index]['PartyContactMethod']['billing'] = 't';
                 $person_data[$index]['PartyContactMethod']['shipping'] = 't';
                 $person_data[$index]['PartyContactMethod']['payment'] = 't';
                 $person_data[$index]['PartyContactMethod']['technical'] = 't';
                 $index++;
             }
         }
         if (parent::save('Person', $person_data, $errors)) {
             foreach ($this->saved_models as $model) {
                 if (isset($model['Person'])) {
                     $person = $model['Person'];
                     $sorder->person_id = $person->id;
                     $sorder_data['party_id'] = $person->party_id;
                 }
             }
         } else {
             $errors[] = 'Error saving Customer Details : ' . $db->ErrorMsg();
         }
     }
     foreach (array('invoice' => $sorder_data['inv_address_id'], 'delivery' => $sorder_data['del_address_id']) as $address_type => $address_id) {
         $address_data = array();
         if (!empty($sorder_data[$address_type]['street1'])) {
             // New address
             $address_data['Address']['id'] = '';
             $address_data['Address']['street1'] = $sorder_data[$address_type]['street1'];
             $address_data['Address']['street2'] = $sorder_data[$address_type]['street2'];
             $address_data['Address']['street3'] = $sorder_data[$address_type]['street3'];
             $address_data['Address']['town'] = $sorder_data[$address_type]['town'];
             $address_data['Address']['county'] = $sorder_data[$address_type]['county'];
             $address_data['Address']['postcode'] = $sorder_data[$address_type]['postcode'];
             $address_data['Address']['countrycode'] = $sorder_data[$address_type]['countrycode'];
             $address_data['PartyAddress']['address_id'] = '';
         } elseif (!empty($person_data['Person'])) {
             // Existing address needs to be linked to new person
             $address_data['PartyAddress']['address_id'] = $address_id;
         }
         if (!empty($sorder_data[$address_type]['street1']) || !empty($person_data['Person'])) {
             // If new address or new person, link address to party
             $address_data['PartyAddress']['id'] = '';
             $address_data['PartyAddress']['party_id'] = $sorder_data['party_id'];
             $address_data['PartyAddress']['name'] = 'MAIN';
             $address_data['PartyAddress']['main'] = $address_type == 'invoice' ? 't' : 'f';
             $address_data['PartyAddress']['billing'] = $address_type == 'invoice' ? 't' : 'f';
             $address_data['PartyAddress']['shipping'] = $address_type == 'delivery' ? 't' : 'f';
             $address_data['PartyAddress']['payment'] = $address_type == 'invoice' ? 't' : 'f';
             $address_data['PartyAddress']['technical'] = 'f';
             if (parent::save('Address', $address_data, $errors)) {
                 foreach ($this->saved_models as $model) {
                     if (isset($model['Address'])) {
                         $address = $model['Address'];
                         if ($address_type == 'invoice') {
                             $sorder_data['inv_address_id'] = $address->id;
                         } else {
                             $sorder_data['del_address_id'] = $address->id;
                         }
                     }
                 }
             } else {
                 $errors[] = 'Error saving Customer ' . $address_type . ' Address Details : ' . $db->ErrorMsg();
             }
         }
     }
     if (!empty($sorder_data['inv_address_id']) && $sorder_data['inv_address_id'] > 0) {
         $sorder->inv_address_id = $sorder_data['inv_address_id'];
     }
     if (!empty($sorder_data['del_address_id']) && $sorder_data['del_address_id'] > 0) {
         $sorder->del_address_id = $sorder_data['del_address_id'];
     } else {
         $sorder->del_address_id = $sorder_data['inv_address_id'];
     }
     if (count($errors) > 0 || !$sorder->save()) {
         $errors[] = 'Error updating customer order details';
     }
     // create and post the invoice
     if (count($errors) == 0) {
         $invoice = $this->createPostInvoice($sorder, $errors);
     }
     // Create the Payment
     if (count($errors) == 0) {
         $cb_data = array();
         $cb_data['cb_account_id'] = $slcustomer->cb_account_id;
         $cb_data['source'] = 'S';
         $cb_data['slmaster_id'] = $sorder->slmaster_id;
         $cb_data['company_id'] = $slcustomer->company_id;
         if ($sorder->person_id > 0) {
             $cb_data['person_id'] = $sorder->person_id;
         }
         $cb_data['ext_reference'] = $sorder_data['ext_reference'];
         $cb_data['payment_term_id'] = $slcustomer->payment_term_id;
         $cb_data['payment_type_id'] = $sorder_data['payment_type_id'];
         $cb_data['transaction_type'] = 'R';
         $cb_data['currency_id'] = $sorder->currency_id;
         $cb_data['transaction_date'] = date(DATE_FORMAT);
         // $cb_data['net_value'] = $sorder_data['gross_value'];
         $cb_data['net_value'] = bcsub($invoice->gross_value, $invoice->settlement_discount);
         if (!SLTransaction::saveTransaction($cb_data, $errors)) {
             $errors[] = 'Error saving Payment Details';
         }
     }
     // Match Payment to Invoice
     if (count($errors) == 0) {
         if ($invoice->isLoaded()) {
             $invoice_trans = DataObjectFactory::Factory('SLTransaction');
             $invoice_trans->identifierField = gross_value;
             $cc = new ConstraintChain();
             $cc->add(new Constraint('transaction_type', '=', 'I'));
             $cc->add(new Constraint('status', '=', 'O'));
             $cc->add(new Constraint('our_reference', '=', $invoice->invoice_number));
             $transactions = $invoice_trans->getAll($cc);
             $transactions[$cb_data['ledger_transaction_id']] = $cb_data['payment_value'];
             // Save settlement discount if present?
             if ($invoice->settlement_discount > 0) {
                 $payment_term = DataObjectFactory::Factory('PaymentTerm');
                 $payment_term->load($slcustomer->payment_term_id);
                 // Create GL Journal for settlement discount
                 $discount = $cb_data;
                 $discount['gross_value'] = $discount['net_value'] = $invoice->settlement_discount;
                 $discount['glaccount_id'] = $payment_term->sl_discount_glaccount_id;
                 $discount['glcentre_id'] = $payment_term->sl_discount_glcentre_id;
                 $discount['tax_value'] = '0.00';
                 $discount['source'] = 'S';
                 $discount['transaction_type'] = 'SD';
                 $discount['description'] = !empty($payment_term->sl_discount_description) ? $payment_term->sl_discount_description . ' ' : '';
                 $discount['description'] .= $cb_data->description;
                 $discount['status'] = 'P';
                 $sldiscount = SLTransaction::Factory($discount, $errors, 'SLTransaction');
                 if ($sldiscount && $sldiscount->save('', $errors) && $sldiscount->saveGLTransaction($discount, $errors)) {
                     $transactions[$sldiscount->{$sldiscount->idField}] = bcadd($discount['net_value'], 0);
                 } else {
                     $errors[] = 'Errror saving Settlement Discount : ' . $db->ErrorMsg();
                     $flash->addErrors($errors);
                 }
             }
             if (!SLTransaction::allocatePayment($transactions, $invoice->slmaster_id, $errors) || !SLAllocation::saveAllocation($transactions, $errors)) {
                 $errors[] = 'Error allocating Payment';
             }
         } else {
             $errors[] = 'Error matching to Invoice';
         }
     }
     // Check for errors
     if (count($errors) > 0) {
         $flash->clear();
         $flash->addErrors($errors);
         $flash->addError('Sale confirmation failed');
         $db->failTrans();
     } else {
         $flash->addMessage('Sale Confirmed');
     }
     $db->CompleteTrans();
     if (count($errors) == 0) {
         // now print the invoice to the users default printer
         $userPreferences = UserPreferences::instance(EGS_USERNAME);
         $defaultPrinter = $userPreferences->getPreferenceValue('default_printer', 'shared');
         if (empty($defaultPrinter)) {
             // Use normal print action
             // ATTN: what happens here? If we don't have a default printer set it's going to ignore our request?
             $this->_data['invoice_id'] = $invoice->id;
             $this->_data['printaction'] = 'printinvoice';
             parent::printaction();
             $this->printaction = array('Print' => 'Print');
             $this->printtype = array('pdf' => 'PDF');
             $this->_templateName = $this->getTemplateName('printaction');
             return;
         } else {
             // Overide print action
             $data = array();
             $data['invoice_id'] = $invoice->id;
             $data['printtype'] = 'pdf';
             $data['printaction'] = 'Print';
             $data['printer'] = $defaultPrinter;
             // call printInvoice and decode the response, output errors / messages
             $response = json_decode($this->printInvoice($invoice, $data));
             if ($response->status === true) {
                 $flash->addMessage('Print Sales Invoice Completed');
                 $invoice->update($invoice->id, array('date_printed', 'print_count'), array(fix_date(date(DATE_FORMAT)), $invoice->print_count + 1));
             } else {
                 $flash->addError('Print Sales Invoice Failed');
                 $flash->addError($response->message);
             }
         }
         // we're not using JavaScript to get here
         // so just go back to the order view
     }
     sendTo($this->name, 'view', $this->_modules, array('id' => $sorder->id));
 }
 private function create_standard($header, $errors = array())
 {
     if (isset($this->_data['reverse']) && $this->_data['reverse'] == 'yes') {
         $reversal = true;
     }
     // Create new header from selected header
     $newheader = array();
     foreach ($header->getFields() as $fieldname => $field) {
         $newheader[$fieldname] = $header->{$fieldname};
     }
     unset($newheader['id']);
     unset($newheader['docref']);
     $period = DataObjectFactory::Factory('GLPeriod');
     $period->getCurrentPeriod();
     $newheader['transaction_date'] = un_fix_date($period->enddate);
     $newheader['glperiods_id'] = $period->id;
     if ($reversal) {
         $newheader['comment'] = 'Reversal of Ref:' . $header->docref . ' ' . $newheader['comment'];
         if ($newheader['glperiods_id'] == $header->glperiods_id) {
             $newheader['glperiods_id'] = key($this->getPeriods($newheader['transaction_date']));
         }
     } elseif ($newheader['accruals'] == 't') {
         $newheader['accrual_period_id'] = key($this->getPeriods($newheader['transaction_date']));
     }
     $newheader['type'] = $header->standardJournal();
     // Save the header and transactions
     $result = parent::save($this->modeltype, $newheader, $errors);
     if ($result) {
         // Copy existing header journal transactions to unposted transactions table
         $header->setTransactionsCollection();
         $sh = $this->setSearchHandler($header->transactions);
         $sh->setFields('*');
         // Ignore any reverse accruals when copying posted transactions
         $header->setTransactionsConstraints($sh, TRUE);
         $transactions = $header->transactions->load($sh, null, RETURN_ROWS);
         $docref = $this->saved_model->docref;
         foreach ($transactions as $row) {
             unset($row['id']);
             $row['docref'] = $docref;
             if ($reversal) {
                 $row['value'] = bcmul($row['value'], -1);
             }
             $result = DataObject::Factory($row, $errors, $header->unpostedTransactionFactory());
             if (!$result) {
                 break;
             } else {
                 $newtrans[] = $result;
             }
         }
         if ($result) {
             foreach ($newtrans as $unposted) {
                 if (!$unposted->save()) {
                     $result = FALSE;
                     $errors[] = 'Error creating journal transactions : ' . $db->ErrorMsg();
                     break;
                 }
             }
         }
     }
     if ($result) {
         // now set header to saved model and go to view the new header
         return $this->saved_model;
     }
     return FALSE;
 }
Example #15
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $user = $this->_uses[$this->modeltype];
     $user_data = $this->_data[$this->modeltype];
     $username = $user_data['username'];
     $user = $user->load($username);
     $new_user = TRUE;
     if ($user !== FALSE) {
         $new_user = FALSE;
         if ($user_data['password'] == $user->password) {
             unset($this->_data[$this->modeltype]['password']);
         }
     }
     if (empty($user_data['lastcompanylogin'])) {
         $this->_data[$this->modeltype]['lastcompanylogin'] = EGS_COMPANY_ID;
     }
     if (isset($user_data['debug_options'])) {
         $debug = DebugOption::getUserOption($username);
         if (isset($user_data['debug_enabled'])) {
             $this->_data['DebugOption']['username'] = $username;
             $this->_data['DebugOption']['company_id'] = EGS_COMPANY_ID;
             $this->_data['DebugOption']['options'] = $debug->setOptions($user_data['debug_options']);
         } else {
             if ($debug->isLoaded()) {
                 $debug->delete();
                 unset($this->_data['DebugOption']);
             }
         }
     } else {
         unset($this->_data['DebugOption']);
     }
     if (!parent::save($this->modeltype)) {
         if ($new_user) {
             $this->refresh();
         } else {
             $this->_data['username'] = $this->_data[$this->modeltype]['username'];
             $this->edit();
         }
         return FALSE;
     }
     if ($new_user) {
         $uca = Usercompanyaccess::Factory(array('username' => $username, 'company_id' => EGS_COMPANY_ID, 'enabled' => TRUE), $errors, 'Usercompanyaccess');
         if ($uca) {
             if (!$uca->save()) {
                 $errors[] = 'Failed to save user company access';
             }
         }
         if (!$uca || count($errors)) {
             $flash->addErrors($errors);
             sendTo($this->name, 'index', $this->_modules);
         }
         $user = $this->saved_model;
         $password = $user->setPassword($user_data['password']);
         if (!is_null($user->person_id)) {
             $person = DataObjectFactory::Factory('Person');
             $person->load($user->person_id);
             $to = $person->email->contactmethod;
         } else {
             $to = '';
         }
         if (empty($to)) {
             $to = $user->email;
         }
         if ($to != '') {
             $message = "You have been created an account for " . SERVER_ROOT . "\n" . "Your username is {$user->username}\n" . "Your password is {$password}\n" . "Thank you";
             $subject = 'New Account';
             $headers = 'From: ' . get_config('ADMIN_EMAIL') . "\r\n" . 'X-Mailer: PHP/' . phpversion();
             mail($to, $subject, $message, $headers);
         }
     }
     if (isset($this->_data[$this->modeltype]['roles']) && is_array($this->_data[$this->modeltype]['roles'])) {
         $roles = $this->_data[$this->modeltype]['roles'];
         User::setRoles($user, $roles, $errors);
         $flash->addErrors($errors);
     }
     sendTo($this->name, 'index', $this->_modules);
 }
Example #16
0
 public function save()
 {
     $flash = Flash::Instance();
     if (parent::save('Activity')) {
         // Ensures return to viewing the new activity after saving a follow-up
         if ($_SESSION['refererPage']['controller'] == $this->name && $_SESSION['refererPage']['action'] == 'view') {
             sendTo($this->name, 'view', $this->_modules, ['id' => $this->saved_model->id]);
         }
         // Return to the refering page, e.g. when adding an activity from the contacts module
         sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null);
     } else {
         $this->refresh();
     }
 }
Example #17
0
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     $company = $this->_uses['Lead'];
     if (isset($this->_data['Lead'][$company->idField]) && !empty($this->_data['Lead'][$company->idField])) {
         $company->load($this->_data['Lead'][$company->idField]);
         if ($company === false) {
             echo 'Could not load Company for id=' . $this->_data['Lead'][$person->idField] . ' - Abandoned<br>';
             sendBack();
         }
     }
     $db =& DB::Instance();
     $db->StartTrans();
     $partycontactmethod = DataObjectFactory::Factory('PartyContactMethod');
     foreach ($partycontactmethod->getEnumOptions('type') as $key => $type) {
         if (isset($this->_data[$type]['PartyContactMethod']) && isset($this->_data[$type]['Contactmethod']) && empty($this->_data[$type]['Contactmethod']['contact'])) {
             if (!empty($this->_data[$type]['PartyContactMethod'][$partycontactmethod->idField])) {
                 $partycontactmethod->delete($this->_data[$type]['PartyContactMethod'][$partycontactmethod->idField], $errors);
             }
             unset($this->_data[$type]);
         }
     }
     if (count($errors) == 0 && parent::save('Lead')) {
         foreach ($this->saved_models as $model) {
             if (isset($model['Lead'])) {
                 $company = $model['Lead'];
                 break;
             }
         }
         $this->company_id = $company->id;
         if (isset($this->_data['Lead']['crm'])) {
             $crm_data = $this->_data['Lead']['crm'];
             $ao =& AccessObject::Instance(EGS_USERNAME);
             if ($ao->hasPermission('crm')) {
                 $crm_data['company_id'] = $company->{$company->idField};
                 parent::save('CompanyCrm', $crm_data);
             }
         }
         $category = DataObjectFactory::Factory('CompanyInCategories');
         $current_categories = $category->getCategoryID($company->{$company->idField});
         $check_categories = array();
         if (isset($this->_data['ContactCategories'])) {
             $delete_categories = array_diff($current_categories, $this->_data['ContactCategories']['category_id']);
             $insert_categories = array_diff($this->_data['ContactCategories']['category_id'], $current_categories);
         }
         $result = TRUE;
         if (!empty($delete_categories)) {
             $result = $category->delete(array_keys($delete_categories), $errors);
         }
         if (!empty($insert_categories) && $result) {
             $result = $category->insert($insert_categories, $company->{$company->idField});
         }
         if ($result) {
             $db->CompleteTrans();
             sendTo($this->name, 'view', $this->_modules, array($company->idField => $company->{$company->idField}));
         }
     }
     // Errors
     $flash->addErrors($errors);
     $db->FailTrans();
     $db->CompleteTrans();
     $this->refresh();
 }
Example #18
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $data = $this->_data[$this->modeltype];
     $data['item_code'] = strtoupper($data['item_code']);
     $update_cost = FALSE;
     $stitem = $this->_uses[$this->modeltype];
     if (!empty($data['id'])) {
         $stitem->load($data['id']);
     }
     if ($data['comp_class'] == 'B') {
         $data['latest_cost'] = $data['latest_mat'];
         $data['latest_lab'] = 0;
         $data['latest_osc'] = 0;
         $data['latest_ohd'] = 0;
         if ($stitem->isLoaded()) {
             $old_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $stitem->latest_ohd);
             $new_costs = array($data['latest_cost'], $data['latest_mat'], $data['latest_lab'], $data['latest_osc'], $data['latest_ohd']);
             $total_costs = count($old_costs);
             for ($i = 0; $i < $total_costs; $i++) {
                 if (bccomp($old_costs[$i], $new_costs[$i], $stitem->cost_decimals) != 0) {
                     $update_cost = true;
                     break;
                 }
             }
         } elseif ($data['latest_cost'] > 0) {
             $update_cost = true;
         }
     } else {
         unset($data['latest_mat']);
     }
     $product_data = array();
     if ($stitem->isLoaded()) {
         if (is_null($stitem->obsolete_date) && !empty($data['obsolete_date'])) {
             $product_data['end_date'] = 'obsolete_date';
         }
         if ($data['prod_group_id'] != $stitem->prod_group_id) {
             $product_data['prod_group_id'] = 'prod_group_id';
         }
     }
     if (!$update_cost && $stitem->isLoaded()) {
         $update_cost = $data['uom_id'] != $stitem->uom_id || $data['cost_decimals'] != $stitem->cost_decimals || $data['comp_class'] != $stitem->comp_class || strlen($data['obsolete_date']) > 0 && fix_date($data['obsolete_date']) != $stitem->obsolete_date;
     }
     if (parent::save($this->modeltype, $data, $errors)) {
         if ($update_cost) {
             $this->saved_model->calcLatestCost();
             if ($this->saved_model->saveCosts() && STCost::saveItemCost($this->saved_model)) {
                 if (!$this->saved_model->rollUp(STItem::ROLL_UP_MAX_LEVEL)) {
                     $errors[] = 'Could not roll-up latest costs';
                     $db->FailTrans();
                 }
             } else {
                 $errors[] = 'Could not save latest costs';
                 $db->FailTrans();
             }
         }
         if (!empty($product_data)) {
             // Need to cascade data changes to linked current products
             // if they exist
             $products['PO'] = $this->saved_model->getPOProductlineHeader();
             $products['SO'] = $this->saved_model->getSOProductlineHeader();
             foreach ($products as $type => $product) {
                 if ($product->isLoaded()) {
                     foreach ($product_data as $field => $value) {
                         $product->{$field} = $this->saved_model->{$value};
                     }
                     if (!$product->save()) {
                         $errors[] = 'Error updating ' . $type . ' Product : ' . $db->ErrorMsg();
                         $db->FailTrans();
                     }
                 }
             }
         }
     } else {
         $errors[] = 'Could not save stock item';
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $this->refresh();
     } elseif (isset($this->_data['saveform'])) {
         sendTo($this->name, 'view', $this->_modules, array('id' => $this->saved_model->id));
     } else {
         sendTo($this->name, 'new', $this->_modules);
     }
 }
Example #19
0
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     $calendar = $this->_templateobject;
     if (!isset($this->_data['CRMCalendar']['colour'])) {
         $colours = $calendar->getEnumOptions('colour');
         $this->_data['CRMCalendar']['colour'] = $colours[array_rand($colours)];
     }
     if (parent::save('CRMCalendar', '', $errors)) {
         sendBack();
     } else {
         sendBack();
     }
 }
Example #20
0
 public function save_data_input()
 {
     $flash = Flash::Instance();
     $errors = array();
     $_SESSION['di_search'] = $this->_data['Search'];
     if (parent::save('GLBudgetCollection', '', $errors)) {
         sendBack();
     } else {
         $flash->addErrors($errors);
         sendBack();
     }
 }
 public function save()
 {
     if (!$this->checkParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $errors = array();
     $contains = array();
     foreach ($this->_data[$this->modeltype] as $key => $packinglist) {
         if (is_array($packinglist) && isset($packinglist['contains'])) {
             if ($packinglist['contains'] > $packinglist['available']) {
                 $errors[] = 'Pack qty for ' . $key . ' exceeds that available for packing';
             } else {
                 $contains[$key] = $packinglist['contains'];
             }
         }
     }
     $this->_data[$this->modeltype]['contents'] = base64_encode(serialize($contains));
     if (count($errors) == 0 && parent::save($this->modeltype)) {
         sendTo('sorders', 'view', $this->_modules, array('id' => $this->_data[$this->modeltype]['order_id']));
     } else {
         $flash->addErrors($errors);
         $this->_data['order_id'] = $this->_data[$this->modeltype]['order_id'];
         $this->refresh();
     }
 }
Example #22
0
 public function save()
 {
     $flash = Flash::Instance();
     $errors = array();
     $modelname = get_class($this->_templateobject);
     if (!$this->checkParams($modelname)) {
         sendBack();
     }
     $company = $this->_templateobject;
     $companydata = $this->_data[$modelname];
     $companyidfield = $company->idField;
     $companyid = '';
     if (isset($companydata[$companyidfield]) && !empty($companydata[$companyidfield])) {
         $companyid = $companydata[$companyidfield];
         $company->load($companyid);
         if ($company === false) {
             $flash->addError('Could not load Company for id=' . $companyid . ' - Abandoned');
             sendBack();
         }
     }
     $db =& DB::Instance();
     $db->StartTrans();
     $partycontactmethod = DataObjectFactory::Factory('PartyContactMethod');
     foreach ($partycontactmethod->getEnumOptions('type') as $key => $type) {
         if (isset($this->_data[$type]['PartyContactMethod']) && isset($this->_data[$type]['Contactmethod']) && empty($this->_data[$type]['Contactmethod']['contact'])) {
             if (!empty($this->_data[$type]['PartyContactMethod'][$partycontactmethod->idField])) {
                 $partycontactmethod->delete($this->_data[$type]['PartyContactMethod'][$partycontactmethod->idField], $errors);
             }
             unset($this->_data[$type]);
         }
     }
     if (count($errors) == 0 && parent::save($modelname, $this->_data, $errors)) {
         foreach ($this->saved_models as $model) {
             if (isset($model[$modelname])) {
                 $company = $model[$modelname];
                 break;
             }
         }
         $this->company_id = $company->{$companyidfield};
         if (isset($companydata['crm'])) {
             $crm_data = $companydata['crm'];
             $ao =& AccessObject::Instance(EGS_USERNAME);
             if ($ao->hasPermission('crm')) {
                 $crm_data['company_id'] = $company->{$companyidfield};
                 parent::save('CompanyCrm', $crm_data);
             }
         }
         $company_category = DataObjectFactory::Factory('CompanyInCategories');
         $current_categories = $company_category->getCategoryID($company->{$companyidfield});
         $check_categories = array();
         $delete_categories = array();
         $insert_categories = array();
         $new_categories = array();
         if (isset($this->_data['ContactCategories'])) {
             $delete_categories = array_diff($current_categories, $this->_data['ContactCategories']['category_id']);
             $insert_categories = array_diff($this->_data['ContactCategories']['category_id'], $current_categories);
             $new_categories = array_diff($current_categories, $delete_categories);
             $new_categories += $insert_categories;
         } else {
             $delete_categories = $current_categories;
         }
         $ledger_category = DataObjectFactory::Factory('LedgerCategory');
         $ledger_types = $ledger_category->checkCompanyUsage($company->{$companyidfield});
         foreach ($ledger_types as $ledger_type => $categories) {
             if ($categories['exists'] && !array_intersect($categories['categories'], $new_categories)) {
                 foreach (array_intersect($categories['categories'], $delete_categories) as $category_id) {
                     $category = DataObjectFactory::Factory('ContactCategory');
                     $category->load($category_id);
                     $errors[$category->name] = 'Cannot remove category ' . $category->name . ' - ' . $ledger_type . ' entry exists';
                 }
             }
         }
         $result = count($errors) == 0;
         if (!empty($delete_categories) && $result) {
             // All OK, so delete the associations
             $result = $company_category->delete(array_keys($delete_categories), $errors);
         }
         if (!empty($insert_categories) && $result) {
             // No errors and some new categories to assign to the company
             $result = $company_category->insert($insert_categories, $company->{$companyidfield});
         }
         if ($result) {
             // All OK
             $db->CompleteTrans();
             sendTo($this->name, 'view', $this->_modules, array($companyidfield => $company->{$companyidfield}));
         }
     }
     // Errors
     $flash->addErrors($errors);
     $db->FailTrans();
     $db->CompleteTrans();
     $this->refresh();
 }