function validate($data, &$errors) { $return = TRUE; $today = fix_date(date(DATE_FORMAT)); $payment_date = fix_date($data['payment_date']); $process_date = $this->getProcessDate($payment_date); if ($process_date <= $today) { $errors[] = 'Payment Date must be at least 2 working days from today'; $return = FALSE; } if (in_array(date('D', strtotime($payment_date)), array('Sat', 'Sun'))) { $errors[] = 'Payment Date cannot be Saturday or Sunday'; $return = FALSE; } if (isset($data['PLTransaction'])) { $progressbar = new progressBar('checking_supplier_details'); $callback = function ($unused, $supplier_id) use(&$errors) { $supplier = DataObjectFactory::Factory('PLSupplier'); $supplier->load($supplier_id); if (!$supplier->isLoaded()) { $errors[] = 'Error checking supplier'; return FALSE; } elseif (!is_numeric($supplier->sort_code) || strlen($supplier->sort_code) != 6 || !is_numeric($supplier->account_number) || strlen($supplier->account_number) != 8) { $errors[$supplier_id] = 'Invalid Bank Account details for ' . $supplier->name; $errors[$supplier->name] = $supplier->name . ' Sort Code:' . $supplier->sort_code . ' Account Number:' . $supplier->account_number; return FALSE; } }; $return = $progressbar->process($data['PLTransaction'], $callback); } return $return; }
static function depreciateAll(&$errors) { $assets = new AssetCollection(DataObjectFactory::Factory('Asset')); $sh = new SearchHandler($assets, false); $sh->addConstraint(new Constraint('disposal_date', 'is', 'NULL')); $assets->load($sh); if ($assets) { $progressbar = new progressBar('depreciation'); $callback = function ($asset, $key) use(&$errors) { assetHandling::depreciateAsset($asset, $errors); if (count($errors) > 0) { return FALSE; } }; if ($progressbar->process($assets, $callback) === FALSE) { $errors[] = 'Error running depreciation'; } } else { $errors[] = 'Failed to load assets'; } }
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 process_output() { set_time_limit(0); $flash = Flash::Instance(); $errors = array(); $header = DataObjectFactory::Factory('OutputHeader'); $header->load($this->_data['id']); switch ($this->_data['type']) { case 'cancel': if (!$header->delete()) { $errors[] = 'Failed to delete entry'; } break; case 'process': if ($header) { $counter = 0; $data = array('filename' => $header->filename, 'printtype' => $header->printtype, 'printer' => $header->printer, 'emailtext' => $header->emailtext, 'fieldnames' => $header->fieldnames, 'fieldseparater' => $header->fieldseparater, 'textdelimiter' => $header->textdelimiter); $progressbar = new progressBar($header->process); $controller = $this; $callback = function ($detail, $key) use(&$data, &$counter, &$errors, $controller, $header) { $data['printaction'] = $detail->printaction; $data['email'] = $detail->email; $data['print_copies'] = $detail->print_copies; // filename can NOT be the same for any of the files in this loop, this is because // printIPP gets confused when trying to print the same file... $data['filename'] = $header->filename . "-" . $counter++; $result = call_user_func(array($controller, $header->process), $detail->select_id, $data); // we cannot always expect an array back if (is_array($result)) { $message = $result['message']; $status = $result['status']; } else { $status = $result; } if (!$status) { $errors[] = $message; $detail->status = 'Failed'; } else { $detail->status = 'OK'; } $detail->save(); }; if ($progressbar->process($header->output_details, $callback) === FALSE || count($errors) > 0) { $errors[] = 'Error outputing ' . $header->type . ' batch'; } if (!$header->update($header->id, 'processed', TRUE)) { $errors[] = 'Error marking process as complete'; } } break; } if (count($errors) > 0) { $flash->addErrors($errors); } sendTo($this->name, 'output_summary', $this->_modules, array('type' => $header->type)); }