public void add(CSSaleItem csSaleItem)
 {
     using (context = new CSFPOSWareContext())
     {
         context.CSSaleItems.Add(csSaleItem);
         try
         {
             context.SaveChanges();
         }
         catch (Exception e)
         {
             Logger.log.Error(e.Message);
             Logger.log.Error(e.StackTrace);
             Logger.log.Error(e.InnerException.StackTrace);
         }
     }
 }
        public void removeBySaleItem(CSSaleItem csSaleItem)
        {
            List<CSSaleItemDiscount> CSSaleItemDiscountList = null;
            using (context = new CSFPOSWareContext())
            {
                var query = from sid in context.CSSaleItemDiscounts
                            where sid.CSSaleItemID == csSaleItem.CSSaleItemID
                            select sid;
                CSSaleItemDiscountList = query.ToList();
            }

            using (context = new CSFPOSWareContext())
            {
                foreach (CSSaleItemDiscount CSSaleItemDiscount in CSSaleItemDiscountList)
                {
                    context.CSSaleItemDiscounts.Remove(CSSaleItemDiscount);
                    context.SaveChanges();
                }
            }
        }
 public void remove(CSSaleItem csSaleItem)
 {
     csSaleItem = get(csSaleItem.CSSaleItemID);
     using (context = new CSFPOSWareContext())
     {
         if (null != csSaleItem)
         {
             context.CSSaleItems.Remove(csSaleItem);
             try
             {
                 context.SaveChanges();
             }
             catch (Exception e)
             {
                 Logger.log.Error(e.Message);
                 Logger.log.Error(e.StackTrace);
                 Logger.log.Error(e.InnerException.StackTrace);
             }
         }
     }
 }
        private void _buildCSSaleItem(List<Seat> seats)
        {
            List<Discount> discounts = maintenanceDAO.getDiscounts();
            //Logger.log.Debug(discounts);
            SaleItemDiscountDAO saleItemDiscountDAO = new SaleItemDiscountDAO();
            ItemDAO itemDAO = new ItemDAO();
            foreach (Seat seat in seats)
            {
                seat.csSaleItems = new List<CSSaleItem>();
                foreach (SaleItem saleItem in seat.saleItems)
                {
                    Item item = itemDAO.get(saleItem.ItemName);
                    //Logger.log.Debug(BaseModel.ToString(saleItem));
                    List<SaleItemDiscount> saleItemDiscounts = saleItemDiscountDAO.find(saleItem.SaleItemID.ToString());
                    //double discountAmount = 0;
                    decimal vatableAmount = Util.Number.fposToMoney(saleItem.BasePrice);
                    decimal vatableTax = Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12) * new decimal(0.12);
                    decimal vatExemptAmount = 0;
                    decimal vatExemptTax = 0;
                    decimal zeroRatedAmount = 0;
                    decimal zeroRatedtax = 0;

                    decimal amountDue = Util.Number.fposToMoney(saleItem.BasePrice);
                    bool hasSCDisc = false;
                    bool hasPWDDisc = false;

                    foreach (SaleItemDiscount saleItemDiscount in saleItemDiscounts)
                    {
                        Logger.log.Debug(saleItem.ItemName + " discountIndex : " + saleItemDiscount.DiscountIndex);
                        CSConfigurationDAO csConfigurationDAO = new CSConfigurationDAO();
                        int PWDMapIndex = int.Parse(csConfigurationDAO.get(CSConfiguration.Sections.DiscountIndexMap, CSConfiguration.Parameters.PWDIndex).Value);
                        int SeniorMapIndex = int.Parse(csConfigurationDAO.get(CSConfiguration.Sections.DiscountIndexMap, CSConfiguration.Parameters.SeniorIndex).Value);

                        Discount currDiscount = discounts[saleItemDiscount.DiscountIndex];

                        decimal discountAmount = 0;
                        if (saleItemDiscount.DiscountIndex == SeniorMapIndex)
                        {//Senior - refer to this file for computation : https://condorsolutions.slack.com/files/otalusan/F0E994FEK/senior_discount_computation.txt
                            discountAmount += (Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12)) * currDiscount.PercentOff / 10000;
                            vatableAmount = 0;
                            vatableTax = 0;
                            vatExemptAmount = Util.Number.fposToMoney(saleItem.BasePrice);
                            vatExemptTax = Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12) * new decimal(0.12);
                            amountDue -= discountAmount;
                        }
                        else if (saleItemDiscount.DiscountIndex == PWDMapIndex)
                        {//PWD
                            if (Main.GlobalValues.mallSetup.Equals(CSConfiguration.Values.Megaworld))
                            {//Megaworld setup
                                discountAmount += Util.Number.fposToMoney(saleItem.BasePrice) * currDiscount.PercentOff / 10000;
                                vatableAmount = Util.Number.fposToMoney(saleItem.BasePrice);
                                vatableTax = Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12) * new decimal(0.12);
                                amountDue -= discountAmount;
                            }
                            else if (Main.GlobalValues.mallSetup.Equals(CSConfiguration.Values.SM))
                            {//SM setup
                                discountAmount += Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12) * currDiscount.PercentOff / 10000;
                                decimal pwdAmountDue = amountDue - Util.Number.fposToMoney(saleItem.BasePrice) * currDiscount.PercentOff / 10000;
                                vatableAmount = pwdAmountDue;
                                vatableTax = pwdAmountDue / new decimal(1.12) * new decimal(0.12);
                                amountDue -= Util.Number.fposToMoney(saleItem.BasePrice) * currDiscount.PercentOff / 10000;
                            }
                        }
                        else //other discounts not PWD and Senior
                        {
                            vatableAmount = Util.Number.fposToMoney(saleItem.BasePrice);
                            vatableTax = Util.Number.fposToMoney(saleItem.BasePrice) / new decimal(1.12) * new decimal(0.12);
                            discountAmount += Util.Number.fposToMoney(saleItemDiscount.Amount);
                            amountDue -= discountAmount;
                        }

                        //TODO: otalusan: Zero-rated items
                        if (item.TaxFlags == 4)
                        {
                            Logger.log.Info("TODO: otalusan: Zero-rated items");
                        }
                        CSSaleItemDiscount csSaleItemDiscount = new CSSaleItemDiscount();
                        csSaleItemDiscount.CSSaleItemDiscountID = saleItemDiscount.SaleItemDiscountID;
                        csSaleItemDiscount.CSSaleItemID = saleItemDiscount.SaleItemID;
                        csSaleItemDiscount.ItemIndex = saleItemDiscount.ItemIndex;
                        csSaleItemDiscount.DiscountName = currDiscount.DiscountName;
                        csSaleItemDiscount.Amount = discountAmount;

                        CSSaleItemDiscountDAO csSaleItemDiscountDAO = new CSSaleItemDiscountDAO();
                        Logger.log.Debug(BaseModel.ToString(csSaleItemDiscount));
                        //csSaleItemDiscountDAO.remove(csSaleItemDiscount);
                        csSaleItemDiscountDAO.add(csSaleItemDiscount);
                        break;//Apply only ONE discount
                    }//foreach(SaleItemDiscount saleItemDiscount in saleItemDiscounts)

                    CSSaleItemTaxDAO csSaleItemTaxDAO = new CSSaleItemTaxDAO();
                    // csSaleItemTaxDAO.removeBySaleItem(saleItem);//cleanup

                    //1. VAT sales
                    CSSaleItemTax csSaleItemTaxVAT = new CSSaleItemTax();
                    csSaleItemTaxVAT.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItemTaxVAT.Type = CSSaleItemTax.Types.VAT;
                    csSaleItemTaxVAT.Amount = vatableAmount;
                    csSaleItemTaxVAT.Tax = vatableTax;
                    if (csSaleItemTaxVAT.Amount > 0)
                        csSaleItemTaxDAO.add(csSaleItemTaxVAT);
                    //2. VAT exempt sales
                    CSSaleItemTax csSaleItemTaxVATExempt = new CSSaleItemTax();
                    csSaleItemTaxVATExempt.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItemTaxVATExempt.Type = CSSaleItemTax.Types.VATExempt;
                    csSaleItemTaxVATExempt.Amount = vatExemptAmount;
                    csSaleItemTaxVATExempt.Tax = vatExemptTax;
                    if (csSaleItemTaxVATExempt.Amount > 0)
                        csSaleItemTaxDAO.add(csSaleItemTaxVATExempt);
                    //3. Zero-rated sales
                    CSSaleItemTax csSaleItemTaxZero = new CSSaleItemTax();
                    csSaleItemTaxZero.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItemTaxZero.Type = CSSaleItemTax.Types.ZERORated;
                    csSaleItemTaxZero.Amount = zeroRatedAmount;
                    csSaleItemTaxZero.Tax = zeroRatedtax;
                    if (csSaleItemTaxZero.Amount > 0)
                        csSaleItemTaxDAO.add(csSaleItemTaxZero);
                    //TODO: otalusan - local and other taxes
                    //4. Local tax
                    CSSaleItemTax csSaleItemLocalTax = new CSSaleItemTax();
                    csSaleItemLocalTax.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItemLocalTax.Type = CSSaleItemTax.Types.Local;

                    //5. Other taxes
                    CSSaleItemTax csSaleItemOtherTax = new CSSaleItemTax();
                    csSaleItemOtherTax.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItemOtherTax.Type = CSSaleItemTax.Types.Other;

                    //build CSSaleItem - all amounts computed for
                    CSSaleItem csSaleItem = new CSSaleItem();
                    csSaleItem.CSSaleItemID = saleItem.SaleItemID;
                    csSaleItem.CSSaleID = saleItem.SaleID;
                    csSaleItem.ItemName = saleItem.ItemName;

                    csSaleItem.SeatNumber = seat.number;
                    csSaleItem.BasePrice = saleItem.BasePrice / 100;
                    csSaleItem.Quantity = saleItem.Quantity;
                    csSaleItem.AmountDue = amountDue;

                    //csSaleItem.vatableAmount = Util.Number.moneyToFposWare(vatableAmount);
                    //csSaleItem.vatableTax = Util.Number.moneyToFposWare(vatableTax);
                    //csSaleItem.vatExemptAmount = Util.Number.moneyToFposWare(vatExemptAmount);
                    //csSaleItem.vatExemptTax = Util.Number.moneyToFposWare(vatExemptTax);
                    //csSaleItem.zeroRatedAmount = Util.Number.moneyToFposWare(zeroRatedAmount);
                    //csSaleItem.zeroRatedtax = Util.Number.moneyToFposWare(zeroRatedtax);

                    CSSaleItemDAO csSaleItemDAO = new CSSaleItemDAO();
                    // csSaleItemDAO.remove(csSaleItem);
                    csSaleItemDAO.add(csSaleItem);
                    seat.csSaleItems.Add(csSaleItem);
                }//foreach(SaleItem saleItem in seat.saleItems)
            }//foreach(Seat seat in saleInfo.seats)
        }
        //public void removeBySaleItem(SaleItem saleItem)
        //{
        //    List<CSSaleItemTax> CSSaleItemTaxes = findBySaleItem(saleItem.SaleItemID);
        //    if (CSSaleItemTaxes != null)
        //    {
        //        using (context = new CSFPOSWareContext())
        //        {
        //            foreach (CSSaleItemTax csSaleItemTax in CSSaleItemTaxes)
        //            {
        //                Logger.log.Debug("deleting CSSaleItemTaxes...");
        //                context.CSSaleItemTaxes.Remove(csSaleItemTax);
        //                try
        //                {
        //                    context.SaveChanges();
        //                }
        //                catch (Exception e)
        //                {
        //                    Logger.log.Error(e.Message);
        //                    Logger.log.Error(e.StackTrace);
        //                    Logger.log.Error(e.InnerException.StackTrace);
        //                }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        Logger.log.Debug("no CSSaleItemTaxes to delete");
        //    }
        //}
        public void removeBySaleItem(CSSaleItem csSaleItem)
        {
            List<CSSaleItemTax> CSSaleItemTaxList = null;
            using (context = new CSFPOSWareContext())
            {
                var query = from sit in context.CSSaleItemTaxes
                            where sit.CSSaleItemID == csSaleItem.CSSaleItemID
                            select sit;
                CSSaleItemTaxList = query.ToList();
            }

            using (context = new CSFPOSWareContext())
            {
                foreach (CSSaleItemTax CSSaleItemTax in CSSaleItemTaxList)
                {
                    context.CSSaleItemTaxes.Remove(CSSaleItemTax);
                    context.SaveChanges();
                }
            }
        }