public override int SaveOrUpdateInvoiceLine(InvoiceLine invoiceLine)
        {
            if (invoiceLine != null && invoiceLine.ID > 0)
                ResetCache(invoiceLine.ID);

            return base.SaveOrUpdateInvoiceLine(invoiceLine);
        }
        private List<InvoiceLine> GetInvoiceLines()
        {
            var dao = Global.DaoFactory;

            var invoiceLines = new List<InvoiceLine>();

            if (!Request.Form.AllKeys.Any(x => x.StartsWith("iLineItem_")))
                throw new Exception(CRMErrorsResource.InvoiceItemsListEmpty);

            foreach (var customField in Request.Form.AllKeys)
            {
                if (!customField.StartsWith("iLineItem_")) continue;

                var id = Convert.ToInt32(customField.Split('_')[1]);
                var sortOrder = Convert.ToInt32(customField.Split('_')[2]);

                var invoiceItemID = Convert.ToInt32(Request["iLineItem_" + id + "_" + sortOrder]);
                var invoiceTax1ID = Convert.ToInt32(Request["iLineTax1_" + id + "_" + sortOrder]);
                var invoiceTax2ID = Convert.ToInt32(Request["iLineTax2_" + id + "_" + sortOrder]);

                if (!dao.GetInvoiceItemDao().IsExist(invoiceItemID))
                    throw new Exception(CRMErrorsResource.InvoiceItemNotFound);

                if (invoiceTax1ID > 0 && !dao.GetInvoiceTaxDao().IsExist(invoiceTax1ID))
                    throw new Exception(CRMErrorsResource.InvoiceTaxNotFound);

                if (invoiceTax2ID > 0 && !dao.GetInvoiceTaxDao().IsExist(invoiceTax2ID))
                    throw new Exception(CRMErrorsResource.InvoiceTaxNotFound);

                var line = new InvoiceLine
                {
                    ID = id,
                    InvoiceItemID = invoiceItemID,
                    InvoiceTax1ID = invoiceTax1ID,
                    InvoiceTax2ID = invoiceTax2ID,
                    Description = Request["iLineDescription_" + id + "_" + sortOrder],
                    Quantity = Convert.ToInt32(Request["iLineQuantity_" + id + "_" + sortOrder]),
                    Price = Convert.ToDecimal(Request["iLinePrice_" + id + "_" + sortOrder], new CultureInfo("en-US")),
                    Discount = Convert.ToInt32(Request["iLineDiscount_" + id + "_" + sortOrder]),
                    SortOrder = sortOrder
                };

                invoiceLines.Add(line);
            }

            return invoiceLines;
        }
        private int SaveOrUpdateInvoiceLine(InvoiceLine invoiceLine, DbManager db)
        {
            if (invoiceLine.InvoiceID <= 0 || invoiceLine.InvoiceItemID <= 0)
                throw new ArgumentException();

            if (db.ExecuteScalar<int>(Query("crm_invoice_line").SelectCount().Where(Exp.Eq("id", invoiceLine.ID))) == 0)
            {
                invoiceLine.ID = db.ExecuteScalar<int>(
                               Insert("crm_invoice_line")
                              .InColumnValue("id", 0)
                              .InColumnValue("invoice_id", invoiceLine.InvoiceID)
                              .InColumnValue("invoice_item_id", invoiceLine.InvoiceItemID)
                              .InColumnValue("invoice_tax1_id", invoiceLine.InvoiceTax1ID)
                              .InColumnValue("invoice_tax2_id", invoiceLine.InvoiceTax2ID)
                              .InColumnValue("sort_order", invoiceLine.SortOrder)
                              .InColumnValue("description", invoiceLine.Description)
                              .InColumnValue("quantity", invoiceLine.Quantity)
                              .InColumnValue("price", invoiceLine.Price)
                              .InColumnValue("discount", invoiceLine.Discount)
                              .Identity(1, 0, true));
            }
            else
            {

                db.ExecuteNonQuery(
                    Update("crm_invoice_line")
                        .Set("invoice_id", invoiceLine.InvoiceID)
                        .Set("invoice_item_id", invoiceLine.InvoiceItemID)
                        .Set("invoice_tax1_id", invoiceLine.InvoiceTax1ID)
                        .Set("invoice_tax2_id", invoiceLine.InvoiceTax2ID)
                        .Set("sort_order", invoiceLine.SortOrder)
                        .Set("description", invoiceLine.Description)
                        .Set("quantity", invoiceLine.Quantity)
                        .Set("price", invoiceLine.Price)
                        .Set("discount", invoiceLine.Discount)
                        .Where(Exp.Eq("id", invoiceLine.ID)));
            }
            return invoiceLine.ID;
        }
 public InvoiceLineWrapper(InvoiceLine invoiceLine)
     : base(invoiceLine.ID)
 {
     InvoiceID = invoiceLine.InvoiceID;
     InvoiceItemID = invoiceLine.InvoiceItemID;
     InvoiceTax1ID = invoiceLine.InvoiceTax1ID;
     InvoiceTax2ID = invoiceLine.InvoiceTax2ID;
     SortOrder = invoiceLine.SortOrder;
     Description = invoiceLine.Description;
     Quantity = invoiceLine.Quantity;
     Price = invoiceLine.Price;
     Discount = invoiceLine.Discount;
 }
        public virtual int SaveOrUpdateInvoiceLine(InvoiceLine invoiceLine)
        {
            _cache.Insert(_invoiceLineCacheKey, String.Empty);

            using (var db = GetDb())
            {
                return SaveOrUpdateInvoiceLine(invoiceLine, db);
            }
        }
        public static void DemandCreateOrUpdate(InvoiceLine line)
        {
            if (line.InvoiceID <= 0 || line.InvoiceItemID <= 0 ||
                line.Quantity < 0 || line.Price < 0 || line.Discount < 0 || line.Discount > 100 ||
                line.InvoiceTax1ID < 0 || line.InvoiceTax2ID < 0)
                throw new ArgumentException();

            var invoice = Global.DaoFactory.GetInvoiceDao().GetByID(line.InvoiceID);
            if (invoice == null) throw new ArgumentException();
            if (!CRMSecurity.CanEdit(invoice)) throw CRMSecurity.CreateSecurityException();

            if (!Global.DaoFactory.GetInvoiceItemDao().IsExist(line.InvoiceItemID))
                throw new ArgumentException();

            if (line.InvoiceTax1ID > 0 && !Global.DaoFactory.GetInvoiceTaxDao().IsExist(line.InvoiceTax1ID))
                throw new ArgumentException();

            if (line.InvoiceTax2ID > 0 && !Global.DaoFactory.GetInvoiceTaxDao().IsExist(line.InvoiceTax2ID))
                throw new ArgumentException();
        }
        public InvoiceLineWrapper CreateInvoiceLine(
            int invoiceId,
            int invoiceItemId,
            int invoiceTax1Id,
            int invoiceTax2Id,
            int sortOrder,
            string description,
            int quantity,
            decimal price,
            int discount
            )
        {
            var invoiceLine = new InvoiceLine
                {
                    InvoiceID = invoiceId,
                    InvoiceItemID = invoiceItemId,
                    InvoiceTax1ID = invoiceTax1Id,
                    InvoiceTax2ID = invoiceTax2Id,
                    SortOrder = sortOrder,
                    Description = description,
                    Quantity = quantity,
                    Price = price,
                    Discount = discount
                };

            if (invoiceId <= 0)
                throw new ArgumentException();
            var invoice = Global.DaoFactory.GetInvoiceDao().GetByID(invoiceId);
            CRMSecurity.DemandCreateOrUpdate(invoiceLine, invoice);

            invoiceLine.ID = DaoFactory.GetInvoiceLineDao().SaveOrUpdateInvoiceLine(invoiceLine);

            DaoFactory.GetInvoiceDao().UpdateInvoiceJsonDataAfterLinesUpdated(invoice);
            if (Global.CanDownloadInvoices)
            {
                new InvoiceFileUpdateHelper().UpdateInvoiceFileIDInThread(invoice.ID);
            }

            return ToInvoiceLineWrapper(invoiceLine);
        }
        private List<InvoiceLine> CreateInvoiceLines(List<InvoiceLine> invoiceLines, Invoice invoice)
        {
            var result = new List<InvoiceLine>();
            for (var i = 0; i < invoiceLines.Count; i++)
            {
                var line = new InvoiceLine
                {
                    ID = 0,
                    InvoiceID = invoice.ID,
                    InvoiceItemID = invoiceLines[i].InvoiceItemID,
                    InvoiceTax1ID = invoiceLines[i].InvoiceTax1ID,
                    InvoiceTax2ID = invoiceLines[i].InvoiceTax2ID,
                    SortOrder = i,
                    Description = invoiceLines[i].Description,
                    Quantity = invoiceLines[i].Quantity,
                    Price = invoiceLines[i].Price,
                    Discount = Convert.ToInt32(invoiceLines[i].Discount)
                };

                line.ID = DaoFactory.GetInvoiceLineDao().SaveOrUpdateInvoiceLine(line);
                result.Add(line);
            }
            return result;
        }
 private InvoiceLineWrapper ToInvoiceLineWrapper(InvoiceLine invoiceLine)
 {
     return new InvoiceLineWrapper(invoiceLine);
 }
        public InvoiceLineWrapper CreateInvoiceLine(
            int invoiceId,
            int invoiceItemId,
            int invoiceTax1Id,
            int invoiceTax2Id,
            int sortOrder,
            string description,
            int quantity,
            decimal price,
            int discount
            )
        {
            var invoiceLine = new InvoiceLine
                {
                    InvoiceID = invoiceId,
                    InvoiceItemID = invoiceItemId,
                    InvoiceTax1ID = invoiceTax1Id,
                    InvoiceTax2ID = invoiceTax2Id,
                    SortOrder = sortOrder,
                    Description = description,
                    Quantity = quantity,
                    Price = price,
                    Discount = discount
                };

            CRMSecurity.DemandCreateOrUpdate(invoiceLine);

            invoiceLine.ID = DaoFactory.GetInvoiceLineDao().SaveOrUpdateInvoiceLine(invoiceLine);

            return ToInvoiceLineWrapper(invoiceLine);
        }