コード例 #1
0
ファイル: create.php プロジェクト: rrsc/beansbooks
 protected function _execute()
 {
     // Independently validate $this->_date_billed
     if ($this->_date_billed and $this->_date_billed != date("Y-m-d", strtotime($this->_date_billed))) {
         throw new Exception("Invalid invoice date: must be in YYYY-MM-DD format.");
     }
     // Three laws.
     $temp_date_created = isset($this->_data->date_created) ? $this->_data->date_created : date("Y-m-d");
     if ($temp_date_created != date("Y-m-d", strtotime($temp_date_created))) {
         throw new Exception("Invalid sale date: must be in YYYY-MM-DD format.");
     }
     if ($this->_date_billed and strtotime($this->_date_billed) < strtotime($temp_date_created)) {
         throw new Exception("Invalid invoice date: must on or after the date of the sale order.");
     }
     if ($this->_date_due and $this->_date_due != date("Y-m-d", strtotime($this->_date_due))) {
         throw new Exception("Invalid due date: must be in YYYY-MM-DD format.");
     }
     if ($this->_date_due and strtotime($this->_date_due) < strtotime($this->_date_billed)) {
         throw new Exception("Invalid due date: must be on or after the bill date.");
     }
     $this->_sale->entity_id = isset($this->_data->customer_id) ? (int) $this->_data->customer_id : NULL;
     $this->_sale->account_id = isset($this->_data->account_id) ? $this->_data->account_id : NULL;
     $this->_sale->refund_form_id = isset($this->_data->refund_sale_id) ? $this->_data->refund_sale_id : NULL;
     $this->_sale->sent = isset($this->_data->sent) ? $this->_data->sent : NULL;
     $this->_sale->date_created = isset($this->_data->date_created) ? $this->_data->date_created : NULL;
     $this->_sale->code = (isset($this->_data->sale_number) and $this->_data->sale_number) ? $this->_data->sale_number : "AUTOGENERATE";
     $this->_sale->reference = isset($this->_data->order_number) ? $this->_data->order_number : NULL;
     $this->_sale->alt_reference = isset($this->_data->po_number) ? $this->_data->po_number : NULL;
     $this->_sale->aux_reference = isset($this->_data->quote_number) ? $this->_data->quote_number : NULL;
     $this->_sale->tax_exempt = (isset($this->_data->tax_exempt) and $this->_data->tax_exempt) ? TRUE : FALSE;
     $this->_sale->tax_exempt_reason = ($this->_sale->tax_exempt and isset($this->_data->tax_exempt_reason) and $this->_data->tax_exempt_reason) ? $this->_data->tax_exempt_reason : NULL;
     $this->_sale->billing_address_id = isset($this->_data->billing_address_id) ? (int) $this->_data->billing_address_id : NULL;
     $this->_sale->shipping_address_id = isset($this->_data->shipping_address_id) ? (int) $this->_data->shipping_address_id : NULL;
     // Handle Default Account Receivable
     // Customer Default Account Receivable
     if ($this->_sale->account_id === NULL) {
         $customer = $this->_load_customer($this->_sale->entity_id);
         if ($customer->loaded() and $customer->default_account_id) {
             $this->_sale->account_id = $customer->default_account_id;
         }
     }
     // Default Account Receivable
     if ($this->_sale->account_id === NULL) {
         $this->_sale->account_id = $this->_beans_setting_get('account_default_receivable');
     }
     // Make sure we have good sale information before moving on.
     $this->_validate_customer_sale($this->_sale);
     $this->_sale->total = 0.0;
     $this->_sale->amount = 0.0;
     if (!isset($this->_data->lines) or !is_array($this->_data->lines) or !count($this->_data->lines)) {
         throw new Exception("Invalid sale line items: none provided.");
     }
     if (isset($this->_data->taxes)) {
         if (!is_array($this->_data->taxes)) {
             throw new Exception("Invalid sale taxes: must be an array.");
         }
         foreach ($this->_data->taxes as $sale_tax) {
             $new_sale_tax = $this->_default_form_tax();
             $new_sale_tax->tax_id = isset($sale_tax->tax_id) ? (int) $sale_tax->tax_id : NULL;
             if (!$new_sale_tax->tax_id) {
                 throw new Exception("Invalid sale tax ID: none provided.");
             }
             $tax = $this->_load_tax($new_sale_tax->tax_id);
             if (!$tax->loaded()) {
                 throw new Exception("Invalid sale tax ID: tax not found.");
             }
             $new_sale_tax->tax_percent = $tax->percent;
             $new_sale_tax->form_line_amount = 0.0;
             $new_sale_tax->form_line_taxable_amount = 0.0;
             $new_sale_tax->total = 0.0;
             if (!isset($this->_sale_taxes[$new_sale_tax->tax_id])) {
                 $this->_sale_taxes[$new_sale_tax->tax_id] = $new_sale_tax;
             }
         }
     }
     foreach ($this->_data->lines as $sale_line) {
         $new_sale_line = $this->_default_form_line();
         $new_sale_line->account_id = isset($sale_line->account_id) ? (int) $sale_line->account_id : NULL;
         $new_sale_line->tax_exempt = $this->_sale->tax_exempt || (isset($sale_line->tax_exempt) and $sale_line->tax_exempt) ? TRUE : FALSE;
         $new_sale_line->description = isset($sale_line->description) ? $sale_line->description : NULL;
         $new_sale_line->amount = isset($sale_line->amount) ? $this->_beans_round($sale_line->amount) : NULL;
         $new_sale_line->quantity = isset($sale_line->quantity) ? (double) $sale_line->quantity : NULL;
         // Handle Default Income Account
         if ($new_sale_line->account_id === NULL) {
             $new_sale_line->account_id = $this->_beans_setting_get('account_default_income');
         }
         $this->_validate_customer_sale_line($new_sale_line);
         $new_sale_line->total = $this->_beans_round($new_sale_line->amount * $new_sale_line->quantity);
         if (!$new_sale_line->tax_exempt) {
             foreach ($this->_sale_taxes as $tax_id => $sale_tax) {
                 $this->_sale_taxes[$tax_id]->form_line_taxable_amount = $this->_beans_round($this->_sale_taxes[$tax_id]->form_line_taxable_amount + $new_sale_line->total);
             }
         }
         $this->_sale->amount = $this->_beans_round($this->_sale->amount + $new_sale_line->total);
         $this->_sale_lines[] = $new_sale_line;
     }
     $this->_sale->total = $this->_beans_round($this->_sale->total + $this->_sale->amount);
     foreach ($this->_sale_taxes as $tax_id => $sale_tax) {
         $this->_sale_taxes[$tax_id]->total = $this->_beans_round($sale_tax->tax_percent * $sale_tax->form_line_taxable_amount);
         $this->_sale_taxes[$tax_id]->form_line_amount = $this->_sale->amount;
         $this->_sale->total = $this->_beans_round($this->_sale->total + $this->_sale_taxes[$tax_id]->total);
     }
     // Validate Totals
     if ($this->_sale->refund_form_id) {
         $refund_form = $this->_load_customer_sale($this->_sale->refund_form_id);
         if (!$refund_form->loaded()) {
             throw new Exception("That refund_sale_id was not found.");
         }
         $original_sale = $refund_form;
         $refund_sale = $this->_sale;
         if ($original_sale->total > 0.0 and $refund_sale->total > 0.0 or $original_sale->total < 0.0 and $refund_sale->total < 0.0) {
             throw new Exception("Refund and original sale totals must offset each other ( they cannot both be positive or negative ).");
         }
         if (abs($refund_sale->total) > abs($original_sale->total)) {
             throw new Exception("The refund total cannot be greater than the original sale total.");
         }
     }
     // Save Sale + Children
     $this->_sale->save();
     foreach ($this->_sale_lines as $j => $sale_line) {
         $sale_line->form_id = $this->_sale->id;
         $sale_line->save();
     }
     foreach ($this->_sale_taxes as $tax_id => $sale_tax) {
         $this->_sale_taxes[$tax_id]->form_id = $this->_sale->id;
         $this->_sale_taxes[$tax_id]->save();
     }
     if ($this->_sale->code == "AUTOGENERATE") {
         $this->_sale->code = $this->_sale->id;
     }
     $this->_sale->save();
     $sale_calibrate = new Beans_Customer_Sale_Calibrate($this->_beans_data_auth((object) array('ids' => array($this->_sale->id))));
     $sale_calibrate_result = $sale_calibrate->execute();
     if (!$sale_calibrate_result->success) {
         // We've had an account transaction failure and need to delete the sale we just created.
         $delete_sale = new Beans_Customer_Sale_Delete($this->_beans_data_auth((object) array('id' => $this->_sale->id)));
         $delete_sale_result = $delete_sale->execute();
         // TODO - 100PERCENTWORKING
         if (!$delete_sale_result->success) {
             throw new Exception("Error creating account transaction for sale. COULD NOT DELETE SALE! " . $delete_sale_result->error);
         }
         throw new Exception("Error trying to create sale: " . $sale_calibrate_result->error);
     }
     // Reload the sale.
     $this->_sale = $this->_load_customer_sale($this->_sale->id);
     if ($this->_date_billed) {
         $customer_sale_invoice = new Beans_Customer_Sale_Invoice($this->_beans_data_auth((object) array('id' => $this->_sale->id, 'date_billed' => $this->_date_billed, 'date_due' => $this->_date_due ? $this->_date_due : FALSE)));
         $customer_sale_invoice_result = $customer_sale_invoice->execute();
         // If it fails - we undo everything ( this was a single request, after all ).
         if (!$customer_sale_invoice_result->success) {
             $delete_sale = new Beans_Customer_Sale_Delete($this->_beans_data_auth((object) array('id' => $this->_sale->id)));
             $delete_sale_result = $delete_sale->execute();
             // TODO - 100PERCENTWORKING
             if (!$delete_sale_result->success) {
                 throw new Exception("Error creating account transaction for sale. COULD NOT DELETE SALE! " . $delete_sale_result->error);
             }
             throw new Exception("Error creating sale invoice: " . $customer_sale_invoice_result->error);
         }
         return $this->_beans_return_internal_result($customer_sale_invoice_result);
     }
     // We need to reload the sale so that we can get the correct balance, etc.
     $this->_sale = $this->_load_customer_sale($this->_sale->id);
     return (object) array("sale" => $this->_return_customer_sale_element($this->_sale));
 }
コード例 #2
0
ファイル: json.php プロジェクト: rrsc/beansbooks
 public function action_salecancel()
 {
     $sale_id = $this->request->post('sale_id');
     $sale_delete = new Beans_Customer_Sale_Delete($this->_beans_data_auth((object) array('id' => $sale_id)));
     $sale_delete_result = $sale_delete->execute();
     if (!$sale_delete_result->success) {
         $sale_cancel = new Beans_Customer_Sale_Cancel($this->_beans_data_auth((object) array('id' => $sale_id)));
         $sale_cancel_result = $sale_cancel->execute();
         if (!$sale_cancel_result->success) {
             return $this->_return_error("An error occurred when trying to cancel that sale:<br>" . $this->_beans_result_get_error($sale_cancel_result));
         }
         $html = new View_Partials_Customers_Sales_Sale();
         $html->sale = $sale_cancel_result->data->sale;
         $html->invoice_view = $this->request->post('invoice_view');
         $sale_cancel_result->data->sale->html = $html->render();
         $this->_return_object->data->sale = $sale_cancel_result->data->sale;
     }
 }