// ticket.change
// This script Sets an item line using the Promotions module
//
//    uniCenta oPOS - Touch Friendly Point Of Sale
//    Copyright (c) 2009-2015 uniCenta
//    http://sourceforge.net/projects/unicentaopos
//
//    This file is part of uniCenta oPOS.
//
//    uniCenta oPOS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    uniCenta oPOS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with uniCenta oPOS.  If not, see <http://www.gnu.org/licenses/>.
// **************************************************************************

import com.openbravo.format.Formats;
import com.openbravo.pos.forms.DataLogicSales; 
import com.openbravo.data.loader.Session;
import com.openbravo.pos.ticket.TicketLineInfo;
import com.openbravo.pos.ticket.TicketProductInfo;
import com.openbravo.pos.promotion.PromoInfo;
import java.util.List;
import java.util.ArrayList;

Session session = new Session("jdbc:mysql://localhost:3306/unicentaopos", "root", "pass123"); 
DataLogicSales logic = new DataLogicSales();
logic.init(session);


javax.swing.JOptionPane.showMessageDialog(null, "Value :"+ticket.getLinesCount(), "Product", JOptionPane.WARNING_MESSAGE);

index = ticket.getLinesCount(); 
if(index == null){
 index = 0;
}
initialcount = index;
indexinit = sales.getSelectedIndex();
_promos = logic.getPromos();


// if at least one promotion is detected + lines in the ticket, then we will build the : 
// --> ticket composition
// --> Price list
HashMap _ticketcomposition = new HashMap();
if((_promos.length>0) && (index>0)) {

 for (int j = 0; j < index; j++) {
  
  _articleid = ticket.getLine(j).getProductID();
  _quantity = ticket.getLine(j).getMultiply();
  _number = _ticketcomposition.get(_articleid);

  if(_number!= null){
   _ticketcomposition.put(_articleid, _number + _quantity);  
  }else{
   _ticketcomposition.put(_articleid, _quantity);
  }
 }
}

if(index>0){

 _taxnormal = taxes.get("8e1780e4-af87-4841-9144-4af60e0814e1");

 for (int i = 0; i < _promos.length;i++) {
  _promo = _promos[i];

  switch(_promo.getType()){
   case 1:
    if(_promo.getAmount() > 0.0){
     if(_ticketcomposition != null){
      if(_promo.getArticle() != null){
       _number = _ticketcomposition.get(_promo.getArticle());
       if(_number != null){
        for (int j = 0; j < initialcount; j++) { 
         if(ticket.getLine(j).getProductID().equals(_promo.getArticle())){
         
          line = ticket.getLine(j);
          
          ticket.insertLine(index,
           new TicketLineInfo(
           "999001",
           "REM % " + _promo.getName() + " " + _promo.getAmount()+ " %",
           "001",
           line.getMultiply(), 
           -line.getPrice () * (_promo.getAmount()/100.0),
           _taxnormal));  
          sales.setSelectedIndex(indexinit);
          index = index + 1;
         }
        }
       }
      }
     }
    }
   break;
   
   case 2:
    if(_promo.getAmount() > 0.0){
     if(_ticketcomposition != null){
      if(_promo.getArticle() != null){
       _number = _ticketcomposition.get(_promo.getArticle());
       if(_number != null){
        for (int j = 0; j < initialcount; j++) { 
         if(ticket.getLine(j).getProductID().equals(_promo.getArticle())){
         
          line = ticket.getLine(j);
          
          ticket.insertLine(index,
           new TicketLineInfo(
           "999002",
           "REM £ " + _promo.getName() + " " + _promo.getAmount()+ " £",
           "001",
           line.getMultiply(), 
           -_promo.getAmount(),
           _taxnormal));  
          sales.setSelectedIndex(indexinit);
          index = index + 1;
         }
        }
       }
      }
     }
    }     
   break;  
   case 3:

    if(_ticketcomposition != null){
     if(_promo.getArticle() != null){
      _number = _ticketcomposition.get(_promo.getArticle());
      if(_number != null){
       if(_number >= _promo.getStepQuantity()){
        for (int j = 0; j < initialcount; j++) { 
         if(ticket.getLine(j).getProductID().equals(_promo.getArticle())){
         
          line = ticket.getLine(j);
          
          ticket.insertLine(index,
           new TicketLineInfo(
           _promo.getArticleBonus(),
           _promo.getArticleBonusDescription() + " - Gift ",
           "001",
           1, 
           0.00,
           _taxnormal));  
          sales.setSelectedIndex(indexinit);
          index = index + 1;
          break;
         }
        }
       }
      }
     }
    }
   break;
   
   case 4:
    if(_ticketcomposition != null){
    
     HashMap _catcomposition = new HashMap();
     _article = null;
     _prix = null;
     _catpromo = _promo.getArticleCategory();
     // list price
     for (int j = 0; j < index; j++) {
      if(ticket.getLine(j).getProductCategoryID() != null){
       if(ticket.getLine(j).getProductCategoryID().equals(_catpromo)){
       
        _articleid = ticket.getLine(j).getProductID();
        _unitprice = ticket.getLine(j).getPrice();
        if(_articleid != null){
         if(_catcomposition.get(_articleid) == null){
          _catcomposition.put(_articleid, _unitprice);
         }
        }
       }
      }
     }
     // check for the cheapest article of the category

     if(_catcomposition.size() > 1){
      Iterator iter1 = _catcomposition.entrySet().iterator();
      
      while (iter1.hasNext()) {
       Map.Entry ent = (Map.Entry) iter1.next();
       _key = ent.getKey();
       Double _value = ent.getValue();
       
       if(_article != null){
        if(_value < _price){
         _article = _key;
         _price = _value; 
        }
       } else {
        _article = _key;
        _price = _value;    
       }
      }
     }
     
     
     if(_catcomposition != null){
         
      if(_catcomposition.size() > 1){
       for (int j = 0; j < initialcount; j++) { 
        if(ticket.getLine(j).getProductID().equals(_article)){
        
         line = ticket.getLine(j);
         ticket.insertLine(index,
          new TicketLineInfo(
          "999004",
          "REM %" + line.getProductName() + " - " + _promo.getAmount() + " %",
          "001",
          line.getMultiply(), 
          -line.getPrice() * (_promo.getAmount()/100.0),
          _taxnormal));  
         sales.setSelectedIndex(indexinit);
         index = index + 1;
         break;
        }
       }
      }
     }
    }
   break; 
   
   case 5: // mix'n'match buy 2 / get 3
    if(_promo.getArticle() != null){
     if(_ticketcomposition != null){
      
      _number = _ticketcomposition.get(_promo.getArticle());
      if(_number != null){
      
       _min = _promo.getMinQuantity();
       _min = _min + 1;
       if(_number >= _min){
        
        _qtyconsidered = _number;
        if(_number > _promo.getMaxQuantity()){
         _qtyconsidered = _promo.getMaxQuantity();
        }
        
        //calculation of the free product number
        _qtyfree = Math.floor(_qtyconsidered/_min);
        
        for (int j = 0; j < initialcount; j++) { 
        
         
         if(ticket.getLine(j).getProductID().equals(_promo.getArticle())){
         
          line = ticket.getLine(j);
          
          ticket.insertLine(index,
           new TicketLineInfo(
           "999003",
           line.getProductName() + " *" + _qtyfree +" Free",
           "001",
           _qtyfree, 
           -line.getPrice (),
           _taxnormal));  
          sales.setSelectedIndex(indexinit);
          index = index + 1;
          break;
         }
        }
       }
      }
     }
    }
   break;
  }
 }
}