Забыл поделиться
Вот что в итоге получилось:
Код:
import ru.bitel.common.Utils;
import bitel.billing.common.TimeUtils;
import org.apache.log4j.Logger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.math.BigDecimal;
import bitel.billing.server.bill.bean.TemplateBill;
import bitel.billing.server.bill.bean.TemplateBillManager;
import bitel.billing.server.bill.bean.PositionValue;
import bitel.billing.server.bill.bean.Position;
import bitel.billing.server.contract.bean.BalanceUtils;
import ru.bitel.common.XMLUtils;
import java.io.StringWriter;
import bitel.billing.server.bill.bean.TemplateBillManager;
import bitel.billing.server.bill.bean.InvoiceManager;
public void main( setup, con, conSlave )
{
int mid = 14; //mid модуля Бухгалтерии
Calendar month = new GregorianCalendar();
//корректировка времени
month.add( Calendar.MONTH, -1 );
month.set(Calendar.DATE,month.getActualMaximum(Calendar.DATE));
int MM = month.get(Calendar.MONTH);
int YY = month.get(Calendar.YEAR);
print("Date of bills: " + month.getTime());
String query = "select id from bill_invoice_data_14 where yy=? and mm=?";
PreparedStatement ps = con.prepareStatement( query );
ps.setInt(1, YY);
ps.setInt(2, MM);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
id_new = rs.getInt(1);
ModuleSetup moduleSetup = setup.getModuleSetup( mid );
TemplateBillManager man = new TemplateBillManager( moduleSetup, conSlave, mid, true ); // false если надо для счетов
InvoiceManager pbm = new InvoiceManager( setup, con, mid, moduleSetup );
String query_update = "UPDATE bill_invoice_data_"+mid+" SET xml=? WHERE id=?";
PreparedStatement ps_update = con.prepareStatement( query_update );
int index = 1;
Document doc_update = XMLUtils.parseDocument( new ByteArrayInputStream( pbm.getXMLData( id_new ) ) );
Element bill_update = (Element)doc_update.getDocumentElement().getElementsByTagName( "bill" ).item( 0 );
Float saldo = Float.parseFloat ( bill_update.getAttribute( "saldo_prev" ) ); //Берем сальдо, если положительно или равно нулю, то нужно ставить Платежный документ
if (saldo >= 0) {
String PRD = getRPD(id_new, saldo, con);
print (PRD);
if (PRD != null && !PRD.equals("")){
bill_update.setAttribute( "payDoc", PRD );
bill_update.setAttribute( "payDocDate", " " );
}
ByteArrayOutputStream bos_update = new ByteArrayOutputStream( 4096 );
XMLUtils.serialize( doc_update, bos_update, "windows-1251" );
ps_update.setBytes( index++, bos_update.toByteArray() );
ps_update.setInt(index++, id_new);
ps_update.executeUpdate();
ps_update.close();
}
}
}
private String getRPD( int id_new, Float saldo, con )
{
String query = "select CP.id, CP.summa, CP.comment, date_format(CP.dt, '%d.%m.%Y') from bill_invoice_data_14 BL, contract_payment CP where BL.id=? and BL.cid=CP.cid and CP.pt=7 order by CP.dt DESC";
PreparedStatement ps = con.prepareStatement( query );
ps.setInt(1, id_new);
ResultSet rs = ps.executeQuery();
String PRD = "";
Float summa = saldo;
while(rs.next() && (summa >= 0)){
summa = summa - rs.getFloat(2);
print( "ChetF: " + rs.getString(1) + "; Pay:" + rs.getString(2) + "; Saldo:" + saldo + "; Summa:" + summa + "Komm:" + rs.getString(3) );
PRD = PRD + rs.getString(3) + "; ";
}
ps.close();
return PRD;
}