public JsonResult createInvoice(int?id) { if (id != null) { //get invoice id var sa = db.site_setting_tbl.Find(1); int sireal = Convert.ToInt32(sa.Invoic); //increament id sireal++; //get order tbl data var os = db.order_tbl.Find(id); os.invoice_no = sireal; os.invoice_prefix = "#INV"; db.Entry(os).State = EntityState.Modified; db.SaveChanges(); //update setting tbl invoice row sa.Invoic = sireal; db.Entry(sa).State = EntityState.Modified; db.SaveChanges(); //quntity the cart product var or = db.order_product_tbl.Where(a => a.order_id == os.order_id).ToList(); foreach (var u in or) { //get order_product tbl var o = db.order_product_tbl.Find(u.order_product_id); if (o.varints != null || o.varints != "null") { List <int> s = JsonConvert.DeserializeObject <List <int> >(o.varints); if (s != null) { foreach (var i in s) { //update varinat iunt var pvv = db.product_variant_value.Find(i); pvv.quentity -= u.quantity; db.Entry(pvv).State = EntityState.Modified; db.SaveChanges(); } } } //update product quantity var p = db.product_tbl.Find(u.product_id); //first create itemladger ItemladgerController ic = new ItemladgerController(); ic.ladgerItem(Convert.ToInt32(p.product_id), Convert.ToInt32(u.quantity), "out", Convert.ToInt32(os.WareHouseID), Convert.ToString(os.invoice_prefix + os.invoice_no)); p.quantity -= u.quantity; db.Entry(p).State = EntityState .Modified; db.SaveChanges(); //update dicount table quantity // get discount var date = DateTime.Today; var d = db.product_discount_tbl.OrderBy(a => a.priority).Where(a => a.date_start <= date && a.date_end >= date && a.quantity != 0 && a.product_id == u.product_id).FirstOrDefault(); if (d != null) { d.quantity -= u.quantity; db.Entry(d).State = EntityState.Modified; } } } return(Json(false, JsonRequestBehavior.AllowGet)); }
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")); }