Esempio n. 1
0
 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();
     }
 }
Esempio n. 2
0
 public function action_calibrateall()
 {
     set_time_limit(60 * 10);
     $account_calibrate = new Beans_Account_Calibrate($this->_beans_data_auth());
     $account_calibrate_result = $account_calibrate->execute();
     if (!$account_calibrate_result->success) {
         Session::instance()->set('global_error_message', $account_calibrate_result->error . $account_calibrate_result->auth_error . $account_calibrate_result->config_error);
         $this->request->redirect('/accounts/');
     }
     Session::instance()->set('global_success_message', 'Calibration successful. Adjusted ' . count($account_calibrate_result->data->calibrated_account_ids) . ' Account' . (count($account_calibrate_result->data->calibrated_account_ids) != 1 ? 's' : '') . ' and ' . count($account_calibrate_result->data->calibrated_form_ids) . ' Form' . (count($account_calibrate_result->data->calibrated_form_ids) != 1 ? 's' : '') . '.');
     $this->request->redirect('/accounts/');
 }