Exemple #1
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()
 {
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data['PInvoiceLine'];
     if (empty($data['invoice_id'])) {
         $errors[] = 'Invoice header not defined';
     } else {
         $pinvoice = DataObjectFactory::Factory('Pinvoice');
         $pinvoice->load($data['invoice_id']);
         if (!$pinvoice->isLoaded()) {
             $errors[] = 'Cannot find invoice header';
         } elseif ($pinvoice->isLatest($this->_data['PInvoice'], $errors)) {
             $pinvoiceline = PInvoiceLine::Factory($pinvoice, $data, $errors);
             if ($pinvoiceline && count($errors) == 0) {
                 if (!$pinvoiceline->save($pinvoice)) {
                     $errors[] = 'Failed to save Purchase ' . $pinvoice->getFormatted('transaction_type') . ' line';
                 }
             }
         }
     }
     if (count($errors) == 0) {
         $flash->addMessage('Purchase ' . $pinvoice->getFormatted('transaction_type') . ' Line Saved');
         if (isset($this->_data['saveAnother'])) {
             $other = array('invoice_id' => $pinvoiceline->invoice_id);
             if (isset($this->_data['dialog'])) {
                 $other += array('dialog' => '');
             }
             if (isset($this->_data['ajax'])) {
                 $other += array('ajax' => '');
             }
             sendTo($this->name, 'new', $this->_modules, $other);
         } else {
             $action = 'view';
             $controller = 'pinvoices';
             $other = array('id' => $pinvoiceline->invoice_id);
         }
         if (isset($this->_data['dialog'])) {
             $link = array('modules' => $this->_modules, 'controller' => $controller, 'action' => $action, 'other' => $other);
             $flash->save();
             echo parent::returnJSONResponse(TRUE, array('redirect' => '/?' . setParamsString($link)));
             exit;
         } else {
             sendTo($controller, $action, $this->_modules, $other);
         }
     } else {
         $flash->addErrors($errors);
         $this->_data['id'] = $this->_data['PInvoiceLine']['id'];
         $this->_data['invoice_id'] = $this->_data['PInvoiceLine']['invoice_id'];
         $this->refresh();
     }
 }