Beispiel #1
0
 public function toConstraintChain()
 {
     $cc = new ConstraintChain();
     if ($this->cleared) {
         return $cc;
     }
     debug('BaseSearch::toConstraintChain Fields: ' . print_r($this->fields, true));
     // Certain hidden fields need to be excluded from the constraint
     foreach ($this->fields as $group => $group_data) {
         foreach ($group_data as $field => $searchField) {
             if ($field == 'purchase_order_number') {
                 $search_value = $searchField->getValue();
                 if (!empty($search_value)) {
                     $invoices = PInvoice::getInvoices($searchField->getValue());
                     if (!empty($invoices)) {
                         $cc->add(new Constraint('id', 'in', '(' . implode(',', array_keys($invoices)) . ')'));
                     }
                 }
             } elseif ($searchField->doConstraint()) {
                 $c = $searchField->toConstraint();
                 if ($c !== false) {
                     $cc->add($c);
                 }
             }
         }
     }
     debug('BaseSearch::toConstraintChain Constraints: ' . print_r($cc, true));
     return $cc;
 }
Beispiel #2
0
 public function save_model($data)
 {
     // Used to save Invoice Header and Invoice Lines from import or copy of existing
     $flash = Flash::Instance();
     if (empty($data['PInvoice']) || empty($data['PInvoiceLine'])) {
         $flash->addError('Error trying to save invoice');
         return false;
     }
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $header = $data['PInvoice'];
     $lines_data = DataObjectCollection::joinArray($data['PInvoiceLine'], 0);
     if (!$lines_data || empty($lines_data)) {
         $lines_data[] = $data['PInvoiceLine'];
     }
     $invoice = PInvoice::Factory($header, $errors);
     if (!$invoice || count($errors) > 0) {
         $errors[] = 'Invoice validation failed';
     } elseif (!$invoice->save()) {
         $errors[] = 'Invoice creation failed';
     }
     if ($invoice) {
         foreach ($lines_data as $line) {
             $line['invoice_id'] = $invoice->{$invoice->idField};
             $invoiceline = PInvoiceLine::Factory($invoice, $line, $errors);
             if (!$invoiceline || count($errors) > 0) {
                 $errors[] = 'Invoice Line validation failed for line ' . $line['line_number'];
             } elseif (!$invoiceline->save()) {
                 $errors[] = 'Invoice Line creation failed for line ' . $line['line_number'];
             }
         }
     }
     if (count($errors) === 0) {
         if (!$invoice->save()) {
             $errors[] = 'Error updating Invoice totals';
         } else {
             $result = array('internal_id' => $invoice->{$invoice->idField}, 'internal_identifier_field' => $invoice->identifierField, 'internal_identifier_value' => $invoice->getidentifierValue());
         }
     }
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $db->FailTrans();
         $result = false;
     }
     $db->CompleteTrans();
     return $result;
 }
Beispiel #3
0
 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();
 }
Beispiel #4
0
 public static function Factory(PInvoice $header, $line_data, &$errors)
 {
     if (empty($line_data['invoice_id'])) {
         $line_data['invoice_id'] = $header->id;
     }
     if (empty($line_data['line_number'])) {
         $line_data['line_number'] = $header->getNextLineNumber();
     }
     if ($line_data['net_value'] > 0) {
         // do nothing
     } else {
         $errors[] = 'Zero net value';
         return FALSE;
     }
     $line_data['invoice_id'] = $header->id;
     if (empty($line_data['description'])) {
         $line_data['description'] = $line_data['item_description'];
     }
     $line_data['tax_status_id'] = $header->tax_status_id;
     $line_data['currency_id'] = $header->currency_id;
     $line_data['rate'] = $header->rate;
     $line_data['twin_currency_id'] = $header->twin_currency_id;
     $line_data['twin_rate'] = $header->twin_rate;
     $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors);
     return parent::makeLine($line_data, 'PInvoiceLine', $errors);
 }