use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class RequestInvoice method invoiceNew.
// invoiceDone
/**
* New Invoice
* @param request request
*/
private void invoiceNew(MRequest request) {
m_invoice = new MInvoice(getCtx(), 0, get_TrxName());
m_invoice.setIsSOTrx(true);
MBPartner partner = new MBPartner(getCtx(), request.getC_BPartner_ID(), null);
m_invoice.setBPartner(partner);
m_invoice.saveEx();
m_linecount = 0;
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class TaxDeclarationCreate method doIt.
// prepare
/**
* Process
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
log.info("C_TaxDeclaration_ID=" + p_C_TaxDeclaration_ID);
m_td = new MTaxDeclaration(getCtx(), p_C_TaxDeclaration_ID, get_TrxName());
if (m_td.get_ID() == 0)
throw new AdempiereSystemError("@NotFound@ @C_TaxDeclaration_ID@ = " + p_C_TaxDeclaration_ID);
if (p_DeleteOld) {
// Delete old
String sql = "DELETE C_TaxDeclarationLine WHERE C_TaxDeclaration_ID=?";
int no = DB.executeUpdate(sql, p_C_TaxDeclaration_ID, false, get_TrxName());
if (no != 0)
log.config("Delete Line #" + no);
sql = "DELETE C_TaxDeclarationAcct WHERE C_TaxDeclaration_ID=?";
no = DB.executeUpdate(sql, p_C_TaxDeclaration_ID, false, get_TrxName());
if (no != 0)
log.config("Delete Acct #" + no);
}
// Get Invoices
String sql = "SELECT * FROM C_Invoice i " + "WHERE TRUNC(i.DateInvoiced, 'DD') >= ? AND TRUNC(i.DateInvoiced, 'DD') <= ? " + " AND Processed='Y'" + " AND NOT EXISTS (SELECT * FROM C_TaxDeclarationLine tdl " + "WHERE i.C_Invoice_ID=tdl.C_Invoice_ID)";
PreparedStatement pstmt = null;
ResultSet rs = null;
int noInvoices = 0;
try {
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setTimestamp(1, m_td.getDateFrom());
pstmt.setTimestamp(2, m_td.getDateTo());
rs = pstmt.executeQuery();
while (rs.next()) {
// no lock
create(new MInvoice(getCtx(), rs, null));
noInvoices++;
}
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return "@C_Invoice_ID@ #" + noInvoices + " (" + m_noLines + ", " + m_noAccts + ")";
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class CreateOrderBasedOnAnother method createAllocations.
/**
* Create Allocations for new order
* @param targetOrder
*/
private void createAllocations(MOrder targetOrder) {
List<MPayment> payments = MPayment.getOfOrder(targetOrder);
MInvoice[] invoices = targetOrder.getInvoices();
BigDecimal totalPay = BigDecimal.ZERO;
BigDecimal totalInvoiced = BigDecimal.ZERO;
for (MPayment payment : payments) totalPay = totalPay.add(payment.getPayAmt());
for (MInvoice invoice : invoices) {
totalInvoiced = totalInvoiced.add(invoice.getGrandTotal());
}
if (totalInvoiced.signum() != 0 && totalPay.signum() != 0 && totalInvoiced.compareTo(totalPay) == 0) {
MAllocationHdr allocation = new MAllocationHdr(getCtx(), true, today, targetOrder.getC_Currency_ID(), targetOrder.getDescription(), get_TrxName());
allocation.setDocStatus(org.compiere.process.DocAction.STATUS_Drafted);
allocation.setDocAction(org.compiere.process.DocAction.ACTION_Complete);
allocation.saveEx();
addLog(allocation.getDocumentInfo());
for (MInvoice invoice : invoices) {
MAllocationLine allocationLine = new MAllocationLine(allocation);
allocationLine.setDocInfo(targetOrder.getC_BPartner_ID(), targetOrder.getC_Order_ID(), invoice.getC_Invoice_ID());
allocationLine.setAmount(invoice.getGrandTotal());
allocationLine.saveEx();
}
for (MPayment payment : payments) {
MAllocationLine allocationLine = new MAllocationLine(allocation);
allocationLine.setPaymentInfo(payment.get_ID(), 0);
allocationLine.setAmount(payment.getPayAmt());
allocationLine.saveEx();
}
allocation.processIt(org.compiere.process.DocAction.ACTION_Complete);
allocation.saveEx();
}
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class ReverseTheSalesTransaction method cancelInvoices.
private void cancelInvoices() {
for (MInOut customerReturn : customerReturns) {
ProcessInfo processInfo = ProcessBuilder.create(getCtx()).process(// AD_Process_ID = 154
"M_InOut_CreateInvoice").withTitle("Generate Invoice from Receipt").withRecordId(MInOut.Table_ID, customerReturn.getM_InOut_ID()).withoutTransactionClose().execute(get_TrxName());
if (processInfo.isError()) {
String errorMessage = Msg.parseTranslation(getCtx(), processInfo.getTitle() + " @ProcessRunError@ " + processInfo.getSummary() + " " + processInfo.getLogInfo());
throw new AdempierePOSException(errorMessage);
}
addLog(processInfo.getLogInfo());
for (MInvoice creditNote : getCreditNotes(customerReturn.getM_RMA_ID())) {
if (creditNote != null && creditNote.getC_Invoice_ID() > 0) {
creditNote.setDateInvoiced(today);
creditNote.setDateAcct(today);
creditNote.processIt(DocAction.ACTION_Complete);
creditNote.saveEx();
addLog(creditNote.getDocumentInfo());
}
}
}
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class InvoiceServlet method streamInvoice.
// doPost
/**
* Stream invoice
* @param request request
* @param response response
* @return "" or error message
*/
private String streamInvoice(HttpServletRequest request, HttpServletResponse response) {
// if not created size is 1015
int MIN_SIZE = 2000;
// Get Invoice ID
int C_Invoice_ID = WebUtil.getParameterAsInt(request, "Invoice_ID");
if (C_Invoice_ID == 0) {
log.fine("No ID)");
return "No Invoice ID";
}
// Get Invoice
Properties ctx = JSPEnv.getCtx(request);
MInvoice invoice = new MInvoice(ctx, C_Invoice_ID, null);
if (invoice.getC_Invoice_ID() != C_Invoice_ID) {
log.fine("Invoice not found - ID=" + C_Invoice_ID);
return "Invoice not found";
}
// Get WebUser & Compare with invoice
HttpSession session = request.getSession(true);
WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
if (wu.getC_BPartner_ID() != invoice.getC_BPartner_ID()) {
log.warning("Invoice from BPartner - C_Invoice_ID=" + C_Invoice_ID + " - BP_Invoice=" + invoice.getC_BPartner_ID() + " = BP_Web=" + wu.getC_BPartner_ID());
return "Your invoice not found";
}
// Check Directory
String dirName = ctx.getProperty("documentDir", ".");
try {
File dir = new File(dirName);
if (!dir.exists())
dir.mkdir();
} catch (Exception ex) {
log.log(Level.SEVERE, "Could not create directory " + dirName, ex);
return "Streaming error - directory";
}
// Check if Invoice already created
String fileName = invoice.getPDFFileName(dirName);
File file = new File(fileName);
if (file.exists() && file.isFile() && file.length() > MIN_SIZE)
log.info("Existing: " + file + " - " + new Timestamp(file.lastModified()));
else {
log.info("New: " + fileName);
file = invoice.createPDF(file);
if (file != null) {
invoice.setDatePrinted(new Timestamp(System.currentTimeMillis()));
invoice.saveEx();
}
}
// Issue Error
if (file == null || !file.exists() || file.length() < MIN_SIZE) {
log.warning("File does not exist - " + file);
return "Streaming error - file";
}
// Send PDF
try {
// 2k Buffer
int bufferSize = 2048;
int fileLength = (int) file.length();
//
response.setContentType("application/pdf");
response.setBufferSize(bufferSize);
response.setContentLength(fileLength);
//
log.fine(file.getAbsolutePath() + ", length=" + fileLength);
// timer start
long time = System.currentTimeMillis();
//
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[bufferSize];
double totalSize = 0;
int count = 0;
do {
count = in.read(buffer, 0, bufferSize);
if (count > 0) {
totalSize += count;
out.write(buffer, 0, count);
}
} while (count != -1);
out.flush();
out.close();
//
in.close();
time = System.currentTimeMillis() - time;
double speed = (totalSize / 1024) / ((double) time / 1000);
log.fine("Length=" + totalSize + " - " + time + " ms - " + speed + " kB/sec");
} catch (IOException ex) {
log.log(Level.SEVERE, ex.toString());
return "Streaming error";
}
return null;
}
Aggregations