Пример #1
0
        public List <CRMLead> crmLeadData()
        {
            String[] colourcodes = new String[] { "#3498db", "#e67e22", "#c0392b", "#2ecc71", "#d35400", "#27ae60", " #e74c3c", "#2980b9" };
            try
            {
                //JObject res = odooConnector.odooCrmLeadDataCall("sale.crm", "get_your_pipelines_all_orders");

                App.filterdict["range"] = "False";
                App.filterdict["days"]  = "False";
                App.filterdict["month"] = "True";

                //   odooConnector = OdooRPC.InstanceCreation(Settings.UserUrlName);
                JObject res = odooConnector.odooFilterDataCall("sale.crm", "get_your_pipelines_all_orders");

                //List<OrderLine> test = new List<OrderLine>();

                App.statedict   = res["state_list"].ToObject <Dictionary <int, string> >();
                App.countrydict = res["country_list"].ToObject <Dictionary <int, string> >();

                App.partner_name  = res["user_name"].ToString();
                App.partner_image = res["user_image_medium"].ToString();
                App.partner_id    = res["partner_id"].ToObject <int>();
                App.partner_email = res["user_email"].ToString();

                App.taxList     = res["taxes"].ToObject <List <taxes> >();
                App.paytermList = res["payment_terms"].ToObject <List <paytermList> >();

                App.commisiongroupList = res["commission_group"].ToObject <List <commisiongroupList> >();

                App.salesteam    = res["sales_team"].ToObject <Dictionary <int, string> >();
                App.salespersons = res["sales_persons"].ToObject <Dictionary <int, string> >();

                App.productList = res["Products"].ToObject <List <ProductsList> >();

                App.user_location_string = res["location"].ToString();

                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <UserModelDB>();


                if (App.UserListDb.Count == 0)
                {
                    var json_state = Newtonsoft.Json.JsonConvert.SerializeObject(res["state_list"]);

                    var json_country = Newtonsoft.Json.JsonConvert.SerializeObject(res["country_list"]);

                    var json_tags         = Newtonsoft.Json.JsonConvert.SerializeObject(res["crm_lead_tags"]);
                    var json_salesteam    = Newtonsoft.Json.JsonConvert.SerializeObject(res["sales_team"]);
                    var json_salespersons = Newtonsoft.Json.JsonConvert.SerializeObject(res["sales_persons"]);

                    var json_customers_list = Newtonsoft.Json.JsonConvert.SerializeObject(res["Customers"]);
                    var json_next_activity  = Newtonsoft.Json.JsonConvert.SerializeObject(res["next_activity"]);
                    var json_stages         = Newtonsoft.Json.JsonConvert.SerializeObject(res["stages"]);

                    var json_payment_terms = Newtonsoft.Json.JsonConvert.SerializeObject(res["payment_terms"]);

                    var json_commission_groups = Newtonsoft.Json.JsonConvert.SerializeObject(res["commission_group"]);

                    var jso_tags_list = Newtonsoft.Json.JsonConvert.SerializeObject(res["taxes"]);

                    var jso_products_list = Newtonsoft.Json.JsonConvert.SerializeObject(res["Products"]);


                    var sample = new UserModelDB
                    {
                        userid            = App.userid,
                        partnerid         = App.partner_id,
                        user_image_medium = App.partner_image,
                        user_email        = App.partner_email,
                        user_name         = App.partner_name,
                        state_list        = json_state,
                        country_list      = json_country,
                        tagsPicker        = json_tags,
                        sales_team        = json_salesteam,
                        sales_persons     = json_salespersons,
                        customers_list    = json_customers_list,
                        next_activity     = json_next_activity,
                        stages            = json_stages,
                        payment_terms     = json_payment_terms,
                        commission_group  = json_commission_groups,
                        tax_list          = jso_tags_list,
                        products          = jso_products_list
                    };
                    App._connection.Insert(sample);

                    try
                    {
                        var details = (from y in App._connection.Table <UserModelDB>() select y).ToList();

                        App.UserListDb = details;

                        List <ProductsList>      productslistdb = new List <ProductsList>();
                        Dictionary <int, string> cusdictdb      = new Dictionary <int, string>();
                        foreach (var item in App.UserListDb)
                        {
                            productslistdb = JsonConvert.DeserializeObject <List <ProductsList> >(item.products);

                            cusdictdb = JsonConvert.DeserializeObject <Dictionary <int, string> >(item.customers_list);

                            App.ProductListDb = productslistdb;
                            App.cusdictDb     = cusdictdb;
                            App.userid_db     = item.userid;
                        }
                    }


                    catch
                    {
                        int te = 0;
                    }

                    //List<ProductsList> productslistdb = new List<ProductsList>();
                    //foreach (var item in App.UserListDb)
                    //{
                    //    productslistdb = JsonConvert.DeserializeObject<List<ProductsList>>(item.products);

                    //    App.productList = productslistdb;
                    //}
                }



                App.nextActivityList = res["next_activity"].ToObject <List <next_activity> >();

                App.crmList = res["crm_leads"].ToObject <List <CRMLead> >();

                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <CRMLeadDB>();

                var crmlead_details = App._connection.Query <CRMLeadDB>("SELECT * from CRMLeadDB where yellowimg_string = ?", "yellowcircle.png");

                if (crmlead_details.Count() == 0)
                {
                    App._connection.Query <SalesQuotationDB>("DELETE from CRMLeadDB");

                    foreach (var item in App.crmList)
                    {
                        var sample = new CRMLeadDB
                        {
                            id               = item.id,
                            customer         = item.customer,
                            next_activity    = item.next_activity,
                            name             = item.name,
                            probability      = item.probability,
                            phone            = item.phone,
                            title_action     = item.title_action,
                            expected_revenue = item.expected_revenue,

                            team_name    = item.team_name,
                            priority     = item.priority,
                            state        = item.state,
                            street       = item.street,
                            street2      = item.street2,
                            city         = item.city,
                            country      = item.country,
                            contact_name = item.contact_name,
                            mobile       = item.mobile,
                            email        = item.email,
                            pipe_date    = item.pipe_date,
                            pipe_date1   = item.pipe_date1,
                            conDate      = item.conDate,
                            conDate1     = item.conDate1,
                            DateOrder    = item.DateOrder,
                            FullState    = item.FullState,

                            state_colour = item.state_colour,

                            //order_line = item.order_line[0].ToString()
                        };
                        App._connection.Insert(sample);


                        //App._samp.Add(sample);
                    }

                    try
                    {
                        var details = (from y in App._connection.Table <CRMLeadDB>() select y).ToList();

                        App.crmListDb = details;
                    }

                    catch
                    {
                        int te = 0;
                    }
                }

                App.crmOpprList = res["crm_quotations"].ToObject <List <CRMOpportunities> >();

                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <CRMOpportunitiesDB>();

                var crmoppo_details = App._connection.Query <CRMOpportunitiesDB>("SELECT * from CRMOpportunitiesDB where yellowimg_string = ?", "yellowcircle.png");

                if (crmoppo_details.Count == 0)
                {
                    App._connection.Query <SalesQuotationDB>("DELETE from CRMOpportunitiesDB");
                    foreach (var item in App.crmOpprList)
                    {
                        var sample = new CRMOpportunitiesDB
                        {
                            customer         = item.customer,
                            next_activity    = item.next_activity,
                            name             = item.name,
                            probability      = item.probability,
                            phone            = item.phone,
                            title_action     = item.title_action,
                            expected_revenue = item.expected_revenue,
                            team_name        = item.team_name,
                            priority         = item.priority,
                            state            = item.state,
                            street           = item.street,
                            street2          = item.street2,
                            city             = item.city,
                            country          = item.country,
                            contact_name     = item.contact_name,
                            mobile           = item.mobile,
                            email            = item.email,
                            FullState        = item.FullState,
                            state_colour     = item.state_colour,
                            pipe_date        = item.pipe_date,
                            //pipe_datetime = item.pipe_datetime,
                            //pipe_datetime1 = item.pipe_datetime1,
                            pipe_datetime  = item.pipe_datetime.ToString(),
                            pipe_datetime1 = item.pipe_datetime1.ToString(),
                            DateOrder      = item.DateOrder,
                            newpipe_date   = item.newpipe_date,
                        };
                        App._connection.Insert(sample);
                    }

                    try
                    {
                        var details = (from y in App._connection.Table <CRMOpportunitiesDB>() select y).ToList();

                        App.CRMOpportunitiesListDb = details;
                    }
                    catch
                    {
                        int te = 0;
                    }
                }

                App.salesQuotList = res["sale_quotations"].ToObject <List <SalesQuotation> >();

                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <SalesQuotationDB>();

                var sq_details = App._connection.Query <SalesQuotationDB>("SELECT * from SalesQuotationDB where yellowimg_string = ?", "yellowcircle.png");


                if (sq_details.Count() == 0)
                {
                    App._connection.Query <SalesQuotationDB>("DELETE from SalesQuotationDB");

                    foreach (var item in App.salesQuotList)
                    {
                        var json_orderline = Newtonsoft.Json.JsonConvert.SerializeObject(item.order_line);
                        var sample         = new SalesQuotationDB
                        {
                            customer = item.customer,
                            //    next_activity = item.next_activity,
                            name = item.name,
                            //  probability = item.probability,
                            // phone = item.phone,
                            //  title_action = item.title_action,
                            //  expected_revenue = item.expected_revenue,
                            payment_term = item.payment_term,
                            // team_name = item.team_name,
                            priority = item.priority,
                            state    = item.state,
                            street   = item.street,
                            street2  = item.street2,
                            city     = item.city,
                            country  = item.country,
                            // contact_name = item.contact_name,
                            //  mobile = item.mobile,
                            //  email = item.email,
                            // state_colour = item.state_colour,
                            sales_person       = item.sales_person,
                            sales_team         = item.sales_team,
                            customer_reference = item.customer_reference,
                            fiscal_position    = item.fiscal_position,
                            FullState          = item.FullState,
                            state_colour       = item.state_colour,
                            DateOrder          = item.DateOrder,
                            order_line         = json_orderline
                        };
                        App._connection.Insert(sample);
                        //App._samp.Add(sample);
                    }

                    try
                    {
                        var details = (from y in App._connection.Table <SalesQuotationDB>() select y).ToList();
                        App.SalesQuotationListDb = details;
                    }

                    catch
                    {
                        int te = 0;
                    }
                }


                App.promotionsList = res["promotions"].ToObject <List <ActivePromotions> >();

                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <ActivePromotionsDB>();

                //var promotion_details = App._connection.Query<ActivePromotionsDB>("SELECT * from ActivePromotionsDB");

                if (App.promotionsList.Count() != 0)
                {
                    App._connection.Query <ActivePromotionsDB>("DELETE from ActivePromotionsDB");

                    foreach (var item in App.promotionsList)
                    {
                        var dblist1     = Newtonsoft.Json.JsonConvert.SerializeObject(item.list1);
                        var dblist2     = Newtonsoft.Json.JsonConvert.SerializeObject(item.list2);
                        var dbtype_list = Newtonsoft.Json.JsonConvert.SerializeObject(item.type_list);

                        var sample = new ActivePromotionsDB
                        {
                            product      = item.product,
                            name         = item.name,
                            start_date   = item.Fstart_date,
                            end_date     = item.Fend_date,
                            type         = item.type,
                            type_list    = dbtype_list,
                            list1        = dblist1,
                            list2        = dblist2,
                            Fstart_date  = item.Fstart_date,
                            Fend_date    = item.Fend_date,
                            EndDateColor = item.EndDateColor,
                        };
                        App._connection.Insert(sample);
                        //App._samp.Add(sample);
                    }

                    try
                    {
                        var details = (from y in App._connection.Table <ActivePromotionsDB>() select y).ToList();
                        App.promotionsListDB = details;
                    }

                    catch
                    {
                        int te = 0;
                    }
                }


                App.salesOrderList = res["sale_orders"].ToObject <List <SalesOrder> >();


                App._connection = DependencyService.Get <ISQLiteDb>().GetConnection();
                App._connection.CreateTable <SalesOrderDB>();

                if (App.SalesOrderListDb.Count == 0)
                {
                    foreach (var item in App.salesOrderList)
                    {
                        var json_orderline = Newtonsoft.Json.JsonConvert.SerializeObject(item.order_line);

                        var sample = new SalesOrderDB
                        {
                            customer           = item.customer,
                            next_activity      = item.next_activity,
                            name               = item.name,
                            probability        = item.probability,
                            phone              = item.phone,
                            title_action       = item.title_action,
                            expected_revenue   = item.expected_revenue,
                            payment_term       = item.payment_term,
                            team_name          = item.team_name,
                            priority           = item.priority,
                            state              = item.state,
                            street             = item.street,
                            street2            = item.street2,
                            city               = item.city,
                            country            = item.country,
                            contact_name       = item.contact_name,
                            mobile             = item.mobile,
                            email              = item.email,
                            state_colour       = item.state_colour,
                            sales_person       = item.sales_person,
                            sales_team         = item.sales_team,
                            customer_reference = item.customer_reference,
                            fiscal_position    = item.fiscal_position,
                            FullState          = item.FullState,

                            //  ColorCode = item.ColorCode,

                            DateOrder = item.DateOrder,

                            order_line = json_orderline,
                        };
                        App._connection.Insert(sample);
                        //App._samp.Add(sample);
                    }

                    try
                    {
                        var details = (from y in App._connection.Table <SalesOrderDB>() select y).ToList();
                        App.SalesOrderListDb = details;
                    }
                    catch
                    {
                        int te = 0;
                    }
                }


                App.stageList = res["stages"].ToObject <List <stages> >();

                String colorCodeData = "";

                int cnt = 0;
                foreach (stages stateObj in res["stages"].ToObject <List <stages> >())
                {
                    try
                    {
                        colorCodeData = colorCodeData + "," + stateObj.name + "^" + colourcodes[cnt];
                    }
                    catch
                    {
                        cnt = 0;
                        continue;
                    }
                    cnt++;
                }

                Settings.StageColourCode = colorCodeData;
                App.cusdict = res["Customers"].ToObject <Dictionary <int, string> >();

                App.reasondict = res["lost_reason"].ToObject <Dictionary <int, string> >();

                App.crmleadtags = res["crm_lead_tags"].ToObject <Dictionary <int, string> >();

                App.user_gps_enabled = res["user_gps_enabled"].ToObject <Boolean>();
                App.user_gps_time    = res["user_gps_time"].ToObject <int>();

                App.locationsList = res["Location"].ToObject <List <LocationsList> >();

                return(App.crmList);
            }
            catch (Exception ea)
            {
                System.Diagnostics.Debug.WriteLine("::::: CRM Warning Message ::::  " + ea.Message);

                if (ea.Message.Contains("(Network is unreachable)") || ea.Message.Contains("NameResolutionFailure"))
                {
                    App.NetAvailable = false;
                }

                else if (ea.Message.Contains("(503) Service Unavailable"))
                {
                    App.responseState = false;
                }
                return(App.crmList);
            }
        }
Пример #2
0
        public SalesOrderListviewDetail(SalesOrderDB item)
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);

            //    App.orderLineList = item.order_line;

            Cus.Text = item.customer;
            CD.Text  = item.DateOrder;
            PT.Text  = item.payment_term;
            SP.Text  = item.sales_person;
            ST.Text  = item.sales_team;
            CR.Text  = item.customer_reference;
            FP.Text  = item.fiscal_position;

            List <OrderLine> or_linelistdb = new List <OrderLine>();


            var json_orderline = JsonConvert.SerializeObject(item.order_line);

            String convertstring = json_orderline.ToString();

            //  "\"[{\\\"customer_lead\\\":\\\"0\\\",\\\"price_unit\\\":\\\"10000\\\",\\\"product_uom_qty\\\":\\\"10\\\",\\\"price_subtotal\\\":\\\"100000\\\",\\\"taxes\\\":[],\\\"product_name\\\":\\\"Floordeck 1000x060 MM\\\"},{\\\"customer_lead\\\":\\\"0\\\",\\\"price_unit\\\":\\\"1\\\",\\\"product_uom_qty\\\":\\\"1\\\",\\\"price_subtotal\\\":\\\"1\\\",\\\"taxes\\\":[\\\"Sales Tax N/A SRCA-S\\\"],\\\"product_name\\\":\\\"Floordeck 1000x060 MM\\\"}]\""

            String finstring = convertstring.Replace("\\", "");

            finstring = finstring.Substring(1);

            finstring = finstring.Remove(finstring.Length - 1);

            JArray stringres = JsonConvert.DeserializeObject <JArray>(finstring);

            //  OrderLine stringres = JsonConvert.DeserializeObject<OrderLine>(json_orderline)

            int    cus_lead  = 0;
            string prod_name = "";



            foreach (JObject obj in stringres)
            {
                OrderLine or_line = new OrderLine();


                or_line.product_name    = obj["product_name"].ToString();
                or_line.product_uom_qty = obj["product_uom_qty"].ToString();
                or_line.price_subtotal  = obj["price_subtotal"].ToString();
                try
                {
                    or_line.tax_names = obj["tax_names"].ToString();
                }
                catch
                {
                    or_line.tax_names = "";
                }

                try
                {
                    or_line.discount = obj["discount"].ToString();
                }

                catch
                {
                    or_line.discount = "";
                }

                or_linelistdb.Add(or_line);
            }


            orderListview.ItemsSource = or_linelistdb;


            //var backImgRecognizer = new TapGestureRecognizer();
            //backImgRecognizer.Tapped += async (s, e) => {
            //    // handle the tap

            //    var currentpage = new LoadingAlert();
            //    await PopupNavigation.PushAsync(currentpage);

            //    // Navigation.PopAllPopupAsync();
            //    App.Current.MainPage = new MasterPage(new CrmTabbedPage());
            //    //  orderListview.ItemsSource = null;
            //    Loadingalertcall();

            //};
            //backImg.GestureRecognizers.Add(backImgRecognizer);
        }
Пример #3
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (project != null)
            {
                toolBar             toolBar          = new cntrl.toolBar();
                List <project_task> project_taskLIST = project.project_task.Where(x => x.IsSelected).ToList();

                sales_order sales_order = new entity.sales_order();
                if (Generate_Budget)
                {
                    SalesBudgetDB SalesBudgetDB = new SalesBudgetDB();

                    sales_budget sales_budget = new entity.sales_budget();

                    sales_budget.id_contact = (int)project.id_contact;
                    sales_budget.contact    = SalesBudgetDB.contacts.Where(x => x.id_contact == (int)project.id_contact).FirstOrDefault();

                    sales_budget.id_project    = project.id_project;
                    sales_budget.id_condition  = (int)cbxCondition.SelectedValue;
                    sales_budget.id_contract   = (int)cbxContract.SelectedValue;
                    sales_budget.id_currencyfx = (int)cbxCurrency.SelectedValue;
                    sales_budget.comment       = "Project -> " + project.name;
                    sales_budget.trans_date    = DateTime.Now;

                    foreach (project_task _project_task in project_taskLIST)
                    {
                        sales_budget_detail sales_budget_detail = new sales_budget_detail();
                        sales_budget_detail.State            = EntityState.Added;
                        sales_budget_detail.id_sales_budget  = sales_budget.id_sales_budget;
                        sales_budget_detail.sales_budget     = sales_budget;
                        sales_budget_detail.id_item          = (int)_project_task.id_item;
                        sales_budget_detail.item_description = _project_task.item_description;

                        if (project.is_Executable)
                        {
                            sales_budget_detail.quantity = (decimal)_project_task.quantity_exe;
                        }
                        else
                        {
                            sales_budget_detail.quantity = (decimal)_project_task.quantity_est;
                        }


                        if (_project_task.unit_price_vat != null)
                        {
                            sales_budget_detail.UnitPrice_Vat = (decimal)_project_task.unit_price_vat;
                        }

                        sales_budget_detail.id_project_task = _project_task.id_project_task;
                        _project_task.IsSelected            = false;

                        sales_budget.sales_budget_detail.Add(sales_budget_detail);
                    }

                    sales_budget.State      = EntityState.Added;
                    sales_budget.IsSelected = true;
                    SalesBudgetDB.sales_budget.Add(sales_budget);
                    SalesBudgetDB.SaveChanges();
                }
                else
                {
                    //if (Generate_Budget)
                    //{
                    //    sales_order.id_sales_budget = sales_budget.id_sales_budget;
                    //}
                    if (SalesOrderDB.contacts.Where(x => x.id_contact == (int)project.id_contact).FirstOrDefault() != null)
                    {
                        sales_order.id_contact = (int)project.id_contact;
                        sales_order.contact    = SalesOrderDB.contacts.Where(x => x.id_contact == (int)project.id_contact).FirstOrDefault();
                    }
                    else
                    {
                        toolBar.msgWarning("Contact Not Found...");
                        return;
                    }

                    if (Generate_Invoice)
                    {
                        if (cbxDocument.SelectedValue != null)
                        {
                            sales_order.id_range = (int)cbxDocument.SelectedValue;
                        }
                        else
                        {
                            toolBar.msgWarning("Document Range Needed for Approval");
                            // return;
                        }
                    }
                    sales_order.id_project = project.id_project;

                    if (Convert.ToInt16(cbxCondition.SelectedValue) > 0)
                    {
                        sales_order.id_condition = (int)cbxCondition.SelectedValue;
                    }
                    else
                    {
                        toolBar.msgWarning("Condition Not Found...");
                        return;
                    }

                    if (Convert.ToInt16(cbxContract.SelectedValue) > 0)
                    {
                        sales_order.id_contract = (int)cbxContract.SelectedValue;
                    }
                    else
                    {
                        toolBar.msgWarning("Contract Not Found...");
                        return;
                    }

                    if (Convert.ToInt16(cbxCurrency.SelectedValue) > 0)
                    {
                        sales_order.id_currencyfx = (int)cbxCurrency.SelectedValue;
                    }
                    else
                    {
                        toolBar.msgWarning("Currency Not Found...");
                        return;
                    }

                    sales_order.comment = "Project -> " + project.name;

                    sales_order_detail sales_order_detail = null;

                    foreach (project_task _project_task in project_taskLIST)
                    {
                        if (_project_task.items.id_item_type == item.item_type.Task || _project_task.sales_detail == null)
                        {
                            sales_order_detail                = new sales_order_detail();
                            sales_order_detail.State          = EntityState.Added;
                            sales_order_detail.id_sales_order = sales_order.id_sales_order;
                            sales_order_detail.sales_order    = sales_order;
                            if (Convert.ToInt16(_project_task.id_item) > 0)
                            {
                                sales_order_detail.id_item = (int)_project_task.id_item;
                            }

                            sales_order_detail.item_description = _project_task.item_description;

                            if (project.is_Executable)
                            {
                                sales_order_detail.quantity = (decimal)_project_task.quantity_exe;
                            }
                            else
                            {
                                sales_order_detail.quantity = (decimal)_project_task.quantity_est;
                            }


                            if (_project_task.unit_price_vat != null)
                            {
                                sales_order_detail.UnitPrice_Vat = (decimal)_project_task.unit_price_vat;
                            }

                            sales_order_detail.id_project_task = _project_task.id_project_task;
                            _project_task.IsSelected           = false;
                            _project_task.sales_detail         = sales_order_detail;
                        }

                        if (sales_order_detail != null)
                        {
                            sales_order.sales_order_detail.Add(sales_order_detail);
                        }
                    }

                    if (sales_order.sales_order_detail.Count() > 0)
                    {
                        sales_order.State      = EntityState.Added;
                        sales_order.IsSelected = true;

                        SalesOrderDB.sales_order.Add(sales_order);
                        SalesOrderDB.SaveChanges();

                        if (Generate_Invoice)
                        {
                            SalesOrderDB.Approve();
                            sales_invoice sales_invoice = new entity.sales_invoice();

                            if (SalesOrderDB.app_document_range.Where(x => x.app_document.id_application == App.Names.SalesBudget).FirstOrDefault() != null)
                            {
                                sales_invoice.id_range = SalesOrderDB.app_document_range.Where(x => x.app_document.id_application == App.Names.SalesInvoice).FirstOrDefault().id_range;
                            }
                            sales_invoice.id_contact    = (int)project.id_contact;
                            sales_invoice.contact       = SalesOrderDB.contacts.Where(x => x.id_contact == (int)project.id_contact).FirstOrDefault();
                            sales_invoice.sales_order   = sales_order;
                            sales_invoice.id_project    = project.id_project;
                            sales_invoice.id_condition  = (int)cbxCondition.SelectedValue;
                            sales_invoice.id_contract   = (int)cbxContract.SelectedValue;
                            sales_invoice.id_currencyfx = (int)cbxCurrency.SelectedValue;
                            sales_invoice.comment       = "Project -> " + project.name;
                            sales_invoice.trans_date    = DateTime.Now;
                            sales_invoice_detail sales_invoice_detail = null;

                            foreach (project_task _project_task in project_taskLIST)
                            {
                                sales_invoice_detail                  = new sales_invoice_detail();
                                sales_invoice_detail.State            = EntityState.Added;
                                sales_invoice_detail.id_sales_invoice = sales_invoice.id_sales_invoice;
                                sales_invoice_detail.sales_invoice    = sales_invoice;
                                sales_invoice_detail.id_item          = (int)_project_task.id_item;
                                sales_invoice_detail.item_description = _project_task.item_description;

                                if (project.is_Executable)
                                {
                                    sales_invoice_detail.quantity = (decimal)_project_task.quantity_exe;
                                }
                                else
                                {
                                    sales_invoice_detail.quantity = (decimal)_project_task.quantity_est;
                                }

                                if (_project_task.unit_price_vat != null)
                                {
                                    sales_invoice_detail.UnitPrice_Vat = (decimal)_project_task.unit_price_vat;
                                }

                                sales_invoice_detail.id_project_task = _project_task.id_project_task;
                                _project_task.IsSelected             = false;

                                sales_invoice.sales_invoice_detail.Add(sales_invoice_detail);
                            }

                            sales_invoice.State      = EntityState.Added;
                            sales_invoice.IsSelected = true;
                            crm_opportunity crm_opportunity = sales_order.crm_opportunity;
                            crm_opportunity.sales_invoice.Add(sales_invoice);
                            SalesOrderDB.crm_opportunity.Attach(crm_opportunity);
                            SalesOrderDB.sales_invoice.Add(sales_invoice);
                        }
                    }
                }

                SalesOrderDB.SaveChanges();
                btnCancel_Click(null, null);
            }
        }