//    Posics Saleculator - Billing System.
//    Copyright (C) 2009 Posics
//
//    This file is part of Posics Saleculator.
//
// Print an order ticket to the Kitchen

boolean kot1 = false;
boolean kot2 = false;
boolean kot3 = false;
boolean change_kot1 = false;
boolean change_kot2 = false;
boolean change_kot3 = false;

for(int i= 0; i < ticket.getLinesCount(); i++){
line = ticket.getLine(i);
// Set Discount(kotnum=NULL) to N/A so it does not error on next section.
if (line.getProperty("kotnum") == null){
line.setProperty("kotnum", "KOT1");
}
if (line.getProperty("sendstatus") == null){
line.setProperty("sendstatus", "No");
}

 if((line.getProperty("kotnum").equals("KOT1")) && (line.getProperty("sendstatus").equals("No"))){
 kot1 = true; //There is something to print to KOT1
 }else if ((line.getProperty("kotnum").equals("KOT2")) && (line.getProperty("sendstatus").equals("No"))){
 kot2 = true; //There is something to print to KOT2
 }else if ((line.getProperty("kotnum").equals("KOT3")) && (line.getProperty("sendstatus").equals("No"))){
 kot3 = true; //There is something to print to KOT3
 }else if ((line.getProperty("kotnum").equals("KOT1")) && (line.getProperty("sendstatus").equals("Cancel"))){
 change_kot1 = true; //There is something to change for KOT1
 }else if ((line.getProperty("kotnum").equals("KOT1")) && (line.getProperty("sendstatus").equals("Modify"))){
 change_kot1 = true; //There is a quantity modification for KOT1
 }else if ((line.getProperty("kotnum").equals("KOT2")) && (line.getProperty("sendstatus").equals("Cancel"))){
 change_kot2 = true; //There is something to change for KOT2
 }else if ((line.getProperty("kotnum").equals("KOT2")) && (line.getProperty("sendstatus").equals("Modify"))){
 change_kot2 = true; //There is a quantity modification for KOT2
 }else if ((line.getProperty("kotnum").equals("KOT3")) && (line.getProperty("sendstatus").equals("Cancel"))){
 change_kot3 = true; //There is something to change for KOT3
 }else if ((line.getProperty("kotnum").equals("KOT3")) && (line.getProperty("sendstatus").equals("Modify"))){
 change_kot3 = true; //There is a quantity modification for KOT3
 }
}

// Prepare direct quantity edits of sent items for clear KOT printing.
for(int i= 0; i < ticket.getLinesCount(); i++){
 line = ticket.getLine(i);
 if(line.getProperty("sendstatus").equals("Modify")){
  double oldQty = Double.parseDouble(line.getProperty("kotmodifyqty"));
  double newQty = line.getMultiply();

  if(oldQty < newQty){
   line.setProperty("kotadjustlabel", "Add");
   line.setProperty("kotadjustqty", String.valueOf(newQty - oldQty));
  }else if(oldQty > newQty){
   line.setProperty("kotadjustlabel", "Cancel");
   line.setProperty("kotadjustqty", String.valueOf(oldQty - newQty));
  }else{
   line.setProperty("kotadjustlabel", "Change");
   line.setProperty("kotadjustqty", String.valueOf(newQty));
  }
 }
}

// Match cancel + add of the same sent item as a quantity correction.
// This prevents a full replacement (25 -> 26) from appearing as 25 cancelled.
for(int i= 0; i < ticket.getLinesCount(); i++){
 line = ticket.getLine(i);
 if(!line.isProductCom() && line.getProperty("sendstatus").equals("Cancel")){
  for(int j= 0; j < ticket.getLinesCount(); j++){
   newline = ticket.getLine(j);
   if(!newline.isProductCom() && newline.getProperty("sendstatus").equals("No")
       && line.getProperty("kotnum").equals(newline.getProperty("kotnum"))
       && line.getProductName().equals(newline.getProductName())
       && line.getSalePrice() == newline.getSalePrice()
       && ((line.getProductID() == null && newline.getProductID() == null)
           || (line.getProductID() != null && line.getProductID().equals(newline.getProductID())))){

    double oldQty = line.getMultiply();
    double newQty = newline.getMultiply();

    if(oldQty > newQty){
     line.setMultiply(oldQty - newQty);
     newline.setProperty("sendstatus", "ReplaceKeep");
    }else{
     line.setProperty("sendstatus", "Replaced");
     if(oldQty < newQty){
      newline.setProperty("kotadjustlabel", "Add");
      newline.setProperty("kotadjustqty", String.valueOf(newQty - oldQty));
     }else{
      newline.setProperty("kotadjustlabel", "Change");
      newline.setProperty("kotadjustqty", String.valueOf(newQty));
     }
    }

    for(int ci = i + 1; ci < ticket.getLinesCount(); ci++){
     oldcom = ticket.getLine(ci);
     if(!oldcom.isProductCom()){
      break;
     }
     if(!oldcom.getProperty("sendstatus").equals("Cancel")){
      continue;
     }

     for(int cj = j + 1; cj < ticket.getLinesCount(); cj++){
      newcom = ticket.getLine(cj);
      if(!newcom.isProductCom()){
       break;
      }
      if(newcom.getProperty("sendstatus").equals("No")
          && newcom.getProperty("kotadjustpaired") == null
          && oldcom.getProperty("kotnum").equals(newcom.getProperty("kotnum"))
          && oldcom.getProductName().equals(newcom.getProductName())
          && oldcom.getSalePrice() == newcom.getSalePrice()
          && ((oldcom.getProductID() == null && newcom.getProductID() == null)
              || (oldcom.getProductID() != null && oldcom.getProductID().equals(newcom.getProductID())))){

       double oldComQty = oldcom.getMultiply();
       double newComQty = newcom.getMultiply();

       if(oldComQty > newComQty){
        oldcom.setMultiply(oldComQty - newComQty);
        newcom.setProperty("sendstatus", "ReplaceKeep");
       }else{
        oldcom.setProperty("sendstatus", "Replaced");
        if(oldComQty < newComQty){
         newcom.setProperty("kotadjustlabel", "Add");
         newcom.setProperty("kotadjustqty", String.valueOf(newComQty - oldComQty));
        }else{
         newcom.setProperty("kotadjustlabel", "Change");
         newcom.setProperty("kotadjustqty", String.valueOf(newComQty));
        }
       }
       newcom.setProperty("kotadjustpaired", "Yes");
       break;
      }
     }
    }
    break;
   }
  }
 }
}

if (kot1 || kot2 || kot3 || change_kot1 || change_kot2 || change_kot3) {
sales.keepTicket(ticket); 
}

if ((change_kot1 && kot1) || (change_kot1 && !kot1)) {
sales.printTicket("Printer.KOT1_Change"); //Print changed kitchen items to KOT1 printer
}
if ((change_kot2 && kot2) || (change_kot2 && !kot2)) {
sales.printTicket("Printer.KOT2_Change"); //Print changed dessert items to KOT2 printer
}
if ((change_kot3 && kot3) || (change_kot3 && !kot3)) {
sales.printTicket("Printer.KOT3_Change"); //Print changed dessert items to KOT3 printer
}
if (kot1 && !change_kot1) {
sales.printTicket("Printer.KOT1"); //Print KOT1 items to KOT1 printer
}
if (kot2 && !change_kot2) {
sales.printTicket("Printer.KOT2"); //Print KOT2 items to KOT2 printer
}
if (kot3 && !change_kot3) {
sales.printTicket("Printer.KOT3"); //Print KOT3 items to KOT3 printer
}

//Show a nice message for confirmation
//if (kot1 && kot2 && kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to KOT1, KOT2 & KOT3.");
//} else if (kot1 && !kot2 && !kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT1.");
//} else if (!kot1 && kot2 && !kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT2.");
//} else if (!kot1 && !kot2 && kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT3.");
//} else if (kot1 && kot2 && !kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT1 & KOT2.");
//} else if (!kot1 && kot2 && kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT2 & KOT3.");
//} else if (kot1 && !kot2 && kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your order has been sent to the KOT1 & KOT3.");
//} else if (change_kot1 || change_kot2 || change_kot3){
//javax.swing.JOptionPane.showMessageDialog(null, "Your cancelled items have been sent.");
//} else {
//javax.swing.JOptionPane.showMessageDialog(null, "There is nothing new to send.", "Print Warning", JOptionPane.WARNING_MESSAGE);
//}

//Set kotnum property of item to Yes so it is not printed again
for(int i = ticket.getLinesCount()-1; i>= 0 ; i--){
 line = ticket.getLine(i);
 String a = line.getProperty("sendstatus");
 String b = "Cancel";
 String c = "Replaced";
 String d = "ReplaceKeep";
 String e = "Modify";
 if(a.equals(e)){
  double oldQty = Double.parseDouble(line.getProperty("kotmodifyqty"));
  double newQty = line.getMultiply();
  if(oldQty > newQty){
   removedline = new com.posics.pos.ticket.TicketLineInfo(line);
   removedline.setMultiply(oldQty - newQty);
   removedline.setProperty("sendstatus", "Cancel");
   sales.keepLine(removedline);
  }
 }

 if(((line.getProperty("kotnum").equals("KOT1")) || (line.getProperty("kotnum").equals("KOT2")) || (line.getProperty("kotnum").equals("KOT3"))) && (line.getProperty("sendstatus").equals("No") || line.getProperty("sendstatus").equals(d) || line.getProperty("sendstatus").equals(e))){
 line.setProperty("sendstatus", "Yes");
 line.getProperties().remove("kotadjustlabel");
 line.getProperties().remove("kotadjustqty");
 line.getProperties().remove("kotadjustpaired");
 line.getProperties().remove("kotmodifyqty");
 }

 //Remove cancelled lines
 if (a.equals(b)) {
 ticket.removeLine(i);
 sales.keepLine(line);
 }else if (a.equals(c)) {
 ticket.removeLine(i);
 }
}
