BillingListDocs.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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($date, $type, $item) {
  10. $bill_doc = new Windykacja_BillingDoc($date, $type, $item);
  11. if ('FVAT' == $type && '0000-00-00' == $date) {
  12. $date = $item['BILL_DATE'];
  13. }
  14. $this->_docs[$date][] = $bill_doc;
  15. if ($date <= date("Y-m-d")) {
  16. $this->_saldo += $bill_doc->get_saldo();
  17. }
  18. }
  19. public function add_event($date, $type, $item) {
  20. $event = new Windykacja_EventDoc($date, $type, $item);
  21. $this->_docs[$date][] = $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. function set_saldo_for_all_docs() {
  82. $saldo = 0;
  83. foreach ($this->_docs as $day => $listDayDocs) {
  84. foreach ($listDayDocs as $doc) {
  85. $saldo += $doc->get_saldo();
  86. $doc->set_saldo_overall($saldo);
  87. }
  88. }
  89. }
  90. public function get_unpaid_fvat() {
  91. $today_date = date("Y-m-d");
  92. $fvat_arr = array();
  93. $saldo_curr = $this->get_saldo();
  94. if ($saldo_curr >= 0) {
  95. return $fvat_arr;
  96. }
  97. $break = false;
  98. $saldo_dates_keys = array_keys($this->_docs);
  99. $saldo_dates_keys = array_reverse($saldo_dates_keys);
  100. foreach ($saldo_dates_keys as $k_data) {
  101. if ($k_data > $today_date) continue;
  102. $v_saldo_arr = $this->_docs[$k_data];
  103. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  104. if ($v_doc->get_type() == 'FVAT') {
  105. $h = $v_doc->get_data();
  106. $h['WINIEN_POZOSTALO'] = round($h['WINIEN'], 2);
  107. $saldo_curr += round($h['WINIEN'], 2);
  108. if ($saldo_curr >= 0) {
  109. $h['WINIEN_POZOSTALO'] -= $saldo_curr;
  110. $break = true;
  111. }
  112. $fvat_arr[] = $h;
  113. }
  114. if ($break) break;
  115. }
  116. if ($break) break;
  117. }
  118. return $fvat_arr;
  119. }
  120. public function getLastIncomeDocsForSaldo($saldoLimit) {
  121. $today_date = date("Y-m-d");
  122. $incomeDocs = array();
  123. $break = false;
  124. $saldo_dates_keys = array_keys($this->_docs);
  125. $saldo_dates_keys = array_reverse($saldo_dates_keys);
  126. $saldo_curr = 0;
  127. foreach ($saldo_dates_keys as $k_data) {
  128. if ($k_data > $today_date) continue;
  129. $v_saldo_arr = $this->_docs[$k_data];
  130. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  131. if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
  132. $h = $v_doc->get_data();
  133. $h['MA_POZOSTALO'] = round($h['MA'], 2);
  134. $saldo_curr += round($h['MA'], 2);
  135. if ($saldo_curr >= $saldoLimit) {
  136. $h['MA_POZOSTALO'] -= $saldo_curr - $saldoLimit;
  137. $break = true;
  138. }
  139. $incomeDocs[] = $h;
  140. }
  141. if ($break) break;
  142. }
  143. if ($break) break;
  144. }
  145. return $incomeDocs;
  146. }
  147. public function get_last_pay_doc() {
  148. $last_pay_doc = null;
  149. $today_date = date("Y-m-d");
  150. $break = false;
  151. foreach ($this->_docs as $k_data => $v_saldo_arr) {
  152. if ($k_data > $today_date) continue;
  153. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  154. if (in_array($v_doc->get_type(), array('WB_MASS','KP'))) {
  155. $last_pay_doc = $v_doc;
  156. break;
  157. }
  158. }
  159. if ($break) break;
  160. }
  161. return $last_pay_doc;
  162. }
  163. public function get_last_fvat_doc() {
  164. $last_fvat_doc = null;
  165. $today_date = date("Y-m-d");
  166. $break = false;
  167. foreach ($this->_docs as $k_data => $v_saldo_arr) {
  168. if ($k_data > $today_date) continue;
  169. foreach ($v_saldo_arr as $k_ind => $v_doc) {
  170. if (in_array($v_doc->get_type(), array('FVAT'))) {
  171. $last_fvat_doc = $v_doc;
  172. break;
  173. }
  174. }
  175. if ($break) break;
  176. }
  177. return $last_fvat_doc;
  178. }
  179. }
  180. class Windykacja_TimelineDoc {
  181. var $_type;
  182. var $_date;
  183. var $_data;
  184. var $_class;
  185. var $_saldo;
  186. var $_saldo_overall;
  187. public function get_type() { return $this->_type; }
  188. public function get_date() { return $this->_date; }
  189. public function get_data() { return $this->_data; }
  190. public function get_class() { return $this->_class; }
  191. public function get_saldo() { return $this->_saldo; }
  192. public function get($key) {
  193. return $this->_data[$key];
  194. }
  195. public function get_saldo_overall() { return $this->_saldo_overall; }
  196. public function set_saldo_overall($overallSaldo) {
  197. $this->_saldo_overall = round($overallSaldo, 2);
  198. }
  199. }
  200. class Windykacja_EventDoc extends Windykacja_TimelineDoc {
  201. public function __construct($date, $type, &$data) {
  202. $this->_date = $date;
  203. $this->_data = $data;
  204. $this->_class = 'EVENT';
  205. $this->_type = $type;
  206. $this->_saldo = 0;
  207. }
  208. }
  209. class Windykacja_BillingDoc extends Windykacja_TimelineDoc {
  210. public function __construct($date, $type, &$data) {
  211. $this->_date = $date;
  212. $this->_data = $data;
  213. $this->_class = 'BILLING';
  214. $this->_convert_type($type);
  215. $this->_count_saldo();
  216. }
  217. public function get($key) {
  218. return $this->_data[$key];
  219. }
  220. public function _convert_type($type) {
  221. $this->_type = $type;
  222. switch ($this->_type) {
  223. case 'FVAT':
  224. $this->_data['nr'] = $this->_data['ID_BILLING_NUMBERS'];
  225. $this->_data['WINIEN'] = $this->_data['WARTOSC'];
  226. $this->_data['MA'] = 0;
  227. break;
  228. case 'KP':
  229. case 'KW':
  230. case 'WB':
  231. case 'WB_MASS':
  232. case 'KORV':
  233. $this->_data['nr'] = $this->_data['NUMBER'];
  234. break;
  235. default:
  236. }
  237. }
  238. public function _count_saldo() {
  239. $this->_saldo = 0;
  240. switch ($this->_type) {
  241. case 'FVAT':
  242. $this->_saldo -= round($this->_data['WARTOSC'], 2);
  243. break;
  244. case 'KORV':
  245. $this->_saldo -= round($this->_data['MA'], 2);
  246. break;
  247. case 'KP':
  248. case 'KW':
  249. case 'WB':
  250. case 'WB_MASS':
  251. $this->_saldo += round($this->_data['MA'], 2);
  252. $this->_saldo -= round($this->_data['WINIEN'], 2);
  253. break;
  254. default:
  255. }
  256. }
  257. }