protected function _execute() { if (!$this->_purchase->loaded()) { throw new Exception("That purchase could not be found."); } if ($this->_purchase->refund_form->loaded()) { throw new Exception("That purchase already belongs to a refund-set."); } $this->_data->vendor_id = $this->_purchase->entity_id; $this->_data->refund_purchase_id = $this->_purchase->id; $this->_data->code = "R" . $this->_purchase->code; $this->_data->reference = $this->_purchase->reference ? 'R' . $this->_purchase->reference : NULL; $this->_data->alt_reference = $this->_purchase->alt_reference ? 'R' . $this->_purchase->alt_reference : NULL; $create_purchase = new Beans_Vendor_Purchase_Create($this->_beans_data_auth($this->_data)); $create_purchase_result = $create_purchase->execute(); if (!$create_purchase_result->success) { throw new Exception($create_purchase_result->error); } $this->_purchase->refund_form_id = $create_purchase_result->data->purchase->id; $this->_purchase->save(); return (object) array("purchase" => $create_purchase_result->data->purchase); }
/** * Purchase Order Actions */ public function action_purchasecreate() { $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."); } $create_purchase_data = new stdClass(); $create_purchase_data->vendor_id = $vendor_info[0]; $create_purchase_data->date_created = $this->request->post('date_created') ? date("Y-m-d", strtotime($this->request->post('date_created'))) : date("Y-m-d"); $create_purchase_data->date_due = $this->request->post('date_due') ? date("Y-m-d", strtotime($this->request->post('date_due'))) : date("Y-m-d", strtotime($create_purchase_data->date_created . ' +' . $account_info[1] . ' Days')); $create_purchase_data->remit_address_id = $this->request->post('remit_address_id'); $create_purchase_data->shipping_address_id = $this->request->post('shipping_address_id'); $create_purchase_data->account_id = $account_info[0]; $create_purchase_data->purchase_number = $this->request->post('purchase_number'); $create_purchase_data->so_number = $this->request->post('so_number'); $create_purchase_data->quote_number = $this->request->post('quote_number'); // Invoice Data if ($this->request->post('date_billed')) { $create_purchase_data->date_billed = $this->request->post('date_billed'); } if ($this->request->post('invoice_number')) { $create_purchase_data->invoice_number = $this->request->post('invoice_number'); } $create_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); $create_purchase_data->lines[] = $purchase_line; } } } $create_purchase = new Beans_Vendor_Purchase_Create($this->_beans_data_auth($create_purchase_data)); $create_purchase_result = $create_purchase->execute(); if (!$create_purchase_result->success) { return $this->_return_error("An error occurred when creating that purchase purchase:<br>" . $this->_beans_result_get_error($create_purchase_result)); } $html = new View_Partials_Vendors_Purchases_Purchase(); $html->purchase = $create_purchase_result->data->purchase; $this->_return_object->data->purchase = $create_purchase_result->data->purchase; $this->_return_object->data->purchase->html = $html->render(); }