package sn.unitech.stock.metier;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import sn.unitech.stock.dao.EventsDao;
import sn.unitech.stock.dao.iface.IEventsDao;
import sn.unitech.stock.entity.Client;
import sn.unitech.stock.entity.Commande;
import sn.unitech.stock.entity.DetailEvent;
import sn.unitech.stock.entity.Ecritures;
import sn.unitech.stock.entity.Events;
import sn.unitech.stock.entity.Fournisseur;
import sn.unitech.stock.entity.IUniversel;
import sn.unitech.stock.entity.Stock;
import sn.unitech.stock.entity.SysAdherEvent;
import sn.unitech.stock.entity.SysAdherEventId;
import sn.unitech.stock.entity.SysAdherent;
import sn.unitech.stock.entity.SysEvents;
import sn.unitech.stock.entity.SysPeriode;
import sn.unitech.stock.entity.SysPiece;
import sn.unitech.stock.entity.SysProduit;
import sn.unitech.stock.entity.SysRangmnt;
import sn.unitech.stock.entity.SysTypcli;
import sn.unitech.stock.entity.Taxes;
import sn.unitech.stock.metier.iface.IEventsM;
import sn.unitech.stock.modal.Resultat;
public class EventsM<E> implements IEventsM<Events>{
IEventsDao eventsDao=new EventsDao();
SysProduitM<E> sysProduitM=new SysProduitM<E>();
public EventsM() {
// TODO Auto-generated constructor stub
}
public int getIndex(List<IUniversel> lp, String etiquette){
int index=-1;
for(int i=0;i<lp.size();i++){
if(lp.get(i).getSysProduit().getEtiquette().equals(ToolsMetier.completeStr(etiquette,'0',7,true))){
index=i;
break;
}
}
return index;
}
public Map<String,String> getAllOperation(String jpql,Map param){
List <SysEvents>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,String> curMap=new LinkedHashMap<String,String>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),curList.get(i).getIdEvent());
}
return curMap;
}
public Map<String,Integer> getAllFournisseurs(String jpql,Map param){
List <Fournisseur>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getPrenom()+" "+curList.get(i).getNom(),curList.get(i).getIdFourn());
}
return curMap;
}
public Map<String,Integer> getAllClients(String jpql,Map param){
List <Client>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getPrenom()+" "+curList.get(i).getNom(),curList.get(i).getIdClient());
}
return curMap;
}
public Map<String,Integer> getTypeClients(String jpql,Map param){
List <SysTypcli>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),curList.get(i).getIdTypcli());
}
return curMap;
}
public Map<String,String> getAllPiece(String jpql,Map param){
List <SysPiece>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,String> curMap=new LinkedHashMap<String,String>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),curList.get(i).getIdPiece()+"");
}
return curMap;
}
public Map<String,String> getAllRangement(String jpql,Map param){
List <SysRangmnt>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,String> curMap=new LinkedHashMap<String,String>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),curList.get(i).getIdRangmnt()+"");
}
return curMap;
}
public Map<String,Integer> getAllCommandeFournisseur(String jpql,Map param){
List <Commande>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getIdCommand()+" "+curList.get(i).getSysDate(),curList.get(i).getIdCommand());
}
return curMap;
}
public Map<String,Integer> getAllPeriode(String jpql,Map param){
List <SysPeriode>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),curList.get(i).getIdPeriode());
}
return curMap;
}
public SysAdherEvent getSingleAdherEvent(SysAdherEventId id){
return (SysAdherEvent)this.eventsDao.find(SysAdherEvent.class, id,null);
}
public SysEvents getCurrentOperation(String id){
return (SysEvents)this.eventsDao.find(SysEvents.class, id,null);
}
public SysTypcli getCurrentTypeClient(int id){
return (SysTypcli) this.eventsDao.find(SysTypcli.class, id,null);
}
public Client getCurrentClient(int id){
return (Client) this.eventsDao.find(Client.class, id,null);
}
public Fournisseur getCurrentFournisseur(int id){
return (Fournisseur) this.eventsDao.find(Fournisseur.class, id,null);
}
@Override
public List<IUniversel> getDetailCommande(String jpql,Map param) {
return this.eventsDao.getResultList(jpql, null, param);
}
@Override
public List<IUniversel> getProduitsFromStock(String jpql, Map param) {
return this.eventsDao.getResultList(jpql, null, param);
}
@Override
public Map<String,String> findGroupProd(String jpql,Map param){
return this.sysProduitM.findAllGroupsProd(jpql, param);
}
@Override
public List<IUniversel> getProduitsFromDetailEvent(String jpql, Map param) {
return this.eventsDao.getResultList(jpql, null, param);
}
public List <IUniversel> newListIUniversel(List<IUniversel> initialIUniversel,List<IUniversel> selectedIUniversel){
for (int i=0 ;i < selectedIUniversel.size();i++){
int pos;
if((pos=this.positionInList(initialIUniversel,selectedIUniversel.get(i)))!=-1)
initialIUniversel.remove(pos);
}
return initialIUniversel;
}
public int positionInList(List<IUniversel> initialCarac,IUniversel iuniversel){
for (int i=0 ;i < initialCarac.size();i++)
if(initialCarac.get(i).getId().equals(iuniversel.getId()))return i ;
return -1;
}
public Double getMontant(List<Taxes> listTaxes,String natureTaxe){
Double somme=0d;
for(int i=0;(listTaxes!=null)&&(i<listTaxes.size());i++){
if(listTaxes.get(i).getNatureTaxe().equals(natureTaxe))
somme+=listTaxes.get(i).getValeur();
}
return somme;
}
public Double getMontantTotalProduits(List<IUniversel> listIcro){
Double total=0d;
for(int i=0;(listIcro!=null)&&(i<listIcro.size());i++){
total+=(listIcro.get(i).getQteContenant()*listIcro.get(i).getPrixUnitc())+
(listIcro.get(i).getQteDetail()*listIcro.get(i).getPrixUnitd());
}
return total;
}
public List<Taxes> getTaxesAndValues(List<IUniversel> listIcro,Events events){
return new SysAdherEvtTaxM(listIcro,events).getTaxesAndValues();
}
public List<Events> listEvents(String jpql,Map param){
return this.eventsDao.getResultList(jpql,null, param);
}
@Override
public Resultat saveEvents(List<DetailEvent> listDetailEvent,
List<Taxes> listTaxe) {
return eventsDao.saveEvents(listDetailEvent, listTaxe);
}
public int findIdStock(String jpql,Map param){
Object stock=this.eventsDao.getSingleResult(jpql, null, param);
if(stock==null)return -1;
else
return ((Stock)stock).getIdStock();
}
public List<DetailEvent> listDetailEvent(String jpql,Map param){
return this.eventsDao.getResultList(jpql,null, param);
}
public List<Ecritures> listEcritures(String jpql,Map param){
return this.eventsDao.getResultList(jpql,null, param);
}
public Map<String,Integer> getAllPeriodeAndCoefficiants(String jpql,Map param){
List <SysPeriode>curList= this.eventsDao.getResultList(jpql,null, param);
Map<String,Integer> curMap=new LinkedHashMap<String,Integer>();
for(int i=0;i<curList.size();i++){
curMap.put(curList.get(i).getLibelle(),Integer.parseInt(Math.round(curList.get(i).getRapport())+""));
}
return curMap;
}
public List<DetailEvent>getInventaire(String jpql,Map param){
return this.eventsDao.getResultList(jpql,null, param);
}
public static void main(String [] a){
Map param=new HashMap<Integer, Integer>();
param.put(0, "1");
param.put(1, "VT");
/*param.put(2, "GORA FALL");
param.put(3, "");
param.put(4, "");*/
List<IUniversel> l=new EventsDao().getResultList("from DetailEvent as de where (de.events.sysUsers.sysAdherent.idAdherent=? and de.events.sysEvents.idEvent=?) and de.isTotal='False'", null, param);
System.out.println("size "+l.size());
}
@Override
public boolean isOkQuantite(IUniversel iuniversel) {
Integer rapport=iuniversel.getSysProduit().getRapport();
int qteSurplus=0;
if(rapport>0) qteSurplus=iuniversel.getQteDetail()/rapport;
if(qteSurplus>0){
iuniversel.setQteContenant(iuniversel.getQteContenant()+qteSurplus);
iuniversel.setQteDetail(iuniversel.getQteDetail()% rapport);
}
if(iuniversel.getQteContenantCtrl()>iuniversel.getQteContenant())return true;
else if(iuniversel.getQteContenantCtrl()<iuniversel.getQteContenant())return false;
else{
if(iuniversel.getQteDetailCtrl()>=iuniversel.getQteDetail())return true;
else return false;
}
}
}