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() { if (!$this->checkParams($this->modeltype)) { sendBack(); } $flash = Flash::Instance(); $errors = array(); $data = $this->_data; $header = $data[$this->modeltype]; if (isset($header['id']) && $header['id'] != '') { $action = 'updated'; } else { $action = 'added'; } $trans_type = $this->_uses[$this->modeltype]->getEnum('transaction_type', $header['transaction_type']); $invoice = PInvoice::Factory($header, $errors); $result = false; if (count($errors) == 0 && $invoice) { $result = $invoice->save(); if ($result && $data['saveform'] == 'Save and Post') { // reload the invoice to refresh the dependencies $invoice->load($invoice->id); if (!$invoice->post($errors)) { $result = false; } } } if ($result !== FALSE) { $flash->addMessage($trans_type . ' ' . $action . ' successfully'); sendTo($this->name, 'view', $this->_modules, array('id' => $invoice->id)); } $errors[] = 'Error saving ' . $trans_type; $flash->addErrors($errors); if (isset($header['id']) && $header['id'] != '') { $this->_data['id'] = $header['id']; } if (isset($header['plmaster_id']) && $header['plmaster_id'] != '') { $this->_data['plmaster_id'] = $header['plmaster_id']; } $this->refresh(); }