public bool SaveIncome(tblIncomeSource income)
        {
            long maxId = context.tblIncomeSources.Select(cc => cc.ID).DefaultIfEmpty(0).Max();

            income.ID = ++maxId;
            context.tblIncomeSources.Add(income);
            return(context.SaveChanges() > 0);
        }
        public bool SaveCar(tblCar objcar)
        {
            int maxId = context.tblCars.Select(p => p.ID).DefaultIfEmpty(0).Max();

            objcar.ID = ++maxId;
            context.tblCars.Add(objcar);
            return(context.SaveChanges() > 0);
        }
        public bool SaveConsumption(tblCostingSource consumption)
        {
            long maxId = context.tblCostingSources.Select(cc => cc.ID).DefaultIfEmpty(0).Max();

            consumption.ID = ++maxId;
            context.tblCostingSources.Add(consumption);
            return(context.SaveChanges() > 0);
        }
Beispiel #4
0
        public bool EditProduct(tblProduct product)
        {
            var existProd = context.tblProducts.Where(pp => pp.ID >= product.ID).FirstOrDefault();

            existProd.productName = product.productName;
            existProd.parentId    = product.parentId;
            return(context.SaveChanges() > 0);
        }
        public bool SaveSector(tblCommonElement sector)
        {
            int maxId = context.tblCommonElements.Select(cc => cc.ID).DefaultIfEmpty(0).Max();

            sector.ID = ++maxId;
            //sector.elementCode = 3;
            context.tblCommonElements.Add(sector);
            return(context.SaveChanges() > 0);
        }
Beispiel #6
0
        // just try to avoid opening values
        public Employee SaveEmp(Employee employee)
        {
            try
            {
                long maxId = context.Employees.Select(p => p.ID).DefaultIfEmpty(0).Max();
                employee.ID = ++maxId;
                context.Employees.Add(employee);

                return(context.SaveChanges() > 0 ? employee : null);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
Beispiel #7
0
        public long SaveBalancePaddy(tblDue objDue)
        {
            try
            {
                #region save dueable
                long maxId = context.tblDues.Select(p => p.ID).DefaultIfEmpty(0).Max();
                objDue.ID       = ++maxId;
                objDue.isActive = 1;
                var lastDueId =
                    context.tblDues.Where(p => p.partyId == objDue.partyId)
                    .Select(p => p.ID)
                    .DefaultIfEmpty()
                    .Max();

                if (lastDueId > 0)
                {
                    var lastdue = context.tblDues.Where(p => p.ID == lastDueId).FirstOrDefault();
                    lastdue.isActive      = 0;
                    objDue.openingBalance = lastdue.openingBalance + objDue.amount;
                }
                else
                {
                    objDue.openingBalance = objDue.amount;
                }
                context.tblDues.Add(objDue);
                #endregion
                return(context.SaveChanges() > 0 ? objDue.ID : 0);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
Beispiel #8
0
        public bool SaveLoaner(tblLoanar loaner)
        {
            int maxId = context.tblLoanars.Select(p => p.ID).DefaultIfEmpty(0).Max();

            loaner.ID = ++maxId;
            context.tblLoanars.Add(loaner);
            return(context.SaveChanges() > 0);
        }
Beispiel #9
0
        public BagTransaction SaveBag(BagTransaction objBag)
        {
            long maxId = context.BagTransactions.Select(s => s.ID).DefaultIfEmpty(0).Max();

            var results = from sack in context.BagTransactions
                          where sack.partyId == objBag.partyId.Value && sack.date <= objBag.date && (sack.rcvPrice == 0 || sack.rcvPrice == null) && (sack.sentPrice == 0 || sack.sentPrice == null)
                          orderby sack.date descending, sack.ID descending
            select sack;
            var prevDue = results.FirstOrDefault();

            var pric = (from sack in context.BagTransactions
                        where sack.partyId == objBag.partyId.Value && sack.date <= objBag.date && (sack.rcvPrice > 0 || sack.sentPrice > 0)
                        orderby sack.date descending, sack.ID descending
                        select sack);
            var prevprice = pric.FirstOrDefault();

            if (objBag.sentPrice > 0)
            {
                int bag = objBag.comSentBag > 0 ? objBag.comSentBag.Value : 1;
                objBag.priceDues = prevprice.priceDues - objBag.sentPrice * bag;

                var nextprices = from sack in context.BagTransactions
                                 where sack.partyId == objBag.partyId.Value && sack.date > objBag.date && (sack.rcvPrice > 0 || sack.sentPrice > 0)
                                 select sack;
                foreach (var item in nextprices)
                {
                    item.priceDues -= objBag.sentPrice * bag;
                }
            }
            else
            {
                objBag.bagDues = prevDue.bagDues - objBag.comSentBag;

                var nextBags = from sack in context.BagTransactions
                               where sack.partyId == objBag.partyId.Value && sack.date > objBag.date && (sack.rcvPrice == 0 || sack.rcvPrice == null) && (sack.sentPrice == 0 || sack.sentPrice == null)
                               select sack;
                foreach (var item in nextBags)
                {
                    item.bagDues -= objBag.comSentBag;
                }
            }

            objBag.ID = ++maxId;
            context.BagTransactions.Add(objBag);
            return(context.SaveChanges() > 0 ? objBag : null);
        }
        public long SavePaddyTransfer(STK_Balance objStkBalance)
        {
            var transferFrom =
                context.STK_Balance.Where(
                    ss => ss.stockId == objStkBalance.stockId && ss.productId == objStkBalance.productId && ss.sackWeight == objStkBalance.sackWeight).FirstOrDefault();

            transferFrom.sackQuantity = transferFrom.sackQuantity - objStkBalance.sackQuantity;
            if (transferFrom.sackQuantity < 0)
            {
                return(0);
            }
            var transferTo = context.STK_Balance.Where(
                ss => ss.stockId == objStkBalance.targetStockId && ss.productId == objStkBalance.productId && ss.sackWeight == objStkBalance.sackWeight).FirstOrDefault();

            if (transferTo != null)
            {
                transferTo.sackQuantity += objStkBalance.sackQuantity;
            }
            else
            {
                var         maxId   = context.STK_Balance.Select(ss => ss.ID).DefaultIfEmpty(0).Max();
                STK_Balance stockTo = new STK_Balance();
                stockTo.ID         = ++maxId;
                stockTo.stockId    = objStkBalance.targetStockId;
                stockTo.sackWeight = objStkBalance.sackWeight;
                context.STK_Balance.Add(stockTo);
            }
            #region paddy transaction
            var maxTransId = context.PaddyTransactions.Select(ss => ss.ID).DefaultIfEmpty(0).Max();
            PaddyTransaction exstRlsStk  = context.PaddyTransactions.Where(pp => pp.prodId == objStkBalance.productId && pp.stockId == objStkBalance.stockId && pp.bagWeight == objStkBalance.sackWeight).FirstOrDefault();
            PaddyTransaction objTransRls = new PaddyTransaction();
            objTransRls.ID           = ++maxTransId;
            objTransRls.date         = objStkBalance.date;
            objTransRls.bagWeight    = objStkBalance.sackWeight;
            objTransRls.stockId      = objStkBalance.stockId.Value;
            objTransRls.prodId       = objStkBalance.productId.Value;
            objTransRls.releaseQty   = objStkBalance.sackQuantity;
            objTransRls.openingStock = exstRlsStk == null ? 0 : exstRlsStk.openingStock - objStkBalance.sackQuantity.Value;
            context.PaddyTransactions.Add(objTransRls);

            PaddyTransaction exstRcvStk  = context.PaddyTransactions.Where(pp => pp.prodId == objStkBalance.productId && pp.stockId == objStkBalance.targetStockId && pp.bagWeight == objStkBalance.sackWeight).FirstOrDefault();
            PaddyTransaction objTransRcv = new PaddyTransaction();
            objTransRcv.ID           = ++maxTransId;
            objTransRcv.date         = objStkBalance.date;
            objTransRcv.bagWeight    = objStkBalance.sackWeight;
            objTransRcv.stockId      = objStkBalance.targetStockId;
            objTransRcv.prodId       = objStkBalance.productId.Value;
            objTransRcv.rcvQty       = objStkBalance.sackQuantity;
            objTransRcv.openingStock = exstRcvStk == null ? 0 : exstRcvStk.openingStock + objStkBalance.sackQuantity.Value;
            objTransRcv.serial       = objStkBalance.serial;
            objTransRcv.millCost     = objStkBalance.millCost;
            objTransRcv.fromStock    = objStkBalance.stockId;
            context.PaddyTransactions.Add(objTransRcv);
            #endregion
            return(context.SaveChanges() > 0 ? objTransRcv.ID : 0);
        }
        public bool EditStock(STK_tblStock stk)
        {
            var existStk = context.STK_tblStock.Where(ss => ss.ID >= stk.ID).FirstOrDefault();

            existStk.stockName    = stk.stockName;
            existStk.stockAddress = stk.stockAddress;
            return(context.SaveChanges() > 0);
        }
 public tblCostingSource Save(tblCostingSource withdraw)
 {
     try
     {
         long maxId = context.tblCostingSources.Select(p => p.ID).DefaultIfEmpty(0).Max();
         withdraw.ID = ++maxId;
         context.tblCostingSources.Add(withdraw);
         return(context.SaveChanges() > 0 ? withdraw : null);
     }
     catch (Exception exc)
     {
         throw new Exception(exc.Message);
     }
 }
        public long Save(tblCostingSource objLoanCostingSource)
        {
            try
            {
                long maxId    = context.tblCostingSources.Select(ss => ss.ID).DefaultIfEmpty(0).Max();
                long maxDueId = context.tblDues.Select(pp => pp.ID).DefaultIfEmpty(0).Max();
                objLoanCostingSource.ID             = ++maxId;
                objLoanCostingSource.srcDescription = "ধান ক্রয় বাবদ ব্যায় পরিশোধ";
                objLoanCostingSource.sourceName     = "ধান";
                objLoanCostingSource.srcDescId      = 23;
                objLoanCostingSource.isDue          = 1;
                context.tblCostingSources.Add(objLoanCostingSource);

                tblDue latestDue =
                    context.tblDues.Where(ss => ss.partyId == objLoanCostingSource.partyId && ss.date <= objLoanCostingSource.date).OrderByDescending(ss => ss.date).ThenByDescending(ss => ss.ID).FirstOrDefault();
                List <tblDue> nextDues = context.tblDues.Where(dd => dd.partyId == objLoanCostingSource.partyId && dd.date > objLoanCostingSource.date).ToList();
                //dueChange.isActive = 0;
                tblDue newDue = new tblDue();
                newDue.ID = ++maxDueId;
                //newDue.isActive = 1;
                newDue.costingId = objLoanCostingSource.ID;
                newDue.amount    = (-1) * objLoanCostingSource.amount;
                if (latestDue == null)
                {
                    newDue.openingBalance = newDue.amount;
                }
                else
                {
                    newDue.openingBalance = latestDue.openingBalance + newDue.amount;
                }
                newDue.date    = objLoanCostingSource.date;
                newDue.partyId = objLoanCostingSource.partyId.Value;

                if (nextDues != null)
                {
                    foreach (var item in nextDues)
                    {
                        item.openingBalance += newDue.amount;
                    }
                }

                context.tblDues.Add(newDue);

                return(context.SaveChanges() > 0 ? objLoanCostingSource.ID : 0);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
        public long Save(tblIncomeSource objDueIncomeSource)
        {
            try
            {
                long maxId    = context.tblIncomeSources.Select(ss => ss.ID).DefaultIfEmpty(0).Max();
                long maxPayId = context.tblPayables.Select(pp => pp.ID).DefaultIfEmpty(0).Max();
                objDueIncomeSource.ID = ++maxId;
                if (objDueIncomeSource.description != null)
                {
                    objDueIncomeSource.description = objDueIncomeSource.sourceName + " বকেয়া বাবদ আয়" + "," + objDueIncomeSource.description;
                }
                else
                {
                    objDueIncomeSource.description = objDueIncomeSource.sourceName + " বকেয়া বাবদ আয়";
                }

                objDueIncomeSource.isDue = 1;

                context.tblIncomeSources.Add(objDueIncomeSource);

                tblPayable payableChange =
                    context.tblPayables.Where(ss => ss.partyId == objDueIncomeSource.partyId && ss.isActive == 1).FirstOrDefault();
                payableChange.isActive = 0;
                tblPayable newPayable = new tblPayable();
                newPayable.ID             = ++maxPayId;
                newPayable.isActive       = 1;
                newPayable.amount         = (-1) * objDueIncomeSource.amount;
                newPayable.openingBalance = payableChange.openingBalance - objDueIncomeSource.amount;
                newPayable.date           = objDueIncomeSource.date;
                newPayable.partyId        = objDueIncomeSource.partyId.Value;
                newPayable.incSrcId       = objDueIncomeSource.ID;
                context.tblPayables.Add(newPayable);

                return(context.SaveChanges() > 0 ? objDueIncomeSource.ID : 0);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
Beispiel #15
0
 public bool SaveUser(tblUser user)
 {
     user.password = Md5.CalculateMD5Hash(user.password);
     context.tblUsers.Add(user);
     return(context.SaveChanges() > 0);
 }
        // just try to avoid opening values
        public long SavePaddy(tblBuy paddyInfo)
        {
            try
            {
                long maxId = context.tblBuys.Select(p => p.ID).DefaultIfEmpty(0).Max();
                paddyInfo.ID = ++maxId;
                if (paddyInfo.amount > 0 || paddyInfo.labourCostPerBag > 0 || paddyInfo.transportCost > 0)
                {
                    #region costing source
                    long             cstId   = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max();
                    tblCostingSource costObj = new tblCostingSource();
                    costObj.ID             = ++cstId;
                    costObj.sourceName     = "ধান"; // shoul be come from commonelement
                    costObj.srcDescId      = 23;    // should be come from commonelemnt
                    costObj.srcDescription = "ধান ক্রয় বাবদ খরচ";
                    // costObj.amount =  paddyInfo.amount + labCost* paddyInfo.noOfBag+ paddyInfo.transportCost ?? 0;
                    costObj.labourCostPerBag = paddyInfo.labourCostPerBag ?? 0;
                    if (paddyInfo.transportCostInclude)
                    {
                        costObj.transportCost = paddyInfo.transportCost ?? 0;
                    }
                    else
                    {
                        costObj.transportCost = 0;
                    }

                    costObj.amount  = paddyInfo.amount;
                    costObj.date    = paddyInfo.date;
                    costObj.partyId = paddyInfo.partyId;
                    costObj.buyId   = paddyInfo.ID;
                    context.tblCostingSources.Add(costObj);

                    #endregion
                }

                #region save dues
                long   maxdueId = context.tblDues.Select(i => i.ID).DefaultIfEmpty(0).Max();
                tblDue objDue   = new tblDue();
                objDue.ID      = ++maxdueId;
                objDue.partyId = paddyInfo.partyId;
                objDue.buyId   = paddyInfo.ID;
                objDue.date    = paddyInfo.date;

                //tblDue dueItem = context.tblDues.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                //List<tblDue> dueItems = context.tblDues.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId).ToList();
                double?totalPr = paddyInfo.price * paddyInfo.quantityPerBag / 40 * paddyInfo.noOfBag;
                var    due     = totalPr - paddyInfo.amount;
                objDue.amount = due;
                //if (dueItem == null)
                //{
                //    objDue.openingBalance = objDue.amount;
                //}
                //else
                //{
                //    objDue.openingBalance = dueItem.openingBalance + objDue.amount;
                //}
                //if (dueItems != null)
                //{
                //    foreach (var item in dueItems)
                //    {
                //        item.openingBalance += objDue.amount;
                //    }
                //}
                context.tblDues.Add(objDue);
                #endregion

                context.tblBuys.Add(paddyInfo);

                #region stock balance
                STK_Balance padStk = context.STK_Balance.Where(ss => ss.stockId == paddyInfo.stockId && ss.productId == paddyInfo.productId && ss.sackWeight == paddyInfo.quantityPerBag).FirstOrDefault();
                if (padStk != null)
                {
                    padStk.sackQuantity += paddyInfo.noOfBag;
                }
                else
                {
                    STK_Balance newStk   = new STK_Balance();
                    int         maxbalId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max();
                    newStk.ID           = ++maxbalId;
                    newStk.productId    = paddyInfo.productId;
                    newStk.stockId      = paddyInfo.stockId;
                    newStk.sackWeight   = paddyInfo.quantityPerBag;
                    newStk.sackQuantity = paddyInfo.noOfBag;
                    context.STK_Balance.Add(newStk);
                }
                #endregion

                #region stock transaction
                long maxprdstkId = context.PaddyTransactions.Select(p => p.ID).DefaultIfEmpty(0).Max();
                long laststkId   = context.PaddyTransactions.Where(s => s.stockId == paddyInfo.stockId && s.prodId == paddyInfo.productId && s.bagWeight == paddyInfo.quantityPerBag).Select(l => l.ID).DefaultIfEmpty(0).Max();
                var  lastTrans   = context.PaddyTransactions.Where(ll => ll.ID == laststkId).FirstOrDefault();

                PaddyTransaction objStkTrans = new PaddyTransaction();
                objStkTrans.ID           = maxprdstkId + 1;
                objStkTrans.date         = paddyInfo.date;
                objStkTrans.rcvQty       = paddyInfo.noOfBag;
                objStkTrans.releaseQty   = 0;
                objStkTrans.stockId      = paddyInfo.stockId;
                objStkTrans.prodId       = paddyInfo.productId;
                objStkTrans.buyId        = paddyInfo.ID;
                objStkTrans.bagWeight    = paddyInfo.quantityPerBag;
                objStkTrans.openingStock = lastTrans == null ? paddyInfo.noOfBag : lastTrans.openingStock + paddyInfo.noOfBag;
                context.PaddyTransactions.Add(objStkTrans);
                #endregion

                #region BagTransactions
                long maxBagId = context.BagTransactions.Select(b => b.ID).DefaultIfEmpty(0).Max();
                if (paddyInfo.bagPrice > 0)
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice > 0 || bb.sentPrice > 0)).ToList();
                    //int bagCnt = paddyInfo.noOfBag > 0 ? paddyInfo.noOfBag : 1;
                    double?curPriceDue = prevItem == null ? paddyInfo.noOfBag * paddyInfo.bagPrice : prevItem.priceDues + paddyInfo.noOfBag * paddyInfo.bagPrice;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = paddyInfo.bagPrice;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = curPriceDue;
                    bagTrans.bagDues    = 0;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.priceDues += curPriceDue;
                        }
                    }
                }
                else
                {
                    BagTransaction        prevItem  = context.BagTransactions.Where(bb => bb.date <= paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).OrderByDescending(oo => oo.date).ThenByDescending(ii => ii.ID).FirstOrDefault();
                    List <BagTransaction> nextItems = context.BagTransactions.Where(bb => bb.date > paddyInfo.date && bb.partyId == paddyInfo.partyId && (bb.rcvPrice == 0 || bb.rcvPrice == null) && (bb.sentPrice == 0 || bb.sentPrice == null)).ToList();
                    int?curBagDue = prevItem == null ? paddyInfo.noOfBag : prevItem.bagDues + paddyInfo.noOfBag;

                    BagTransaction bagTrans = new BagTransaction();
                    bagTrans.ID         = ++maxBagId;
                    bagTrans.partyId    = paddyInfo.partyId;
                    bagTrans.rcvId      = paddyInfo.ID;
                    bagTrans.comRcvBag  = paddyInfo.noOfBag;
                    bagTrans.rcvPrice   = 0;
                    bagTrans.comSentBag = 0;
                    bagTrans.sentPrice  = 0;
                    bagTrans.priceDues  = 0;
                    bagTrans.bagDues    = curBagDue;
                    bagTrans.date       = paddyInfo.date;
                    context.BagTransactions.Add(bagTrans);

                    if (nextItems != null)
                    {
                        foreach (var item in nextItems)
                        {
                            item.bagDues += curBagDue;
                        }
                    }
                }

                #endregion

                return(context.SaveChanges() > 0 ? paddyInfo.ID : 0);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
Beispiel #17
0
        public tblSell SaveHusk(tblSell huskInfo)
        {
            try
            {
                long maxId = context.tblSells.Select(p => p.ID).DefaultIfEmpty(0).Max();
                huskInfo.ID = ++maxId;

                if (huskInfo.paidAmount > 0)
                {
                    #region income source
                    long            incId     = context.tblIncomeSources.Select(i => i.ID).DefaultIfEmpty(0).Max();
                    tblIncomeSource incomeObj = new tblIncomeSource();
                    incomeObj.ID          = ++incId;
                    incomeObj.partyId     = huskInfo.partyId;
                    incomeObj.sourceName  = "তুষ"; // shoul be come from commonelement
                    incomeObj.srcDescId   = 21;    // should be come from commonelemnt
                    incomeObj.description = "তুষ বিক্রয় বাবদ আয়";
                    incomeObj.amount      = huskInfo.paidAmount;
                    incomeObj.date        = huskInfo.date;

                    context.tblIncomeSources.Add(incomeObj);
                    #endregion
                    huskInfo.incSrcId = incomeObj.ID;
                }

                #region save payable

                double?totalPr = 0;
                if (huskInfo.transportCostInclude)
                {
                    totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity + huskInfo.transportCost;
                }
                else
                {
                    totalPr = huskInfo.noOfBag * huskInfo.unitPrice * huskInfo.quantity;
                    if (huskInfo.transportCost > 0)
                    {
                        #region costing source
                        tblCostingSource objTblCostingSource = new tblCostingSource();
                        long             maxCost             = context.tblCostingSources.Select(i => i.ID).DefaultIfEmpty(0).Max();
                        objTblCostingSource.ID             = ++maxCost;
                        objTblCostingSource.partyId        = huskInfo.partyId;
                        objTblCostingSource.amount         = Convert.ToDouble(huskInfo.transportCost);
                        objTblCostingSource.sourceName     = "তুষ";
                        objTblCostingSource.srcDescription = "তুষ বিক্রয়ের পরিবহন খরচ";
                        objTblCostingSource.srcDescId      = 21;
                        objTblCostingSource.date           = huskInfo.date;
                        objTblCostingSource.sellId         = huskInfo.ID;

                        context.tblCostingSources.Add(objTblCostingSource);
                        #endregion
                    }
                }
                if (totalPr > 0)
                {
                    long       maxpayId      = context.tblPayables.Select(i => i.ID).DefaultIfEmpty(0).Max();
                    tblPayable objTblPayable = new tblPayable();
                    objTblPayable.ID      = ++maxpayId;
                    objTblPayable.partyId = huskInfo.partyId;
                    objTblPayable.date    = huskInfo.date;
                    objTblPayable.sellId  = huskInfo.ID;
                    var lastPayable = context.tblPayables.Where(p => p.partyId == huskInfo.partyId && p.isActive == 1).FirstOrDefault();


                    var loan = totalPr - huskInfo.paidAmount;
                    objTblPayable.amount = loan;
                    if (lastPayable != null)
                    {
                        lastPayable.isActive         = 0;
                        objTblPayable.openingBalance = objTblPayable.amount + lastPayable.openingBalance;
                    }
                    else
                    {
                        objTblPayable.openingBalance = objTblPayable.amount;
                    }

                    objTblPayable.isActive = 1;

                    context.tblPayables.Add(objTblPayable);
                }
                #endregion
                context.tblSells.Add(huskInfo);

                #region substract husk from stock
                STK_Balance huskStk = context.STK_Balance.Where(ss => ss.stockId == huskInfo.stockId && ss.productId == huskInfo.productId).FirstOrDefault();//&& ss.sackWeight==huskInfo.quantity
                if (huskStk == null)
                {
                    var         maxStkBalId = context.STK_Balance.Select(p => p.ID).DefaultIfEmpty(0).Max();
                    STK_Balance stkBal      = new STK_Balance();
                    stkBal.ID           = ++maxStkBalId;
                    stkBal.productId    = huskInfo.productId;
                    stkBal.stockId      = huskInfo.stockId;
                    stkBal.sackQuantity = -huskInfo.noOfBag;
                    context.STK_Balance.Add(stkBal);
                }
                else
                {
                    huskStk.sackQuantity -= huskInfo.noOfBag;
                }
                #endregion

                #region stock transaction
                long maxprdstkId = context.STK_Transaction.Select(p => p.ID).DefaultIfEmpty(0).Max();
                //long laststkId = context.STK_Transaction.Where(s => s.stockId == huskInfo.stockId && s.prodId == huskInfo.productId).Select(l => l.ID).DefaultIfEmpty(0).Max();
                STK_Transaction objStkTrans = new STK_Transaction();
                objStkTrans.ID        = maxprdstkId + 1;
                objStkTrans.date      = huskInfo.date;
                objStkTrans.rcvQty    = 0;
                objStkTrans.sellQty   = huskInfo.noOfBag;
                objStkTrans.stockId   = huskInfo.stockId.Value;
                objStkTrans.prodId    = huskInfo.productId;
                objStkTrans.operation = 1;
                objStkTrans.sellId    = huskInfo.ID;
                if (huskStk == null)
                {
                    objStkTrans.openingStock = -huskInfo.noOfBag;
                }
                else
                {
                    objStkTrans.openingStock = huskStk.sackQuantity.Value;
                }



                //var lastTrans = context.STK_Transaction.Where(ll => ll.ID == laststkId).FirstOrDefault();
                //objStkTrans.openingStock = lastTrans == null ? 0 - huskInfo.noOfBag : lastTrans.openingStock - huskInfo.noOfBag;
                context.STK_Transaction.Add(objStkTrans);
                #endregion
                tblSell newSell = new tblSell();
                newSell.ID       = huskInfo.ID;
                newSell.incSrcId = huskInfo.incSrcId;

                return(context.SaveChanges() > 0 ? newSell : null);
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }
 public bool SaveParty(tblParty party)
 {
     context.tblParties.Add(party);
     return(context.SaveChanges() > 0);
 }