public static function Factory($header_data, $lines_data, &$errors) { if (!isset($header_data['transfer_number'])) { $gen_id = new WarehouseTransferNumberHandler(); $header_data['transfer_number'] = $gen_id->handle(new whtransfer()); } $transfer_header = DataObject::Factory($header_data, $errors, 'WHTransfer'); if (count($errors) == 0 && $transfer_header) { if (count($lines_data) > 0) { foreach ($lines_data as $line) { $line['wh_transfer_id'] = $transfer_header->id; $line['from_whlocation_id'] = $header_data['from_whlocation_id']; $line['to_whlocation_id'] = $header_data['to_whlocation_id']; $transfer_line = DataObject::Factory($line, $errors, 'WHTransferline'); if ($transfer_line) { $transfer_header->unsaved_lines[] = $transfer_line; } else { $errors[] = 'Errors in Transfer Line'; break; } } } else { $errors[] = 'No Transfer Lines'; } } return $transfer_header; }
public static function makeLine($data, $do, &$errors) { //net value is unit-price * quantity if (!isset($data['tax_value'])) { //tax (in the UK at least) is dependent on the tax_rate of the item, and the tax status of the customer. //this function is a wrapper to a call to a config-dependent method $data['tax_percentage'] = calc_tax_percentage($data['tax_rate_id'], $data['tax_status_id'], $data['net_value']); $data['tax_value'] = round(bcmul($data['net_value'], $data['tax_percentage'], 4), 2); $data['tax_rate_percent'] = bcmul($data['tax_percentage'], 100); } else { $tax_rate = DataObjectFactory::Factory('TaxRate'); $tax_rate->load($data['tax_rate_id']); $data['tax_rate_percent'] = $tax_rate->percentage; } //gross value is net + tax; use bcadd to format the data $data['tax_value'] = bcadd($data['tax_value'], 0); $data['gross_value'] = bcadd($data['net_value'], $data['tax_value']); //then convert to the base currency if ($data['rate'] == 1) { $data['base_net_value'] = $data['net_value']; $data['base_tax_value'] = $data['tax_value']; $data['base_gross_value'] = $data['gross_value']; } else { $data['base_net_value'] = round(bcdiv($data['net_value'], $data['rate'], 4), 2); $data['base_tax_value'] = round(bcdiv($data['tax_value'], $data['rate'], 4), 2); $data['base_gross_value'] = round(bcadd($data['base_tax_value'], $data['base_net_value']), 2); } //and to the twin-currency $data['twin_net_value'] = round(bcmul($data['base_net_value'], $data['twin_rate'], 4), 2); $data['twin_tax_value'] = round(bcmul($data['base_tax_value'], $data['twin_rate'], 4), 2); $data['twin_gross_value'] = round(bcadd($data['twin_tax_value'], $data['twin_net_value']), 2); return DataObject::Factory($data, $errors, $do); }
function insert($ids = null, $company_id = null, &$errors = array()) { if (empty($ids) || empty($company_id)) { $errors[] = 'Invalid/incomplete data trying to insert Company Categories'; return FALSE; } if (!is_array($ids)) { $ids = array($ids); } $categories = array(); foreach ($ids as $id) { $category = DataObject::Factory(array('category_id' => $id, 'company_id' => $company_id), $errors, get_class($this)); if ($category) { $categories[] = $category; } else { $errors[] = 'Error validating Company Category'; return FALSE; } } $db = DB::Instance(); foreach ($categories as $category) { if (!$category->save()) { $errors = $db->ErrorMsg(); $db->FailTrans(); $db->completeTrans(); return FALSE; } } return $db->completeTrans(); }
public static function makeHeader($data, $do, &$errors) { if (strtotime(fix_date($data['order_date'])) > strtotime(fix_date(date(DATE_FORMAT)))) { $errors[] = 'Order Date cannot be in the future'; return false; } if (!isset($data['id']) || $data['id'] == '') { // $generator = new OrderNumberHandler(); $generator = new UniqueNumberHandler(false, $data['type'] != 'T'); $data['order_number'] = $generator->handle(DataObjectFactory::Factory($do)); $data['status'] = 'N'; $user = getCurrentUser(); $data['raised_by'] = $user->username; } //determine the base currency $currency = DataObjectFactory::Factory('Currency'); $currency->load($data['currency_id']); $data['rate'] = $currency->rate; //determine the twin currency $glparams = DataObjectFactory::Factory('GLParams'); $twin_currency = DataObjectFactory::Factory('Currency'); $twin_currency->load($glparams->base_currency()); $data['twin_rate'] = $twin_currency->rate; $data['twin_currency_id'] = $twin_currency->id; return DataObject::Factory($data, $errors, $do); }
public static function Factory(&$data, &$errors, $do) { $data['due_date'] = $data['transaction_date']; if (isset($data['ext_reference']) && !isset($data['our_reference'])) { $data['our_reference'] = $data['ext_reference']; } elseif (isset($data['reference']) && !isset($data['our_reference'])) { $data['our_reference'] = $data['reference']; } if (isset($data['description']) && !isset($data['comment'])) { $data['comment'] = $data['description']; } if (isset($data['comment']) && !isset($data['description'])) { $data['description'] = $data['comment']; } $mult = static::$multipliers[$data['source']][$data['transaction_type']]; $data['net_value'] = $data['net_value'] * $mult; self::setCurrency($data); //the outstanding (os) values are the gross values to begin with $prefixes = array('', 'base_', 'twin_'); foreach ($prefixes as $prefix) { $data[$prefix . 'os_value'] = $data[$prefix . 'gross_value']; } // Validate the Ledger Transactions if (empty($data['status'])) { $data['status'] = 'O'; } return parent::Factory($data, $errors, $do); }
function copyStructure($data, &$errors) { $mfstructures = new MFStructureCollection(DataObjectFactory::Factory('MFStructure')); $cc1 = new ConstraintChain(); $cc1->add(new Constraint('stitem_id', '=', $data->stitem_id)); $cc1->add(new Constraint('start_date', '<=', fix_date(date(DATE_FORMAT)))); $cc2 = new ConstraintChain(); $cc2->add(new Constraint('end_date', '>=', fix_date(date(DATE_FORMAT)))); $cc2->add(new Constraint('end_date', 'is', 'NULL'), 'OR'); $sh = new SearchHandler($mfstructures, false); $sh->addConstraintChain($cc1); $sh->addConstraintChain($cc2); $mfstructures->load($sh); $wo_structure = array(); $wo_structures = array(); $copyfields = array('line_no', 'qty', 'uom_id', 'remarks', 'waste_pc', 'ststructure_id'); foreach ($mfstructures as $input) { $wo_structure['work_order_id'] = $data->id; foreach ($copyfields as $field) { $wo_structure[$field] = $input->{$field}; } $wo_structures[$input->line_no] = DataObject::Factory($wo_structure, $errors, 'MFWOStructure'); } return $wo_structures; }
public static function Factory($data, &$errors = array(), $do_name = null) { if (empty($data['calendar_week'])) { $data['calendar_week'] = date('W', strtotime(fix_date($data['period_start_date'], DATE_TIME_FORMAT, $errors))); } return parent::Factory($data, $errors, $do_name); }
static function Factory($data, &$errors, $do) { if (!isset($data['id']) || $data['id'] == '') { $generator = new ComplaintNumberHandler(); $data['complaint_number'] = $generator->handle(new $do()); } return parent::Factory($data, $errors, $do); }
public static function Factory($data, &$errors = array(), $do_name = null) { if ($data['debit'] > 0) { $data['value'] = BCADD($data['debit'], 0); } elseif ($data['credit'] > 0) { $data['value'] = BCMUL($data['credit'], -1); } return parent::Factory($data, $errors, $do_name); }
static function Factory($data, &$errors, $do_name = __CLASS__) { $do = DataObjectFactory::Factory($do_name); $db = DB::Instance(); if (empty($data['docref'])) { $data['docref'] = $db->GenID('gl_transactions_docref_seq'); } $glperiod = GLPeriod::getPeriod(fix_date($data['transaction_date'])); if (!$glperiod || count($glperiod) == 0) { $errors[] = 'No period exists for this date'; } $data['glperiods_id'] = $glperiod['id']; $data['status'] = $do->newStatus(); return parent::Factory($data, $errors, $do); }
public function save($errno, $errstr, $errfile, $errline) { $db = DB::Instance(); $db->CompleteTrans(); $errors = array(); $flash = Flash::Instance(); $data = array('error_number' => $errno, 'error_message' => $errstr, 'error_file' => $errfile, 'error_line' => $errline); $error = DataObject::Factory($data, $errors, 'ErrorHandler'); if (!$error || count($errors) > 0 || !$error->save()) { $flash->addError('System Error - ' . $errstr . ' in ' . $errfile . ' at ' . $errline); } else { $flash->addError('System Error (Ref:' . $error->id . ' - ' . $errstr); } return false; }
public static function Factory($data, &$errors) { $hash = $data['cb_account_id']; $hash .= $data['currency_id']; $hash .= $data['payment_type_id']; $hash .= $data['reference']; $hash .= fix_date($data['payment_date']); $hash .= $data['number_transactions']; $hash .= $data['payment_total']; $progressbar = new progressBar('create_security_key'); $callback = function ($pl_data, $key) use(&$hash, &$errors) { $hash .= $pl_data['plmaster_id']; $hash .= bcadd($pl_data['net_value'], 0); }; $progressbar->process($data['PLTransaction'], $callback); $data['hash_key'] = self::generateHashcode($hash); return parent::Factory($data, $errors, 'PLPayment'); }
/** * Build the invoice header, as if from a submitted form * */ public static function makeHeader($data, $do, &$errors) { if (!isset($data['id']) || $data['id'] == '') { $generator = new UniqueNumberHandler(false, $data['transaction_type'] != 'T'); $data['invoice_number'] = $generator->handle(DataObjectFactory::Factory($do)); $data['status'] = 'N'; } //determine the base currency $currency = DataObjectFactory::Factory('Currency'); $currency->load($data['currency_id']); $data['rate'] = $currency->rate; //determine the twin currency $glparams = DataObjectFactory::Factory('GLParams'); $twin_currency = DataObjectFactory::Factory('Currency'); $twin_currency->load($glparams->twin_currency()); $data['twin_rate'] = $twin_currency->rate; $data['twin_currency_id'] = $twin_currency->id; $terms = DataObjectFactory::Factory('PaymentTerm'); $terms->load($data['payment_term_id']); $today = date(DATE_FORMAT); if (empty($data['invoice_date'])) { $data['invoice_date'] = $today; } if ($data['transaction_type'] == 'I') { $data['due_date'] = calc_due_date($data['invoice_date'], $terms->basis, $terms->days, $terms->months); } else { if (fix_date($data['due_date']) < fix_date($today)) { $data['due_date'] = $today; } } $data['original_due_date'] = $data['due_date']; // build the lines - header totals are built from the lines // so initialise the values to zero $sums = array('net_value', 'tax_value', 'gross_value'); $prefixes = array('', 'twin_', 'base_'); foreach ($prefixes as $prefix) { foreach ($sums as $sum) { $data[$prefix . $sum] = '0.00'; } } return DataObject::Factory($data, $errors, $do); }
static function saveAllocation($transactions, $payment_id, &$errors) { $db = DB::Instance(); $db->StartTrans(); $alloc_id = $db->GenID('pl_allocation_id_seq'); foreach ($transactions as $id => $value) { // $trans = new PLTransaction(); // $trans->load($id); $data = array('allocation_id' => $alloc_id, 'transaction_id' => $id, 'payment_id' => $payment_id, 'payment_value' => $value); $alloc = DataObject::Factory($data, $errors, 'PLAllocation'); if (count($errors) > 0 || !$alloc->save()) { break; } } if (count($errors) > 0) { $db->FailTrans(); $db->CompleteTrans(); return false; } return $db->CompleteTrans(); }
public static function setUsers($role, $users, &$errors = array()) { if (!$role instanceof Role) { $roleid = $role; $role = new Role(); $role = $role->load($roleid); if ($role === false) { return false; } } $db = DB::Instance(); $db->StartTrans(); $query = "delete from hasrole where roleid=" . $db->qstr($role->id); $db->Execute($query); foreach ($users as $user) { $ob = DataObject::Factory(array('roleid' => $role->id, 'username' => $user), $errors, 'HasRole'); if (count($errors) == 0 && $ob) { $ob->save(); } } $db->CompleteTrans(); }
public function save() { if (!$this->checkParams($this->modeltype)) { sendBack(); } $flash = Flash::Instance(); $errors = array(); $db = DB::Instance(); $db->StartTrans(); if (empty($this->_data[$this->modeltype]['person_id'])) { $sc = DataObjectFactory::Factory('Systemcompany'); $sc->load(EGS_COMPANY_ID); $data = array('title' => $this->_data[$this->modeltype]['title'], 'firstname' => $this->_data[$this->modeltype]['firstname'], 'middlename' => $this->_data[$this->modeltype]['middlename'], 'surname' => $this->_data[$this->modeltype]['surname'], 'suffix' => $this->_data[$this->modeltype]['suffix'], 'company_id' => $sc->company_id, 'jobtitle' => $this->_data[$this->modeltype]['jobtitle'], 'department' => $this->_data[$this->modeltype]['department'], 'reports_to' => $this->_data[$this->modeltype]['reports_to']); $person = DataObject::Factory($data, $errors, 'Person'); if ($person && $person->save()) { // Person does not exist, so create them $this->_data[$this->modeltype]['person_id'] = $person->id; } else { $errors[] = 'Error saving new employee details : ' . $db->ErrorMsg(); } } else { $person = DataObjectFactory::Factory('Person'); $person->load($this->_data[$this->modeltype]['person_id']); if (empty($this->_data[$this->modeltype]['reports_to'])) { $this->_data[$this->modeltype]['reports_to'] = 'NULL'; } if (!$person->isLoaded() || !$person->update($person->id, array('title', 'firstname', 'middlename', 'surname', 'suffix', 'jobtitle', 'department', 'reports_to'), array($this->_data[$this->modeltype]['title'], $this->_data[$this->modeltype]['firstname'], $this->_data[$this->modeltype]['middlename'], $this->_data[$this->modeltype]['surname'], $this->_data[$this->modeltype]['suffix'], $this->_data[$this->modeltype]['jobtitle'], $this->_data[$this->modeltype]['department'], $this->_data[$this->modeltype]['reports_to']))) { $errors[] = 'Error updating employee details' . $db->ErrorMsg(); } } if (count($errors) == 0 && isset($this->_data[$this->modeltype]['person_id'])) { // Link them to any categories identified as Employee Categories $categories = DataObjectFactory::Factory('PeopleInCategories'); // Get list of current categories assigned to this person $current_categories = $categories->getCategoryID($person->id); $ledger_category = DataObjectFactory::Factory('LedgerCategory'); // Get the people categories linked to HR $employee_categories = $ledger_category->getCategoriesByModel('employee'); // Get list of people categories not currently assigned to this person $insert_categories = array_diff($employee_categories, $current_categories); // Not deleting any categories here - may need to add delete later (see PersonsController::save) if (count($insert_categories) > 0) { $categories->insert($employee_categories, $person->id, $errors); } $ee_data[$this->modeltype] = $this->_data[$this->modeltype]; if (isset($this->_data['saveAnother'])) { // Override default saveAnother called from parent::save $save_another = TRUE; unset($this->_data['saveAnother']); } if (parent::save($this->modeltype, $ee_data, $errors)) { $this->saved_model->saveAuthorisation($this->_data[$this->modeltype], 'authorisation_type', $errors, 'HRAuthoriser'); $this->saved_model->saveAuthorisers($this->_data[$this->modeltype], 'expense_authorisers_id', $errors, 'ExpenseAuthoriser'); $this->saved_model->saveAuthorisers($this->_data[$this->modeltype], 'holiday_authorisers_id', $errors, 'HolidayAuthoriser'); } } if (count($errors) == 0) { $db->CompleteTrans(); if ($save_another) { $this->saveAnother(); } $idfield = $this->_templateobject->idField; sendTo($this->name, 'view', $this->_modules, array($idfield => $this->saved_model->{$idfield})); } $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->refresh(); }
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 save_item() { $option = $this->checkValidOption() or sendBack(); $modelname = $this->setup_options[$option]; $db = DB::Instance(); if (isset($this->_data[$modelname])) { if (empty($this->_data[$modelname]['id'])) { $action = 'added'; $update = FALSE; } else { $action = 'updated'; $update = TRUE; } if (!empty($this->_data[$modelname]['position'])) { if ($update) { $model = DataObjectFactory::Factory($modelname); $model->load($this->_data[$modelname]['id']); $current_position = $model->position; } else { $model = DataObjectFactory::Factory($modelname); $model->loadBy('position', $this->_data[$modelname]['position']); if (!$model->isLoaded()) { // No need to update sequences because new sequence is not used $current_position = $this->_data[$modelname]['position']; } } $this->updatePositions($modelname, 'position', $this->_data[$modelname]['position'], $current_position); } $flash = Flash::Instance(); $errors = array(); $model = DataObject::Factory($this->_data[$modelname], $errors, $modelname); if ($model && $model->save()) { $flash->addMessage('Item ' . $action . ' successfully'); sendTo($this->name, 'view', $this->_modules, array('option' => $option)); } else { $errors[] = 'Error saving item : ' . $db->ErrorMsg(); $flash->addErrors($errors); sendBack(); } } }
private function validate_new($data, $employee, &$errors = array()) { // TODO: Move to hr_controller as parent; see also EmployeecontractdetailsController // Check that the new rate does not overlap an existing rate of this type // Standard rate start date must either match the employee start date // if this is only rate for the employee, or must be one day greater // than an existing rate $existing = DataObjectFactory::Factory($this->modeltype); $existing->getLatest($data['employee_id'], $data['payment_type_id']); $new_start_date = fix_date($data['start_date']); $set_end_date = fix_date(date(DATE_FORMAT, strtotime($new_start_date . ' -1 day'))); $db = DB::Instance(); $db->StartTrans(); if ($existing->isLoaded()) { // Rules : // Input start date must be after existing start date if ($existing->start_date >= $new_start_date) { $errors[] = 'Start date must be after latest rate start'; } elseif (is_null($existing->end_date)) { // Close off existing end date as day before new start date $existing->end_date = $set_end_date; if (!$existing->save()) { $errors[] = 'Error closing off latest entry : ' . $db->ErrorMsg(); } } } if (count($errors == 0)) { $new = DataObject::Factory($data, $errors, $this->modeltype); if ($new && !$new->save()) { $errors[] = 'Error saving new details : ' . $db->ErrorMsg(); } } if (count($errors) > 0) { $db->FailTrans(); } $db->CompleteTrans(); }
public function saveResources($resource_ids = array(), &$errors = array()) { if (!is_array($resource_ids) || empty($resource_ids)) { $errors[] = 'Invalid Resource Id'; return false; } $eng_resource = new EngineeringResource(); $eng_resources = new EngineeringResourceCollection($eng_resource); $sh = new SearchHandler($eng_resources, FALSE); $sh->addConstraint(new Constraint('work_schedule_id', '=', $this->id)); if ($eng_resources->delete($sh) === FALSE) { $errors[] = 'Error deleting existing resources'; return false; } $data = array('work_schedule_id' => $this->id); foreach ($resource_ids as $resource_id) { $data['id'] = ''; $data['resource_id'] = $resource_id; $eng_resource = DataObject::Factory($data, $errors, 'EngineeringResource'); if (!$eng_resource || !$eng_resource->save()) { $errors[] = 'Error saving resources'; return false; } } return true; }
public function save() { if (!$this->CheckParams($this->modeltype)) { sendBack(); } $this->loadData(); $systemcompany = $this->_uses[$this->modeltype]; $db = DB::Instance(); $db->StartTrans(); $errors = array(); $flash = Flash::Instance(); $data = $this->_data[$this->modeltype]; if (!isset($data['id']) || empty($data['id'])) { $company = DataObject::Factory($data, $errors, 'Company'); if (count($errors) > 0 || !$company->save()) { $errors[] = 'Failed to create Company'; $db->FailTrans(); } else { $data['company_id'] = $company->id; } } if (isset($data['debug_options']) && isset($data['id'])) { $debug = DebugOption::getCompanyOption($data['id']); if (isset($data['debug_enabled'])) { $data['DebugOption']['company_id'] = $data['id']; $data['DebugOption']['options'] = $debug->setOptions($data['debug_options']); } else { if ($debug->isLoaded()) { $debug->delete(); } unset($data['DebugOption']); } } else { unset($data['DebugOption']); } if (isset($data['delete_logo'])) { if ($this->delete_logo($systemcompany->logo_file_id, $errors)) { $data['logo_file_id'] = NULL; } else { $errors[] = 'Error deleting Logo image'; } } if (!empty($_FILES['file']['name'])) { // Need to upload file before checking $file = File::Factory($_FILES['file'], $errors, DataObjectFactory::Factory('File')); if ($file->save()) { if (!is_null($systemcompany->logo_file_id)) { $old_file = DataObjectFactory::Factory('File'); if (!$this->delete_logo($systemcompany->logo_file_id, $errors)) { $errors[] = 'Error replacing Logo image'; } } $data['logo_file_id'] = $file->id; } else { $errors[] = 'Error loading Logo image'; } } if (count($errors) == 0) { if (!parent::save($this->modeltype, $data, $errors)) { $errors[] = 'Failed to save System Company'; $db->FailTrans(); } } if (count($errors) == 0) { $result = $this->saved_model->setPermissions($this->_data['Permission'], $errors); if ($result === FALSE) { $errors[] = 'Failed to save System Company Permissions'; $db->FailTrans(); } } $db->CompleteTrans(); if (count($errors) > 0) { $flash->addErrors($errors); if (isset($data['id'])) { $this->_data['id'] = $data['id']; } $this->refresh(); } else { sendTo($this->name, 'index', $this->_modules); } }
public function save_permissions() { if (!$this->checkParams(array('ModuleComponent', 'ModuleObject'))) { sendBack(); } if (!$this->loadData()) { sendBack(); } $moduleobject = $this->_uses[$this->modeltype]; $idField = $moduleobject->idField; $idValue = $moduleobject->{$moduleobject->idField}; $errors = array(); $flash = Flash::Instance(); $db = DB::Instance(); $db->Debug(); $db->StartTrans(); $components = $this->_data['ModuleComponent']; $current_components = array(); foreach ($components as $key => $component) { // delete any registered components that are de-registered if (!isset($component['register'])) { if (!empty($component['id'])) { $modulecomponent = new ModuleComponent(); $modulecomponent->delete($component['id']); } unset($components[$key]); } else { // create list of current registered components if (!empty($component['id'])) { $current_components[$component['id']] = $key; } } } // delete any entries that no longer exist in the file system foreach ($moduleobject->module_components as $module_component) { if (!isset($current_components[$module_component->id])) { $module_component->delete(); } } foreach ($components as $data) { $component = DataObject::Factory($data, $errors, 'ModuleComponent'); if (!$component || !$component->save()) { $errors[] = 'Failed to save components'; break; } } if (count($errors) > 0) { $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->_data['id'] = $idValue; $this->refresh(); return; } else { $moduleobject->update($idValue, 'registered', true); $flash->addMessage('components added'); } $db->CompleteTrans(); sendTo($this->name, 'index', $this->_modules); }
public static function Factory($data, &$errors = array(), $do_name = 'Employee') { $unique_field = 'employee_number'; $do = DataObjectFactory::Factory($do_name); if (empty($data[$do->idField]) && empty($data[$unique_field])) { $do->identifierField = $unique_field; $generator = new UniqueNumberHandler(); $data[$unique_field] = $generator->handle($do); } return parent::Factory($data, $errors, $do); }
public static function Factory(array $data, array &$errors, $do_name = null) { if (empty($data['name'])) { return false; } if (!is_uploaded_file($data['tmp_name'])) { $errors[] = 'Error with file upload- it would appear you\'re trying to be naughty'; } $new_name = FILE_ROOT . 'data/tmp/' . uniqid('file'); if (!move_uploaded_file($data['tmp_name'], $new_name)) { $errors[] = 'Error moving uploaded file, contact the server admin'; } if (!chmod($new_name, 0655)) { $errors[] = 'Error changing permission of uploaded file, contact the server admin'; } $file = parent::Factory($data, $errors, $do_name); if ($file instanceof File) { $file->tmp_name = $new_name; } return $file; }
static function Factory($data, &$errors, $do) { if (!isset($data['expenses_header_id'])) { $errors[] = 'Cannot find expenses header'; } else { $header = DataObjectFactory::Factory('Expense'); $header->load($data['expenses_header_id']); } if ($header) { $data['currency_id'] = $header->currency_id; $data['rate'] = $header->rate; $data['twin_currency_id'] = $header->twin_currency_id; $data['twin_rate'] = $header->twin_rate; if (empty($data['line_number']) && empty($data['id'])) { $data['line_number'] = $header->lines->count() + 1; } } if (isset($data['tax_rate_id'])) { $taxrate = DataObjectFactory::Factory('TaxRate'); $taxrate->load($data['tax_rate_id']); if ($taxrate) { $tax_percentage = $taxrate->percentage; } } if ($tax_percentage == 0 && $data['tax_value'] != 0) { $errors[] = 'Tax value should be zero for this tax rate'; } elseif ($tax_percentage != 0 && $data['tax_value'] == 0) { $errors[] = 'Tax value required for this tax rate'; } if ($data['rate'] == 1) { $data['base_net_value'] = $data['net_value']; $data['base_tax_value'] = $data['tax_value']; $data['base_gross_value'] = $data['gross_value']; } else { $data['base_net_value'] = round(bcdiv($data['net_value'], $data['rate'], 4), 2); $data['base_tax_value'] = round(bcdiv($data['tax_value'], $data['rate'], 4), 2); $data['base_gross_value'] = round(bcadd($data['base_tax_value'], $data['base_net_value']), 2); } //and to the twin-currency $data['twin_net_value'] = round(bcmul($data['base_net_value'], $data['twin_rate'], 4), 2); $data['twin_tax_value'] = round(bcmul($data['base_tax_value'], $data['twin_rate'], 4), 2); $data['twin_gross_value'] = round(bcadd($data['twin_tax_value'], $data['twin_net_value']), 2); $line = parent::Factory($data, $errors, $do); if (count($errors) > 0 || !$line) { return false; } return $line; }
function loadAccounts($data, &$errors = array(), &$warnings = array()) { $system = system::Instance(); $db = DB::Instance(); $flash = Flash::Instance(); if ($this->abort_action == 'A') { // Set encompassing transaction if need to abort all on error $db->StartTrans(); } $data_in = $this->convertData($data); if (is_array($data_in) && count($data_in) > 0) { foreach ($data_in as $line) { $model_data = array(); foreach ($line as $model => $fields) { switch ($model) { case 'Company': $model_data['Party']['type'] = $model; if (empty($fields['accountnumber'])) { $model = 'Lead'; unset($fields['accountnumber']); } $company_model = $model; $model_data[$model] = $fields; $model_data[$model]['is_lead'] = empty($fields['accountnumber']) ? true : false; if (!empty($model_data[$model]['name'])) { $account = new $company_model(); $account->loadBy('name', $model_data[$model]['name']); if ($account->isLoaded()) { $model_data[$model][$account->idField] = $account->{$account->idField}; $model_data['Party']['id'] = $account->party_id; } } break; case 'CompanyAddress': $model_data['Address'] = $fields; $model_data['PartyAddress']['main'] = true; // Check for existing entry $address = new CompanyAddress(); $address->loadBy(array('street1', 'street2', 'street3', 'town', 'county', 'postcode'), array($fields['street1'], $fields['street2'], $fields['street3'], $fields['town'], $fields['county'], $fields['postcode'])); if ($address->isLoaded()) { $model_data['PartyAddress']['address_id'] = $model_data['Address'][$address->idField] = $address->{$address->idField}; } break; case 'ContactMethod': foreach ($fields as $type => $value) { if (!empty($value)) { $model_data[$type]['ContactMethod']['contact'] = $value; $model_data[$type]['PartyContactMethod']['main'] = true; switch ($type) { case 'phone': $model_data[$type]['PartyContactMethod']['type'] = 'T'; break; case 'email': $model_data[$type]['PartyContactMethod']['type'] = 'E'; break; case 'fax': $model_data[$type]['PartyContactMethod']['type'] = 'F'; break; case 'mobile': $model_data[$type]['PartyContactMethod']['type'] = 'M'; break; default: $model_data[$type]['PartyContactMethod']['type'] = ''; } // Check for existing entry $contactmethod = new ContactMethod(); $contactmethod->loadBy('contact', $value); if ($contactmethod->isLoaded()) { $model_data[$type]['PartyContactMethod']['contactmethod_id'] = $model_data[$type]['ContactMethod'][$contactmethod->idField] = $contactmethod->{$contactmethod->idField}; } } } break; } } if (!$system->controller->save_model($company_model, $model_data, $errors, $warnings, $this->duplicates_action)) { if ($this->abort_action != 'S') { // Abort on Error if ($this->abort_action == 'A') { // Rollback all imported records $db->FailTrans(); $db->CompleteTrans(); } // If not abort_all_on_error, // then imported records up to this point will be saved return false; } // Skip error record and continue, so reset errors array $warnings[] = 'Ignoring Company ' . $model_data[$model]['name'] . ' ' . $db->ErrorMsg(); $warnings = array_merge($warnings, $errors); $errors = array(); $flash->clear(); } $company = $system->controller->getSavedModel($company_model); if (isset($accounts_data['CompanyInCategories'])) { foreach ($accounts_data['CompanyInCategories'] as $type => $value) { $companycategories['company_id'] = $company->id; if (!empty($value)) { // look up ContactCategory for this type $category = new ContactCategory(); $categories = $category->getCategoriesByName(ucfirst($type)); if (count($categories) == 1) { $companycategories['category_id'] = key($categories); $account_errors = array(); $companyincategory = DataObject::Factory($companycategories, $account_errors, 'CompanyInCategories'); if (count($account_errors) > 0 || !$companyincategory || !$companyincategory->save()) { if ($this->abort_action != 'S') { // Abort on Error if ($this->abort_action == 'A') { // Rollback all imported records $errors = array_merge_recursive($errors, $account_errors); $errors[] = 'Failed to save Company Category ' . $db->ErrorMsg(); $db->FailTrans(); $db->CompleteTrans(); } // If not abort_all_on_error, // then imported records up to this point will be saved return false; } // Skip error record and continue, so reset errors array $warnings[] = 'Ignoring Company Category for ' . $company->name . ' ' . $db->ErrorMsg(); $warnings = array_merge($warnings, $account_errors); $flash->clear(); } } } } } $system->controller->clearSavedModels(); } } if ($this->abort_action == 'A') { // Everything is OK here, just need to complete transaction // if mode is set to abort all on error $db->CompleteTrans(); } return array('internal_id' => null, 'internal_identifier_field' => '', 'internal_identifier_value' => ''); }
public function save_defaults() { if (!$this->checkParams('ModuleComponent')) { sendBack(); } if (!$this->loadData()) { sendBack(); } $modulecomponent = $this->_uses[$this->modeltype]; $idField = $modulecomponent->idField; $idValue = $modulecomponent->{$modulecomponent->idField}; $errors = array(); $flash = Flash::Instance(); $db = DB::Instance(); $db->Debug(); $db->StartTrans(); switch ($modulecomponent->type) { case 'M': if (strpos($modulecomponent->name, 'collection')) { $do = new $modulecomponent->name(); } else { $do = DataObjectFactory::Factory($modulecomponent->name); } $enabled = $do->getDisplayFields(); break; case 'T': $controllername = $modulecomponent->controller . 'Controller'; $do = new $controllername('', $this->view); $enabled = $do->getInputFields(); break; default: $do = null; } if (is_object($do)) { $do_name = get_class($do); } else { $do_name = 'none'; } if (isset($this->_data[$do_name])) { $defaults = array(); foreach ($this->_data[$do_name] as $key => $field) { foreach ($field as $name => $value) { if (substr($name, 0, 17) == '_checkbox_exists_') { $checkbox_name = substr($name, 17); if (isset($this->_data[$do_name][$key][$checkbox_name])) { continue; } else { $defaults[$checkbox_name][$key] = 'false'; } } else { $defaults[$name][$key] = $value; } } } foreach ($defaults as $field => $default) { $current = $do->getField($field); if (!is_object($current)) { // Non Database Field continue; } if ((!isset($enabled[$field]) && !$default['enabled'] || isset($enabled[$field]) && $enabled[$field] == $default['enabled']) && ($current->has_default && $current->default_value == $default['default_value'])) { // don't update if value not changed! continue; } $moduledefault = DataObjectFactory::Factory('ModuleDefault'); $moduledefault->loadBy(array('module_components_id', 'field_name'), array($idValue, $field)); $data = array(); if ($moduledefault->isLoaded()) { $data['id'] = $moduledefault->id; } $data['field_name'] = $field; if (!isset($default['default_value'])) { $default['default_value'] = ''; } $data['default_value'] = $default['default_value']; $data['enabled'] = $default['enabled']; $data['module_components_id'] = $idValue; $moduledefault = DataObject::Factory($data, $errors, 'ModuleDefault'); if (!$moduledefault || !$moduledefault->save(true)) { $errors[] = 'Failed to update default value for ' . $field; break; } } } if (count($errors) > 0) { $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->_data['id'] = $idValue; $this->refresh(); } else { $flash->addMessage('defaults updated'); $db->CompleteTrans(); sendTo($this->name, 'view', $this->_modules, array($idField => $idValue)); } }
function save_data() { if (!isset($this->_data['dataset_id'])) { $this->dataError(); sendBack(); } $flash = Flash::Instance(); $errors = array(); $dataset = $this->_uses[$this->modeltype]; $dataset->load($this->_data['dataset_id']); $model = $this->newModel($dataset); $save_model = DataObject::Factory($this->_data['DataObject'], $errors, $model); if (!$save_model || count($errors) > 0 || !$save_model->save()) { $flash->addErrors($errors); $db = DB::Instance(); $flash->addError('Error saving data to ' . $model->getTitle() . ' : ' . $db->ErrorMsg()); } else { $flash->addMessage($model->getTitle() . ' saved OK'); } if (isset($this->_data['saveAnother'])) { sendTo($this->name, 'edit_data', $this->_modules, array('dataset_id' => $dataset->{$dataset->idField})); } sendTo($this->name, 'view_collection', $this->_modules, array($dataset->idField => $dataset->{$dataset->idField})); }
function add($data, $type, &$errors) { $data['transaction_type'] = $type; return DataObject::Factory($data, $errors, 'ARTransaction'); }
private function setHasPermissions($permission_id, $data, &$errors = array()) { $db = DB::Instance(); $db->StartTrans(); $data['permissionsid'] = $permission_id; $hp = DataObject::Factory($data, $errors, 'HasPermission'); if (count($errors) > 0 || !$hp->save()) { $errors[] = 'Failed to assign permissions to roles'; $db->FailTrans(); } else { $permission = DataObjectFactory::Factory('Permission'); $permission->load($permission_id); if ($permission && $permission->sub_permissions->count() > 0) { foreach ($permission->sub_permissions as $sub_permission) { $this->setHasPermissions($sub_permission->id, $data, $errors); if (count($errors) > 0) { $errors[] = 'Failed to assign permissions to roles'; $db->FailTrans(); break; } } } } return $db->CompleteTrans(); }