public function getOrderList($_item_type = '', $_account = '')
 {
     if (isset($this->_data['ajax'])) {
         if (!empty($this->_data['item_type'])) {
             $_item_type = $this->_data['item_type'];
         }
         if (!empty($this->_data['account'])) {
             $_account = $this->_data['account'];
         }
     }
     switch ($_item_type) {
         case 'PO':
             $order = new POrder();
             $field = 'plmaster_id';
             break;
         case 'SI':
             $order = new SInvoice();
             $field = 'slmaster_id';
             break;
     }
     $cc = new ConstraintChain();
     $cc->add(new Constraint($field, '=', $_account));
     $db = DB::Instance();
     $subquery = 'select item_id from project_costs_charges where item_type=' . $db->qstr($_item_type);
     $cc->add(new Constraint('id', 'not in', '(' . $subquery . ')'));
     $orders = $order->getAll($cc);
     if (isset($this->_data['ajax'])) {
         $output['item_id'] = array('data' => $orders, 'is_array' => true);
         $this->view->set('data', $output);
         $this->setTemplateName('ajax_multiple');
     } else {
         return $orders;
     }
 }
 public function _new()
 {
     $flash = Flash::Instance();
     parent::_new();
     // Get the invoice Line Object - if loaded, this is an edit
     $sinvoiceline = $this->_uses[$this->modeltype];
     if (!$sinvoiceline->isLoaded()) {
         if (empty($this->_data['invoice_id'])) {
             $flash->addError('No Sales invoice supplied');
             sendBack();
         }
         $sinvoiceline->invoice_id = $this->_data['invoice_id'];
     }
     $sinvoice = new SInvoice();
     $sinvoice->load($sinvoiceline->invoice_id);
     $_slmaster_id = $sinvoice->slmaster_id;
     if (isset($this->_data[$this->modeltype])) {
         // We've had an error so refresh the page
         $_slmaster_id = $this->_data['SInvoice']['slmaster_id'];
         $sinvoiceline->line_number = $this->_data['SInvoiceLine']['line_number'];
         $_product_search = $this->_data['SInvoiceLine']['product_search'];
         if (!empty($this->_data['SInvoiceLine']['productline_id'])) {
             $_productline_id = $this->_data['SInvoiceLine']['productline_id'];
         } else {
             $_productline_id = '';
         }
         $_glaccount_id = $this->_data['SInvoiceLine']['glaccount_id'];
     } elseif ($sinvoiceline->isLoaded()) {
         $_product_search = $sinvoiceline->description;
         $_productline_id = $sinvoiceline->productline_id;
         $_glaccount_id = $sinvoiceline->glaccount_id;
     } else {
         $sinvoiceline->due_despatch_date = $sinvoice->despatch_date;
         $sinvoiceline->due_delivery_date = $sinvoice->due_date;
     }
     $display_fields = $sinvoiceline->getDisplayFields();
     if (isset($display_fields['product_search'])) {
         if (empty($_product_search)) {
             $_product_search = 'None';
             $productline_options = array('' => 'None');
         } else {
             $productline_options = $this->getProductLines($_slmaster_id, $_product_search);
         }
     } else {
         $productline_options = $this->getProductLines($_slmaster_id);
     }
     if (empty($_productline_id)) {
         $_productline_id = key($productline_options);
     }
     $this->view->set('display_fields', $display_fields);
     $this->view->set('product_search', $_product_search);
     $this->view->set('productline_options', $productline_options);
     $data = $this->getProductLineData($_productline_id, $_slmaster_id);
     $this->view->set('stuom_options', $data['stuom_id']);
     $this->view->set('glaccount_options', $data['glaccount_id']);
     if (empty($_glaccount_id)) {
         $_glaccount_id = key($data['glaccount_id']);
     }
     $this->view->set('glcentre_options', $this->getCentre($_glaccount_id, $_productline_id));
     $this->view->set('taxrate_options', $data['tax_rate_id']);
     $this->view->set('sinvoice', $sinvoice);
 }
Beispiel #3
0
 public static function Factory(SInvoice $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['sales_qty'] > 0 && $line_data['sales_price'] > 0) {
         // do nothing
     } else {
         $errors[] = 'Zero quantity or net value';
         return false;
     }
     $line_data['invoice_id'] = $header->id;
     if (!isset($line_data['move_stock'])) {
         $line_data['move_stock'] = 'f';
     }
     if ($line_data['productline_id'] == -1) {
         $line_data['productline_id'] = '';
         $line_data['stitem_id'] = '';
     } elseif ($line_data['productline_id'] > 0) {
         $productline = DataObjectFactory::Factory('SOProductline');
         $productline->load($line_data['productline_id']);
         if ($productline && $productline->isLoaded()) {
             $productlineheader = $productline->product_detail;
             $line_data['stitem_id'] = $productlineheader->stitem_id;
             $line_data['item_description'] = $productlineheader->stitem;
         } else {
             $line_data['stitem_id'] = '';
         }
         if (!empty($line_data['stitem_id'])) {
             $stitem = DataObjectFactory::Factory('STitem');
             $stitem->load($line_data['stitem_id']);
             if ($stitem) {
                 $line_data['item_description'] = $stitem->getIdentifierValue();
                 $line_data['stuom_id'] = $stitem->uom_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['settlement_discount'] = $header->payment_term->calcSettlementDiscount($line_data['net_value']);
     $line_data['glaccount_centre_id'] = GLAccountCentre::getAccountCentreId($line_data['glaccount_id'], $line_data['glcentre_id'], $errors);
     return parent::makeLine($line_data, 'SInvoiceLine', $errors);
 }
Beispiel #4
0
 public function getInvoiceExportList($_definition_id = '')
 {
     $invoice = new SInvoice();
     $collection = new SInvoiceCollection($invoice);
     $cc = new ConstraintChain();
     $cc->add(new Constraint('transaction_type', '=', 'I'));
     $cc->add(new Constraint('status', '=', 'O'));
     $cc->add(new Constraint('despatch_date', 'is not', 'NULL'));
     $cc->add(new Constraint('print_count', '=', '0'));
     $cc->add(new Constraint('invoice_method', '=', 'D'));
     $cc->add(new Constraint('edi_invoice_definition_id', '=', $_definition_id));
     $translog = new EDITransactionLog();
     $cc1 = new ConstraintChain();
     $cc1->add(new Constraint('status', '=', 'C'));
     $cc1->add(new Constraint('action', '=', 'S'));
     $cc1->add(new Constraint('data_definition_id', '=', $_definition_id));
     $cc1->add(new Constraint('internal_id', '=', '(' . $collection->getViewName() . '.id)'));
     $cc->add(new Constraint('not', 'exists', '(select id from ' . $translog->getTableName() . ' where ' . $cc1->__toString() . ')'));
     $invoice->orderby = 'invoice_number';
     //		echo 'cc='.$cc->__toString().'<br>';
     return $invoice->getAll($cc, false, true);
 }
Beispiel #5
0
 public function getSalesTotals()
 {
     $expenses = new SInvoice();
     $cc = new ConstraintChain();
     $cc->add(new Constraint('project_id', '=', $this->id));
     $totals = $expenses->getSumFields(array('net_value'), $cc);
     return array('total_invoiced' => $totals['net_value']);
 }
Beispiel #6
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['SInvoice']) || empty($data['SInvoiceLine'])) {
         $flash->addError('Error trying to save invoice');
         return false;
     }
     $errors = array();
     $db = DB::Instance();
     $db->StartTrans();
     $header = $data['SInvoice'];
     $lines_data = DataObjectCollection::joinArray($data['SInvoiceLine'], 0);
     if (!$lines_data || empty($lines_data)) {
         $lines_data[] = $data['SInvoiceLine'];
     }
     $invoice = SInvoice::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 = SInvoiceLine::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 #7
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 = SInvoice::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['slmaster_id']) && $header['slmaster_id'] != '') {
         $this->_data['slmaster_id'] = $header['slmaster_id'];
     }
     $this->refresh();
 }
Beispiel #8
0
 private function createPostInvoice($sorder, &$errors)
 {
     if ($sorder->customerdetails->accountStopped()) {
         $errors[] = 'Cannot create invoice, customer account stopped';
         return FALSE;
     }
     set_time_limit(0);
     $linestatuses = $sorder->getLineStatuses();
     $linestatus = $linestatuses['count'];
     if (!$sorder->someLinesDespatched($linestatus) && !$sorder->someLinesPicked($linestatus)) {
         $errors[] = 'No lines picked or despatched';
         return FALSE;
     }
     $sinvoicelines = array();
     $db = DB::Instance();
     $db->startTrans();
     $sodespatchlines = array();
     $create_sodespatchlines = array();
     $latest_despatch_date = 0;
     $sorderline = DataObjectFactory::Factory('SOrderLine');
     $sorderlines = new SOrderLineCollection($sorderline);
     $sh = new SearchHandler($sorderlines, FALSE);
     $sh->addConstraint(new Constraint('order_id', '=', $sorder->id));
     $statuses = array($db->qstr($sorderline->despatchStatus()), $db->qstr($sorderline->pickedStatus()));
     $sh->addConstraint(new Constraint('status', 'in', '(' . implode(',', $statuses) . ')'));
     $sorderlines->load($sh);
     $sodespatchline = DataObjectFactory::Factory('SODespatchLine');
     foreach ($sorderlines as $sorderline) {
         $sinvoiceline = array();
         $sodespatchlines[$sorderline->id]['id'] = $sorderline->id;
         $sodespatchlines[$sorderline->id]['line_number'] = $sorderline->line_number;
         if ($sorderline->status == $sorderline->pickedStatus()) {
             $sinvoiceline['move_stock'] = 't';
             $sorderline->del_qty = $sorderline->revised_qty;
             $sorderline->actual_despatch_date = fix_date(date(DATE_FORMAT));
             if (!is_null($sorderline->stitem_id)) {
                 $create_sodespatchlines[$sorder->id][$sorderline->id] = SODespatchLine::makeline($sorder, $sorderline, $errors);
                 $create_sodespatchlines[$sorder->id][$sorderline->id]['status'] = $sodespatchline->despatchStatus();
             }
             $sodespatchlines[$sorderline->id]['fields'] = array('status', 'del_qty', 'actual_despatch_date');
             $sodespatchlines[$sorderline->id]['values'] = array($sorderline->invoicedStatus(), $sorderline->del_qty, $sorderline->actual_despatch_date);
         } else {
             $sodespatchlines[$sorderline->id]['fields'] = array('status');
             $sodespatchlines[$sorderline->id]['values'] = array($sorderline->invoicedStatus());
         }
         foreach ($sorderline->getFields() as $key => $value) {
             $sinvoiceline[$key] = $sorderline->{$key};
         }
         foreach ($sorderline->audit_fields as $field) {
             unset($sinvoiceline[$field]);
         }
         if ($sorderline->actual_despatch_date > $latest_despatch_date) {
             $latest_despatch_date = $sorderline->actual_despatch_date;
         }
         $sinvoiceline['sales_order_id'] = $sorderline->order_id;
         $sinvoiceline['order_line_id'] = $sorderline->id;
         $sinvoiceline['sales_qty'] = $sorderline->del_qty;
         $sinvoiceline['sales_price'] = $sorderline->price;
         $sinvoiceline['net_value'] = bcmul($sorderline->price, $sorderline->del_qty);
         $sinvoiceline['glaccount_centre_id'] = null;
         $sinvoiceline['id'] = '';
         $sinvoicelines[] = $sinvoiceline;
     }
     if (count($errors) == 0 && count($create_sodespatchlines) > 0 && !SODespatchLine::createDespatchNote($create_sodespatchlines, $errors)) {
         $errors[] = 'Error creating Despatch Note';
     }
     foreach ($sodespatchlines as $id => $data) {
         $data['fields'][] = 'glaccount_centre_id';
         $data['values'][] = 'null';
         $result = $this->_uses['SOrderLine']->update($id, $data['fields'], $data['values']);
         if (!$result) {
             $errors['id' . $id] = 'Failed to update order line ' . $data['line_number'] . ' : ' . $db->errorMsg();
         }
     }
     if (count($errors) == 0) {
         // Check line statuses and update header status accordingly
         if (!$sorder->save()) {
             $errors[] = 'Failed to update order header ' . $db->ErrorMsg();
         }
     }
     if (count($sinvoicelines) == 0) {
         $errors[] = 'Failed to create invoice lines';
     }
     // Save the Invoice Header if no errors
     if (count($errors) == 0) {
         $sinvoiceheader = array();
         foreach ($sorder->getFields() as $key => $value) {
             $sinvoiceheader[$key] = $sorder->{$key};
         }
         foreach ($sorder->audit_fields as $field) {
             unset($sinvoiceheader[$field]);
         }
         $sinvoiceheader['id'] = '';
         $sinvoiceheader['sales_order_id'] = $sorder->id;
         $sinvoiceheader['sales_order_number'] = $sorder->order_number;
         $sinvoiceheader['despatch_date'] = un_fix_date($latest_despatch_date);
         $sinvoiceheader['transaction_type'] = 'I';
         $sinvoiceheader['invoice_date'] = un_fix_date($latest_despatch_date);
         $sinvoice = SInvoice::Factory($sinvoiceheader, $errors);
         if ($sinvoice) {
             $result = $sinvoice->save();
             if (!$result) {
                 $errors[] = 'Failed to create invoice';
             }
         }
     }
     // Save the Invoice Lines if no errors
     if (count($errors) == 0) {
         $result = FALSE;
         foreach ($sinvoicelines as $line_data) {
             $sinvoiceline = SInvoiceLine::Factory($sinvoice, $line_data, $errors);
             if ($sinvoiceline) {
                 $result = $sinvoiceline->save();
             }
             if (!$result) {
                 $errors[] = 'Failed to create invoice line ' . $db->ErrorMsg();
                 break;
             }
         }
         // Now save the header again to update the totals
         $result = $sinvoice->save();
         if (!$result) {
             $errors[] = 'Failed to update invoice totals';
             $errors[] = $db->ErrorMsg();
         }
     }
     // Post the Invoice if no errors
     if (count($errors) == 0) {
         $result = $sinvoice->post($errors);
         if (!$result) {
             $errors[] = 'Failed to post invoice';
         }
         foreach ($sodespatchlines as $id => $data) {
             $sodespatchlines = new SODespatchLineCollection($sodespatchline);
             $sh = new SearchHandler($sodespatchlines, FALSE);
             $sh->addConstraint(new Constraint('orderline_id', '=', $id));
             $sh->addConstraint(new Constraint('status', '=', $sodespatchline->despatchStatus()));
             $sh->addConstraint(new Constraint('invoice_id', 'is', 'NULL'));
             if ($sodespatchlines->update(array('invoice_id', 'invoice_number'), array($sinvoice->id, $sinvoice->invoice_number), $sh) === false) {
                 $errors[] = 'Error updating despatch lines : ' . $db->ErrorMsg();
             }
         }
     }
     if (count($errors) > 0) {
         $db->FailTrans();
         $result = FALSE;
     } else {
         // Everything is OK so return the newly created Invoice object
         $result = $sinvoice;
     }
     $db->CompleteTrans();
     return $result;
 }