public static string Insert(Sale _Sale, Sale_B_KNITTED _Sale_B_KNITTED, List<Sale_B_WOVEN> list_Sale_B_WOVEN, Sale_B_NONWOVEN _Sale_B_NONWOVEN, Sale_B_TERRY _Sale_B_TERRY, Sale_B_KNITWRIST _Sale_B_KNITWRIST, Sale_B_LEATHER _Sale_B_LEATHER)
        {
            _Sale.ID = InsertSale(_Sale);

            if (_Sale.ID > 0)
            {

                if (_Sale_B_KNITTED != null)
                    InsertKNITTED(_Sale_B_KNITTED, _Sale.ID, _Sale.CreatedBy);
                if (list_Sale_B_WOVEN != null)
                    foreach (Sale_B_WOVEN _Sale_B_WOVEN in list_Sale_B_WOVEN)
                        InsertWOVEN(_Sale_B_WOVEN, _Sale.ID, _Sale.CreatedBy);

                if (_Sale_B_NONWOVEN != null)
                    InsertNONWOVEN(_Sale_B_NONWOVEN, _Sale.ID, _Sale.CreatedBy);
                if (_Sale_B_TERRY != null)
                    InsertTERRY(_Sale_B_TERRY, _Sale.ID, _Sale.CreatedBy);
                if (_Sale_B_KNITWRIST != null)
                    InsertKNITWRIST(_Sale_B_KNITWRIST, _Sale.ID, _Sale.CreatedBy);
                if (_Sale_B_LEATHER != null)
                    InsertLEATHER(_Sale_B_LEATHER, _Sale.ID, _Sale.CreatedBy);

                return "0";
            }
            else
            {
                return "1";
            }
        }
        public static int InsertSale(Sale _Sale)
        {
            DbCommand command = Catalog_Access.CreateCommand();
            command.CommandText = "sp_insertSale";

            DbParameter param;

            param = command.CreateParameter();
            param.ParameterName = "@Article_ID";
            param.Value = _Sale.Article_ID;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Customer_ID";
            param.Value = _Sale.Customer_ID;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Style";
            param.Value = _Sale.Style;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Size";
            param.Value = _Sale.Size;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Pair_Bar_Code";
            param.Value = _Sale.Pair_Bar_Code;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Doz_Bar_Code";
            param.Value = _Sale.Doz_Bar_Code;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Ctn_Bar_Code";
            param.Value = _Sale.Ctn_Bar_Code;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@LABLE1";
            param.Value = _Sale.LABLE1;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@LABLE2";
            param.Value = _Sale.LABLE2;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@PLCMNT1";
            param.Value = _Sale.PLCMNT1;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@PLCMNT2";
            param.Value = _Sale.PLCMNT2;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Poly_Bag_Packing";
            param.Value = _Sale.Poly_Bag_Packing;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Packing_Unit";
            param.Value = _Sale.Packing_Unit;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Net_Wt_PerUnit";
            param.Value = _Sale.Net_Wt_PerUnit;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Unit";
            param.Value = _Sale.Unit;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@CTN_Packing";
            param.Value = _Sale.CTN_Packing;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@CTN_Packing_Unit";
            param.Value = _Sale.CTNPacking_Unit;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@CTN_Unit";
            param.Value = _Sale.CTN_Unit;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Gross_Wt_PerCTN";
            param.Value = _Sale.Gross_Wt_PerCTN;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Gross_Wt_Unit";
            param.Value = _Sale.Gross_Wt_Unit;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@D1";
            param.Value = _Sale.D1;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@D2";
            param.Value = _Sale.D2;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@D3";
            param.Value = _Sale.D3;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@CreatedBy";
            param.Value = _Sale.CreatedBy;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Return";
            param.DbType = DbType.Int32;
            param.Direction = ParameterDirection.Output;
            command.Parameters.Add(param);

            Catalog_Access.ExecuteNonQuery(command);

            int Return = int.Parse(command.Parameters["@Return"].Value.ToString());

            return Return;
        }
        public static string Update(Sale _Sale, Sale_B_KNITTED _Sale_B_KNITTED, List<Sale_B_WOVEN> list_Sale_B_WOVEN, Sale_B_NONWOVEN _Sale_B_NONWOVEN, Sale_B_TERRY _Sale_B_TERRY, Sale_B_KNITWRIST _Sale_B_KNITWRIST, Sale_B_LEATHER _Sale_B_LEATHER)
        {
            if (UpdateSale(_Sale) == "4")
            {
                if (_Sale_B_KNITTED != null)
                    if (_Sale_B_KNITTED.Id == 0)
                        InsertKNITTED(_Sale_B_KNITTED, _Sale.ID, _Sale.UpdatedBy);
                    else
                        UpdateKNITTED(_Sale_B_KNITTED, _Sale.ID, _Sale.UpdatedBy);

                if (list_Sale_B_WOVEN != null)
                    foreach (Sale_B_WOVEN _Sale_B_WOVEN in list_Sale_B_WOVEN)
                    {
                        if (_Sale_B_WOVEN.Id == 0)
                            InsertWOVEN(_Sale_B_WOVEN, _Sale.ID, _Sale.UpdatedBy);
                        else
                            UpdateWOVEN(_Sale_B_WOVEN, _Sale.ID, _Sale.UpdatedBy);
                    }
                if (_Sale_B_NONWOVEN != null)
                    if (_Sale_B_NONWOVEN.Id == 0)
                        InsertNONWOVEN(_Sale_B_NONWOVEN, _Sale.ID, _Sale.UpdatedBy);
                    else
                        UpdateNONWOVEN(_Sale_B_NONWOVEN, _Sale.ID, _Sale.UpdatedBy);

                if (_Sale_B_TERRY != null)
                    if (_Sale_B_TERRY.Id == 0)
                        InsertTERRY(_Sale_B_TERRY, _Sale.ID, _Sale.UpdatedBy);
                    else
                        UpdateTERRY(_Sale_B_TERRY, _Sale.ID, _Sale.UpdatedBy);

                if (_Sale_B_KNITWRIST != null)
                    if (_Sale_B_KNITWRIST.Id == 0)
                        InsertKNITWRIST(_Sale_B_KNITWRIST, _Sale.ID, _Sale.UpdatedBy);
                    else
                        UpdateKNITWRIST(_Sale_B_KNITWRIST, _Sale.ID, _Sale.UpdatedBy);

                if (_Sale_B_LEATHER != null)
                    if (_Sale_B_LEATHER.Id == 0)
                        InsertLEATHER(_Sale_B_LEATHER, _Sale.ID, _Sale.UpdatedBy);
                    else
                        UpdateLEATHER(_Sale_B_LEATHER, _Sale.ID, _Sale.UpdatedBy);

                return "4";
            }
            else
            {
                return "1";
            }
        }
        public void Filter_Get_Sale(int iDisplayLength, int iDisplayStart, int iSortCol_0, string sSortDir_0, string sSearch)
        {
            int filteredCount = 0;

            List<Sale> Sale_List = new List<Sale>();

            DataTable dt = Sale_DA.Filter_Get_Sale(iDisplayLength, iDisplayStart, iSortCol_0, sSortDir_0, sSearch);

            foreach (DataRow row in dt.Rows)
            {
                filteredCount = int.Parse(row["TotalCount"].ToString());

                Sale _Sale = new Sale();

                _Sale.ID = int.Parse(row["ID"].ToString());
                _Sale.Article_Code = row["Article_Code"].ToString();
                _Sale.Distinctive_Code = row["Distinctive_Code"].ToString();
                _Sale.Customer_Code = row["Customer_Code"].ToString();
                _Sale.Customer_Name = row["Customer_Name"].ToString();
                _Sale.Style = row["Style"].ToString();

                _Sale.Size = row["Size"].ToString();
                _Sale.Pair_Bar_Code = row["Pair_Bar_Code"].ToString();
                _Sale.Doz_Bar_Code = row["Doz_Bar_Code"].ToString();
                _Sale.Ctn_Bar_Code = row["Ctn_Bar_Code"].ToString();
                _Sale.Poly_Bag_Packing_String = row["Poly_Bag_Packing_String"].ToString();
                _Sale.Packing_Unit_Name = row["Packing_Unit_Name"].ToString();
                _Sale.Unit_Name = row["Unit_Name"].ToString();
                _Sale.CTN_Packing_string = row["CTN_Packing_string"].ToString();
                _Sale.CTN_Packing_Unit_Name = row["CTN_Packing_Unit_Name"].ToString();

                _Sale.Gross_Wt_Unit_Name = row["Gross_Wt_Unit_Name"].ToString();
                if (row["D1"].ToString() != "")
                    _Sale.D1 = float.Parse(row["D1"].ToString());
                if (row["D2"].ToString() != "")
                    _Sale.D2 = float.Parse(row["D2"].ToString());
                if (row["D3"].ToString() != "")
                    _Sale.D3 = float.Parse(row["D3"].ToString());
                if (row["CBM"].ToString() != "")
                    _Sale.CBM = float.Parse(row["CBM"].ToString());
                if (row["CBM28"].ToString() != "")
                    _Sale.CBM28 = float.Parse(row["CBM28"].ToString());
                if (row["CBM56"].ToString() != "")
                    _Sale.CBM56 = float.Parse(row["CBM56"].ToString());
                if (row["CBM65"].ToString() != "")
                    _Sale.CBM65 = float.Parse(row["CBM65"].ToString());
                if (row["CBM72"].ToString() != "")
                    _Sale.CBM72 = float.Parse(row["CBM72"].ToString());

                _Sale.ForEdit = row["ID"].ToString();
                _Sale.ForDelete = row["ID"].ToString();

                Sale_List.Add(_Sale);

            }
            var result = new
            {
                iTotalRecords = GetTotalCount(),
                iTotalDisplayRecords = filteredCount,
                aaData = Sale_List
            };

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(result));
        }
        public string Submit(Sale _Sale, Sale_B_KNITTED _Sale_B_KNITTED, List<Sale_B_WOVEN> list_Sale_B_WOVEN, Sale_B_NONWOVEN _Sale_B_NONWOVEN, Sale_B_TERRY _Sale_B_TERRY, Sale_B_KNITWRIST _Sale_B_KNITWRIST, Sale_B_LEATHER _Sale_B_LEATHER)
        {
            string msg = "";

            if (_Sale.ID == 0)
            {
                _Sale.CreatedBy = Session["User"].ToString();
                msg = Sale_DA.Insert(_Sale, _Sale_B_KNITTED, list_Sale_B_WOVEN, _Sale_B_NONWOVEN, _Sale_B_TERRY, _Sale_B_KNITWRIST, _Sale_B_LEATHER);

            }
            else
            {
                _Sale.UpdatedBy = Session["User"].ToString();
                msg = Sale_DA.Update(_Sale, _Sale_B_KNITTED, list_Sale_B_WOVEN, _Sale_B_NONWOVEN, _Sale_B_TERRY, _Sale_B_KNITWRIST, _Sale_B_LEATHER);
            }

            return msg;
        }
        public Sale Get(int ID)
        {
            Sale _Sale = new Sale();

            DataTable dt = Sale_DA.Get(ID);

            foreach (DataRow row in dt.Rows)
            {
                _Sale.ID = int.Parse(row["ID"].ToString());
                _Sale.Article_ID = int.Parse(row["Article_ID"].ToString());
                _Sale.Customer_ID = int.Parse(row["Customer_ID"].ToString());
                _Sale.Style = row["Style"].ToString();

                _Sale.Size = row["Size"].ToString();
                _Sale.Pair_Bar_Code = row["Pair_Bar_Code"].ToString();
                _Sale.Doz_Bar_Code = row["Doz_Bar_Code"].ToString();
                _Sale.Ctn_Bar_Code = row["Ctn_Bar_Code"].ToString();

                _Sale.LABLE1 = row["LABLE1"].ToString();
                _Sale.LABLE2 = row["LABLE2"].ToString();
                _Sale.PLCMNT1 = row["PLCMNT1"].ToString();
                _Sale.PLCMNT2 = row["PLCMNT2"].ToString();

                if (row["Poly_Bag_Packing"].ToString() != "")
                    _Sale.Poly_Bag_Packing = int.Parse(row["Poly_Bag_Packing"].ToString());
                if (row["Packing_Unit"].ToString() != "")
                    _Sale.Packing_Unit = int.Parse(row["Packing_Unit"].ToString());
                if (row["Net_Wt_PerUnit"].ToString() != "")
                    _Sale.Net_Wt_PerUnit = float.Parse(row["Net_Wt_PerUnit"].ToString());
                if (row["Unit"].ToString() != "")
                    _Sale.Unit = int.Parse(row["Unit"].ToString());
                if (row["CTN_Packing"].ToString() != "")
                    _Sale.CTN_Packing = int.Parse(row["CTN_Packing"].ToString());
                if (row["CTN_Packing_Unit"].ToString() != "")
                    _Sale.CTNPacking_Unit = int.Parse(row["CTN_Packing_Unit"].ToString());
                if (row["CTN_Unit"].ToString() != "")
                    _Sale.CTN_Unit = int.Parse(row["CTN_Unit"].ToString());

                if (row["Gross_Wt_PerCTN"].ToString() != "")
                    _Sale.Gross_Wt_PerCTN = float.Parse(row["Gross_Wt_PerCTN"].ToString());
                if (row["Gross_Wt_Unit"].ToString() != "")
                    _Sale.Gross_Wt_Unit = int.Parse(row["Gross_Wt_Unit"].ToString());

                if (row["D1"].ToString() != "")
                    _Sale.D1 = float.Parse(row["D1"].ToString());
                if (row["D2"].ToString() != "")
                    _Sale.D2 = float.Parse(row["D2"].ToString());
                if (row["D3"].ToString() != "")
                    _Sale.D3 = float.Parse(row["D3"].ToString());

            }
            return _Sale;
        }
        public static List<Sale> GetSales(DateTime initialDate, DateTime finalDate)
        {
            // Create an empty list of sales
            List<Sale> sales = new List<Sale>();

            //Initialize company
            if (!InitializeCompany())
                return sales;

            //DataDescarga always null ?
            StdBELista salesQuery = PriEngine.Engine.Consulta(
                "SELECT CabecDoc.Id AS CabecDocId, CabecDoc.Nome AS CabecDocNome, CabecDoc.Entidade AS CabecDocEntidade, CabecDoc.Moeda AS CabecDocMoeda, CabecDoc.TipoDoc AS CabecDocTipoDoc, CabecDoc.Data AS CabecDocData, CabecDoc.DataVencimento AS CabecDocDataVencimento, CabecDoc.DataCarga AS CabecDocDataCarga, CabecDoc.DataDescarga AS CabecDocsDataDescarga, " +
                "LinhasDoc.Id AS LinhasDocId, LinhasDoc.PrecoLiquido AS LinhasDocPrecoLiquido, " +
                "Artigo.Artigo AS ArtigoId, Artigo.Marca AS ArtigoMarca, Artigo.Modelo AS ArtigoModelo, Artigo.Descricao AS ArticoDescricao, Artigo.TipoArtigo AS ArtigoTipoArtigo, " +
                "Familias.Familia AS FamiliaId, Familias.Descricao AS FamiliaDescricao, " +
                "Iva.Taxa AS IvaTaxa " +
                "FROM CabecDoc " +
                "INNER JOIN LinhasDoc ON LinhasDoc.IdCabecDoc = CabecDoc.Id " +
                "INNER JOIN Artigo ON Artigo.Artigo = LinhasDoc.Artigo " +
                "INNER JOIN Familias ON Artigo.Familia = Familias.Familia " +
                "INNER JOIN Iva ON LinhasDoc.CodIva = Iva.Iva " +
                "WHERE CabecDoc.Data >= '" + initialDate.ToString("yyyyMMdd") + "' AND CabecDoc.Data <= '" + finalDate.ToString("yyyyMMdd") + "' " +
                "ORDER BY CabecDoc.Data"
                );

            while (!salesQuery.NoFim())
            {
                Sale sale = new Sale();

                sale.ID = salesQuery.Valor("LinhasDocId");
                sale.DocumentDate = ParseDate(salesQuery, "CabecDocData");
                sale.DocumentType = salesQuery.Valor("CabecDocTipoDoc");
                sale.DueDate = ParseDate(salesQuery, "CabecDocDataVencimento");
                sale.ReceptionDate = ParseDate(salesQuery, "CabecDocsDataDescarga");
                sale.ClientId = salesQuery.Valor("CabecDocEntidade");
                sale.ClientName = salesQuery.Valor("CabecDocNome");
                sale.Value = new Money(salesQuery.Valor("LinhasDocPrecoLiquido"), salesQuery.Valor("CabecDocMoeda"));
                sale.Iva = salesQuery.Valor("IvaTaxa") / 100.0;

                Product product = new Product();
                product.Id = salesQuery.Valor("ArtigoId");
                product.Brand = salesQuery.Valor("ArtigoMarca");
                product.Model = salesQuery.Valor("ArtigoModelo");
                product.Description = salesQuery.Valor("ArticoDescricao");
                product.FamilyId = salesQuery.Valor("FamiliaId");
                product.FamilyDescription = salesQuery.Valor("FamiliaDescricao");
                sale.Product = product;

                sales.Add(sale);

                // Next item:
                salesQuery.Seguinte();
            }

            return sales;
        }