| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- class Windykacja_BillingListDocs {
- var $_docs;
- var $_saldo;
- public function __construct() {
- $this->_docs = array();
- $this->_saldo = 0;
- }
- public function add_bill_doc($data, $type, $h) {
- $bill_doc = new Windykacja_BillingDoc($type, $h);
- if ('FVAT' == $type && '0000-00-00' == $data) {
- $data = $h['BILL_DATE'];
- }
- $this->_docs[$data][] = $bill_doc;
- if ($data <= date("Y-m-d")) {
- $this->_saldo += $bill_doc->get_saldo();
- }
- }
- public function add_event($data, $type, $h) {
- $event = new Windykacja_EventDoc($type, $h);
- $this->_docs[$data][] = $event;
- }
- public function get_saldo() {
- if (round($this->_saldo, 2) === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
- $this->_saldo = 0.0;
- }
- return round($this->_saldo, 2);
- }
- public function get_saldo_30_dni() {
- // `PAY_SALDO_30_DNI` bez fv z terminem płatności < 30 dni
- $saldo_30_dni = 0;
- $dateMinus30dni = date('Y-m-d', mktime(0,0,0, date('n'), date('j') - 30, date('Y')));
- foreach ($this->_docs as $kData => $vDocs) {
- foreach ($vDocs as $vDoc) {
- if ($vDoc instanceof Windykacja_BillingDoc) {
- if ('FVAT' == $vDoc->get_type()) {// TODO: if FVAT then use only date PAYMENT_TERM >= 30 dni
- if ($vDoc->get('PAYMENT_TERM') > $dateMinus30dni) {
- //echo'<pre>POMIŃ! $vDoc pay_term('.$vDoc->get('PAYMENT_TERM').') ';print_r($vDoc);echo'</pre>';
- } else {
- //echo'<pre>$vDoc pay_term('.$vDoc->get('PAYMENT_TERM').'/'.($vDoc->get('PAYMENT_TERM') > $dateMinus30dni).') ';print_r($vDoc);echo'</pre>';
- $saldo_30_dni += $vDoc->get_saldo();
- }
- } else {
- $saldo_30_dni += $vDoc->get_saldo();
- }
- }
- }
- }
- $saldo_30_dni = round($saldo_30_dni, 2);
- if ($saldo_30_dni === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
- $saldo_30_dni = 0.0;
- }
- return $saldo_30_dni;
- }
- public function get_saldo_issued() {
- // `PAY_SALDO_ISSUED` fv wg daty wystawienia
- $saldo_issued = 0;
- foreach ($this->_docs as $kData => $vDocs) {
- foreach ($vDocs as $vDoc) {
- if ($vDoc instanceof Windykacja_BillingDoc) {
- if (1) {// TODO: if FVAT then use SELL_DATE instead of PAYMENT_TERM
- $saldo_issued += $vDoc->get_saldo();
- } else {
- $saldo_issued += $vDoc->get_saldo();
- }
- }
- }
- }
- $saldo_issued = round($saldo_issued, 2);
- if ($saldo_issued === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
- $saldo_issued = 0.0;
- }
- return $saldo_issued;
- }
- public function get_docs() {
- return $this->_docs;
- }
- public function sort_docs() {
- ksort($this->_docs);
- }
- public function get_unpaid_fvat() {
- $today_date = date("Y-m-d");
- $fvat_arr = array();
- $saldo_curr = $this->get_saldo();
- if ($saldo_curr >= 0) {
- return $fvat_arr;
- }
- $break = false;
- $saldo_dates_keys = array_keys($this->_docs);
- $saldo_dates_keys = array_reverse($saldo_dates_keys);
- foreach ($saldo_dates_keys as $k_data) {
- if ($k_data > $today_date) continue;
- $v_saldo_arr = $this->_docs[$k_data];
- foreach ($v_saldo_arr as $k_ind => $v_doc) {
- if ($v_doc->get_type() == 'FVAT') {
- $h = $v_doc->get_data();
- $h['WINIEN_POZOSTALO'] = round($h['WINIEN'], 2);
- $saldo_curr += round($h['WINIEN'], 2);
- if ($saldo_curr >= 0) {
- $h['WINIEN_POZOSTALO'] -= $saldo_curr;
- $break = true;
- }
- $fvat_arr[] = $h;
- }
- if ($break) break;
- }
- if ($break) break;
- }
- return $fvat_arr;
- }
- public function getLastIncomeDocsForSaldo($saldoLimit) {
- $today_date = date("Y-m-d");
- $incomeDocs = array();
- $break = false;
- $saldo_dates_keys = array_keys($this->_docs);
- $saldo_dates_keys = array_reverse($saldo_dates_keys);
- $saldo_curr = 0;
- foreach ($saldo_dates_keys as $k_data) {
- if ($k_data > $today_date) continue;
- $v_saldo_arr = $this->_docs[$k_data];
- foreach ($v_saldo_arr as $k_ind => $v_doc) {
- if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
- $h = $v_doc->get_data();
- $h['MA_POZOSTALO'] = round($h['MA'], 2);
- $saldo_curr += round($h['MA'], 2);
- if ($saldo_curr >= $saldoLimit) {
- $h['MA_POZOSTALO'] -= $saldo_curr - $saldoLimit;
- $break = true;
- }
- $incomeDocs[] = $h;
- }
- if ($break) break;
- }
- if ($break) break;
- }
- return $incomeDocs;
- }
- public function get_last_pay_doc() {
- $last_pay_doc = null;
- $today_date = date("Y-m-d");
- $break = false;
- foreach ($this->_docs as $k_data => $v_saldo_arr) {
- if ($k_data > $today_date) continue;
- foreach ($v_saldo_arr as $k_ind => $v_doc) {
- if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
- $last_pay_doc = $v_doc;
- break;
- }
- }
- if ($break) break;
- }
- return $last_pay_doc;
- }
- public function get_last_fvat_doc() {
- $last_fvat_doc = null;
- $today_date = date("Y-m-d");
- $break = false;
- foreach ($this->_docs as $k_data => $v_saldo_arr) {
- if ($k_data > $today_date) continue;
- foreach ($v_saldo_arr as $k_ind => $v_doc) {
- if (in_array($v_doc->get_type(), array('FVAT'))) {
- $last_fvat_doc = $v_doc;
- break;
- }
- }
- if ($break) break;
- }
- return $last_fvat_doc;
- }
- }
- class Windykacja_TimelineDoc {
- var $_type;
- var $_data;
- var $_class;
- public function get_type() { return $this->_type; }
- public function get_data() { return $this->_data; }
- public function get_class() { return $this->_class; }
- public function get($key) {
- return $this->_data[$key];
- }
- }
- class Windykacja_EventDoc extends Windykacja_TimelineDoc {
- public function __construct($type, &$data) {
- $this->_data = $data;
- $this->_class = 'EVENT';
- $this->_type = $type;
- }
- }
- class Windykacja_BillingDoc extends Windykacja_TimelineDoc {
- public function __construct($type, &$data) {
- $this->_data = $data;
- $this->_class = 'BILLING';
- $this->_convert_type($type);
- $this->_count_saldo();
- }
- public function get_saldo() {
- return $this->_saldo;
- }
- public function get($key) {
- return $this->_data[$key];
- }
- public function _convert_type($type) {
- $this->_type = $type;
- switch ($this->_type) {
- case 'FVAT':
- $this->_data['nr'] = $this->_data['ID_BILLING_NUMBERS'];
- $this->_data['WINIEN'] = $this->_data['WARTOSC'];
- $this->_data['MA'] = 0;
- break;
- case 'KP':
- case 'KW':
- case 'WB':
- case 'WB_MASS':
- case 'KORV':
- $this->_data['nr'] = $this->_data['NUMBER'];
- break;
- default:
- }
- }
- public function _count_saldo() {
- $this->_saldo = 0;
- switch ($this->_type) {
- case 'FVAT':
- $this->_saldo -= round($this->_data['WARTOSC'], 2);
- break;
- case 'KORV':
- $this->_saldo -= round($this->_data['MA'], 2);
- break;
- case 'KP':
- case 'KW':
- case 'WB':
- case 'WB_MASS':
- $this->_saldo += round($this->_data['MA'], 2);
- $this->_saldo -= round($this->_data['WINIEN'], 2);
- break;
- default:
- }
- }
- }
|