public function action_setup() { // Update chart_setup $beans_company_update = new Beans_Setup_Company_Update($this->_beans_data_auth((object) array('settings' => (object) array('chart_setup' => TRUE)))); $beans_company_update_result = $beans_company_update->execute(); if ($this->_beans_result_check($beans_company_update_result)) { $this->_view->beans_company_update_result = $beans_company_update_result; } $this->request->redirect('/accounts'); }
public function action_calibratedate() { $date = $this->request->post('date'); $manual = $this->request->post('manual'); if (!$date or $date != date("Y-m-d", strtotime($date))) { return $this->_return_error('Invalid date provided: ' . $date . ' expected YYYY-MM-DD'); } // If manual, we don;t want them trying to calibrate pre-fye if ($manual == "1") { $account_closebooks_check = new Beans_Account_Closebooks_Check($this->_beans_data_auth()); $account_closebooks_check_result = $account_closebooks_check->execute(); if (!$account_closebooks_check_result->success) { return $this->_return_error("An unexpected error occurred when trying to validate the date: " . $account_closebooks_check_result->error); } if ($account_closebooks_check_result->data->previous_date && strtotime($date) <= strtotime($account_closebooks_check_result->data->previous_date)) { return $this->_return_error("The books were closed most recently on " . $account_closebooks_check_result->data->previous_date . ". " . "Please choose a date after that to begin manual calibration."); } } // This can take a while. set_time_limit(60 * 10); ini_set('memory_limit', '256M'); // Recalibrate Customer Invoices / Cancellations $customer_sale_calibrate = new Beans_Customer_Sale_Calibrate($this->_beans_data_auth((object) array('date_after' => $date, 'date_before' => $date))); $customer_sale_calibrate_result = $customer_sale_calibrate->execute(); if (!$customer_sale_calibrate_result->success) { return $this->_return_error('Error updating customer sales: ' . $customer_sale_calibrate_result->error); } // Recalibrate any payments tied to these sales AFTER this transaction date. $customer_payment_calibrate = new Beans_Customer_Payment_Calibrate($this->_beans_data_auth((object) array('date_after' => $date, 'date_before' => $date))); $customer_payment_calibrate_result = $customer_payment_calibrate->execute(); if (!$customer_payment_calibrate_result->success) { return $this->_return_error('Error updating customer payments: ' . $customer_payment_calibrate_result->error); } // Recalibrate Vendor Invoices / Cancellations $vendor_purchase_calibrate = new Beans_Vendor_Purchase_Calibrate($this->_beans_data_auth((object) array('date_after' => $date, 'date_before' => $date))); $vendor_purchase_calibrate_result = $vendor_purchase_calibrate->execute(); if (!$vendor_purchase_calibrate_result->success) { return $this->_return_error('Error updating vendor purchases: ' . $vendor_purchase_calibrate_result->error); } // Recalibrate any payments tied to these purchases AFTER this transaction date. $vendor_payment_calibrate = new Beans_Vendor_Payment_Calibrate($this->_beans_data_auth((object) array('date_after' => $date, 'date_before' => $date))); $vendor_payment_calibrate_result = $vendor_payment_calibrate->execute(); if (!$vendor_payment_calibrate_result->success) { return $this->_return_error('Error updating vendor payments: ' . $vendor_payment_calibrate_result->error); } $this->_return_object->data->date_next = date("Y-m-d", strtotime($date . " +1 Day")); $account_transaction_search = new Beans_Account_Transaction_Search($this->_beans_data_auth((object) array('sort_by' => 'newest', 'page_size' => 1))); $account_transaction_search_result = $account_transaction_search->execute(); if (!$account_transaction_search_result->success || !count($account_transaction_search_result->data->transactions)) { return $this->_return_error('Error getting ending transaction: ' . $account_transaction_search_result->error); } $date_end = $account_transaction_search_result->data->transactions[0]->date; // This gets run on the very last iteration of calibration. if (strtotime($date_end) < strtotime($this->_return_object->data->date_next)) { $this->_return_object->data->date_next = FALSE; $account_calibrate = new Beans_Account_Calibrate($this->_beans_data_auth()); $account_calibrate_result = $account_calibrate->execute(); if (!$account_calibrate_result->success) { return $this->_return_error('Error calibrating individual account balances: ' . $account_calibrate_result->error); } $customer_sale_calibrate_check = new Beans_Customer_Sale_Calibrate_Check($this->_beans_data_auth()); $customer_sale_calibrate_check_result = $customer_sale_calibrate_check->execute(); if (!$customer_sale_calibrate_check_result->success) { return $this->_return_error('Error calibrating customer sales: ' . $customer_sale_calibrate_check_result->error); } if (count($customer_sale_calibrate_check_result->data->ids)) { $customer_sale_calibrate = new Beans_Customer_Sale_Calibrate($this->_beans_data_auth((object) array('ids' => $customer_sale_calibrate_check_result->data->ids))); $customer_sale_calibrate_result = $customer_sale_calibrate->execute(); if (!$customer_sale_calibrate_result->success) { return $this->_return_error('Error calibrating customer sales: ' . $customer_sale_calibrate_result->error); } } $vendor_purchase_calibrate_check = new Beans_Vendor_Purchase_Calibrate_Check($this->_beans_data_auth()); $vendor_purchase_calibrate_check_result = $vendor_purchase_calibrate_check->execute(); if (!$vendor_purchase_calibrate_check_result->success) { return $this->_return_error('Error calibrating vendor purchases: ' . $vendor_purchase_calibrate_check_result->error); } if (count($vendor_purchase_calibrate_check_result->data->ids)) { $vendor_purchase_calibrate = new Beans_Vendor_Purchase_Calibrate($this->_beans_data_auth((object) array('ids' => $vendor_purchase_calibrate_check_result->data->ids))); $vendor_purchase_calibrate_result = $vendor_purchase_calibrate->execute(); if (!$vendor_purchase_calibrate_result->success) { return $this->_return_error('Error calibrating vendor purchases: ' . $vendor_purchase_calibrate_result->error); } } } // Update our latest date in case user pauses and comes back later. // We only do this if the user isn't manually recalibrating books. if ($manual !== "1") { $setup_company_update = new Beans_Setup_Company_Update($this->_beans_data_auth((object) array('settings' => array('calibrate_date_next' => $this->_return_object->data->date_next)))); $setup_company_update_result = $setup_company_update->execute(); } }
private function _add_sub_accounts($beans_auth_array, $account_types, $parent_account_id, $accounts) { foreach ($accounts as $account) { if (!isset($account['name'])) { die("Fatal error encountered: corrupt internal data and could not create accounts."); } $beans_account_create = new Beans_Account_Create((object) array_merge($beans_auth_array, array('parent_account_id' => $parent_account_id, 'account_type_id' => $account_types[$account['type']], 'reserved' => isset($account['reserved']) ? $account['reserved'] : FALSE, 'name' => $account['name'], 'code' => substr(strtolower(str_replace(' ', '', $account['name'])), 0, 16), 'terms' => isset($account['terms']) ? $account['terms'] : NULL, 'writeoff' => isset($account['writeoff']) and $account['writeoff'] ? TRUE : FALSE))); $beans_account_create_result = $beans_account_create->execute(); if (!$beans_account_create_result->success) { if (Kohana::$is_cli) { echo "Error occurred creating account " . $account['name'] . " (" . $account['type'] . ") : " . $beans_account_create_result->error . "\n"; } } else { if (Kohana::$is_cli) { echo "Created account: " . $beans_account_create_result->data->account->name . "\n"; } if (isset($account['default_setting_account'])) { $settings = new stdClass(); if (is_array($account['default_setting_account'])) { foreach ($account['default_setting_account'] as $setting) { $settings->{$setting} = $beans_account_create_result->data->account->id; } } else { $settings->{$account['default_setting_account']} = $beans_account_create_result->data->account->id; } // Add as setting. $beans_company_update = new Beans_Setup_Company_Update((object) array_merge($beans_auth_array, array('settings' => $settings))); $beans_company_update_result = $beans_company_update->execute(); } if (isset($account['accounts']) and count($account['accounts'])) { $this->_add_sub_accounts($beans_auth_array, $account_types, $beans_account_create_result->data->account->id, $account['accounts']); } } } }
function action_settings() { // Handle $_POST $data = new stdClass(); $data->settings = new stdClass(); if (count($this->request->post())) { // Update every account - THREE LAWS. $accounts_search = new Beans_Account_Search($this->_beans_data_auth()); $accounts_search_result = $accounts_search->execute(); if ($this->_beans_result_check($accounts_search_result)) { foreach ($accounts_search_result->data->accounts as $account) { if ($account->parent_account_id and !$account->reserved) { $account_update_result = FALSE; if (in_array($account->id, $this->request->post('writeoff_account_ids'))) { $account_update = new Beans_Account_Update($this->_beans_data_auth((object) array('id' => $account->id, 'writeoff' => TRUE))); $account_update_result = $account_update->execute(); } else { $account_update = new Beans_Account_Update($this->_beans_data_auth((object) array('id' => $account->id, 'writeoff' => FALSE))); $account_update_result = $account_update->execute(); } if (!$account_update_result) { $this->_view->send_error_message('An unexpected error occurred.'); } // We can ignore the return value - this will post the necessary error. $this->_beans_result_check($account_update_result); } } $data->settings->company_name = $this->request->post('company_name'); $data->settings->company_email = $this->request->post('company_email'); $data->settings->company_phone = $this->request->post('company_phone'); $data->settings->company_fax = $this->request->post('company_fax'); $data->settings->company_address_address1 = $this->request->post('company_address_address1'); $data->settings->company_address_address2 = $this->request->post('company_address_address2'); $data->settings->company_address_city = $this->request->post('company_address_city'); $data->settings->company_address_state = $this->request->post('company_address_state'); $data->settings->company_address_zip = $this->request->post('company_address_zip'); $data->settings->company_address_country = $this->request->post('company_address_country'); $data->settings->company_fye = $this->request->post('company_fye'); $data->settings->company_currency = $this->request->post('company_currency'); $data->settings->account_default_deposit = $this->request->post('account_default_deposit'); $data->settings->account_default_receivable = $this->request->post('account_default_receivable'); $data->settings->account_default_income = $this->request->post('account_default_income'); $data->settings->account_default_returns = $this->request->post('account_default_returns'); $data->settings->account_default_expense = $this->request->post('account_default_expense'); $data->settings->account_default_order = $this->request->post('account_default_order'); $data->settings->account_default_costofgoods = $this->request->post('account_default_costofgoods'); $data->settings->account_default_payable = $this->request->post('account_default_payable'); if (isset($_FILES['logo']) and $_FILES['logo']['error'] == UPLOAD_ERR_OK) { // Do some magic. $type = strtolower(substr($_FILES['logo']['type'], 1 + strpos($_FILES['logo']['type'], '/'))); if (substr($_FILES['logo']['type'], 0, strpos($_FILES['logo']['type'], '/')) == "image" and ($type == "gif" or $type == "png" or $type == "jpeg" or $type == "jpg")) { $image = FALSE; if ($type == "jpeg" or $type == "jpg") { $image = imagecreatefromjpeg($_FILES['logo']['tmp_name']); } else { if ($type == "gif") { $image = imagecreatefromgif($_FILES['logo']['tmp_name']); } else { if ($type == "png") { $image = imagecreatefrompng($_FILES['logo']['tmp_name']); imagealphablending($image, TRUE); imagesavealpha($image, TRUE); } } } if (!$image) { $this->_view->send_error_message("An error occurred when reading your image. Must be JPG/JPEG, PNG, or GIF."); } else { $width = imagesx($image); $height = imagesy($image); if ($width > 150 or $height > 50) { // Resize $new_width = 150; $new_height = 50; if ($width / $height > $new_width / $new_height) { $new_height = $new_width * ($height / $width); } else { $new_width = $new_height * ($width / $height); } $new_image = imagecreatetruecolor($new_width, $new_height); if (strtolower($type) == "png") { imagealphablending($new_image, false); imagesavealpha($new_image, true); $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); imagefilledrectangle($new_image, 0, 0, $new_width, $new_height, $transparent); } imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $image = $new_image; } // Fancy trick to avoid writing to the filesystem. $logo_filename = "logo."; ob_start(); if ($type == "jpeg" or $type == "jpg") { $logo_filename .= "jpg"; imagejpeg($image, NULL, 95); } else { if ($type == "gif") { $logo_filename .= "gif"; imagegif($image); } else { if ($type == "png") { $logo_filename .= "png"; imagepng($image, NULL, 9); } } } $image_string = ob_get_contents(); ob_end_clean(); // Ensure "image/jpeg" if it's JPEG. $type = $type == "jpg" ? "jpeg" : $type; $data->settings->company_logo_data = base64_encode($image_string); $data->settings->company_logo_type = 'image/' . $type; // Not really necessary. $data->settings->company_logo_filename = $logo_filename; } } else { $this->_view->send_error_message("That logo was not an acceptable image format. Must be JPG/JPEG, PNG, or GIF."); } } else { if (isset($_FILES['logo']) and $_FILES['logo']['error'] != UPLOAD_ERR_NO_FILE) { $this->_view->send_error_message("An error occurred when receiving that logo. Please try again with a smaller file." . $_FILES['logo']['error']); } } } } $beans_company_update = new Beans_Setup_Company_Update($this->_beans_data_auth($data)); $beans_company_update_result = $beans_company_update->execute(); if ($this->_beans_result_check($beans_company_update_result)) { $this->_view->beans_company_update_result = $beans_company_update_result; } }