public ActionResult create() { purchaseVM purchasevm = new purchaseVM(); //supplier var suplier = db.Supplier_tbl.ToList(); ViewBag.Suplier = new SelectList(suplier, "supplier_id", "Name"); //Warehouse var warehouse = db.Warehouse_tbl.ToList(); ViewBag.Warehouse = new SelectList(warehouse, "warehouse_id", "w_name"); //list of purchase var product = db.product_tbl.Where(p => p.status == 1).ToList(); ViewBag.ProductList = new MultiSelectList(product, "product_id", "p_name"); ViewBag.Message = TempData["Message"]; ViewBag.Status = TempData["Status"]; return(View()); }
public ActionResult create(purchaseVM pvm) { decimal total_amount = 0; if (pvm != null && pvm.product_id.Count() != 0) { purchase p = new purchase(); p.supplier_id = pvm.supplier_id; p.warehouse_id = pvm.warehouse_id; p.total_amount = pvm.total_amount; p.create_date = DateTime.Now; db.purchases.Add(p); db.SaveChanges(); //get the purchas id var id = db.purchases.Find(p.purchase_id); //create purchase invoice id.pInv = "#PINV-" + id.purchase_id + id.supplier_id; //save all data in purchasse detiels page purchase_details purchaseDetails = new purchase_details(); for (int i = 0; i < pvm.product_id.Count(); i++) { purchaseDetails.product_id = pvm.product_id[i]; purchaseDetails.quantity = pvm.quantity[i]; purchaseDetails.purchase_price = pvm.bayPrice[i]; purchaseDetails.sale_price = pvm.salePrice[i]; purchaseDetails.purchase_id = id.purchase_id; purchaseDetails.sku = "SKU=" + purchaseDetails.product_id + "-" + purchaseDetails.purchase_id; db.purchase_details.Add(purchaseDetails); db.SaveChanges(); //samition the total price total_amount += Convert.ToDecimal(purchaseDetails.purchase_price); //update every product colams var pro = db.product_tbl.Find(purchaseDetails.product_id); //first create itemladger ItemladgerController ic = new ItemladgerController(); ic.ladgerItem(Convert.ToInt32(pro.product_id), Convert.ToInt32(purchaseDetails.quantity), "in", Convert.ToInt32(p.warehouse_id), Convert.ToString(id.pInv)); pro.quantity += purchaseDetails.quantity; pro.price = purchaseDetails.sale_price; db.Entry(pro).State = EntityState.Modified; db.SaveChanges(); } //save the total price in id.total_amount = total_amount; db.Entry(id).State = EntityState.Modified; db.SaveChanges(); message = " Purchase Created Is Successfully"; status = true; TempData["Message"] = message; TempData["Status"] = status; return(RedirectToAction("purchaseInvoice", new { id = id.purchase_id })); } else { message = "There Are Some Problems"; } TempData["Message"] = message; TempData["Status"] = status; return(RedirectToAction("create")); }