public void saveProductPurchased(purchaseproduct purchasedPorduct) { #region commented handling sellertotal receivable table /* SellerTotalPayable totalPayables = dbContext.SellerTotalPayables.Where(p => p.sellerid == purchasedPorduct.sellerid).FirstOrDefault(); if (totalPayables != null) { if(totalPayables.totalreceivable>0) { if(totalPayables.totalreceivable>purchasedPorduct.payableprice) { totalPayables.totalreceivable -= (int)purchasedPorduct.payableprice; totalPayables.totalpayable = 0; purchasedPorduct.totalpayable = 0; purchasedPorduct.totalreceivable = (int) totalPayables.totalreceivable; } else { purchasedPorduct.totalpayable =purchasedPorduct.payableprice- totalPayables.totalreceivable; purchasedPorduct.totalreceivable = 0; totalPayables.totalreceivable = 0; totalPayables.totalpayable = purchasedPorduct.totalpayable; } } else { purchasedPorduct.totalpayable = totalPayables.totalpayable + purchasedPorduct.payableprice; totalPayables.totalpayable = purchasedPorduct.totalpayable; } var entry = dbContext.Entry<SellerTotalPayable>(totalPayables); if (entry.State.Equals(EntityState.Detached)) { dbContext.SellerTotalPayables.Attach(totalPayables); } entry.State = EntityState.Modified; } else { totalPayables = new SellerTotalPayable(); purchasedPorduct.totalpayable = purchasedPorduct.payableprice; totalPayables.seller_cde = purchasedPorduct.seller_cde; totalPayables.sellerid = purchasedPorduct.sellerid; totalPayables.totalpayable = purchasedPorduct.totalpayable; dbContext.SellerTotalPayables.Add(totalPayables); }*/ #endregion //saving in PurchaseProduct table dbContext.purchaseproducts.Add(purchasedPorduct); dbContext.SaveChanges(); #region Khata var result = from n in dbContext.Khatas where n.bpid == purchasedPorduct.purchaserid select n; //now putting entries in KHATA table for purchaser(e.g ans) khata Khata kh = new Khata() { entrycode = "p",//indicates purchaser record purchaseproductid = purchasedPorduct.seqid, saleproductid = 0, activitycode = ActivityType.Purchase.GetStringValue(), date = System.DateTime.Now, productid = purchasedPorduct.productid, bpid = purchasedPorduct.purchaserid, sellertypecode = purchasedPorduct.seller_cde, payable_naam = purchasedPorduct.price, receivable_jama = 0, purchasertypecode = purchasedPorduct.purchaser_cde, totalpayable_naam = (result != null && result.Count() > 0) ? result.Sum(p => p.payable_naam) + purchasedPorduct.price : purchasedPorduct.price, totalreceivable_jama = (result != null && result.Count() > 0) ? result.Sum(p => p.receivable_jama) : 0 }; dbContext.Khatas.Add(kh); //now putting entries in KHATA table for seller(e.g Kissaan) khata result = from n in dbContext.Khatas where n.bpid == purchasedPorduct.sellerid select n; kh = new Khata() { entrycode = "s",//indicates seller record purchaseproductid = purchasedPorduct.seqid, saleproductid = 0, activitycode = ActivityType.Purchase.GetStringValue(), date = System.DateTime.Now, productid = purchasedPorduct.productid, bpid = purchasedPorduct.sellerid, sellertypecode = purchasedPorduct.seller_cde, payable_naam = 0, receivable_jama = purchasedPorduct.payableprice, purchasertypecode = purchasedPorduct.purchaser_cde, totalpayable_naam = (result != null && result.Count() > 0) ? result.Sum(p => p.payable_naam) : 0, totalreceivable_jama = (result != null && result.Count() > 0) ? result.Sum(p => p.receivable_jama) + purchasedPorduct.payableprice : purchasedPorduct.payableprice }; dbContext.Khatas.Add(kh); #endregion #region Other Khata //Now adding in otherKhata table for labor commission etc. //here collecting information about labour khata so as to calculate previous values of naam and jama string khatatypecode = KhataType.Labour.GetStringValue(); var resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase if (purchasedPorduct.selectedProduct.producttype != ProductType.Cash.GetStringValue()&& purchasedPorduct.selectedProduct.producttype != ProductType.cheque.GetStringValue())//there will be no other khata in case of cash. { OtherKhata ok = new OtherKhata() { purchaseproductid = purchasedPorduct.seqid, saleproductid = 0, khatatypecde = KhataType.Labour.GetStringValue(), date = System.DateTime.Now, receivable_jama = purchasedPorduct.labour, payable_naam = 0, productname = purchasedPorduct.productname, weight = purchasedPorduct.totalweight, activitycode = ActivityType.Purchase.GetStringValue(), purchaserid = purchasedPorduct.purchaserid, sellerid = purchasedPorduct.sellerid, sellertypecode = purchasedPorduct.seller_cde, purchasertypecode = purchasedPorduct.purchaser_cde, totalpayable_naam = (resultOtherKhata!= null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + purchasedPorduct.price : purchasedPorduct.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); if (purchasedPorduct.seller_cde != "00002")//if purchasing from commission shop then no commission to be entered { //here collecting information about commission khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Commission.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { purchaseproductid = purchasedPorduct.seqid, saleproductid = 0, khatatypecde = KhataType.Commission.GetStringValue(), date = System.DateTime.Now, receivable_jama = purchasedPorduct.commissionamt, payable_naam = 0, productname = purchasedPorduct.productname, weight = purchasedPorduct.totalweight, activitycode = ActivityType.Purchase.GetStringValue(), purchaserid = purchasedPorduct.purchaserid, sellerid = purchasedPorduct.sellerid, sellertypecode = purchasedPorduct.seller_cde, purchasertypecode = purchasedPorduct.purchaser_cde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) : 0, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) + purchasedPorduct.payableprice : purchasedPorduct.payableprice }; dbContext.OtherKhatas.Add(ok); } } #endregion dbContext.SaveChanges(); }
public void saveProductSold(saleproduct productSold) { try { #region commented code save to feedmilltotla receivaable /* FeedMillTotalRecievable totalRecievable = dbContext.FeedMillTotalRecievables.Where(p => p.feedmillid == productSold.feedmillid).FirstOrDefault(); if (totalRecievable != null) { if(totalRecievable.totalPayable>0) { if(totalRecievable.totalPayable>=productSold.recievableprice) { totalRecievable.totalPayable -= productSold.recievableprice; productSold.totalpayable = totalRecievable.totalPayable; totalRecievable.totalPriceRecievable = 0; productSold.totalpricerecieveable = 0; } else { productSold.totalpricerecieveable = productSold.recievableprice - totalRecievable.totalPayable; totalRecievable.totalPriceRecievable = productSold.totalpricerecieveable; productSold.totalpayable = 0; totalRecievable.totalPayable = 0; } } else { productSold.totalpricerecieveable = totalRecievable.totalPriceRecievable + productSold.recievableprice; totalRecievable.totalPriceRecievable = productSold.totalpricerecieveable; } var entry = dbContext.Entry<FeedMillTotalRecievable>(totalRecievable); if (entry.State.Equals(EntityState.Detached)) { dbContext.FeedMillTotalRecievables.Attach(totalRecievable); } entry.State = EntityState.Modified; } else { totalRecievable = new FeedMillTotalRecievable(); productSold.totalpricerecieveable = productSold.recievableprice; totalRecievable.feedmillid = productSold.feedmillid; totalRecievable.totalPriceRecievable = productSold.totalpricerecieveable; dbContext.FeedMillTotalRecievables.Add(totalRecievable); } */ #endregion //entry in product sold dbContext.saleproducts.Add(productSold); dbContext.SaveChanges(); #region Khata var result = from n in dbContext.Khatas where n.bpid == productSold.purchaserid select n; //entry in khata table for purchaser Khata kh = new Khata() { entrycode = "p",//indicates purchaser record purchaseproductid =0, saleproductid = productSold.seqid, activitycode = ActivityType.Sale.GetStringValue(), date = System.DateTime.Now, productid = productSold.productid, bpid = productSold.purchaserid, //sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, payable_naam = productSold.price, receivable_jama = 0, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (result != null && result.Count() > 0) ? result.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (result != null && result.Count() > 0) ? result.Sum(p => p.receivable_jama) : 0 }; dbContext.Khatas.Add(kh); //now putting entries in KHATA table for seller(e.g Kissaan) khata result = from n in dbContext.Khatas where n.bpid == productSold.sellerid select n; kh = new Khata() { entrycode = "s",//indicates purchaser record purchaseproductid = 0, saleproductid = productSold.seqid, activitycode = ActivityType.Sale.GetStringValue(), date = System.DateTime.Now, productid = productSold.productid, //purchaserid = productSold.purchaserid, bpid = productSold.sellerid, sellertypecode = productSold.sellertypecde, payable_naam = 0, receivable_jama = productSold.recievableprice, totalpayable_naam = (result != null && result.Count() > 0) ? result.Sum(p => p.payable_naam) : 0, totalreceivable_jama = (result != null && result.Count() > 0) ? result.Sum(p => p.receivable_jama) + productSold.recievableprice : productSold.recievableprice, purchasertypecode = productSold.purchasertypecde }; dbContext.Khatas.Add(kh); #endregion //------------------------------------------------------- //Now adding in otherKhata table for labor commission etc. //------------------------------------------------------- #region Other Khata //here collecting information about labour khata so as to calculate previous values of naam and jama string khatatypecode = KhataType.Labour.GetStringValue(); var resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase OtherKhata ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Labour.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.labour, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); //here collecting information about labour khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Commission.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Commission.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.commissionamount, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); //here collecting information about labour khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Bardana.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Bardana.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.bardana, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); //here collecting information about labour khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Fee.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Fee.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.fee, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); //here collecting information about labour khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Munshiyana.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Munshiyana.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.accountcharges, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); //here collecting information about labour khata so as to calculate previous values of naam and jama khatatypecode = KhataType.Sootli.GetStringValue(); resultOtherKhata = from n in dbContext.OtherKhatas where n.khatatypecde == khatatypecode select n;//activity code indicates it is khata agains purchase ok = new OtherKhata() { activitycode = ActivityType.Sale.GetStringValue(), khatatypecde = KhataType.Sootli.GetStringValue(), date = System.DateTime.Now, receivable_jama = productSold.thread, payable_naam = 0, productname = productSold.productname, weight = productSold.weight, purchaserid = productSold.purchaserid, sellerid = productSold.sellerid, sellertypecode = productSold.sellertypecde, purchasertypecode = productSold.purchasertypecde, totalpayable_naam = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.payable_naam) + productSold.price : productSold.price, totalreceivable_jama = (resultOtherKhata != null && resultOtherKhata.Count() > 0) ? resultOtherKhata.Sum(p => p.receivable_jama) : 0 }; dbContext.OtherKhatas.Add(ok); #endregion dbContext.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.Message); throw; } }
private void btnSearch_Click(object sender, RoutedEventArgs e) { HISTORY = new ObservableCollection<OtherKhata>(); string khatatypecode; if (CONTROLLER.ISENGLISHVISIBLE) { if (cmbKhataType_e.SelectedItem != null) { KhataTypeCode khatatype = (KhataTypeCode) cmbKhataType_e.SelectedItem; khatatypecode = khatatype.khatatypecde; //filling the collection as per search criteria using (var db = new HCSMLEntities1()) { var qry = from k in db.OtherKhatas where k.khatatypecde == khatatypecode select k; foreach (var item in (qry)) { OtherKhata kh = new OtherKhata(); kh.date = item.date; kh.activitycode = item.activitycode; kh.id = item.id; kh.payable_naam = item.payable_naam; kh.productname = item.productname; kh.purchaserid = item.purchaserid; kh.purchasertypecode = item.purchasertypecode; kh.receivable_jama = item.receivable_jama; kh.sellerid = item.sellerid; kh.sellertypecode = item.sellertypecode; kh.sellername = getName(kh.sellertypecode, kh.sellerid); kh.purchasername = getName(kh.purchasertypecode, kh.purchaserid); kh.totalpayable_naam = item.totalpayable_naam; kh.totalreceivable_jama = item.totalreceivable_jama; HISTORY.Add(kh); } } RefreshUsers(); //setTotalRecievable(); } } if (CONTROLLER.ISURDUVISIBLE) { if (cmbKhataType_u.SelectedItem != null) { KhataTypeCode khatatype = (KhataTypeCode) cmbKhataType_u.SelectedItem; khatatypecode = khatatype.khatatypecde; //filling the collection as per search criteria using (var db = new HCSMLEntities1()) { var qry = from k in db.OtherKhatas where k.khatatypecde == khatatypecode select k; foreach (var item in (qry)) { OtherKhata kh = new OtherKhata(); kh.date = item.date; kh.activitycode = item.activitycode; kh.id = item.id; kh.payable_naam = item.payable_naam; kh.productname = item.productname; kh.purchaserid = item.purchaserid; kh.purchasertypecode = item.purchasertypecode; kh.receivable_jama = item.receivable_jama; kh.sellerid = item.sellerid; kh.sellertypecode = item.sellertypecode; kh.sellername = getName(kh.sellertypecode, kh.sellerid); kh.purchasername = getName(kh.purchasertypecode, kh.purchaserid); kh.totalpayable_naam = item.totalpayable_naam; kh.totalreceivable_jama = item.totalreceivable_jama; HISTORY.Add(kh); } } RefreshUsers(); //setTotalRecievable(); } } }