public function action_purchaseupdate() { $purchase_id = $this->request->post('purchase_id'); $line_keys = array(); foreach ($this->request->post() as $key => $value) { if ($value == "PURCHASELINEKEY") { $line_keys[] = str_replace('purchase-line-key-', '', $key); } } $account_info = explode('#', $this->request->post('account')); $vendor_info = explode('#', $this->request->post('vendor')); if (count($account_info) != 2) { return $this->_return_error("Please select a valid account for this expense."); } if (!isset($vendor_info[0]) or !strlen($vendor_info[0])) { return $this->_return_error("Please select a valid vendor for this expense."); } $update_purchase_data = new stdClass(); $update_purchase_data->id = $purchase_id; $update_purchase_data->vendor_id = $vendor_info[0]; $update_purchase_data->date_created = $this->request->post('date_created') ? date("Y-m-d", strtotime($this->request->post('date_created'))) : date("Y-m-d"); $update_purchase_data->date_due = $this->request->post('date_due') ? date("Y-m-d", strtotime($this->request->post('date_due'))) : date("Y-m-d", strtotime($update_purchase_data->date_created . ' +' . $account_info[1] . ' Days')); $update_purchase_data->remit_address_id = $this->request->post('remit_address_id'); $update_purchase_data->shipping_address_id = $this->request->post('shipping_address_id'); $update_purchase_data->account_id = $account_info[0]; $update_purchase_data->purchase_number = $this->request->post('purchase_number'); $update_purchase_data->so_number = $this->request->post('so_number'); $update_purchase_data->quote_number = $this->request->post('quote_number'); // Invoice Data if ($this->request->post('date_billed')) { $update_purchase_data->date_billed = $this->request->post('date_billed'); } if ($this->request->post('invoice_number')) { $update_purchase_data->invoice_number = $this->request->post('invoice_number'); } $update_purchase_data->lines = array(); foreach ($line_keys as $line_key) { if (($this->request->post('line-description-' . $line_key) or floatval($this->request->post('line-price-' . $line_key)) or floatval($this->request->post('line-quantity-' . $line_key))) and (!$this->request->post('line-account_id-' . $line_key) or !$this->request->post('line-description-' . $line_key) or !strlen($this->request->post('line-price-' . $line_key)) or !strlen($this->request->post('line-quantity-' . $line_key)))) { return $this->_return_error("One of those line items is missing a value."); } else { if ($this->request->post('line-account_id-' . $line_key) and $this->request->post('line-description-' . $line_key) and strlen($this->request->post('line-price-' . $line_key)) and strlen($this->request->post('line-quantity-' . $line_key))) { $purchase_line = new stdClass(); $purchase_line->account_id = $this->request->post('line-account_id-' . $line_key); $purchase_line->description = $this->request->post('line-description-' . $line_key); $purchase_line->amount = $this->request->post('line-price-' . $line_key); $purchase_line->quantity = $this->request->post('line-quantity-' . $line_key); $update_purchase_data->lines[] = $purchase_line; } } } $update_purchase = new Beans_Vendor_Purchase_Update($this->_beans_data_auth($update_purchase_data)); $update_purchase_result = $update_purchase->execute(); if (!$update_purchase_result->success) { return $this->_return_error("An error occurred when updating that purchase purchase:<br>" . $this->_beans_result_get_error($update_purchase_result)); } $html = new View_Partials_Vendors_Purchases_Purchase(); $html->purchase = $update_purchase_result->data->purchase; $this->_return_object->data->purchase = $update_purchase_result->data->purchase; $this->_return_object->data->purchase->html = $html->render(); }
protected function _execute() { if (!$this->_purchase->loaded()) { throw new Exception("That purchase could not be found."); } if ($this->_purchase->date_cancelled) { throw new Exception("A purchase cannot be converted to an invoice after it has been cancelled."); } if ($this->_purchase->date_billed) { throw new Exception("That purchase has already been converted to an invoice."); } if (!$this->_transaction_purchase_account_id) { throw new Exception("INTERNAL ERROR: Could not find default PO account."); } if (!$this->_transaction_purchase_line_account_id) { throw new Exception("INTERNAL ERROR: Could not find default PO Line account."); } if ($this->_date_billed != date("Y-m-d", strtotime($this->_date_billed))) { throw new Exception("Invalid invoice date: must be in YYYY-MM-DD format."); } if (strtotime($this->_date_billed) < strtotime($this->_purchase->date_created)) { throw new Exception("Invalid invoice date: must be on or after the creation date of " . $this->_purchase->date_created . "."); } if ($this->_invoice_number and strlen($this->_invoice_number) > 16) { throw new Exception("Invalid invoice number: maximum of 16 characters."); } if ($this->_so_number and strlen($this->_so_number) > 16) { throw new Exception("Invalid SO number: maximum of 16 characters."); } // Figure out if we need to create an adjusting entry. if ($this->_invoice_amount and $this->_purchase->total != $this->_invoice_amount) { if (!$this->_invoice_adjustment_description) { throw new Exception("Invalid invoice adjustment description: none provided."); } if (!$this->_invoice_adjustment_account_id) { throw new Exception("Invalid invoice adjustment writeoff account: none provided."); } // Add line to purchase. $purchase_lines = array(); foreach ($this->_purchase->form_lines->find_all() as $line) { $purchase_lines[] = (object) array('description' => $line->description, 'account_id' => $line->account_id, 'quantity' => $line->quantity, 'amount' => $line->amount); } $purchase_lines[] = (object) array('description' => $this->_invoice_adjustment_description, 'account_id' => $this->_invoice_adjustment_account_id, 'quantity' => 1, 'amount' => $this->_beans_round($this->_invoice_amount - $this->_purchase->total), 'adjustment' => TRUE); if (!$this->_validate_only) { $vendor_purchase_update = new Beans_Vendor_Purchase_Update($this->_beans_data_auth((object) array('id' => $this->_purchase->id, 'lines' => $purchase_lines))); $vendor_purchase_update_result = $vendor_purchase_update->execute(); if (!$vendor_purchase_update_result->success) { throw new Exception("Could not adjust purchase: " . $vendor_purchase_update_result->error); } // Re-load purchase. $this->_purchase = $this->_load_vendor_purchase($this->_purchase->id); } } if ($this->_validate_only) { return (object) array(); } $this->_purchase->date_billed = $this->_date_billed; $this->_purchase->date_due = date("Y-m-d", strtotime($this->_purchase->date_billed . ' +' . $this->_purchase->account->terms . ' Days')); if (strlen($this->_invoice_number)) { $this->_purchase->aux_reference = $this->_invoice_number; } if ($this->_so_number) { $this->_purchase->reference = $this->_so_number; } $this->_purchase->save(); $purchase_calibrate = new Beans_Vendor_Purchase_Calibrate($this->_beans_data_auth((object) array('ids' => array($this->_purchase->id)))); $purchase_calibrate_result = $purchase_calibrate->execute(); $this->_purchase = $this->_load_vendor_purchase($this->_purchase->id); if (!$purchase_calibrate_result->success) { $this->_purchase->date_billed = NULL; $this->_purchase->date_due = NULL; $this->_purchase->aux_reference = NULL; $this->_purchase->reference = NULL; $this->_purchase->save(); throw new Exception("Error trying to invoice purchase: " . $purchase_calibrate_result->error); } // Recalibrate Payments $vendor_payment_calibrate = new Beans_Vendor_Payment_Calibrate($this->_beans_data_auth((object) array('form_ids' => array($this->_purchase->id)))); $vendor_payment_calibrate_result = $vendor_payment_calibrate->execute(); if (!$vendor_payment_calibrate_result->success) { throw new Exception("Error encountered when calibrating payments: " . $vendor_payment_calibrate_result->error); } $this->_purchase = $this->_load_vendor_purchase($this->_purchase->id); return (object) array("purchase" => $this->_return_vendor_purchase_element($this->_purchase)); }