public function update_pay_reference() { $db = DB::Instance(); $db->StartTrans(); $flash = Flash::Instance(); $errors = array(); if ($this->checkParams('PLPayment')) { $plpayment = $this->_uses[$this->modeltype]; $plpayment->load($this->_data['PLPayment']['id']); if (!$plpayment) { $errors[] = 'Error trying to get Payment Details'; } else { if (isset($this->_data['start_reference']) && isset($this->_data['end_reference']) && $this->_data['end_reference'] != $this->_data['start_reference'] + $plpayment->number_transactions - 1) { $errors[] = 'Reference range does not match number of transactions'; } else { $pltransactions = new PLTransactionCollection(DataObjectFactory::Factory('PLTransaction')); $pltransactions->paidList($plpayment->id); $ext_ref = $this->_data['start_reference']; $progressbar = new progressBar('update_payment_reference'); $callback = function ($pl_data, $key) use(&$ext_ref, &$errors, $db) { $cbtrans = DataObjectFactory::Factory('CBTransaction'); $cbtrans->loadBy(array('source', 'reference', 'ext_reference', 'gross_value'), array('P', $pl_data->our_reference, $pl_data->cross_ref, $pl_data->gross_value)); if (!$cbtrans || !$cbtrans->update($cbtrans->id, 'ext_reference', $ext_ref)) { $errors[] = 'Error updating CB Transaction External Reference : ' . $db->ErrorMsg(); return FALSE; } if (!$pl_data->update($pl_data->id, 'ext_reference', $ext_ref)) { $errors[] = 'Error updating PL Transaction External Reference : ' . $db->ErrorMsg(); return FALSE; } else { $ext_ref += 1; } }; if ($progressbar->process($pltransactions, $callback) === FALSE) { $errors[] = 'Error updating payment reference'; } } } } if (count($errors) > 0) { $flash->addErrors($errors); $this->_templateName = $this->getTemplateName('enter_payment_reference'); $this->view->set('PLPayment', $plpayment); $db->FailTrans(); $db->CompleteTrans(); } else { $db->CompleteTrans(); sendTo('pltransactions', 'select_remittances', $this->_modules, array('id' => $plpayment->id)); } }
public function save($pay_data, &$errors) { $db = DB::Instance(); $db->StartTrans(); // Save the Payment Header if (!parent::save()) { $errors[] = 'Failed to save Payment Header'; $db->FailTrans(); return false; } $flash = Flash::Instance(); // Validate and write purchase ledger, cashbook and general ledger transactions $progressbar = new progressBar('creating_pl_transactions'); $payment_id = $this->id; $callback = function ($data, $key) use(&$pay_data, &$errors, $payment_id) { $data['cb_account_id'] = $pay_data['cb_account_id']; $data['reference'] = $pay_data['reference']; $data['cross_ref'] = $data['ext_reference'] = $payment_id; $data['description'] = $pay_data['description']; $data['transaction_type'] = 'P'; $data['transaction_date'] = $pay_data['transaction_date']; $data['source'] = $pay_data['source']; $supplier = DataObjectFactory::Factory('PLSupplier'); $supplier->load($data['plmaster_id']); $data['payment_term_id'] = $supplier->payment_term_id; if (PLTransaction::saveTransaction($data, $errors) === false) { $errors[] = 'Failed to save payments'; return false; } $pay_data['PLTransaction'][$key]['ledger_transaction_id'] = $data['ledger_transaction_id']; }; if ($progressbar->process($pay_data['PLTransaction'], $callback) === FALSE) { $db->FailTrans(); $db->CompleteTrans(); return FALSE; } // Match and update purchase ledger payments $payment_total = 0; $progressbar = new progressBar('allocate_payments'); $callback = function ($data, $key) use(&$pay_data, &$errors, $payment_id, &$payment_total) { $db = DB::Instance(); // Get the payment transaction and update it to paid $pltransaction = DataObjectFactory::Factory('PLTransaction'); $pltransaction->load($data['ledger_transaction_id']); if (!$pltransaction->update($pltransaction->id, array('status', 'os_value', 'twin_os_value', 'base_os_value'), array('P', '0.00', '0.00', '0.00'))) { $errors[] = 'Error updating payment status : ' . $db->ErrorMsg(); return false; } // get all the transactions linked for payment to the payment transaction $pltransactions = new PLTransactionCollection(DataObjectFactory::Factory('PLTransaction'), 'pl_allocation_overview'); $pltransactions->getPaid($data); // the allocation amount is the gross payment value $allocations = array($pltransaction->id => $pltransaction->gross_value); $trans_total = 0; $trans_base_total = $pltransaction->base_gross_value; foreach ($pltransactions as $trans) { // now mark all the linked transactions as paid // update the invoices linked to thes transactions as paid $trans_total = bcadd($trans->gross_value, $trans_total); $trans_base_total = bcadd($trans->base_gross_value, $trans_base_total); if (!$trans->update($trans->id, array('status', 'for_payment', 'os_value', 'twin_os_value', 'base_os_value'), array($trans->Paid(), false, '0.00', '0.00', '0.00'))) { $errors[] = 'Error updating transaction status : ' . $db->ErrorMsg(); return false; } if ($trans->transaction_type == 'C' || $trans->transaction_type == 'I') { $invoice = DataObjectFactory::Factory('PInvoice'); if (!$invoice->updateStatus($trans->our_reference, 'P')) { $errors[] = 'Error updating Invoice : ' . $db->ErrorMsg(); return false; } } $allocations[$trans->id] = $trans->gross_value; // Save settlement discount if present? if ($trans->settlement_discount > 0 && $trans->include_discount == 't') { // Create GL Journal for settlement discount $discount = array(); $discount['gross_value'] = $discount['net_value'] = $trans->settlement_discount; $discount['glaccount_id'] = $trans->pl_discount_glaccount_id; $discount['glcentre_id'] = $trans->pl_discount_glcentre_id; $discount['transaction_date'] = date(DATE_FORMAT); $discount['tax_value'] = '0.00'; $discount['source'] = 'P'; $discount['transaction_type'] = 'SD'; $discount['our_reference'] = $trans->our_reference; $discount['ext_reference'] = $trans->ext_reference; $discount['currency_id'] = $trans->currency_id; $discount['rate'] = $trans->rate; $discount['description'] = !is_null($trans->pl_discount_description) ? $trans->pl_discount_description . ' ' : ''; $discount['description'] .= !is_null($trans->description) ? $trans->description : $trans->ext_reference; $discount['payment_term_id'] = $trans->payment_term_id; $discount['plmaster_id'] = $trans->plmaster_id; $discount['status'] = 'P'; $pldiscount = PLTransaction::Factory($discount, $errors, 'PLTransaction'); if ($pldiscount && $pldiscount->save('', $errors) && $pldiscount->saveGLTransaction($discount, $errors)) { $allocations[$pldiscount->{$pldiscount->idField}] = bcadd($discount['net_value'], 0); $trans_total = bcadd($trans_total, $pldiscount->gross_value); $trans_base_total = bcadd($trans_base_total, $pldiscount->base_gross_value); } else { $errors[] = 'Errror saving PL Transaction Discount : ' . $db->ErrorMsg(); } } } if ($data['net_value'] != $pltransaction->gross_value * -1 || $data['net_value'] != $trans_total) { $errors[] = 'Transaction Payment mismatch ' . $data['net_value'] . ' ' . $pltransaction->gross_value * -1 . ' ' . $trans_total . ' for ' . $trans->supplier; return false; } // save the allocations if (!PLAllocation::saveAllocation($allocations, $payment_id, $errors)) { return false; } if ($trans_base_total != 0) { $adj_data = array(); $errors = array(); $adj_data['docref'] = $pltransaction->plmaster_id; $adj_data['original_source'] = 'P'; $adj_data['reference'] = ''; $adj_data['value'] = $trans_base_total * -1; $adj_data['comment'] = 'Purchase Allocation Currency Adjustment'; if (!GLTransaction::currencyAdjustment($adj_data, $errors)) { return false; } } $payment_total = bcadd($payment_total, $trans_total); }; if ($progressbar->process($pay_data['PLTransaction'], $callback) === FALSE) { $db->FailTrans(); $db->CompleteTrans(); return FALSE; } if ($payment_total != $pay_data['payment_total']) { $errors[] = 'Payment Mismatch - Total ' . $pay_data['payment_total'] . ' not equal sum Transaction Payments ' . $payment_total; $db->FailTrans(); $db->CompleteTrans(); return false; } return $db->CompleteTrans(); }
public function allocate() { if (!$this->loadData()) { $this->dataError(); sendBack(); } $supplier = $this->_uses[$this->modeltype]; $transaction = DataObjectFactory::Factory('PLTransaction'); $transactions = new PLTransactionCollection($transaction, 'pl_allocation_overview'); $sh = new SearchHandler($transactions, false); $db = DB::Instance(); $sh->addConstraint(new Constraint('status', 'in', '(' . $db->qstr($transaction->open()) . ',' . $db->qstr($transaction->partPaid()) . ')')); $sh->addConstraint(new Constraint('plmaster_id', '=', $supplier->id)); $sh->setOrderby(array('supplier', 'our_reference')); $transactions->load($sh); $this->view->set('allocated_total', 0); $this->view->set('transactions', $transactions); $this->view->set('no_ordering', true); }
function populate() { $trans = new PLTransactionCollection(new PLTransaction()); $pl = new PageList('aged_creditors_summary'); $this->contents = $trans->agedSummary(); }
public function select_remittances() { $flash = Flash::Instance(); $errors = array(); if (!$this->checkParams('id')) { sendBack(); } else { $plpayment = DataObjectFactory::Factory('PLPayment'); $plpayment->load($this->_data['id']); if (!$plpayment) { $errors[] = 'Error trying to get Payment Details'; } else { $pltransactions = new PLTransactionCollection(); $pltransactions->paidList($plpayment->id); foreach ($pltransactions as $pltransaction) { $pltransaction->gross_value = bcmul($pltransaction->gross_value, -1); $pltransaction->setAdditional('email'); $pltransaction->email = $pltransaction->email_remittance; } } } $type = 'remittance'; $output_details = $this->output_types[$type]; $selected = empty($_SESSION['selected_output'][$type]) ? array() : $_SESSION['selected_output'][$type]; $new_selection = array('count' => 0); foreach ($pltransactions as $detail) { if (isset($selected[$detail->id])) { $new_selection[$detail->id] = $selected[$detail->id]; if ($selected[$detail->id]['select'] == 'true') { $new_selection['count']++; } } else { if (isset($output_details['identifier'])) { $new_selection[$detail->id]['description'] = prettify($output_details['identifier']) . ' : ' . $detail->{$output_details}['identifier']; } else { $new_selection[$detail->id]['description'] = $detail->getIdentifierValue(); } if (empty($new_selection[$detail->id]['description'])) { $new_selection[$detail->id]['description'] = 'no identifier set'; } if (!is_null($detail->method)) { switch ($detail->method) { case 'E': $new_selection[$detail->id]['printaction'] = 'email'; break; case 'D': $new_selection[$detail->id]['printaction'] = 'edi'; break; case 'F': case 'P': $new_selection[$detail->id]['printaction'] = 'print'; break; default: $new_selection[$detail->id]['printaction'] = strtolower($detail->method); } } elseif ($detail->email == '') { $new_selection[$detail->id]['printaction'] = 'print'; } else { $new_selection[$detail->id]['printaction'] = 'email'; } $new_selection[$detail->id]['email'] = $detail->email; if ($detail->remittance_advice) { $new_selection[$detail->id]['select'] = 'true'; $new_selection['count']++; } else { $new_selection[$detail->id]['select'] = 'false'; } } } $_SESSION['selected_output'][$type] = $selected = $new_selection; $this->view->set('type', 'remittance'); $this->view->set('fields', array('payee_name', 'gross_value', 'currency', 'payment_type', 'email')); $this->view->set('clickaction', 'view'); $this->view->set('no_ordering', true); $this->view->set('collection', $pltransactions); $this->_data['printaction'] = $output_details['printaction']; $this->_data['filename'] = 'Remittance'; unset($this->printaction['view']); $this->printAction(); $this->view->set('page_title', $this->getPageName("", 'Print Remitttances for')); $this->view->set('selected_output', $selected); $this->view->set('count_selected', $selected['count']); $this->view->set('type_id', $this->_data['id']); foreach ($this->_modules as $key => $value) { $modules[] = $key . '=' . $value; } $link = implode('&', $modules) . '&controller=' . $this->name . '&action=adjust_selection&type=' . $type; $this->view->set('link', $link); $this->setTemplateName('select_for_output'); }
public function hasCurrentActivity() { $db = DB::Instance(); // Check that no orders/invoices have been raised $model = DataObjectFactory::Factory('POrder'); $collection = new POrderCollection($model); $sh = new SearchHandler($collection, FALSE); $sh->addConstraint(new Constraint('plmaster_id', '=', $this->id)); $sh->addConstraint(new Constraint('status', 'NOT IN', '(' . $db->qstr($model->cancelStatus()) . ', ' . $db->qstr($model->invoiceStatus()) . ')')); if (count($collection->load($sh, null, RETURN_ROWS)) > 0) { return TRUE; } $model = DataObjectFactory::Factory('PInvoice'); $collection = new PInvoiceCollection($model); $sh = new SearchHandler($collection, FALSE); $sh->addConstraint(new Constraint('plmaster_id', '=', $this->id)); $sh->addConstraint(new Constraint('status', '!=', $model->paidStatus())); if (count($collection->load($sh, null, RETURN_ROWS)) > 0) { return TRUE; } $model = DataObjectFactory::Factory('PLTransaction'); $collection = new PLTransactionCollection($model); $sh = new SearchHandler($collection, FALSE); $sh->addConstraint(new Constraint('plmaster_id', '=', $this->id)); $sh->addConstraint(new Constraint('status', '!=', $model->paid())); if (count($collection->load($sh, null, RETURN_ROWS)) > 0) { return TRUE; } return FALSE; }