public ActionResult Edit(int?id)
        {
            var ses   = sesrepo.GetSessionById((User as CustomPrincipal).UserId);
            int orgid = ses.OrganizationId;

            if (id == null || id == 0)
            {
                return(HttpNotFound());
            }
            var detail = db.GetPurchaseDetail(orgid, (int)id);

            if (detail.AssetPurchase == null)
            {
                return(HttpNotFound());
            }
            FA_Purchase sn  = (db.GetBillSerialNo(orgid, (int)id));
            int         BSN = Convert.ToInt32(sn.BillSerialNo);

            ViewBag.BSN = BSN;

            ViewBag.CategoryList   = ddl.GetAssetCategoryList(orgid);
            ViewBag.AssetItemList  = ddl.GetAssetItemList(orgid);
            ViewBag.UnitList       = ddl.GetUnitList(orgid);
            ViewBag.VendorId       = new SelectList(ddl.GetInvVenodrList(orgid), "Id", "Name", detail.AssetPurchase.VendorId);
            ViewBag.OrganizationId = orgid;
            return(View(detail));
        }
        public void EditAssetPurchase(FA_Purchase fapurchase)
        {
            string sql = " update FA_Purchase set " +
                         "  OrganizationId=@OrganizationId,VendorId=@VendorId, BillNo=@BillNo, BillDate=@BillDate, BillDateBS=@BillDateBS," +
                         " TotalAmount=@TotalAmount, " +
                         " VatApplicable=@VatApplicable,VatAmount=@VatAmount,VATPercent=@VATPercent," +
                         " DiscountAmount=@DiscountAmount,TotalWithVat=@TotalWithVat," +
                         " LastUpdatedBy=@LastUpdatedBy, LastUpdatedDate=@LastUpdatedDate " +
                         " where PurchaseId=@PurchaseId";

            using (var db = DbHelper.GetDBConnection())
            {
                //using (var trsn = db.BeginTransaction())
                //{
                db.Query(sql, fapurchase);
                //  trsn.Commit();
                //}
            }
        }
        public ActionResult Details(int?id)
        {
            var         ses   = sesrepo.GetSessionById((User as CustomPrincipal).UserId);
            int         orgid = ses.OrganizationId;
            FA_Purchase sn    = (db.GetBillSerialNo(orgid, (int)id));
            int         BSN   = Convert.ToInt32(sn.BillSerialNo);

            ViewBag.BSN = BSN;
            if (id == null || id == 0)
            {
                return(HttpNotFound());
            }
            var details = db.GetPurchaseDetail(orgid, (int)id);

            if (details.AssetPurchase == null)
            {
                return(HttpNotFound());
            }
            return(View(details));
        }
        public ActionResult Create()
        {
            var ses   = sesrepo.GetSessionById((User as CustomPrincipal).UserId);
            int orgid = ses.OrganizationId;
            //int orgid= orgid;
            FA_Purchase sn = (db.GetBillSerialNo(orgid));

            if (sn == null)
            {
                ViewBag.BSN = 1;
            }
            else
            {
                int sn1 = Convert.ToInt32(sn.BillSerialNo) + 1;
                ViewBag.BSN = sn1;
            }
            ViewBag.CategoryList   = ddl.GetAssetCategoryList(orgid);
            ViewBag.AssetItemList  = ddl.GetAssetItemList(orgid);
            ViewBag.UnitList       = ddl.GetUnitList(orgid);
            ViewBag.VendorId       = new SelectList(ddl.GetInvVenodrList(orgid), "Id", "Name");
            ViewBag.OrganizationId = orgid;
            return(View());
        }
        public int AddAssetPurchase(FA_Purchase fapurchase)
        {
            string sql = " insert into FA_Purchase (" +
                         " OrganizationId,VendorId, BillNo, BillDate, BillDateBS, TotalAmount,VatApplicable,VATPercent,VatAmount, IsVerified," +
                         " DiscountAmount,TotalWithVat,VerifiedBy, VerifiedDate, EnteredBy, EnteredDate, LastUpdatedBy, LastUpdatedDate, IsDeleted, DeletedBy," +
                         " DeletedDate, BillSerialNo" +
                         ")" +
                         " values " +
                         " (" +
                         "@OrganizationId,@VendorId, @BillNo, @BillDate, @BillDateBS, @TotalAmount, @VatApplicable,@VatPercent,@VatAmount, @IsVerified," +
                         " @DiscountAmount,@TotalWithVat,@VerifiedBy, @VerifiedDate, @EnteredBy, @EnteredDate, 0, null, 0, 0," +
                         " null, @BillSerialNo" +
                         ") SELECT CAST(SCOPE_IDENTITY() as int)";

            using (var db = DbHelper.GetDBConnection())
            {
                //using (var trsn = db.BeginTransaction())
                //{
                int a = db.Query <int>(sql, fapurchase).SingleOrDefault();
                //  trsn.Commit();
                //}
                return(a);
            }
        }
        public ActionResult Create(FormCollection frm, string[] hddrowindex)
        {
            var             ses   = sesrepo.GetSessionById((User as CustomPrincipal).UserId);
            int             orgid = ses.OrganizationId;
            int             uid   = (User as CustomPrincipal).UserId;
            FA_Purchase     pbill = new FA_Purchase();
            FA_PurchaseItem pitem;//= new INV_SoldItem();
            int             purchaseid = 0;
            var             cdate      = DateTime.Now;

            try
            {
                if (frm["PurchaseId"] == "0")
                {
                    pbill = new FA_Purchase();
                    pbill.OrganizationId = orgid;
                    pbill.VendorId       = string.IsNullOrEmpty(frm["VendorId"]) ? 0 : Convert.ToInt32(frm["VendorId"]);
                    pbill.BillNo         = frm["BillNo"];
                    pbill.BillSerialNo   = Convert.ToInt32(frm["BillSerialNo"]);
                    pbill.BillSerialNo   = Convert.ToInt32(frm["BillSerialNo"]);
                    ViewBag.BSN2         = Convert.ToInt32(frm["BillSerialNo"]) + 1;
                    pbill.BillDate       = DateTime.ParseExact(frm["BillDate"], "yyyy-MM-dd", null);
                    pbill.BillDateBS     = frm["BillDateBS"];
                    pbill.TotalAmount    = Convert.ToDecimal(frm["TotalAmount"]);
                    //pbill.VatAmount = Convert.ToDecimal(frm["VatAmount"]);

                    pbill.TotalWithVat = Convert.ToDecimal(frm["TotalWithVat"]);

                    if (frm["VatApplicable"] == "Yes")
                    {
                        pbill.VatApplicable = true;
                        pbill.VatAmount     = Convert.ToDecimal(frm["VatAmount"]);
                        pbill.VATPercent    = Convert.ToInt32(frm["VATPercent"]);
                    }
                    else
                    {
                        pbill.VatApplicable = false;
                        pbill.VatAmount     = 0;
                        pbill.VATPercent    = 0;
                    }
                    pbill.DiscountAmount = 0;// Convert.ToDecimal(frm["DiscountAmount"]);
                    pbill.EnteredBy      = uid;
                    pbill.EnteredDate    = cdate;
                    pbill.IsVerified     = true;
                    pbill.VerifiedBy     = uid;
                    pbill.VerifiedDate   = cdate;
                    pbill.IsDeleted      = false;
                    purchaseid           = db.AddAssetPurchase(pbill);
                    //purchaseid = db.AddInvetoryPurchase(pbill);
                }
                else
                {
                    purchaseid = Convert.ToInt32(frm["PurchaseId"]);
                    pbill      = db.GetPurchaseById(orgid, purchaseid);
                    //pbill.OrganizationId = orgid;
                    pbill.VendorId     = string.IsNullOrEmpty(frm["VendorId"]) ? 0 : Convert.ToInt32(frm["VendorId"]);
                    pbill.BillNo       = frm["BillNo"];
                    pbill.BillSerialNo = Convert.ToInt32(frm["BillSerialNo"]);
                    ViewBag.BSN2       = Convert.ToInt32(frm["BillSerialNo"]) + 1;
                    pbill.BillDate     = DateTime.ParseExact(frm["BillDate"], "yyyy-MM-dd", null);
                    pbill.BillDateBS   = frm["BillDateBS"];
                    pbill.TotalAmount  = Convert.ToDecimal(frm["TotalAmount"]);
                    //pbill.VatAmount = Convert.ToDecimal(frm["VatAmount"]);
                    pbill.TotalWithVat = Convert.ToDecimal(frm["TotalWithVat"]);

                    if (frm["VatApplicable"] == "Yes")
                    {
                        pbill.VatApplicable = true;
                        pbill.VatAmount     = Convert.ToDecimal(frm["VatAmount"]);
                        pbill.VATPercent    = Convert.ToInt32(frm["VATPercent"]);
                    }
                    else
                    {
                        pbill.VatApplicable = false;
                        pbill.VatAmount     = 0;
                        pbill.VATPercent    = 0;
                    }
                    pbill.DiscountAmount  = 0;
                    pbill.LastUpdatedBy   = uid;
                    pbill.LastUpdatedDate = cdate;
                    pbill.IsDeleted       = false;
                    db.EditAssetPurchase(pbill);
                }
                if (hddrowindex != null)
                {
                    //INV_SoldItem item;
                    foreach (var indx in hddrowindex)
                    {
                        if (frm["PurchaseItemId-" + indx] == "0")
                        {
                            pitem = new FA_PurchaseItem();
                            pitem.OrganizationId   = orgid;
                            pitem.PurchaseId       = purchaseid;
                            pitem.AssetItemId      = Convert.ToInt32(frm["AssetItemId-" + indx]);
                            pitem.PurchaseQuantity = Convert.ToInt32(frm["PurchaseQuantity-" + indx]);
                            pitem.Rate             = Convert.ToDecimal(frm["Rate-" + indx]);
                            pitem.Total            = Convert.ToDecimal(frm["Total-" + indx]);
                            pitem.RegisterToAsset  = string.IsNullOrEmpty(frm["RegisterToAsset-" + indx]) ? false : true;
                            pitem.IsReturned       = false;
                            pitem.IsVerified       = true;
                            pitem.VerifiedBy       = uid;
                            pitem.VerifiedDate     = cdate;
                            pitem.IsDeleted        = false;
                            pitem.EnteredBy        = uid;
                            pitem.EnteredDate      = cdate;
                            pitem.OrganizationId   = orgid;
                            db.AddAssetPurchaseItem(pitem);
                        }
                        else
                        {
                            pitem = db.GetPurchaseItemById(orgid, Convert.ToInt32(frm["PurchaseItemId-" + indx]));
                            pitem.OrganizationId   = orgid;
                            pitem.PurchaseId       = purchaseid;
                            pitem.AssetItemId      = Convert.ToInt32(frm["AssetItemId-" + indx]);
                            pitem.PurchaseQuantity = Convert.ToInt32(frm["PurchaseQuantity-" + indx]);
                            pitem.Rate             = Convert.ToDecimal(frm["Rate-" + indx]);
                            pitem.Total            = Convert.ToDecimal(frm["Total-" + indx]);
                            pitem.RegisterToAsset  = string.IsNullOrEmpty(frm["RegisterToAsset-" + indx]) ? false : true;
                            pitem.IsReturned       = false;
                            pitem.IsVerified       = true;
                            pitem.VerifiedBy       = uid;
                            pitem.VerifiedDate     = cdate;
                            pitem.IsDeleted        = false;
                            pitem.LastUpdatedBy    = uid;
                            pitem.LastUpdatedDate  = cdate;
                            pitem.OrganizationId   = orgid;
                            db.EditAssetPurchaseItem(pitem);
                        }
                    }
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.CategoryList   = ddl.GetAssetCategoryList(orgid);
                ViewBag.AssetItemList  = ddl.GetAssetItemList(orgid);
                ViewBag.UnitList       = ddl.GetUnitList(orgid);
                ViewBag.VendorId       = new SelectList(ddl.GetInvVenodrList(orgid), "Id", "Name");
                ViewBag.OrganizationId = orgid;
                return(View());
            }
        }
        public ActionResult SavePurchaseBill(string PurchaseId = "", string VendorId = "", string BillNo = "", string BillDate = "", string BillDateBS = "", string TotalAmount = "", string VatApplicable = "", string VatAmount = "0", string DiscountAmount = "0", string BillSerialNo = "")
        {
            var         ses   = sesrepo.GetSessionById((User as CustomPrincipal).UserId);
            int         orgid = ses.OrganizationId;
            int         uid   = (User as CustomPrincipal).UserId;
            FA_Purchase pbill;
            var         cdate = DateTime.Now;

            if (string.IsNullOrEmpty(PurchaseId) || PurchaseId == "0")
            {
                pbill = new FA_Purchase();
                pbill.BillSerialNo   = Convert.ToInt32(BillSerialNo);
                pbill.OrganizationId = orgid;
                pbill.VendorId       = string.IsNullOrEmpty(VendorId) ? 0 : Convert.ToInt32(VendorId);
                pbill.BillNo         = BillNo;
                pbill.BillDate       = DateTime.ParseExact(BillDate, "yyyy-MM-dd", null);
                pbill.BillDateBS     = BillDateBS;
                pbill.TotalAmount    = Convert.ToDecimal(TotalAmount);
                //pbill.VatAmount = Convert.ToDecimal(VatAmount);

                if (VatApplicable == "Yes")
                {
                    pbill.VatApplicable = true;
                    pbill.VatAmount     = string.IsNullOrEmpty(VatAmount) ? 0 : Convert.ToDecimal(VatAmount);
                    //pbill.VATPercent = string.IsNullOrEmpty(VATPercent) ? 0 : Convert.ToInt32(VATPercent);
                }
                else
                {
                    pbill.VatApplicable = false;
                    pbill.VatAmount     = 0;
                    pbill.VATPercent    = 0;
                }

                pbill.DiscountAmount = Convert.ToDecimal(DiscountAmount);     // Convert.ToDecimal(frm["DiscountAmount"]);
                pbill.TotalWithVat   = Convert.ToDecimal(pbill.TotalWithVat); // + pbill.TotalAmount;// Convert.ToDecimal(TotalWithVat);
                pbill.EnteredBy      = uid;
                pbill.EnteredDate    = cdate;
                pbill.IsVerified     = true;
                pbill.VerifiedBy     = uid;
                pbill.VerifiedDate   = cdate;
                pbill.IsDeleted      = false;
                int purchaseid = db.AddAssetPurchase(pbill); //int soldid = db.AddAssetPurchase(pbill);
                return(Json(new { result = purchaseid }));
            }
            else
            {
                int purchaseid = Convert.ToInt32(PurchaseId);
                pbill = db.GetPurchaseById(orgid, purchaseid);
                pbill.OrganizationId = orgid;
                //pbill.BillSerialNo = Convert.ToInt32(BillSerialNo);
                pbill.VendorId    = string.IsNullOrEmpty(VendorId) ? 0 : Convert.ToInt32(VendorId);
                pbill.BillNo      = BillNo;
                pbill.BillDate    = DateTime.ParseExact(BillDate, "yyyy-MM-dd", null);
                pbill.BillDateBS  = BillDateBS;
                pbill.TotalAmount = Convert.ToDecimal(TotalAmount);
                //pbill.VatAmount = Convert.ToDecimal(VatAmount);

                if (VatApplicable == "No")
                {
                    pbill.VatAmount     = 0;
                    pbill.VatApplicable = false;
                }
                else if (VatApplicable == "Yes")
                {
                    pbill.VatAmount     = Convert.ToInt32(VatAmount);
                    pbill.VatApplicable = true;
                }
                pbill.DiscountAmount = Convert.ToDecimal(DiscountAmount);// Convert.ToDecimal(frm["DiscountAmount"]);
                //pbill.TotalWithVat = Convert.ToDecimal(TotalWithVat);
                pbill.LastUpdatedBy   = uid;
                pbill.LastUpdatedDate = cdate;
                pbill.IsDeleted       = false;
                db.EditAssetPurchase(pbill);
                return(Json(new { result = purchaseid }));
            }
        }