Exemple #1
0
        private void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            purchase_invoice purchase_invoice = (purchase_invoice)purchase_invoiceDataGrid.SelectedItem;

            //Contract
            if (cbxCondition.SelectedItem != null)
            {
                app_condition app_condition = cbxCondition.SelectedItem as app_condition;
                cbxContract.ItemsSource = PurchaseInvoiceDB.app_contract.Where(a => a.is_active == true &&
                                                                               a.id_company == CurrentSession.Id_Company &&
                                                                               a.id_condition == app_condition.id_condition).ToList();
                //Selects first Item
                if (purchase_invoice != null)
                {
                    if (purchase_invoice.id_contract == 0)
                    {
                        cbxContract.SelectedIndex = 0;
                    }
                    else
                    {
                        cbxContract.SelectedValue = purchase_invoice.id_contract;
                    }
                }
            }
        }
Exemple #2
0
        private void id_conditionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            sales_budget sales_budget = sales_budgetDataGrid.SelectedItem as sales_budget;

            if (cbxCondition.SelectedItem != null)
            {
                app_condition app_condition = cbxCondition.SelectedItem as app_condition;

                CollectionViewSource contractViewSource = ((CollectionViewSource)(FindResource("contractViewSource")));
                contractViewSource.View.Filter = i =>
                {
                    app_contract objContract = (app_contract)i;
                    if (objContract.id_condition == app_condition.id_condition)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                };
                if (sales_budget != null)
                {
                    if (sales_budget.id_contract == 0)
                    {
                        cbxContract.SelectedIndex = 0;
                    }
                }
            }
        }
Exemple #3
0
 private void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (cbxCondition.SelectedItem != null)
     {
         app_condition app_condition = cbxCondition.SelectedItem as app_condition;
         cbxContract.ItemsSource = SalesOrderDB.app_contract.Where(a => a.is_active == true &&
                                                                   a.id_company == entity.Properties.Settings.Default.company_ID &&
                                                                   a.id_condition == app_condition.id_condition).ToList();
         cbxContract.SelectedIndex = 0;
     }
 }
Exemple #4
0
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            crud_modal.Visibility = System.Windows.Visibility.Visible;
            cntrl.condition objCon            = new cntrl.condition();
            app_condition   obj_app_condition = new app_condition();

            entity.db.app_condition.Add(obj_app_condition);
            conditionViewSource.View.MoveCurrentToLast();
            objCon.conditionViewSource = conditionViewSource;
            objCon.entity = entity;
            crud_modal.Children.Add(objCon);
        }
Exemple #5
0
 private void btnDelete_Click(object sender, RoutedEventArgs e)
 {
     if (!isExternalCall)
     {
         MessageBoxResult res = MessageBox.Show("Are you sure want to Delete?", "Cognitivo", MessageBoxButton.YesNo, MessageBoxImage.Question);
         if (res == MessageBoxResult.Yes)
         {
             app_condition objCon = conditionViewSource.View.CurrentItem as app_condition;
             objCon.is_active = false;
             btnSave_Click(sender, e);
         }
     }
 }
Exemple #6
0
        public app_contract GenerateDefaultContrat(app_condition app_condition, int interval)
        {
            app_contract app_contract = new app_contract();

            app_contract.app_condition = app_condition;
            app_contract.name          = interval.ToString() + " Días";
            app_contract_detail _app_contract_detail = new app_contract_detail();

            _app_contract_detail.coefficient  = 1;
            _app_contract_detail.app_contract = app_contract;
            _app_contract_detail.interval     = (short)interval;
            _app_contract_detail.is_order     = false;
            app_contract.app_contract_detail.Add(_app_contract_detail);
            return(app_contract);
        }
Exemple #7
0
        private void sync_Condition()
        {
            app_condition app_condition = new app_condition();

            app_condition.name       = "Contado";
            app_condition.id_company = id_company;
            app_condition.is_active  = true;
            dbContext.app_condition.Add(app_condition);

            app_condition app_conditionCred = new app_condition();

            app_conditionCred.name       = "Crédito";
            app_conditionCred.id_company = id_company;
            app_conditionCred.is_active  = true;
            dbContext.app_condition.Add(app_conditionCred);
            dbContext.SaveChanges();
        }
Exemple #8
0
        private async void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            sales_invoice sales_invoice = (sales_invoice)sales_invoiceDataGrid.SelectedItem;

            //Contract
            if (cbxCondition.SelectedItem != null)
            {
                app_condition app_condition = cbxCondition.SelectedItem as app_condition;
                cbxContract.ItemsSource = await SalesInvoiceDB.app_contract.Where(a => a.is_active == true &&
                                                                                  a.id_company == CurrentSession.Id_Company &&
                                                                                  a.id_condition == app_condition.id_condition).ToListAsync();

                if (sales_invoice != null)
                {
                    if (sales_invoice.id_contract == 0)
                    {
                        cbxContract.SelectedIndex = 0;
                    }
                }
            }
        }
Exemple #9
0
 private void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     //Contract
     if (cbxCondition.SelectedItem != null)
     {
         app_condition app_condition = cbxCondition.SelectedItem as app_condition;
         contractViewSource.View.Filter = i =>
         {
             app_contract objContract = (app_contract)i;
             if (objContract.id_condition == app_condition.id_condition)
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         };
         cbxContract.SelectedIndex = 0;
     }
 }
Exemple #10
0
        private void id_conditionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            app_condition app_condition = id_conditionComboBox.SelectedItem as app_condition;

            if (app_condition != null)
            {
                contractViewSource.View.Filter = a =>
                {
                    app_contract app_contract = a as app_contract;
                    if (app_contract.id_condition == app_condition.id_condition)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                };
                id_contractComboBox.SelectedIndex = 0;
            }
        }
Exemple #11
0
        private void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            sales_order sales_order = (sales_order)sales_orderDataGrid.SelectedItem;

            //Contract
            if (cbxCondition.SelectedItem != null)
            {
                app_condition app_condition = cbxCondition.SelectedItem as app_condition;
                if (app_condition != null)
                {
                    cbxContract.ItemsSource = CurrentSession.Get_Contract().Where(x => x.id_condition == app_condition.id_condition).ToList();
                }

                if (sales_order != null)
                {
                    if (sales_order.id_contract == 0)
                    {
                        cbxContract.SelectedIndex = 0;
                    }
                }
            }
        }
Exemple #12
0
        private async void cbxCondition_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            purchase_order purchase_order = (purchase_order)purchase_orderDataGrid.SelectedItem;

            //Contract
            if (cbxCondition.SelectedItem != null)
            {
                app_condition app_condition = cbxCondition.SelectedItem as app_condition;
                cbxContract.ItemsSource = await PurchaseOrderDB.app_contract.Where(a => a.is_active == true &&
                                                                                   a.id_company == CurrentSession.Id_Company &&
                                                                                   a.id_condition == app_condition.id_condition).ToListAsync();

                //Selects first Item
                if (purchase_order != null)
                {
                    if (purchase_order.id_contract == 0)
                    {
                        cbxContract.SelectedIndex = 0;
                    }
                }
            }
        }
Exemple #13
0
        public void purchase()
        {
            string sql = " SELECT " +
            " COMPRAS.CODCOMPRA, " +        //0
            " COMPRAS.NUMCOMPRA, " +        //1
            " COMPRAS.FECHACOMPRA, " +      //2
            " COMPRAS.TOTALDESCUENTO, " +   //3
            " COMPRAS.TOTALEXENTA, " +      //4
            " COMPRAS.TOTALGRAVADA, " +     //5
            " COMPRAS.TOTALIVA, " +         //6
            " COMPRAS.MODALIDADPAGO, " +    //7
            " COMPRAS.FECGRA, " +           //8
            " COMPRAS.ESTADO, " +           //9
            " COMPRAS.MOTIVOANULADO, " +    //10
            " COMPRAS.FECHACOMPRA, " +      //11
            " COMPRAS.TIMBRADOPROV, " +     //12
            " COMPRAS.TOTALIVA5, " +        //13
            " COMPRAS.TOTALIVA10, " +       //14
            " COMPRAS.METODO, " +           //15
            " COMPRAS.TOTALGRAVADO5, " +    //16
            " COMPRAS.TOTALGRAVADO10, " +   //17
            " COMPRAS.ASENTADO, " +         //18
            " COMPRAS.TOTALCOMPRA, " +      //19
            " PROVEEDOR.NOMBRE, " +         //20
            " PROVEEDOR.RUC_CIN, " +        //21
            " dbo.SUCURSAL.DESSUCURSAL, " +//28
            " COMPRAS.COTIZACION1, " +
            " dbo.FACTURAPAGAR.FECHAVCTO" +
            " FROM  COMPRAS RIGHT OUTER JOIN " +
            " PROVEEDOR ON COMPRAS.CODPROVEEDOR = PROVEEDOR.CODPROVEEDOR" +
            " LEFT OUTER JOIN FACTURAPAGAR ON COMPRAS.CODCOMPRA = FACTURAPAGAR.CODCOMPRA" +
             " RIGHT OUTER JOIN SUCURSAL ON COMPRAS.CODSUCURSAL = SUCURSAL.CODSUCURSAL";

            SqlConnection conn = new SqlConnection(_connString);

            //Counts Total number of Rows we have to process
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.CommandText = "SELECT COUNT(*) FROM COMPRAS";
            cmd.CommandType = CommandType.Text;
            int count = (int)cmd.ExecuteScalar();
            conn.Close();

            int value = 0;

            Dispatcher.BeginInvoke((Action)(() => purchaseMaximum.Text = count.ToString()));
            Dispatcher.BeginInvoke((Action)(() => purchaseValue.Text = value.ToString()));
            Dispatcher.BeginInvoke((Action)(() => progPurchase.Maximum = count));
            Dispatcher.BeginInvoke((Action)(() => progPurchase.Value = value));

            cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.CommandType = CommandType.Text;
            DataTable dt_purchase = exeDT(sql);
            //SqlDataReader reader = cmd.ExecuteReader();

            foreach (DataRow purchaserow in dt_purchase.Rows)
            {
                using (PurchaseInvoiceDB db = new PurchaseInvoiceDB())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;

                    purchase_invoice purchase_invoice = db.New();

                    purchase_invoice.number = purchaserow["NUMCOMPRA"] is DBNull ? null : purchaserow["NUMCOMPRA"].ToString();
                    if (!(purchaserow["FECHACOMPRA"] is DBNull))
                    {
                        purchase_invoice.trans_date = Convert.ToDateTime(purchaserow["FECHACOMPRA"]);
                    }
                    else
                    {
                        continue;
                    }

                    //Supplier
                    if (!(purchaserow["NOMBRE"] is DBNull))
                    {
                        string _customer = purchaserow["NOMBRE"].ToString();
                        contact contact = db.contacts.Where(x => x.name == _customer && x.id_company == id_company).FirstOrDefault();
                        purchase_invoice.id_contact = contact.id_contact;
                    }

                    //Condition (Cash or Credit)
                    if (!(purchaserow["MODALIDADPAGO"] is DBNull) && Convert.ToInt32(purchaserow["MODALIDADPAGO"]) == 0)
                    {
                        app_condition app_condition = db.app_condition.Where(x => x.name == "Contado").FirstOrDefault();
                        purchase_invoice.id_condition = app_condition.id_condition;
                        //Contract...

                        app_contract_detail app_contract_detail = db.app_contract_detail.Where(x => x.app_contract.id_condition == purchase_invoice.id_condition && x.app_contract.id_company == id_company).FirstOrDefault();
                        if (app_contract_detail != null)
                        {
                            purchase_invoice.id_contract = app_contract_detail.id_contract;
                        }
                        else
                        {
                            app_contract app_contract = GenerateDefaultContrat(app_condition, 0);
                            db.app_contract.Add(app_contract);

                            purchase_invoice.app_contract = app_contract;
                            purchase_invoice.id_contract = app_contract.id_contract;
                        }
                    }
                    else if (!(purchaserow["MODALIDADPAGO"] is DBNull) && Convert.ToInt32(purchaserow["MODALIDADPAGO"]) == 1)
                    {
                        app_condition app_condition = db.app_condition.Where(x => x.name == "Crédito" && x.id_company == id_company).FirstOrDefault();
                        purchase_invoice.id_condition = app_condition.id_condition;
                        //Contract...
                        if (!(purchaserow["FECHAVCTO"] is DBNull))
                        {
                            DateTime _due_date = Convert.ToDateTime(purchaserow["FECHAVCTO"]);
                            int interval = (_due_date - purchase_invoice.trans_date).Days;
                            app_contract_detail app_contract_detail = db.app_contract_detail.Where(x => x.app_contract.id_condition == purchase_invoice.id_condition && x.app_contract.id_company == id_company && x.interval == interval).FirstOrDefault();
                            if (app_contract_detail != null)
                            {
                                purchase_invoice.id_contract = app_contract_detail.id_contract;
                            }
                            else
                            {
                                app_contract app_contract = GenerateDefaultContrat(app_condition, interval);
                                db.app_contract.Add(app_contract);

                                purchase_invoice.app_contract = app_contract;
                                purchase_invoice.id_contract = app_contract.id_contract;
                            }
                        }
                    }

                    int id_location = 0;
                    //Branch
                    if (!(purchaserow["DESSUCURSAL"] is DBNull))
                    {
                        //Branch
                        string _branch = purchaserow["DESSUCURSAL"].ToString();
                        app_branch app_branch = db.app_branch.Where(x => x.name == _branch && x.id_company == id_company).FirstOrDefault();
                        purchase_invoice.id_branch = app_branch.id_branch;

                        //Location
                        id_location = db.app_location.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault().id_location;

                        //Terminal
                        purchase_invoice.id_terminal = db.app_terminal.Where(x => x.app_branch.id_branch == app_branch.id_branch).FirstOrDefault().id_terminal;
                    }

                    string _desMoneda = string.Empty;

                    //Sales Invoice Detail
                    string sqlDetail = "SELECT"
                    + " dbo.PRODUCTOS.DESPRODUCTO as ITEM_DESPRODUCTO," //0
                    + " dbo.COMPRASDETALLE.DESPRODUCTO," //1
                    + " dbo.COMPRASDETALLE.CANTIDADCOMPRA, " //2
                    + " dbo.COMPRASDETALLE.COSTOUNITARIO, " //3
                    + " dbo.COMPRASDETALLE.IVA, " //4
                    + " dbo.COMPRAS.COTIZACION1, " //5
                    + " dbo.MONEDA.DESMONEDA " //6
                    + " FROM dbo.COMPRAS LEFT OUTER JOIN"
                    + " dbo.MONEDA ON dbo.COMPRAS.CODMONEDA = dbo.MONEDA.CODMONEDA LEFT OUTER JOIN"
                    + " dbo.COMPRASDETALLE ON dbo.COMPRAS.CODCOMPRA = dbo.COMPRASDETALLE.CODCOMPRA LEFT OUTER JOIN"
                    + " dbo.PRODUCTOS ON dbo.COMPRASDETALLE.CODPRODUCTO = dbo.PRODUCTOS.CODPRODUCTO"
                    + " WHERE (dbo.COMPRASDETALLE.CODCOMPRA = " + purchaserow["CODCOMPRA"].ToString() + ")";

                    DataTable dt = exeDT(sqlDetail);
                    foreach (DataRow row in dt.Rows)
                    {
                        //db Related Insertion.
                        purchase_invoice.id_currencyfx = 1;

                        purchase_invoice_detail purchase_invoice_detail = new purchase_invoice_detail();

                        string _prod_Name = row["ITEM_DESPRODUCTO"].ToString();
                        if (db.items.Where(x => x.name == _prod_Name && x.id_company == id_company).FirstOrDefault() != null)
                        {
                            //Only if Item Exists
                            item item = db.items.Where(x => x.name == _prod_Name && x.id_company == id_company).FirstOrDefault();
                            purchase_invoice_detail.id_item = item.id_item;
                        }

                        if (row["DESPRODUCTO"] is DBNull)
                        {
                            //If not Item Description, then just continue out of this loop.
                            continue;
                        }

                        purchase_invoice_detail.item_description = row["DESPRODUCTO"].ToString();
                        purchase_invoice_detail.quantity = Convert.ToDecimal(row["CANTIDADCOMPRA"]);

                        purchase_invoice_detail.id_location = id_location;

                        string _iva = row["IVA"].ToString();
                        if (_iva == "10.00")
                        {
                            purchase_invoice_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "10%").FirstOrDefault().id_vat_group;
                        }
                        else if (_iva == "5.00")
                        {
                            purchase_invoice_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "5%").FirstOrDefault().id_vat_group;
                        }
                        else
                        {
                            if (db.app_vat_group.Where(x => x.name == "Excento").FirstOrDefault() != null)
                            {
                                purchase_invoice_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "Excento").FirstOrDefault().id_vat_group;
                            }
                        }

                        decimal cotiz1 = Convert.ToDecimal((row["COTIZACION1"] is DBNull) ? 1 : Convert.ToDecimal(row["COTIZACION1"]));
                        // purchase_invoice_detail.unit_price = (Convert.ToDecimal(row["PRECIOVENTANETO"]) / purchase_invoice_detail.quantity) / cotiz1;

                        if (row["COSTOUNITARIO"] is DBNull)
                        {
                            purchase_invoice_detail.unit_cost = 0;
                        }
                        else
                        {
                            purchase_invoice_detail.unit_cost = Convert.ToDecimal(row["COSTOUNITARIO"]);
                        }
                        //Commit Sales Invoice Detail
                        purchase_invoice.purchase_invoice_detail.Add(purchase_invoice_detail);
                    }

                    if (purchase_invoice.Error == null)
                    {
                        try
                        {
                            purchase_invoice.State = System.Data.Entity.EntityState.Added;
                            purchase_invoice.IsSelected = true;

                            // db.purchase_invoice.Add(purchase_invoice);
                            IEnumerable<DbEntityValidationResult> validationresult = db.GetValidationErrors();
                            if (validationresult.Count() == 0)
                            {
                                db.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                        //Sales Brillo
                        //if (reader.GetInt32(10) == 1)
                        //{
                        //    entity.Brillo.Approve.SalesInvoice salesBrillo = new entity.Brillo.Approve.SalesInvoice();
                        //    salesBrillo.Start(ref db, sales_invoice);
                        //    sales_invoice.status = 0; ?????
                        //}
                        //else if (reader.GetInt32(10))
                        //{
                        //    entity.Brillo.Approve.SalesInvoice salesBrillo = new entity.Brillo.Approve.SalesInvoice();
                        //    salesBrillo.Start(ref db, sales_invoice);
                        //    entity.Brillo.Annul.SalesInvoice salesAnullBrillo = new entity.Brillo.Annul.SalesInvoice();
                        //    salesAnullBrillo.Start(ref db, sales_invoice);
                        //    sales_invoice.status = 0; ?????
                        //}

                        value += 1;
                        Dispatcher.BeginInvoke((Action)(() => progPurchase.Value = value));
                        Dispatcher.BeginInvoke((Action)(() =>purchaseValue.Text = value.ToString()));
                    }
                    else
                    {
                        //Add code to include error contacts into
                        purchase_invoice_ErrorList.Add(purchase_invoice);
                    }
                }
            }
               // reader.Close();
            cmd.Dispose();
            conn.Close();

            _customer_Current = _customer_Max;
        }
Exemple #14
0
        public void sales()
        {
            string sql = " SELECT "
                         + " dbo.VENTAS.CODVENTA, dbo.VENTAS.NUMVENTA, dbo.VENTAS.FECHAVENTA, dbo.VENTAS.PORCENTAJEDESCUENTO,"
                         + " dbo.VENTAS.TOTALEXENTA, dbo.VENTAS.TOTALGRAVADA, dbo.VENTAS.TOTALIVA, dbo.VENTAS.TOTALDESCUENTO,"
                         + " dbo.VENTAS.MODALIDADPAGO, dbo.VENTAS.FECGRA, dbo.VENTAS.ESTADO, dbo.VENTAS.MOTIVOANULADO,"
                         + " dbo.VENTAS.FECHAANULADO, dbo.VENTAS.TIPOVENTA, dbo.VENTAS.TIPOPRECIO, dbo.VENTAS.NUMVENTATIMBRADO,"
                         + " dbo.VENTAS.TOTAL5, dbo.VENTAS.TOTAL10, dbo.VENTAS.CODPRESUPUESTO, dbo.VENTAS.METODO, dbo.VENTAS.ENVIADO,"
                         + " dbo.VENTAS.TOTALGRAVADO5, dbo.VENTAS.TOTALGRAVADO10, dbo.VENTAS.ASENTADO,"
                         + " dbo.VENTAS.TOTALVENTA, dbo.VENDEDOR.DESVENDEDOR, dbo.CLIENTES.NOMBRE, dbo.CLIENTES.RUC,"
                         + " dbo.SUCURSAL.DESSUCURSAL, dbo.VENTAS.COTIZACION1, FACTURACOBRAR_1.FECHAVCTO,"
                         + " dbo.FACTURACOBRAR.FECHAVCTO AS Expr1, dbo.FACTURACOBRAR.SALDOCUOTA, dbo.FACTURACOBRAR.IMPORTECUOTA, "
                         + " dbo.FACTURACOBRAR.COTIZACION, dbo.VENTASFORMACOBRO.IMPORTE, dbo.VENTASFORMACOBRO.DESTIPOCOBRO,"
                         + " dbo.VENTASFORMACOBRO.NUMDEVOLUCION, dbo.VENTASFORMACOBRO.TIPOCOBRO"
                         + " FROM  dbo.SUCURSAL RIGHT OUTER JOIN"
                         + " dbo.FACTURACOBRAR RIGHT OUTER JOIN"
                         + " dbo.VENTAS ON dbo.FACTURACOBRAR.CODVENTA = dbo.VENTAS.CODVENTA LEFT OUTER JOIN"
                         + " dbo.VENTASFORMACOBRO ON dbo.VENTAS.CODVENTA = dbo.VENTASFORMACOBRO.CODVENTA ON dbo.SUCURSAL.CODSUCURSAL = dbo.VENTAS.CODSUCURSAL"
                         + " LEFT OUTER JOIN dbo.VENDEDOR ON dbo.VENTAS.CODVENDEDOR = dbo.VENDEDOR.CODVENDEDOR LEFT OUTER JOIN"
                         + " dbo.CLIENTES ON dbo.VENTAS.CODCLIENTE = dbo.CLIENTES.CODCLIENTE LEFT OUTER JOIN"
                         + " dbo.FACTURACOBRAR AS FACTURACOBRAR_1 ON dbo.VENTAS.CODVENTA = FACTURACOBRAR_1.CODVENTA";

            SqlConnection conn = new SqlConnection(_connString);

            //Counts Total number of Rows we have to process
            SqlCommand cmd = new SqlCommand(sql, conn);

            conn.Open();
            cmd.CommandType = CommandType.Text;
            DataTable dt_sales = exeDT(sql);
            int       count    = (int)dt_sales.Rows.Count;

            conn.Close();

            int value = 0;

            Dispatcher.BeginInvoke((Action)(() => salesMaximum.Text = count.ToString()));
            Dispatcher.BeginInvoke((Action)(() => salesValue.Text = value.ToString()));
            Dispatcher.BeginInvoke((Action)(() => progSales.Maximum = count));
            Dispatcher.BeginInvoke((Action)(() => progSales.Value = value));

            //Sales Invoice Detail
            string sqlDetail = "SELECT"
                               + " dbo.PRODUCTOS.DESPRODUCTO,"           //0
                               + " dbo.VENTASDETALLE.CANTIDADVENTA,"     //1
                               + " dbo.VENTASDETALLE.PRECIOVENTANETO, "  //2
                               + " dbo.VENTASDETALLE.PRECIOVENTALISTA, " //3
                               + " dbo.VENTASDETALLE.COSTOPROMEDIO, "    //4
                               + " dbo.VENTASDETALLE.COSTOULTIMO, "      //5
                               + " dbo.VENTASDETALLE.IVA, "              //6
                               + " dbo.VENTAS.COTIZACION1, "             //7
                               + " dbo.MONEDA.DESMONEDA, "               //8
                               + " dbo.VENTASDETALLE.CODVENTA"
                               + " FROM dbo.VENTAS LEFT OUTER JOIN"
                               + " dbo.MONEDA ON dbo.VENTAS.CODMONEDA = dbo.MONEDA.CODMONEDA LEFT OUTER JOIN"
                               + " dbo.VENTASDETALLE ON dbo.VENTAS.CODVENTA = dbo.VENTASDETALLE.CODVENTA LEFT OUTER JOIN"
                               + " dbo.PRODUCTOS ON dbo.VENTASDETALLE.CODPRODUCTO = dbo.PRODUCTOS.CODPRODUCTO";

            DataTable dt_detail = exeDT(sqlDetail);

            int RoofValue  = 1000;
            int FloorValue = 0;

            //Run a Foreach Lap
            for (int i = FloorValue; i < RoofValue; i++)
            {
                using (SalesInvoiceDB db = new SalesInvoiceDB())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;

                    List <entity.app_vat_group>  VATGroupList       = db.app_vat_group.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.contact>        ContactList        = db.contacts.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.sales_rep>      sales_repList      = db.sales_rep.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.app_branch>     BranchList         = db.app_branch.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.app_location>   LocationList       = db.app_location.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.app_terminal>   TerminalList       = db.app_terminal.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.item>           ItemList           = db.items.Where(x => x.id_company == CurrentSession.Id_Company).ToList();
                    List <entity.app_currencyfx> app_currencyfxList = db.app_currencyfx.Where(x => x.id_company == CurrentSession.Id_Company).ToList();

                    app_condition  app_conditionCrédito = db.app_condition.Where(x => x.name == "Crédito" && x.id_company == id_company).FirstOrDefault();
                    app_condition  app_conditionContado = db.app_condition.Where(x => x.name == "Contado" && x.id_company == id_company).FirstOrDefault();
                    app_currencyfx app_currencyfx       = null;
                    if (app_currencyfxList.Where(x => x.is_active).FirstOrDefault() != null)
                    {
                        app_currencyfx = app_currencyfxList.Where(x => x.is_active).FirstOrDefault();
                    }

                    app_vat_group app_vat_group10 = VATGroupList.Where(x => x.name.Contains("10")).FirstOrDefault();
                    app_vat_group app_vat_group5  = VATGroupList.Where(x => x.name.Contains("5")).FirstOrDefault();
                    app_vat_group app_vat_group0  = VATGroupList.Where(x => x.name.Contains("0")).FirstOrDefault();


                    foreach (DataRow InnerRow in dt_sales.Select("CODVENTA > " + FloorValue + " AND CODVENTA < " + RoofValue + ""))
                    {
                        sales_invoice sales_invoice = new entity.sales_invoice();
                        sales_invoice.State      = EntityState.Added;
                        sales_invoice.status     = Status.Documents_General.Pending;
                        sales_invoice.IsSelected = true;
                        sales_invoice.trans_type = Status.TransactionTypes.Normal;
                        sales_invoice.trans_date = DateTime.Now.AddDays(0);
                        sales_invoice.timestamp  = DateTime.Now;
                        sales_invoice.id_company = id_company;
                        sales_invoice.number     = (InnerRow["NUMVENTA"] is DBNull) ? null : InnerRow["NUMVENTA"].ToString();

                        sales_invoice.trans_date = (InnerRow["FECHAVENTA"] is DBNull) ? DateTime.Now :Convert.ToDateTime(InnerRow["FECHAVENTA"]);

                        //Customer
                        if (!(InnerRow["NOMBRE"] is DBNull))
                        {
                            string  _customer = InnerRow["NOMBRE"].ToString();
                            contact contact   = ContactList.Where(x => x.name == _customer && x.id_company == id_company).FirstOrDefault();

                            if (contact != null)
                            {
                                sales_invoice.id_contact = contact.id_contact;
                                sales_invoice.contact    = contact;
                            }
                        }

                        //Condition (Cash or Credit)
                        if (!(InnerRow["TIPOVENTA"] is DBNull) && Convert.ToByte(InnerRow["TIPOVENTA"]) == 0)
                        {
                            sales_invoice.id_condition = app_conditionContado.id_condition;
                            //Contract...

                            app_contract_detail app_contract_detail =
                                db.app_contract_detail.Where(x => x.id_company == id_company &&
                                                             x.app_contract.id_condition == app_conditionContado.id_condition)
                                .FirstOrDefault();

                            if (app_contract_detail != null)
                            {
                                sales_invoice.app_contract = app_contract_detail.app_contract;
                                sales_invoice.id_contract  = app_contract_detail.id_contract;
                            }
                            else
                            {
                                app_contract app_contract = GenerateDefaultContrat(app_conditionContado, 0);
                                db.app_contract.Add(app_contract);
                                sales_invoice.app_contract = app_contract;
                                sales_invoice.id_contract  = app_contract.id_contract;
                            }
                        }
                        else if (!(InnerRow["TIPOVENTA"] is DBNull) && Convert.ToByte(InnerRow["TIPOVENTA"]) == 1)
                        {
                            sales_invoice.id_condition = app_conditionCrédito.id_condition;

                            //Contract...
                            if (!(InnerRow["FECHAVCTO"] is DBNull))
                            {
                                DateTime _due_date = Convert.ToDateTime(InnerRow["FECHAVCTO"]);
                                int      interval  = (_due_date - sales_invoice.trans_date).Days;

                                app_contract_detail app_contract_detail =
                                    db.app_contract_detail.Where(x =>
                                                                 x.app_contract.id_condition == sales_invoice.id_condition &&
                                                                 x.app_contract.id_company == id_company &&
                                                                 x.interval == interval).FirstOrDefault();

                                if (app_contract_detail != null)
                                {
                                    sales_invoice.app_contract = app_contract_detail.app_contract;
                                    sales_invoice.id_contract  = app_contract_detail.id_contract;
                                }
                                else
                                {
                                    app_contract app_contract = GenerateDefaultContrat(app_conditionCrédito, interval);
                                    db.app_contract.Add(app_contract);
                                    sales_invoice.app_contract = app_contract;
                                    sales_invoice.id_contract  = app_contract.id_contract;
                                }
                            }
                            else
                            {
                                if (db.app_contract.Where(x => x.name == "0 Días").Count() == 0)
                                {
                                    app_contract app_contract = GenerateDefaultContrat(app_conditionCrédito, 0);
                                    db.app_contract.Add(app_contract);
                                    sales_invoice.app_contract = app_contract;
                                    sales_invoice.id_contract  = app_contract.id_contract;
                                }
                                else
                                {
                                    app_contract app_contract = db.app_contract.Where(x => x.name == "0 Días").FirstOrDefault();
                                    sales_invoice.app_contract = app_contract;
                                    sales_invoice.id_contract  = app_contract.id_contract;
                                }
                            }
                        }
                        else
                        {
                            sales_invoice.id_condition = app_conditionContado.id_condition;

                            if (db.app_contract.Where(x => x.name == "0 Días").Count() == 0)
                            {
                                app_contract app_contract = GenerateDefaultContrat(app_conditionContado, 0);
                                db.app_contract.Add(app_contract);
                                sales_invoice.app_contract = app_contract;
                                sales_invoice.id_contract  = app_contract.id_contract;
                            }
                            else
                            {
                                app_contract app_contract = db.app_contract.Where(x => x.name == "0 Días").FirstOrDefault();
                                sales_invoice.app_contract = app_contract;
                                sales_invoice.id_contract  = app_contract.id_contract;
                            }
                        }

                        //Sales Rep
                        if (!(InnerRow["DESVENDEDOR"] is DBNull))
                        {
                            string    _sales_rep = InnerRow["DESVENDEDOR"].ToString();
                            sales_rep sales_rep  = sales_repList.Where(x => x.name == _sales_rep && x.id_company == id_company).FirstOrDefault();
                            sales_invoice.id_sales_rep = sales_rep.id_sales_rep;
                        }

                        int          id_location  = 0;
                        app_location app_location = null;

                        //Branch
                        if (!(InnerRow["DESSUCURSAL"] is DBNull))
                        {
                            //Branch
                            string     _branch    = InnerRow["DESSUCURSAL"].ToString();
                            app_branch app_branch = BranchList.Where(x => x.name == _branch && x.id_company == id_company).FirstOrDefault();
                            sales_invoice.id_branch = app_branch.id_branch;

                            //Location
                            if (LocationList.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault() != null)
                            {
                                id_location  = LocationList.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault().id_location;
                                app_location = LocationList.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault();
                            }


                            //Terminal
                            sales_invoice.id_terminal = TerminalList.Where(x => x.app_branch.id_branch == app_branch.id_branch).FirstOrDefault().id_terminal;
                        }


                        if (app_currencyfx != null)
                        {
                            sales_invoice.id_currencyfx  = app_currencyfx.id_currencyfx;
                            sales_invoice.app_currencyfx = app_currencyfx;
                        }

                        DataTable dt_CurrentDetail = new DataTable();
                        if (dt_detail.Select("CODVENTA =" + InnerRow[0].ToString()).Count() > 0)
                        {
                            dt_CurrentDetail = dt_detail.Select("CODVENTA =" + InnerRow[0].ToString()).CopyToDataTable();
                        }

                        foreach (DataRow row in dt_CurrentDetail.Rows)
                        {
                            //db Related Insertion.
                            sales_invoice_detail sales_invoice_detail = new sales_invoice_detail();

                            string _prod_Name = row["DESPRODUCTO"].ToString();
                            item   item       = ItemList.Where(x => x.name == _prod_Name && x.id_company == id_company).FirstOrDefault();
                            sales_invoice_detail.id_item  = item.id_item;
                            sales_invoice_detail.quantity = Convert.ToDecimal(row["CANTIDADVENTA"]);

                            sales_invoice_detail.id_location  = id_location;
                            sales_invoice_detail.app_location = app_location;

                            string _iva = row["IVA"].ToString();
                            if (_iva == "10.00")
                            {
                                if (app_vat_group10 != null)
                                {
                                    sales_invoice_detail.id_vat_group = app_vat_group10.id_vat_group;
                                }
                            }
                            else if (_iva == "5.00")
                            {
                                if (app_vat_group5 != null)
                                {
                                    sales_invoice_detail.id_vat_group = app_vat_group5.id_vat_group;
                                }
                            }
                            else
                            {
                                if (app_vat_group0 != null)
                                {
                                    sales_invoice_detail.id_vat_group = app_vat_group0.id_vat_group;
                                }
                            }

                            decimal cotiz1 = Convert.ToDecimal((row["COTIZACION1"] is DBNull) ? 1 : Convert.ToDecimal(row["COTIZACION1"]));
                            if (cotiz1 == 0)
                            {
                                cotiz1 = 1;
                            }
                            sales_invoice_detail.unit_price = (Convert.ToDecimal(row["PRECIOVENTANETO"]) / sales_invoice_detail.quantity) / cotiz1;
                            sales_invoice_detail.unit_cost  = Convert.ToDecimal(row["COSTOPROMEDIO"]);

                            //Commit Sales Invoice Detail
                            sales_invoice.sales_invoice_detail.Add(sales_invoice_detail);
                        }

                        if (sales_invoice.Error == null)
                        {
                            sales_invoice.State      = System.Data.Entity.EntityState.Added;
                            sales_invoice.IsSelected = true;
                            db.sales_invoice.Add(sales_invoice);

                            if (!(InnerRow["ESTADO"] is DBNull))
                            {
                                int status = Convert.ToInt32(InnerRow["ESTADO"]);

                                if (status == 0)
                                {
                                    sales_invoice.status = Status.Documents_General.Pending;
                                }
                                else if (status == 1)
                                {
                                    db.Approve(true);
                                    sales_invoice.State      = System.Data.Entity.EntityState.Modified;
                                    sales_invoice.status     = Status.Documents_General.Approved;
                                    sales_invoice.IsSelected = true;

                                    add_paymnet_detail(db, sales_invoice, InnerRow["SALDOCUOTA"], InnerRow["IMPORTE"]);
                                }
                                else if (status == 2)
                                {
                                    sales_invoice.status = Status.Documents_General.Annulled;

                                    if (!(InnerRow["MOTIVOANULADO"] is DBNull))
                                    {
                                        sales_invoice.comment = InnerRow["MOTIVOANULADO"].ToString();
                                    }
                                }

                                try
                                {
                                    db.SaveChanges();
                                    sales_invoice.IsSelected = false;
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                            }
                        }
                        else
                        {
                            //Add code to include error contacts into
                            SalesInvoice_ErrorList.Add(sales_invoice);
                        }
                        // }
                        value += 1;
                        Dispatcher.BeginInvoke((Action)(() => progSales.Value = value));
                        Dispatcher.BeginInvoke((Action)(() => salesValue.Text = value.ToString()));
                    }
                }


                FloorValue = RoofValue;
                RoofValue += 1000;
            }
        }
Exemple #15
0
        public void salesReturn()
        {
            string sql = " SELECT "
                         + " DEVOLUCION.CODDEVOLUCION,"
                         + " CLIENTES.NOMBRE,"
                         + " VENTAS.NUMVENTA,"
                         + " DEVOLUCION.CODCOMPROBANTE,"
                         + " DEVOLUCION.CODVENTA,"
                         + " DEVOLUCION.NUMDEVOLUCION,"
                         + " DEVOLUCION.FECHADEVOLUCION, "
                         + " DEVOLUCION.TOTALEXENTA,"
                         + " DEVOLUCION.TOTALGRAVADA,"
                         + " DEVOLUCION.TOTALIVA,"
                         + " DEVOLUCION.TOTALDEVOLUCION,"
                         + " DEVOLUCION.COTIZACION1,"
                         + " DEVOLUCION.FECGRA,"
                         + " DEVOLUCION.CODVENDEDOR, "
                         + " DEVOLUCION.CODCOMPROBANTERECP,"
                         + " DEVOLUCION.COBRADO,"
                         + " DEVOLUCION.CODDEPOSITO,"
                         + " DEVOLUCION.ESTADO,"
                         + " DEVOLUCION.MOTIVOANULADO,"
                         + " DEVOLUCION.MOTIVODESCARTE, "
                         + " DEVOLUCION.TOTALIVA5,"
                         + " DEVOLUCION.TOTALIVA10,"
                         + " DEVOLUCION.TIPODEVOLUCION,"
                         + " DEVOLUCION.DESCONTARMONTO,"
                         + " MONEDA.DESMONEDA,"
                         + " SUCURSAL.DESSUCURSAL,"
                         + " DEVOLUCION.SALDO, "
                         + " VENDEDOR.DESVENDEDOR"
                         + " FROM DEVOLUCION INNER JOIN"
                         + " CLIENTES ON DEVOLUCION.CODCLIENTE = CLIENTES.CODCLIENTE INNER JOIN"
                         + " MONEDA ON DEVOLUCION.CODMONEDA = MONEDA.CODMONEDA INNER JOIN"
                         + " SUCURSAL ON DEVOLUCION.CODSUCURSAL = SUCURSAL.CODSUCURSAL LEFT OUTER JOIN"
                         + " VENDEDOR ON DEVOLUCION.CODVENDEDOR = VENDEDOR.CODVENDEDOR LEFT OUTER JOIN"
                         + " VENTAS ON CLIENTES.CODCLIENTE = VENTAS.CODCLIENTE AND DEVOLUCION.CODVENTA = VENTAS.CODVENTA ";


            SqlConnection conn = new SqlConnection(_connString);

            //Counts Total number of Rows we have to process
            SqlCommand cmd = new SqlCommand(sql, conn);

            conn.Open();
            cmd.CommandText = "SELECT COUNT(*) FROM DEVOLUCION ";
            cmd.CommandType = CommandType.Text;
            int count = (int)cmd.ExecuteScalar();

            conn.Close();

            int value = 0;

            Dispatcher.BeginInvoke((Action)(() => salesReturnMaximum.Text = count.ToString()));
            Dispatcher.BeginInvoke((Action)(() => salesReturnValue.Text = value.ToString()));
            Dispatcher.BeginInvoke((Action)(() => progSalesReturn.Maximum = count));
            Dispatcher.BeginInvoke((Action)(() => progSalesReturn.Value = value));

            cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.CommandType = CommandType.Text;
            //SqlDataReader reader = cmd.ExecuteReader();
            DataTable dt_salesReturn = exeDT(sql);

            foreach (DataRow reader in dt_salesReturn.Rows)
            {
                using (SalesReturnDB db = new SalesReturnDB())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;

                    sales_return sales_return = db.New();
                    sales_return.id_company = id_company;

                    //if ((reader[6] is DBNull))
                    //{
                    //    sales_invoice.is_accounted = false;
                    //}
                    //else
                    //{
                    //    sales_invoice.is_accounted = (Convert.ToByte(reader[23]) == 0) ? false : true;
                    //}


                    //sales_invoice.version = 1;

                    sales_return.number     = (reader["NUMDEVOLUCION"] is DBNull) ? null : reader["NUMDEVOLUCION"].ToString();
                    sales_return.trans_date = Convert.ToDateTime(reader["FECHADEVOLUCION"]);

                    //Customer
                    if (!(reader["NOMBRE"] is DBNull))
                    {
                        string  _customer = reader["NOMBRE"].ToString();
                        contact contact   = db.contacts.Where(x => x.name == _customer && x.id_company == id_company).FirstOrDefault();
                        if (contact != null)
                        {
                            sales_return.id_contact = contact.id_contact;
                            sales_return.contact    = contact;
                        }
                    }

                    //Condition (Cash or Credit)

                    app_condition app_condition = db.app_condition.Where(x => x.name == "Contado").FirstOrDefault();
                    sales_return.id_condition = app_condition.id_condition;
                    if (db.app_contract.Where(x => x.name == "0 Días").Count() == 0)
                    {
                        app_contract app_contract = GenerateDefaultContrat(app_condition, 0);
                        db.app_contract.Add(app_contract);
                        sales_return.app_contract = app_contract;
                        sales_return.id_contract  = app_contract.id_contract;
                    }
                    else
                    {
                        app_contract app_contract = db.app_contract.Where(x => x.name == "0 Días").FirstOrDefault();
                        sales_return.app_contract = app_contract;
                        sales_return.id_contract  = app_contract.id_contract;
                    }



                    int          id_location  = 0;
                    app_location app_location = null;

                    //Branch
                    if (!(reader["DESSUCURSAL"] is DBNull))
                    {
                        //Branch
                        string     _branch    = reader["DESSUCURSAL"].ToString();
                        app_branch app_branch = db.app_branch.Where(x => x.name == _branch && x.id_company == id_company).FirstOrDefault();
                        sales_return.id_branch = app_branch.id_branch;

                        //Location
                        id_location  = db.app_location.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault().id_location;
                        app_location = db.app_location.Where(x => x.id_branch == app_branch.id_branch && x.is_default).FirstOrDefault();
                        //Terminal
                        sales_return.id_terminal = db.app_terminal.Where(x => x.app_branch.id_branch == app_branch.id_branch).FirstOrDefault().id_terminal;
                    }

                    if (!(reader["NUMVENTA"] is DBNull))
                    {
                        string        _salesNumber  = reader["NUMVENTA"].ToString();
                        sales_invoice sales_invoice = db.sales_invoice.Where(x => x.number == _salesNumber).FirstOrDefault();
                        sales_return.id_sales_invoice = sales_invoice.id_sales_invoice;
                        //   sales_return.sales_invoice = sales_invoice;
                    }

                    string _desMoneda = string.Empty;

                    //Sales Invoice Detail
                    string sqlDetail = "SELECT"
                                       + " dbo.PRODUCTOS.DESPRODUCTO,"              //0
                                       + " dbo.DEVOLUCIONDETALLE.CANTIDADDEVUELTA," //1
                                       + " dbo.DEVOLUCIONDETALLE.PRECIONETO, "      //2
                                       + " dbo.DEVOLUCIONDETALLE.COSTOPROMEDIO, "   //4
                                       + " dbo.DEVOLUCIONDETALLE.COSTOULTIMO, "     //5
                                       + " dbo.DEVOLUCIONDETALLE.IVA, "             //6
                                       + " dbo.DEVOLUCION.COTIZACION1 "             //6
                                       + " FROM dbo.DEVOLUCION LEFT OUTER JOIN"
                                       + " dbo.DEVOLUCIONDETALLE ON dbo.DEVOLUCION.CODDEVOLUCION = dbo.DEVOLUCIONDETALLE.CODDEVOLUCION LEFT OUTER JOIN"
                                       + " dbo.PRODUCTOS ON dbo.DEVOLUCIONDETALLE.CODPRODUCTO = dbo.PRODUCTOS.CODPRODUCTO"
                                       + " WHERE (dbo.DEVOLUCIONDETALLE.CODDEVOLUCION = " + reader["CODDEVOLUCION"].ToString() + ")";

                    DataTable dt = exeDT(sqlDetail);
                    foreach (DataRow row in dt.Rows)
                    {
                        //db Related Insertion.
                        sales_return.id_currencyfx  = db.app_currencyfx.Where(x => x.is_active).FirstOrDefault().id_currencyfx;
                        sales_return.app_currencyfx = db.app_currencyfx.Where(x => x.is_active).FirstOrDefault();

                        sales_return_detail sales_return_detail = new sales_return_detail();

                        string _prod_Name = row["DESPRODUCTO"].ToString();
                        item   item       = db.items.Where(x => x.name == _prod_Name && x.id_company == id_company).FirstOrDefault();
                        if (item != null)
                        {
                            sales_return_detail.id_item = item.id_item;
                        }
                        else
                        {
                            value += 1;
                            Dispatcher.BeginInvoke((Action)(() => progSalesReturn.Value = value));
                            Dispatcher.BeginInvoke((Action)(() => salesReturnValue.Text = value.ToString()));
                            continue;
                        }
                        sales_return_detail.id_item  = item.id_item;
                        sales_return_detail.quantity = Convert.ToDecimal(row["CANTIDADDEVUELTA"]);

                        sales_return_detail.id_location  = id_location;
                        sales_return_detail.app_location = app_location;

                        string _iva = row["IVA"].ToString();
                        if (_iva == "10.00")
                        {
                            sales_return_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "10%").FirstOrDefault().id_vat_group;
                        }
                        else if (_iva == "5.00")
                        {
                            sales_return_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "5%").FirstOrDefault().id_vat_group;
                        }
                        else
                        {
                            if (db.app_vat_group.Where(x => x.name == "Excento").FirstOrDefault() != null)
                            {
                                sales_return_detail.id_vat_group = db.app_vat_group.Where(x => x.name == "Excento").FirstOrDefault().id_vat_group;
                            }
                        }

                        decimal cotiz1 = Convert.ToDecimal((row["COTIZACION1"] is DBNull) ? 1 : Convert.ToDecimal(row["COTIZACION1"]));
                        sales_return_detail.unit_price = (Convert.ToDecimal(row["PRECIONETO"]) / sales_return_detail.quantity) / cotiz1;
                        if (!(row["COSTOPROMEDIO"] is DBNull))
                        {
                            sales_return_detail.unit_cost = Convert.ToDecimal(row["COSTOPROMEDIO"]);
                        }


                        //Commit Sales Invoice Detail
                        sales_return.sales_return_detail.Add(sales_return_detail);
                    }
                    sales_return.return_type = Status.ReturnTypes.Bonus;

                    if (sales_return.Error == null)
                    {
                        sales_return.State      = System.Data.Entity.EntityState.Added;
                        sales_return.IsSelected = true;
                        db.sales_return.Add(sales_return);
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        if (!(reader["ESTADO"] is DBNull))
                        {
                            int status = Convert.ToInt32(reader["ESTADO"]);
                            if (status == 0)
                            {
                                sales_return.status = Status.Documents_General.Pending;
                                if (!(reader[11] is DBNull))
                                {
                                    sales_return.comment = reader[11].ToString();
                                }
                            }
                            else if (status == 1)
                            {
                                sales_return.status = Status.Documents_General.Approved;
                                if (!(reader[11] is DBNull))
                                {
                                    sales_return.comment = reader[11].ToString();
                                }
                                db.Approve();
                            }
                            else if (status == 2)
                            {
                                sales_return.status = Status.Documents_General.Annulled;
                                if (!(reader[11] is DBNull))
                                {
                                    sales_return.comment = reader[11].ToString();
                                }
                                db.Approve();
                                db.Anull();
                            }
                        }


                        value += 1;
                        Dispatcher.BeginInvoke((Action)(() => progSalesReturn.Value = value));
                        Dispatcher.BeginInvoke((Action)(() => salesReturnValue.Text = value.ToString()));
                    }
                    else
                    {
                        //Add code to include error contacts into
                        sales_return_ErrorList.Add(sales_return);
                    }
                }
            }
            // reader.Close();
            //cmd.Dispose();
            conn.Close();

            //_customer_Current = _customer_Max;
        }
Exemple #16
0
        private void saveOrder_Click(object sender, RoutedEventArgs e)
        {
            project_event project_costing = project_costingViewSource.View.CurrentItem as project_event;

            if (project_costing != null)
            {
                contact       contact       = EventDB.contacts.Where(x => x.id_contact == sbxContact.ContactID).FirstOrDefault();
                app_condition app_condition = id_conditionComboBox.SelectedItem as app_condition;
                app_contract  app_contract  = id_contractComboBox.SelectedItem as app_contract;


                if (contact != null && app_condition != null && app_contract != null)
                {
                    using (SalesBudgetDB db = new SalesBudgetDB())
                    {
                        sales_budget sales_budget = new sales_budget();
                        sales_budget.IsSelected = true;
                        sales_budget.State      = EntityState.Added;
                        sales_budget.status     = Status.Documents_General.Pending;
                        sales_budget.id_contact = contact.id_contact;
                        sales_budget.contact    = db.contacts.Where(x => x.id_contact == contact.id_contact).FirstOrDefault();

                        if (CurrentSession.Id_Branch > 0)
                        {
                            sales_budget.id_branch = CurrentSession.Id_Branch;
                        }
                        else
                        {
                            sales_budget.id_branch = db.app_branch.Where(a => a.is_active == true && a.id_company == CurrentSession.Id_Company).FirstOrDefault().id_branch;
                        }

                        sales_budget.id_condition  = app_condition.id_condition;
                        sales_budget.id_contract   = app_contract.id_contract;
                        sales_budget.id_currencyfx = project_costing.id_currencyfx;


                        foreach (project_event_variable project_event_variable in project_costing.project_event_variable.Where(a => a.is_included == true))
                        {
                            sales_budget_detail sales_budget_detail = new sales_budget_detail();
                            sales_budget_detail.sales_budget  = sales_budget;
                            sales_budget_detail.item          = db.items.Where(a => a.id_item == project_event_variable.id_item).FirstOrDefault();
                            sales_budget_detail.id_item       = project_event_variable.id_item;
                            sales_budget_detail.quantity      = ((project_event_variable.adult_consumption) + (project_event_variable.child_consumption));
                            sales_budget_detail.UnitPrice_Vat = Math.Round(get_Price(contact, IDcurrencyfx, sales_budget_detail.item, entity.App.Modules.Sales));
                            sales_budget.sales_budget_detail.Add(sales_budget_detail);
                        }

                        foreach (project_event_fixed project_event_fixed in project_costing.project_event_fixed.Where(a => a.is_included == true))
                        {
                            sales_budget_detail sales_budget_detail = new sales_budget_detail();
                            sales_budget_detail.sales_budget  = sales_budget;
                            sales_budget_detail.item          = db.items.Where(a => a.id_item == project_event_fixed.id_item).FirstOrDefault();
                            sales_budget_detail.id_item       = project_event_fixed.id_item;
                            sales_budget_detail.quantity      = project_event_fixed.consumption;
                            sales_budget_detail.UnitPrice_Vat = Math.Round(get_Price(contact, IDcurrencyfx, sales_budget_detail.item, entity.App.Modules.Sales));
                            sales_budget.sales_budget_detail.Add(sales_budget_detail);
                        }

                        sales_budget_detail sales_budget_detail_hall = new sales_budget_detail();
                        sales_budget_detail_hall.sales_budget  = sales_budget;
                        sales_budget_detail_hall.item          = db.items.Where(a => a.id_item == project_costing.id_item).FirstOrDefault();
                        sales_budget_detail_hall.id_item       = project_costing.id_item;
                        sales_budget_detail_hall.quantity      = 1;
                        sales_budget_detail_hall.UnitPrice_Vat = Math.Round(get_Price(contact, IDcurrencyfx, sales_budget_detail_hall.item, entity.App.Modules.Sales));
                        sales_budget.sales_budget_detail.Add(sales_budget_detail_hall);

                        db.sales_budget.Add(sales_budget);
                        db.SaveChanges();
                    }

                    lblCancel_MouseDown(null, null);
                }
                else
                {
                    if (MessageBox.Show("VALIDATION EXCEPTION : Please fill up all the fields", "Cognitivo", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk) == MessageBoxResult.Cancel)
                    {
                        lblCancel_MouseDown(null, null);
                    }
                }
            }
        }
Exemple #17
0
        public string SalesInvoice(sales_invoice sales_invoice)
        {
            string Header       = string.Empty;
            string Detail       = string.Empty;
            string Footer       = string.Empty;
            string BranchName   = string.Empty;
            string TerminalName = string.Empty;

            app_company app_company = null;

            if (sales_invoice.app_company != null)
            {
                app_company = sales_invoice.app_company;
            }
            else
            {
                using (db db = new db())
                {
                    if (db.app_company.Where(x => x.id_company == sales_invoice.id_company).FirstOrDefault() != null)
                    {
                        app_company = db.app_company.Where(x => x.id_company == sales_invoice.id_company).FirstOrDefault();
                    }
                }
            }

            if (sales_invoice.app_branch != null)
            {
                BranchName = sales_invoice.app_branch.name;
            }
            else
            {
                using (db db = new db())
                {
                    app_branch app_branch = db.app_branch.Where(x => x.id_branch == sales_invoice.id_branch).FirstOrDefault();
                    if (app_branch != null)
                    {
                        BranchName = app_branch.name;
                    }
                }
            }

            string UserGiven = "";

            if (sales_invoice.security_user != null)
            {
                UserGiven = sales_invoice.security_user.name;
            }
            else
            {
                using (db db = new db())
                {
                    security_user security_user = db.security_user.Where(x => x.id_user == sales_invoice.id_user).FirstOrDefault();
                    if (security_user != null)
                    {
                        UserGiven = security_user.name;
                    }
                }
            }

            string ContractName = "";

            if (sales_invoice.app_contract != null)
            {
                ContractName = sales_invoice.app_contract.name;
            }
            else
            {
                using (db db = new db())
                {
                    app_contract app_contract = db.app_contract.Where(x => x.id_contract == sales_invoice.id_contract).FirstOrDefault();
                    if (app_contract != null)
                    {
                        ContractName = app_contract.name;
                    }
                }
            }

            string ConditionName = "";

            if (sales_invoice.app_condition != null)
            {
                ConditionName = sales_invoice.app_condition.name;
            }
            else
            {
                using (db db = new db())
                {
                    app_condition app_condition = db.app_condition.Where(x => x.id_condition == sales_invoice.id_condition).FirstOrDefault();
                    if (app_condition != null)
                    {
                        ConditionName = app_condition.name;
                    }
                }
            }

            string CurrencyName = "";

            if (sales_invoice.app_currencyfx != null)
            {
                if (sales_invoice.app_currencyfx.app_currency != null)
                {
                    CurrencyName = sales_invoice.app_currencyfx.app_currency.name;
                }
            }
            else
            {
                using (db db = new db())
                {
                    app_currencyfx app_currencyfx = db.app_currencyfx.Where(x => x.id_currencyfx == sales_invoice.id_currencyfx).FirstOrDefault();
                    if (app_currencyfx != null)
                    {
                        CurrencyName = app_currencyfx.app_currency.name;
                    }
                }
            }

            string   TransNumber = sales_invoice.number;
            DateTime TransDate   = sales_invoice.trans_date;

            Header =
                app_company.name + "\n"
                + "RUC:" + app_company.gov_code + "\n"
                + app_company.address + "\n"
                + "***" + app_company.alias + "***" + "\n"
                + "Timbrado    : " + sales_invoice.app_document_range.code + "\n"
                + "Vencimiento : " + sales_invoice.app_document_range.expire_date + "\n"
                + "--------------------------------"
                + "Descripcion, Cantiad, Precio" + "\n"
                + "--------------------------------" + "\n"
                + "\n";

            foreach (sales_invoice_detail d in sales_invoice.sales_invoice_detail)
            {
                string  ItemName = d.item.name;
                string  ItemCode = d.item.code;
                decimal?Qty      = d.quantity;
                string  TaskName = d.item_description;

                Detail = Detail
                         + ItemName + "\n"
                         + Qty.ToString() + "\t" + ItemCode + "\t" + Math.Round((d.UnitPrice_Vat + d.DiscountVat), 2) + "\n";
            }

            decimal DiscountTotal = sales_invoice.sales_invoice_detail.Sum(x => x.Discount_SubTotal_Vat);

            Footer  = "--------------------------------" + "\n";
            Footer += "Total Bruto       : " + Math.Round((sales_invoice.GrandTotal + DiscountTotal), 2) + "\n";
            Footer += "Total Descuento   : -" + Math.Round(sales_invoice.sales_invoice_detail.Sum(x => x.Discount_SubTotal_Vat), 2) + "\n";
            Footer += "Total " + CurrencyName + " : " + Math.Round(sales_invoice.GrandTotal, 2) + "\n";
            Footer += "Fecha & Hora      : " + sales_invoice.trans_date + "\n";
            Footer += "Numero de Factura : " + sales_invoice.number + "\n";
            Footer += "-------------------------------" + "\n";

            if (sales_invoice != null)
            {
                List <sales_invoice_detail> sales_invoice_detail = sales_invoice.sales_invoice_detail.ToList();
                if (sales_invoice_detail.Count > 0)
                {
                    using (db db = new db())
                    {
                        var listvat = sales_invoice_detail
                                      .Join(db.app_vat_group_details, ad => ad.id_vat_group, cfx => cfx.id_vat_group
                                            , (ad, cfx) => new { name = cfx.app_vat.name, value = ad.unit_price * cfx.app_vat.coefficient, id_vat = cfx.app_vat.id_vat, ad })
                                      .GroupBy(a => new { a.name, a.id_vat, a.ad })
                                      .Select(g => new
                        {
                            vatname = g.Key.ad.app_vat_group.name,
                            id_vat  = g.Key.id_vat,
                            name    = g.Key.name,
                            value   = g.Sum(a => a.value * a.ad.quantity)
                        }).ToList();
                        var VAtList = listvat.GroupBy(x => x.id_vat).Select(g => new
                        {
                            vatname = g.Max(y => y.vatname),
                            id_vat  = g.Max(y => y.id_vat),
                            name    = g.Max(y => y.name),
                            value   = g.Sum(a => a.value)
                        }).ToList();
                        foreach (dynamic item in VAtList)
                        {
                            Footer += item.vatname + "   : " + Math.Round(item.value, 2) + "\n";
                        }
                        Footer += "Total IVA : " + CurrencyName + " " + Math.Round(VAtList.Sum(x => x.value), 2) + "\n";
                    }
                }
            }

            Footer += "-------------------------------";
            Footer += "Cliente    : " + sales_invoice.contact.name + "\n";
            Footer += "Documento  : " + sales_invoice.contact.gov_code + "\n";
            Footer += "Condicion  : " + ConditionName + "\n";
            Footer += "-------------------------------";
            Footer += "Sucursal   : " + BranchName + "\n";
            Footer += "Terminal   : " + TerminalName;

            if (sales_invoice.id_sales_rep > 0)
            {
                string SalesRep_Name = "";

                if (sales_invoice.sales_rep == null)
                {
                    using (db db = new db())
                    {
                        SalesRep_Name = db.sales_rep.Where(x => x.id_sales_rep == (int)sales_invoice.id_sales_rep).FirstOrDefault().name;
                    }
                }
                else
                {
                    SalesRep_Name = sales_invoice.sales_rep.name;
                }

                Footer += "\n";
                Footer += "Vendedor/a : " + SalesRep_Name;
            }
            Footer += "\n";
            Footer += "Cajero/a : " + UserGiven;

            string Text = Header + Detail + Footer;

            return(Text);
        }