private void btnNew_Click(object sender, RoutedEventArgs e) { crud_modal.Visibility = Visibility.Visible; cntrl.Curd.ItemTag _ItemTag = new cntrl.Curd.ItemTag(); item_tag item_tag = new item_tag(); entity.db.item_tag.Add(item_tag); item_tagViewSource.View.MoveCurrentToLast(); _ItemTag.item_tagViewSource = item_tagViewSource; _ItemTag.entity = entity; crud_modal.Children.Add(_ItemTag); }
public accounting_chart find_Chart(AccountingJournalDB context, item_tag item_tag) { if (context.accounting_chart.Where(i => i.id_tag == item_tag.id_tag).FirstOrDefault() != null) { return(context.accounting_chart.Where(i => i.id_tag == item_tag.id_tag).FirstOrDefault()); } else if (context.accounting_chart.Where(i => i.chartsub_type == accounting_chart.ChartSubType.Inventory && i.is_generic == true).FirstOrDefault() != null) { return(context.accounting_chart.Where(i => i.chartsub_type == accounting_chart.ChartSubType.Inventory && i.is_generic == true).FirstOrDefault()); } else { return(null); } }
public void AddData(SearchableTextbox itemComboBox) { if (itemComboBox.Data != null) { // Data = itemComboBox.Data; int id = Convert.ToInt32(((item_tag)itemComboBox.Data).id_tag); if (id > 0) { if (_item.item_tag_detail.Count > 0) { ((item_tag_detail)ItemsSource.View.CurrentItem).id_tag = id; ((item_tag_detail)ItemsSource.View.CurrentItem).item_tag = ((item_tag)itemComboBox.Data); item_tagViewSource.View.Refresh(); ItemsSource.View.Refresh(); } else { item_tag_detail item_tag_detail = new item_tag_detail(); item_tag_detail.id_tag = id; item_tag_detail.item_tag = ((item_tag)itemComboBox.Data); _item.item_tag_detail.Add(item_tag_detail); ItemsSource.View.Refresh(); } } } else { db db = new db(); item_tag item_tag = new item_tag(); item_tag.name = itemComboBox.TxtSearched.Text; db.item_tag.Add(item_tag); db.SaveChanges(); entity.item_tag.Where(x => x.id_company == _settings_entity.company_ID && x.is_active == true).OrderBy(x => x.name).Load(); itemComboBox.Data = item_tag; int id_tag = item_tag.id_tag; ((item_tag_detail)ItemsSource.View.CurrentItem).item_tag = entity.item_tag.Where(x => x.id_tag == id_tag).FirstOrDefault(); ItemsSource.View.Refresh(); } }
private accounting_journal Calculate_PurchaseInvoice(AccountingJournalDB AccountingJournalDB, purchase_invoice purchase_invoice, accounting_cycle accounting_cycle) { accounting_journal accounting_journal = new accounting_journal(); if (purchase_invoice.accounting_journal == null) { accounting_journal.id_cycle = accounting_cycle.id_cycle; accounting_journal.trans_date = purchase_invoice.trans_date; accounting_journal.IsSelected = true; accounting_journal.State = EntityState.Added; accounting_journal.comment = purchase_invoice.comment; List <accounting_journal_detail> accounting_journal_detailList = new List <accounting_journal_detail>(); foreach (purchase_invoice_detail purchase_invoice_detail in purchase_invoice.purchase_invoice_detail.ToList()) { if (purchase_invoice_detail.app_cost_center.is_product && purchase_invoice_detail.item != null) { List <item_tag_detail> item_tag_detailLIST = purchase_invoice_detail.item.item_tag_detail.ToList(); if (item_tag_detailLIST != null) { Asset.Inventory Inventory = new Asset.Inventory(); accounting_chart INV_Chart = null; foreach (item_tag_detail item_tag_detail in purchase_invoice_detail.item.item_tag_detail.ToList()) { item_tag item_tag = item_tag_detail.item_tag; INV_Chart = Inventory.find_Chart(AccountingJournalDB, item_tag); } if (INV_Chart != null) { accounting_journal_detail INV_accounting_journal = new accounting_journal_detail(); INV_accounting_journal.accounting_chart = INV_Chart; INV_accounting_journal.trans_date = purchase_invoice.trans_date; INV_accounting_journal.debit = Math.Round(purchase_invoice_detail.SubTotal, 2); INV_accounting_journal.id_currencyfx = purchase_invoice.app_currencyfx.id_currencyfx; accounting_journal_detailList.Add(INV_accounting_journal); } } } else if (purchase_invoice_detail.app_cost_center.is_fixedasset) { //Ignore } else { Expense.AdministrationExpense AdministrationExpense = new Expense.AdministrationExpense(); app_cost_center app_cost_center = purchase_invoice_detail.app_cost_center; accounting_chart Exp_Chart = AdministrationExpense.find_Chart(AccountingJournalDB, app_cost_center); if (Exp_Chart != null) { accounting_journal_detail INV_accounting_journal = new accounting_journal_detail(); INV_accounting_journal.accounting_chart = Exp_Chart; INV_accounting_journal.trans_date = purchase_invoice.trans_date; INV_accounting_journal.debit = Math.Round(purchase_invoice_detail.SubTotal, 2); INV_accounting_journal.id_currencyfx = purchase_invoice.app_currencyfx.id_currencyfx; accounting_journal_detailList.Add(INV_accounting_journal); } } } Liability.ValueAddedTax VAT = new Liability.ValueAddedTax(); foreach (purchase_invoice_detail purchase_invoice_detail in purchase_invoice.purchase_invoice_detail.ToList()) { foreach (app_vat_group_details app_vat_group in purchase_invoice_detail.app_vat_group.app_vat_group_details) { accounting_chart VAT_Chart = VAT.find_Chart(AccountingJournalDB, app_vat_group.app_vat); if (VAT_Chart != null) { accounting_journal_detail INV_accounting_journal = new accounting_journal_detail(); INV_accounting_journal.accounting_chart = VAT_Chart; INV_accounting_journal.trans_date = purchase_invoice.trans_date; INV_accounting_journal.debit = Math.Round(Vat.calculate_Vat((purchase_invoice_detail.unit_cost * purchase_invoice_detail.quantity), app_vat_group.app_vat.coefficient), 2); INV_accounting_journal.id_currencyfx = purchase_invoice.app_currencyfx.id_currencyfx; accounting_journal_detailList.Add(INV_accounting_journal); } } } List <payment_schedual> payment_schedualLIST = AccountingJournalDB.payment_schedual.Where(x => x.id_purchase_invoice == purchase_invoice.id_purchase_invoice).ToList(); if (payment_schedualLIST != null) { ///For Loop for each Payment Schedual. foreach (payment_schedual payment_schedual in payment_schedualLIST) { ///Example: 1000$ Invoice. ///Example: 600$ Paid & 400$ NotPaid. ///Payment Done -> Ex. 600$ if (payment_schedual.payment_detail != null && payment_schedual.debit > 0) { Asset.Cash CashAccount = new Asset.Cash(); accounting_chart AR_Chart = CashAccount.find_Chart(AccountingJournalDB, payment_schedual.payment_detail.app_account); if (AR_Chart != null) { accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail(); PAYaccounting_journal_detail.accounting_chart = AR_Chart; PAYaccounting_journal_detail.trans_date = payment_schedual.trans_date; PAYaccounting_journal_detail.credit = payment_schedual.debit; PAYaccounting_journal_detail.id_currencyfx = payment_schedual.app_currencyfx.id_currencyfx; accounting_journal_detailList.Add(PAYaccounting_journal_detail); } } ///Payment Left -> Ex. 400$ else if (payment_schedual.payment_detail == null && payment_schedual.credit > 0) { //Credit Payment Liability.AccountsPayable AccountsPayable = new Liability.AccountsPayable(); accounting_chart AR_Chart = AccountsPayable.find_Chart(AccountingJournalDB, purchase_invoice.contact); if (AR_Chart != null) { accounting_journal_detail AR_accounting_journal_detail = new accounting_journal_detail(); AR_accounting_journal_detail.accounting_chart = AR_Chart; AR_accounting_journal_detail.trans_date = purchase_invoice.trans_date; AR_accounting_journal_detail.credit = payment_schedual.credit - payment_schedual.child.Sum(x => x.debit); AR_accounting_journal_detail.id_currencyfx = purchase_invoice.app_currencyfx.id_currencyfx; accounting_journal_detailList.Add(AR_accounting_journal_detail); } } } } ///Clean up Duplicate Accounts. ///If Duplicate, will sum into first of the same chart it Finds. foreach (accounting_journal_detail accounting_journal_detail in accounting_journal_detailList) { int id_chart = accounting_journal_detail.accounting_chart.id_chart; if (accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).Count() == 0) { accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail(); PAYaccounting_journal_detail.id_chart = accounting_journal_detail.accounting_chart.id_chart; PAYaccounting_journal_detail.accounting_chart = accounting_journal_detail.accounting_chart; PAYaccounting_journal_detail.trans_date = accounting_journal_detail.trans_date; PAYaccounting_journal_detail.credit = Math.Round(accounting_journal_detail.credit, 2); PAYaccounting_journal_detail.debit = Math.Round(accounting_journal_detail.debit, 2); PAYaccounting_journal_detail.id_currencyfx = accounting_journal_detail.id_currencyfx; accounting_journal.accounting_journal_detail.Add(PAYaccounting_journal_detail); } else { accounting_journal_detail PAYaccounting_journal_detail = accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).FirstOrDefault(); PAYaccounting_journal_detail.credit += Math.Round(accounting_journal_detail.credit, 2); PAYaccounting_journal_detail.debit += Math.Round(accounting_journal_detail.debit, 2); } } accounting_journal.purchase_invoice.Add(purchase_invoice); } return(accounting_journal); }
public void product() { string sql = " SELECT dbo.FAMILIA.DESFAMILIA, dbo.LINEA.DESLINEA, dbo.RUBRO.DESRUBRO, dbo.IVA.DESIVA, dbo.CODIGOS.DESCODIGO1, dbo.CODIGOS.CODIGO, dbo.CODIGOS.PESABLE, dbo.CODIGOS.VENCIMIENTO, dbo.CODIGOS.BALANZA, dbo.PRODUCTOS.DESPRODUCTO, dbo.PRODUCTOS.STOCKMINIMO, dbo.PRODUCTOS.STOCKMAXIMO, " + " dbo.PRODUCTOS.SERVICIO, dbo.PRODUCTOS.ESTADO, dbo.PRODUCTOS.ESPECIFICACIONES, dbo.PRODUCTOS.PRODUCTO, dbo.UNIDADMEDIDA.DESMEDIDA, dbo.CODIGOS.CODCODIGO" + " FROM dbo.CODIGOS LEFT OUTER JOIN" + " dbo.PRODUCTOS ON dbo.CODIGOS.CODPRODUCTO = dbo.PRODUCTOS.CODPRODUCTO LEFT OUTER JOIN" + " dbo.UNIDADMEDIDA ON dbo.PRODUCTOS.CODMEDIDA = dbo.UNIDADMEDIDA.CODMEDIDA LEFT OUTER JOIN" + " dbo.RUBRO ON dbo.PRODUCTOS.CODRUBRO = dbo.RUBRO.CODRUBRO LEFT OUTER JOIN" + " dbo.FAMILIA ON dbo.PRODUCTOS.CODFAMILIA = dbo.FAMILIA.CODFAMILIA LEFT OUTER JOIN" + " dbo.LINEA ON dbo.PRODUCTOS.CODLINEA = dbo.LINEA.CODLINEA LEFT OUTER JOIN" + " dbo.IVA ON dbo.PRODUCTOS.CODIVA = dbo.IVA.CODIVA"; SqlConnection conn = new SqlConnection(_connString); //Counts Total number of Rows we have to process SqlCommand cmd = new SqlCommand(); conn.Open(); cmd.Connection = conn; cmd.CommandText = "SELECT COUNT(*) FROM CODIGOS"; cmd.CommandType = CommandType.Text; int count = (int)cmd.ExecuteScalar(); //cmd.Dispose(); conn.Close(); int value = 0; Dispatcher.BeginInvoke((Action)(() => progItem.Maximum = count)); Dispatcher.BeginInvoke((Action)(() => progItem.Value += value)); Dispatcher.BeginInvoke((Action)(() => itemMaximum.Text = count.ToString())); Dispatcher.BeginInvoke((Action)(() => itemValue.Text = value.ToString())); string sql_price = " SELECT dbo.TIPOCLIENTE.DESTIPOCLIENTE, dbo.PRECIO.CANTIDAD, dbo.PRECIO.PRECIOVENTA, dbo.MONEDA.DESMONEDA, dbo.PRODUCTOS.DESPRODUCTO" + " FROM dbo.PRECIO LEFT OUTER JOIN" + " dbo.MONEDA ON dbo.PRECIO.CODMONEDA = dbo.MONEDA.CODMONEDA LEFT OUTER JOIN" + " dbo.PRODUCTOS ON dbo.PRECIO.CODPRODUCTO = dbo.PRODUCTOS.CODPRODUCTO LEFT OUTER JOIN" + " dbo.TIPOCLIENTE ON dbo.PRECIO.CODTIPOCLIENTE = dbo.TIPOCLIENTE.CODTIPOCLIENTE"; DataTable dt_Price = exeDT(sql_price); conn.Open(); cmd.Connection = conn; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; DataTable dt_product = exeDT(sql); foreach (DataRow reader in dt_product.Rows) { using (db db = new db()) { db.Configuration.AutoDetectChangesEnabled = false; item item = new item(); item.id_company = id_company; if (!(reader[9] is DBNull)) { item.name = reader[9].ToString(); } else { continue; } item.code = (reader[5] is DBNull) ? string.Empty : reader[5].ToString(); item.variation = (reader[9] is DBNull) ? string.Empty : reader[9].ToString(); item.is_active = (reader[13] is DBNull) ? false : true; item.description = (reader[14] is DBNull) ? string.Empty : reader[14].ToString(); string name = (reader[16] is DBNull) ? string.Empty : reader[16].ToString(); if (db.app_measurement.Where(x => x.name == name && x.id_company == id_company).FirstOrDefault() != null) { item.id_measurement = db.app_measurement.Where(x => x.name == name && x.id_company == id_company).FirstOrDefault().id_measurement; } //string FAMILIA; if (!(reader[0] is DBNull)) { string tagname = reader[0].ToString(); item_tag item_tagFam = db.item_tag.Where(x => x.name == tagname && x.id_company == id_company).FirstOrDefault(); item_tag_detail tag_detailFam = new item_tag_detail(); tag_detailFam.id_tag = item_tagFam.id_tag; item.item_tag_detail.Add(tag_detailFam); } //string LINEA; if (!(reader[1] is DBNull)) { string tagLinname = reader[1].ToString(); item_tag item_tagLin = db.item_tag.Where(x => x.name == tagLinname && x.id_company == id_company).FirstOrDefault(); item_tag_detail tag_detailLin = new item_tag_detail(); tag_detailLin.id_tag = item_tagLin.id_tag; item.item_tag_detail.Add(tag_detailLin); } //string RUBRO; if (!(reader[2] is DBNull)) { string tagrubro = reader[2].ToString(); item_tag item_tagRub = db.item_tag.Where(x => x.name == tagrubro && x.id_company == id_company).FirstOrDefault(); item_tag_detail tag_detailRub = new item_tag_detail(); tag_detailRub.id_tag = item_tagRub.id_tag; item.item_tag_detail.Add(tag_detailRub); } if (!(reader[15] is DBNull)) { if (Convert.ToInt32(reader[15]) == 1) { //Product item.id_item_type = item.item_type.Product; item_product product = new item_product(); product.id_company = id_company; product.can_expire = (reader[7] is DBNull || Convert.ToInt32(reader[7]) == 0) ? false : true; product.is_weigted = (reader[6] is DBNull || Convert.ToInt32(reader[6]) == 0) ? false : true; product.stock_max = (reader[11] is DBNull) ? 0M : (decimal)reader[11]; product.stock_min = (reader[10] is DBNull) ? 0M : (decimal)reader[10]; item.item_product.Add(product); } else { item.id_item_type = item.item_type.Task; //Generic Service //item_service service } } else { item.id_item_type = item.item_type.Task; //Generic Service //item_service service } decimal _vat_coeficient = 0; if (!(reader[3] is DBNull)) { string vatname = reader[3].ToString(); app_vat_group app_vat_group = db.app_vat_group.Where(x => x.name == vatname && x.id_company == id_company).FirstOrDefault(); item.id_vat_group = app_vat_group.id_vat_group; } decimal coefficient = 0; List <app_vat_group_details> app_vat_group_details = db.app_vat_group_details.Where(x => x.id_vat_group == item.id_vat_group).ToList(); foreach (app_vat_group_details app_vat_group in app_vat_group_details) { coefficient = coefficient + app_vat_group.app_vat.coefficient; } string _DESPRODUCTO = reader["DESPRODUCTO"].ToString(); _DESPRODUCTO = _DESPRODUCTO.Replace("'", ""); try { foreach (DataRow price_row in dt_Price.Select("DESPRODUCTO = '" + _DESPRODUCTO + "'")) { string _desTipoCliente = (price_row.IsNull("DESTIPOCLIENTE")) ? string.Empty : price_row["DESTIPOCLIENTE"].ToString(); string _desMoneda = (price_row.IsNull("DESMONEDA")) ? string.Empty : price_row["DESMONEDA"].ToString(); if (_desTipoCliente != string.Empty && _desMoneda != string.Empty) { item_price_list item_price_list = db.item_price_list.Where(x => x.name == _desTipoCliente && x.id_company == id_company).FirstOrDefault(); app_currency app_currency = db.app_currency.Where(x => x.name == _desMoneda && x.id_company == id_company).FirstOrDefault(); if (item_price_list != null && app_currency != null && _vat_coeficient != -1) { item_price item_price = new item_price(); item_price.item = item; if (price_row["PRECIOVENTA"] is DBNull) { item_price.value = 0; } else { if (coefficient != -1) { item_price.value = ((decimal)price_row["PRECIOVENTA"] / (1 + coefficient)); } } item_price.min_quantity = (price_row.IsNull("CANTIDAD")) ? 0 : Convert.ToDecimal(price_row["CANTIDAD"]); item_price.id_currency = app_currency.id_currency; item_price.id_price_list = item_price_list.id_price_list; item.item_price.Add(item_price); } } } } catch (Exception ex) { throw ex; } try { if (item.Error == null) { db.items.Add(item); db.SaveChanges(); value += 1; Dispatcher.BeginInvoke((Action)(() => progItem.Value = value)); Dispatcher.BeginInvoke((Action)(() => itemValue.Text = value.ToString())); } } catch { } } } //cmd.Dispose(); conn.Close(); _product_Current = _product_Max; }
private void GetServices(object sender, RoutedEventArgs e) { toolBar_btnSave_Click(sender); toolBar_btnEdit_Click(sender); if (project_costingViewSource != null) { project_event project_event = project_costingViewSource.View.CurrentItem as project_event; //if (project_event.project_event_fixed.Count()==0) //{ if (project_event != null && id_template_designerComboBox.SelectedItem != null) { project_event_template project_event_template = id_template_designerComboBox.SelectedItem as project_event_template; if (project_event_template.project_event_template_variable != null && project_event_template.project_event_template_variable.Count > 0) { // EventDB.project_event_variable.RemoveRange(project_event.project_event_variable); foreach (project_event_template_variable person_service in project_event_template.project_event_template_variable) { item_tag item_tag = person_service.item_tag; foreach (item_tag_detail tag_detail in item_tag.item_tag_detail) { if (tag_detail.item.is_active) { if (project_event.project_event_variable.Where(x => x.id_item == tag_detail.item.id_item).Any()) { project_event_variable project_event_variable = project_event.project_event_variable.Where(x => x.id_item == tag_detail.item.id_item).FirstOrDefault(); project_event_variable.adult_consumption = person_service.adult_consumption * project_event.quantity_adult; project_event_variable.child_consumption = person_service.child_consumption * project_event.quantity_child; } else { project_event_variable project_event_variable = new project_event_variable(); project_event_variable.item = tag_detail.item; project_event_variable.id_item = tag_detail.id_item; project_event_variable.item_tag = tag_detail.item_tag; project_event_variable.id_tag = tag_detail.id_tag; project_event_variable.adult_consumption = person_service.adult_consumption * project_event.quantity_adult; project_event_variable.child_consumption = person_service.child_consumption * project_event.quantity_child; project_event_variable.is_included = false; project_event.project_event_variable.Add(project_event_variable); } } } } project_costingproject_event_template_variable_detailsViewSource.View.Refresh(); } } if (project_event != null && id_template_designerComboBox.SelectedItem != null) { project_event_template project_event_template = id_template_designerComboBox.SelectedItem as project_event_template; if (project_event_template.project_event_template_fixed != null && project_event_template.project_event_template_fixed.Count > 0) { // EventDB.project_event_fixed.RemoveRange(project_event.project_event_fixed); foreach (project_event_template_fixed project_event_template_fixed in project_event_template.project_event_template_fixed) { item_tag item_tag = project_event_template_fixed.item_tag; foreach (item_tag_detail tag_detail in item_tag.item_tag_detail) { if (tag_detail.item.is_active) { if (project_event.project_event_fixed.Where(x => x.id_item == tag_detail.item.id_item).Any()) { project_event_fixed services_per_event_details = project_event.project_event_fixed.Where(x => x.id_item == tag_detail.item.id_item).FirstOrDefault(); services_per_event_details.consumption = services_per_event_details.consumption; } else { project_event_fixed services_per_event_details = new project_event_fixed(); services_per_event_details.item = tag_detail.item; services_per_event_details.id_item = tag_detail.id_item; services_per_event_details.item_tag = tag_detail.item_tag; services_per_event_details.id_tag = tag_detail.id_tag; services_per_event_details.consumption = 1; services_per_event_details.is_included = false; project_event.project_event_fixed.Add(services_per_event_details); } } } } project_costingservices_per_event_detailsViewSource.View.Refresh(); } } //} EstimateCost(); } }
private void sync_TagsList() { DataTable dt = new DataTable(); dt = exeDT("SELECT * FROM FAMILIA"); foreach (DataRow row in dt.Rows) { using (db db = new db()) { string _tag = row["DESFAMILIA"].ToString(); if (db.item_tag.Any(x => x.name == _tag && x.id_company == id_company)) { continue; } item_tag tag = new item_tag(); tag.id_company = id_company; tag.name = (string)row["DESFAMILIA"]; db.item_tag.Add(tag); db.SaveChanges(); } } dt.Clear(); dt = exeDT("SELECT * FROM LINEA"); foreach (DataRow row in dt.Rows) { using (db db = new db()) { string _tag = row["DESLINEA"].ToString(); if (db.item_tag.Any(x => x.name == _tag && x.id_company == id_company)) { continue; } item_tag tag = new item_tag(); tag.id_company = id_company; tag.name = (string)row["DESLINEA"]; db.item_tag.Add(tag); db.SaveChanges(); } } dt.Clear(); dt = exeDT("SELECT * FROM RUBRO"); foreach (DataRow row in dt.Rows) { using (db db = new db()) { string _tag = row["DESRUBRO"].ToString(); if (db.item_tag.Any(x => x.name == _tag && x.id_company == id_company)) { continue; } item_tag tag = new item_tag(); tag.id_company = id_company; tag.name = (string)row["DESRUBRO"]; db.item_tag.Add(tag); db.SaveChanges(); } } dt.Clear(); }
private accounting_journal calc(AccountingJournalDB context, sales_invoice sales_invoice, accounting_cycle accounting_cycle) { //List<accounting_journal> ListAccountingJournal = new List<accounting_journal>(); accounting_journal accounting_journal = new accounting_journal(); if (sales_invoice.accounting_journal == null) { // accounting_journal accounting_journal = new accounting_journal(); accounting_journal.id_cycle = accounting_cycle.id_cycle; accounting_journal.comment = sales_invoice.comment; accounting_journal.trans_date = sales_invoice.trans_date; accounting_journal.State = EntityState.Added; List <accounting_journal_detail> accounting_journal_detailList = new List <accounting_journal_detail>(); //List<sales_invoice_detail> _sales_invoice_detail = sales_invoice.sales_invoice_detail.ToList(); Asset.Inventory Inventory = new Asset.Inventory(); foreach (sales_invoice_detail sales_invoice_detail in sales_invoice.sales_invoice_detail.ToList()) { accounting_chart INV_Chart = null; if (sales_invoice_detail.item != null) { if (sales_invoice_detail.item.item_tag_detail != null) { foreach (item_tag_detail item_tag_detail in sales_invoice_detail.item.item_tag_detail.ToList()) { item_tag item_tag = item_tag_detail.item_tag; if (Inventory.find_Chart(context, item_tag) != null) { INV_Chart = Inventory.find_Chart(context, item_tag); } } } } else { INV_Chart = Inventory.find_Chart(context, null); } if (INV_Chart != null) { accounting_journal_detail INV_accounting_journal = new accounting_journal_detail(); INV_accounting_journal.accounting_chart = INV_Chart; if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault() != null) { INV_accounting_journal.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx; } else { INV_accounting_journal.id_currencyfx = sales_invoice_detail.sales_invoice.id_currencyfx; } INV_accounting_journal.credit = sales_invoice_detail.SubTotal; INV_accounting_journal.trans_date = sales_invoice.trans_date; accounting_journal_detailList.Add(INV_accounting_journal); } } foreach (sales_invoice_detail sales_invoice_detail in sales_invoice.sales_invoice_detail.ToList()) { foreach (app_vat_group_details app_vat_group_details in sales_invoice_detail.app_vat_group.app_vat_group_details) { Asset.ValueAddedTax VAT = new Asset.ValueAddedTax(); accounting_chart VAT_Chart = VAT.find_Chart(context, app_vat_group_details.app_vat); if (VAT_Chart != null) { accounting_journal_detail VAT_accounting_journal = new accounting_journal_detail(); VAT_accounting_journal.trans_date = sales_invoice.trans_date; VAT_accounting_journal.accounting_chart = VAT_Chart; VAT_accounting_journal.credit = Vat.calculate_Vat((sales_invoice_detail.unit_price * sales_invoice_detail.quantity), app_vat_group_details.app_vat.coefficient); if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault() != null) { VAT_accounting_journal.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx; } else { VAT_accounting_journal.id_currencyfx = sales_invoice_detail.sales_invoice.id_currencyfx; } accounting_journal_detailList.Add(VAT_accounting_journal); } } } List <payment_schedual> payment_schedualLIST = context.payment_schedual.Where(x => x.id_sales_invoice == sales_invoice.id_sales_invoice).ToList(); if (payment_schedualLIST != null) { ///For Loop for each Payment Schedual. foreach (payment_schedual payment_schedual in payment_schedualLIST) { ///Example: 1000$ Invoice. ///Example: 600$ Paid & 400$ NotPaid. ///Payment Done -> Ex. 600$ if (payment_schedual.payment_detail != null && payment_schedual.credit > 0) { Asset.Cash CashAccount = new Asset.Cash(); accounting_chart AR_Chart = CashAccount.find_Chart(context, payment_schedual.payment_detail.app_account); if (AR_Chart != null) { accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail(); PAYaccounting_journal_detail.accounting_chart = AR_Chart; PAYaccounting_journal_detail.trans_date = payment_schedual.trans_date; PAYaccounting_journal_detail.debit = payment_schedual.credit; if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault() != null) { PAYaccounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currencyfx == payment_schedual.id_currencyfx && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx; } else { PAYaccounting_journal_detail.id_currencyfx = payment_schedual.id_currencyfx; } accounting_journal_detailList.Add(PAYaccounting_journal_detail); } } ///Payment Left -> Ex. 400$ else if (payment_schedual.payment_detail == null && payment_schedual.debit > 0) { //Credit Payment Asset.AccountsReceivable AccountsReceivable = new Asset.AccountsReceivable(); accounting_chart AR_Chart = AccountsReceivable.find_Chart(context, sales_invoice.contact); if (AR_Chart != null) { accounting_journal_detail AR_accounting_journal_detail = new accounting_journal_detail(); AR_accounting_journal_detail.accounting_chart = AR_Chart; AR_accounting_journal_detail.trans_date = sales_invoice.trans_date; AR_accounting_journal_detail.debit = payment_schedual.debit - payment_schedual.child.Sum(x => x.credit); if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault() != null) { AR_accounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx; } else { AR_accounting_journal_detail.id_currencyfx = payment_schedual.id_currencyfx; } accounting_journal_detailList.Add(AR_accounting_journal_detail); } } } } ///SUMMARIZE foreach (accounting_journal_detail accounting_journal_detail in accounting_journal_detailList) { int id_chart = accounting_journal_detail.accounting_chart.id_chart; if (accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).Count() == 0) { accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail(); PAYaccounting_journal_detail.id_chart = accounting_journal_detail.accounting_chart.id_chart; PAYaccounting_journal_detail.accounting_chart = accounting_journal_detail.accounting_chart; PAYaccounting_journal_detail.trans_date = accounting_journal_detail.trans_date; PAYaccounting_journal_detail.credit = accounting_journal_detail.credit; PAYaccounting_journal_detail.debit = accounting_journal_detail.debit; if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == accounting_journal_detail.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault() != null) { PAYaccounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting && x.id_currency == accounting_journal_detail.app_currencyfx.id_currency && x.timestamp <= DateTime.Now) .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx; } else { PAYaccounting_journal_detail.id_currencyfx = accounting_journal_detail.id_currencyfx; } accounting_journal.accounting_journal_detail.Add(PAYaccounting_journal_detail); } else { accounting_journal_detail PAYaccounting_journal_detail = accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).FirstOrDefault(); PAYaccounting_journal_detail.credit += accounting_journal_detail.credit; PAYaccounting_journal_detail.debit += accounting_journal_detail.debit; } } accounting_journal.sales_invoice.Add(sales_invoice); } return(accounting_journal); }