public ActionResult AddNewInvoice(AddNewInvoice addNewInvoice) { //add to details table RepoProcurement <Models.Invoice> invoice = new RepoProcurement <Models.Invoice>(); RepoProcurement <Models.InvoiceDetail> invoiceDetails = new RepoProcurement <Models.InvoiceDetail>(); //if (Session["InvoiceNumber"] == null) //{ // db.Invoice.Add(addNewInvoice.invoice); // db.SaveChanges(); // Session["InvoiceNumber"] = addNewInvoice.invoice.Id; //} var invoiceexists = db.Invoice.FirstOrDefault(e => e.InvoiceNo == addNewInvoice.invoice.InvoiceNo); if (invoiceexists == null) { var SupplierId = db.supplier.FirstOrDefault(e => e.Supplier_Name == addNewInvoice.invoice.SupplierName); addNewInvoice.invoice.SupplierId = SupplierId.Supplier_Code; db.Invoice.Add(addNewInvoice.invoice); db.SaveChanges(); invoiceexists = db.Invoice.FirstOrDefault(e => e.InvoiceNo == addNewInvoice.invoice.InvoiceNo); Session["InvoiceNumber"] = addNewInvoice.invoice.Id; } //make the last invoice the invoice number for the invoice detail addNewInvoice.invoiceDetails.InvoiceNo = invoiceexists.Id; var drugid = db.Drug.Where(p => p.Name.Contains(addNewInvoice.invoiceDetails.Item)).FirstOrDefault().Id; addNewInvoice.invoiceDetails.DrugId = drugid; if (invoiceexists == null) { //add new invoice detail if invoice db.InvoiceDetail.Add(addNewInvoice.invoiceDetails); db.SaveChanges(); } else if (invoiceexists != null && !invoiceexists.FinalApproval) { //add new invoice detail if invoice hasent been authorized db.InvoiceDetail.Add(addNewInvoice.invoiceDetails); db.SaveChanges(); } int invNo = Convert.ToInt32(Session["InvoiceNumber"]); var model = db.InvoiceDetail.Where(p => p.InvoiceNo == invoiceexists.Id).OrderByDescending(p => p.Id).ToList(); return(PartialView("~/Areas/Procurement/Views/ProcurementPurchase/_InvoiceList.cshtml", model)); }
private async void btnAddInvoice_OnClick(object sender, RoutedEventArgs e) { if (this.cbxGroupList.SelectedIndex < 1) { await this.ShowMessageAsync("SAVE INVOICE", "Please select a group!"); return; } if (this.txtPrice.Text == String.Empty) { await this.ShowMessageAsync("SAVE INVOICE", "Price cannot be empty!"); return; } double price; if (!double.TryParse(this.txtPrice.Text, out price)) { await this.ShowMessageAsync("SAVE INVOICE", "Enter a valid price!"); return; } var groupId = this._groupList[this.cbxGroupList.SelectedIndex].GroupId; var userId = this._userId; var time = DateTime.Now.ToString(CultureInfo.InvariantCulture); var invoice = new Invoice { GroupId = groupId, UserId = userId, BuyDate = time, IsExpired = false, Price = price }; var add = new AddNewInvoice(this._dbo); if (add.Insert(invoice)) { await this.ShowMessageAsync("SAVE INVOICE", "Inovice Saved!"); var invoiceId = add.GetLastInvoiceId(); if (this._productList.Count > 0) { // eger product eklenmisse kaydet var saveProduct = new AddNewProduct(this._dbo); if (saveProduct.Insert(this._productList, invoiceId)) { await this.ShowMessageAsync("SAVE PRODUCTS", "Products Saved!"); ClearInvoiceScreen(); } else { await this.ShowMessageAsync("SAVE PRODUCTS", "Something gone wrong!"); } } } else { await this.ShowMessageAsync("SAVE INVOICE", "Something gone wrong!"); } }