public int UpdatePurchaseIndent(DirectPurchaseRequest model)
 {
     using (IDbConnection connection = OpenConnection(dataConnection))
     {
         IDbTransaction txn = connection.BeginTransaction();
         try
         {
             string query = @"UPDATE PurchaseRequest SET
                         PurchaseRequestDate = @PurchaseRequestDate,
                         SpecialRemarks = @SpecialRemarks,
                         RequiredDate = @RequiredDate,
                         CreatedBy = @CreatedBy,
                         CreatedDate = @CreatedDate
                         WHERE PurchaseRequestId = @DirectPurchaseRequestId";
             int    id    = connection.Execute(query, model, txn);
             if (id <= 0)
             {
                 throw new Exception();
             }
             id = new DirectPurchaseItemRepository().UpdatePurchaseIndentItem(model, connection, txn);
             if (id <= 0)
             {
                 throw new Exception();
             }
             txn.Commit();
             return(id);
         }
         catch (Exception)
         {
             txn.Rollback();
             throw;
         }
     }
 }
        public string InsertDirectPurchaseRequest(DirectPurchaseRequest model)
        {
            int id = 0;

            using (IDbConnection connection = OpenConnection(dataConnection))
            {
                IDbTransaction txn = connection.BeginTransaction();
                try
                {
                    var internalId = DatabaseCommonRepository.GetNewDocNo(connection, model.OrganizationId, 10, true, txn);
                    model.PurchaseRequestNo = internalId;


                    if (model.SoOrJc == "JC")
                    {
                        model.SaleOrderId = null;
                    }
                    else if (model.SoOrJc == "SO")
                    {
                        model.JobCardId = null;
                    }
                    else
                    {
                        model.isStockOrder = true;
                    }
                    model.TotalAmount = model.items.Sum(m => (m.Quantity * m.Rate));
                    string sql = @"INSERT INTO DirectPurchaseRequest
                                (
	                                [PurchaseRequestNo],
	                                [PurchaseRequestDate],
	                                [SpecialRemarks],
	                                [RequiredDate],
	                                [TotalAmount],
	                                [CreatedBy],
	                                [CreatedDate],
	                                [OrganizationId],
	                                [isActive],
	                                [isApproved],
                                    [SaleOrderId],
                                    [JobCardId],
                                    [isStockOrder]
                                )
                                VALUES
                                (
	                                @PurchaseRequestNo,
                                    @PurchaseRequestDate,
                                    @SpecialRemarks,
                                    @RequiredDate,
                                    @TotalAmount,
                                    @CreatedBy,
                                    @CreatedDate,
                                    @OrganizationId,
                                    1,
                                    0,
                                    @SaleOrderId,
                                    @JobCardId,
                                    @isStockOrder
                                )
                            SELECT CAST(SCOPE_IDENTITY() AS INT)";
                    id = connection.Query <int>(sql, model, txn).Single <int>();

                    var supplyorderitemrepo = new DirectPurchaseItemRepository();
                    foreach (var item in model.items)
                    {
                        item.DirectPurchaseRequestId = id;
                        new DirectPurchaseItemRepository().InsertDirectPurchaseRequestItem(item, connection, txn);
                    }
                    InsertLoginHistory(dataConnection, model.CreatedBy, "Create", "Direct Purchase", id.ToString(), "0");
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                    txn.Rollback();
                    throw;
                }
                return(model.PurchaseRequestNo);
            }
        }