protected function _execute() { if (!$this->_sale->loaded()) { throw new Exception("That sale could not be found."); } if ($this->_sale->refund_form->loaded()) { throw new Exception("That sale already belongs to a refund-set."); } $this->_data->customer_id = $this->_sale->entity_id; $this->_data->refund_sale_id = $this->_sale->id; $this->_data->code = "R" . $this->_sale->code; $this->_data->reference = $this->_sale->reference ? 'R' . $this->_sale->reference : NULL; $this->_data->alt_reference = $this->_sale->alt_reference ? 'R' . $this->_sale->alt_reference : NULL; // Add default return account if (!isset($this->_data->account_id) or !$this->_data->account_id) { $this->_data->account_id = $this->_beans_setting_get('account_default_returns'); } $create_sale = new Beans_Customer_Sale_Create($this->_beans_data_auth($this->_data)); $create_sale_result = $create_sale->execute(); if (!$create_sale_result->success) { throw new Exception($create_sale_result->error); } $this->_sale->refund_form_id = $create_sale_result->data->sale->id; $this->_sale->save(); return (object) array("sale" => $create_sale_result->data->sale); }
public function action_salecreate() { $line_keys = array(); foreach ($this->request->post() as $key => $value) { if ($value == "SALELINEKEY") { $line_keys[] = str_replace('sale-line-key-', '', $key); } } $account_info = explode('#', $this->request->post('account')); $customer_info = explode('#', $this->request->post('customer')); if (count($account_info) != 2) { return $this->_return_error("Please select a valid account for this sale."); } if (count($customer_info) != 5) { return $this->_return_error("Please select a valid customer for this sale."); } $create_sale_data = new stdClass(); $create_sale_data->customer_id = $customer_info[0]; $create_sale_data->date_created = $this->request->post('date_created') ? date("Y-m-d", strtotime($this->request->post('date_created'))) : date("Y-m-d"); $create_sale_data->billing_address_id = $this->request->post('billing_address_id'); $create_sale_data->shipping_address_id = $this->request->post('shipping_address_id'); $create_sale_data->account_id = $account_info[0]; $create_sale_data->sale_number = $this->request->post('sale_number'); $create_sale_data->order_number = $this->request->post('order_number'); $create_sale_data->quote_number = $this->request->post('quote_number'); $create_sale_data->po_number = $this->request->post('po_number'); $create_sale_data->tax_exempt = $this->request->post('form_tax_exempt') ? TRUE : FALSE; $create_sale_data->tax_exempt_reason = $this->request->post('form_tax_exempt_reason'); // IF INVOICE if ($this->request->post('date_billed')) { $create_sale_data->date_created = $this->request->post('date_billed'); $create_sale_data->date_billed = $this->request->post('date_billed'); if ($this->request->post('date_due')) { $create_sale_data->date_due = $this->request->post('date_due'); } } else { if ($this->request->post('invoice_view')) { $create_sale_data->date_created = date("Y-m-d"); $create_sale_data->date_billed = date("Y-m-d"); if ($this->request->post('date_due')) { $create_sale_data->date_due = $this->request->post('date_due'); } } } $create_sale_data->taxes = array(); if ($this->request->post('form-tax_ids')) { foreach (explode('#', $this->request->post('form-tax_ids')) as $tax_id) { if (trim($tax_id)) { $create_sale_data->taxes[] = (object) array('tax_id' => $tax_id); } } } $create_sale_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))) { $sale_line = new stdClass(); $sale_line->account_id = $this->request->post('line-account_id-' . $line_key); $sale_line->description = $this->request->post('line-description-' . $line_key); $sale_line->amount = $this->request->post('line-price-' . $line_key); $sale_line->quantity = $this->request->post('line-quantity-' . $line_key); $sale_line->tax_exempt = $this->request->post('line-tax-exempt-' . $line_key) ? TRUE : FALSE; $create_sale_data->lines[] = $sale_line; } } } $create_sale = new Beans_Customer_Sale_Create($this->_beans_data_auth($create_sale_data)); $create_sale_result = $create_sale->execute(); if (!$create_sale_result->success) { return $this->_return_error("An error occurred when creating that sale:<br>" . $this->_beans_result_get_error($create_sale_result)); } $html = new View_Partials_Customers_Sales_Sale(); $html->sale = $create_sale_result->data->sale; $html->invoice_view = $this->request->post('invoice_view'); $this->_return_object->data->sale = $create_sale_result->data->sale; $this->_return_object->data->sale->html = $html->render(); }