package com.finiac.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.finiac.model.Sales;
public class SalesDAOImpl implements SalesDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public void newSalesEntry(Sales sales) {
hibernateTemplate.saveOrUpdate(sales);
}
@SuppressWarnings("unchecked")
@Override
public List<Sales> getSalesEntries(long billNo) {
return hibernateTemplate.find("from Sales where isActive = 1 and billId="+ billNo);
}
@SuppressWarnings("unchecked")
@Override
public List<Sales> selectById(int id) {
return hibernateTemplate.find("from Sales where id="+id + " and isActive = 1");
}
@Override
public void updateSalesEntry(Sales sales) {
hibernateTemplate.update(sales);
}
@SuppressWarnings("unchecked")
@Override
public List<Sales> selectByBillId(long billId) {
List<Sales> salesList = hibernateTemplate.find("from Sales where bill_id="+billId + " and isActive = 1");
return salesList;
}
@SuppressWarnings("unchecked")
@Override
public List<Sales> selectByDate(String date) {
Sales sales = new Sales();
Sales salesFinal = new Sales();
int addFlag=0;
ArrayList<Sales> finalSalesList = new ArrayList<Sales>();
List<Sales> salesList =hibernateTemplate.find("from Sales where isActive=1");
for(int i=0;i<salesList.size();i++)
{
sales = salesList.get(i);
if(sales.getBillId().getDate().equals(date))
{
addFlag=0;
for(int j=0;j<finalSalesList.size();j++)
{
salesFinal = finalSalesList.get(j);
if(salesFinal.getProductId().getId()== sales.getProductId().getId())
{
addFlag=1;
salesFinal.setQtySold(sales.getQtySold()+salesFinal.getQtySold());
salesFinal.setTotal(sales.getTotal()+salesFinal.getTotal());
}
}
if(addFlag==0)
finalSalesList.add(sales);
}
}
return finalSalesList;
}
@SuppressWarnings("unchecked")
@Override
public List<Sales> selectByMonth(String month, String year) {
Sales sales = new Sales();
String year1,month1;
Sales salesFinal = new Sales();
int addFlag=0;
ArrayList<Sales> finalSalesList = new ArrayList<Sales>();
List<Sales> salesList =hibernateTemplate.find("from Sales where isActive=1");
for(int i=0;i<salesList.size();i++)
{
sales = salesList.get(i);
month1= sales.getBillId().getDate().substring(0, 2);
year1=sales.getBillId().getDate().substring(6, 10);
if(month1.equals(month) && year1.equals(year1))
{
addFlag=0;
for(int j=0;j<finalSalesList.size();j++)
{
salesFinal = finalSalesList.get(j);
if(salesFinal.getProductId().getId()== sales.getProductId().getId())
{
addFlag=1;
salesFinal.setQtySold(sales.getQtySold()+salesFinal.getQtySold());
salesFinal.setTotal(sales.getTotal()+salesFinal.getTotal());
}
}
if(addFlag==0)
finalSalesList.add(sales);
}
}
return finalSalesList;
}
}