protected function populate(int $price_item_id, &$price_item) { if ($price_item == null) { $price_item = new rental_invoice_price_item(0, $this->db->f('id', true), $this->db->f('invoice_id', true), $this->db->f('title', true), $this->db->f('agresso_id', true), $this->db->f('is_area', true), $this->db->f('price', true), $this->db->f('area', true), $this->db->f('count', true), strtotime($this->db->f('date_start', true)), strtotime($this->db->f('date_end', true))); $price_item->set_total_price($this->db->f('total_price', true)); } return $price_item; }
public function get_total_price_current_year() { /* 1. Get current year * 2. Check contract dates to see if the contract is/has: * 2.1 ...ended - return 0 * 2.2 ...active alle year - return total price * 2.3 ...ends or starts current year - calculate price * 2.3.1 */ $date_start = phpgw::get_var('date_start'); $date_end = phpgw::get_var('date_end'); if (isset($date_start)) { $aDate = explode("/", $date_start); $date_start = $aDate[1] . "/" . $aDate[0] . "/" . $aDate[2]; $timestamp_invoice_start = strtotime($date_start); } if (isset($date_end)) { $aDate = explode("/", $date_end); $date_end = $aDate[1] . "/" . $aDate[0] . "/" . $aDate[2]; $timestamp_invoice_end = strtotime($date_end); } if (!isset($timestamp_invoice_start) || $timestamp_invoice_start == "" || !isset($timestamp_invoice_end) || $timestamp_invoice_end == "") { $current_year = date("Y"); $timestamp_invoice_start = strtotime("{$current_year}-1-1"); $timestamp_invoice_end = strtotime("{$current_year}-12-31"); } $contract_dates = $this->get_contract_date(); if (isset($contract_dates)) { $contract_start = $contract_dates->get_start_date(); $contract_end = $contract_dates->get_end_date(); if (isset($contract_end) && $contract_end < $timestamp_invoice_start) { return 0; // The contract ends before start of current year } else { if ($contract_start > $timestamp_invoice_end) { return 0; // The contract starts after the end of current year } } } else { return 0; // The contract has no dates } // The contract is active only parts of the current year, we must calculate the total sum using the billing logic $total_sum = 0; // Holding the total price of the contract current year $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $this->get_id())); // Run through the contract price items foreach ($contract_price_items as $contract_price_item) { // ---- Period calculation --- // Determine start date for price item $contract_price_item_start = $contract_price_item->get_date_start(); if ($contract_price_item_start == null || $contract_price_item_start == '') { // We just use the invoice date for our calculations $contract_price_item_start = $timestamp_invoice_start; } // Determine end date for price item $contract_price_item_end = $contract_price_item->get_date_end(); if ($contract_price_item_end == null || $contract_price_item_end == '') { // We just use the invoice date for our calculations $contract_price_item_end = $timestamp_invoice_end; } // Sanity check - end date should never be before start date if ($contract_price_item_end < $contract_price_item_start) { continue; // We don't add this price item - continue to next } // Checking the start date against the invoice dates if ($contract_price_item_start < $timestamp_invoice_start) { $invoice_price_item_start = $timestamp_invoice_start; // We use the invoice start } else { if ($contract_price_item_start > $timestamp_invoice_end) { continue; // We don't add this price item - continue to next } else { $invoice_price_item_start = $contract_price_item_start; // We use the price item start } } // Checking the end date against invoice dates if ($contract_price_item_end < $timestamp_invoice_start) { continue; // We don't add this price item - continue to next } else { if ($contract_price_item_end < $timestamp_invoice_end) { $invoice_price_item_end = $contract_price_item_end; // We use the price item end } else { $invoice_price_item_end = $timestamp_invoice_end; // We use the invoice end } } // Checking the contract dates against the temporary price item dates if (isset($contract_start) && !$contract_price_item->is_one_time()) { if ($contract_start > $timestamp_invoice_end) { continue; //No price items for this contract will be billed } if ($contract_start > $invoice_price_item_start) { $invoice_price_item_start = $contract_start; } } if (isset($contract_end) && !$contract_price_item->is_one_time()) { if ($contract_end < $timestamp_invoice_start) { continue; //No price items for this contract will be billed } if ($contract_end < $invoice_price_item_end) { $invoice_price_item_end = $contract_end; } } // --- End of period calculation --- // Create a new invoice price item $invoice_price_item = new rental_invoice_price_item(2, -1, 0, $contract_price_item->get_title(), $contract_price_item->get_agresso_id(), $contract_price_item->is_area(), $contract_price_item->get_price(), $contract_price_item->get_area(), $contract_price_item->get_count(), $invoice_price_item_start, $invoice_price_item_end); $total_price_price_item = 0; // If the contract price item is of type one-time and it's dates are within the invoice period ... if ($contract_price_item->is_one_time()) { if ($contract_price_item_start >= $timestamp_invoice_start && $contract_price_item_start <= $timestamp_invoice_end) { // ... set the total price of the invoice price item to the total price of the contract price item $total_price_price_item = $contract_price_item->get_total_price(); } } else { $total_price_price_item = $invoice_price_item->get_total_price(); } $total_sum += round($total_price_price_item, 2); } // end of looping through the contract price items $total_sum = round($total_sum, 2); return $total_sum; }
/** * Create invoice * * @param int $decimals the number of decimals on the total sum of the onvoice * @param int $billing_id the billing this invoice is part of * @param int $contract_id the contract * @param bool $override flag to indicate if the invoice start period should be overridden with the billing start date of contract * @param int $timestamp_invoice_start the startdate of the invoice period * @param int $timestamp_invoice_end the enddate of the invoice period * @param bool $bill_only_one_time flag to indicate if the the invoice should only bil one time price elements * @return rental_invoice the newly created invoice */ public static function create_invoice(int $decimals, int $billing_id, int $contract_id, bool $override, int $timestamp_invoice_start, int $timestamp_invoice_end, $bill_only_one_time, $dry_run = false, $billing_term = 0) { $contract = rental_socontract::get_instance()->get_single($contract_id); // If the invoice period should be overriden with the biling start date if ($override) { $timestamp_invoice_start = $contract->get_billing_start_date(); } // If no account out is specified: check if the contract type defines any data to be used in this field (AGRESSO specific logic) $account_out = $contract->get_account_out(); if (!isset($account_out) || $account_out == '') { //If no account out - check the contract type for default $account_tmp = rental_socontract::get_instance()->get_contract_type_account($contract->get_contract_type_id()); if (isset($account_tmp) && $account_tmp != '') { $account_out = $account_tmp; } else { $account_out = rental_socontract::get_instance()->get_default_account($contract->get_location_id(), false); } } // Create invoice ... $invoice = new rental_invoice(-1, $billing_id, $contract_id, time(), $timestamp_invoice_start, $timestamp_invoice_end, 0, $contract->get_rented_area(), $contract->get_invoice_header(), $contract->get_account_in(), $account_out, $contract->get_service_id(), $contract->get_responsibility_id()); // ... and add party identifier, project number and the old contract identifier $invoice->set_party_id($contract->get_payer_id()); $invoice->set_project_id($contract->get_project_id()); $invoice->set_old_contract_id($contract->get_old_contract_id()); if (!$dry_run) { rental_soinvoice::get_instance()->store($invoice); // We must store the invoice at this point to have an id to give to the price item } // Retrieve the contract price items: only one-time or all if ($bill_only_one_time) { $filters2 = array('contract_id' => $contract->get_id(), 'contract_ids_one_time' => true, 'billing_term_id' => $billing_term, 'year' => date('Y', $timestamp_invoice_start), 'month' => date('m', $timestamp_invoice_start)); //$contract_price_items = $socontract_price_item->get($start_index, $num_of_objects, $sort_field, $sort_ascending, $search_for, $search_type, $filters2); $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, $filters2); //$contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $contract->get_id(), 'one_time' => true)); } else { $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $contract->get_id())); } $total_sum = 0; // Holding the total price of the invoice $contract_dates = $contract->get_contract_date(); if (isset($contract_dates)) { $contract_start = $contract->get_contract_date()->get_start_date(); $contract_end = $contract->get_contract_date()->get_end_date(); } // Run through the contract price items foreach ($contract_price_items as $contract_price_item) { // ---- Period calculation --- // Determine start date for price item $contract_price_item_start = $contract_price_item->get_date_start(); if ($contract_price_item_start == null || $contract_price_item_start == '') { // We just use the invoice date for our calculations $contract_price_item_start = $timestamp_invoice_start; } // Determine end date for price item $contract_price_item_end = $contract_price_item->get_date_end(); if ($contract_price_item_end == null || $contract_price_item_end == '') { // We just use the invoice date for our calculations $contract_price_item_end = $timestamp_invoice_end; } // Sanity check - end date should never be before start date if ($contract_price_item_end < $contract_price_item_start) { continue; // We don't add this price item - continue to next } // Checking the start date against the invoice dates if ($contract_price_item_start < $timestamp_invoice_start) { $invoice_price_item_start = $timestamp_invoice_start; // We use the invoice start } else { if ($contract_price_item_start > $timestamp_invoice_end) { continue; // We don't add this price item - continue to next } else { $invoice_price_item_start = $contract_price_item_start; // We use the price item start } } // Checking the end date against invoice dates if ($contract_price_item_end < $timestamp_invoice_start) { continue; // We don't add this price item - continue to next } else { if ($contract_price_item_end < $timestamp_invoice_end) { $invoice_price_item_end = $contract_price_item_end; // We use the price item end } else { $invoice_price_item_end = $timestamp_invoice_end; // We use the invoice end } } // Checking the contract dates against the temporary price item dates if (isset($contract_start) && !$contract_price_item->is_one_time()) { if ($contract_start > $timestamp_invoice_end) { continue; //No price items for this contract will be billed } if ($contract_start > $invoice_price_item_start) { $invoice_price_item_start = $contract_start; } } if (isset($contract_end) && !$contract_price_item->is_one_time()) { if ($contract_end < $timestamp_invoice_start) { continue; //No price items for this contract will be billed } if ($contract_end < $invoice_price_item_end) { $invoice_price_item_end = $contract_end; } } // --- End of period calculation --- // Create a new invoice price item $invoice_price_item = new rental_invoice_price_item($decimals, -1, $invoice->get_id(), $contract_price_item->get_title(), $contract_price_item->get_agresso_id(), $contract_price_item->is_area(), $contract_price_item->get_price(), $contract_price_item->get_area(), $contract_price_item->get_count(), $invoice_price_item_start, $invoice_price_item_end); // If the contract price item is of type one-time and it's dates are within the invoice period ... if ($contract_price_item->is_one_time()) { if ($contract_price_item_start >= $timestamp_invoice_start && $contract_price_item_start <= $timestamp_invoice_end) { // ... set the total price of the invoice price item to the total price of the contract price item $invoice_price_item->set_total_price($contract_price_item->get_total_price()); // ... and set the contract price item as billed $contract_price_item->set_is_billed(true); if (!$dry_run) { rental_socontract_price_item::get_instance()->store($contract_price_item); } } } if (!$dry_run) { // Store the invoice price item rental_soinvoice_price_item::get_instance()->store($invoice_price_item); } // Add the price item to the invoice $invoice->add_invoice_price_item($invoice_price_item); // Add this price item's total sum to the tota sum of the invoice $total_sum += $invoice_price_item->get_total_price(); } // end of looping through the contract price items // Set the total sum of the invoice rounded to the specified number of decimals $invoice->set_total_sum(round($total_sum, $decimals)); if (!$dry_run) { // ... and store the invoice rental_soinvoice::get_instance()->store($invoice); } return $invoice; }