Ejemplo n.º 1
0
 public static void UpdateSumOfTax(Session session, NAS.DAL.Invoice.Bill bill)
 {
     #region Update SumOfTax of the bill
     if (bill.TaxCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_BILL_BY_PERCENTAGE))
     {
         BillTax billTax = bill.BillTaxs.FirstOrDefault();
         if (billTax == null)
         {
             billTax = new BillTax(session)
             {
                 BillId          = bill,
                 TaxInNumber     = 0,
                 TaxInPercentage = 0
             };
             billTax.Save();
         }
         if (billTax.TaxId == null)
         {
             bill.SumOfTax = 0;
         }
         else
         {
             bill.SumOfTax = (bill.SumOfItemPrice - bill.SumOfPromotion)
                             * billTax.TaxId.Percentage / 100;
         }
     }
     else if (bill.TaxCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_ITEMS))
     {
         if (bill.PromotionCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_ITEMS))
         {
             var    billItemTaxs = bill.BillItems.SelectMany(r => r.BillItemTaxs);
             double taxInNumber  = billItemTaxs.Sum(r =>
                                                    (r.BillItemId.TotalPrice - r.BillItemId.PromotionInNumber) * r.TaxInPercentage / 100);
             bill.SumOfTax = taxInNumber;
             bill.Save();
         }
         else if (bill.PromotionCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_BILL_BY_AMOUNT) ||
                  bill.PromotionCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_BILL_BY_PERCENTAGE))
         {
             var    billItemTaxs = bill.BillItems.SelectMany(r => r.BillItemTaxs);
             double taxInNumber  = billItemTaxs.Sum(r =>
                                                    r.BillItemId.TotalPrice * r.TaxInPercentage / 100);
             bill.SumOfTax = taxInNumber;
             bill.Save();
         }
     }
     session.FlushChanges();
     #endregion Update SumOfTax of the bill
 }
Ejemplo n.º 2
0
        public virtual void CreateBillItem(
            DevExpress.Xpo.Session session,
            Guid billId,
            Guid itemId,
            Guid unitId,
            double quantity,
            double price,
            double promotionInPercentage,
            string comment)
        {
            try
            {
                NAS.DAL.Invoice.Bill bill =
                    session.GetObjectByKey <NAS.DAL.Invoice.Bill>(billId);
                if (bill == null)
                {
                    throw new Exception("Could not found specific bill");
                }
                //Get ItemUnit
                Item item = session.GetObjectByKey <Item>(itemId);
                Unit unit = session.GetObjectByKey <Unit>(unitId);

                ItemUnit itemUnit = session.FindObject <ItemUnit>(
                    CriteriaOperator.And(
                        new BinaryOperator("ItemId", item),
                        new BinaryOperator("UnitId", unit),
                        CriteriaOperator.Or(
                            new BinaryOperator("RowStatus", Utility.Constant.ROWSTATUS_ACTIVE),
                            new BinaryOperator("RowStatus", Utility.Constant.ROWSTATUS_DEFAULT)
                            )
                        ));
                if (itemUnit == null)
                {
                    throw new Exception("Could not found item unit");
                }

                BillItem billItem = new BillItem(session)
                {
                    BillId  = bill,
                    Comment = comment,
                    //Currency
                    ItemUnitId        = itemUnit,
                    Price             = price,
                    PromotionInNumber = promotionInPercentage != -1
                        ? (price * quantity * promotionInPercentage) / 100 : 0,
                    PromotionInPercentage = promotionInPercentage,
                    Quantity   = quantity,
                    RowStatus  = Utility.Constant.ROWSTATUS_ACTIVE,
                    TotalPrice = price * quantity
                };
                billItem.Save();

                //update SumOfItemPrice
                bill.SumOfItemPrice = bill.BillItems.Sum(r => r.TotalPrice);
                bill.Save();

                if (bill.PromotionCalculationType.Equals(Utility.Constant.CALCULATION_TYPE_ON_ITEMS))
                {
                    UpdatePromotionOnItems(session, billId);
                }

                //Insert into BillItemTax...
                //Get VAT TaxType of Item
                ItemBO  itemBO  = new ItemBO();
                ItemTax itemTax = itemBO.GetCurrentVATOfItem(session, itemId);

                if (itemTax != null)
                {
                    BillItemTax billItemTax = new BillItemTax(session)
                    {
                        BillItemId      = billItem,
                        ItemTaxId       = itemTax,
                        TaxInNumber     = itemTax.TaxId.Amount,
                        TaxInPercentage = itemTax.TaxId.Percentage
                    };
                    billItemTax.Save();
                }

                UpdateSumOfTax(session, bill);

                #region Update bill Total
                bill.Total = bill.SumOfItemPrice - bill.SumOfPromotion + bill.SumOfTax;
                bill.Save();
                #endregion Update bill Total
            }
            catch (Exception ex)
            {
                throw;
            }
        }