public function save_contras() { if (!$this->loadData()) { $this->dataError(); sendBack(); } $db = DB::Instance(); $db->StartTrans(); $flash = Flash::Instance(); $errors = array(); $transactions = array(); $contras_sessionobject = new SessionData('sl_contras'); foreach ($this->_data['SLTransaction'] as $id => $data) { $data['contra'] = isset($data['contra']) && $data['contra'] == 'on'; $contras_sessionobject->updatePageData($id, $data, $errors); } $contra_total = isset($this->_data['contra_total']) ? $this->_data['contra_total'] : '0.00'; $contra_sum = 0; foreach ($contras_sessionobject->getPageData($errors) as $id => $data) { if (isset($data['contra']) && $data['contra'] == 'on') { // using bcadd to format value $transactions[$id] = bcadd($data['os_value'], 0); $contra_sum = bcadd($contra_sum, $data['os_value']); } } if (count($transactions) == 0) { $errors[] = 'You must select at least one transaction'; } elseif ($contra_total == $contra_sum) { $pl_journal_seq = $db->GenID('pl_journals_id_seq'); $sl_journal_seq = $db->GenID('sl_journals_id_seq'); // Create the PL and SL contra journals $sltransaction = DataObjectFactory::Factory('SLTransaction'); $sltransaction->load($id); $slcontra = array(); $slcontra['gross_value'] = $slcontra['net_value'] = bcmul($contra_sum, -1); $glparams = DataObjectFactory::Factory('GLParams'); $slcontra['glaccount_id'] = $glparams->contras_control_account(); $slcontra['glcentre_id'] = $glparams->balance_sheet_cost_centre(); $slcontra['transaction_date'] = date(DATE_FORMAT); $slcontra['tax_value'] = '0.00'; $slcontra['source'] = 'S'; $slcontra['transaction_type'] = 'J'; $slcontra['our_reference'] = $sl_journal_seq; $slcontra['currency_id'] = $this->_data['SLCustomer']['currency_id']; $slcontra['rate'] = $this->_data['SLCustomer']['rate']; $slcontra['payment_term_id'] = $this->_data['SLCustomer']['payment_term_id']; $plcontra = $slcontra; $slcontra['slmaster_id'] = $this->_data['SLCustomer']['id']; $slcontra['description'] = 'Contra Sales Ledger - PL Ref:' . $pl_journal_seq; $sltrans = SLTransaction::Factory($slcontra, $errors, 'SLTransaction'); if ($sltrans && $sltrans->save('', $errors) && $sltrans->saveGLTransaction($slcontra, $errors)) { $transactions[$sltrans->{$sltrans->idField}] = bcadd($slcontra['net_value'], 0); } else { $errors[] = 'Errror saving SL Transaction Contra : ' . $db->ErrorMsg(); $flash->addErrors($errors); } $plcontra['source'] = 'P'; $plcontra['our_reference'] = $pl_journal_seq; $plcontra['description'] = 'Contra Purchase Ledger - SL Ref:' . $sl_journal_seq; $plcontra['gross_value'] = $plcontra['net_value'] = bcmul($contra_sum, -1); $supplier = DataObjectFactory::Factory('PLSupplier'); $supplier->loadBy('company_id', $this->_data['SLCustomer']['company_id']); if ($supplier->isLoaded()) { $plcontra['plmaster_id'] = $supplier->{$supplier->idField}; $pltrans = PLTransaction::Factory($plcontra, $errors, 'PLTransaction'); } else { $pltrans = FALSE; } if (!$pltrans || !$pltrans->save('', $errors) || !$pltrans->saveGLTransaction($plcontra, $errors)) { $errors[] = 'Errror saving PL Transaction Contra : ' . $db->ErrorMsg(); $flash->addErrors($errors); } } else { $errors[] = 'Transactions sum mismatch Sum: ' . $contra_sum . ' Control Total: ' . $contra_total; } if (count($errors) > 0 || !SLTransaction::allocatePayment($transactions, $this->_data['id'], $errors) || !SLAllocation::saveAllocation($transactions, $errors)) { $db->FailTrans(); } if ($db->CompleteTrans()) { $contras_sessionobject->clear(); $flash->addMessage('Contra Transactions matched'); sendTo($this->name, 'view', $this->_modules, array('id' => $this->_data['id'])); } $flash->addErrors($errors); $this->outstanding_transactions(); }
public function save_confirmsale() { if (!isset($this->_data) || !$this->loadData()) { $this->dataError(); sendBack(); } $sorder = $this->_uses[$this->modeltype]; $sorder_data = $this->_data[$this->modeltype]; $flash = Flash::Instance(); $errors = array(); $db = DB::Instance(); $db->startTrans(); $slcustomer = DataObjectFactory::Factory('SLCustomer'); $slcustomer->load($sorder->slmaster_id); // save the customer details // Existing or new person? $person_data = array(); if (!empty($sorder_data['person_id']) && $sorder_data['person_id'] > 0) { // Existing person $sorder->person_id = $sorder_data['person_id']; $person = DataObjectFactory::Factory('Person'); $person->load($sorder_data['person_id']); if ($person->isLoaded()) { $sorder_data['party_id'] = $person->party_id; } } elseif (!empty($sorder_data['surname'])) { // TODO: Saving of person and party data needs to move to appropriate model // See also EmployeesController, CompanysController and PersonsController // new person $sorder_data['party_id'] = ''; $person_data['Party']['id'] = ''; $person_data['Party']['type'] = 'Person'; $person_data['Person']['title'] = $sorder_data['title']; $person_data['Person']['firstname'] = $sorder_data['firstname']; $person_data['Person']['surname'] = $sorder_data['surname']; $person_data['Person']['id'] = ''; $person_data['Person']['party_id'] = ''; $person_data['Person']['company_id'] = $slcustomer->company_id; $index = 0; foreach (array('phone', 'email') as $contact_type) { if (!empty($sorder_data[$contact_type])) { $cm = DataObjectFactory::Factory('PartyContactMethod'); $person_data[$index]['Contactmethod']['id'] = ''; $person_data[$index]['Contactmethod']['contact'] = $sorder_data[$contact_type]; $person_data[$index]['PartyContactMethod']['id'] = ''; $person_data[$index]['PartyContactMethod']['contactmethod_id'] = ''; $person_data[$index]['PartyContactMethod']['party_id'] = ''; $person_data[$index]['PartyContactMethod']['name'] = 'MAIN'; $person_data[$index]['PartyContactMethod']['type'] = $cm->getType($contact_type); $person_data[$index]['PartyContactMethod']['main'] = 't'; $person_data[$index]['PartyContactMethod']['billing'] = 't'; $person_data[$index]['PartyContactMethod']['shipping'] = 't'; $person_data[$index]['PartyContactMethod']['payment'] = 't'; $person_data[$index]['PartyContactMethod']['technical'] = 't'; $index++; } } if (parent::save('Person', $person_data, $errors)) { foreach ($this->saved_models as $model) { if (isset($model['Person'])) { $person = $model['Person']; $sorder->person_id = $person->id; $sorder_data['party_id'] = $person->party_id; } } } else { $errors[] = 'Error saving Customer Details : ' . $db->ErrorMsg(); } } foreach (array('invoice' => $sorder_data['inv_address_id'], 'delivery' => $sorder_data['del_address_id']) as $address_type => $address_id) { $address_data = array(); if (!empty($sorder_data[$address_type]['street1'])) { // New address $address_data['Address']['id'] = ''; $address_data['Address']['street1'] = $sorder_data[$address_type]['street1']; $address_data['Address']['street2'] = $sorder_data[$address_type]['street2']; $address_data['Address']['street3'] = $sorder_data[$address_type]['street3']; $address_data['Address']['town'] = $sorder_data[$address_type]['town']; $address_data['Address']['county'] = $sorder_data[$address_type]['county']; $address_data['Address']['postcode'] = $sorder_data[$address_type]['postcode']; $address_data['Address']['countrycode'] = $sorder_data[$address_type]['countrycode']; $address_data['PartyAddress']['address_id'] = ''; } elseif (!empty($person_data['Person'])) { // Existing address needs to be linked to new person $address_data['PartyAddress']['address_id'] = $address_id; } if (!empty($sorder_data[$address_type]['street1']) || !empty($person_data['Person'])) { // If new address or new person, link address to party $address_data['PartyAddress']['id'] = ''; $address_data['PartyAddress']['party_id'] = $sorder_data['party_id']; $address_data['PartyAddress']['name'] = 'MAIN'; $address_data['PartyAddress']['main'] = $address_type == 'invoice' ? 't' : 'f'; $address_data['PartyAddress']['billing'] = $address_type == 'invoice' ? 't' : 'f'; $address_data['PartyAddress']['shipping'] = $address_type == 'delivery' ? 't' : 'f'; $address_data['PartyAddress']['payment'] = $address_type == 'invoice' ? 't' : 'f'; $address_data['PartyAddress']['technical'] = 'f'; if (parent::save('Address', $address_data, $errors)) { foreach ($this->saved_models as $model) { if (isset($model['Address'])) { $address = $model['Address']; if ($address_type == 'invoice') { $sorder_data['inv_address_id'] = $address->id; } else { $sorder_data['del_address_id'] = $address->id; } } } } else { $errors[] = 'Error saving Customer ' . $address_type . ' Address Details : ' . $db->ErrorMsg(); } } } if (!empty($sorder_data['inv_address_id']) && $sorder_data['inv_address_id'] > 0) { $sorder->inv_address_id = $sorder_data['inv_address_id']; } if (!empty($sorder_data['del_address_id']) && $sorder_data['del_address_id'] > 0) { $sorder->del_address_id = $sorder_data['del_address_id']; } else { $sorder->del_address_id = $sorder_data['inv_address_id']; } if (count($errors) > 0 || !$sorder->save()) { $errors[] = 'Error updating customer order details'; } // create and post the invoice if (count($errors) == 0) { $invoice = $this->createPostInvoice($sorder, $errors); } // Create the Payment if (count($errors) == 0) { $cb_data = array(); $cb_data['cb_account_id'] = $slcustomer->cb_account_id; $cb_data['source'] = 'S'; $cb_data['slmaster_id'] = $sorder->slmaster_id; $cb_data['company_id'] = $slcustomer->company_id; if ($sorder->person_id > 0) { $cb_data['person_id'] = $sorder->person_id; } $cb_data['ext_reference'] = $sorder_data['ext_reference']; $cb_data['payment_term_id'] = $slcustomer->payment_term_id; $cb_data['payment_type_id'] = $sorder_data['payment_type_id']; $cb_data['transaction_type'] = 'R'; $cb_data['currency_id'] = $sorder->currency_id; $cb_data['transaction_date'] = date(DATE_FORMAT); // $cb_data['net_value'] = $sorder_data['gross_value']; $cb_data['net_value'] = bcsub($invoice->gross_value, $invoice->settlement_discount); if (!SLTransaction::saveTransaction($cb_data, $errors)) { $errors[] = 'Error saving Payment Details'; } } // Match Payment to Invoice if (count($errors) == 0) { if ($invoice->isLoaded()) { $invoice_trans = DataObjectFactory::Factory('SLTransaction'); $invoice_trans->identifierField = gross_value; $cc = new ConstraintChain(); $cc->add(new Constraint('transaction_type', '=', 'I')); $cc->add(new Constraint('status', '=', 'O')); $cc->add(new Constraint('our_reference', '=', $invoice->invoice_number)); $transactions = $invoice_trans->getAll($cc); $transactions[$cb_data['ledger_transaction_id']] = $cb_data['payment_value']; // Save settlement discount if present? if ($invoice->settlement_discount > 0) { $payment_term = DataObjectFactory::Factory('PaymentTerm'); $payment_term->load($slcustomer->payment_term_id); // Create GL Journal for settlement discount $discount = $cb_data; $discount['gross_value'] = $discount['net_value'] = $invoice->settlement_discount; $discount['glaccount_id'] = $payment_term->sl_discount_glaccount_id; $discount['glcentre_id'] = $payment_term->sl_discount_glcentre_id; $discount['tax_value'] = '0.00'; $discount['source'] = 'S'; $discount['transaction_type'] = 'SD'; $discount['description'] = !empty($payment_term->sl_discount_description) ? $payment_term->sl_discount_description . ' ' : ''; $discount['description'] .= $cb_data->description; $discount['status'] = 'P'; $sldiscount = SLTransaction::Factory($discount, $errors, 'SLTransaction'); if ($sldiscount && $sldiscount->save('', $errors) && $sldiscount->saveGLTransaction($discount, $errors)) { $transactions[$sldiscount->{$sldiscount->idField}] = bcadd($discount['net_value'], 0); } else { $errors[] = 'Errror saving Settlement Discount : ' . $db->ErrorMsg(); $flash->addErrors($errors); } } if (!SLTransaction::allocatePayment($transactions, $invoice->slmaster_id, $errors) || !SLAllocation::saveAllocation($transactions, $errors)) { $errors[] = 'Error allocating Payment'; } } else { $errors[] = 'Error matching to Invoice'; } } // Check for errors if (count($errors) > 0) { $flash->clear(); $flash->addErrors($errors); $flash->addError('Sale confirmation failed'); $db->failTrans(); } else { $flash->addMessage('Sale Confirmed'); } $db->CompleteTrans(); if (count($errors) == 0) { // now print the invoice to the users default printer $userPreferences = UserPreferences::instance(EGS_USERNAME); $defaultPrinter = $userPreferences->getPreferenceValue('default_printer', 'shared'); if (empty($defaultPrinter)) { // Use normal print action // ATTN: what happens here? If we don't have a default printer set it's going to ignore our request? $this->_data['invoice_id'] = $invoice->id; $this->_data['printaction'] = 'printinvoice'; parent::printaction(); $this->printaction = array('Print' => 'Print'); $this->printtype = array('pdf' => 'PDF'); $this->_templateName = $this->getTemplateName('printaction'); return; } else { // Overide print action $data = array(); $data['invoice_id'] = $invoice->id; $data['printtype'] = 'pdf'; $data['printaction'] = 'Print'; $data['printer'] = $defaultPrinter; // call printInvoice and decode the response, output errors / messages $response = json_decode($this->printInvoice($invoice, $data)); if ($response->status === true) { $flash->addMessage('Print Sales Invoice Completed'); $invoice->update($invoice->id, array('date_printed', 'print_count'), array(fix_date(date(DATE_FORMAT)), $invoice->print_count + 1)); } else { $flash->addError('Print Sales Invoice Failed'); $flash->addError($response->message); } } // we're not using JavaScript to get here // so just go back to the order view } sendTo($this->name, 'view', $this->_modules, array('id' => $sorder->id)); }