BillingListDocs.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. class Windykacja_BillingListDocs {
  3. var $_docs;
  4. var $_saldo;
  5. public function __construct() {
  6. $this->_docs = array();
  7. $this->_saldo = 0;
  8. }
  9. public function add_bill_doc($data, $type, $h) {
  10. $bill_doc = new Windykacja_BillingDoc($type, $h);
  11. if ('FVAT' == $type && '0000-00-00' == $data) {
  12. $data = $h['BILL_DATE'];
  13. }
  14. $this->_docs[$data][] = $bill_doc;
  15. if ($data <= date("Y-m-d")) {
  16. $this->_saldo += $bill_doc->get_saldo();
  17. }
  18. }
  19. public function add_event($data, $type, $h) {
  20. $event = new Windykacja_EventDoc($type, $h);
  21. $this->_docs[$data][] = $event;
  22. }
  23. public function get_saldo() {
  24. if (round($this->_saldo, 2) === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
  25. $this->_saldo = 0.0;
  26. }
  27. return round($this->_saldo, 2);
  28. }
  29. public function get_saldo_30_dni() {
  30. // `PAY_SALDO_30_DNI` bez fv z terminem płatności < 30 dni
  31. $saldo_30_dni = 0;
  32. $dateMinus30dni = date('Y-m-d', mktime(0,0,0, date('n'), date('j') - 30, date('Y')));
  33. foreach ($this->_docs as $kData => $vDocs) {
  34. foreach ($vDocs as $vDoc) {
  35. if ($vDoc instanceof Windykacja_BillingDoc) {
  36. if ('FVAT' == $vDoc->get_type()) {// TODO: if FVAT then use only date PAYMENT_TERM >= 30 dni
  37. if ($vDoc->get('PAYMENT_TERM') > $dateMinus30dni) {
  38. //echo'<pre>POMIŃ! $vDoc pay_term('.$vDoc->get('PAYMENT_TERM').') ';print_r($vDoc);echo'</pre>';
  39. } else {
  40. //echo'<pre>$vDoc pay_term('.$vDoc->get('PAYMENT_TERM').'/'.($vDoc->get('PAYMENT_TERM') > $dateMinus30dni).') ';print_r($vDoc);echo'</pre>';
  41. $saldo_30_dni += $vDoc->get_saldo();
  42. }
  43. } else {
  44. $saldo_30_dni += $vDoc->get_saldo();
  45. }
  46. }
  47. }
  48. }
  49. $saldo_30_dni = round($saldo_30_dni, 2);
  50. if ($saldo_30_dni === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
  51. $saldo_30_dni = 0.0;
  52. }
  53. return $saldo_30_dni;
  54. }
  55. public function get_saldo_issued() {
  56. // `PAY_SALDO_ISSUED` fv wg daty wystawienia
  57. $saldo_issued = 0;
  58. foreach ($this->_docs as $kData => $vDocs) {
  59. foreach ($vDocs as $vDoc) {
  60. if ($vDoc instanceof Windykacja_BillingDoc) {
  61. if (1) {// TODO: if FVAT then use SELL_DATE instead of PAYMENT_TERM
  62. $saldo_issued += $vDoc->get_saldo();
  63. } else {
  64. $saldo_issued += $vDoc->get_saldo();
  65. }
  66. }
  67. }
  68. }
  69. $saldo_issued = round($saldo_issued, 2);
  70. if ($saldo_issued === 0.0) {// float point bug round('-6.821210263297E-13', 2) = -0
  71. $saldo_issued = 0.0;
  72. }
  73. return $saldo_issued;
  74. }
  75. public function get_docs() {
  76. return $this->_docs;
  77. }
  78. public function sort_docs() {
  79. ksort($this->_docs);
  80. }
  81. public function get_unpaid_fvat() {
  82. $today_date = date("Y-m-d");
  83. $fvat_arr = array();
  84. $saldo_curr = $this->get_saldo();
  85. if ($saldo_curr >= 0) {
  86. return $fvat_arr;
  87. }
  88. $break = false;
  89. $saldo_dates_keys = array_keys($this->_docs);
  90. $saldo_dates_keys = array_reverse($saldo_dates_keys);
  91. foreach ($saldo_dates_keys as $k_data) {
  92. if ($k_data > $today_date) continue;
  93. $v_saldo_arr = $this->_docs[$k_data];
  94. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  95. if ($v_doc->get_type() == 'FVAT') {
  96. $h = $v_doc->get_data();
  97. $h['WINIEN_POZOSTALO'] = round($h['WINIEN'], 2);
  98. $saldo_curr += round($h['WINIEN'], 2);
  99. if ($saldo_curr >= 0) {
  100. $h['WINIEN_POZOSTALO'] -= $saldo_curr;
  101. $break = true;
  102. }
  103. $fvat_arr[] = $h;
  104. }
  105. if ($break) break;
  106. }
  107. if ($break) break;
  108. }
  109. return $fvat_arr;
  110. }
  111. public function getLastIncomeDocsForSaldo($saldoLimit) {
  112. $today_date = date("Y-m-d");
  113. $incomeDocs = array();
  114. $break = false;
  115. $saldo_dates_keys = array_keys($this->_docs);
  116. $saldo_dates_keys = array_reverse($saldo_dates_keys);
  117. $saldo_curr = 0;
  118. foreach ($saldo_dates_keys as $k_data) {
  119. if ($k_data > $today_date) continue;
  120. $v_saldo_arr = $this->_docs[$k_data];
  121. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  122. if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
  123. $h = $v_doc->get_data();
  124. $h['MA_POZOSTALO'] = round($h['MA'], 2);
  125. $saldo_curr += round($h['MA'], 2);
  126. if ($saldo_curr >= $saldoLimit) {
  127. $h['MA_POZOSTALO'] -= $saldo_curr - $saldoLimit;
  128. $break = true;
  129. }
  130. $incomeDocs[] = $h;
  131. }
  132. if ($break) break;
  133. }
  134. if ($break) break;
  135. }
  136. return $incomeDocs;
  137. }
  138. public function get_last_pay_doc() {
  139. $last_pay_doc = null;
  140. $today_date = date("Y-m-d");
  141. $break = false;
  142. foreach ($this->_docs as $k_data => $v_saldo_arr) {
  143. if ($k_data > $today_date) continue;
  144. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  145. if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
  146. $last_pay_doc = $v_doc;
  147. break;
  148. }
  149. }
  150. if ($break) break;
  151. }
  152. return $last_pay_doc;
  153. }
  154. public function get_last_fvat_doc() {
  155. $last_fvat_doc = null;
  156. $today_date = date("Y-m-d");
  157. $break = false;
  158. foreach ($this->_docs as $k_data => $v_saldo_arr) {
  159. if ($k_data > $today_date) continue;
  160. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  161. if (in_array($v_doc->get_type(), array('FVAT'))) {
  162. $last_fvat_doc = $v_doc;
  163. break;
  164. }
  165. }
  166. if ($break) break;
  167. }
  168. return $last_fvat_doc;
  169. }
  170. }
  171. class Windykacja_TimelineDoc {
  172. var $_type;
  173. var $_data;
  174. var $_class;
  175. public function get_type() { return $this->_type; }
  176. public function get_data() { return $this->_data; }
  177. public function get_class() { return $this->_class; }
  178. public function get($key) {
  179. return $this->_data[$key];
  180. }
  181. }
  182. class Windykacja_EventDoc extends Windykacja_TimelineDoc {
  183. public function __construct($type, &$data) {
  184. $this->_data = $data;
  185. $this->_class = 'EVENT';
  186. $this->_type = $type;
  187. }
  188. }
  189. class Windykacja_BillingDoc extends Windykacja_TimelineDoc {
  190. public function __construct($type, &$data) {
  191. $this->_data = $data;
  192. $this->_class = 'BILLING';
  193. $this->_convert_type($type);
  194. $this->_count_saldo();
  195. }
  196. public function get_saldo() {
  197. return $this->_saldo;
  198. }
  199. public function get($key) {
  200. return $this->_data[$key];
  201. }
  202. public function _convert_type($type) {
  203. $this->_type = $type;
  204. switch ($this->_type) {
  205. case 'FVAT':
  206. $this->_data['nr'] = $this->_data['ID_BILLING_NUMBERS'];
  207. $this->_data['WINIEN'] = $this->_data['WARTOSC'];
  208. $this->_data['MA'] = 0;
  209. break;
  210. case 'KP':
  211. case 'KW':
  212. case 'WB':
  213. case 'WB_MASS':
  214. case 'KORV':
  215. $this->_data['nr'] = $this->_data['NUMBER'];
  216. break;
  217. default:
  218. }
  219. }
  220. public function _count_saldo() {
  221. $this->_saldo = 0;
  222. switch ($this->_type) {
  223. case 'FVAT':
  224. $this->_saldo -= round($this->_data['WARTOSC'], 2);
  225. break;
  226. case 'KORV':
  227. $this->_saldo -= round($this->_data['MA'], 2);
  228. break;
  229. case 'KP':
  230. case 'KW':
  231. case 'WB':
  232. case 'WB_MASS':
  233. $this->_saldo += round($this->_data['MA'], 2);
  234. $this->_saldo -= round($this->_data['WINIEN'], 2);
  235. break;
  236. default:
  237. }
  238. }
  239. }