Преглед изворни кода

Fix zestawienie zadłużonych + nadpłaty dla vectra

Piotr Labudda пре 11 година
родитељ
комит
b64fbeaa18
1 измењених фајлова са 81 додато и 23 уклоњено
  1. 81 23
      SE/superedit-USERS2_WINDYKACJA_STATUS.php

+ 81 - 23
SE/superedit-USERS2_WINDYKACJA_STATUS.php

@@ -1932,18 +1932,32 @@ body{font-family:arial;}
 				, w.`NR_SPRAWY_KRD`
 				, w.`NR_SPRAWY_SAD`
 				, w.`NR_SPRAWY_KOMORNIK`
+				, w.`LAST_FVAT_PAY_TERM`
+		";
+		$sqlWhereAdd = "
+			and (w.`LAST_FVAT_PAY_TERM`>='2014-12-01'
+				or (	w.`LAST_FVAT_PAY_TERM`='0000-00-00'
+					and w.`LAST_FVAT_SELL_DATE`>='2014-12-01'
+				)
+			)
+			and c.`ID_BILLING_PREFIXES` in('0','1')
+			and c.`BILLING_OWNER` in('1')
 		";
 		$sql = "select {$sql_select}
 			from `USERS2_WINDYKACJA_STATUS` as w
 				join `COMPANIES` as c on(c.`ID`=w.`ID_BILLING_USERS`)
 			where c.`A_ADM_COMPANY`='19994_PODMIOT_Vectra_wlasciciel'
-				and w.`PAY_SALDO`>=0
-				and w.`LAST_PAY_DATE`>='2014-12-01'
+				{$sqlWhereAdd}
 		";
+		if (V::get('DBG_SQL', '', $_GET)) {
+			header('Content-Type: text/plain; charset=utf-8');
+			echo $sql;
+			exit;
+		}
 		$res = $db->query($sql);
 		if ($db->has_errors()) {
 			print_r($db->get_errors());
-			die();
+			die('die L.' . __LINE__);
 		}
 		while ($r = $db->fetch($res)) {
 			$usersList[$r->ID_BILLING_USERS] = $r;
@@ -1960,28 +1974,35 @@ body{font-family:arial;}
 			echo implode("\n", array_keys($usersList));
 			exit;
 		}
-		$limit = 10000;
-		$date_limit = date("Y-m-d");
-		$csvFileName = "Zestawienie." . date("Y-m-d");
+		$limit = V::get('DBG_LOOP_LIMIT', 100000, $_GET, 'int');
+		if(V::get('DBG_LOOP_LIMIT', 0, $_GET, 'int')){echo "Loimit: {$limit}\n";}
+		$date_limit = V::get('DATE_LIMIT', date("Y-m-d"), $_GET);
+		$date_limit_from = '2014-12-01';
+		$csvFileName = "Zestawienie-nadplaty-do-{$date_limit}";
 		header('Content-Type: text/csv; charset=utf-8');
 		header("Content-Disposition: attachment; filename={$csvFileName}.csv");
 		$showCsvHeader = true;
 		foreach ($usersList as $userId => $user) {
 			$billing_docs = WindykacjaStatsModel::get_bill_dosc_by_date($user, $date_limit);
 			$billing_docs->sort_docs();
-			$fvat_arr = $billing_docs->get_unpaid_fvat();
-			WindykacjaStatsModel::update_doc_number($fvat_arr);
+			$saldo = $billing_docs->get_saldo();
+			if ($saldo <= 0) {
+				continue;
+			}
+			if(V::get('DBG_LOOP', '', $_GET)){echo "{$userId}:{$saldo}\n";}
+			$incomeDocs = $billing_docs->getLastIncomeDocsForSaldo($saldo);
+			if(V::get('DBG_LOOP', '', $_GET)){print_r($incomeDocs);echo "\n";}
+			//WindykacjaStatsModel::update_doc_number($incomeDocs);
 
 			$out_tbl = array();
-			foreach ($fvat_arr as $v_fvat) {
+			foreach ($incomeDocs as $v_fvat) {
 				$out_tr = array();
 				$out_tr['ID'] = $v_fvat['ID'];
 				$out_tr['nr'] = $v_fvat['NUMBER'] . '/' . $v_fvat['ID_BILLING_PREFIXES'];
-				$out_tr['winien'] = number_format($v_fvat['WINIEN'], 2, ',', ' ');
+				$out_tr['wplata'] = number_format($v_fvat['MA'], 2, '.', '');
 				$out_tr['data'] = $v_fvat['BILL_DATE'];
-				$out_tr['termin'] = $v_fvat['PAYMENT_TERM'];
-				$out_tr['pozostalo'] = number_format($v_fvat['WINIEN_POZOSTALO'], 2, ',', ' ');
-				array_unshift($out_tbl, $out_tr);// add to the begining
+				$out_tr['pozostalo'] = number_format($v_fvat['MA_POZOSTALO'], 2, '.', '');
+				array_push($out_tbl, $out_tr);// add to the begining
 			}
 
 			{// csv
@@ -1989,9 +2010,8 @@ body{font-family:arial;}
 				$csvLine[] = '"id klienta"';
 				$csvLine[] = '"nr dokumentu"';
 				$csvLine[] = '"kwota brutto"';
-				$csvLine[] = '"data faktury"';
-				$csvLine[] = '"termin płatności"';
-				$csvLine[] = '"pozostało do zapłaty"';
+				$csvLine[] = '"data wpłaty"';
+				$csvLine[] = '"pozostało"';
 				if ($showCsvHeader) {
 					echo implode(';', $csvLine) . ";\n";
 					$showCsvHeader = false;
@@ -2000,14 +2020,13 @@ body{font-family:arial;}
 					$csvLine = array();
 					$csvLine[] = '"' . $userId . '"';
 					$csvLine[] = '"' . $tr['nr'] . '"';
-					$csvLine[] = '"' . $tr['winien'] . '"';
+					$csvLine[] = '"' . $tr['wplata'] . '"';
 					$csvLine[] = '"' . $tr['data'] . '"';
-					$csvLine[] = '"' . $tr['termin'] . '"';
 					$csvLine[] = '"' . $tr['pozostalo'] . '"';
 					echo implode(';', $csvLine) . ";\n";
 				}
 			}
-			//echo '<pre style="max-height:200px;overflow:auto">fvat_arr: ';print_r($fvat_arr);echo'</pre>';
+			//echo '<pre style="max-height:200px;overflow:auto">incomeDocs: ';print_r($incomeDocs);echo'</pre>';
 			//echo '<pre style="max-height:200px;overflow:auto">ilosc('.count($out_tbl).') ' . "client({$userId}): ";print_r($out_tbl);echo'</pre>';
 			if (--$limit < 0) {
 				echo '<br>BREAK';
@@ -2062,8 +2081,6 @@ body{font-family:arial;}
 				, w.`LAST_FVAT_PAY_TERM`
 		";
 		$sqlWhereAdd = "
-			and w.`PAY_SALDO`<0 -- zaleglosci >= 1 PLN
-			-- and w.`PAY_FVAT`>0 -- zaleglosci < 1 PLN
 			and (w.`LAST_FVAT_PAY_TERM`>='2014-12-01'
 				or (	w.`LAST_FVAT_PAY_TERM`='0000-00-00'
 					and w.`LAST_FVAT_SELL_DATE`>='2014-12-01'
@@ -2074,8 +2091,6 @@ body{font-family:arial;}
 		";
 		if (V::get('DBG_NUM', '', $_GET)) {// number of clients with fvat after 2014-12-01
 			$sqlWhereAdd = "
-				-- and w.`PAY_SALDO`<0 -- zaleglosci >= 1 PLN
-				-- and w.`PAY_FVAT`>0 -- zaleglosci < 1 PLN
 				and (w.`LAST_FVAT_PAY_TERM`>='2014-12-01'
 					or (	w.`LAST_FVAT_PAY_TERM`='0000-00-00'
 						and w.`LAST_FVAT_SELL_DATE`>='2014-12-01'
@@ -2139,6 +2154,17 @@ body{font-family:arial;}
 				}
 			}
 
+			$user->hasActiveDeal = 0;
+			$sql = "select sum(t.`DEALS_ACTIVE`) as activeDeals
+				from `temp_DEALS_STATUS` as t
+				where t.`DEALS_ACTIVE`=1
+					and t.`ID_BILLING_USERS`='{$userId}'
+			";
+			$res = $db->query($sql);
+			if ($db->num_rows($res) > 0) {
+				$user->hasActiveDeal = 1;
+			}
+
 			$out_tbl = array();
 			foreach ($fvat_arr as $v_fvat) {
 				if ($v_fvat['BILL_DATE'] < $date_limit_from || $v_fvat['BILL_DATE'] > $date_limit) {
@@ -2158,6 +2184,8 @@ body{font-family:arial;}
 			{// csv
 				$csvLine = array();
 				$csvLine[] = '"id klienta"';
+				//$csvLine[] = '"status klienta (9=blokada)"';
+				//$csvLine[] = '"aktywne uslugi"';
 				$csvLine[] = '"nr dokumentu"';
 				$csvLine[] = '"kwota brutto"';
 				$csvLine[] = '"data faktury"';
@@ -2170,6 +2198,8 @@ body{font-family:arial;}
 				foreach ($out_tbl as $tr) {
 					$csvLine = array();
 					$csvLine[] = '"' . $userId . '"';
+					//$csvLine[] = '"' . $user->STATUS . '"';
+					//$csvLine[] = '"' . $user->hasActiveDeal . '"';
 					$csvLine[] = '"' . $tr['nr'] . '"';
 					$csvLine[] = '"' . $tr['winien'] . '"';
 					$csvLine[] = '"' . $tr['data'] . '"';
@@ -2783,6 +2813,34 @@ class BillingDocs {
 		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");